1 package OpenSRF::Application;
2 use vars qw/$_app $log @_METHODS $thunk $server_class/;
5 use OpenSRF::AppSession;
6 use OpenSRF::DomainObject::oilsMethod;
7 use OpenSRF::DomainObject::oilsResponse qw/:status/;
8 use OpenSRF::Utils::Logger qw/:level/;
10 use Time::HiRes qw/time/;
11 use OpenSRF::EX qw/:try/;
12 #use OpenSRF::UnixServer; # to get the server class from UnixServer::App
19 $log = 'OpenSRF::Utils::Logger';
22 our @pending_requests;
27 $server_class = $class;
35 $thunk = $flag if (defined $flag);
39 sub application_implementation {
47 $log->error( "Error loading application_implementation: $app -> $@", ERROR);
56 my ($self, $session, $app_msg) = @_;
62 $log->debug( "In Application::handler()", DEBUG );
64 my $app = $self->application_implementation;
67 $log->debug( "Application is $app", DEBUG);
69 $log->debug( "Message is ".$app_msg->toString(1), INTERNAL);
72 if ($session->last_message_type eq 'REQUEST') {
73 $log->debug( "We got a REQUEST: ". $app_msg->method, INFO );
75 my $method_name = $app_msg->method;
76 $log->debug( " * Looking up $method_name inside $app", DEBUG);
78 my $method_proto = $session->last_message_api_level;
79 $log->debug( " * Method API Level [$method_proto]", DEBUG);
81 my $coderef = $app->method_lookup( $method_name, $method_proto, 1, 1 );
84 $session->status( OpenSRF::DomainObject::oilsMethodException->new() );
88 $log->debug( " (we got coderef $coderef", DEBUG);
90 unless ($session->continue_request) {
92 OpenSRF::DomainObject::oilsConnectStatus->new(
93 statusCode => STATUS_REDIRECTED(),
94 status => 'Disconnect on max requests' ) );
100 my @args = $app_msg->params;
101 my $appreq = OpenSRF::AppRequest->new( $session );
103 $log->debug( "in_request = $in_request : [" . $appreq->threadTrace."]", DEBUG );
105 $log->debug( "Pushing onto pending requests: " . $appreq->threadTrace, DEBUG );
106 push @pending_requests, [ $appreq, \@args, $coderef ];
113 $log->debug( "Executing coderef for {$method_name -> ".join(', ', @args)."}", INTERNAL );
118 $resp = $coderef->run( $appreq, @args);
119 my $time = sprintf '%.3f', time() - $start;
120 $log->debug( "Method duration for {$method_name -> ".join(', ', @args)."}: ". $time, DEBUG );
121 if( defined( $resp ) ) {
122 #$log->debug( "Calling respond_complete: ". $resp->toString(), INTERNAL );
123 $appreq->respond_complete( $resp );
125 $appreq->status( OpenSRF::DomainObject::oilsConnectStatus->new(
126 statusCode => STATUS_COMPLETE(),
127 status => 'Request Complete' ) );
131 $e = $e->{-text} || $e->message if (ref $e);
132 my $sess_id = $session->session_id;
134 OpenSRF::DomainObject::oilsMethodException->new(
135 statusCode => STATUS_INTERNALSERVERERROR(),
136 status => " *** Call to [$method_name] failed for session ".
137 "[$sess_id], thread trace [".$appreq->threadTrace."]:\n".$e
144 # ----------------------------------------------
147 # XXX may need this later
148 # $_->[1] = 1 for (@OpenSRF::AppSession::_CLIENT_CACHE);
152 $log->debug( "Pending Requests: " . scalar(@pending_requests), INTERNAL );
154 # cycle through queued requests
155 while( my $aref = shift @pending_requests ) {
160 my $response = $aref->[2]->run( $aref->[0], @{$aref->[1]} );
161 my $time = sprintf '%.3f', time - $start;
162 $log->debug( "Method duration for {[".$aref->[2]->name." -> ".join(', ',@{$aref->[1]}).'}: '.$time, DEBUG );
164 $appreq = $aref->[0];
165 if( ref( $response ) ) {
166 $log->debug( "Calling respond_complete: ". $response->toString(), INTERNAL );
167 $appreq->respond_complete( $response );
169 $appreq->status( OpenSRF::DomainObject::oilsConnectStatus->new(
170 statusCode => STATUS_COMPLETE(),
171 status => 'Request Complete' ) );
173 $log->debug( "Executed: " . $appreq->threadTrace, DEBUG );
177 OpenSRF::DomainObject::oilsMethodException->new(
178 statusCode => STATUS_INTERNALSERVERERROR(),
179 status => "Call to [".$aref->[2]->name."] faild: ".$e->{-text}
188 my $res = OpenSRF::DomainObject::oilsMethodException->new;
189 $session->send('ERROR', $res);
194 $log->debug( "Pushing ". $app_msg->toString ." onto queue", INTERNAL );
195 $session->push_queue([ $app_msg, $session->last_threadTrace ]);
198 $session->last_message_type('');
199 $session->last_message_api_level('');
204 sub register_method {
206 my $app = ref($self) || $self;
210 throw OpenSRF::DomainObject::oilsMethodException unless ($args{method});
212 $args{api_level} ||= 1;
215 $args{package} = $app;
216 $args{server_class} = server_class();
217 $args{api_name} ||= $args{server_class} . '.' . $args{method};
219 $_METHODS[$args{api_level}]{$args{api_name}} = bless \%args => $app;
221 __PACKAGE__->register_method(
223 api_name => $args{api_name}.'.atomic',
224 method => 'make_stream_atomic'
225 ) if ($args{stream});
228 sub retrieve_remote_apis {
229 my $session = OpenSRF::AppSession->create('settings');
231 $session->connect or OpenSRF::EX::WARN->throw("Connection to settings timed out");
234 $log->debug( "Remote subrequest returned an error:\n". $e );
237 return undef unless ($session->state == $session->CONNECTED);
240 my $req = $session->request( 'opensrf.settings.xpath.get', '//activeapps/appname' );
241 my $list = $req->recv;
243 if( UNIVERSAL::isa($list,"Error") ) {
247 my $content = $list->content;
251 $session->disconnect;
253 my %u_list = map { ($_ => 1) } @$content;
255 for my $class ( keys %u_list ) {
256 next if($class eq $server_class);
257 populate_remote_method_cache($class);
261 sub populate_remote_method_cache {
264 my $session = OpenSRF::AppSession->create($class);
266 $session->connect or OpenSRF::EX::WARN->throw("Connection to $class timed out");
268 my $req = $session->request( 'opensrf.system.method.all' );
270 while (my $method = $req->recv) {
271 next if (UNIVERSAL::isa($method, 'Error'));
273 $method = $method->content;
274 next if ( exists($_METHODS[$$method{api_level}]) &&
275 exists($_METHODS[$$method{api_level}]{$$method{api_name}}) );
276 $method->{remote} = 1;
277 bless($method, __PACKAGE__ );
278 $_METHODS[$$method{api_level}]{$$method{api_name}} = $method;
283 $session->disconnect;
287 $log->debug( "Remote subrequest returned an error:\n". $e );
296 my $no_recurse = shift || 0;
297 my $no_remote = shift || 0;
299 # this instead of " || 1;" above to allow api_level 0
300 $proto = 1 unless (defined $proto);
302 my $class = ref($self) || $self;
304 $log->debug("Lookup of [$method] by [$class]", DEBUG);
305 $log->debug("Available methods\n".Dumper(\@_METHODS), INTERNAL);
308 if (__PACKAGE__->thunk) {
309 for my $p ( reverse(1 .. $proto) ) {
310 if (exists $_METHODS[$p]{$method}) {
311 $meth = $_METHODS[$p]{$method};
315 if (exists $_METHODS[$proto]{$method}) {
316 $meth = $_METHODS[$proto]{$method};
321 $log->debug("Looks like we found [$method]!", DEBUG);
322 $log->debug("Method object is ".Dumper($meth), INTERNAL);
323 if($no_remote and $meth->{remote}) {
324 $log->debug("OH CRAP We're not supposed to return remote methods", WARN);
328 } elsif (!$no_recurse) {
329 retrieve_remote_apis();
330 $meth = $self->method_lookup($method,$proto,1);
343 if ( !UNIVERSAL::isa($req, 'OpenSRF::AppRequest') ) {
344 $log->debug("Creating a SubRequest object", DEBUG);
345 unshift @params, $req;
346 $req = OpenSRF::AppSubrequest->new;
348 $log->debug("This is a top level request", DEBUG);
351 if (!$self->{remote}) {
352 my $code ||= \&{$self->{package} . '::' . $self->{method}};
353 $resp = $code->($self, $req, @params);
355 if ( ref($req) and UNIVERSAL::isa($req, 'OpenSRF::AppSubrequest') ) {
356 $req->respond($resp) if (defined $resp);
357 $log->debug("A SubRequest object is responding " . join(" ",$req->responses), DEBUG);
358 return $req->responses;
360 $log->debug("A top level Request object is responding $resp", DEBUG);
364 my $session = OpenSRF::AppSession->create($self->{server_class});
366 $session->connect or OpenSRF::EX::WARN->throw("Connection to [$$self{server_class}] timed out");
367 my $remote_req = $session->request( $self->{api_name}, @params );
368 while (my $remote_resp = $remote_req->recv) {
369 OpenSRF::Utils::Logger->debug("Remote Subrequest Received " . $remote_resp, INTERNAL );
370 if( UNIVERSAL::isa($remote_resp,"Error") ) {
373 $req->respond( $remote_resp->content );
375 $remote_req->finish();
380 $log->debug( "Remote subrequest returned an error:\n". $e );
384 $log->debug( "Remote Subrequest Responses " . join(" ", $req->responses), INTERNAL );
386 return $req->responses;
388 # huh? how'd we get here...
397 $method = undef if ($self->{api_name} =~ /all$/o);
399 for my $api_level ( 1 .. $#_METHODS ) {
400 for my $api_name ( sort keys %{$_METHODS[$api_level]} ) {
401 if (!$_METHODS[$api_level]{$api_name}{remote}) {
402 if (defined($method)) {
403 if ($api_name =~ /$method/) {
404 $client->respond( $_METHODS[$api_level]{$api_name} );
407 $client->respond( $_METHODS[$api_level]{$api_name} );
415 __PACKAGE__->register_method(
417 method => 'introspect',
418 api_name => 'opensrf.system.method.all'
421 __PACKAGE__->register_method(
423 method => 'introspect',
425 api_name => 'opensrf.system.method'
428 sub make_stream_atomic {
433 (my $m_name = $self->api_name) =~ s/\.atomic$//o;
434 my @results = $self->method_lookup($m_name)->run(@args);