1 package OpenSRF::Application;
3 use vars qw/$_app $log @_METHODS $thunk $server_class/;
6 use OpenSRF::AppSession;
7 use OpenSRF::DomainObject::oilsMethod;
8 use OpenSRF::DomainObject::oilsResponse qw/:status/;
9 use OpenSRF::Utils::Logger qw/:level $logger/;
11 use Time::HiRes qw/time/;
12 use OpenSRF::EX qw/:try/;
14 use OpenSRF::Utils::JSON;
21 $log = 'OpenSRF::Utils::Logger';
24 our @pending_requests;
29 return 1 unless ref($self);
30 return $self->{package};
35 return 0 unless ref($self);
36 return $self->{signature};
41 return 0 unless ref($self);
42 return $self->{strict};
47 return 0 unless ref($self);
53 return 0 unless ref($self);
54 return $self->{max_bundle_size} if (defined($self->{max_bundle_size}));
58 sub max_bundle_count {
60 return 0 unless ref($self);
61 return $self->{max_bundle_count} || 0;
66 return 0 unless ref($self);
67 return $self->{max_chunk_size} if (defined($self->{max_chunk_size}));
68 return $self->max_bundle_size * 2;
73 return 1 unless ref($self);
74 return $self->{api_name};
79 return 1 unless ref($self);
80 return $self->{api_level};
88 $self->{session} = $session;
90 return $self->{session};
96 $server_class = $class;
104 $thunk = $flag if (defined $flag);
108 sub application_implementation {
116 $log->error( "Error loading application_implementation: $app -> $@", ERROR);
125 my ($self, $session, $app_msg) = @_;
131 my $app = $self->application_implementation;
133 if ($session->last_message_type eq 'REQUEST') {
135 my @p = $app_msg->params;
136 my $method_name = $app_msg->method;
137 my $method_proto = $session->last_message_api_level;
139 # By default, we log all method params at the info level
140 # Here we are consult our shared portion of the config file
141 # to look for any exceptions to this behavior
142 my $logdata = "CALL: ".$session->service." $method_name ";
143 my $redact_params = 0;
145 if (ref($shared_conf->shared->log_protect) eq 'ARRAY') {
146 foreach my $match_string (@{$shared_conf->shared->log_protect}) {
147 if ($method_name =~ /^$match_string/) {
153 if ($redact_params) {
154 $logdata .= "**PARAMS REDACTED**";
156 $logdata .= join(', ', map { (defined $_) ? $_ : '' } @p);
159 $log->info($logdata);
161 my $coderef = $app->method_lookup( $method_name, $method_proto, 1, 1 );
164 $session->status( OpenSRF::DomainObject::oilsMethodException->new(
165 statusCode => STATUS_NOTFOUND(),
166 status => "Method [$method_name] not found for $app"));
170 unless ($session->continue_request) {
172 OpenSRF::DomainObject::oilsConnectStatus->new(
173 statusCode => STATUS_REDIRECTED(),
174 status => 'Disconnect on max requests' ) );
180 my @args = $app_msg->params;
181 $coderef->session( $session );
182 my $appreq = OpenSRF::AppRequest->new( $session );
183 $appreq->max_bundle_size( $coderef->max_bundle_size );
184 $appreq->max_bundle_count( $coderef->max_bundle_count );
185 $appreq->max_chunk_size( $coderef->max_chunk_size );
187 $log->debug( "in_request = $in_request : [" . $appreq->threadTrace."]", INTERNAL );
189 $log->debug( "Pushing onto pending requests: " . $appreq->threadTrace, DEBUG );
190 push @pending_requests, [ $appreq, \@args, $coderef ];
197 $log->debug( "Executing coderef for {$method_name}", INTERNAL );
201 # un-if(0) this block to enable param checking based on signature and argc
202 if ($coderef->strict) {
203 if (@args < $coderef->argc) {
204 die "Not enough params passed to ".
205 $coderef->api_name." : requires ". $coderef->argc
208 my $sig = $coderef->signature;
209 if ($sig && exists $sig->{params}) {
210 for my $p (0 .. scalar(@{ $sig->{params} }) - 1 ) {
211 my $s = $sig->{params}->[$p];
213 if ($s->{class} && OpenSRF::Utils::JSON->lookup_hint(ref $a) ne $s->{class}) {
214 die "Incorrect param class at position $p : should be a '$$s{class}'";
215 } elsif ($s->{type}) {
216 if (lc($s->{type}) eq 'object' && $a !~ /HASH/o) {
217 die "Incorrect param type at position $p : should be an 'object'";
218 } elsif (lc($s->{type}) eq 'array' && $a !~ /ARRAY/o) {
219 die "Incorrect param type at position $p : should be an 'array'";
220 } elsif (lc($s->{type}) eq 'number' && (ref($a) || $a !~ /^-?\d+(?:\.\d+)?$/o)) {
221 die "Incorrect param type at position $p : should be a 'number'";
222 } elsif (lc($s->{type}) eq 'string' && ref($a)) {
223 die "Incorrect param type at position $p : should be a 'string'";
232 $resp = $coderef->run( $appreq, @args);
233 my $time = sprintf '%.3f', time() - $start;
235 $log->debug( "Method duration for [$method_name]: ". $time );
236 $appreq->respond_complete( $resp );
240 warn "Caught error from 'run' method: $e\n";
242 if(UNIVERSAL::isa($e,"Error")) {
243 $e = $e->stringify();
245 my $sess_id = $session->session_id;
247 OpenSRF::DomainObject::oilsMethodException->new(
248 statusCode => STATUS_INTERNALSERVERERROR(),
249 status => " *** Call to [$method_name] failed for session ".
250 "[$sess_id], thread trace ".
251 "[".$appreq->threadTrace."]:\n$e\n"
258 # ----------------------------------------------
261 # XXX may need this later
262 # $_->[1] = 1 for (@OpenSRF::AppSession::_CLIENT_CACHE);
266 $log->debug( "Pending Requests: " . scalar(@pending_requests), INTERNAL );
268 # cycle through queued requests
269 while( my $aref = shift @pending_requests ) {
273 # un-if(0) this block to enable param checking based on signature and argc
275 if (@args < $aref->[2]->argc) {
276 die "Not enough params passed to ".
277 $aref->[2]->api_name." : requires ". $aref->[2]->argc
280 my $sig = $aref->[2]->signature;
281 if ($sig && exists $sig->{params}) {
282 for my $p (0 .. scalar(@{ $sig->{params} }) - 1 ) {
283 my $s = $sig->{params}->[$p];
285 if ($s->{class} && OpenSRF::Utils::JSON->lookup_hint(ref $a) ne $s->{class}) {
286 die "Incorrect param class at position $p : should be a '$$s{class}'";
287 } elsif ($s->{type}) {
288 if (lc($s->{type}) eq 'object' && $a !~ /HASH/o) {
289 die "Incorrect param type at position $p : should be an 'object'";
290 } elsif (lc($s->{type}) eq 'array' && $a !~ /ARRAY/o) {
291 die "Incorrect param type at position $p : should be an 'array'";
292 } elsif (lc($s->{type}) eq 'number' && (ref($a) || $a !~ /^-?\d+(?:\.\d+)?$/o)) {
293 die "Incorrect param type at position $p : should be a 'number'";
294 } elsif (lc($s->{type}) eq 'string' && ref($a)) {
295 die "Incorrect param type at position $p : should be a 'string'";
304 my $response = $aref->[2]->run( $aref->[0], @{$aref->[1]} );
305 my $time = sprintf '%.3f', time - $start;
306 $log->debug( "Method duration for [".$aref->[2]->api_name." -> ".join(', ',@{$aref->[1]}).']: '.$time, DEBUG );
308 $appreq = $aref->[0];
309 $appreq->respond_complete( $response );
310 $log->debug( "Executed: " . $appreq->threadTrace, INTERNAL );
314 if(UNIVERSAL::isa($e,"Error")) {
315 $e = $e->stringify();
318 OpenSRF::DomainObject::oilsMethodException->new(
319 statusCode => STATUS_INTERNALSERVERERROR(),
320 status => "Call to [".$aref->[2]->api_name."] faild: $e"
330 $log->info("Received non-REQUEST message in Application handler");
332 my $res = OpenSRF::DomainObject::oilsMethodException->new(
333 status => "Received non-REQUEST message in Application handler");
334 $session->send('ERROR', $res);
339 $session->push_queue([ $app_msg, $session->last_threadTrace ]);
342 $session->last_message_type('');
343 $session->last_message_api_level('');
350 my $api_name = shift;
351 my $api_level = shift || 1;
352 return exists($_METHODS[$api_level]{$api_name});
356 sub normalize_whitespace {
363 $txt =~ s/\. /\. /gso;
368 sub parse_string_signature {
370 return [] unless $string;
371 my @chunks = split(/\@/smo, $string);
377 if (/^return (.+)$/so) {
378 $ret = [normalize_whitespace($1)];
379 } elsif (/^param (\w+) \b(.+)$/so) {
380 push @params, [ $1, normalize_whitespace($2) ];
382 $desc .= '@' if $desc;
387 return [normalize_whitespace($desc),\@params, $ret];
390 sub parse_array_signature {
392 my ($d,$p,$r) = @$array;
393 return {} unless ($d or $p or $r);
414 sub register_method {
416 my $app = ref($self) || $self;
420 throw OpenSRF::DomainObject::oilsMethodException unless ($args{method});
422 $args{api_level} = 1 unless(defined($args{api_level}));
426 $args{package} ||= $app;
427 $args{server_class} = server_class();
428 $args{api_name} ||= $args{server_class} . '.' . $args{method};
430 # un-if(0) this block to enable signature parsing
431 if (!$args{signature}) {
432 if ($args{notes} && !ref($args{notes})) {
434 parse_array_signature( parse_string_signature( $args{notes} ) );
436 } elsif( !ref($args{signature}) ) {
438 parse_array_signature( parse_string_signature( $args{signature} ) );
439 } elsif( ref($args{signature}) eq 'ARRAY') {
441 parse_array_signature( $args{signature} );
444 unless ($args{object_hint}) {
445 ($args{object_hint} = $args{package}) =~ s/::/_/go;
448 OpenSRF::Utils::JSON->register_class_hint(
449 strip => ['session'],
451 hint => $args{object_hint},
455 $_METHODS[$args{api_level}]{$args{api_name}} = bless \%args => $app;
457 __PACKAGE__->register_method(
460 api_name => $args{api_name}.'.atomic',
461 method => 'make_stream_atomic',
462 notes => "This is a system generated method. Please see the definition for $args{api_name}",
463 ) if ($args{stream});
466 sub retrieve_remote_apis {
468 my $session = OpenSRF::AppSession->create('router');
470 $session->connect or OpenSRF::EX::WARN->throw("Connection to router timed out");
473 $log->debug( "Remote subrequest returned an error:\n". $e );
476 return undef unless ($session->state == $session->CONNECTED);
479 my $req = $session->request( 'opensrf.router.info.class.list' );
480 my $list = $req->recv;
482 if( UNIVERSAL::isa($list,"Error") ) {
486 my $content = $list->content;
490 $session->disconnect;
492 my %u_list = map { ($_ => 1) } @$content;
494 for my $class ( keys %u_list ) {
495 next if($class eq $server_class);
496 populate_remote_method_cache($class, $method);
500 sub populate_remote_method_cache {
504 my $session = OpenSRF::AppSession->create($class);
506 $session->connect or OpenSRF::EX::WARN->throw("Connection to $class timed out");
508 my $call = 'opensrf.system.method.all' unless (defined $meth);
509 $call = 'opensrf.system.method' if (defined $meth);
511 my $req = $session->request( $call, $meth );
513 while (my $method = $req->recv) {
514 next if (UNIVERSAL::isa($method, 'Error'));
516 $method = $method->content;
517 next if ( exists($_METHODS[$$method{api_level}]) &&
518 exists($_METHODS[$$method{api_level}]{$$method{api_name}}) );
519 $method->{remote} = 1;
520 bless($method, __PACKAGE__ );
521 $_METHODS[$$method{api_level}]{$$method{api_name}} = $method;
526 $session->disconnect;
530 $log->debug( "Remote subrequest returned an error:\n". $e );
539 my $no_recurse = shift || 0;
540 my $no_remote = shift || 0;
542 # this instead of " || 1;" above to allow api_level 0
543 $proto = $self->api_level unless (defined $proto);
545 my $class = ref($self) || $self;
547 $log->debug("Lookup of [$method] by [$class] in api_level [$proto]", DEBUG);
548 $log->debug("Available methods\n\t".join("\n\t", keys %{ $_METHODS[$proto] }), INTERNAL);
551 if (__PACKAGE__->thunk) {
552 for my $p ( reverse(1 .. $proto) ) {
553 if (exists $_METHODS[$p]{$method}) {
554 $meth = $_METHODS[$p]{$method};
558 if (exists $_METHODS[$proto]{$method}) {
559 $meth = $_METHODS[$proto]{$method};
564 if($no_remote and $meth->{remote}) {
565 $log->debug("OH CRAP We're not supposed to return remote methods", WARN);
569 } elsif (!$no_recurse) {
570 $log->debug("We didn't find [$method], asking everyone else.", DEBUG);
571 retrieve_remote_apis($method);
572 $meth = $self->method_lookup($method,$proto,1);
575 $meth->session($self->session) if $meth && ref($self); # Pass the caller's session
581 $log->debug("Creating a dispatching SubRequest object", DEBUG);
582 my $req = OpenSRF::AppSubrequest->new( session => $self->session, respond_directly => 1 );
583 return $self->run($req,@_);
593 if ( !UNIVERSAL::isa($req, 'OpenSRF::AppRequest') ) {
594 $log->debug("Creating a SubRequest object", DEBUG);
595 unshift @params, $req;
596 $req = OpenSRF::AppSubrequest->new( session => $self->session );
598 $log->debug("This is a top level request", DEBUG);
601 if (!$self->{remote}) {
602 my $code = \&{$self->{package} . '::' . $self->{method}};
607 $warnhandler = $SIG{__WARN__};
608 $SIG{__WARN__} = sub {
609 (my $msg = shift) =~ s/\n$//;
610 $log->warn($self->{api_name} . ": $msg");
611 return 1; # prevents warning going out to stderr
614 $resp = $code->($self, $req, @params);
619 if( ref($self) eq 'HASH') {
620 $log->error("Sub $$self{package}::$$self{method} DIED!!!\n\t$err\n", ERROR);
623 $SIG{__WARN__} = $warnhandler;
627 if(UNIVERSAL::isa($err,"Error")) {
635 $log->debug("Coderef for [$$self{package}::$$self{method}] has been run", DEBUG);
637 if ( ref($req) and UNIVERSAL::isa($req, 'OpenSRF::AppSubrequest') and !$req->respond_directly ) {
638 $req->respond($resp) if (defined $resp);
639 $log->debug("SubRequest object is responding with : " . join(" ",$req->responses), DEBUG);
640 return $req->responses;
642 $log->debug("A top level Request object is responding $resp", DEBUG) if (defined $resp);
646 my $session = OpenSRF::AppSession->create($self->{server_class});
648 #$session->connect or OpenSRF::EX::WARN->throw("Connection to [$$self{server_class}] timed out");
649 my $remote_req = $session->request( $self->{api_name}, @params );
650 while (my $remote_resp = $remote_req->recv) {
651 OpenSRF::Utils::Logger->debug("Remote Subrequest Received " . $remote_resp, INTERNAL );
652 if( UNIVERSAL::isa($remote_resp,"Error") ) {
655 $req->respond( $remote_resp->content );
657 $remote_req->finish();
661 $log->debug( "Remote subrequest returned an error:\n". $e );
666 $session->disconnect();
670 $log->debug( "Remote Subrequest Responses " . join(" ", $req->responses), INTERNAL );
672 return $req->responses;
674 # huh? how'd we get here...
685 if ($self->api_name =~ /all$/o) {
691 my ($seen,$returned) = (0,0);
692 for my $api_level ( reverse(1 .. $#_METHODS) ) {
693 for my $api_name ( sort keys %{$_METHODS[$api_level]} ) {
694 if (!$offset || $offset <= $seen) {
695 if (!$_METHODS[$api_level]{$api_name}{remote}) {
696 if (defined($method)) {
697 if ($api_name =~ $method) {
698 if (!$limit || $returned < $limit) {
699 $client->respond( $_METHODS[$api_level]{$api_name} );
704 if (!$limit || $returned < $limit) {
705 $client->respond( $_METHODS[$api_level]{$api_name} );
717 __PACKAGE__->register_method(
719 method => 'introspect',
720 api_name => 'opensrf.system.method.all',
723 desc => q/This method is used to introspect an entire OpenSRF Application/,
725 desc => q/A stream of objects describing the methods available via this OpenSRF Application/,
730 __PACKAGE__->register_method(
732 method => 'introspect',
734 api_name => 'opensrf.system.method',
737 desc => q/Use this method to get the definition of a single OpenSRF Method/,
739 { desc => q/The method to introspect/,
742 return => { desc => q/An object describing the method requested, or an error if it can't be found/,
752 $client->respond( $_ ) for (@args);
755 __PACKAGE__->register_method(
757 method => 'echo_method',
759 api_name => 'opensrf.system.echo',
761 desc => q/A test method that will echo back it's arguments in a streaming response/,
763 { desc => q/One or more arguments to echo back/ }
765 return => { desc => q/A stream of the arguments passed/ }
770 my( $self, $conn ) = @_;
773 __PACKAGE__->register_method(
774 method => 'time_method',
776 api_name => 'opensrf.system.time',
778 desc => q/Returns the current system time as epoch seconds/,
779 return => { desc => q/epoch seconds/ }
783 sub make_stream_atomic {
788 (my $m_name = $self->api_name) =~ s/\.atomic$//o;
789 my $m = $self->method_lookup($m_name);
791 $m->session( $req->session );
792 my @results = $m->run(@args);