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