/[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.7 by joko, Thu Dec 5 07:57:48 2002 UTC revision 1.19 by joko, Wed Jun 25 22:53:58 2003 UTC
# Line 1  Line 1 
1  ##    --------------------------------------------------------------------------------  ##    ------------------------------------------------------------------------
2  ##    $Id$  ##    $Id$
3  ##    --------------------------------------------------------------------------------  ##    ------------------------------------------------------------------------
4  ##    $Log$  ##    $Log$
5    ##    Revision 1.19  2003/06/25 22:53:58  joko
6    ##    don't disconnect automagically
7    ##
8    ##    Revision 1.18  2003/06/06 03:40:57  joko
9    ##    disabled autovivifying of arguments as attributes
10    ##
11    ##    Revision 1.17  2003/05/13 07:58:49  joko
12    ##    fix: die if methodname is empty
13    ##    fixes to log-string
14    ##
15    ##    Revision 1.16  2003/04/18 16:07:53  joko
16    ##    just use logger if instantiation successed
17    ##
18    ##    Revision 1.15  2003/02/20 20:19:13  joko
19    ##    tried to get auto-disconnect working again - failed with that
20    ##
21    ##    Revision 1.14  2003/02/09 05:12:28  joko
22    ##    + quoting of strings used in sql-queries!
23    ##
24    ##    Revision 1.13  2003/01/30 22:27:05  joko
25    ##    + added new abstract methods
26    ##
27    ##    Revision 1.12  2003/01/30 21:46:32  joko
28    ##    + fixed behaviour of AUTOLOAD-method
29    ##
30    ##    Revision 1.11  2003/01/20 16:43:18  joko
31    ##    + better argument-property merging
32    ##    + debugging-output !!!
33    ##    + abstract-dummy 'sub createChildNode'
34    ##
35    ##    Revision 1.10  2003/01/19 02:31:51  joko
36    ##    + fix to 'sub AUTOLOAD'
37    ##
38    ##    Revision 1.9  2002/12/19 16:30:23  joko
39    ##    + added 'sub dropDb' and 'sub rebuildDb' as croakers for concrete implementations of methods in proper handlers
40    ##
41    ##    Revision 1.8  2002/12/13 21:48:35  joko
42    ##    + sub _abstract_function
43    ##
44  ##    Revision 1.7  2002/12/05 07:57:48  joko  ##    Revision 1.7  2002/12/05 07:57:48  joko
45  ##    + now using Tie::SecureHash as a base for the COREHANDLE  ##    + now using Tie::SecureHash as a base for the COREHANDLE
46  ##    + former public COREHANDLE becomes private _COREHANDLE now  ##    + former public COREHANDLE becomes private _COREHANDLE now
# Line 24  Line 63 
63  ##  ##
64  ##    Revision 1.1  2002/10/10 03:44:07  cvsjoko  ##    Revision 1.1  2002/10/10 03:44:07  cvsjoko
65  ##    + new  ##    + new
66  ##    --------------------------------------------------------------------------------  ##    ------------------------------------------------------------------------
67    
68    
69  package Data::Storage::Handler::Abstract;  package Data::Storage::Handler::Abstract;
# Line 32  package Data::Storage::Handler::Abstract Line 71  package Data::Storage::Handler::Abstract
71  use strict;  use strict;
72  use warnings;  use warnings;
73    
74    use base qw( DesignPattern::Object );
75    
76    
77  use Data::Dumper;  use Data::Dumper;
78  use Tie::SecureHash;  use Tie::SecureHash;
79  #use Data::Storage::Handler;  #use Data::Storage::Handler;
80    use Hash::Merge qw( merge );
81    
82    #use Log::Dispatch::Config;
83    #Log::Dispatch::Config->configure();
84    
85  # get logger instance  # get logger instance
86  my $logger = Log::Dispatch::Config->instance;  my $logger;
87    eval('$logger = Log::Dispatch::Config->instance;');
88    
89  #our $lock_info;  #our $lock_info;
90    
# Line 46  sub new { Line 93  sub new {
93    my $class = ref($invocant) || $invocant;    my $class = ref($invocant) || $invocant;
94        
95    # logging info about the actual handler called    # logging info about the actual handler called
96      $logger->debug( "$invocant->new( @_ )" );      $logger->debug( "$invocant->new( @_ )" ) if $logger;
97      #$logger->debug( __PACKAGE__ . "->" . "new()" );      #$logger->debug( __PACKAGE__ . "->" . "new()" );
98    
99    # V1 - arguments become properties automagically / normal perl mode blessing    # V1 - arguments become properties automagically / normal perl mode blessing
# Line 57  sub new { Line 104  sub new {
104    bless $self, $class;    bless $self, $class;
105  =cut  =cut
106    
107    #=pod
108    # V2 - maybe more convenient/secure? utilizing Damian Conway's Tie::SecureHash...    # V2 - maybe more convenient/secure? utilizing Damian Conway's Tie::SecureHash...
109    my $self = Tie::SecureHash->new(    my $self = Tie::SecureHash->new(
110      $class,      $class,
# Line 81  sub new { Line 128  sub new {
128        #_protected => ,        #_protected => ,
129        #__private => ,        #__private => ,
130    );    );
131    #=cut
132    
133    # 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
134        
135      # mungle arguments from array into hash - perl does the job  ;)      # mungle arguments from array into hash - perl does the job  ;)
136      my %args = @_;      #my %args = @_;
137          #my %args = ();
138    
139    #print Dumper(@_);
140    
141      # merge attributes one-by-one      # merge attributes one-by-one
142      # TODO: deep_copy? / merge_deep?      # TODO: deep_copy? / merge_deep?
143      foreach (keys %args) {      while (my $elemName = shift @_) {
144        #print "key: $_", "\n";        #print "elemName: $elemName", "\n";
145        $self->{$_} = $args{$_};        if (my $elemValue = shift @_) {
146            #print Dumper($elemValue);
147            #print "elemName=$elemName, elemValue=$elemValue", "\n";
148            $self->{$elemName} = $elemValue;
149          }
150          #$self->{$_} = $args{$_};
151          #$self->{$_} = $args{$_};
152      }      }
153        
154    # 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 104  sub new { Line 160  sub new {
160    # handle meta data    # handle meta data
161      #my $metainfo = $self->getMetaInfo($class);      #my $metainfo = $self->getMetaInfo($class);
162      my $metainfo = $self->getMetaInfo();      my $metainfo = $self->getMetaInfo();
163      if (!$metainfo->{disconnectMethod}) { $metainfo->{disconnectMethod} = 'disconnect'; }      #if (!$metainfo->{disconnectMethod}) { $metainfo->{disconnectMethod} = 'disconnect'; }
164      # type?      # type?
165      $invocant =~ s/Data::Storage::Handler:://;      $invocant =~ s/Data::Storage::Handler:://;
166      $metainfo->{type} = $invocant;      $metainfo->{type} = $invocant;
# Line 116  sub new { Line 172  sub new {
172  }  }
173    
174    
175    # TODO: use NEXT.pm inside this mechanism (to avoid repetitions...)
176  sub AUTOLOAD {  sub AUTOLOAD {
177    
178    # recursion problem to be avoided here!!!    # recursion problem to be avoided here!!!
# Line 152  sub AUTOLOAD { Line 209  sub AUTOLOAD {
209    #if (!$self->exists('COREHANDLE')) { return; }    #if (!$self->exists('COREHANDLE')) { return; }
210    
211    # handle locking (hack)    # handle locking (hack)
212    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}) {
213      $self->{lock_info}->{log_lock} = 1;      $self->{lock_info}->{log_lock} = 1;
214    } else {    } else {
215      $self->{lock_info}->{log_lock} = 0;      $self->{lock_info}->{log_lock} = 0;
# Line 173  sub AUTOLOAD { Line 230  sub AUTOLOAD {
230    #if (!$self->exists('_COREHANDLE')) {    #if (!$self->exists('_COREHANDLE')) {
231    #if (!$self->{_COREHANDLE}) {    #if (!$self->{_COREHANDLE}) {
232    if (!$core) {    if (!$core) {
233      my $err_msg_core = __PACKAGE__ . "[$self->{metainfo}->{type}]" . ": " . "COREHANDLE is undefined while trying to execute method \"$methodname\"";  
234  print $err_msg_core, "\n";  #print Dumper($self);    
235      if ($self->exists('lock_info') && !$self->{lock_info}->{log_lock}) {  #exit;
236    
237        my $handlertype = $self->{metainfo}->{type};
238        $handlertype ||= '';
239    
240        my $err_msg_core = __PACKAGE__ . "[$handlertype]" . ": " . "COREHANDLE is undefined while trying to execute method \"$methodname\"";
241    
242    #print $err_msg_core, "\n";
243    
244        #if ($self->exists('lock_info') && !$self->{lock_info}->{log_lock}) {
245        $logger->error( $err_msg_core );        $logger->error( $err_msg_core );
246      }      #}
247    
248      return;      return;
249        
250    }    }
251  #=cut  #=cut
252    
253    =pod
254      if (!$methodname) {
255        die("Methodname is not defined!");
256        return;
257      }
258    =cut
259    
260  #print "$methodname - 3", "\n";  #print "$methodname - 3", "\n";
261    
262    # try to dispatch method-call to Storage::Handler::*    # try to dispatch method-call to Storage::Handler::*
# Line 210  print $err_msg_core, "\n"; Line 285  print $err_msg_core, "\n";
285      #$lock_AUTOLOAD = 1 if ($methodname eq 'insert');      #$lock_AUTOLOAD = 1 if ($methodname eq 'insert');
286      if (!$self->{lock_info}->{log_lock}) {      if (!$self->{lock_info}->{log_lock}) {
287        #print "method: $methodname", "\n";        #print "method: $methodname", "\n";
288        $logger->debug( __PACKAGE__ . "[$self->{metainfo}->{type}]" . "->" . $methodname . "(@_)" );        my $type = $self->{metainfo}->{type};
289          $type ||= '';
290          # FIXME!
291          #$logger->debug( __PACKAGE__ . "[$type]" . "->" . $methodname . "(@_)" );
292          $logger->debug( __PACKAGE__ . "[$type]" . "->" . $methodname );
293      } else {      } else {
294        # 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
295      }      }
# Line 219  print $err_msg_core, "\n"; Line 298  print $err_msg_core, "\n";
298            
299  #print "calling: $methodname", "\n";  #print "calling: $methodname", "\n";
300            
301      # 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
302      return $core->$methodname(@_);      return $core->$methodname(@_);
303        
304    } elsif ($self->can($methodname)) {    } elsif ($self->can($methodname)) {
305        # try to call specified method inside our or inherited module(s) scope(s)
306      return $self->$methodname(@_);      return $self->$methodname(@_);
307    }    }
308    
# Line 230  print $err_msg_core, "\n"; Line 310  print $err_msg_core, "\n";
310    
311  sub DESTROY {  sub DESTROY {
312    my $self = shift;    my $self = shift;
313    #if ($self->{COREHANDLE}) {  
314    if ($self->exists('_COREHANDLE')) {  return;
315    
316      $logger->debug( __PACKAGE__ . "[$self->{metainfo}->{type}]" . "->DESTROY" );
317    
318      my $disconnectMethod = $self->{metainfo}->{disconnectMethod};
319      print "meth: ", $disconnectMethod, "\n";
320      
321      #$disconnectMethod && $self->{_COREHANDLE} && ( $self->{_COREHANDLE}->$disconnectMethod() );
322      $self->{_COREHANDLE}->$disconnectMethod();
323      #$self->$disconnectMethod();
324    
325      #my $core1 = $self->getCOREHANDLE() if $self->can('getCOREHANDLE');
326      #$core1->$disconnectMethod();
327    
328    return;
329    
330      print "DESTROY-1", "\n";
331      #if ($self->{__COREHANDLE}) {
332      #if ($self->exists('_COREHANDLE')) {
333    
334      # get corehandle instance from underlying handler
335      my $core;
336      $core = $self->getCOREHANDLE() if $self->can('getCOREHANDLE');
337    
338      #if ($self->{STORAGEHANDLE}) {
339      if ($core) {
340        print "DESTROY-2", "\n";
341      $logger->debug( __PACKAGE__ . "[$self->{metainfo}->{type}]" . "->DESTROY" );      $logger->debug( __PACKAGE__ . "[$self->{metainfo}->{type}]" . "->DESTROY" );
342    
343      my $disconnectMethod = $self->{metainfo}->{disconnectMethod};      my $disconnectMethod = $self->{metainfo}->{disconnectMethod};
# Line 244  sub DESTROY { Line 350  sub DESTROY {
350    }    }
351  }  }
352    
   
 sub _abstract_function {  
   my $self = shift;  
   my $fName = shift;  
   my $class = ref($self);  
   # was:  
   # $logger->error( __PACKAGE__ . ": function \"$fName\" is an abstract method, please implement it in \"$class\"");  
   # is:  
   die( __PACKAGE__ . ": function \"$fName\" is an abstract method, please implement it in \"$class\"");  
   #exit;  
 }  
   
