1 |
################################# |
## -------------------------------------------------------------------------------- |
2 |
# |
## $Id$ |
3 |
# $Id$ |
## -------------------------------------------------------------------------------- |
4 |
# |
## $Log$ |
5 |
# $Log$ |
## Revision 1.7 2002/12/05 07:57:48 joko |
6 |
# Revision 1.2 2002/10/17 00:08:07 joko |
## + now using Tie::SecureHash as a base for the COREHANDLE |
7 |
# + bugfixes regarding "deep recursion" stuff |
## + former public COREHANDLE becomes private _COREHANDLE now |
8 |
# |
## |
9 |
# Revision 1.1 2002/10/10 03:44:07 cvsjoko |
## Revision 1.6 2002/12/03 15:52:24 joko |
10 |
# + new |
## + fix/feature: if dispatching to deep core method fails (is not declared), try method at Data::Storage - level |
11 |
# |
## |
12 |
# |
## Revision 1.5 2002/12/01 22:19:33 joko |
13 |
################################# |
## + just disconnect if COREHANDLE exists |
14 |
|
## |
15 |
|
## Revision 1.4 2002/12/01 04:45:38 joko |
16 |
|
## + sub eraseAll |
17 |
|
## + sub createDb |
18 |
|
## |
19 |
|
## Revision 1.3 2002/11/29 04:58:20 joko |
20 |
|
## + Storage::Result now uses the same dispatching mechanism like Storage::Handler |
21 |
|
## |
22 |
|
## Revision 1.2 2002/10/17 00:08:07 joko |
23 |
|
## + bugfixes regarding "deep recursion" stuff |
24 |
|
## |
25 |
|
## Revision 1.1 2002/10/10 03:44:07 cvsjoko |
26 |
|
## + new |
27 |
|
## -------------------------------------------------------------------------------- |
28 |
|
|
29 |
|
|
30 |
package Data::Storage::Handler::Abstract; |
package Data::Storage::Handler::Abstract; |
31 |
|
|
33 |
use warnings; |
use warnings; |
34 |
|
|
35 |
use Data::Dumper; |
use Data::Dumper; |
36 |
|
use Tie::SecureHash; |
37 |
|
#use Data::Storage::Handler; |
38 |
|
|
39 |
# get logger instance |
# get logger instance |
40 |
my $logger = Log::Dispatch::Config->instance; |
my $logger = Log::Dispatch::Config->instance; |
49 |
$logger->debug( "$invocant->new( @_ )" ); |
$logger->debug( "$invocant->new( @_ )" ); |
50 |
#$logger->debug( __PACKAGE__ . "->" . "new()" ); |
#$logger->debug( __PACKAGE__ . "->" . "new()" ); |
51 |
|
|
52 |
|
# V1 - arguments become properties automagically / normal perl mode blessing |
53 |
|
=pod |
54 |
|
# autovivify passed-in arguments as future object attributes into to-be-blessed hash |
55 |
|
my $self = { @_ }; |
56 |
|
# create object from blessed hash-reference |
57 |
|
bless $self, $class; |
58 |
|
=cut |
59 |
|
|
60 |
|
|
61 |
|
# V2 - maybe more convenient/secure? utilizing Damian Conway's Tie::SecureHash... |
62 |
|
my $self = Tie::SecureHash->new( |
63 |
|
$class, |
64 |
|
# that's it: |
65 |
|
'metainfo' => undef, |
66 |
|
'lock_info' => undef, |
67 |
|
'_COREHANDLE' => undef, |
68 |
|
'meta' => undef, |
69 |
|
'locator' => undef, |
70 |
|
'dataStorageLayer' => undef, |
71 |
|
'dbi' => undef, |
72 |
|
# tries: |
73 |
|
#'Data::Storage::Handler::Abstract::metainfo' => undef, |
74 |
|
#'dataStorageLayer' |
75 |
|
#'Data::Storage::Handler::Abstract::dataStorageLayer' => undef, |
76 |
|
#'Data::Storage::Handler::Tangram::dataStorageLayer' => undef, |
77 |
|
#'Data::Dumper::locator' => undef, |
78 |
|
#$class . '::locator' => undef, |
79 |
|
#'Data::Storage::Handler::Tangram::locator' => undef, |
80 |
|
#'Data::Storage::Handler::DBI::locator' => undef, |
81 |
|
#_protected => , |
82 |
|
#__private => , |
83 |
|
); |
84 |
|
|
85 |
|
|
86 |
|
# merge passed-in arguments to constructor as properties into already blessed secure object |
87 |
|
|
88 |
|
# mungle arguments from array into hash - perl does the job ;) |
89 |
|
my %args = @_; |
90 |
|
|
91 |
|
# merge attributes one-by-one |
92 |
|
# TODO: deep_copy? / merge_deep? |
93 |
|
foreach (keys %args) { |
94 |
|
#print "key: $_", "\n"; |
95 |
|
$self->{$_} = $args{$_}; |
96 |
|
} |
97 |
|
|
98 |
|
# V3 - rolling our own security (just for {COREHANDLE} - nothing else) - nope, Tie::SecureHash works wonderful |
99 |
|
#my $self = { @_ }; |
100 |
|
#bless $self, $class; |
101 |
|
|
102 |
|
|
103 |
|
|
104 |
# handle meta data |
# handle meta data |
105 |
my $metainfo = _getMetaInfo($class); |
#my $metainfo = $self->getMetaInfo($class); |
106 |
|
my $metainfo = $self->getMetaInfo(); |
107 |
if (!$metainfo->{disconnectMethod}) { $metainfo->{disconnectMethod} = 'disconnect'; } |
if (!$metainfo->{disconnectMethod}) { $metainfo->{disconnectMethod} = 'disconnect'; } |
108 |
# type? |
# type? |
109 |
$invocant =~ s/Data::Storage::Handler:://; |
$invocant =~ s/Data::Storage::Handler:://; |
110 |
$metainfo->{type} = $invocant; |
$metainfo->{type} = $invocant; |
111 |
|
|
|
# create object from blessed hash-reference |
|
112 |
# direct accessible (non-protected) properties ( was: my $self = { }; ) |
# direct accessible (non-protected) properties ( was: my $self = { }; ) |
113 |
my $self = { metainfo => $metainfo, @_ }; |
$self->{metainfo} = $metainfo; |
|
bless $self, $class; |
|
114 |
|
|
115 |
return $self; |
return $self; |
116 |
} |
} |
137 |
# find out methodname |
# find out methodname |
138 |
my $methodname = $AUTOLOAD; |
my $methodname = $AUTOLOAD; |
139 |
$methodname =~ s/^.*:://; |
$methodname =~ s/^.*:://; |
140 |
|
|
141 |
|
#print "method: $methodname", "\n"; |
142 |
|
|
143 |
|
# TODO: document this! handler-private methods listed here will not be triggered (internal use) |
144 |
|
# in this case, "exists" is a method reserved for Tie::SecureHash, |
145 |
|
# which encapsulates the perl data structure (HASH) under this object |
146 |
|
# this is to prevent deep recursion's |
147 |
|
return if lc $methodname eq 'exists'; |
148 |
|
|
149 |
|
#print "$methodname - 1", "\n"; |
150 |
|
|
151 |
|
# TODO: enhance logging/debugging |
152 |
|
#if (!$self->exists('COREHANDLE')) { return; } |
153 |
|
|
154 |
# handle locking (hack) |
# handle locking (hack) |
155 |
if ($self->{lock_info}->{last_method} && $methodname eq $self->{lock_info}->{last_method}) { |
if ($self->exists('lock_info') && $self->{lock_info}->{last_method} && $methodname eq $self->{lock_info}->{last_method}) { |
156 |
$self->{lock_info}->{log_lock} = 1; |
$self->{lock_info}->{log_lock} = 1; |
157 |
} else { |
} else { |
158 |
$self->{lock_info}->{log_lock} = 0; |
$self->{lock_info}->{log_lock} = 0; |
159 |
} |
} |
160 |
$self->{lock_info}->{last_method} = $methodname; |
$self->{lock_info}->{last_method} = $methodname; |
161 |
|
|
162 |
|
#print "$methodname - 2", "\n"; |
163 |
|
|
164 |
|
#print Dumper($self); |
165 |
|
#exit; |
166 |
|
|
167 |
|
# get corehandle instance from underlying handler |
168 |
|
my $core = $self->getCOREHANDLE(); |
169 |
|
|
170 |
# test for COREHANDLE |
# test for COREHANDLE |
171 |
if (!$self->{COREHANDLE}) { |
#if (!$self->{_COREHANDLE}) { |
172 |
#print "no COREHANDLE", "\n"; |
#=pod |
173 |
if (!$self->{lock_info}->{log_lock}) { |
#if (!$self->exists('_COREHANDLE')) { |
174 |
$logger->error( __PACKAGE__ . "[$self->{metainfo}->{type}]" . ": " . "COREHANDLE is undefined while trying to execute method \"$methodname\"" ); |
#if (!$self->{_COREHANDLE}) { |
175 |
|
if (!$core) { |
176 |
|
my $err_msg_core = __PACKAGE__ . "[$self->{metainfo}->{type}]" . ": " . "COREHANDLE is undefined while trying to execute method \"$methodname\""; |
177 |
|
print $err_msg_core, "\n"; |
178 |
|
if ($self->exists('lock_info') && !$self->{lock_info}->{log_lock}) { |
179 |
|
$logger->error( $err_msg_core ); |
180 |
} |
} |
181 |
return; |
return; |
182 |
} |
} |
183 |
|
#=cut |
184 |
|
|
185 |
|
#print "$methodname - 3", "\n"; |
186 |
|
|
187 |
# try to dispatch method-call to Storage::Handler::* |
# try to dispatch method-call to Storage::Handler::* |
188 |
#if ($self->can($methodname)) { |
#if ($self->can($methodname)) { |
191 |
#if ($res) { return $res; } |
#if ($res) { return $res; } |
192 |
#} |
#} |
193 |
|
|
194 |
|
#print "$methodname - 4", "\n"; |
195 |
|
|
196 |
# try to dispatch method-call to COREHANDLE |
# try to dispatch method-call to COREHANDLE |
197 |
if ($self->{COREHANDLE}->can($methodname) || $self->{COREHANDLE}->can("AUTOLOAD")) { |
# was: |
198 |
|
#my $core = $self->{_COREHANDLE}; |
199 |
|
# is: |
200 |
|
#my $core = $self->getCOREHANDLE(); |
201 |
|
|
202 |
|
#print Dumper($core); |
203 |
|
#exit; |
204 |
|
|
205 |
|
# was: |
206 |
|
#if ($self->{_COREHANDLE}->can($methodname) || $self->{_COREHANDLE}->can("AUTOLOAD")) { |
207 |
|
# is: |
208 |
|
if ($core->can($methodname) || $core->can("AUTOLOAD")) { |
209 |
#$logger->debug( __PACKAGE__ . "->" . $methodname . " (AUTOLOAD)" ); |
#$logger->debug( __PACKAGE__ . "->" . $methodname . " (AUTOLOAD)" ); |
210 |
#$lock_AUTOLOAD = 1 if ($methodname eq 'insert'); |
#$lock_AUTOLOAD = 1 if ($methodname eq 'insert'); |
211 |
if (!$self->{lock_info}->{log_lock}) { |
if (!$self->{lock_info}->{log_lock}) { |
216 |
} |
} |
217 |
#$lock_AUTOLOAD = 0; |
#$lock_AUTOLOAD = 0; |
218 |
#$logger->log( level => 'debug', message => __PACKAGE__ . "->" . $methodname . " (AUTOLOAD)" ); |
#$logger->log( level => 'debug', message => __PACKAGE__ . "->" . $methodname . " (AUTOLOAD)" ); |
219 |
return $self->{COREHANDLE}->$methodname(@_); |
|
220 |
|
#print "calling: $methodname", "\n"; |
221 |
|
|
222 |
|
# method calls doing it until here will get dispatched to the proper handler |
223 |
|
return $core->$methodname(@_); |
224 |
|
|
225 |
|
} elsif ($self->can($methodname)) { |
226 |
|
return $self->$methodname(@_); |
227 |
} |
} |
228 |
|
|
229 |
} |
} |
230 |
|
|
231 |
sub DESTROY { |
sub DESTROY { |
232 |
my $self = shift; |
my $self = shift; |
233 |
if ($self->{COREHANDLE}) { |
#if ($self->{COREHANDLE}) { |
234 |
|
if ($self->exists('_COREHANDLE')) { |
235 |
$logger->debug( __PACKAGE__ . "[$self->{metainfo}->{type}]" . "->DESTROY" ); |
$logger->debug( __PACKAGE__ . "[$self->{metainfo}->{type}]" . "->DESTROY" ); |
236 |
|
|
237 |
my $disconnectMethod = $self->{metainfo}->{disconnectMethod}; |
my $disconnectMethod = $self->{metainfo}->{disconnectMethod}; |
238 |
|
|
239 |
# call "disconnect" or alike on COREHANDLE |
# call "disconnect" or alike on COREHANDLE |
240 |
# was: $self->{COREHANDLE}->disconnect(); |
# was: $self->{COREHANDLE}->disconnect(); |
241 |
$disconnectMethod && ( $self->{COREHANDLE}->$disconnectMethod() ); |
$disconnectMethod && $self->{_COREHANDLE} && ( $self->{_COREHANDLE}->$disconnectMethod() ); |
242 |
|
|
243 |
undef $self->{COREHANDLE}; |
undef $self->{_COREHANDLE}; |
244 |
} |
} |
245 |
} |
} |
246 |
|
|
249 |
my $self = shift; |
my $self = shift; |
250 |
my $fName = shift; |
my $fName = shift; |
251 |
my $class = ref($self); |
my $class = ref($self); |
252 |
$logger->error( __PACKAGE__ . ": function \"$fName\" is an abstract method, please implement it in \"$class\""); |
# was: |
253 |
|
# $logger->error( __PACKAGE__ . ": function \"$fName\" is an abstract method, please implement it in \"$class\""); |
254 |
|
# is: |
255 |
|
die( __PACKAGE__ . ": function \"$fName\" is an abstract method, please implement it in \"$class\""); |
256 |
#exit; |
#exit; |
257 |
} |
} |
258 |
|
|
259 |
sub _typeCheck { |
sub _typeCheck2 { |
260 |
my $type = shift; |
my $type = shift; |
261 |
print "type: $type"; |
print "type: $type"; |
262 |
eval("use Data::Storage::$type;"); |
eval("use Data::Storage::$type;"); |
263 |
} |
} |
264 |
|
|
|
sub _getMetaInfo { |
|
|
my $packagename = shift; |
|
|
#return $self->$metainfo; |
|
|
|
|
|
my $ns = '$' . $packagename . "::metainfo"; |
|
|
$logger->debug( __PACKAGE__ . "->" . "_getMetaInfo()" . " " . "[$ns]" ); |
|
|
return eval($ns); |
|
|
} |
|
|
|
|
265 |
|
|
266 |
# ==================================================== |
# ==================================================== |
267 |
# PUBLIC METHODS |
# PUBLIC METHODS |
268 |
# ==================================================== |
# ==================================================== |
269 |
|
|
270 |
# TODO: abstract "abstract methods" to list/hash to be used in AUTOLOAD |
sub existsChildNode { |
|
# e.g.: my @ABSTRACT_METHODS = (qw( connect sendCommand getChildNodes )); |
|
|
|
|
|
sub connect { |
|
|
|
|
271 |
my $self = shift; |
my $self = shift; |
272 |
$self->_abstract_function('connect'); |
my $nodename = shift; |
273 |
return; |
#$nodename = 'TransactionRoutingTable'; |
274 |
|
$logger->debug( __PACKAGE__ . "->getChildNode( nodename $nodename )" ); |
275 |
$logger->debug( "\"connect\" has to be implemented by handler!" ); |
$self->getChildNodes() unless $self->{meta}->{childnodes}; |
276 |
return; |
my $result = grep(m/$nodename/i, @{$self->{meta}->{childnodes}}); # TODO: use "/i" only on win32-systems! |
277 |
|
return $result; |
278 |
|
} |
279 |
|
|
280 |
|
sub connect_tmp { |
281 |
|
|
282 |
# my $self = shift; |
# my $self = shift; |
283 |
# my $connectionString = shift; |
# my $connectionString = shift; |
286 |
|
|
287 |
} |
} |
288 |
|
|
289 |
|
|
290 |
|
# ==================================================== |
291 |
|
# CONCRETE METHOD DUMMIES (croaking via "$logger" by calling "_abstract_function") |
292 |
|
# ==================================================== |
293 |
|
|
294 |
|
# TODO: |
295 |
|
# - abstract "abstract methods" to list/hash to be used in AUTOLOAD |
296 |
|
# e.g.: my @ABSTRACT_METHODS = (qw( connect sendCommand getChildNodes )); |
297 |
|
# use Class::XYZ (Construct) |
298 |
|
# - build them via anonymous subs |
299 |
|
# - introduce them via symbols |
300 |
|
|
301 |
|
sub getCOREHANDLE { |
302 |
|
my $self = shift; |
303 |
|
$self->_abstract_function('getCOREHANDLE'); |
304 |
|
} |
305 |
|
|
306 |
sub sendCommand { |
sub sendCommand { |
307 |
my $self = shift; |
my $self = shift; |
308 |
$self->_abstract_function('sendCommand'); |
$self->_abstract_function('sendCommand'); |
309 |
} |
} |
310 |
|
|
311 |
|
sub existsChildNode_tmp { |
312 |
|
my $self = shift; |
313 |
|
$self->_abstract_function('existsChildNode'); |
314 |
|
} |
315 |
|
|
316 |
sub getChildNodes { |
sub getChildNodes { |
317 |
my $self = shift; |
my $self = shift; |
318 |
$self->_abstract_function('getChildNodes'); |
$self->_abstract_function('getChildNodes'); |
323 |
$self->_abstract_function('configureCOREHANDLE'); |
$self->_abstract_function('configureCOREHANDLE'); |
324 |
} |
} |
325 |
|
|
|
1; |
|
326 |
|
sub getMetaInfo { |
327 |
|
my $self = shift; |
328 |
|
$self->_abstract_function('getMetaInfo'); |
329 |
|
return; |
330 |
|
} |
331 |
|
|
332 |
|
sub getListUnfiltered { |
333 |
|
my $self = shift; |
334 |
|
$self->_abstract_function('getListUnfiltered'); |
335 |
|
return; |
336 |
|
} |
337 |
|
|
338 |
|
sub getListFiltered { |
339 |
|
my $self = shift; |
340 |
|
$self->_abstract_function('getListFiltered'); |
341 |
|
return; |
342 |
|
} |
343 |
|
|
344 |
|
sub sendQuery { |
345 |
|
my $self = shift; |
346 |
|
$self->_abstract_function('sendQuery'); |
347 |
|
return; |
348 |
|
} |
349 |
|
|
350 |
|
sub connect { |
351 |
|
my $self = shift; |
352 |
|
$self->_abstract_function('connect'); |
353 |
|
return; |
354 |
|
} |
355 |
|
|
356 |
|
sub testIntegrity { |
357 |
|
my $self = shift; |
358 |
|
$self->_abstract_function('testIntegrity'); |
359 |
|
return; |
360 |
|
} |
361 |
|
|
362 |
|
sub quoteSql { |
363 |
|
my $self = shift; |
364 |
|
$self->_abstract_function('quoteSql'); |
365 |
|
return; |
366 |
|
} |
367 |
|
|
368 |
|
sub eraseAll { |
369 |
|
my $self = shift; |
370 |
|
$self->_abstract_function('eraseAll'); |
371 |
|
return; |
372 |
|
} |
373 |
|
|
374 |
|
sub createDb { |
375 |
|
my $self = shift; |
376 |
|
$self->_abstract_function('createDb'); |
377 |
|
return; |
378 |
|
} |
379 |
|
|
380 |
|
1; |