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