1 |
################################# |
## -------------------------------------------------------------------------------- |
2 |
# |
## $Id$ |
3 |
# $Id$ |
## -------------------------------------------------------------------------------- |
4 |
# |
## $Log$ |
5 |
# $Log$ |
## Revision 1.8 2002/12/13 21:48:35 joko |
6 |
# Revision 1.5 2002/12/01 22:19:33 joko |
## + sub _abstract_function |
7 |
# + just disconnect if COREHANDLE exists |
## |
8 |
# |
## Revision 1.7 2002/12/05 07:57:48 joko |
9 |
# Revision 1.4 2002/12/01 04:45:38 joko |
## + now using Tie::SecureHash as a base for the COREHANDLE |
10 |
# + sub eraseAll |
## + former public COREHANDLE becomes private _COREHANDLE now |
11 |
# + sub createDb |
## |
12 |
# |
## Revision 1.6 2002/12/03 15:52:24 joko |
13 |
# Revision 1.3 2002/11/29 04:58:20 joko |
## + fix/feature: if dispatching to deep core method fails (is not declared), try method at Data::Storage - level |
14 |
# + Storage::Result now uses the same dispatching mechanism like Storage::Handler |
## |
15 |
# |
## Revision 1.5 2002/12/01 22:19:33 joko |
16 |
# Revision 1.2 2002/10/17 00:08:07 joko |
## + just disconnect if COREHANDLE exists |
17 |
# + bugfixes regarding "deep recursion" stuff |
## |
18 |
# |
## Revision 1.4 2002/12/01 04:45:38 joko |
19 |
# Revision 1.1 2002/10/10 03:44:07 cvsjoko |
## + sub eraseAll |
20 |
# + new |
## + sub createDb |
21 |
# |
## |
22 |
# |
## Revision 1.3 2002/11/29 04:58:20 joko |
23 |
################################# |
## + Storage::Result now uses the same dispatching mechanism like Storage::Handler |
24 |
|
## |
25 |
|
## Revision 1.2 2002/10/17 00:08:07 joko |
26 |
|
## + bugfixes regarding "deep recursion" stuff |
27 |
|
## |
28 |
|
## Revision 1.1 2002/10/10 03:44:07 cvsjoko |
29 |
|
## + new |
30 |
|
## -------------------------------------------------------------------------------- |
31 |
|
|
32 |
|
|
33 |
package Data::Storage::Handler::Abstract; |
package Data::Storage::Handler::Abstract; |
34 |
|
|
35 |
use strict; |
use strict; |
36 |
use warnings; |
use warnings; |
37 |
|
|
38 |
|
use base qw( DesignPattern::Object ); |
39 |
|
|
40 |
use Data::Dumper; |
use Data::Dumper; |
41 |
|
use Tie::SecureHash; |
42 |
|
#use Data::Storage::Handler; |
43 |
|
|
44 |
# get logger instance |
# get logger instance |
45 |
my $logger = Log::Dispatch::Config->instance; |
my $logger = Log::Dispatch::Config->instance; |
54 |
$logger->debug( "$invocant->new( @_ )" ); |
$logger->debug( "$invocant->new( @_ )" ); |
55 |
#$logger->debug( __PACKAGE__ . "->" . "new()" ); |
#$logger->debug( __PACKAGE__ . "->" . "new()" ); |
56 |
|
|
57 |
# arguments become properties |
# V1 - arguments become properties automagically / normal perl mode blessing |
58 |
|
=pod |
59 |
|
# autovivify passed-in arguments as future object attributes into to-be-blessed hash |
60 |
my $self = { @_ }; |
my $self = { @_ }; |
61 |
# create object from blessed hash-reference |
# create object from blessed hash-reference |
62 |
bless $self, $class; |
bless $self, $class; |
63 |
|
=cut |
64 |
|
|
65 |
|
|
66 |
|
# V2 - maybe more convenient/secure? utilizing Damian Conway's Tie::SecureHash... |
67 |
|
my $self = Tie::SecureHash->new( |
68 |
|
$class, |
69 |
|
# that's it: |
70 |
|
'metainfo' => undef, |
71 |
|
'lock_info' => undef, |
72 |
|
'_COREHANDLE' => undef, |
73 |
|
'meta' => undef, |
74 |
|
'locator' => undef, |
75 |
|
'dataStorageLayer' => undef, |
76 |
|
'dbi' => undef, |
77 |
|
# tries: |
78 |
|
#'Data::Storage::Handler::Abstract::metainfo' => undef, |
79 |
|
#'dataStorageLayer' |
80 |
|
#'Data::Storage::Handler::Abstract::dataStorageLayer' => undef, |
81 |
|
#'Data::Storage::Handler::Tangram::dataStorageLayer' => undef, |
82 |
|
#'Data::Dumper::locator' => undef, |
83 |
|
#$class . '::locator' => undef, |
84 |
|
#'Data::Storage::Handler::Tangram::locator' => undef, |
85 |
|
#'Data::Storage::Handler::DBI::locator' => undef, |
86 |
|
#_protected => , |
87 |
|
#__private => , |
88 |
|
); |
89 |
|
|
90 |
|
|
91 |
|
# merge passed-in arguments to constructor as properties into already blessed secure object |
92 |
|
|
93 |
|
# mungle arguments from array into hash - perl does the job ;) |
94 |
|
my %args = @_; |
95 |
|
|
96 |
|
# merge attributes one-by-one |
97 |
|
# TODO: deep_copy? / merge_deep? |
98 |
|
foreach (keys %args) { |
99 |
|
#print "key: $_", "\n"; |
100 |
|
$self->{$_} = $args{$_}; |
101 |
|
} |
102 |
|
|
103 |
|
# V3 - rolling our own security (just for {COREHANDLE} - nothing else) - nope, Tie::SecureHash works wonderful |
104 |
|
#my $self = { @_ }; |
105 |
|
#bless $self, $class; |
106 |
|
|
107 |
|
|
108 |
|
|
109 |
# handle meta data |
# handle meta data |
110 |
#my $metainfo = $self->getMetaInfo($class); |
#my $metainfo = $self->getMetaInfo($class); |
142 |
# find out methodname |
# find out methodname |
143 |
my $methodname = $AUTOLOAD; |
my $methodname = $AUTOLOAD; |
144 |
$methodname =~ s/^.*:://; |
$methodname =~ s/^.*:://; |
145 |
|
|
146 |
|
#print "method: $methodname", "\n"; |
147 |
|
|
148 |
|
# TODO: document this! handler-private methods listed here will not be triggered (internal use) |
149 |
|
# in this case, "exists" is a method reserved for Tie::SecureHash, |
150 |
|
# which encapsulates the perl data structure (HASH) under this object |
151 |
|
# this is to prevent deep recursion's |
152 |
|
return if lc $methodname eq 'exists'; |
153 |
|
|
154 |
|
#print "$methodname - 1", "\n"; |
155 |
|
|
156 |
|
# TODO: enhance logging/debugging |
157 |
|
#if (!$self->exists('COREHANDLE')) { return; } |
158 |
|
|
159 |
# handle locking (hack) |
# handle locking (hack) |
160 |
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}) { |
161 |
$self->{lock_info}->{log_lock} = 1; |
$self->{lock_info}->{log_lock} = 1; |
162 |
} else { |
} else { |
163 |
$self->{lock_info}->{log_lock} = 0; |
$self->{lock_info}->{log_lock} = 0; |
164 |
} |
} |
165 |
$self->{lock_info}->{last_method} = $methodname; |
$self->{lock_info}->{last_method} = $methodname; |
166 |
|
|
167 |
|
#print "$methodname - 2", "\n"; |
168 |
|
|
169 |
|
#print Dumper($self); |
170 |
|
#exit; |
171 |
|
|
172 |
|
# get corehandle instance from underlying handler |
173 |
|
my $core = $self->getCOREHANDLE(); |
174 |
|
|
175 |
# test for COREHANDLE |
# test for COREHANDLE |
176 |
if (!$self->{COREHANDLE}) { |
#if (!$self->{_COREHANDLE}) { |
177 |
#print "no COREHANDLE", "\n"; |
#=pod |
178 |
if (!$self->{lock_info}->{log_lock}) { |
#if (!$self->exists('_COREHANDLE')) { |
179 |
$logger->error( __PACKAGE__ . "[$self->{metainfo}->{type}]" . ": " . "COREHANDLE is undefined while trying to execute method \"$methodname\"" ); |
#if (!$self->{_COREHANDLE}) { |
180 |
|
if (!$core) { |
181 |
|
my $err_msg_core = __PACKAGE__ . "[$self->{metainfo}->{type}]" . ": " . "COREHANDLE is undefined while trying to execute method \"$methodname\""; |
182 |
|
print $err_msg_core, "\n"; |
183 |
|
if ($self->exists('lock_info') && !$self->{lock_info}->{log_lock}) { |
184 |
|
$logger->error( $err_msg_core ); |
185 |
} |
} |
186 |
return; |
return; |
187 |
} |
} |
188 |
|
#=cut |
189 |
|
|
190 |
|
#print "$methodname - 3", "\n"; |
191 |
|
|
192 |
# try to dispatch method-call to Storage::Handler::* |
# try to dispatch method-call to Storage::Handler::* |
193 |
#if ($self->can($methodname)) { |
#if ($self->can($methodname)) { |
196 |
#if ($res) { return $res; } |
#if ($res) { return $res; } |
197 |
#} |
#} |
198 |
|
|
199 |
|
#print "$methodname - 4", "\n"; |
200 |
|
|
201 |
# try to dispatch method-call to COREHANDLE |
# try to dispatch method-call to COREHANDLE |
202 |
if ($self->{COREHANDLE}->can($methodname) || $self->{COREHANDLE}->can("AUTOLOAD")) { |
# was: |
203 |
|
#my $core = $self->{_COREHANDLE}; |
204 |
|
# is: |
205 |
|
#my $core = $self->getCOREHANDLE(); |
206 |
|
|
207 |
|
#print Dumper($core); |
208 |
|
#exit; |
209 |
|
|
210 |
|
# was: |
211 |
|
#if ($self->{_COREHANDLE}->can($methodname) || $self->{_COREHANDLE}->can("AUTOLOAD")) { |
212 |
|
# is: |
213 |
|
if ($core->can($methodname) || $core->can("AUTOLOAD")) { |
214 |
#$logger->debug( __PACKAGE__ . "->" . $methodname . " (AUTOLOAD)" ); |
#$logger->debug( __PACKAGE__ . "->" . $methodname . " (AUTOLOAD)" ); |
215 |
#$lock_AUTOLOAD = 1 if ($methodname eq 'insert'); |
#$lock_AUTOLOAD = 1 if ($methodname eq 'insert'); |
216 |
if (!$self->{lock_info}->{log_lock}) { |
if (!$self->{lock_info}->{log_lock}) { |
222 |
#$lock_AUTOLOAD = 0; |
#$lock_AUTOLOAD = 0; |
223 |
#$logger->log( level => 'debug', message => __PACKAGE__ . "->" . $methodname . " (AUTOLOAD)" ); |
#$logger->log( level => 'debug', message => __PACKAGE__ . "->" . $methodname . " (AUTOLOAD)" ); |
224 |
|
|
225 |
|
#print "calling: $methodname", "\n"; |
226 |
|
|
227 |
# method calls doing it until here will get dispatched to the proper handler |
# method calls doing it until here will get dispatched to the proper handler |
228 |
return $self->{COREHANDLE}->$methodname(@_); |
return $core->$methodname(@_); |
229 |
|
|
230 |
|
} elsif ($self->can($methodname)) { |
231 |
|
return $self->$methodname(@_); |
232 |
} |
} |
233 |
|
|
234 |
} |
} |
235 |
|
|
236 |
sub DESTROY { |
sub DESTROY { |
237 |
my $self = shift; |
my $self = shift; |
238 |
if ($self->{COREHANDLE}) { |
#if ($self->{COREHANDLE}) { |
239 |
|
if ($self->exists('_COREHANDLE')) { |
240 |
$logger->debug( __PACKAGE__ . "[$self->{metainfo}->{type}]" . "->DESTROY" ); |
$logger->debug( __PACKAGE__ . "[$self->{metainfo}->{type}]" . "->DESTROY" ); |
241 |
|
|
242 |
my $disconnectMethod = $self->{metainfo}->{disconnectMethod}; |
my $disconnectMethod = $self->{metainfo}->{disconnectMethod}; |
243 |
|
|
244 |
# call "disconnect" or alike on COREHANDLE |
# call "disconnect" or alike on COREHANDLE |
245 |
# was: $self->{COREHANDLE}->disconnect(); |
# was: $self->{COREHANDLE}->disconnect(); |
246 |
$disconnectMethod && $self->{COREHANDLE} && ( $self->{COREHANDLE}->$disconnectMethod() ); |
$disconnectMethod && $self->{_COREHANDLE} && ( $self->{_COREHANDLE}->$disconnectMethod() ); |
247 |
|
|
248 |
undef $self->{COREHANDLE}; |
undef $self->{_COREHANDLE}; |
249 |
} |
} |
250 |
} |
} |
251 |
|
|
252 |
|
sub _typeCheck2 { |
|
sub _abstract_function { |
|
|
my $self = shift; |
|
|
my $fName = shift; |
|
|
my $class = ref($self); |
|
|
$logger->error( __PACKAGE__ . ": function \"$fName\" is an abstract method, please implement it in \"$class\""); |
|
|
#exit; |
|
|
} |
|
|
|
|
|
sub _typeCheck { |
|
253 |
my $type = shift; |
my $type = shift; |
254 |
print "type: $type"; |
print "type: $type"; |
255 |
eval("use Data::Storage::$type;"); |
eval("use Data::Storage::$type;"); |
291 |
# - build them via anonymous subs |
# - build them via anonymous subs |
292 |
# - introduce them via symbols |
# - introduce them via symbols |
293 |
|
|
294 |
|
sub getCOREHANDLE { |
295 |
|
my $self = shift; |
296 |
|
$self->_abstract_function('getCOREHANDLE'); |
297 |
|
} |
298 |
|
|
299 |
sub sendCommand { |
sub sendCommand { |
300 |
my $self = shift; |
my $self = shift; |
301 |
$self->_abstract_function('sendCommand'); |
$self->_abstract_function('sendCommand'); |
302 |
} |
} |
303 |
|
|
304 |
|
sub existsChildNode_tmp { |
305 |
|
my $self = shift; |
306 |
|
$self->_abstract_function('existsChildNode'); |
307 |
|
} |
308 |
|
|
309 |
sub getChildNodes { |
sub getChildNodes { |
310 |
my $self = shift; |
my $self = shift; |
311 |
$self->_abstract_function('getChildNodes'); |
$self->_abstract_function('getChildNodes'); |