From b658b2ceda586840be92305dfc66d17113817550 Mon Sep 17 00:00:00 2001 From: erickson Date: Thu, 5 Jan 2006 18:56:58 +0000 Subject: [PATCH] scriptrunner is a generic script handling API spidermonkey is our spidermonkey JS wrapper code for moving objects around git-svn-id: svn://svn.open-ils.org/ILS/trunk@2626 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- .../perlmods/OpenILS/Utils/ScriptRunner.pm | 39 ++++++ .../perlmods/OpenILS/Utils/SpiderMonkey.pm | 115 ++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 Open-ILS/src/perlmods/OpenILS/Utils/ScriptRunner.pm create mode 100644 Open-ILS/src/perlmods/OpenILS/Utils/SpiderMonkey.pm diff --git a/Open-ILS/src/perlmods/OpenILS/Utils/ScriptRunner.pm b/Open-ILS/src/perlmods/OpenILS/Utils/ScriptRunner.pm new file mode 100644 index 0000000000..c3d4e2e9f9 --- /dev/null +++ b/Open-ILS/src/perlmods/OpenILS/Utils/ScriptRunner.pm @@ -0,0 +1,39 @@ +package OpenILS::Utils::ScriptRunner; +use strict; use warnings; +use OpenSRF::Utils::Logger qw(:logger); +use OpenILS::Utils::SpiderMonkey; + +sub new { + + my $class = shift; + my %params = @_; + $class = ref($class) || $class; + + my $type = $params{type} || 'js'; + my $file = $params{file}; + my $thingy = OpenILS::Utils::SpiderMonkey->new( $file ) if( $type =~ /js/i ); + + if($thingy) { + $thingy->init; + return $thingy; + + } else { + $logger->error("Unknown script type in OpenILS::Utils::ScriptRunner"); + } + return undef; +} + +sub init {$logger->error("METHOD NOT DEFINED"); } +sub context {$logger->error("METHOD NOT DEFINED"); } +sub insert_fm { $logger->error("METHOD NOT DEFINED"); } +sub insert_hash { $logger->error("METHOD NOT DEFINED"); } +# loads an external script +sub load { $logger->error("METHOD NOT DEFINED"); } + +# Runs an external script. +# @return 1 on success, 0 on failure +sub run { $logger->error("METHOD NOT DEFINED"); } +# load an external library +sub load_lib { $logger->error("METHOD NOT DEFINED"); } + +1; diff --git a/Open-ILS/src/perlmods/OpenILS/Utils/SpiderMonkey.pm b/Open-ILS/src/perlmods/OpenILS/Utils/SpiderMonkey.pm new file mode 100644 index 0000000000..6f1d4b8dd0 --- /dev/null +++ b/Open-ILS/src/perlmods/OpenILS/Utils/SpiderMonkey.pm @@ -0,0 +1,115 @@ +package OpenILS::Utils::SpiderMonkey; +use strict; use warnings; +use OpenSRF::Utils::Logger qw(:logger); +use OpenILS::Utils::ScriptRunner; +use base 'OpenILS::Utils::ScriptRunner'; +use JavaScript::SpiderMonkey; + +sub new { + my ( $class, $file ) = @_; + $class = ref($class) || $class; + my $self = { file => $file }; + return bless( $self, $class ); +} + +sub context { + my( $self, $context ) = @_; + $self->{ctx} = $context if $context; + return $self->{ctx}; +} + +sub init { + my $self = shift; + my $js = JavaScript::SpiderMonkey->new(); + $js->init(); + $js->function_set("perl_print", sub { print "@_\n"; } ); + $js->function_set("log_error", sub { $logger->error(@_); return 1;} ); + $js->function_set("log_warn", sub { $logger->warn(@_); return 1;} ); + $js->function_set("log_info", sub { $logger->info(@_); return 1;} ); + $js->function_set("log_debug", sub { $logger->debug(@_); return 1;} ); + $js->function_set("log_internal", sub { $logger->internal(@_); return 1;} ); + $self->context($js); + return $self; +} + + +sub load { + my( $self, $filename ) = @_; + $self->{file} = $filename; +} + +sub run { + my $self = shift; + my $file = shift() || $self->{file}; + + if( ! open(F, $file) ) { + $logger->error("Error opening script file: $file"); + return 0; + } + + my $js = $self->context; + $js->property_by_path("js.file", join( "\n", )); + + if( ! $js->eval("eval( js.file );") ) { + $logger->error("$file Eval failed: $@"); + return 0; + } + + close(F); + return 1; +} + +sub load_lib { + my( $self, $file ) = @_; + $self->run( $file ); +} + +sub _js_prop_name { + my $name = shift; + $name =~ s/^.*\.//o; + return $name; +} + +sub insert_fm { + + my( $self, $key, $fm ) = @_; + my $ctx = $self->context; + return undef unless ($ctx and $key and $fm); + my $o = $ctx->object_by_path($key); + + for my $f ( $fm->properties ) { + $ctx->property_by_path("$key.$f", $fm->$f(), + + sub { + my $k = _js_prop_name(shift()); + $fm->$k(); + }, + + sub { + my $k = _js_prop_name(shift()); + $fm->ischanged(1); + $fm->$k(@_); + } + ); + } +} + +sub insert_hash { + + my( $self, $key, $hash ) = @_; + my $ctx = $self->context; + return undef unless ($ctx and $key and $hash); + $ctx->object_by_path($key); + + for my $k ( keys %$hash ) { + $ctx->property_by_path( + "$key.$k", $hash->{$k}, + sub { $hash->{_js_prop_name(shift())} }, + sub { + my( $key, $val ) = @_; + $hash->{_js_prop_name($key)} = $val; } + ); + } +} + +1; -- 2.43.2