1 package OpenSRF::Application;
2 use vars qw/$_app $log @_METHODS $thunk//;
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
17 $log = 'OpenSRF::Utils::Logger';
20 our @pending_requests;
25 $thunk = $flag if (defined $flag);
29 sub application_implementation {
37 $log->error( "Error loading application_implementation: $app -> $@", ERROR);
46 my ($self, $session, $app_msg) = @_;
52 $log->debug( "In Application::handler()", DEBUG );
54 my $app = $self->application_implementation;
57 $log->debug( "Application is $app", DEBUG);
59 $log->debug( "Message is ".$app_msg->toString(1), INTERNAL);
62 if ($session->last_message_type eq 'REQUEST') {
63 $log->debug( "We got a REQUEST: ". $app_msg->method, INFO );
65 my $method_name = $app_msg->method;
66 $log->debug( " * Looking up $method_name inside $app", DEBUG);
68 my $method_proto = $session->last_message_api_level;
69 $log->debug( " * Method API Level [$method_proto]", DEBUG);
71 my $coderef = $app->method_lookup( $method_name, $method_proto );
74 $session->status( OpenSRF::DomainObject::oilsMethodException->new() );
78 $log->debug( " (we got coderef $coderef", DEBUG);
80 unless ($session->continue_request) {
82 OpenSRF::DomainObject::oilsConnectStatus->new(
83 statusCode => STATUS_REDIRECTED(),
84 status => 'Disconnect on max requests' ) );
90 my @args = $app_msg->params;
91 my $appreq = OpenSRF::AppRequest->new( $session );
93 $log->debug( "in_request = $in_request : [" . $appreq->threadTrace."]", DEBUG );
95 $log->debug( "Pushing onto pending requests: " . $appreq->threadTrace, DEBUG );
96 push @pending_requests, [ $appreq, \@args, $coderef ];
103 $log->debug( "Executing coderef for {$method_name -> ".join(', ', @args)."}", INTERNAL );
108 $resp = $coderef->run( $appreq, @args);
109 my $time = sprintf '%.3f', time() - $start;
110 $log->debug( "Method duration for {$method_name -> ".join(', ', @args)."}: ". $time, DEBUG );
112 $log->debug( "Calling respond_complete: ". $resp->toString(), INTERNAL );
113 $appreq->respond_complete( $resp );
115 $appreq->status( OpenSRF::DomainObject::oilsConnectStatus->new(
116 statusCode => STATUS_COMPLETE(),
117 status => 'Request Complete' ) );
121 $e = $e->{-text} || $e->message if (ref $e);
122 my $sess_id = $session->session_id;
124 OpenSRF::DomainObject::oilsMethodException->new(
125 statusCode => STATUS_INTERNALSERVERERROR(),
126 status => " *** Call to [$method_name] failed for session ".
127 "[$sess_id], thread trace [".$appreq->threadTrace."]:\n".$e
134 # ----------------------------------------------
137 # XXX may need this later
138 # $_->[1] = 1 for (@OpenSRF::AppSession::_CLIENT_CACHE);
142 $log->debug( "Pending Requests: " . scalar(@pending_requests), INTERNAL );
144 # cycle through queued requests
145 while( my $aref = shift @pending_requests ) {
150 my $response = $aref->[2]->run( $aref->[0], @{$aref->[1]} );
151 my $time = sprintf '%.3f', time - $start;
152 $log->debug( "Method duration for {[".$aref->[2]->name." -> ".join(', ',@{$aref->[1]}).'}: '.$time, DEBUG );
154 $appreq = $aref->[0];
155 if( ref( $response ) ) {
156 $log->debug( "Calling respond_complete: ". $response->toString(), INTERNAL );
157 $appreq->respond_complete( $response );
159 $appreq->status( OpenSRF::DomainObject::oilsConnectStatus->new(
160 statusCode => STATUS_COMPLETE(),
161 status => 'Request Complete' ) );
163 $log->debug( "Executed: " . $appreq->threadTrace, DEBUG );
167 OpenSRF::DomainObject::oilsMethodException->new(
168 statusCode => STATUS_INTERNALSERVERERROR(),
169 status => "Call to [".$aref->[2]->name."] faild: ".$e->{-text}
178 my $res = OpenSRF::DomainObject::oilsMethodException->new;
179 $session->send('ERROR', $res);
184 $log->debug( "Pushing ". $app_msg->toString ." onto queue", INTERNAL );
185 $session->push_queue([ $app_msg, $session->last_threadTrace ]);
188 $session->last_message_type('');
189 $session->last_message_api_level('');
194 sub register_method {
196 my $app = ref($self) || $self;
200 throw OpenSRF::DomainObject::oilsMethodException unless ($args{method});
202 $args{api_level} ||= 1;
204 $args{package} ||= $app;
205 $args{api_name} ||= UnixServer->app() . '.' . $args{method};
206 $args{server_class} ||= UnixServer->app();
207 $args{code} ||= \&{$app . '::' . $args{method}};
209 $_METHODS[$args{api_level}]{$args{api_name}} = bless \%args => $app;
211 __PACKAGE__->register_method(
213 api_name => $args{api_name}.'.atomic',
214 method => 'make_stream_atomic'
224 # this instead of " || 1;" above to allow api_level 0
225 $proto = 1 unless (defined $proto);
227 my $class = ref($self) || $self;
229 $log->debug("Lookup of [$method] by [$class]", DEBUG);
230 $log->debug("Available methods\n".Dumper(\%_METHODS), INTERNAL);
233 if (__PACKAGE__->thunk) {
234 for my $p ( reverse(1 .. $proto) ) {
235 if (exists $_METHODS[$p]{$method}) {
236 $meth = $_METHODS[$p]{$method};
240 if (exists $_METHODS[$proto]{$method}) {
241 $meth = $_METHODS[$proto]{$method};
246 $log->debug("Looks like we found [$method]!", DEBUG);
247 $log->debug("Method object is ".Dumper($meth), INTERNAL);
258 if ( !ref($req) || !UNIVERSAL::isa($req, 'OpenSRF::AppRequest') ) {
259 $log->debug("Creating a SubRequest object", DEBUG);
261 $req = OpenSRF::AppSubrequest->new;
263 $log->debug("This is a top level request", DEBUG);
266 my $resp = $self->{code}->($self, $req, @_);
268 if ( ref($req) and UNIVERSAL::isa($req, 'OpenSRF::AppSubrequest') ) {
269 $req->respond($resp) if ($resp);
270 for my $r ( $req->responses ) {
271 $log->debug("A SubRequest object is responding with $r", DEBUG);
273 return $req->responses;
275 $log->debug("A top level Request object is responding $resp", DEBUG);
285 for my $api_level ( 1 .. $#_METHODS ) {
286 $client->respond( { $api_level => $_METHODS[$api_level] } );
291 __PACKAGE__->register_method(
293 method => 'introspect',
294 api_name => 'opensrf.system.method_list'
297 sub make_stream_atomic {
302 (my $m_name = $self->api_name) =~ s/\.atomic$//o;
303 my @results = $self->method_lookup($m_name)->run(@args);