blib/lib/MOBY/Async/WSRF.pm untested

filter: Statementstmt 0.0% 0 / 298 Branchbran 0.0% 0 / 122 Conditioncond 0.0% 0 / 64 MC/DCmcdc 0.0% 0 / 35 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 555 CC 61 SCAR 54.0
CC
61
Cov
0%
std_envelope
650.0
header
110.0
GetMultipleResourceProperties
72.0
CRAP 222.0
1 =pod
2  
3 =head1 NAME
4  
5 MOBY::Async::WSRF - utilities to work with WSRF in MOBY
6  
7 =head1 AUTHORS
8  
9 Former developer
10 Enrique de Andres Saiz (enrique.deandres@pcm.uam.es) -
11 INB GNHC-1 (Madrid Science Park, Spain) (2006-2007).
12  
13 Maintainers
14 Jose Manuel Rodriguez (jmrodriguez@cnio.es),
15 Jose Maria Fernandez (jmfernandez@cnio.es) -
16 INB GN2 (CNIO, Spain).
17  
18 =head1 DESCRIPTION
19  
20 It extends L<WSRF::Lite> Perl module and provides everything required for
21 L<MOBY::Async::SimpleServer> class.
22  
23 It is not intendeed to be used directly unless you want to create a new class
24 as L<WSRF::Async::SimpleServer>.
25  
26 =cut
27  
28 package MOBY::Async::WSRF;
29 use strict;
30 use WSRF::Lite 0.8.2.2;
31 use File::Path;
32  
33 use vars qw /$VERSION/;
34 $VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /: (\d+)\.(\d+)/;
35  
36 $WSRF::WSRP::Private{queryIDs} = [];
37 $WSRF::WSRP::MobyPrivatePrefixes = ['pid', 'input'];
38 $WSRF::WSRP::MobyPropertiesPrefixes = ['status', 'result'];
39  
40 $WSRF::Constants::DataDir = (exists($ENV{TMPDIR}) && defined($ENV{TMPDIR}) && $ENV{TMPDIR} ne '')?$ENV{TMPDIR}:'/tmp';
41 mkpath($WSRF::Constants::DataDir,1,0777);
42 $WSRF::Constants::DataPrefix = 'moby_';
43 $WSRF::Constants::Data = $WSRF::Constants::DataDir .'/'. $WSRF::Constants::DataPrefix;
44 $WSRF::Constants::MOBY = 'http://biomoby.org/';
45 $WSRF::Constants::MOBY_MESSAGE_NS = 'http://www.biomoby.org/moby';
46 #$WSRF::Constants::WSA = 'http://www.w3.org/2005/08/addressing';
47 #$WSRF::Constants::WSRP = 'http://docs.oasis-open.org/wsrf/rp-2';
48 #$WSRF::Constants::WSRL = 'http://docs.oasis-open.org/wsrf/rl-2';
49 #$WSRF::Constants::WSSG = 'http://docs.oasis-open.org/wsrf/sg-2';
50 #$WSRF::Constants::WSBF = 'http://docs.oasis-open.org/wsrf/bf-2';
51 #$WSRF::Constants::WSA_ANON = 'http://www.w3.org/2005/08/addressing/anonymous';
52 $WSRF::Constants::WSRPW = 'http://docs.oasis-open.org/wsrf/rpw-2';
53 $WSRF::Constants::WSRLW = 'http://docs.oasis-open.org/wsrf/rlw-2';
54  
55 #===============================================================================
56 # WSRF::Serializer
57 #
58 # THIS CODE IS TAKEN FROM WSRF::LITE. I HAVE PUT $WSRF_HEADER VARIABLE, THEN I
59 # CAN INSERT HEADERS WHEN A FAULT OCCURS
60 #
61 package WSRF::Serializer;
62 use base qw(WSRF::WSRFSerializer);
63  
64 my $WSRF_HEADER;
65  
66 sub std_envelope {
67 SOAP::Trace::trace('()');
68 my $self = shift->new;
69 my $type = shift;
70  
71 $self->autotype(0);
72 $self->attr ({'xmlns:wsa' => $WSRF::Constants::WSA,
73 'xmlns:wsrl' => $WSRF::Constants::WSRL,
74 'xmlns:wsrp' => $WSRF::Constants::WSRP,
75 'xmlns:wsu' => $WSRF::Constants::WSU,
76 'xmlns:wsse' => $WSRF::Constants::WSSE,
77 'xmlns:mobyws' => $WSRF::Constants::MOBY
78 } );
79
80
81 my(@parameters, @header);
82 for (@_) {
83 # Find all the SOAP Headers
84 if (defined($_) && ref($_) && UNIVERSAL::isa($_ => 'SOAP::Header')) {
85 push(@header, $_);
86  
87 # Find all the SOAP Message Parts (attachments)
88 } elsif (defined($_) && ref($_) &&
89 $self->context && $self->context->packager->is_supported_part($_)
90 ) {
91 $self->context->packager->push_part($_);
92  
93 # Find all the SOAP Body elements
94 } else {
95 push(@parameters, $_);
96 }
97 }
98 my $header = @header ? SOAP::Data->set_value(@header) : undef;
99 $header = $WSRF_HEADER unless ($header); ########## THIS IS THE LINE I HAVE ADDED ##########
100 my($body,$parameters);
101 if ($type eq 'method' || $type eq 'response') {
102 SOAP::Trace::method(@parameters);
103  
104 my $method = shift(@parameters);
105 # or die "Unspecified method for SOAP call\n";
106  
107 $parameters = @parameters ? SOAP::Data->set_value(@parameters) : undef;
108 if (!defined($method)) {
109 } elsif (UNIVERSAL::isa($method => 'SOAP::Data')) {
110 $body = $method;
111 } elsif ($self->use_default_ns) {
112 if ($self->{'_ns_uri'}) {
113 $body = SOAP::Data->name($method)->attr( {
114 'xmlns' => $self->{'_ns_uri'},
115 } );
116 } else {
117 $body = SOAP::Data->name($method);
118 }
119 } else {
120 # Commented out by Byrne on 1/4/2006 - to address default namespace problems
121 # $body = SOAP::Data->name($method)->uri($self->{'_ns_uri'});
122 # $body = $body->prefix($self->{'_ns_prefix'}) if ($self->{'_ns_prefix'});
123  
124 # Added by Byrne on 1/4/2006 - to avoid the unnecessary creation of a new
125 # namespace
126 # Begin New Code (replaces code commented out above)
127 $body = SOAP::Data->name($method);
128 my $pre = $self->find_prefix($self->{'_ns_uri'});
129 $body = $body->prefix($pre) if ($self->{'_ns_prefix'});
130 # End new code
131  
132 }
133 # This is breaking a unit test right now...
134 $body->set_value(SOAP::Utils::encode_data($parameters ? \$parameters : ()))
135 if $body;
136 } elsif ($type eq 'fault') {
137 SOAP::Trace::fault(@parameters);
138 $body = SOAP::Data
139 ->name(SOAP::Utils::qualify($self->envprefix => 'Fault'))
140 # parameters[1] needs to be escaped - thanks to aka_hct at gmx dot de
141 # commented on 2001/03/28 because of failing in ApacheSOAP
142 # need to find out more about it
143 # -> attr({'xmlns' => ''})
144 ->value(\SOAP::Data->set_value(
145 SOAP::Data->name(faultcode => SOAP::Utils::qualify($self->envprefix => $parameters[0]))->type(""),
146 SOAP::Data->name(faultstring => SOAP::Utils::encode_data($parameters[1]))->type(""),
147 defined($parameters[2]) ? SOAP::Data->name(detail => do{my $detail = $parameters[2]; ref $detail ? \$detail : $detail}) : (),
148 defined($parameters[3]) ? SOAP::Data->name(faultactor => $parameters[3])->type("") : (),
149 ));
150 } elsif ($type eq 'freeform') {
151 SOAP::Trace::freeform(@parameters);
152 $body = SOAP::Data->set_value(@parameters);
153 } elsif (!defined($type)) {
154 # This occurs when the Body is intended to be null. When no method has been
155 # passed in of any kind.
156 } else {
157 die "Wrong type of envelope ($type) for SOAP call\n";
158 }
159
160 $self->seen({}); # reinitialize multiref table
161 # Build the envelope
162 # Right now it is possible for $body to be a SOAP::Data element that has not
163 # XML escaped any values. How do you remedy this?
164 my($encoded) = $self->encode_object(
165 SOAP::Data->name(
166 SOAP::Utils::qualify($self->envprefix => 'Envelope') => \SOAP::Data->value(
167 ($header ? SOAP::Data->name(SOAP::Utils::qualify($self->envprefix => 'Header') => \$header) : ()),
168 ($body ? SOAP::Data
169 ->name(SOAP::Utils::qualify($self->envprefix => 'Body') => \$body)
170 ->attr( {
171 'wsu:Id' => 'myBody'
172 } )
173 :
174 SOAP::Data
175 ->name(SOAP::Utils::qualify($self->envprefix => 'Body'))
176 ->attr( {
177 'wsu:Id' => 'myBody'
178 } )
179 ),
180 )
181 )->attr($self->attr)
182 );
183 $self->signature($parameters->signature) if ref $parameters;
184
185 # IMHO multirefs should be encoded after Body, but only some
186 # toolkits understand this encoding, so we'll keep them for now (04/15/2001)
187 # as the last element inside the Body
188 # v -------------- subelements of Envelope
189 # vv -------- last of them (Body)
190 # v --- subelements
191 push(@{$encoded->[2]->[-1]->[2]}, $self->encode_multirefs) if ref $encoded->[2]->[-1]->[2];
192
193 # Sometimes SOAP::Serializer is invoked statically when there is no context.
194 # So first check to see if a context exists.
195 # TODO - a context needs to be initialized by a constructor?
196 if ($self->context && $self->context->packager->parts) {
197 # TODO - this needs to be called! Calling it though wraps the payload twice!
198 # return $self->context->packager->package($self->xmlize($encoded));
199 }
200 return $self->xmlize($encoded);
201 }
202  
203  
204 #===============================================================================
205 # WSRF::FileBasedMobyResourceProperties
206 #
207 # Base module for the file based WSRF services - if a service inherits from this
208 # class then the ResourceProperties are stored in a file between calls.
209 # We inherit this to gain access to the envelope - see SOAP::Lite
210 #
211 # THIS CODE IS TAKEN FROM WSRF::LITE. I HAVE PUT WSRF::MobyFile INSTEAD OF
212 # WSRF::File.
213 #
214 package WSRF::FileBasedMobyResourceProperties;
215 use strict;
216 use XML::LibXML;
217 use base qw(WSRF::WSRP);
218  
219 # Load the ResourceProperties from the file into the ResourceProperties hash
220 # then call the super operation.
221 sub GetResourceProperty {
222 my $self = shift @_;
223 my $envelope = pop @_;
224
225 my $lock = WSRF::MobyFile->new($envelope);
226 $lock->toFile();
227  
228 my($isValidQName)=1;
229 my($search)=undef;
230 my($localsearch)=undef;
231 eval {
232 my($parser)=XML::LibXML->new();
233 my($context)=XML::LibXML::XPathContext->new();
234 $context->registerNs('wsrf-rp',$WSRF::Constants::WSRP);
235 my($envxml)=$parser->parse_string($envelope->raw_xml());
236 foreach my $searchnode ($context->findnodes('//wsrf-rp:GetResourceProperty',$envxml)) {
237 $search=$searchnode->textContent();
238
239 $localsearch=$search;
240 my($prefix)='';
241 my($icolon)=index($search,':');
242 if($icolon!=-1) {
243 $prefix=substr($search,0,$icolon);
244 $localsearch=substr($search,$icolon+1);
245 }
246 my($nsnode)=$searchnode->lookupNamespaceURI($prefix);
247 unless(defined($nsnode) && $nsnode eq $WSRF::Constants::MOBY) {
248 $isValidQName=undef;
249 }
250
251 last;
252 }
253 };
254  
255 if($@) {
256 $search = $envelope->valueof("//{$WSRF::Constants::WSRP}GetResourceProperty/");
257 $localsearch=$search;
258 my($prefix)='';
259 my($icolon)=index($search,':');
260 if($icolon!=-1) {
261 $prefix=substr($search,0,$icolon);
262 $localsearch=substr($search,$icolon+1);
263 }
264 }
265
266 WSRF::BaseFaults::die_with_fault( $envelope, (
267 BaseFault => "InvalidResourcePropertyQNameFault",
268 Description => "Property $search does not exist"
269 ) ) unless(defined($isValidQName) && exists($WSRF::WSRP::ResourceProperties{$localsearch}) && defined($WSRF::WSRP::ResourceProperties{$localsearch}));
270
271 my @resp = $self->SUPER::GetResourceProperty($envelope);
272 return @resp;
273 }
274  
275 # Load the ResourceProperties from the file into the ResourceProperties hash
276 # then call the super operation.
277 sub GetMultipleResourceProperties {
278 my $self = shift @_;
279 my $envelope = pop @_;
280  
281 my $lock = WSRF::MobyFile->new($envelope);
282 $lock->toFile();
283
284 my @notfound;
285 eval {
286 my($parser)=XML::LibXML->new();
287 my($context)=XML::LibXML::XPathContext->new();
288 $context->registerNs('wsrf-rp',$WSRF::Constants::WSRP);
289 my($envxml)=$parser->parse_string($envelope->raw_xml());
290 foreach my $searchnode ($context->findnodes('//wsrf-rp:ResourceProperty',$envxml)) {
291 my($search)=$searchnode->textContent();
292
293 my($localsearch)=$search;
294 my($prefix)='';
295 my($icolon)=index($search,':');
296 if($icolon!=-1) {
297 $prefix=substr($search,0,$icolon);
298 $localsearch=substr($search,$icolon+1);
299 }
300 my($isValidQName)=undef;
301 my($nsnode)=$searchnode->lookupNamespaceURI($prefix);
302 if(defined($nsnode) && $nsnode eq $WSRF::Constants::MOBY) {
303 $isValidQName=1;
304 }
305
306 push(@notfound, $search) unless(defined($isValidQName) && exists($WSRF::WSRP::ResourceProperties{$localsearch}) && defined($WSRF::WSRP::ResourceProperties{$localsearch}));
307
308 }
309 };
310  
311 if($@) {
312 foreach my $search ($envelope->valueof("//{$WSRF::Constants::WSRP}ResourceProperty/")) {
313 my($localsearch)=$search;
314 my($prefix)='';
315 my($icolon)=index($search,':');
316 if($icolon!=-1) {
317 $prefix=substr($search,0,$icolon);
318 $localsearch=substr($search,$icolon+1);
319 }
320
321 push(@notfound, $search) unless(exists($WSRF::WSRP::ResourceProperties{$localsearch}) && defined($WSRF::WSRP::ResourceProperties{$localsearch}));
322 }
323 }
324 WSRF::BaseFaults::die_with_fault( $envelope, (
325 BaseFault => "InvalidResourcePropertyQNameFault",
326 Description => "Property ".join(", ", @notfound) ." does not exist"
327 ) ) if (scalar(@notfound));
328
329 my @resp = $self->SUPER::GetMultipleResourceProperties($envelope);
330 return @resp;
331 }
332  
333  
334 #===============================================================================
335 # WSRF::FileBasedMobyResourceLifetimes
336 #
337 # Inherits from WSRF::FileBasedMobyResourceProperties, this class adds the
338 # required WSRL operations to the Service. Again all the ResourceProperties are
339 # stored in a file between calls
340 #
341 # THIS CODE IS TAKEN FROM WSRF::LITE. I HAVE PUT WSRF::MobyFile INSTEAD OF
342 # WSRF::File. I HAVE ALSO INCLUDED WSRF::BaseFaults.
343 #
344 package WSRF::FileBasedMobyResourceLifetimes;
345 use strict;
346 use base qw(WSRF::FileBasedMobyResourceProperties);
347  
348 # Add resource property TerminationTime - initalise to nothing (infinity).
349 $WSRF::WSRP::ResourceProperties{'TerminationTime'} = '';
350 $WSRF::WSRP::PropertyNamespaceMap->{TerminationTime}{prefix} = "wsrl";
351 $WSRF::WSRP::Nillable{TerminationTime} = 1;
352 $WSRF::WSRP::NotModifiable{TerminationTime} = 1;
353  
354 # Add resource property CurrentTime - in this case a subroutine that returns
355 # the current time in the correct format.
356 $WSRF::WSRP::ResourceProperties{'CurrentTime'} = sub {
357 return "<wsrl:CurrentTime>".
358 WSRF::Time::ConvertEpochTimeToString().
359 "</wsrl:CurrentTime>"; };
360 $WSRF::WSRP::PropertyNamespaceMap->{CurrentTime}{prefix} = "wsrl";
361 $WSRF::WSRP::NotDeletable{CurrentTime} = 1;
362 $WSRF::WSRP::NotModifiable{CurrentTime} = 1;
363  
364 # Remove the file with the resource properties in it.
365 sub Destroy {
366 my $self = shift @_;
367 my $envelope = pop @_;
368 my $lock = WSRF::MobyFile->new($envelope);
369 my $file = $WSRF::Constants::Data.$lock->ID();
370 unlink $file or WSRF::BaseFaults::die_with_fault( $envelope, (
371 BaseFault => "ResourceNotDestroyedFault",
372 Description => "Could not remove WS-Resource file"
373 ) );
374 return WSRF::Header::header($envelope);
375 }
376  
377  
378 #===============================================================================
379 # WSRF::BaseFaults (WS-BaseFaults spec.)
380 #
381 # This module allows you to return a WS-BaseFault. Simply call die_with_fault
382 # to case your service to through an exception.
383 #
384 # The function takes hash with the following:
385 # BaseFault (specific fault of BaseFault as default)
386 # OriginatorReference (where did the fault originally originate)
387 # ErrorCode (some code number)
388 # dialect (URI that defines the context in which the ErrorCode
389 # must be interpreted)
390 # Description (a description of the fault)
391 # FaultCause (underlying cause of this faulte)
392 #
393 # THIS CODE IS TAKEN FROM WSRF::LITE. I HAVE INCLUDED THE ENVELOPE PARAMETER
394 # (FOR CREATING THE WSRF HEADER WHEN THERE IS A FAULT) AND THE BASEFAULT KEY
395 # (FOR SPECIFYING WHICH KIND OF FAULT IT IS).
396 #
397 package WSRF::BaseFaults;
398 use strict;
399  
400 sub die_with_fault {
401 my ($envelope, %args) = @_;
402
403 # Has the client defined a BaseFault
404 my $fault;
405 if (defined($args{BaseFault})) {
406 $fault = "<wsbf:".$args{BaseFault}." xmlns:wsbf=\"$WSRF::Constants::WSBF\">";
407 } else {
408 $fault = "<wsbf:BaseFault xmlns:wsbf=\"$WSRF::Constants::WSBF\">";
409 }
410
411 # Timestamp
412 $fault .= "<wsbf:Timestamp>".WSRF::Time::ConvertEpochTimeToString(time)."</wsbf:Timestamp>";
413
414 # Has the client defined an OriginatorReference
415 if (defined($args{OriginatorReference})) {
416 $fault .= "<wsbf:OriginatorReference>".$args{OriginatorReference}."</wsbf:OriginatorReference>";
417 }
418
419 # Has the client defined an error code & dialect
420 if (defined($args{ErrorCode})) {
421 if (defined($args{dialect})) {
422 $fault .= "<wsbf:ErrorCode dialect=\"".$args{dialect}."\">".$args{ErrorCode}."</wsbf:ErrorCode>";
423 } else {
424 $fault .= "<wsbf:ErrorCode>".$args{ErrorCode}."</wsbf:ErrorCode>";
425 }
426 }
427
428 # Has the client defined a Description
429 if (defined($args{Description})) {
430 $fault .= "<wsbf:Description>".$args{Description}."</wsbf:Description>";
431 }
432
433 # Has the client defined a BaseCause
434 if (defined($args{FaultCause})) {
435 $fault .= "<wsbf:FaultCause>".$args{FaultCause}."</wsbf:FaultCause>";
436 }
437
438 # Has the client defined a BaseFault
439 if (defined($args{BaseFault})) {
440 $fault .= "</wsbf:".$args{BaseFault}.">";
441 } else {
442 $fault .= "</wsbf:BaseFault>";
443 }
444
445 $WSRF_HEADER = WSRF::Header::header($envelope, ( Action => "http://docs.oasis-open.org/wsrf/fault" ));
446 die SOAP::Fault->faultdetail($fault);
447 }
448  
449  
450 #===============================================================================
451 # WSRF::Header (WS-Address spec.)
452 #
453 # header function creates a SOAP::Header that should be included
454 # in the response to the client. Handles the WS-Address stuff.
455 # Takes the original envelope and creates a Header from it -
456 # the second paramter will be stuffed into the Header so must
457 # be XML
458 #
459 # BUG This should be better automated - probably in the SOAP serializer,
460 # not sure how because we need to remember the MessageID
461 #
462 # THIS CODE IS TAKEN FROM WSRF::LITE. I HAVE ADDED A SECOND PARAMETER
463 # WHICH IS A HASH WHOOSE KEYS ARE WSRF HEADERS WHICH MODIFIES
464 # THE DEFAULT BEHAVIOUR ON THE COMPOSITION OF THE HEADER.
465 #
466 package WSRF::Header;
467 use strict;
468  
469 my(%URI2ACTION)=(
470 $WSRF::Constants::WSRP => [$WSRF::Constants::WSRPW,undef],
471 $WSRF::Constants::WSRL => [$WSRF::Constants::WSRLW,'ImmediateResourceTermination']
472 );
473 no warnings 'redefine';
474 sub header {
475 my ($envelope, %args) = @_;
476 my $myHeader;
477
478 # wsa:To
479 if (defined($args{To})) {
480 $myHeader .= "<wsa:To wsu:Id=\"To\">".$args{To}."</wsa:To>";
481 } else {
482 $myHeader .= "<wsa:To wsu:Id=\"To\">$WSRF::Constants::WSA_ANON</wsa:To>";
483 }
484
485 # wsa:From
486 if (defined($args{From})) {
487 $myHeader .= "<wsa:From wsu:Id=\"From\">".$args{From}."</wsa:From>";
488 } else {
489 if ( $envelope->match("/{$SOAP::Constants::NS_ENV}Envelope/{$SOAP::Constants::NS_ENV}Header/{$WSRF::Constants::WSA}To") ) {
490 my $from = $envelope->valueof("/{$SOAP::Constants::NS_ENV}Envelope/{$SOAP::Constants::NS_ENV}Header/{$WSRF::Constants::WSA}To");
491 $myHeader .= "<wsa:From wsu:Id=\"From\"><wsa:Address>$from</wsa:Address></wsa:From>";
492 }
493 }
494
495 # wsa:MessageID
496 if (defined($args{MessageID})) {
497 $myHeader .= "<wsa:MessageID wsu:Id=\"MessageID\">".$args{MessageID}."</wsa:MessageID>";
498 } else {
499 $myHeader .= "<wsa:MessageID wsu:Id=\"MessageID\">".WSRF::WS_Address::MessageID()."</wsa:MessageID>";
500 }
501
502 # wsa:Action
503 if (defined($args{Action})) {
504 $myHeader .= "<wsa:Action wsu:Id=\"Action\">".$args{Action}."</wsa:Action>";
505 } else {
506 my $data = $envelope->match("/{$SOAP::Constants::NS_ENV}Envelope/{$SOAP::Constants::NS_ENV}Body/[1]")->dataof;
507 my $method = $data->name;
508 my $uri = $data->uri;
509 if(exists($URI2ACTION{$uri})) {
510 $uri = $URI2ACTION{$uri}[0].'/'.(defined($URI2ACTION{$uri}[1])?$URI2ACTION{$uri}[1]:$method);
511 }
512 $myHeader .= "<wsa:Action wsu:Id=\"Action\">".$uri."/".$method."Response</wsa:Action>";
513 }
514
515 # wsa:RelatesTo
516 if (defined($args{RelatesTo})) {
517 $myHeader .= "<wsa:RelatesTo wsu:Id=\"RelatesTo\">".$args{RelatesTo}."</wsa:RelatesTo>";
518 } else {
519 my $messageID = $envelope->headerof("//{$WSRF::Constants::WSA}MessageID");
520 if ( defined $messageID ) {
521 $messageID = $envelope->headerof("//{$WSRF::Constants::WSA}MessageID")->value;
522 $myHeader .= "<wsa:RelatesTo wsu:Id=\"RelatesTo\">".$messageID."</wsa:RelatesTo>";
523 }
524 }
525
526 # Create the SOAP::Header object and return it
527 return SOAP::Header->value($myHeader)->type('xml');
528 };
529  
530 #===============================================================================
531 # WSRF::MobyFile
532 #
533 # This module supports writing all the resource properties of a Resource to a
534 # file. Allows the state of the resource to be stored in a file between calls
535 # to the Resource.
536 #
537 # THIS CODE IS TAKEN FROM WSRF::LITE. I HAVE ONLY MODIFIED WHERE TO SEARCH THE
538 # ID (FROM AN ENVIRONMENT VARIABLE INSTEAD OF ENVELOPE) AND THE PROCESS TO
539 # LOAD AUTOMATICALLY THE PROPERTIES IN new METHOD, AND DESTROYIN THE LOCK IN
540 # toFile METHOD.
541 #
542 package WSRF::MobyFile;
543 use strict;
544  
545 use base qw(WSRF::File);
546  
547 sub new {
548 my( $class, $envelope, $ID) = @_;
549  
550 unless(defined($ID)) {
551 $ID = $envelope->valueof("/{$SOAP::Constants::NS_ENV}Envelope/{$SOAP::Constants::NS_ENV}Header/{$WSRF::Constants::MOBY}ServiceInvocationId");
552 $ENV{ID} = $ID;
553 }
554
555
556 # Check the ID is safe - we do not accept dots,
557 # All paths will be relative to $ENV{WRF_MODULES}
558 # Only allow alphanumeric, underscore and hyphen
559 if( $ID =~ /^([-\w]+)$/ ) {
560 $ID = $1;
561 } else {
562 WSRF::BaseFaults::die_with_fault( $envelope, (
563 BaseFault => "ResourceUnknownFault",
564 Description => "Badly formed WS-Resource Identifier $ID"
565 ) );
566 }
567
568 # ID can be of the form 1341-4565, we use this form to all multiple
569 # WS-Resources to share the same state, the state is in the file
570 # 1341 - we use this with ServiceGroup/ServiceGroupEntry
571 my $ID_clipped = $ID;
572 $ID_clipped =~ s/-\w*//o;
573
574 # File containing resource properties
575 my $path = $WSRF::Constants::Data.$ID_clipped;
576 WSRF::BaseFaults::die_with_fault( $envelope, (
577 BaseFault => "ResourceUnknownFault",
578 Description => "No WS-Resource with Identifer $ID"
579 ) ) if ( ! -e $path );
580
581 # The address of the lock file
582 my $lock = $path.".lock";
583
584 # Acquire a lock for the file
585 my $Lock = WSRF::FileLock->new($lock);
586
587 my $hashref = Storable::lock_retrieve($path);
588 %WSRF::WSRP::Private = (%WSRF::WSRP::Private, %{$hashref->{Private}});
589 foreach my $queryID (@{$WSRF::WSRP::Private{queryIDs}}) {
590 foreach my $privatePrefix (@{$WSRF::WSRP::MobyPrivatePrefixes}) {
591 $WSRF::WSRP::Private{$privatePrefix.'_'.$queryID} = $WSRF::WSRP::Private{$privatePrefix.'_'.$queryID} || '';
592 }
593 foreach my $propertyPrefix (@{$WSRF::WSRP::MobyPropertiesPrefixes}) {
594 $WSRF::WSRP::ResourceProperties{$propertyPrefix.'_'.$queryID} = $WSRF::WSRP::ResourceProperties{$propertyPrefix.'_'.$queryID} || '';
595 $WSRF::WSRP::PropertyNamespaceMap->{$propertyPrefix.'_'.$queryID}{prefix} = 'mobyws';
596 $WSRF::WSRP::PropertyNamespaceMap->{$propertyPrefix.'_'.$queryID}{namespace} = $WSRF::Constants::MOBY;
597 $WSRF::WSRP::NotDeletable{$propertyPrefix.'_'.$queryID} = 1;
598 $WSRF::WSRP::NotModifiable{$propertyPrefix.'_'.$queryID} = 1;
599 }
600 }
601 %WSRF::WSRP::ResourceProperties = (%WSRF::WSRP::ResourceProperties, %{$hashref->{Properties}});
602
603 # Check that the resource is still alive - if TT time is not
604 # set then TT is infinity
605 if ( defined($WSRF::WSRP::ResourceProperties{'TerminationTime'}) &&
606 ($WSRF::WSRP::ResourceProperties{'TerminationTime'} ne "") ) {
607 if ( WSRF::Time::ConvertStringToEpochTime($WSRF::WSRP::ResourceProperties{'TerminationTime'}) < time ) {
608
609 unlink $path or die SOAP::Fault->faultcode("Container Failure")
610 ->faultstring("Container Failure: Could not remove file");
611 rmdir $lock or die SOAP::Fault->faultcode("Container Failure")
612 ->faultstring("Container Failure: Could not remove lock file");
613 WSRF::BaseFaults::die_with_fault( $envelope, (
614 BaseFault => "ResourceUnknownFault",
615 Description => "No such WS-Resource $ID - Lifetime expired"
616 ) );
617 }
618 }
619
620 bless {
621 _ID => $ID,
622 _path => $path,
623 _lock => $Lock
624 }, $class;
625 }
626  
627 sub toFile {
628 my $class = $_[0];
629 $class->SUPER::toFile(@_);
630 my $lock = ref($class) ? $class->{_lock} : '';
631 $lock->DESTROY if ($lock);
632 }
633  
634  
635 #===============================================================================
636 # WSRF::FileLock
637 #
638 # This module provides file locking for us - when an object of this class is
639 # created a lock file is created. The lock file is automatically removed when
640 # the object is destroyed.
641 #
642 # THIS CODE IS TAKEN FROM WSRF::LITE. I HAVE ONLY DELETED LOGS.
643 #
644 package WSRF::FileLock;
645 use strict;
646  
647 sub new {
648 my ($self, $file) = @_;
649 until ( mkdir $file ) {
650 select(undef,undef,undef,0.5);
651 }
652 bless{
653 _file => $file
654 }, $self;
655 }
656  
657 sub DESTROY {
658 my ($self) = @_;
659 if( -d $self->{_file} ) {
660 rmdir $self->{_file} or die SOAP::Fault->faultcode("Container Failure")
661 ->faultstring("Container Failure: Could not remove WS-Resource lock file");
662 }
663 }
664  
665 1;