| 1 |
cvsjoko |
1.1 |
################################# |
| 2 |
|
|
# |
| 3 |
|
|
# $Id$ |
| 4 |
|
|
# |
| 5 |
|
|
# $Log$ |
| 6 |
|
|
# |
| 7 |
|
|
################################# |
| 8 |
|
|
|
| 9 |
|
|
package Data::Storage; |
| 10 |
|
|
|
| 11 |
|
|
use strict; |
| 12 |
|
|
use warnings; |
| 13 |
|
|
|
| 14 |
|
|
use Data::Storage::Locator; |
| 15 |
|
|
use Data::Storage::Handler::DBI; |
| 16 |
|
|
use Data::Storage::Handler::Tangram; |
| 17 |
|
|
|
| 18 |
|
|
# get logger instance |
| 19 |
|
|
my $logger = Log::Dispatch::Config->instance; |
| 20 |
|
|
|
| 21 |
|
|
sub new { |
| 22 |
|
|
my $invocant = shift; |
| 23 |
|
|
my $class = ref($invocant) || $invocant; |
| 24 |
|
|
#my @args = normalizeArgs(@_); |
| 25 |
|
|
|
| 26 |
|
|
my $arg_locator = shift; |
| 27 |
|
|
my $arg_options = shift; |
| 28 |
|
|
|
| 29 |
|
|
#my $self = { STORAGEHANDLE => undef, @_ }; |
| 30 |
|
|
my $self = { STORAGEHANDLE => undef, locator => $arg_locator, options => $arg_options }; |
| 31 |
|
|
$logger->debug( __PACKAGE__ . "[$self->{locator}->{type}]" . "->new(@_)" ); |
| 32 |
|
|
return bless $self, $class; |
| 33 |
|
|
} |
| 34 |
|
|
|
| 35 |
|
|
sub AUTOLOAD { |
| 36 |
|
|
|
| 37 |
|
|
my $self = shift; |
| 38 |
|
|
our $AUTOLOAD; |
| 39 |
|
|
|
| 40 |
|
|
# ->DESTROY would - if not declared - trigger an AUTOLOAD also |
| 41 |
|
|
return if $AUTOLOAD =~ m/::DESTROY$/; |
| 42 |
|
|
|
| 43 |
|
|
my $method = $AUTOLOAD; |
| 44 |
|
|
$method =~ s/^.*:://; |
| 45 |
|
|
|
| 46 |
|
|
#$logger->debug( __PACKAGE__ . "[$self->{locator}->{type}]" . "->" . $method . "(@_)" . " (AUTOLOAD)" ); |
| 47 |
|
|
|
| 48 |
|
|
if ($self->_filter_AUTOLOAD($method)) { |
| 49 |
|
|
$self->_accessStorage(); |
| 50 |
|
|
$self->{STORAGEHANDLE}->$method(@_); |
| 51 |
|
|
} |
| 52 |
|
|
|
| 53 |
|
|
} |
| 54 |
|
|
|
| 55 |
|
|
sub _filter_AUTOLOAD { |
| 56 |
|
|
my $self = shift; |
| 57 |
|
|
my $method = shift; |
| 58 |
|
|
if ($self->{options}->{protected}) { |
| 59 |
|
|
if ($method eq 'disconnect') { |
| 60 |
|
|
return; |
| 61 |
|
|
} |
| 62 |
|
|
} |
| 63 |
|
|
return 1; |
| 64 |
|
|
} |
| 65 |
|
|
|
| 66 |
|
|
|
| 67 |
|
|
sub normalizeArgs { |
| 68 |
|
|
my %args = @_; |
| 69 |
|
|
if (!$args{dsn} && $args{meta}{dsn}) { |
| 70 |
|
|
$args{dsn} = $args{meta}{dsn}; |
| 71 |
|
|
} |
| 72 |
|
|
my @result = %args; |
| 73 |
|
|
return @result; |
| 74 |
|
|
} |
| 75 |
|
|
|
| 76 |
|
|
sub _accessStorage { |
| 77 |
|
|
my $self = shift; |
| 78 |
|
|
# TODO: to some tracelevel! |
| 79 |
|
|
#$logger->debug( __PACKAGE__ . "[$self->{type}]" . "->_accessStorage()" ); |
| 80 |
|
|
if (!$self->{STORAGEHANDLE}) { |
| 81 |
|
|
$self->_createStorageHandle(); |
| 82 |
|
|
} |
| 83 |
|
|
} |
| 84 |
|
|
|
| 85 |
|
|
sub _createStorageHandle { |
| 86 |
|
|
my $self = shift; |
| 87 |
|
|
|
| 88 |
|
|
my $type = $self->{locator}->{type}; |
| 89 |
|
|
$logger->debug( __PACKAGE__ . "[$type]" . "->_createStorageHandle()" ); |
| 90 |
|
|
|
| 91 |
|
|
my $pkg = "Data::Storage::Handler::" . $type . ""; |
| 92 |
|
|
|
| 93 |
|
|
# propagate args to handler |
| 94 |
|
|
# needs some more thoughts! (not only "dbi" to Tangram, when (in future) db is not more the common case) |
| 95 |
|
|
if ($type eq 'DBI') { |
| 96 |
|
|
#my @args = %{$self->{locator}->{dbi}}; |
| 97 |
|
|
my @args = %{$self->{locator}}; |
| 98 |
|
|
$self->{STORAGEHANDLE} = $pkg->new( @args ); |
| 99 |
|
|
} |
| 100 |
|
|
if ($type eq 'Tangram') { |
| 101 |
|
|
#$self->{STORAGEHANDLE} = $pkg->new( dsn => $self->{locator}->{dbi}->{dsn} ); |
| 102 |
|
|
#my @args = %{$self->{locator}->{dbi}}; |
| 103 |
|
|
my @args = %{$self->{locator}}; |
| 104 |
|
|
$self->{STORAGEHANDLE} = $pkg->new( @args ); |
| 105 |
|
|
#$self->{STORAGEHANDLE_UNDERLYING} = $self->{STORAGEHANDLE}->getUnderlyingStorage(); |
| 106 |
|
|
#$self->{STORAGEHANDLE_UNDERLYING}->_configureCOREHANDLE(); |
| 107 |
|
|
} |
| 108 |
|
|
|
| 109 |
|
|
} |
| 110 |
|
|
|
| 111 |
|
|
sub addLogDispatchHandler { |
| 112 |
|
|
|
| 113 |
|
|
my $self = shift; |
| 114 |
|
|
my $name = shift; |
| 115 |
|
|
my $package = shift; |
| 116 |
|
|
my $logger = shift; |
| 117 |
|
|
my $objectCreator = shift; |
| 118 |
|
|
|
| 119 |
|
|
#$logger->add( Log::Dispatch::Tangram->new( name => $name, |
| 120 |
|
|
$logger->add( $package->new( name => $name, |
| 121 |
|
|
#min_level => 'debug', |
| 122 |
|
|
min_level => 'info', |
| 123 |
|
|
storage => $self, |
| 124 |
|
|
objectCreator => $objectCreator, |
| 125 |
|
|
fields => { |
| 126 |
|
|
message => 'usermsg', |
| 127 |
|
|
timestamp => 'stamp', |
| 128 |
|
|
level => 'level', |
| 129 |
|
|
name => 'code', |
| 130 |
|
|
}, |
| 131 |
|
|
filter_patterns => [ '->insert\(SystemEvent=' ], |
| 132 |
|
|
#filter_patterns => [ 'SystemEvent' ], |
| 133 |
|
|
|
| 134 |
|
|
#format => '[%d] [%p] %m%n', |
| 135 |
|
|
) ); |
| 136 |
|
|
|
| 137 |
|
|
} |
| 138 |
|
|
|
| 139 |
|
|
sub removeLogDispatchHandler { |
| 140 |
|
|
|
| 141 |
|
|
my $self = shift; |
| 142 |
|
|
my $name = shift; |
| 143 |
|
|
my $logger = shift; |
| 144 |
|
|
|
| 145 |
|
|
$logger->remove($name); |
| 146 |
|
|
|
| 147 |
|
|
} |
| 148 |
|
|
|
| 149 |
|
|
sub getDbName { |
| 150 |
|
|
my $self = shift; |
| 151 |
|
|
my $dsn = $self->{locator}->{dbi}->{dsn}; |
| 152 |
|
|
$dsn =~ m/database=(.+?);/; |
| 153 |
|
|
my $database_name = $1; |
| 154 |
|
|
return $database_name; |
| 155 |
|
|
} |
| 156 |
|
|
|
| 157 |
|
|
sub testDsn { |
| 158 |
|
|
my $self = shift; |
| 159 |
|
|
my $dsn = $self->{locator}->{dbi}->{dsn}; |
| 160 |
|
|
my $result; |
| 161 |
|
|
if ( my $dbh = DBI->connect($dsn, '', '', { |
| 162 |
|
|
PrintError => 0, |
| 163 |
|
|
} ) ) { |
| 164 |
|
|
$dbh->disconnect(); |
| 165 |
|
|
return 1; |
| 166 |
|
|
} else { |
| 167 |
|
|
$logger->error( __PACKAGE__ . "[$self->{locator}->{type}]" . "->testDsn(): " . "DBI-error: " . DBI::errstr ); |
| 168 |
|
|
} |
| 169 |
|
|
} |
| 170 |
|
|
|
| 171 |
|
|
1; |