| 1 | #!/usr/bin/perl | ||
| 2 | package OSGi::Osgish::ServerHandler; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use Term::ANSIColor qw(:constants); | ||
| 6 | use OSGi::Osgish::Agent; | ||
| 7 | use Data::Dumper; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | OSGi::Osgish::ServerHandler - Handler for coordinating the server access | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | =head1 METHODS | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | sub new { | ||
| 22 | my $class = shift; | ||
| 23 | my $osgish = shift || die "No osgish object given"; | ||
| 24 | my $self = { | ||
| 25 | osgish => $osgish | ||
| 26 | }; | ||
| 27 | bless $self,(ref($class) || $class); | ||
| 28 | my $server = $self->_init_server_list($osgish->{initial_server},$osgish); | ||
| 29 | $self->connect_to_server($server) if $server; | ||
| 30 | return $self; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub connect_to_server { | ||
| 34 | my $self = shift; | ||
| 35 | my $server = shift; | ||
| 36 | my $name = shift; | ||
| 37 | |||
| 38 | my $server_map = $self->{server_map}; | ||
| 39 | my $s = $server_map->{$server}; | ||
| 40 | unless ($s) { | ||
| 41 | unless ($server =~ m|^\w+://[\w:]+/|) { | ||
| 42 | print "Invalid URL $server\n"; | ||
| 43 | return; | ||
| 44 | } | ||
| 45 | $name ||= $self->_prepare_server_name($server); | ||
| 46 | my $entry = { name => $name, url => $server }; | ||
| 47 | push @{$self->{server_list}},$entry; | ||
| 48 | $self->{server_map}->{$name} = $entry; | ||
| 49 | $s = $entry; | ||
| 50 | } | ||
| 51 | my $osgish = $self->{osgish}; | ||
| 52 | my ($old_server,$old_agent) = ($self->server,$osgish->agent); | ||
| 53 | eval { | ||
| 54 | my $agent = $self->_create_agent($s->{name}) || die "Unknown $server (not an alias nor a proper URL).\n";; | ||
| 55 | $agent->init(); | ||
| 56 | $osgish->agent($agent); | ||
| 57 | $self->{server} = $s->{name}; | ||
| 58 | $osgish->{last_error} = undef; | ||
| 59 | }; | ||
| 60 | if ($@) { | ||
| 61 | if ($osgish->agent && $osgish->agent->last_error) { | ||
| 62 | $osgish->{last_error} = $osgish->agent->last_error; | ||
| 63 | } else { | ||
| 64 | $osgish->{last_error} = $@; | ||
| 65 | } | ||
| 66 | $self->{server} = $old_server; | ||
| 67 | $osgish->agent($old_agent); | ||
| 68 | die $@; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | sub server { | ||
| 73 | return shift->{server}; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub list { | ||
| 77 | my $self = shift; | ||
| 78 | return $self->{server_list}; | ||
| 79 | } | ||
| 80 | |||
| 81 | |||
| 82 | sub _init_server_list { | ||
| 83 | my $self = shift; | ||
| 84 | my $server = shift; | ||
| 85 | my $context = shift; | ||
| 86 | my $config = $context->{config}; | ||
| 87 | my $args = $context->{args}; | ||
| 88 | my @servers = map { { name => $_->{name}, url => $_->{url}, from_config => 1 } } @{$config->get_servers}; | ||
| 89 | my $ret_server; | ||
| 90 | if ($server) { | ||
| 91 | my $config_s = $config->get_server_config($server); | ||
| 92 | if ($config_s) { | ||
| 93 | my $found = 0; | ||
| 94 | my $i = 0; | ||
| 95 | my $entry = { name => $server, url => $config_s->{url}, from_config => 1 } ; | ||
| 96 | for my $s (@servers) { | ||
| 97 | if ($s->{name} eq $server) { | ||
| 98 | $servers[$i] = $entry; | ||
| 99 | $found = 1; | ||
| 100 | last; | ||
| 101 | } | ||
| 102 | $i++; | ||
| 103 | } | ||
| 104 | push @servers,$entry unless $found; | ||
| 105 | $ret_server = $config_s->{name}; | ||
| 106 | } else { | ||
| 107 | die "Invalid URL ",$server,"\n" unless ($server =~ m|^\w+://|); | ||
| 108 | my $name = $self->_prepare_server_name($server); | ||
| 109 | push @servers,{ name => $name, url => $server }; | ||
| 110 | $ret_server = $name; | ||
| 111 | } | ||
| 112 | } | ||
| 113 | $self->{server_list} = \@servers; | ||
| 114 | $self->{server_map} = { map { $_->{name} => $_ } @servers }; | ||
| 115 | return $ret_server; | ||
| 116 | } | ||
| 117 | |||
| 118 | # ========================================================================================= | ||
| 119 | |||
| 120 | sub _prepare_server_name { | ||
| 121 | my $self = shift; | ||
| 122 | my $url = shift; | ||
| 123 | if ($url =~ m|^\w+://([^/]+)/?|) { | ||
| 124 | return $1; | ||
| 125 | } else { | ||
| 126 | return $url; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | sub _create_agent { | ||
| 131 | my $self = shift; | ||
| 132 | my $server = shift; | ||
| 133 | return undef unless $server; | ||
| 134 | # TODO: j4p_args, jmx_config; | ||
| 135 | my $osgish = $self->{osgish}; | ||
| 136 | my $j4p_args = $self->_j4p_args($osgish->{args} || {}); | ||
| 137 | my $jmx_config = $osgish->{config} || {}; | ||
| 138 | my $sc = $self->{server_map}->{$server}; | ||
| 139 | return undef unless $sc; | ||
| 140 | if ($sc->{from_config}) { | ||
| 141 | return new OSGi::Osgish::Agent({ %$j4p_args, server => $server, config => $jmx_config}); | ||
| 142 | } else { | ||
| 143 | return new OSGi::Osgish::Agent({ %$j4p_args, url => $sc->{url}}); | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | sub _j4p_args { | ||
| 148 | my $self = shift; | ||
| 149 | my $o = shift; | ||
| 150 | my $ret = { }; | ||
| 151 | |||
| 152 | for my $arg (qw(user password)) { | ||
| 153 | if (defined($o->{$arg})) { | ||
| 154 | $ret->{$arg} = $o->{$arg}; | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | if (defined($o->{proxy})) { | ||
| 159 | my $proxy = {}; | ||
| 160 | $proxy->{url} = $o->{proxy}; | ||
| 161 | for my $k (qw(proxy-user proxy-password)) { | ||
| 162 | $proxy->{$k} = defined($o->{$k}) if $o->{$k}; | ||
| 163 | } | ||
| 164 | $ret->{proxy} = $proxy; | ||
| 165 | } | ||
| 166 | if (defined($o->{target})) { | ||
| 167 | $ret->{target} = { | ||
| 168 | url => $o->{target}, | ||
| 169 | $o->{'target-user'} ? (user => $o->{'target-user'}) : (), | ||
| 170 | $o->{'target-password'} ? (password => $o->{'target-password'}) : (), | ||
| 171 | }; | ||
| 172 | } | ||
| 173 | return $ret; | ||
| 174 | } | ||
| 175 | |||
| 176 | =head1 LICENSE | ||
| 177 | |||
| 178 | This file is part of osgish. | ||
| 179 | |||
| 180 | Osgish is free software: you can redistribute it and/or modify | ||
| 181 | it under the terms of the GNU General Public License as published by | ||
| 182 | the Free Software Foundation, either version 2 of the License, or | ||
| 183 | (at your option) any later version. | ||
| 184 | |||
| 185 | osgish is distributed in the hope that it will be useful, | ||
| 186 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 187 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 188 | GNU General Public License for more details. | ||
| 189 | |||
| 190 | You should have received a copy of the GNU General Public License | ||
| 191 | along with osgish. If not, see <http://www.gnu.org/licenses/>. | ||
| 192 | |||
| 193 | A commercial license is available as well. Please contact roland@cpan.org for | ||
| 194 | further details. | ||
| 195 | |||
| 196 | =head1 PROFESSIONAL SERVICES | ||
| 197 | |||
| 198 | Just in case you need professional support for this module (or JMX or OSGi in | ||
| 199 | general), you might want to have a look at www.consol.com Contact | ||
| 200 | roland.huss@consol.de for further information (or use the contact form at | ||
| 201 | http://www.consol.com/contact/) | ||
| 202 | |||
| 203 | =head1 AUTHOR | ||
| 204 | |||
| 205 | roland@cpan.org | ||
| 206 | |||
| 207 | =cut | ||
| 208 | |||
| 209 | 1; | ||
| 210 |