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