353  sub _typeCheck2 {  sub _typeCheck2 {
354    my $type = shift;    my $type = shift;
355    print "type: $type";    print "type: $type";
# Line 270  sub _typeCheck2 { Line 364  sub _typeCheck2 {
364    sub existsChildNode {    sub existsChildNode {
365      my $self = shift;      my $self = shift;
366      my $nodename = shift;      my $nodename = shift;
367      #$nodename = 'TransactionRoutingTable';  
368      $logger->debug( __PACKAGE__ . "->getChildNode( nodename $nodename )" );      # TODO: don't use $self->{meta}->{childnodes} directly in here
369        # get it returned from $self->getChildNodes()!!!
370    
371        $logger->debug( __PACKAGE__ . "->existsChildNode( nodename=$nodename )" );
372      $self->getChildNodes() unless $self->{meta}->{childnodes};      $self->getChildNodes() unless $self->{meta}->{childnodes};
373      my $result = grep(m/$nodename/i, @{$self->{meta}->{childnodes}});     # TODO: use "/i" only on win32-systems!  
374        # quote this, it might contain meta characters which don't work in a regex
375          $nodename = quotemeta($nodename);
376    
377        # trace
378          #print Dumper($self->{meta});
379          #print "nodename: $nodename", "\n";
380        
381        # FIXME: use "/i" only on win32-systems!
382        my $result = grep(m/$nodename/i, @{$self->{meta}->{childnodes}});
383        
384      return $result;      return $result;
385    }    }
386    
# Line 308  sub _typeCheck2 { Line 415  sub _typeCheck2 {
415      $self->_abstract_function('sendCommand');      $self->_abstract_function('sendCommand');
416    }    }
417    
418      sub createChildNode {
419        my $self = shift;
420        $self->_abstract_function('createChildNode');
421      }
422    
423    sub existsChildNode_tmp {    sub existsChildNode_tmp {
424      my $self = shift;      my $self = shift;
425      $self->_abstract_function('existsChildNode');      $self->_abstract_function('existsChildNode');
# Line 377  sub _typeCheck2 { Line 489  sub _typeCheck2 {
489      return;      return;
490    }    }
491    
492      sub dropDb {
493        my $self = shift;
494        $self->_abstract_function('dropDb');
495        return;
496      }
497    
498      sub rebuildDb {
499        my $self = shift;
500        $self->_abstract_function('rebuildDb');
501        return;
502      }
503    
504      sub getDbName {
505        my $self = shift;
506        $self->_abstract_function('getDbName');
507        return;
508      }
509    
510      sub testAvailability {
511        my $self = shift;
512        $self->_abstract_function('testAvailability');
513        return;
514      }
515    
516      sub isConnected {
517        my $self = shift;
518        $self->_abstract_function('isConnected');
519        return;
520      }
521    
522      sub testDsn {
523        my $self = shift;
524        $self->_abstract_function('testDsn');
525        return;
526      }
527    
528  1;  1;

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.19

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