5ae98bfe1deeae9f1c969f693c4d9ab9a8abf601
[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                         my $appreq = OpenSRF::AppRequest->new( $session );
175                         $appreq->max_chunk_size( $coderef->max_chunk_size );
176                         $appreq->max_chunk_count( $coderef->max_chunk_count );
177
178                         $log->debug( "in_request = $in_request : [" . $appreq->threadTrace."]", INTERNAL );
179                         if( $in_request ) {
180                                 $log->debug( "Pushing onto pending requests: " . $appreq->threadTrace, DEBUG );
181                                 push @pending_requests, [ $appreq, \@args, $coderef ]; 
182                                 return 1;
183                         }
184
185
186                         $in_request++;
187
188                         $log->debug( "Executing coderef for {$method_name}", INTERNAL );
189
190                         my $resp;
191                         try {
192                                 # un-if(0) this block to enable param checking based on signature and argc
193                                 if ($coderef->strict) {
194                                         if (@args < $coderef->argc) {
195                                                 die     "Not enough params passed to ".
196                                                         $coderef->api_name." : requires ". $coderef->argc
197                                         }
198                                         if (@args) {
199                                                 my $sig = $coderef->signature;
200                                                 if ($sig && exists $sig->{params}) {
201                                                         for my $p (0 .. scalar(@{ $sig->{params} }) - 1 ) {
202                                                                 my $s = $sig->{params}->[$p];
203                                                                 my $a = $args[$p];
204                                                                 if ($s->{class} && OpenSRF::Utils::JSON->lookup_hint(ref $a) ne $s->{class}) {
205                                                                         die "Incorrect param class at position $p : should be a '$$s{class}'";
206                                                                 } elsif ($s->{type}) {
207                                                                         if (lc($s->{type}) eq 'object' && $a !~ /HASH/o) {
208                                                                                 die "Incorrect param type at position $p : should be an 'object'";
209                                                                         } elsif (lc($s->{type}) eq 'array' && $a !~ /ARRAY/o) {
210                                                                                 die "Incorrect param type at position $p : should be an 'array'";
211                                                                         } elsif (lc($s->{type}) eq 'number' && (ref($a) || $a !~ /^-?\d+(?:\.\d+)?$/o)) {
212                                                                                 die "Incorrect param type at position $p : should be a 'number'";
213                                                                         } elsif (lc($s->{type}) eq 'string' && ref($a)) {
214                                                                                 die "Incorrect param type at position $p : should be a 'string'";
215                                                                         }
216                                                                 }
217                                                         }
218                                                 }
219                                         }
220                                 }
221
222                                 my $start = time();
223                                 $resp = $coderef->run( $appreq, @args); 
224                                 my $time = sprintf '%.3f', time() - $start;
225
226                                 $log->debug( "Method duration for [$method_name]:  ". $time );
227                                 $appreq->respond_complete( $resp );
228
229                         } catch Error with {
230                                 my $e = shift;
231                                 warn "Caught error from 'run' method: $e\n";
232
233                                 if(UNIVERSAL::isa($e,"Error")) {
234                                         $e = $e->stringify();
235                                 } 
236                                 my $sess_id = $session->session_id;
237                                 $session->status(
238                                         OpenSRF::DomainObject::oilsMethodException->new(
239                                                         statusCode      => STATUS_INTERNALSERVERERROR(),
240                                                         status          => " *** Call to [$method_name] failed for session ".
241                                                                            "[$sess_id], thread trace ".
242                                                                            "[".$appreq->threadTrace."]:\n$e\n"
243                                         )
244                                 );
245                         };
246
247
248
249                         # ----------------------------------------------
250
251
252                         # XXX may need this later
253                         # $_->[1] = 1 for (@OpenSRF::AppSession::_CLIENT_CACHE);
254
255                         $in_request--;
256
257                         $log->debug( "Pending Requests: " . scalar(@pending_requests), INTERNAL );
258
259                         # cycle through queued requests
260                         while( my $aref = shift @pending_requests ) {
261                                 $in_request++;
262                                 my $resp;
263                                 try {
264                                         # un-if(0) this block to enable param checking based on signature and argc
265                                         if (0) {
266                                                 if (@args < $aref->[2]->argc) {
267                                                         die     "Not enough params passed to ".
268                                                                 $aref->[2]->api_name." : requires ". $aref->[2]->argc
269                                                 }
270                                                 if (@args) {
271                                                         my $sig = $aref->[2]->signature;
272                                                         if ($sig && exists $sig->{params}) {
273                                                                 for my $p (0 .. scalar(@{ $sig->{params} }) - 1 ) {
274                                                                         my $s = $sig->{params}->[$p];
275                                                                         my $a = $args[$p];
276                                                                         if ($s->{class} && OpenSRF::Utils::JSON->lookup_hint(ref $a) ne $s->{class}) {
277                                                                                 die "Incorrect param class at position $p : should be a '$$s{class}'";
278                                                                         } elsif ($s->{type}) {
279                                                                                 if (lc($s->{type}) eq 'object' && $a !~ /HASH/o) {
280                                                                                         die "Incorrect param type at position $p : should be an 'object'";
281                                                                                 } elsif (lc($s->{type}) eq 'array' && $a !~ /ARRAY/o) {
282                                                                                         die "Incorrect param type at position $p : should be an 'array'";
283                                                                                 } elsif (lc($s->{type}) eq 'number' && (ref($a) || $a !~ /^-?\d+(?:\.\d+)?$/o)) {
284                                                                                         die "Incorrect param type at position $p : should be a 'number'";
285                                                                                 } elsif (lc($s->{type}) eq 'string' && ref($a)) {
286                                                                                         die "Incorrect param type at position $p : should be a 'string'";
287                                                                                 }
288                                                                         }
289                                                                 }
290                                                         }
291                                                 }
292                                         }
293
294                                         my $start = time;
295                                         my $response = $aref->[2]->run( $aref->[0], @{$aref->[1]} );
296                                         my $time = sprintf '%.3f', time - $start;
297                                         $log->debug( "Method duration for [".$aref->[2]->api_name." -> ".join(', ',@{$aref->[1]}).']:  '.$time, DEBUG );
298
299                                         $appreq = $aref->[0];   
300                                         $appreq->respond_complete( $response );
301                                         $log->debug( "Executed: " . $appreq->threadTrace, INTERNAL );
302
303                                 } catch Error with {
304                                         my $e = shift;
305                                         if(UNIVERSAL::isa($e,"Error")) {
306                                                 $e = $e->stringify();
307                                         }
308                                         $session->status(
309                                                 OpenSRF::DomainObject::oilsMethodException->new(
310                                                                 statusCode => STATUS_INTERNALSERVERERROR(),
311                                                                 status => "Call to [".$aref->[2]->api_name."] faild:  $e"
312                                                 )
313                                         );
314                                 };
315                                 $in_request--;
316                         }
317
318                         return 1;
319                 } 
320
321                 $log->info("Received non-REQUEST message in Application handler");
322
323                 my $res = OpenSRF::DomainObject::oilsMethodException->new( 
324                                 status => "Received non-REQUEST message in Application handler");
325                 $session->send('ERROR', $res);
326                 $session->kill_me;
327                 return 1;
328
329         } else {
330                 $session->push_queue([ $app_msg, $session->last_threadTrace ]);
331         }
332
333         $session->last_message_type('');
334         $session->last_message_api_level('');
335
336         return 1;
337 }
338
339 sub is_registered {
340         my $self = shift;
341         my $api_name = shift;
342         my $api_level = shift || 1;
343         return exists($_METHODS[$api_level]{$api_name});
344 }
345
346
347 sub normalize_whitespace {
348         my $txt = shift;
349
350         $txt =~ s/^\s+//gso;
351         $txt =~ s/\s+$//gso;
352         $txt =~ s/\s+/ /gso;
353         $txt =~ s/\n//gso;
354         $txt =~ s/\. /\.  /gso;
355
356         return $txt;
357 }
358
359 sub parse_string_signature {
360         my $string = shift;
361         return [] unless $string;
362         my @chunks = split(/\@/smo, $string);
363
364         my @params;
365         my $ret;
366         my $desc = '';
367         for (@chunks) {
368                 if (/^return (.+)$/so) {
369                         $ret = [normalize_whitespace($1)];
370                 } elsif (/^param (\w+) \b(.+)$/so) {
371                         push @params, [ $1, normalize_whitespace($2) ];
372                 } else {
373                         $desc .= '@' if $desc;
374                         $desc .= $_;
375                 }
376         }
377
378         return [normalize_whitespace($desc),\@params, $ret];
379 }
380
381 sub parse_array_signature {
382         my $array = shift;
383         my ($d,$p,$r) = @$array;
384         return {} unless ($d or $p or $r);
385
386         return {
387                 desc    => $d,
388                 params  => [
389                         map { 
390                                 { name  => $$_[0],
391                                   desc  => $$_[1],
392                                   type  => $$_[2],
393                                   class => $$_[3],
394                                 }
395                         } @$p
396                 ],
397                 'return'=>
398                         { desc  => $$r[0],
399                           type  => $$r[1],
400                           class => $$r[2],
401                         }
402         };
403 }
404
405 sub register_method {
406         my $self = shift;
407         my $app = ref($self) || $self;
408         my %args = @_;
409
410
411         throw OpenSRF::DomainObject::oilsMethodException unless ($args{method});
412
413         $args{api_level} = 1 unless(defined($args{api_level}));
414         $args{stream} ||= 0;
415         $args{remote} ||= 0;
416         $args{argc} ||= 0;
417         $args{package} ||= $app;                
418         $args{server_class} = server_class();
419         $args{api_name} ||= $args{server_class} . '.' . $args{method};
420
421         # un-if(0) this block to enable signature parsing
422         if (!$args{signature}) {
423                 if ($args{notes} && !ref($args{notes})) {
424                         $args{signature} =
425                                 parse_array_signature( parse_string_signature( $args{notes} ) );
426                 }
427         } elsif( !ref($args{signature}) ) {
428                 $args{signature} =
429                         parse_array_signature( parse_string_signature( $args{signature} ) );
430         } elsif( ref($args{signature}) eq 'ARRAY') {
431                 $args{signature} =
432                         parse_array_signature( $args{signature} );
433         }
434         
435         unless ($args{object_hint}) {
436                 ($args{object_hint} = $args{package}) =~ s/::/_/go;
437         }
438
439         OpenSRF::Utils::JSON->register_class_hint( name => $args{package}, hint => $args{object_hint}, type => "hash" );
440
441         $_METHODS[$args{api_level}]{$args{api_name}} = bless \%args => $app;
442
443         __PACKAGE__->register_method(
444                 stream => 0,
445                 argc => $args{argc},
446                 api_name => $args{api_name}.'.atomic',
447                 method => 'make_stream_atomic',
448                 notes => "This is a system generated method.  Please see the definition for $args{api_name}",
449         ) if ($args{stream});
450 }
451
452 sub retrieve_remote_apis {
453         my $method = shift;
454         my $session = OpenSRF::AppSession->create('router');
455         try {
456                 $session->connect or OpenSRF::EX::WARN->throw("Connection to router timed out");
457         } catch Error with {
458                 my $e = shift;
459                 $log->debug( "Remote subrequest returned an error:\n". $e );
460                 return undef;
461         } finally {
462                 return undef unless ($session->state == $session->CONNECTED);
463         };
464
465         my $req = $session->request( 'opensrf.router.info.class.list' );
466         my $list = $req->recv;
467
468         if( UNIVERSAL::isa($list,"Error") ) {
469                 throw $list;
470         }
471
472         my $content = $list->content;
473
474         $req->finish;
475         $session->finish;
476         $session->disconnect;
477
478         my %u_list = map { ($_ => 1) } @$content;
479
480         for my $class ( keys %u_list ) {
481                 next if($class eq $server_class);
482                 populate_remote_method_cache($class, $method);
483         }
484 }
485
486 sub populate_remote_method_cache {
487         my $class = shift;
488         my $meth = shift;
489
490         my $session = OpenSRF::AppSession->create($class);
491         try {
492                 $session->connect or OpenSRF::EX::WARN->throw("Connection to $class timed out");
493
494                 my $call = 'opensrf.system.method.all' unless (defined $meth);
495                 $call = 'opensrf.system.method' if (defined $meth);
496
497                 my $req = $session->request( $call, $meth );
498
499                 while (my $method = $req->recv) {
500                         next if (UNIVERSAL::isa($method, 'Error'));
501
502                         $method = $method->content;
503                         next if ( exists($_METHODS[$$method{api_level}]) &&
504                                 exists($_METHODS[$$method{api_level}]{$$method{api_name}}) );
505                         $method->{remote} = 1;
506                         bless($method, __PACKAGE__ );
507                         $_METHODS[$$method{api_level}]{$$method{api_name}} = $method;
508                 }
509
510                 $req->finish;
511                 $session->finish;
512                 $session->disconnect;
513
514         } catch Error with {
515                 my $e = shift;
516                 $log->debug( "Remote subrequest returned an error:\n". $e );
517                 return undef;
518         };
519 }
520
521 sub method_lookup {             
522         my $self = shift;
523         my $method = shift;
524         my $proto = shift;
525         my $no_recurse = shift || 0;
526         my $no_remote = shift || 0;
527
528         # this instead of " || 1;" above to allow api_level 0
529         $proto = $self->api_level unless (defined $proto);
530
531         my $class = ref($self) || $self;
532
533         $log->debug("Lookup of [$method] by [$class] in api_level [$proto]", DEBUG);
534         $log->debug("Available methods\n\t".join("\n\t", keys %{ $_METHODS[$proto] }), INTERNAL);
535
536         my $meth;
537         if (__PACKAGE__->thunk) {
538                 for my $p ( reverse(1 .. $proto) ) {
539                         if (exists $_METHODS[$p]{$method}) {
540                                 $meth = $_METHODS[$p]{$method};
541                         }
542                 }
543         } else {
544                 if (exists $_METHODS[$proto]{$method}) {
545                         $meth = $_METHODS[$proto]{$method};
546                 }
547         }
548
549         if (defined $meth) {
550                 if($no_remote and $meth->{remote}) {
551                         $log->debug("OH CRAP We're not supposed to return remote methods", WARN);
552                         return undef;
553                 }
554
555         } elsif (!$no_recurse) {
556                 $log->debug("We didn't find [$method], asking everyone else.", DEBUG);
557                 retrieve_remote_apis($method);
558                 $meth = $self->method_lookup($method,$proto,1);
559         }
560
561         return $meth;
562 }
563
564 sub run {
565         my $self = shift;
566         my $req = shift;
567
568         my $resp;
569         my @params = @_;
570
571         if ( !UNIVERSAL::isa($req, 'OpenSRF::AppRequest') ) {
572                 $log->debug("Creating a SubRequest object", DEBUG);
573                 unshift @params, $req;
574                 $req = OpenSRF::AppSubrequest->new;
575                 $req->session( $self->session ) if ($self->session);
576
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;