| 1 | #/*==================================================================== | ||
| 2 | # * Babel Objects, Version 1.0 | ||
| 3 | # * ==================================================================== | ||
| 4 | # * | ||
| 5 | # * Copyright (c) 2000 The Babel Objects Network. All rights reserved. | ||
| 6 | # * | ||
| 7 | # * This source file is subject to version 1.1 of The Babel Objects | ||
| 8 | # * License, that is bundled with this package in the file LICENSE, | ||
| 9 | # * and is available through the world wide web at : | ||
| 10 | # * | ||
| 11 | # * http://www.BabelObjects.Org/law/license/1.1.txt | ||
| 12 | # * | ||
| 13 | # * If you did not receive a copy of the Babel Objects license and are | ||
| 14 | # * unable to obtain it through the world wide web, please send a note | ||
| 15 | # * to license@BabelObjects.Org so we can mail you a copy immediately. | ||
| 16 | # * | ||
| 17 | # * -------------------------------------------------------------------- | ||
| 18 | # * | ||
| 19 | # * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | ||
| 20 | # * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
| 21 | # * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| 22 | # * DISCLAIMED. IN NO EVENT SHALL THE BABEL OBJECTS NETWORK OR | ||
| 23 | # * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 24 | # * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| 25 | # * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | ||
| 26 | # * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
| 27 | # * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
| 28 | # * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | ||
| 29 | # * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 30 | # * SUCH DAMAGE. | ||
| 31 | # * | ||
| 32 | # * ==================================================================== | ||
| 33 | # * | ||
| 34 | # * This software consists of voluntary contributions made by many | ||
| 35 | # * individuals on behalf of The Babel Objects Network. For more | ||
| 36 | # * information on The Babel Objects Network, please see | ||
| 37 | # * <http://www.BabelObjects.org/>. | ||
| 38 | # * | ||
| 39 | # */ | ||
| 40 | |||
| 41 | package BabelObjects::Runner::Dispatcher; | ||
| 42 | |||
| 43 | use Carp; | ||
| 44 | use strict; | ||
| 45 | |||
| 46 | use BabelObjects::Util::Dvlpt::Log; | ||
| 47 | |||
| 48 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $AUTOLOAD); | ||
| 49 | |||
| 50 | require Exporter; | ||
| 51 | require AutoLoader; | ||
| 52 | |||
| 53 | @ISA = qw(Exporter AutoLoader); | ||
| 54 | # Items to export into callers namespace by default. Note: do not export | ||
| 55 | # names by default without a very good reason. Use EXPORT_OK instead. | ||
| 56 | # Do not simply export all your public functions/methods/constants. | ||
| 57 | @EXPORT = qw( | ||
| 58 | ); | ||
| 59 | $VERSION = '1.00'; | ||
| 60 | |||
| 61 | my $newfilename; | ||
| 62 | my $aLog; | ||
| 63 | |||
| 64 | my %fields = ( | ||
| 65 | runData => undef | ||
| 66 | ); | ||
| 67 | |||
| 68 | sub new { | ||
| 69 | my $proto = shift; | ||
| 70 | my $args = shift; | ||
| 71 | |||
| 72 | my $class = ref($proto) || $proto; | ||
| 73 | |||
| 74 | my $self = { | ||
| 75 | _permitted => \%fields, | ||
| 76 | %fields, | ||
| 77 | }; | ||
| 78 | |||
| 79 | bless ($self, $class); | ||
| 80 | |||
| 81 | my %parameters = %$args; | ||
| 82 | |||
| 83 | $aLog = new BabelObjects::Util::Dvlpt::Log(); | ||
| 84 | #$aLog->log("\n-- BabelObjects::Runner::Dispatcher --"); | ||
| 85 | |||
| 86 | foreach (keys %parameters) { | ||
| 87 | # the following lines are useful to verify argument values | ||
| 88 | #$aLog->log("Before : $_ = ".$parameters{$_}); | ||
| 89 | $self->$_($parameters{$_}); | ||
| 90 | #$aLog->log("After : $_ = ".$self->$_); | ||
| 91 | } | ||
| 92 | |||
| 93 | return $self; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub executeBO { | ||
| 97 | my $self = shift; | ||
| 98 | (my $aClassName, my $aMethodName) = @_; | ||
| 99 | |||
| 100 | # 3 steps | ||
| 101 | # 1 - creates an instance of aClassName | ||
| 102 | # 2 - tries : | ||
| 103 | # * aMethodName in uppercase : getXXXX() | ||
| 104 | # * aMethodName according bo info answer : getXxXx() | ||
| 105 | # * aMethodName as a parameter according bo info : getValue(XxXx) | ||
| 106 | # 3 - | ||
| 107 | |||
| 108 | #$aLog->log("executeBO"); | ||
| 109 | #$aLog->log(" ClassName = $aClassName"); | ||
| 110 | #$aLog->log(" MethodName = $aMethodName"); | ||
| 111 | |||
| 112 | eval("require $aClassName"); | ||
| 113 | |||
| 114 | # be careful : $runData is a reference on a hashtable | ||
| 115 | # to access values, use $$runData{itemkey} | ||
| 116 | |||
| 117 | my %parameters = (); | ||
| 118 | $parameters{"runData"} = $self->runData; | ||
| 119 | #$aLog->log("runData : ".$self->runData); | ||
| 120 | my $aObject = $aClassName->new(\%parameters); | ||
| 121 | #$aLog->log(" $aObject created"); | ||
| 122 | |||
| 123 | my %boAttributes = $aObject->getBoInfo(); | ||
| 124 | |||
| 125 | #foreach (keys %boAttributes) { | ||
| 126 | # $aLog->log("Attrib = ".$boAttributes{$_}."\n"); | ||
| 127 | #} | ||
| 128 | |||
| 129 | my $realMethod = $boAttributes{lc($aMethodName)}; | ||
| 130 | my $realGetMethod = "get".$realMethod; | ||
| 131 | |||
| 132 | my $result = $aObject->$realGetMethod ; | ||
| 133 | |||
| 134 | #$aLog->log("Result = $result"); | ||
| 135 | |||
| 136 | if ( $result =~ /$realGetMethod/ ) { | ||
| 137 | # if ( $result =~ /$aClassName::$realGetMethod/ ) { | ||
| 138 | |||
| 139 | #print "getValue call : getValue(", $realMethod, ")\n"; | ||
| 140 | # the getXXXX() or getXxXx method doesn't exist, | ||
| 141 | # so we try the get method with the $realMethod like | ||
| 142 | # a parameter and we print it | ||
| 143 | #$aLog->log(" Real Method = $realMethod"); | ||
| 144 | $result = $aObject->getValue($realMethod); | ||
| 145 | # if ( $result =~ /$aClassName::getValue/ ) { | ||
| 146 | if ( $result =~ /getValue/ ) { | ||
| 147 | # there is really an error | ||
| 148 | $aLog->log("There is really an error"); | ||
| 149 | exit(); | ||
| 150 | } else { | ||
| 151 | $self->boprint($result); | ||
| 152 | } | ||
| 153 | } else { | ||
| 154 | # if $realMethod is in uppercase, the method prints the result | ||
| 155 | # else the actual method (executeBO) prints the result | ||
| 156 | if ( $realMethod ne uc($realMethod) ) { | ||
| 157 | # the method isn't in upper case so we print the result | ||
| 158 | $self->boprint($result); | ||
| 159 | } | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | # aClassName String long class name | ||
| 164 | sub executeTransition { | ||
| 165 | my $self = shift; | ||
| 166 | my ($aClassName, $aMethodName) = @_; | ||
| 167 | |||
| 168 | #$aLog->log("executeTransition"); | ||
| 169 | #$aLog->log(" ClassName = $aClassName"); | ||
| 170 | #$aLog->log(" MethodName = $aMethodName"); | ||
| 171 | |||
| 172 | eval("require $aClassName"); | ||
| 173 | |||
| 174 | # be careful : $runData is a reference on a hashtable | ||
| 175 | # to access values, use $$runData{itemkey} | ||
| 176 | |||
| 177 | my %parameters = (runData => $self->runData); | ||
| 178 | my $aObject = $aClassName->new(\%parameters); | ||
| 179 | #$aLog->log("$aObject is created"); | ||
| 180 | |||
| 181 | my %boAttributes = $aObject->getBoInfo(); | ||
| 182 | |||
| 183 | #foreach (keys %boAttributes) { | ||
| 184 | # print STDERR "$_ => ", $boAttributes{$_}, "<br>\n"; | ||
| 185 | #} | ||
| 186 | |||
| 187 | my $realMethod = $boAttributes{lc($aMethodName)}; | ||
| 188 | #$aLog->log("Real Method = $realMethod"); | ||
| 189 | |||
| 190 | my $result = $aObject->$realMethod; | ||
| 191 | #$aLog->log("Result = $result"); | ||
| 192 | |||
| 193 | if ( ($result eq "") || ($result =~ /$realMethod/) ) { | ||
| 194 | # if ( $result =~ /$aClassName::$realGetMethod/ ) { | ||
| 195 | #print "realMethod call : ", $realMethod, " -> $result\n"; | ||
| 196 | # the realmethod doesn't exist => error | ||
| 197 | return "PB"; | ||
| 198 | } else { | ||
| 199 | if ($result eq "true") { | ||
| 200 | return "OK"; | ||
| 201 | } elsif ($result eq "false") { | ||
| 202 | return "NOK"; | ||
| 203 | } else { | ||
| 204 | return $result; | ||
| 205 | } | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | sub parseAndExecuteTransition { | ||
| 210 | my $self = shift; | ||
| 211 | |||
| 212 | my $target; | ||
| 213 | my $result; | ||
| 214 | |||
| 215 | my $transition = $self->runData->getParameter("T"); | ||
| 216 | my $prefix = $self->runData->getConfParameter("global/parameter/webDir"); | ||
| 217 | #my $transitionPath = $self->runData->req->url(-absolute=>1); | ||
| 218 | my $transitionPath = $self->runData->req->url(-path_info=>1); | ||
| 219 | |||
| 220 | #$aLog->log("parseAndExecuteTransition"); | ||
| 221 | #$aLog->log(" T = $transition"); | ||
| 222 | #$aLog->log(" Path info = $transitionPath"); | ||
| 223 | #$aLog->log(" Prefix info = $prefix"); | ||
| 224 | |||
| 225 | if ($transition ne "") { | ||
| 226 | $_ = $transition; | ||
| 227 | (my $shortClass, my $shortMethod) = /(.*)\.(.*)/; | ||
| 228 | |||
| 229 | #$aLog->log(" ShortClass = $shortClass"); | ||
| 230 | #$aLog->log(" ShortMethod = $shortMethod"); | ||
| 231 | |||
| 232 | my $aClassName = $self->runData->getConfParameter("classes/parameter/$shortClass"); | ||
| 233 | my $aMethodName = "t_".$shortMethod; | ||
| 234 | |||
| 235 | $result = $self->executeTransition($aClassName, $aMethodName); | ||
| 236 | #$aLog->log("Transition's result = $result"); | ||
| 237 | |||
| 238 | my $path = $self->runData->getParameter($result); | ||
| 239 | $_ = $path; | ||
| 240 | #$aLog->log(" OK's Url = ".$_); | ||
| 241 | if (m!.*://!) { | ||
| 242 | # It's an Url | ||
| 243 | $target = $path; | ||
| 244 | } elsif (m!\.\.!) { | ||
| 245 | # we block potential intruders | ||
| 246 | $target = ""; | ||
| 247 | } elsif (m!^/!) { | ||
| 248 | # absolute path | ||
| 249 | $target = $prefix.$path; | ||
| 250 | } else { | ||
| 251 | $_ = $transitionPath; | ||
| 252 | #$aLog->log(" Transition path = ".$_); | ||
| 253 | if (! /\.\./) { | ||
| 254 | # path relative to the action path | ||
| 255 | (my $dir) = $transitionPath =~ /http:\/\/[^\/]*\/cgi-bin\/Controller[^\/]*(\/.*)\/[^\/]*$/; | ||
| 256 | #$aLog->log(" T - Relative path = ".$dir); | ||
| 257 | $target = $prefix.$dir."/".$path; | ||
| 258 | } else { | ||
| 259 | # back relative path are prohibited | ||
| 260 | $target = $prefix.$path; | ||
| 261 | } | ||
| 262 | } | ||
| 263 | } else { | ||
| 264 | $_ = $self->runData->req->url(-path_info=>1); | ||
| 265 | #$aLog->log(" Path info = ".$_); | ||
| 266 | (my $relativePath) = /\/cgi-bin\/Controller[^\/]*(\/.*)/; | ||
| 267 | #(my $relativePath) = /http:\/\/[^\/]*(\/.*)$/; | ||
| 268 | #(my $relativePath) = /([^\/]*)$/; | ||
| 269 | #$aLog->log(" RelativePath = ".$relativePath); | ||
| 270 | $target = $prefix.$relativePath; | ||
| 271 | } | ||
| 272 | |||
| 273 | return $target; | ||
| 274 | } | ||
| 275 | |||
| 276 | sub parseAndSubstituteBO { | ||
| 277 | my $self = shift; | ||
| 278 | my $aLine = shift; | ||
| 279 | |||
| 280 | $_ = $aLine; | ||
| 281 | #s/\$/\\\$/g; | ||
| 282 | $aLine = $_; | ||
| 283 | |||
| 284 | #my $regExp = "/\$\([A-Z_0-9\.]*\)/"; | ||
| 285 | #my @results = split($regExp, $aLine, 1); | ||
| 286 | my @results = split(/(\$\([A-Za-z_0-9\.]*\))/, $aLine); | ||
| 287 | |||
| 288 | # html parts and bo tags are together so we test if $result starts with... | ||
| 289 | my $result; | ||
| 290 | foreach (@results) { | ||
| 291 | if (/^\$\([A-Za-z_0-9\.]*\)/) { | ||
| 292 | # it's a bo tag | ||
| 293 | (my $aClassName, my $aMethodName) = /\$\(([A-Z_a-z]*)\.([A-Z_a-z]*)\)/; | ||
| 294 | |||
| 295 | #$aLog->log("parseAndSubstituteBO"); | ||
| 296 | #$aLog->log(" ClassName = $aClassName"); | ||
| 297 | #$aLog->log(" MethodName = $aMethodName"); | ||
| 298 | #$aLog->log(" RunData = ".$self->runData); | ||
| 299 | |||
| 300 | my $longClassName = $self->runData->getConfParameter("classes/parameter/".lc($aClassName)); | ||
| 301 | $self->executeBO($longClassName, $aMethodName); | ||
| 302 | } else { | ||
| 303 | $self->boprint($_); | ||
| 304 | } | ||
| 305 | } | ||
| 306 | } | ||
| 307 | |||
| 308 | sub parseFile { | ||
| 309 | my $self = shift; | ||
| 310 | my $filename = shift; | ||
| 311 | |||
| 312 | $newfilename = shift; | ||
| 313 | |||
| 314 | if ($newfilename eq $filename) { | ||
| 315 | #$aLog->log("ERROR : source and target filename are the same\n"); | ||
| 316 | exit(); | ||
| 317 | } | ||
| 318 | |||
| 319 | if ($newfilename ne "") { | ||
| 320 | open(NEWFILE, ">$newfilename"); | ||
| 321 | } | ||
| 322 | |||
| 323 | #$aLog->log("INFO : filename = $filename"); | ||
| 324 | open(FILE, "$filename") || die $aLog->log("The $filename can't be opened"); | ||
| 325 | while (<FILE>) { | ||
| 326 | # $_ contains line feed : no need to add it | ||
| 327 | $self->parseAndSubstituteBO($_); | ||
| 328 | } | ||
| 329 | close(FILE); | ||
| 330 | |||
| 331 | if ($newfilename ne "") { | ||
| 332 | close(NEWFILE); | ||
| 333 | } | ||
| 334 | } | ||
| 335 | |||
| 336 | sub boprint { | ||
| 337 | my $self = shift; | ||
| 338 | my $aString = shift; | ||
| 339 | |||
| 340 | if ($newfilename ne "") { | ||
| 341 | print NEWFILE $aString; | ||
| 342 | } else { | ||
| 343 | print $aString; | ||
| 344 | } | ||
| 345 | } | ||
| 346 | |||
| 347 | sub AUTOLOAD { | ||
| 348 | my $self = shift; | ||
| 349 | my $type = ref($self) or croak "$self is not an object"; | ||
| 350 | |||
| 351 | my $name = $AUTOLOAD; | ||
| 352 | $name =~ s/.*://; # strip fully-qualified portion | ||
| 353 | |||
| 354 | unless (exists $self->{_permitted}->{$name} ) { | ||
| 355 | #croak "Can't access `$name' field in class $type"; | ||
| 356 | # On intercepte ici les erreurs liées aux tentatives d'appel | ||
| 357 | # des méthodes inexistantes | ||
| 358 | #$aLog->log("AUTOLOAD : $self = $AUTOLOAD"); | ||
| 359 | return $AUTOLOAD; | ||
| 360 | } | ||
| 361 | |||
| 362 | if (@_) { | ||
| 363 | return $self->{$name} = shift; | ||
| 364 | } else { | ||
| 365 | return $self->{$name}; | ||
| 366 | } | ||
| 367 | } | ||
| 368 | |||
| 369 | 1; | ||
| 370 | |||
| 371 | __END__ |