1 |
## -------------------------------------------------------------------------------- |
## ------------------------------------------------------------------------ |
2 |
## $Id$ |
## $Id$ |
3 |
## -------------------------------------------------------------------------------- |
## ------------------------------------------------------------------------ |
4 |
## $Log$ |
## $Log$ |
5 |
|
## Revision 1.18 2003/06/06 03:40:57 joko |
6 |
|
## disabled autovivifying of arguments as attributes |
7 |
|
## |
8 |
|
## Revision 1.17 2003/05/13 07:58:49 joko |
9 |
|
## fix: die if methodname is empty |
10 |
|
## fixes to log-string |
11 |
|
## |
12 |
|
## Revision 1.16 2003/04/18 16:07:53 joko |
13 |
|
## just use logger if instantiation successed |
14 |
|
## |
15 |
|
## Revision 1.15 2003/02/20 20:19:13 joko |
16 |
|
## tried to get auto-disconnect working again - failed with that |
17 |
|
## |
18 |
|
## Revision 1.14 2003/02/09 05:12:28 joko |
19 |
|
## + quoting of strings used in sql-queries! |
20 |
|
## |
21 |
|
## Revision 1.13 2003/01/30 22:27:05 joko |
22 |
|
## + added new abstract methods |
23 |
|
## |
24 |
|
## Revision 1.12 2003/01/30 21:46:32 joko |
25 |
|
## + fixed behaviour of AUTOLOAD-method |
26 |
|
## |
27 |
|
## Revision 1.11 2003/01/20 16:43:18 joko |
28 |
|
## + better argument-property merging |
29 |
|
## + debugging-output !!! |
30 |
|
## + abstract-dummy 'sub createChildNode' |
31 |
|
## |
32 |
|
## Revision 1.10 2003/01/19 02:31:51 joko |
33 |
|
## + fix to 'sub AUTOLOAD' |
34 |
|
## |
35 |
|
## Revision 1.9 2002/12/19 16:30:23 joko |
36 |
|
## + added 'sub dropDb' and 'sub rebuildDb' as croakers for concrete implementations of methods in proper handlers |
37 |
|
## |
38 |
|
## Revision 1.8 2002/12/13 21:48:35 joko |
39 |
|
## + sub _abstract_function |
40 |
|
## |
41 |
## Revision 1.7 2002/12/05 07:57:48 joko |
## Revision 1.7 2002/12/05 07:57:48 joko |
42 |
## + now using Tie::SecureHash as a base for the COREHANDLE |
## + now using Tie::SecureHash as a base for the COREHANDLE |
43 |
## + former public COREHANDLE becomes private _COREHANDLE now |
## + former public COREHANDLE becomes private _COREHANDLE now |
60 |
## |
## |
61 |
## Revision 1.1 2002/10/10 03:44:07 cvsjoko |
## Revision 1.1 2002/10/10 03:44:07 cvsjoko |
62 |
## + new |
## + new |
63 |
## -------------------------------------------------------------------------------- |
## ------------------------------------------------------------------------ |
64 |
|
|
65 |
|
|
66 |
package Data::Storage::Handler::Abstract; |
package Data::Storage::Handler::Abstract; |
68 |
use strict; |
use strict; |
69 |
use warnings; |
use warnings; |
70 |
|
|
71 |
|
use base qw( DesignPattern::Object ); |
72 |
|
|
73 |
|
|
74 |
use Data::Dumper; |
use Data::Dumper; |
75 |
use Tie::SecureHash; |
use Tie::SecureHash; |
76 |
#use Data::Storage::Handler; |
#use Data::Storage::Handler; |
77 |
|
use Hash::Merge qw( merge ); |
78 |
|
|
79 |
|
#use Log::Dispatch::Config; |
80 |
|
#Log::Dispatch::Config->configure(); |
81 |
|
|
82 |
# get logger instance |
# get logger instance |
83 |
my $logger = Log::Dispatch::Config->instance; |
my $logger; |
84 |
|
eval('$logger = Log::Dispatch::Config->instance;'); |
85 |
|
|
86 |
#our $lock_info; |
#our $lock_info; |
87 |
|
|
90 |
my $class = ref($invocant) || $invocant; |
my $class = ref($invocant) || $invocant; |
91 |
|
|
92 |
# logging info about the actual handler called |
# logging info about the actual handler called |
93 |
$logger->debug( "$invocant->new( @_ )" ); |
$logger->debug( "$invocant->new( @_ )" ) if $logger; |
94 |
#$logger->debug( __PACKAGE__ . "->" . "new()" ); |
#$logger->debug( __PACKAGE__ . "->" . "new()" ); |
95 |
|
|
96 |
# V1 - arguments become properties automagically / normal perl mode blessing |
# V1 - arguments become properties automagically / normal perl mode blessing |
101 |
bless $self, $class; |
bless $self, $class; |
102 |
=cut |
=cut |
103 |
|
|
104 |
|
#=pod |
105 |
# V2 - maybe more convenient/secure? utilizing Damian Conway's Tie::SecureHash... |
# V2 - maybe more convenient/secure? utilizing Damian Conway's Tie::SecureHash... |
106 |
my $self = Tie::SecureHash->new( |
my $self = Tie::SecureHash->new( |
107 |
$class, |
$class, |
125 |
#_protected => , |
#_protected => , |
126 |
#__private => , |
#__private => , |
127 |
); |
); |
128 |
|
#=cut |
129 |
|
|
130 |
# merge passed-in arguments to constructor as properties into already blessed secure object |
# merge passed-in arguments to constructor as properties into already blessed secure object |
131 |
|
|
132 |
# mungle arguments from array into hash - perl does the job ;) |
# mungle arguments from array into hash - perl does the job ;) |
133 |
my %args = @_; |
#my %args = @_; |
134 |
|
#my %args = (); |
135 |
|
|
136 |
|
#print Dumper(@_); |
137 |
|
|
138 |
# merge attributes one-by-one |
# merge attributes one-by-one |
139 |
# TODO: deep_copy? / merge_deep? |
# TODO: deep_copy? / merge_deep? |
140 |
foreach (keys %args) { |
while (my $elemName = shift @_) { |
141 |
#print "key: $_", "\n"; |
#print "elemName: $elemName", "\n"; |
142 |
$self->{$_} = $args{$_}; |
if (my $elemValue = shift @_) { |
143 |
|
#print Dumper($elemValue); |
144 |
|
#print "elemName=$elemName, elemValue=$elemValue", "\n"; |
145 |
|
$self->{$elemName} = $elemValue; |
146 |
|
} |
147 |
|
#$self->{$_} = $args{$_}; |
148 |
|
#$self->{$_} = $args{$_}; |
149 |
} |
} |
150 |
|
|
151 |
# V3 - rolling our own security (just for {COREHANDLE} - nothing else) - nope, Tie::SecureHash works wonderful |
# V3 - rolling our own security (just for {COREHANDLE} - nothing else) - nope, Tie::SecureHash works wonderful |
169 |
} |
} |
170 |
|
|
171 |
|
|
172 |
|
# TODO: use NEXT.pm inside this mechanism (to avoid repetitions...) |
173 |
sub AUTOLOAD { |
sub AUTOLOAD { |
174 |
|
|
175 |
# recursion problem to be avoided here!!! |
# recursion problem to be avoided here!!! |
206 |
#if (!$self->exists('COREHANDLE')) { return; } |
#if (!$self->exists('COREHANDLE')) { return; } |
207 |
|
|
208 |
# handle locking (hack) |
# handle locking (hack) |
209 |
if ($self->exists('lock_info') && $self->{lock_info}->{last_method} && $methodname eq $self->{lock_info}->{last_method}) { |
if ($self->can('exists') && $self->exists('lock_info') && $self->{lock_info}->{last_method} && $methodname eq $self->{lock_info}->{last_method}) { |
210 |
$self->{lock_info}->{log_lock} = 1; |
$self->{lock_info}->{log_lock} = 1; |
211 |
} else { |
} else { |
212 |
$self->{lock_info}->{log_lock} = 0; |
$self->{lock_info}->{log_lock} = 0; |
227 |
#if (!$self->exists('_COREHANDLE')) { |
#if (!$self->exists('_COREHANDLE')) { |
228 |
#if (!$self->{_COREHANDLE}) { |
#if (!$self->{_COREHANDLE}) { |
229 |
if (!$core) { |
if (!$core) { |
230 |
my $err_msg_core = __PACKAGE__ . "[$self->{metainfo}->{type}]" . ": " . "COREHANDLE is undefined while trying to execute method \"$methodname\""; |
|
231 |
print $err_msg_core, "\n"; |
#print Dumper($self); |
232 |
if ($self->exists('lock_info') && !$self->{lock_info}->{log_lock}) { |
#exit; |
233 |
|
|
234 |
|
my $handlertype = $self->{metainfo}->{type}; |
235 |
|
$handlertype ||= ''; |
236 |
|
|
237 |
|
my $err_msg_core = __PACKAGE__ . "[$handlertype]" . ": " . "COREHANDLE is undefined while trying to execute method \"$methodname\""; |
238 |
|
|
239 |
|
#print $err_msg_core, "\n"; |
240 |
|
|
241 |
|
#if ($self->exists('lock_info') && !$self->{lock_info}->{log_lock}) { |
242 |
$logger->error( $err_msg_core ); |
$logger->error( $err_msg_core ); |
243 |
} |
#} |
244 |
|
|
245 |
return; |
return; |
246 |
|
|
247 |
} |
} |
248 |
#=cut |
#=cut |
249 |
|
|
250 |
|
=pod |
251 |
|
if (!$methodname) { |
252 |
|
die("Methodname is not defined!"); |
253 |
|
return; |
254 |
|
} |
255 |
|
=cut |
256 |
|
|
257 |
#print "$methodname - 3", "\n"; |
#print "$methodname - 3", "\n"; |
258 |
|
|
259 |
# try to dispatch method-call to Storage::Handler::* |
# try to dispatch method-call to Storage::Handler::* |
282 |
#$lock_AUTOLOAD = 1 if ($methodname eq 'insert'); |
#$lock_AUTOLOAD = 1 if ($methodname eq 'insert'); |
283 |
if (!$self->{lock_info}->{log_lock}) { |
if (!$self->{lock_info}->{log_lock}) { |
284 |
#print "method: $methodname", "\n"; |
#print "method: $methodname", "\n"; |
285 |
$logger->debug( __PACKAGE__ . "[$self->{metainfo}->{type}]" . "->" . $methodname . "(@_)" ); |
my $type = $self->{metainfo}->{type}; |
286 |
|
$type ||= ''; |
287 |
|
# FIXME! |
288 |
|
#$logger->debug( __PACKAGE__ . "[$type]" . "->" . $methodname . "(@_)" ); |
289 |
|
$logger->debug( __PACKAGE__ . "[$type]" . "->" . $methodname ); |
290 |
} else { |
} else { |
291 |
# AUTOLOAD - sub is locked to prevent deep recursions if (e.g.) db-inserts cause log-actions to same db itself |
# AUTOLOAD - sub is locked to prevent deep recursions if (e.g.) db-inserts cause log-actions to same db itself |
292 |
} |
} |
295 |
|
|
296 |
#print "calling: $methodname", "\n"; |
#print "calling: $methodname", "\n"; |
297 |
|
|
298 |
# method calls doing it until here will get dispatched to the proper handler |
# method calls making it until here will get dispatched to the proper handler |
299 |
return $core->$methodname(@_); |
return $core->$methodname(@_); |
300 |
|
|
301 |
} elsif ($self->can($methodname)) { |
} elsif ($self->can($methodname)) { |
302 |
|
# try to call specified method inside our or inherited module(s) scope(s) |
303 |
return $self->$methodname(@_); |
return $self->$methodname(@_); |
304 |
} |
} |
305 |
|
|
307 |
|
|
308 |
sub DESTROY { |
sub DESTROY { |
309 |
my $self = shift; |
my $self = shift; |
310 |
#if ($self->{COREHANDLE}) { |
|
311 |
if ($self->exists('_COREHANDLE')) { |
return; |
312 |
|
|
313 |
|
$logger->debug( __PACKAGE__ . "[$self->{metainfo}->{type}]" . "->DESTROY" ); |
314 |
|
|
315 |
|
my $disconnectMethod = $self->{metainfo}->{disconnectMethod}; |
316 |
|
print "meth: ", $disconnectMethod, "\n"; |
317 |
|
|
318 |
|
#$disconnectMethod && $self->{_COREHANDLE} && ( $self->{_COREHANDLE}->$disconnectMethod() ); |
319 |
|
$self->{_COREHANDLE}->$disconnectMethod(); |
320 |
|
#$self->$disconnectMethod(); |
321 |
|
|
322 |
|
#my $core1 = $self->getCOREHANDLE() if $self->can('getCOREHANDLE'); |
323 |
|
#$core1->$disconnectMethod(); |
324 |
|
|
325 |
|
return; |
326 |
|
|
327 |
|
print "DESTROY-1", "\n"; |
328 |
|
#if ($self->{__COREHANDLE}) { |
329 |
|
#if ($self->exists('_COREHANDLE')) { |
330 |
|
|
331 |
|
# get corehandle instance from underlying handler |
332 |
|
my $core; |
333 |
|
$core = $self->getCOREHANDLE() if $self->can('getCOREHANDLE'); |
334 |
|
|
335 |
|
#if ($self->{STORAGEHANDLE}) { |
336 |
|
if ($core) { |
337 |
|
print "DESTROY-2", "\n"; |
338 |
$logger->debug( __PACKAGE__ . "[$self->{metainfo}->{type}]" . "->DESTROY" ); |
$logger->debug( __PACKAGE__ . "[$self->{metainfo}->{type}]" . "->DESTROY" ); |
339 |
|
|
340 |
my $disconnectMethod = $self->{metainfo}->{disconnectMethod}; |
my $disconnectMethod = $self->{metainfo}->{disconnectMethod}; |
347 |
} |
} |
348 |
} |
} |
349 |
|
|
|
|
|
|
sub _abstract_function { |
|
|
my $self = shift; |
|
|
my $fName = shift; |
|
|
my $class = ref($self); |
|
|
# was: |
|
|
# $logger->error( __PACKAGE__ . ": function \"$fName\" is an abstract method, please implement it in \"$class\""); |
|
|
# is: |
|
|
die( __PACKAGE__ . ": function \"$fName\" is an abstract method, please implement it in \"$class\""); |
|
|
#exit; |
|
|
} |
|
|
|
|
350 |
sub _typeCheck2 { |
sub _typeCheck2 { |
351 |
my $type = shift; |
my $type = shift; |
352 |
print "type: $type"; |
print "type: $type"; |
361 |
sub existsChildNode { |
sub existsChildNode { |
362 |
my $self = shift; |
my $self = shift; |
363 |
my $nodename = shift; |
my $nodename = shift; |
364 |
#$nodename = 'TransactionRoutingTable'; |
|
365 |
$logger->debug( __PACKAGE__ . "->getChildNode( nodename $nodename )" ); |
# TODO: don't use $self->{meta}->{childnodes} directly in here |
366 |
|
# get it returned from $self->getChildNodes()!!! |
367 |
|
|
368 |
|
$logger->debug( __PACKAGE__ . "->existsChildNode( nodename=$nodename )" ); |
369 |
$self->getChildNodes() unless $self->{meta}->{childnodes}; |
$self->getChildNodes() unless $self->{meta}->{childnodes}; |
370 |
my $result = grep(m/$nodename/i, @{$self->{meta}->{childnodes}}); # TODO: use "/i" only on win32-systems! |
|
371 |
|
# quote this, it might contain meta characters which don't work in a regex |
372 |
|
$nodename = quotemeta($nodename); |
373 |
|
|
374 |
|
# trace |
375 |
|
#print Dumper($self->{meta}); |
376 |
|
#print "nodename: $nodename", "\n"; |
377 |
|
|
378 |
|
# FIXME: use "/i" only on win32-systems! |
379 |
|
my $result = grep(m/$nodename/i, @{$self->{meta}->{childnodes}}); |
380 |
|
|
381 |
return $result; |
return $result; |
382 |
} |
} |
383 |
|
|
412 |
$self->_abstract_function('sendCommand'); |
$self->_abstract_function('sendCommand'); |
413 |
} |
} |
414 |
|
|
415 |
|
sub createChildNode { |
416 |
|
my $self = shift; |
417 |
|
$self->_abstract_function('createChildNode'); |
418 |
|
} |
419 |
|
|
420 |
sub existsChildNode_tmp { |
sub existsChildNode_tmp { |
421 |
my $self = shift; |
my $self = shift; |
422 |
$self->_abstract_function('existsChildNode'); |
$self->_abstract_function('existsChildNode'); |
486 |
return; |
return; |
487 |
} |
} |
488 |
|
|
489 |
|
sub dropDb { |
490 |
|
my $self = shift; |
491 |
|
$self->_abstract_function('dropDb'); |
492 |
|
return; |
493 |
|
} |
494 |
|
|
495 |
|
sub rebuildDb { |
496 |
|
my $self = shift; |
497 |
|
$self->_abstract_function('rebuildDb'); |
498 |
|
return; |
499 |
|
} |
500 |
|
|
501 |
|
sub getDbName { |
502 |
|
my $self = shift; |
503 |
|
$self->_abstract_function('getDbName'); |
504 |
|
return; |
505 |
|
} |
506 |
|
|
507 |
|
sub testAvailability { |
508 |
|
my $self = shift; |
509 |
|
$self->_abstract_function('testAvailability'); |
510 |
|
return; |
511 |
|
} |
512 |
|
|
513 |
|
sub isConnected { |
514 |
|
my $self = shift; |
515 |
|
$self->_abstract_function('isConnected'); |
516 |
|
return; |
517 |
|
} |
518 |
|
|
519 |
|
sub testDsn { |
520 |
|
my $self = shift; |
521 |
|
$self->_abstract_function('testDsn'); |
522 |
|
return; |
523 |
|
} |
524 |
|
|
525 |
1; |
1; |