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