/[cvs]/nfo/perl/libs/Data/Storage/Handler/Abstract.pm
ViewVC logotype

Diff of /nfo/perl/libs/Data/Storage/Handler/Abstract.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.8 by joko, Fri Dec 13 21:48:35 2002 UTC revision 1.17 by joko, Tue May 13 07:58:49 2003 UTC
# Line 1  Line 1 
1  ##    --------------------------------------------------------------------------------  ##    ------------------------------------------------------------------------
2  ##    $Id$  ##    $Id$
3  ##    --------------------------------------------------------------------------------  ##    ------------------------------------------------------------------------
4  ##    $Log$  ##    $Log$
5    ##    Revision 1.17  2003/05/13 07:58:49  joko
6    ##    fix: die if methodname is empty
7    ##    fixes to log-string
8    ##
9    ##    Revision 1.16  2003/04/18 16:07:53  joko
10    ##    just use logger if instantiation successed
11    ##
12    ##    Revision 1.15  2003/02/20 20:19:13  joko
13    ##    tried to get auto-disconnect working again - failed with that
14    ##
15    ##    Revision 1.14  2003/02/09 05:12:28  joko
16    ##    + quoting of strings used in sql-queries!
17    ##
18    ##    Revision 1.13  2003/01/30 22:27:05  joko
19    ##    + added new abstract methods
20    ##
21    ##    Revision 1.12  2003/01/30 21:46:32  joko
22    ##    + fixed behaviour of AUTOLOAD-method
23    ##
24    ##    Revision 1.11  2003/01/20 16:43:18  joko
25    ##    + better argument-property merging
26    ##    + debugging-output !!!
27    ##    + abstract-dummy 'sub createChildNode'
28    ##
29    ##    Revision 1.10  2003/01/19 02:31:51  joko
30    ##    + fix to 'sub AUTOLOAD'
31    ##
32    ##    Revision 1.9  2002/12/19 16:30:23  joko
33    ##    + added 'sub dropDb' and 'sub rebuildDb' as croakers for concrete implementations of methods in proper handlers
34    ##
35  ##    Revision 1.8  2002/12/13 21:48:35  joko  ##    Revision 1.8  2002/12/13 21:48:35  joko
36  ##    + sub _abstract_function  ##    + sub _abstract_function
37  ##  ##
# Line 27  Line 57 
57  ##  ##
58  ##    Revision 1.1  2002/10/10 03:44:07  cvsjoko  ##    Revision 1.1  2002/10/10 03:44:07  cvsjoko
59  ##    + new  ##    + new
60  ##    --------------------------------------------------------------------------------  ##    ------------------------------------------------------------------------
61    
62    
63  package Data::Storage::Handler::Abstract;  package Data::Storage::Handler::Abstract;
# Line 37  use warnings; Line 67  use warnings;
67    
68  use base qw( DesignPattern::Object );  use base qw( DesignPattern::Object );
69    
70    
71  use Data::Dumper;  use Data::Dumper;
72  use Tie::SecureHash;  use Tie::SecureHash;
73  #use Data::Storage::Handler;  #use Data::Storage::Handler;
74    use Hash::Merge qw( merge );
75    
76    #use Log::Dispatch::Config;
77    #Log::Dispatch::Config->configure();
78    
79  # get logger instance  # get logger instance
80  my $logger = Log::Dispatch::Config->instance;  my $logger;
81    eval('$logger = Log::Dispatch::Config->instance;');
82    
83  #our $lock_info;  #our $lock_info;
84    
# Line 51  sub new { Line 87  sub new {
87    my $class = ref($invocant) || $invocant;    my $class = ref($invocant) || $invocant;
88        
89    # logging info about the actual handler called    # logging info about the actual handler called
90      $logger->debug( "$invocant->new( @_ )" );      $logger->debug( "$invocant->new( @_ )" ) if $logger;
91      #$logger->debug( __PACKAGE__ . "->" . "new()" );      #$logger->debug( __PACKAGE__ . "->" . "new()" );
92    
93    # V1 - arguments become properties automagically / normal perl mode blessing    # V1 - arguments become properties automagically / normal perl mode blessing
# Line 62  sub new { Line 98  sub new {
98    bless $self, $class;    bless $self, $class;
99  =cut  =cut
100    
101    #=pod
102    # V2 - maybe more convenient/secure? utilizing Damian Conway's Tie::SecureHash...    # V2 - maybe more convenient/secure? utilizing Damian Conway's Tie::SecureHash...
103    my $self = Tie::SecureHash->new(    my $self = Tie::SecureHash->new(
104      $class,      $class,
# Line 86  sub new { Line 122  sub new {
122        #_protected => ,        #_protected => ,
123        #__private => ,        #__private => ,
124    );    );
125    #=cut
126    
127    # 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
128        
129      # mungle arguments from array into hash - perl does the job  ;)      # mungle arguments from array into hash - perl does the job  ;)
130      my %args = @_;      #my %args = @_;
131          #my %args = ();
132    
133    #print Dumper(@_);
134    
135      # merge attributes one-by-one      # merge attributes one-by-one
136      # TODO: deep_copy? / merge_deep?      # TODO: deep_copy? / merge_deep?
137      foreach (keys %args) {      while (my $elemName = shift @_) {
138        #print "key: $_", "\n";        #print "elemName: $elemName", "\n";
139        $self->{$_} = $args{$_};        if (my $elemValue = shift @_) {
140            #print Dumper($elemValue);
141            #print "elemName=$elemName, elemValue=$elemValue", "\n";
142            $self->{$elemName} = $elemValue;
143          }
144          #$self->{$_} = $args{$_};
145          #$self->{$_} = $args{$_};
146      }      }
147        
148    # 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
# Line 121  sub new { Line 166  sub new {
166  }  }
167    
168    
169    # TODO: use NEXT.pm inside this mechanism (to avoid repetitions...)
170  sub AUTOLOAD {  sub AUTOLOAD {
171    
172    # recursion problem to be avoided here!!!    # recursion problem to be avoided here!!!
# Line 157  sub AUTOLOAD { Line 203  sub AUTOLOAD {
203    #if (!$self->exists('COREHANDLE')) { return; }    #if (!$self->exists('COREHANDLE')) { return; }
204    
205    # handle locking (hack)    # handle locking (hack)
206    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}) {
207      $self->{lock_info}->{log_lock} = 1;      $self->{lock_info}->{log_lock} = 1;
208    } else {    } else {
209      $self->{lock_info}->{log_lock} = 0;      $self->{lock_info}->{log_lock} = 0;
# Line 178  sub AUTOLOAD { Line 224  sub AUTOLOAD {
224    #if (!$self->exists('_COREHANDLE')) {    #if (!$self->exists('_COREHANDLE')) {
225    #if (!$self->{_COREHANDLE}) {    #if (!$self->{_COREHANDLE}) {
226    if (!$core) {    if (!$core) {
227      my $err_msg_core = __PACKAGE__ . "[$self->{metainfo}->{type}]" . ": " . "COREHANDLE is undefined while trying to execute method \"$methodname\"";  
228  print $err_msg_core, "\n";  #print Dumper($self);    
229      if ($self->exists('lock_info') && !$self->{lock_info}->{log_lock}) {  #exit;
230    
231        my $handlertype = $self->{metainfo}->{type};
232        $handlertype ||= '';
233    
234        my $err_msg_core = __PACKAGE__ . "[$handlertype]" . ": " . "COREHANDLE is undefined while trying to execute method \"$methodname\"";
235    
236    #print $err_msg_core, "\n";
237    
238        #if ($self->exists('lock_info') && !$self->{lock_info}->{log_lock}) {
239        $logger->error( $err_msg_core );        $logger->error( $err_msg_core );
240      }      #}
241    
242      return;      return;
243        
244    }    }
245  #=cut  #=cut
246    
247      if (!$methodname) {
248        die("Methodname is not defined!");
249        return;
250      }
251    
252  #print "$methodname - 3", "\n";  #print "$methodname - 3", "\n";
253    
254    # try to dispatch method-call to Storage::Handler::*    # try to dispatch method-call to Storage::Handler::*
# Line 215  print $err_msg_core, "\n"; Line 277  print $err_msg_core, "\n";
277      #$lock_AUTOLOAD = 1 if ($methodname eq 'insert');      #$lock_AUTOLOAD = 1 if ($methodname eq 'insert');
278      if (!$self->{lock_info}->{log_lock}) {      if (!$self->{lock_info}->{log_lock}) {
279        #print "method: $methodname", "\n";        #print "method: $methodname", "\n";
280        $logger->debug( __PACKAGE__ . "[$self->{metainfo}->{type}]" . "->" . $methodname . "(@_)" );        my $type = $self->{metainfo}->{type};
281          $type ||= '';
282          # FIXME!
283          #$logger->debug( __PACKAGE__ . "[$type]" . "->" . $methodname . "(@_)" );
284          $logger->debug( __PACKAGE__ . "[$type]" . "->" . $methodname );
285      } else {      } else {
286        # 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
287      }      }
# Line 224  print $err_msg_core, "\n"; Line 290  print $err_msg_core, "\n";
290            
291  #print "calling: $methodname", "\n";  #print "calling: $methodname", "\n";
292            
293      # 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
294      return $core->$methodname(@_);      return $core->$methodname(@_);
295        
296    } elsif ($self->can($methodname)) {    } elsif ($self->can($methodname)) {
297        # try to call specified method inside our or inherited module(s) scope(s)
298      return $self->$methodname(@_);      return $self->$methodname(@_);
299    }    }
300    
# Line 235  print $err_msg_core, "\n"; Line 302  print $err_msg_core, "\n";
302    
303  sub DESTROY {  sub DESTROY {
304    my $self = shift;    my $self = shift;
305    #if ($self->{COREHANDLE}) {  
306    if ($self->exists('_COREHANDLE')) {  return;
307    
308      $logger->debug( __PACKAGE__ . "[$self->{metainfo}->{type}]" . "->DESTROY" );
309    
310      my $disconnectMethod = $self->{metainfo}->{disconnectMethod};
311      print "meth: ", $disconnectMethod, "\n";
312      
313      #$disconnectMethod && $self->{_COREHANDLE} && ( $self->{_COREHANDLE}->$disconnectMethod() );
314      $self->{_COREHANDLE}->$disconnectMethod();
315      #$self->$disconnectMethod();
316    
317      #my $core1 = $self->getCOREHANDLE() if $self->can('getCOREHANDLE');
318      #$core1->$disconnectMethod();
319    
320    return;
321    
322      print "DESTROY-1", "\n";
323      #if ($self->{__COREHANDLE}) {
324      #if ($self->exists('_COREHANDLE')) {
325    
326      # get corehandle instance from underlying handler
327      my $core;
328      $core = $self->getCOREHANDLE() if $self->can('getCOREHANDLE');
329    
330      #if ($self->{STORAGEHANDLE}) {
331      if ($core) {
332        print "DESTROY-2", "\n";
333      $logger->debug( __PACKAGE__ . "[$self->{metainfo}->{type}]" . "->DESTROY" );      $logger->debug( __PACKAGE__ . "[$self->{metainfo}->{type}]" . "->DESTROY" );
334    
335      my $disconnectMethod = $self->{metainfo}->{disconnectMethod};      my $disconnectMethod = $self->{metainfo}->{disconnectMethod};
# Line 263  sub _typeCheck2 { Line 356  sub _typeCheck2 {
356    sub existsChildNode {    sub existsChildNode {
357      my $self = shift;      my $self = shift;
358      my $nodename = shift;      my $nodename = shift;
359      #$nodename = 'TransactionRoutingTable';  
360      $logger->debug( __PACKAGE__ . "->getChildNode( nodename $nodename )" );      # TODO: don't use $self->{meta}->{childnodes} directly in here
361        # get it returned from $self->getChildNodes()!!!
362    
363        $logger->debug( __PACKAGE__ . "->existsChildNode( nodename=$nodename )" );
364      $self->getChildNodes() unless $self->{meta}->{childnodes};      $self->getChildNodes() unless $self->{meta}->{childnodes};
365      my $result = grep(m/$nodename/i, @{$self->{meta}->{childnodes}});     # TODO: use "/i" only on win32-systems!  
366        # quote this, it might contain meta characters which don't work in a regex
367          $nodename = quotemeta($nodename);
368    
369        # trace
370          #print Dumper($self->{meta});
371          #print "nodename: $nodename", "\n";
372        
373        # FIXME: use "/i" only on win32-systems!
374        my $result = grep(m/$nodename/i, @{$self->{meta}->{childnodes}});
375        
376      return $result;      return $result;
377    }    }
378    
# Line 301  sub _typeCheck2 { Line 407  sub _typeCheck2 {
407      $self->_abstract_function('sendCommand');      $self->_abstract_function('sendCommand');
408    }    }
409    
410      sub createChildNode {
411        my $self = shift;
412        $self->_abstract_function('createChildNode');
413      }
414    
415    sub existsChildNode_tmp {    sub existsChildNode_tmp {
416      my $self = shift;      my $self = shift;
417      $self->_abstract_function('existsChildNode');      $self->_abstract_function('existsChildNode');
# Line 370  sub _typeCheck2 { Line 481  sub _typeCheck2 {
481      return;      return;
482    }    }
483    
484      sub dropDb {
485        my $self = shift;
486        $self->_abstract_function('dropDb');
487        return;
488      }
489    
490      sub rebuildDb {
491        my $self = shift;
492        $self->_abstract_function('rebuildDb');
493        return;
494      }
495    
496      sub getDbName {
497        my $self = shift;
498        $self->_abstract_function('getDbName');
499        return;
500      }
501    
502      sub testAvailability {
503        my $self = shift;
504        $self->_abstract_function('testAvailability');
505        return;
506      }
507    
508      sub isConnected {
509        my $self = shift;
510        $self->_abstract_function('isConnected');
511        return;
512      }
513    
514      sub testDsn {
515        my $self = shift;
516        $self->_abstract_function('testDsn');
517        return;
518      }
519    
520  1;  1;

Legend:
Removed from v.1.8  
changed lines
  Added in v.1.17

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed