1 package OpenILS::Utils::ScriptRunner;
2 use strict; use warnings;
3 use OpenSRF::Utils::Logger qw(:logger);
4 use OpenSRF::EX qw(:try);
5 use JavaScript::SpiderMonkey;
8 use Time::HiRes qw/time/;
15 $class = ref($class) || $class;
16 $params{paths} ||= [];
18 my $self = bless { file => $params{file},
19 libs => $params{libs},
20 _path => {%_paths} } => $class;
22 $self->add_path($_) for @{$params{paths}};
27 my( $self, $context ) = @_;
28 $self->{ctx} = $context if $context;
34 $self->context( new JavaScript::SpiderMonkey );
35 $self->context->init();
38 # eating our own dog food with insert
39 $self->insert(perl_print => sub { print "@_\n"; } );
40 $self->insert(perl_warn => sub { warn @_; } );
41 $self->insert(log_activity => sub { $logger->activity(@_); return 1;} );
42 $self->insert(log_error => sub { $logger->error(@_); return 1;} );
43 $self->insert(log_warn => sub { $logger->warn(@_); return 1;} );
44 $self->insert(log_info => sub { $logger->info(@_); return 1;} );
45 $self->insert(log_debug => sub { $logger->debug(@_); return 1;} );
46 $self->insert(log_internal => sub { $logger->internal(@_); return 1;} );
47 $self->insert(debug => sub { $logger->debug(@_); return 1;} );
48 $self->insert(alert => sub { $logger->warn(@_); return 1;} );
49 $self->insert(load_lib => sub { $self->load_lib(@_); });
51 # XML support functions
53 _OILS_FUNC_xmlhttprequest_send => sub { $self->_xmlhttprequest_send(@_); });
55 _OILS_FUNC_xml_parse_string => sub { $self->_parse_xml_string(@_); });
57 $self->load_lib($_) for @{$self->{libs}};
64 $self->context->destroy;
69 my( $self, $filename ) = @_;
70 $self->{file} = $filename;
75 my $file = shift() || $self->{file};
76 my $js = $self->context;
78 $file = $self->_find_file($file);
80 if( ! open(F, $file) ) {
81 $logger->error("Error opening script file: $file");
88 if( !$js || !$content || !$js->eval($content) ) {
89 $logger->error("$file Eval failed: $@");
92 $logger->debug("eval of $file took ". sprintf('%0.3f', time - $s) . " seconds");
100 my( $self, $path ) = @_;
102 if ($self->{_path}{$path}) {
103 $self->{_path}{$path} = 0;
105 return $self->{_path}{$path};
107 if ($_paths{$path}) {
110 return $_paths{$path};
115 my( $self, $path ) = @_;
117 if (!$self->{_path}{$path}) {
118 $self->{_path}{$path} = 1;
121 if (!$_paths{$path}) {
131 for my $p ( keys %{ $self->{_path} } ) {
132 next unless ($self->{_path}{$p});
133 my $full = join('/',$p,$file);
134 return $full if (-e $full);
139 my( $self, $file ) = @_;
140 if (!$self->{_loaded}{$file} && $self->run( $file )) {
141 $self->{_loaded}{$file} = 1;
143 return $self->{_loaded}{$file};
153 my( $self, $key ) = @_;
154 return $self->context->property_get($key);
158 my( $self, $obj_key, $meth_name, $sub ) = @_;
159 my $obj = $self->context->object_by_path( $obj_key );
160 $self->context->function_set( $meth_name, $sub, $obj ) if $obj;
165 my( $self, $key, $val, $RO ) = @_;
166 return unless defined($key);
168 if (ref($val) =~ /^Fieldmapper/o) {
169 $self->insert_fm($key, $val, $RO);
170 } elsif (ref($val) and $val =~ /ARRAY/o) {
171 $self->insert_array($key, $val, $RO);
172 } elsif (ref($val) and $val =~ /HASH/o) {
173 $self->insert_hash($key, $val, $RO);
174 } elsif (ref($val) and $val =~ /CODE/o) {
175 $self->context->function_set( $key, $val );
176 } elsif (!ref($val)) {
177 if( defined($val) ) {
178 $self->context->property_by_path(
182 sub { my( $k, $v ) = @_; $val = $v; } :
187 $self->context->property_by_path($key);
199 my( $self, $key, $fm, $RO ) = @_;
200 my $ctx = $self->context;
201 return undef unless ($ctx and $key and $fm);
202 my $o = $ctx->object_by_path($key);
204 for my $f ( $fm->properties ) {
207 $self->insert("$key.$f", $val);
209 $ctx->property_by_path(
213 my $k = _js_prop_name(shift());
219 my $k = _js_prop_name(shift());
232 my( $self, $key, $hash, $RO ) = @_;
233 my $ctx = $self->context;
234 return undef unless ($ctx and $key and $hash);
235 $ctx->object_by_path($key);
237 for my $k ( keys %$hash ) {
240 $self->insert("$key.$k", $v);
242 $ctx->property_by_path(
244 sub { $hash->{_js_prop_name(shift())} },
247 my( $hashkey, $val ) = @_;
248 $hash->{_js_prop_name($hashkey)} = $val; }
260 my( $self, $key, $array ) = @_;
261 my $ctx = $self->context;
262 return undef unless ($ctx and $key and $array);
264 my $a = $ctx->array_by_path($key);
267 for my $v ( @$array ) {
269 my $elobj = $ctx->object_by_path('__tmp_arr_el'.$__array_id);
270 $self->insert('__tmp_arr_el'.$__array_id, $v);
271 $ctx->array_set_element_as_object( $a, $ind, $elobj );
274 $ctx->array_set_element( $a, $ind, $v ) if defined($v);
280 sub _xmlhttprequest_send {
285 my $blocking = shift;
286 my $headerlist = shift;
289 my $ctx = $self->context;
291 # just so perl has access to it...
292 $ctx->object_by_path('__xmlhttpreq_hash.id'.$id);
294 my $headers = new HTTP::Headers;
295 my @lines = split(/\n/so, $headerlist);
296 for my $line (@lines) {
297 if ($line =~ /^(.+?)|(.+)$/o) {
298 $headers->header($1 => $2);
302 my $ua = LWP::UserAgent->new;
303 $ua->agent("OpenILS/0.1");
305 my $req = HTTP::Request->new($method => $url => $headers => $data);
306 my $res = $ua->request($req);
308 if ($res->is_success) {
310 $ctx->property_by_path('__xmlhttpreq_hash.id'.$id.'.responseText', $res->content);
311 $ctx->property_by_path('__xmlhttpreq_hash.id'.$id.'.readyState', 4);
312 $ctx->property_by_path('__xmlhttpreq_hash.id'.$id.'.statusText', $res->status_line);
313 $ctx->property_by_path('__xmlhttpreq_hash.id'.$id.'.status', $res->code);
319 sub _parse_xml_string {
328 $doc = XML::LibXML->new->parse_string( $string );
332 warn "Could not parse document: $e\n";
336 _JS_DOM($self->context, $key, $doc);
344 if ($node->nodeType == 9) {
345 $node = $node->documentElement;
347 my $n = $node->nodeName;
348 my $ns = $node->namespaceURI;
349 $ns =~ s/'/\'/gso if ($ns);
350 $ns = "'$ns'" if ($ns);
351 $ns = 'null' unless ($ns);
354 #warn("$key = DOMImplementation().createDocument($ns,'$n');");
355 $ctx->eval("$key = new DOMImplementation().createDocument($ns,'$n');");
357 $key = $key.'.documentElement';
360 for my $a ($node->attributes) {
361 my $n = $a->nodeName;
365 #warn("$key.setAttribute('$n','$v');");
366 $ctx->eval("$key.setAttribute('$n','$v');");
371 for my $c ($node->childNodes) {
372 if ($c->nodeType == 1) {
373 my $n = $c->nodeName;
374 my $ns = $node->namespaceURI;
377 $ns =~ s/'/\'/gso if ($ns);
378 $ns = "'$ns'" if ($ns);
379 $ns = 'null' unless ($ns);
381 #warn("$key.appendChild($key.ownerDocument.createElementNS($ns,'$n'));");
382 $ctx->eval("$key.appendChild($key.ownerDocument.createElementNS($ns,'$n'));");
383 _JS_DOM($ctx, "$key.childNodes.item($k)",$c);
385 } elsif ($c->nodeType == 3) {
388 #warn("$key.appendChild($key.ownerDocument.createTextNode('$n'));");
389 #warn("path is $key.item($k);");
390 $ctx->eval("$key.appendChild($key.ownerDocument.createTextNode('$n'));");
392 } elsif ($c->nodeType == 4) {
395 #warn("$key.appendChild($key.ownerDocument.createCDATASection('$n'));");
396 $ctx->eval("$key.appendChild($key.ownerDocument.createCDATASection('$n'));");
398 } elsif ($c->nodeType == 8) {
401 #warn("$key.appendChild($key.ownerDocument.createComment('$n'));");
402 $ctx->eval("$key.appendChild($key.ownerDocument.createComment('$n'));");
405 warn "ACK! I don't know how to handle node type ".$c->nodeType;