1 |
cvsjoko |
1.1 |
################################# |
2 |
|
|
# |
3 |
|
|
# $Id$ |
4 |
|
|
# |
5 |
|
|
# $Log$ |
6 |
|
|
# |
7 |
|
|
################################# |
8 |
|
|
|
9 |
|
|
package Data::Storage::Handler::Abstract; |
10 |
|
|
|
11 |
|
|
use strict; |
12 |
|
|
use warnings; |
13 |
|
|
|
14 |
|
|
use Data::Dumper; |
15 |
|
|
|
16 |
|
|
# get logger instance |
17 |
|
|
my $logger = Log::Dispatch::Config->instance; |
18 |
|
|
|
19 |
|
|
#our $lock_info; |
20 |
|
|
|
21 |
|
|
sub new { |
22 |
|
|
my $invocant = shift; |
23 |
|
|
my $class = ref($invocant) || $invocant; |
24 |
|
|
|
25 |
|
|
# logging info about the actual handler called |
26 |
|
|
$logger->debug( __PACKAGE__ . "->" . "new()" ); |
27 |
|
|
$logger->debug( "$invocant->new( @_ )" ); |
28 |
|
|
|
29 |
|
|
# handle meta data |
30 |
|
|
my $metainfo = _getMetaInfo($class); |
31 |
|
|
if (!$metainfo->{disconnectMethod}) { $metainfo->{disconnectMethod} = 'disconnect'; } |
32 |
|
|
# type? |
33 |
|
|
$invocant =~ s/Data::Storage::Handler:://; |
34 |
|
|
$metainfo->{type} = $invocant; |
35 |
|
|
|
36 |
|
|
# create object from blessed hash-reference |
37 |
|
|
# direct accessible (non-protected) properties ( was: my $self = { }; ) |
38 |
|
|
my $self = { metainfo => $metainfo, @_ }; |
39 |
|
|
bless $self, $class; |
40 |
|
|
|
41 |
|
|
return $self; |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
|
45 |
|
|
sub AUTOLOAD { |
46 |
|
|
|
47 |
|
|
# recursion problem to be avoided here!!! |
48 |
|
|
|
49 |
|
|
# - problem: we get recursion-hangs!!! => perl stops with ... |
50 |
|
|
# "Deep recursion on subroutine "Data::Storage::Handler::Abstract::AUTOLOAD" at [...]/libs/Data/Storage.pm line 38." |
51 |
|
|
|
52 |
|
|
# - when: if we log to ourselves as a storage-implementation (e.g. via Log::Dispatch::Tangram) |
53 |
|
|
# - why: debug-messages are on a very low level including every data-operation to "Data::Storage(::Handler::X)", |
54 |
|
|
# so e.g. a "$storage->insert" would trigger another "$storage->insert" itself |
55 |
|
|
# which leads to a infinite recursion loop (deep recursion) |
56 |
|
|
# - solution: locks! (by Hack or via Perl "local"s) |
57 |
|
|
|
58 |
|
|
my $self = shift; |
59 |
|
|
|
60 |
|
|
our $AUTOLOAD; |
61 |
|
|
#return if $AUTOLOAD =~ m/::DESTROY$/; |
62 |
|
|
|
63 |
|
|
# find out methodname |
64 |
|
|
my $methodname = $AUTOLOAD; |
65 |
|
|
$methodname =~ s/^.*:://; |
66 |
|
|
|
67 |
|
|
# handle locking (hack) |
68 |
|
|
if ($self->{lock_info}->{last_method} && $methodname eq $self->{lock_info}->{last_method}) { |
69 |
|
|
$self->{lock_info}->{log_lock} = 1; |
70 |
|
|
} else { |
71 |
|
|
$self->{lock_info}->{log_lock} = 0; |
72 |
|
|
} |
73 |
|
|
$self->{lock_info}->{last_method} = $methodname; |
74 |
|
|
|
75 |
|
|
# test for COREHANDLE |
76 |
|
|
if (!$self->{COREHANDLE}) { |
77 |
|
|
$logger->error( __PACKAGE__ . "[$self->{metainfo}->{type}]" . ": " . "COREHANDLE is undefined while trying to execute method \"$methodname\"" ); |
78 |
|
|
return; |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
# try to dispatch method-call to Storage::Handler::* |
82 |
|
|
#if ($self->can($methodname)) { |
83 |
|
|
#$logger->debug( __PACKAGE__ . "[$self->{metainfo}->{type}]" . "->" . $methodname . "(@_)" ); |
84 |
|
|
#my $res = $self->$methodname(@_); |
85 |
|
|
#if ($res) { return $res; } |
86 |
|
|
#} |
87 |
|
|
|
88 |
|
|
# try to dispatch method-call to COREHANDLE |
89 |
|
|
if ($self->{COREHANDLE}->can($methodname) || $self->{COREHANDLE}->can("AUTOLOAD")) { |
90 |
|
|
#$logger->debug( __PACKAGE__ . "->" . $methodname . " (AUTOLOAD)" ); |
91 |
|
|
#$lock_AUTOLOAD = 1 if ($methodname eq 'insert'); |
92 |
|
|
if (!$self->{lock_info}->{log_lock}) { |
93 |
|
|
#print "method: $methodname", "\n"; |
94 |
|
|
$logger->debug( __PACKAGE__ . "[$self->{metainfo}->{type}]" . "->" . $methodname . "(@_)" ); |
95 |
|
|
} else { |
96 |
|
|
# AUTOLOAD - sub is locked to prevent deep recursions if (e.g.) db-inserts cause log-actions to same db itself |
97 |
|
|
} |
98 |
|
|
#$lock_AUTOLOAD = 0; |
99 |
|
|
#$logger->log( level => 'debug', message => __PACKAGE__ . "->" . $methodname . " (AUTOLOAD)" ); |
100 |
|
|
return $self->{COREHANDLE}->$methodname(@_); |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
sub DESTROY { |
106 |
|
|
my $self = shift; |
107 |
|
|
if ($self->{COREHANDLE}) { |
108 |
|
|
$logger->debug( __PACKAGE__ . "[$self->{metainfo}->{type}]" . "->DESTROY" ); |
109 |
|
|
|
110 |
|
|
my $disconnectMethod = $self->{metainfo}->{disconnectMethod}; |
111 |
|
|
|
112 |
|
|
# call "disconnect" or alike on COREHANDLE |
113 |
|
|
# was: $self->{COREHANDLE}->disconnect(); |
114 |
|
|
$disconnectMethod && ( $self->{COREHANDLE}->$disconnectMethod() ); |
115 |
|
|
|
116 |
|
|
undef $self->{COREHANDLE}; |
117 |
|
|
} |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
|
121 |
|
|
sub _abstract_function { |
122 |
|
|
my $self = shift; |
123 |
|
|
my $fName = shift; |
124 |
|
|
my $class = ref($self); |
125 |
|
|
$logger->error( __PACKAGE__ . ": function \"$fName\" is an abstract method, please implement it in \"$class\""); |
126 |
|
|
#exit; |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
sub _typeCheck { |
130 |
|
|
my $type = shift; |
131 |
|
|
print "type: $type"; |
132 |
|
|
eval("use Data::Storage::$type;"); |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
sub _getMetaInfo { |
136 |
|
|
my $packagename = shift; |
137 |
|
|
#return $self->$metainfo; |
138 |
|
|
|
139 |
|
|
my $ns = '$' . $packagename . "::metainfo"; |
140 |
|
|
$logger->debug( __PACKAGE__ . "->" . "_getMetaInfo()" . " " . "[$ns]" ); |
141 |
|
|
return eval($ns); |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
|
145 |
|
|
# ==================================================== |
146 |
|
|
# PUBLIC METHODS |
147 |
|
|
# ==================================================== |
148 |
|
|
|
149 |
|
|
# TODO: abstract "abstract methods" to list/hash to be used in AUTOLOAD |
150 |
|
|
# e.g.: my @ABSTRACT_METHODS = (qw( connect sendCommand getChildNodes )); |
151 |
|
|
|
152 |
|
|
sub connect { |
153 |
|
|
|
154 |
|
|
my $self = shift; |
155 |
|
|
$self->_abstract_function('connect'); |
156 |
|
|
return; |
157 |
|
|
|
158 |
|
|
$logger->debug( "\"connect\" has to be implemented by handler!" ); |
159 |
|
|
return; |
160 |
|
|
|
161 |
|
|
# my $self = shift; |
162 |
|
|
# my $connectionString = shift; |
163 |
|
|
# # todo: patch connectionString to $args |
164 |
|
|
# _typeCheck($self->{args}{type}); |
165 |
|
|
|
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
sub sendCommand { |
169 |
|
|
my $self = shift; |
170 |
|
|
$self->_abstract_function('sendCommand'); |
171 |
|
|
} |
172 |
|
|
|
173 |
|
|
sub getChildNodes { |
174 |
|
|
my $self = shift; |
175 |
|
|
$self->_abstract_function('getChildNodes'); |
176 |
|
|
} |
177 |
|
|
|
178 |
|
|
sub configureCOREHANDLE { |
179 |
|
|
my $self = shift; |
180 |
|
|
$self->_abstract_function('configureCOREHANDLE'); |
181 |
|
|
} |
182 |
|
|
|
183 |
|
|
1; |