diff --git a/lib/WebGUI/Operation/Workflow.pm b/lib/WebGUI/Operation/Workflow.pm index cdd7bee42..82510c744 100644 --- a/lib/WebGUI/Operation/Workflow.pm +++ b/lib/WebGUI/Operation/Workflow.pm @@ -15,6 +15,7 @@ use Tie::IxHash; use WebGUI::AdminConsole; use WebGUI::HTMLForm; use WebGUI::International; +use WebGUI::Pluggable; use WebGUI::Workflow; use WebGUI::Workflow::Activity; use WebGUI::Workflow::Instance; @@ -80,25 +81,9 @@ sub www_activityHelper { my $sub = $form->get("sub"); return "ERROR" unless (defined $sub && defined $class); - # Load the modules - my $load = "use ".$class; - eval($load); + my $output = eval {WebGUI::Pluggable::instanciate($class, "www_".$sub, [$session])}; if ($@) { - $session->errorHandler->error("Couldn't load activity helper class $class because $@"); - return "ERROR"; - } - - # Make sure the subroutine exists - my $command = $class->can('www_'.$sub); - if (!$command) { - $session->errorHandler->error("Couldn't execute activity helper ${class}::www_${sub} because subroutine does not exist"); - return "ERROR"; - } - - # Execute - my $output = eval { $command->($session) }; - if ($@) { - $session->errorHandler->error("Couldn't execute activity helper ${class}::www_${sub} because $@"); + $session->errorHandler->error($@); return "ERROR"; } return $output; diff --git a/lib/WebGUI/Pluggable.pm b/lib/WebGUI/Pluggable.pm index 2dea8637e..4cbf33e02 100644 --- a/lib/WebGUI/Pluggable.pm +++ b/lib/WebGUI/Pluggable.pm @@ -29,11 +29,11 @@ This package provides a standard way of quickly and safely dynamically loading p use WebGUI::Pluggable; - WebGUI::Pluggable::load($module); + eval { WebGUI::Pluggable::load($module) }; - my $object = WebGUI::Pluggable::instanciate($module, $method, \@params); + my $object = eval { WebGUI::Pluggable::instanciate($module, $method, \@params) }; - my $output = WebGUI::Pluggable::run($module, $function, \@params); + my $output = eval { WebGUI::Pluggable::run($module, $function, \@params) }; =head1 FUNCTIONS @@ -60,11 +60,17 @@ sub instanciate { croak "Could not instanciate object using $sub on $module because $module could not be loaded."; } else { + unless ($module->can($sub)) { + croak "Could not instanciate object using $sub on $module because $sub is not a valid method."; + } my $object = eval{$module->$sub(@{$params})}; if ($@) { croak "Could not instanciate object using $sub on $module because $@"; } else { + unless (defined $object) { + croak "Could not instanciate object using $sub on $module. The result is undefined."; + } return $object; } } diff --git a/lib/WebGUI/Workflow/Activity.pm b/lib/WebGUI/Workflow/Activity.pm index c3b7ab0d5..e6ced0cd1 100644 --- a/lib/WebGUI/Workflow/Activity.pm +++ b/lib/WebGUI/Workflow/Activity.pm @@ -17,6 +17,7 @@ package WebGUI::Workflow::Activity; use strict; use WebGUI::HTMLForm; +use WebGUI::Pluggable; =head1 NAME @@ -297,11 +298,11 @@ sub new { my $main = $session->db->getRow("WorkflowActivity","activityId", $activityId); return unless $main->{activityId}; $class = $main->{className}; - (my $module = "$class.pm") =~ s{'|::}{/}g; - unless (eval { require $module; 1 }) { - $session->errorHandler->error("Couldn't compile workflow activity package: ".$class.". Root cause: ".$@); + eval { WebGUI::Pluggable::load($class) }; + if ($@) { + $session->errorHandler->error($@); return; - } + } my $sub = $session->db->buildHashRef("select name,value from WorkflowActivityData where activityId=?",[$activityId]); my %data = (%{$main}, %{$sub}); for my $definition (reverse @{$class->definition($session)}) { @@ -331,19 +332,18 @@ A properties hash reference. The className of the properties hash must be valid. =cut sub newByPropertyHashRef { - my $class = shift; - my $session = shift; - my $properties = shift; - return unless defined $properties; - return unless exists $properties->{className}; - my $className = $properties->{className}; - my $cmd = "use ".$className; - eval ($cmd); - if ($@) { - $session->errorHandler->warn("Couldn't compile activity package: ".$className.". Root cause: ".$@); - return; - } - bless {_session=>$session, _id=>$properties->{activityId}, _data => $properties}, $className; + my $class = shift; + my $session = shift; + my $properties = shift; + return unless defined $properties; + return unless exists $properties->{className}; + my $className = $properties->{className}; + eval { WebGUI::Pluggable::load($class) }; + if ($@) { + $session->errorHandler->error($@); + return; + } + bless {_session=>$session, _id=>$properties->{activityId}, _data => $properties}, $className; } #------------------------------------------------------------------- diff --git a/lib/WebGUI/Workflow/Instance.pm b/lib/WebGUI/Workflow/Instance.pm index 0bf65c9e1..3a2b6b15f 100644 --- a/lib/WebGUI/Workflow/Instance.pm +++ b/lib/WebGUI/Workflow/Instance.pm @@ -17,6 +17,7 @@ package WebGUI::Workflow::Instance; use strict; use JSON; +use WebGUI::Pluggable; use WebGUI::Workflow::Spectre; use WebGUI::Workflow; @@ -311,42 +312,25 @@ sub run { my $method = $self->get("methodName"); my $params = $self->get("parameters"); my $status = ""; + my $object = undef; if ($class && $method) { - my $cmd = "use $class"; - eval($cmd); - if ($@) { - $self->session->errorHandler->error("Error loading activity class $class: ".$@); + $object = eval { WebGUI::Pluggable::instanciate($class, $method, [$self->session, $params]) }; + if ($@) { + $self->session->errorHandler->error($@); $self->set({lastStatus=>"error", notifySpectre=>0}); return "error"; - } - my $object = eval{ $class->$method($self->session, $params) }; - if ($@) { - $self->session->errorHandler->error("Error instanciating activity (".$activity->getId.") pass-in object: ".$@); - $self->set({lastStatus=>"error", notifySpectre=>0}); - return "error"; - } - unless (defined $object) { - $self->session->errorHandler->error("Pass in object came back undefined for activity (".$activity->getId.") using ".$class.", ".$method.", ".$params."."); - $self->set({lastStatus=>"error", notifySpectre=>0}); - return "error"; - } - $status = eval{$activity->execute($object, $self)}; - if ($@) { - $self->session->errorHandler->error("Caught exception executing workflow activity ".$activity->getId." for instance ".$self->getId." which reported ".$@); - $self->set({lastStatus=>"error", notifySpectre=>0}); - return "error"; - } - } else { - $status = $activity->execute(undef, $self); - if ($@) { - $self->session->errorHandler->error("Caught exception executing workflow activity ".$activity->getId." for instance ".$self->getId." which reported ".$@); - $self->set({lastStatus=>"error", notifySpectre=>0}); - return "error"; - } + } + } + $status = eval { $activity->execute($object, $self) }; + if ($@) { + $self->session->errorHandler->error("Caught exception executing workflow activity ".$activity->getId." for instance ".$self->getId." which reported ".$@); + $self->set({lastStatus=>"error", notifySpectre=>0}); + return "error"; } if ($status eq "complete") { $self->set({lastStatus=>"complete", "currentActivityId"=>$activity->getId, notifySpectre=>0}); - } else { + } + else { $self->set({lastStatus=>$status, notifySpectre=>0}); } return $status;