1 |
## ------------------------------------------------------------------------ |
2 |
## $Id: Abstract.pm,v 1.12 2003/01/30 21:46:32 joko Exp $ |
3 |
## ------------------------------------------------------------------------ |
4 |
## $Log: Abstract.pm,v $ |
5 |
## Revision 1.12 2003/01/30 21:46:32 joko |
6 |
## + fixed behaviour of AUTOLOAD-method |
7 |
## |
8 |
## Revision 1.11 2003/01/20 16:43:18 joko |
9 |
## + better argument-property merging |
10 |
## + debugging-output !!! |
11 |
## + abstract-dummy 'sub createChildNode' |
12 |
## |
13 |
## Revision 1.10 2003/01/19 02:31:51 joko |
14 |
## + fix to 'sub AUTOLOAD' |
15 |
## |
16 |
## Revision 1.9 2002/12/19 16:30:23 joko |
17 |
## + added 'sub dropDb' and 'sub rebuildDb' as croakers for concrete implementations of methods in proper handlers |
18 |
## |
19 |
## Revision 1.8 2002/12/13 21:48:35 joko |
20 |
## + sub _abstract_function |
21 |
## |
22 |
## Revision 1.7 2002/12/05 07:57:48 joko |
23 |
## + now using Tie::SecureHash as a base for the COREHANDLE |
24 |
## + former public COREHANDLE becomes private _COREHANDLE now |
25 |
## |
26 |
## Revision 1.6 2002/12/03 15:52:24 joko |
27 |
## + fix/feature: if dispatching to deep core method fails (is not declared), try method at Data::Storage - level |
28 |
## |
29 |
## Revision 1.5 2002/12/01 22:19:33 joko |
30 |
## + just disconnect if COREHANDLE exists |
31 |
## |
32 |
## Revision 1.4 2002/12/01 04:45:38 joko |
33 |
## + sub eraseAll |
34 |
## + sub createDb |
35 |
## |
36 |
## Revision 1.3 2002/11/29 04:58:20 joko |
37 |
## + Storage::Result now uses the same dispatching mechanism like Storage::Handler |
38 |
## |
39 |
## Revision 1.2 2002/10/17 00:08:07 joko |
40 |
## + bugfixes regarding "deep recursion" stuff |
41 |
## |
42 |
## Revision 1.1 2002/10/10 03:44:07 cvsjoko |
43 |
## + new |
44 |
## ------------------------------------------------------------------------ |
45 |
|
46 |
|
47 |
package Data::Storage::Handler::Abstract; |
48 |
|
49 |
use strict; |
50 |
use warnings; |
51 |
|
52 |
use base qw( DesignPattern::Object ); |
53 |
|
54 |
|
55 |
use Data::Dumper; |
56 |
use Tie::SecureHash; |
57 |
#use Data::Storage::Handler; |
58 |
use Data::Transform::Deep qw( merge ); |
59 |
|
60 |
|
61 |
# get logger instance |
62 |
my $logger = Log::Dispatch::Config->instance; |
63 |
|
64 |
#our $lock_info; |
65 |
|
66 |
sub new { |
67 |
my $invocant = shift; |
68 |
my $class = ref($invocant) || $invocant; |
69 |
|
70 |
# logging info about the actual handler called |
71 |
$logger->debug( "$invocant->new( @_ )" ); |
72 |
#$logger->debug( __PACKAGE__ . "->" . "new()" ); |
73 |
|
74 |
# V1 - arguments become properties automagically / normal perl mode blessing |
75 |
=pod |
76 |
# autovivify passed-in arguments as future object attributes into to-be-blessed hash |
77 |
my $self = { @_ }; |
78 |
# create object from blessed hash-reference |
79 |
bless $self, $class; |
80 |
=cut |
81 |
|
82 |
#=pod |
83 |
# V2 - maybe more convenient/secure? utilizing Damian Conway's Tie::SecureHash... |
84 |
my $self = Tie::SecureHash->new( |
85 |
$class, |
86 |
# that's it: |
87 |
'metainfo' => undef, |
88 |
'lock_info' => undef, |
89 |
'_COREHANDLE' => undef, |
90 |
'meta' => undef, |
91 |
'locator' => undef, |
92 |
'dataStorageLayer' => undef, |
93 |
'dbi' => undef, |
94 |
# tries: |
95 |
#'Data::Storage::Handler::Abstract::metainfo' => undef, |
96 |
#'dataStorageLayer' |
97 |
#'Data::Storage::Handler::Abstract::dataStorageLayer' => undef, |
98 |
#'Data::Storage::Handler::Tangram::dataStorageLayer' => undef, |
99 |
#'Data::Dumper::locator' => undef, |
100 |
#$class . '::locator' => undef, |
101 |
#'Data::Storage::Handler::Tangram::locator' => undef, |
102 |
#'Data::Storage::Handler::DBI::locator' => undef, |
103 |
#_protected => , |
104 |
#__private => , |
105 |
); |
106 |
#=cut |
107 |
|
108 |
# merge passed-in arguments to constructor as properties into already blessed secure object |
109 |
|
110 |
# mungle arguments from array into hash - perl does the job ;) |
111 |
#my %args = @_; |
112 |
#my %args = (); |
113 |
|
114 |
#print Dumper(@_); |
115 |
|
116 |
# merge attributes one-by-one |
117 |
# TODO: deep_copy? / merge_deep? |
118 |
while (my $elemName = shift @_) { |
119 |
#print "elemName: $elemName", "\n"; |
120 |
if (my $elemValue = shift @_) { |
121 |
#print Dumper($elemValue); |
122 |
#print "elemName=$elemName, elemValue=$elemValue", "\n"; |
123 |
$self->{$elemName} = $elemValue; |
124 |
} |
125 |
#$self->{$_} = $args{$_}; |
126 |
#$self->{$_} = $args{$_}; |
127 |
} |
128 |
|
129 |
# V3 - rolling our own security (just for {COREHANDLE} - nothing else) - nope, Tie::SecureHash works wonderful |
130 |
#my $self = { @_ }; |
131 |
#bless $self, $class; |
132 |
|
133 |
|
134 |
|
135 |
# handle meta data |
136 |
#my $metainfo = $self->getMetaInfo($class); |
137 |
my $metainfo = $self->getMetaInfo(); |
138 |
if (!$metainfo->{disconnectMethod}) { $metainfo->{disconnectMethod} = 'disconnect'; } |
139 |
# type? |
140 |
$invocant =~ s/Data::Storage::Handler:://; |
141 |
$metainfo->{type} = $invocant; |
142 |
|
143 |
# direct accessible (non-protected) properties ( was: my $self = { }; ) |
144 |
$self->{metainfo} = $metainfo; |
145 |
|
146 |
return $self; |
147 |
} |
148 |
|
149 |
|
150 |
# TODO: use NEXT.pm inside this mechanism (to avoid repetitions...) |
151 |
sub AUTOLOAD { |
152 |
|
153 |
# recursion problem to be avoided here!!! |
154 |
|
155 |
# - problem: we get recursion-hangs!!! => perl stops with ... |
156 |
# "Deep recursion on subroutine "Data::Storage::Handler::Abstract::AUTOLOAD" at [...]/libs/Data/Storage.pm line 38." |
157 |
|
158 |
# - when: if we log to ourselves as a storage-implementation (e.g. via Log::Dispatch::Tangram) |
159 |
# - why: debug-messages are on a very low level including every data-operation to "Data::Storage(::Handler::X)", |
160 |
# so e.g. a "$storage->insert" would trigger another "$storage->insert" itself |
161 |
# which leads to a infinite recursion loop (deep recursion) |
162 |
# - solution: locks! (by Hack or (maybe) via Perl "local"s) |
163 |
|
164 |
my $self = shift; |
165 |
|
166 |
our $AUTOLOAD; |
167 |
#return if $AUTOLOAD =~ m/::DESTROY$/; |
168 |
|
169 |
# find out methodname |
170 |
my $methodname = $AUTOLOAD; |
171 |
$methodname =~ s/^.*:://; |
172 |
|
173 |
#print "method: $methodname", "\n"; |
174 |
|
175 |
# TODO: document this! handler-private methods listed here will not be triggered (internal use) |
176 |
# in this case, "exists" is a method reserved for Tie::SecureHash, |
177 |
# which encapsulates the perl data structure (HASH) under this object |
178 |
# this is to prevent deep recursion's |
179 |
return if lc $methodname eq 'exists'; |
180 |
|
181 |
#print "$methodname - 1", "\n"; |
182 |
|
183 |
# TODO: enhance logging/debugging |
184 |
#if (!$self->exists('COREHANDLE')) { return; } |
185 |
|
186 |
# handle locking (hack) |
187 |
if ($self->can('exists') && $self->exists('lock_info') && $self->{lock_info}->{last_method} && $methodname eq $self->{lock_info}->{last_method}) { |
188 |
$self->{lock_info}->{log_lock} = 1; |
189 |
} else { |
190 |
$self->{lock_info}->{log_lock} = 0; |
191 |
} |
192 |
$self->{lock_info}->{last_method} = $methodname; |
193 |
|
194 |
#print "$methodname - 2", "\n"; |
195 |
|
196 |
#print Dumper($self); |
197 |
#exit; |
198 |
|
199 |
# get corehandle instance from underlying handler |
200 |
my $core = $self->getCOREHANDLE(); |
201 |
|
202 |
# test for COREHANDLE |
203 |
#if (!$self->{_COREHANDLE}) { |
204 |
#=pod |
205 |
#if (!$self->exists('_COREHANDLE')) { |
206 |
#if (!$self->{_COREHANDLE}) { |
207 |
if (!$core) { |
208 |
|
209 |
#print Dumper($self); |
210 |
#exit; |
211 |
|
212 |
my $handlertype = $self->{metainfo}->{type}; |
213 |
$handlertype ||= ''; |
214 |
|
215 |
my $err_msg_core = __PACKAGE__ . "[$handlertype]" . ": " . "COREHANDLE is undefined while trying to execute method \"$methodname\""; |
216 |
|
217 |
#print $err_msg_core, "\n"; |
218 |
|
219 |
#if ($self->exists('lock_info') && !$self->{lock_info}->{log_lock}) { |
220 |
$logger->error( $err_msg_core ); |
221 |
#} |
222 |
|
223 |
return; |
224 |
|
225 |
} |
226 |
#=cut |
227 |
|
228 |
#print "$methodname - 3", "\n"; |
229 |
|
230 |
# try to dispatch method-call to Storage::Handler::* |
231 |
#if ($self->can($methodname)) { |
232 |
#$logger->debug( __PACKAGE__ . "[$self->{metainfo}->{type}]" . "->" . $methodname . "(@_)" ); |
233 |
#my $res = $self->$methodname(@_); |
234 |
#if ($res) { return $res; } |
235 |
#} |
236 |
|
237 |
#print "$methodname - 4", "\n"; |
238 |
|
239 |
# try to dispatch method-call to COREHANDLE |
240 |
# was: |
241 |
#my $core = $self->{_COREHANDLE}; |
242 |
# is: |
243 |
#my $core = $self->getCOREHANDLE(); |
244 |
|
245 |
#print Dumper($core); |
246 |
#exit; |
247 |
|
248 |
# was: |
249 |
#if ($self->{_COREHANDLE}->can($methodname) || $self->{_COREHANDLE}->can("AUTOLOAD")) { |
250 |
# is: |
251 |
if ($core->can($methodname) || $core->can("AUTOLOAD")) { |
252 |
#$logger->debug( __PACKAGE__ . "->" . $methodname . " (AUTOLOAD)" ); |
253 |
#$lock_AUTOLOAD = 1 if ($methodname eq 'insert'); |
254 |
if (!$self->{lock_info}->{log_lock}) { |
255 |
#print "method: $methodname", "\n"; |
256 |
$logger->debug( __PACKAGE__ . "[$self->{metainfo}->{type}]" . "->" . $methodname . "(@_)" ); |
257 |
} else { |
258 |
# AUTOLOAD - sub is locked to prevent deep recursions if (e.g.) db-inserts cause log-actions to same db itself |
259 |
} |
260 |
#$lock_AUTOLOAD = 0; |
261 |
#$logger->log( level => 'debug', message => __PACKAGE__ . "->" . $methodname . " (AUTOLOAD)" ); |
262 |
|
263 |
#print "calling: $methodname", "\n"; |
264 |
|
265 |
# method calls making it until here will get dispatched to the proper handler |
266 |
return $core->$methodname(@_); |
267 |
|
268 |
} elsif ($self->can($methodname)) { |
269 |
# try to call specified method inside our or inherited module(s) scope(s) |
270 |
return $self->$methodname(@_); |
271 |
} |
272 |
|
273 |
} |
274 |
|
275 |
sub DESTROY { |
276 |
my $self = shift; |
277 |
#if ($self->{COREHANDLE}) { |
278 |
if ($self->exists('_COREHANDLE')) { |
279 |
$logger->debug( __PACKAGE__ . "[$self->{metainfo}->{type}]" . "->DESTROY" ); |
280 |
|
281 |
my $disconnectMethod = $self->{metainfo}->{disconnectMethod}; |
282 |
|
283 |
# call "disconnect" or alike on COREHANDLE |
284 |
# was: $self->{COREHANDLE}->disconnect(); |
285 |
$disconnectMethod && $self->{_COREHANDLE} && ( $self->{_COREHANDLE}->$disconnectMethod() ); |
286 |
|
287 |
undef $self->{_COREHANDLE}; |
288 |
} |
289 |
} |
290 |
|
291 |
sub _typeCheck2 { |
292 |
my $type = shift; |
293 |
print "type: $type"; |
294 |
eval("use Data::Storage::$type;"); |
295 |
} |
296 |
|
297 |
|
298 |
# ==================================================== |
299 |
# PUBLIC METHODS |
300 |
# ==================================================== |
301 |
|
302 |
sub existsChildNode { |
303 |
my $self = shift; |
304 |
my $nodename = shift; |
305 |
#$nodename = 'TransactionRoutingTable'; |
306 |
$logger->debug( __PACKAGE__ . "->existsChildNode( nodename $nodename )" ); |
307 |
$self->getChildNodes() unless $self->{meta}->{childnodes}; |
308 |
my $result = grep(m/$nodename/i, @{$self->{meta}->{childnodes}}); # TODO: use "/i" only on win32-systems! |
309 |
return $result; |
310 |
} |
311 |
|
312 |
sub connect_tmp { |
313 |
|
314 |
# my $self = shift; |
315 |
# my $connectionString = shift; |
316 |
# # todo: patch connectionString to $args |
317 |
# _typeCheck($self->{args}{type}); |
318 |
|
319 |
} |
320 |
|
321 |
|
322 |
# ==================================================== |
323 |
# CONCRETE METHOD DUMMIES (croaking via "$logger" by calling "_abstract_function") |
324 |
# ==================================================== |
325 |
|
326 |
# TODO: |
327 |
# - abstract "abstract methods" to list/hash to be used in AUTOLOAD |
328 |
# e.g.: my @ABSTRACT_METHODS = (qw( connect sendCommand getChildNodes )); |
329 |
# use Class::XYZ (Construct) |
330 |
# - build them via anonymous subs |
331 |
# - introduce them via symbols |
332 |
|
333 |
sub getCOREHANDLE { |
334 |
my $self = shift; |
335 |
$self->_abstract_function('getCOREHANDLE'); |
336 |
} |
337 |
|
338 |
sub sendCommand { |
339 |
my $self = shift; |
340 |
$self->_abstract_function('sendCommand'); |
341 |
} |
342 |
|
343 |
sub createChildNode { |
344 |
my $self = shift; |
345 |
$self->_abstract_function('createChildNode'); |
346 |
} |
347 |
|
348 |
sub existsChildNode_tmp { |
349 |
my $self = shift; |
350 |
$self->_abstract_function('existsChildNode'); |
351 |
} |
352 |
|
353 |
sub getChildNodes { |
354 |
my $self = shift; |
355 |
$self->_abstract_function('getChildNodes'); |
356 |
} |
357 |
|
358 |
sub configureCOREHANDLE { |
359 |
my $self = shift; |
360 |
$self->_abstract_function('configureCOREHANDLE'); |
361 |
} |
362 |
|
363 |
sub getMetaInfo { |
364 |
my $self = shift; |
365 |
$self->_abstract_function('getMetaInfo'); |
366 |
return; |
367 |
} |
368 |
|
369 |
sub getListUnfiltered { |
370 |
my $self = shift; |
371 |
$self->_abstract_function('getListUnfiltered'); |
372 |
return; |
373 |
} |
374 |
|
375 |
sub getListFiltered { |
376 |
my $self = shift; |
377 |
$self->_abstract_function('getListFiltered'); |
378 |
return; |
379 |
} |
380 |
|
381 |
sub sendQuery { |
382 |
my $self = shift; |
383 |
$self->_abstract_function('sendQuery'); |
384 |
return; |
385 |
} |
386 |
|
387 |
sub connect { |
388 |
my $self = shift; |
389 |
$self->_abstract_function('connect'); |
390 |
return; |
391 |
} |
392 |
|
393 |
sub testIntegrity { |
394 |
my $self = shift; |
395 |
$self->_abstract_function('testIntegrity'); |
396 |
return; |
397 |
} |
398 |
|
399 |
sub quoteSql { |
400 |
my $self = shift; |
401 |
$self->_abstract_function('quoteSql'); |
402 |
return; |
403 |
} |
404 |
|
405 |
sub eraseAll { |
406 |
my $self = shift; |
407 |
$self->_abstract_function('eraseAll'); |
408 |
return; |
409 |
} |
410 |
|
411 |
sub createDb { |
412 |
my $self = shift; |
413 |
$self->_abstract_function('createDb'); |
414 |
return; |
415 |
} |
416 |
|
417 |
sub dropDb { |
418 |
my $self = shift; |
419 |
$self->_abstract_function('dropDb'); |
420 |
return; |
421 |
} |
422 |
|
423 |
sub rebuildDb { |
424 |
my $self = shift; |
425 |
$self->_abstract_function('rebuildDb'); |
426 |
return; |
427 |
} |
428 |
|
429 |
sub getDbName { |
430 |
my $self = shift; |
431 |
$self->_abstract_function('getDbName'); |
432 |
return; |
433 |
} |
434 |
|
435 |
sub testAvailability { |
436 |
my $self = shift; |
437 |
$self->_abstract_function('testAvailability'); |
438 |
return; |
439 |
} |
440 |
|
441 |
sub isConnected { |
442 |
my $self = shift; |
443 |
$self->_abstract_function('isConnected'); |
444 |
return; |
445 |
} |
446 |
|
447 |
sub testDsn { |
448 |
my $self = shift; |
449 |
$self->_abstract_function('testDsn'); |
450 |
return; |
451 |
} |
452 |
|
453 |
1; |