]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/perl/lib/OpenSRF/Application.pm
always use respond_complete to finish a request, whether there is a final message...
[OpenSRF.git] / src / perl / lib / OpenSRF / Application.pm
1 package OpenSRF::Application;
2 # vim:noet:ts=4
3 use vars qw/$_app $log @_METHODS $thunk $server_class/;
4
5 use base qw/OpenSRF/;
6 use OpenSRF::AppSession;
7 use OpenSRF::DomainObject::oilsMethod;
8 use OpenSRF::DomainObject::oilsResponse qw/:status/;
9 use OpenSRF::Utils::Logger qw/:level $logger/;
10 use Data::Dumper;
11 use Time::HiRes qw/time/;
12 use OpenSRF::EX qw/:try/;
13 use Carp;
14 use OpenSRF::Utils::JSON;
15 #use OpenSRF::UnixServer;  # to get the server class from UnixServer::App
16
17 sub DESTROY{};
18
19 use strict;
20 use warnings;
21
22 $log = 'OpenSRF::Utils::Logger';
23
24 our $in_request = 0;
25 our @pending_requests;
26
27 sub package {
28         my $self = shift;
29         return 1 unless ref($self);
30         return $self->{package};
31 }
32
33 sub signature {
34         my $self = shift;
35         return 0 unless ref($self);
36         return $self->{signature};
37 }
38
39 sub strict {
40     my $self = shift; 
41     return 0 unless ref($self);
42     return $self->{strict};
43 }
44
45 sub argc {
46         my $self = shift;
47         return 0 unless ref($self);
48         return $self->{argc};
49 }
50
51 sub max_chunk_size {
52         my $self = shift;
53         return 0 unless ref($self);
54         return $self->{max_chunk_size} if (defined($self->{max_chunk_size}));
55         return 10240;
56 }
57
58 sub max_chunk_count {
59         my $self = shift;
60         return 0 unless ref($self);
61         return $self->{max_chunk_count} || 0;
62 }
63
64 sub api_name {
65         my $self = shift;
66         return 1 unless ref($self);
67         return $self->{api_name};
68 }
69
70 sub api_level {
71         my $self = shift;
72         return 1 unless ref($self);
73         return $self->{api_level};
74 }
75
76 sub session {
77         my $self = shift;
78         my $session = shift;
79
80         if($session) {
81                 $self->{session} = $session;
82         }
83         return $self->{session};
84 }
85
86 sub server_class {
87         my $class = shift;
88         if($class) {
89                 $server_class = $class;
90         }
91         return $server_class;
92 }
93
94 sub thunk {
95         my $self = shift;
96         my $flag = shift;
97         $thunk = $flag if (defined $flag);
98         return $thunk;
99 }
100
101 sub application_implementation {
102         my $self = shift;
103         my $app = shift;
104
105         if (defined $app) {
106                 $_app = $app;
107                 $_app->use;
108                 if( $@ ) {
109                         $log->error( "Error loading application_implementation: $app -> $@", ERROR);
110                 }
111
112         }
113
114         return $_app;
115 }
116
117 sub handler {
118         my ($self, $session, $app_msg) = @_;
119
120         if( ! $app_msg ) {
121                 return 1;  # error?
122         }
123
124         my $app = $self->application_implementation;
125
126         if ($session->last_message_type eq 'REQUEST') {
127
128         my @p = $app_msg->params;
129                 my $method_name = $app_msg->method;
130                 my $method_proto = $session->last_message_api_level;
131                 $log->info("CALL: $method_name [". (@p ? join(', ',@p) : '') ."]");
132
133                 my $coderef = $app->method_lookup( $method_name, $method_proto, 1, 1 );
134
135                 unless ($coderef) {
136                         $session->status( OpenSRF::DomainObject::oilsMethodException->new( 
137                                                 statusCode => STATUS_NOTFOUND(),
138                                                 status => "Method [$method_name] not found for $app"));
139                         return 1;
140                 }
141
142                 unless ($session->continue_request) {
143                         $session->status(
144                                 OpenSRF::DomainObject::oilsConnectStatus->new(
145                                                 statusCode => STATUS_REDIRECTED(),
146                                                 status => 'Disconnect on max requests' ) );
147                         $session->kill_me;
148                         return 1;
149                 }
150
151                 if (ref $coderef) {
152                         my @args = $app_msg->params;
153                         my $appreq = OpenSRF::AppRequest->new( $session );
154                         $appreq->max_chunk_size( $coderef->max_chunk_size );
155                         $appreq->max_chunk_count( $coderef->max_chunk_count );
156
157                         $log->debug( "in_request = $in_request : [" . $appreq->threadTrace."]", INTERNAL );
158                         if( $in_request ) {
159                                 $log->debug( "Pushing onto pending requests: " . $appreq->threadTrace, DEBUG );
160                                 push @pending_requests, [ $appreq, \@args, $coderef ]; 
161                                 return 1;
162                         }
163
164
165                         $in_request++;
166
167                         $log->debug( "Executing coderef for {$method_name}", INTERNAL );
168
169                         my $resp;
170                         try {
171                                 # un-if(0) this block to enable param checking based on signature and argc
172                                 if ($coderef->strict) {
173                                         if (@args < $coderef->argc) {
174                                                 die     "Not enough params passed to ".
175                                                         $coderef->api_name." : requires ". $coderef->argc
176                                         }
177                                         if (@args) {
178                                                 my $sig = $coderef->signature;
179                                                 if ($sig && exists $sig->{params}) {
180                                                         for my $p (0 .. scalar(@{ $sig->{params} }) - 1 ) {
181                                                                 my $s = $sig->{params}->[$p];
182                                                                 my $a = $args[$p];
183                                                                 if ($s->{class} && OpenSRF::Utils::JSON->lookup_hint(ref $a) ne $s->{class}) {
184                                                                         die "Incorrect param class at position $p : should be a '$$s{class}'";
185                                                                 } elsif ($s->{type}) {
186                                                                         if (lc($s->{type}) eq 'object' && $a !~ /HASH/o) {
187                                                                                 die "Incorrect param type at position $p : should be an 'object'";
188                                                                         } elsif (lc($s->{type}) eq 'array' && $a !~ /ARRAY/o) {
189                                                                                 die "Incorrect param type at position $p : should be an 'array'";
190                                                                         } elsif (lc($s->{type}) eq 'number' && (ref($a) || $a !~ /^-?\d+(?:\.\d+)?$/o)) {
191                                                                                 die "Incorrect param type at position $p : should be a 'number'";
192                                                                         } elsif (lc($s->{type}) eq 'string' && ref($a)) {
193                                                                                 die "Incorrect param type at position $p : should be a 'string'";
194                                                                         }
195                                                                 }
196                                                         }
197                                                 }
198                                         }
199                                 }
200
201                                 my $start = time();
202                                 $resp = $coderef->run( $appreq, @args); 
203                                 my $time = sprintf '%.3f', time() - $start;
204
205                                 $log->debug( "Method duration for [$method_name]:  ". $time );
206                                 $appreq->respond_complete( $resp );
207
208                         } catch Error with {
209                                 my $e = shift;
210                                 warn "Caught error from 'run' method: $e\n";
211
212                                 if(UNIVERSAL::isa($e,"Error")) {
213                                         $e = $e->stringify();
214                                 } 
215                                 my $sess_id = $session->session_id;
216                                 $session->status(
217                                         OpenSRF::DomainObject::oilsMethodException->new(
218                                                         statusCode      => STATUS_INTERNALSERVERERROR(),
219                                                         status          => " *** Call to [$method_name] failed for session ".
220                                                                            "[$sess_id], thread trace ".
221                                                                            "[".$appreq->threadTrace."]:\n$e\n"
222                                         )
223                                 );
224                         };
225
226
227
228                         # ----------------------------------------------
229
230
231                         # XXX may need this later
232                         # $_->[1] = 1 for (@OpenSRF::AppSession::_CLIENT_CACHE);
233
234                         $in_request--;
235
236                         $log->debug( "Pending Requests: " . scalar(@pending_requests), INTERNAL );
237
238                         # cycle through queued requests
239                         while( my $aref = shift @pending_requests ) {
240                                 $in_request++;
241                                 my $resp;
242                                 try {
243                                         # un-if(0) this block to enable param checking based on signature and argc
244                                         if (0) {
245                                                 if (@args < $aref->[2]->argc) {
246                                                         die     "Not enough params passed to ".
247                                                                 $aref->[2]->api_name." : requires ". $aref->[2]->argc
248                                                 }
249                                                 if (@args) {
250                                                         my $sig = $aref->[2]->signature;
251                                                         if ($sig && exists $sig->{params}) {
252                                                                 for my $p (0 .. scalar(@{ $sig->{params} }) - 1 ) {
253                                                                         my $s = $sig->{params}->[$p];
254                                                                         my $a = $args[$p];
255                                                                         if ($s->{class} && OpenSRF::Utils::JSON->lookup_hint(ref $a) ne $s->{class}) {
256                                                                                 die "Incorrect param class at position $p : should be a '$$s{class}'";
257                                                                         } elsif ($s->{type}) {
258                                                                                 if (lc($s->{type}) eq 'object' && $a !~ /HASH/o) {
259                                                                                         die "Incorrect param type at position $p : should be an 'object'";
260                                                                                 } elsif (lc($s->{type}) eq 'array' && $a !~ /ARRAY/o) {
261                                                                                         die "Incorrect param type at position $p : should be an 'array'";
262                                                                                 } elsif (lc($s->{type}) eq 'number' && (ref($a) || $a !~ /^-?\d+(?:\.\d+)?$/o)) {
263                                                                                         die "Incorrect param type at position $p : should be a 'number'";
264                                                                                 } elsif (lc($s->{type}) eq 'string' && ref($a)) {
265                                                                                         die "Incorrect param type at position $p : should be a 'string'";
266                                                                                 }
267                                                                         }
268                                                                 }
269                                                         }
270                                                 }
271                                         }
272
273                                         my $start = time;
274                                         my $response = $aref->[2]->run( $aref->[0], @{$aref->[1]} );
275                                         my $time = sprintf '%.3f', time - $start;
276                                         $log->debug( "Method duration for [".$aref->[2]->api_name." -> ".join(', ',@{$aref->[1]}).']:  '.$time, DEBUG );
277
278                                         $appreq = $aref->[0];   
279                                         $appreq->respond_complete( $response );
280                                         $log->debug( "Executed: " . $appreq->threadTrace, INTERNAL );
281
282                                 } catch Error with {
283                                         my $e = shift;
284                                         if(UNIVERSAL::isa($e,"Error")) {
285                                                 $e = $e->stringify();
286                                         }
287                                         $session->status(
288                                                 OpenSRF::DomainObject::oilsMethodException->new(
289                                                                 statusCode => STATUS_INTERNALSERVERERROR(),
290                                                                 status => "Call to [".$aref->[2]->api_name."] faild:  $e"
291                                                 )
292                                         );
293                                 };
294                                 $in_request--;
295                         }
296
297                         return 1;
298                 } 
299
300                 $log->info("Received non-REQUEST message in Application handler");
301
302                 my $res = OpenSRF::DomainObject::oilsMethodException->new( 
303                                 status => "Received non-REQUEST message in Application handler");
304                 $session->send('ERROR', $res);
305                 $session->kill_me;
306                 return 1;
307
308         } else {
309                 $session->push_queue([ $app_msg, $session->last_threadTrace ]);
310         }
311
312         $session->last_message_type('');
313         $session->last_message_api_level('');
314
315         return 1;
316 }
317
318 sub is_registered {
319         my $self = shift;
320         my $api_name = shift;
321         my $api_level = shift || 1;
322         return exists($_METHODS[$api_level]{$api_name});
323 }
324
325
326 sub normalize_whitespace {
327         my $txt = shift;
328
329         $txt =~ s/^\s+//gso;
330         $txt =~ s/\s+$//gso;
331         $txt =~ s/\s+/ /gso;
332         $txt =~ s/\n//gso;
333         $txt =~ s/\. /\.  /gso;
334
335         return $txt;
336 }
337
338 sub parse_string_signature {
339         my $string = shift;
340         return [] unless $string;
341         my @chunks = split(/\@/smo, $string);
342
343         my @params;
344         my $ret;
345         my $desc = '';
346         for (@chunks) {
347                 if (/^return (.+)$/so) {
348                         $ret = [normalize_whitespace($1)];
349                 } elsif (/^param (\w+) \b(.+)$/so) {
350                         push @params, [ $1, normalize_whitespace($2) ];
351                 } else {
352                         $desc .= '@' if $desc;
353                         $desc .= $_;
354                 }
355         }
356
357         return [normalize_whitespace($desc),\@params, $ret];
358 }
359
360 sub parse_array_signature {
361         my $array = shift;
362         my ($d,$p,$r) = @$array;
363         return {} unless ($d or $p or $r);
364
365         return {
366                 desc    => $d,
367                 params  => [
368                         map { 
369                                 { name  => $$_[0],
370                                   desc  => $$_[1],
371                                   type  => $$_[2],
372                                   class => $$_[3],
373                                 }
374                         } @$p
375                 ],
376                 'return'=>
377                         { desc  => $$r[0],
378                           type  => $$r[1],
379                           class => $$r[2],
380                         }
381         };
382 }
383
384 sub register_method {
385         my $self = shift;
386         my $app = ref($self) || $self;
387         my %args = @_;
388
389
390         throw OpenSRF::DomainObject::oilsMethodException unless ($args{method});
391
392         $args{api_level} = 1 unless(defined($args{api_level}));
393         $args{stream} ||= 0;
394         $args{remote} ||= 0;
395         $args{argc} ||= 0;
396         $args{package} ||= $app;                
397         $args{server_class} = server_class();
398         $args{api_name} ||= $args{server_class} . '.' . $args{method};
399
400         # un-if(0) this block to enable signature parsing
401         if (!$args{signature}) {
402                 if ($args{notes} && !ref($args{notes})) {
403                         $args{signature} =
404                                 parse_array_signature( parse_string_signature( $args{notes} ) );
405                 }
406         } elsif( !ref($args{signature}) ) {
407                 $args{signature} =
408                         parse_array_signature( parse_string_signature( $args{signature} ) );
409         } elsif( ref($args{signature}) eq 'ARRAY') {
410                 $args{signature} =
411                         parse_array_signature( $args{signature} );
412         }
413         
414         unless ($args{object_hint}) {
415                 ($args{object_hint} = $args{package}) =~ s/::/_/go;
416         }
417
418         OpenSRF::Utils::JSON->register_class_hint( name => $args{package}, hint => $args{object_hint}, type => "hash" );
419
420         $_METHODS[$args{api_level}]{$args{api_name}} = bless \%args => $app;
421
422         __PACKAGE__->register_method(
423                 stream => 0,
424                 argc => $args{argc},
425                 api_name => $args{api_name}.'.atomic',
426                 method => 'make_stream_atomic',
427                 notes => "This is a system generated method.  Please see the definition for $args{api_name}",
428         ) if ($args{stream});
429 }
430
431 sub retrieve_remote_apis {
432         my $method = shift;
433         my $session = OpenSRF::AppSession->create('router');
434         try {
435                 $session->connect or OpenSRF::EX::WARN->throw("Connection to router timed out");
436         } catch Error with {
437                 my $e = shift;
438                 $log->debug( "Remote subrequest returned an error:\n". $e );
439                 return undef;
440         } finally {
441                 return undef unless ($session->state == $session->CONNECTED);
442         };
443
444         my $req = $session->request( 'opensrf.router.info.class.list' );
445         my $list = $req->recv;
446
447         if( UNIVERSAL::isa($list,"Error") ) {
448                 throw $list;
449         }
450
451         my $content = $list->content;
452
453         $req->finish;
454         $session->finish;
455         $session->disconnect;
456
457         my %u_list = map { ($_ => 1) } @$content;
458
459         for my $class ( keys %u_list ) {
460                 next if($class eq $server_class);
461                 populate_remote_method_cache($class, $method);
462         }
463 }
464
465 sub populate_remote_method_cache {
466         my $class = shift;
467         my $meth = shift;
468
469         my $session = OpenSRF::AppSession->create($class);
470         try {
471                 $session->connect or OpenSRF::EX::WARN->throw("Connection to $class timed out");
472
473                 my $call = 'opensrf.system.method.all' unless (defined $meth);
474                 $call = 'opensrf.system.method' if (defined $meth);
475
476                 my $req = $session->request( $call, $meth );
477
478                 while (my $method = $req->recv) {
479                         next if (UNIVERSAL::isa($method, 'Error'));
480
481                         $method = $method->content;
482                         next if ( exists($_METHODS[$$method{api_level}]) &&
483                                 exists($_METHODS[$$method{api_level}]{$$method{api_name}}) );
484                         $method->{remote} = 1;
485                         bless($method, __PACKAGE__ );
486                         $_METHODS[$$method{api_level}]{$$method{api_name}} = $method;
487                 }
488
489                 $req->finish;
490                 $session->finish;
491                 $session->disconnect;
492
493         } catch Error with {
494                 my $e = shift;
495                 $log->debug( "Remote subrequest returned an error:\n". $e );
496                 return undef;
497         };
498 }
499
500 sub method_lookup {             
501         my $self = shift;
502         my $method = shift;
503         my $proto = shift;
504         my $no_recurse = shift || 0;
505         my $no_remote = shift || 0;
506
507         # this instead of " || 1;" above to allow api_level 0
508         $proto = $self->api_level unless (defined $proto);
509
510         my $class = ref($self) || $self;
511
512         $log->debug("Lookup of [$method] by [$class] in api_level [$proto]", DEBUG);
513         $log->debug("Available methods\n\t".join("\n\t", keys %{ $_METHODS[$proto] }), INTERNAL);
514
515         my $meth;
516         if (__PACKAGE__->thunk) {
517                 for my $p ( reverse(1 .. $proto) ) {
518                         if (exists $_METHODS[$p]{$method}) {
519                                 $meth = $_METHODS[$p]{$method};
520                         }
521                 }
522         } else {
523                 if (exists $_METHODS[$proto]{$method}) {
524                         $meth = $_METHODS[$proto]{$method};
525                 }
526         }
527
528         if (defined $meth) {
529                 if($no_remote and $meth->{remote}) {
530                         $log->debug("OH CRAP We're not supposed to return remote methods", WARN);
531                         return undef;
532                 }
533
534         } elsif (!$no_recurse) {
535                 $log->debug("We didn't find [$method], asking everyone else.", DEBUG);
536                 retrieve_remote_apis($method);
537                 $meth = $self->method_lookup($method,$proto,1);
538         }
539
540         return $meth;
541 }
542
543 sub run {
544         my $self = shift;
545         my $req = shift;
546
547         my $resp;
548         my @params = @_;
549
550         if ( !UNIVERSAL::isa($req, 'OpenSRF::AppRequest') ) {
551                 $log->debug("Creating a SubRequest object", DEBUG);
552                 unshift @params, $req;
553                 $req = OpenSRF::AppSubrequest->new;
554                 $req->session( $self->session ) if ($self->session);
555
556         } else {
557                 $log->debug("This is a top level request", DEBUG);
558         }
559
560         if (!$self->{remote}) {
561                 my $code = \&{$self->{package} . '::' . $self->{method}};
562                 my $err = undef;
563
564                 try {
565                         $resp = $code->($self, $req, @params);
566
567                 } catch Error with {
568                         $err = shift;
569
570                         if( ref($self) eq 'HASH') {
571                                 $log->error("Sub $$self{package}::$$self{method} DIED!!!\n\t$err\n", ERROR);
572                         }
573                 };
574
575                 if($err) {
576                         if(UNIVERSAL::isa($err,"Error")) { 
577                                 throw $err;
578                         } else {
579                                 die $err->stringify; 
580                         }
581                 }
582
583
584                 $log->debug("Coderef for [$$self{package}::$$self{method}] has been run", DEBUG);
585
586                 if ( ref($req) and UNIVERSAL::isa($req, 'OpenSRF::AppSubrequest') ) {
587                         $req->respond($resp) if (defined $resp);
588                         $log->debug("SubRequest object is responding with : " . join(" ",$req->responses), DEBUG);
589                         return $req->responses;
590                 } else {
591                         $log->debug("A top level Request object is responding $resp", DEBUG) if (defined $resp);
592                         return $resp;
593                 }
594         } else {
595                 my $session = OpenSRF::AppSession->create($self->{server_class});
596                 try {
597                         #$session->connect or OpenSRF::EX::WARN->throw("Connection to [$$self{server_class}] timed out");
598                         my $remote_req = $session->request( $self->{api_name}, @params );
599                         while (my $remote_resp = $remote_req->recv) {
600                                 OpenSRF::Utils::Logger->debug("Remote Subrequest Received " . $remote_resp, INTERNAL );
601                                 if( UNIVERSAL::isa($remote_resp,"Error") ) {
602                                         throw $remote_resp;
603                                 }
604                                 $req->respond( $remote_resp->content );
605                         }
606                         $remote_req->finish();
607
608                 } catch Error with {
609                         my $e = shift;
610                         $log->debug( "Remote subrequest returned an error:\n". $e );
611                         return undef;
612                 };
613
614                 if ($session) {
615                         $session->disconnect();
616                         $session->finish();
617                 }
618
619                 $log->debug( "Remote Subrequest Responses " . join(" ", $req->responses), INTERNAL );
620
621                 return $req->responses;
622         }
623         # huh? how'd we get here...
624         return undef;
625 }
626
627 sub introspect {
628         my $self = shift;
629         my $client = shift;
630         my $method = shift;
631         my $limit = shift;
632         my $offset = shift;
633
634         if ($self->api_name =~ /all$/o) {
635                 $offset = $limit;
636                 $limit = $method;
637                 $method = undef; 
638         }
639
640         my ($seen,$returned) = (0,0);
641         for my $api_level ( reverse(1 .. $#_METHODS) ) {
642                 for my $api_name ( sort keys %{$_METHODS[$api_level]} ) {
643                         if (!$offset || $offset <= $seen) {
644                                 if (!$_METHODS[$api_level]{$api_name}{remote}) {
645                                         if (defined($method)) {
646                                                 if ($api_name =~ $method) {
647                                                         if (!$limit || $returned < $limit) {
648                                                                 $client->respond( $_METHODS[$api_level]{$api_name} );
649                                                                 $returned++;
650                                                         }
651                                                 }
652                                         } else {
653                                                 if (!$limit || $returned < $limit) {
654                                                         $client->respond( $_METHODS[$api_level]{$api_name} );
655                                                         $returned++;
656                                                 }
657                                         }
658                                 }
659                         }
660                         $seen++;
661                 }
662         }
663
664         return undef;
665 }
666 __PACKAGE__->register_method(
667         stream => 1,
668         method => 'introspect',
669         api_name => 'opensrf.system.method.all',
670         argc => 0,
671         signature => {
672                 desc => q/This method is used to introspect an entire OpenSRF Application/,
673                 return => {
674                         desc => q/A stream of objects describing the methods available via this OpenSRF Application/,
675                         type => 'object'
676                 }
677         },
678 );
679 __PACKAGE__->register_method(
680         stream => 1,
681         method => 'introspect',
682         argc => 1,
683         api_name => 'opensrf.system.method',
684         argc => 1,
685         signature => {
686                 desc => q/Use this method to get the definition of a single OpenSRF Method/,
687                 params => [
688                         { desc => q/The method to introspect/,
689                           type => 'string' },
690                 ],
691                 return => { desc => q/An object describing the method requested, or an error if it can't be found/,
692                             type => 'object' }
693         },
694 );
695
696 sub echo_method {
697         my $self = shift;
698         my $client = shift;
699         my @args = @_;
700
701         $client->respond( $_ ) for (@args);
702         return undef;
703 }
704 __PACKAGE__->register_method(
705         stream => 1,
706         method => 'echo_method',
707         argc => 1,
708         api_name => 'opensrf.system.echo',
709         signature => {
710                 desc => q/A test method that will echo back it's arguments in a streaming response/,
711                 params => [
712                         { desc => q/One or more arguments to echo back/ }
713                 ],
714                 return => { desc => q/A stream of the arguments passed/ }
715         },
716 );
717
718 sub time_method {
719         my( $self, $conn ) = @_;
720         return CORE::time;
721 }
722 __PACKAGE__->register_method(
723         method => 'time_method',
724         argc => 0,
725         api_name => 'opensrf.system.time',
726         signature => {
727                 desc => q/Returns the current system time as epoch seconds/,
728                 return => { desc => q/epoch seconds/ }
729         }
730 );
731
732 sub make_stream_atomic {
733         my $self = shift;
734         my $req = shift;
735         my @args = @_;
736
737         (my $m_name = $self->api_name) =~ s/\.atomic$//o;
738         my $m = $self->method_lookup($m_name);
739
740         $m->session( $req->session );
741         my @results = $m->run(@args);
742         $m->session('');
743
744         return \@results;
745 }
746
747
748 1;
749
750