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