| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | package OSGi::Osgish; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use Term::ANSIColor qw(:constants); | ||
| 7 | use OSGi::Osgish::Shell; | ||
| 8 | use OSGi::Osgish::ServerHandler; | ||
| 9 | use OSGi::Osgish::CompletionHandler; | ||
| 10 | use OSGi::Osgish::CommandHandler; | ||
| 11 | use Data::Dumper; | ||
| 12 | use vars qw($VERSION); | ||
| 13 | |||
| 14 | $VERSION = "0.3.1"; | ||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | OSGi::Osgish - Main osgish object | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | This object is pushed to commands and allows access to all relevant | ||
| 23 | informations shared between commands. A command should consult the | ||
| 24 | osgish object when performing its operation for contacting the OSGi server. | ||
| 25 | The osgish object gets updated in the background e.g. when the server changes. | ||
| 26 | |||
| 27 | =head1 METHODS | ||
| 28 | |||
| 29 | =over | ||
| 30 | |||
| 31 | =item $osgish = new OSGi::Osgish(agent => $agent,...) | ||
| 32 | |||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | sub new { | ||
| 37 | my $class = shift; | ||
| 38 | my $self = ref($_[0]) eq "HASH" ? $_[0] : { @_ }; | ||
| 39 | bless $self,(ref($class) || $class); | ||
| 40 | $self->_init(); | ||
| 41 | return $self; | ||
| 42 | } | ||
| 43 | |||
| 44 | |||
| 45 | =item $agent = $osgish->agent | ||
| 46 | |||
| 47 | Access to the agent object for accessing to the connected server. If there | ||
| 48 | is no connected server, this methods returns C<undef> | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | sub agent { | ||
| 53 | my ($self,$val) = @_; | ||
| 54 | my $ret = $self->{agent}; | ||
| 55 | if ($#_ > 0) { | ||
| 56 | $self->{agent} = $val; | ||
| 57 | } | ||
| 58 | return $ret; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub complete { | ||
| 62 | return shift->{complete}; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub commands { | ||
| 66 | return shift->{commands}; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub servers { | ||
| 70 | return shift->{servers}; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub server { | ||
| 74 | return shift->{servers}->{server}; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub color { | ||
| 78 | return shift->{shell}->color(@_); | ||
| 79 | } | ||
| 80 | |||
| 81 | sub run { | ||
| 82 | my $self = shift; | ||
| 83 | $self->{shell}->run; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub last_error { | ||
| 87 | my $self = shift; | ||
| 88 | my $osgi = $self->agent; | ||
| 89 | return $osgi->last_error if $osgi && $osgi->last_error; | ||
| 90 | return $self->{last_error}; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub _init { | ||
| 94 | my $self = shift; | ||
| 95 | $self->{complete} = new OSGi::Osgish::CompletionHandler($self); | ||
| 96 | $self->{servers} = new OSGi::Osgish::ServerHandler($self); | ||
| 97 | my $shell = $self->_create_shell; | ||
| 98 | $self->{shell} = $shell; | ||
| 99 | my $no_color_prompt = $shell->readline ne "Term::ReadLine::Gnu"; | ||
| 100 | $self->{commands} = new OSGi::Osgish::CommandHandler($self,$self->{shell},no_color_prompt => $no_color_prompt); | ||
| 101 | } | ||
| 102 | |||
| 103 | sub _create_shell { | ||
| 104 | my $self = shift; | ||
| 105 | my $use_color; | ||
| 106 | if (exists $self->{args}->{color}) { | ||
| 107 | $use_color = $self->{args}->{color}; | ||
| 108 | } elsif (exists $self->{config}->{use_color}) { | ||
| 109 | $use_color = $self->{config}->{use_color}; | ||
| 110 | } else { | ||
| 111 | $use_color = "yes"; | ||
| 112 | } | ||
| 113 | return new OSGi::Osgish::Shell(use_color => $use_color =~ /(yes|true|on)$/); | ||
| 114 | } | ||
| 115 | |||
| 116 | =head1 LICENSE | ||
| 117 | |||
| 118 | This file is part of osgish. | ||
| 119 | |||
| 120 | Osgish is free software: you can redistribute it and/or modify | ||
| 121 | it under the terms of the GNU General Public License as published by | ||
| 122 | the Free Software Foundation, either version 2 of the License, or | ||
| 123 | (at your option) any later version. | ||
| 124 | |||
| 125 | osgish is distributed in the hope that it will be useful, | ||
| 126 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 127 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 128 | GNU General Public License for more details. | ||
| 129 | |||
| 130 | You should have received a copy of the GNU General Public License | ||
| 131 | along with osgish. If not, see <http://www.gnu.org/licenses/>. | ||
| 132 | |||
| 133 | A commercial license is available as well. Please contact roland@cpan.org for | ||
| 134 | further details. | ||
| 135 | |||
| 136 | =head1 PROFESSIONAL SERVICES | ||
| 137 | |||
| 138 | Just in case you need professional support for this module (or JMX or OSGi in | ||
| 139 | general), you might want to have a look at www.consol.com Contact | ||
| 140 | roland.huss@consol.de for further information (or use the contact form at | ||
| 141 | http://www.consol.com/contact/) | ||
| 142 | |||
| 143 | =head1 AUTHOR | ||
| 144 | |||
| 145 | roland@cpan.org | ||
| 146 | |||
| 147 | =cut | ||
| 148 | |||
| 149 | 1; | ||
| 150 | |||
| 151 | |||
| 152 | 1; | ||
| 153 | |||
| 154 | |||
| 155 |