1 |
################################################ |
2 |
# |
3 |
# $Id$ |
4 |
# |
5 |
# $Log$ |
6 |
# |
7 |
################################################ |
8 |
|
9 |
package Data::Container; |
10 |
|
11 |
use strict; |
12 |
use warnings; |
13 |
|
14 |
# get logger instance |
15 |
my $logger = Log::Dispatch::Config->instance; |
16 |
|
17 |
use libp; |
18 |
use Data::Dumper; |
19 |
|
20 |
|
21 |
sub new { |
22 |
my $invocant = shift; |
23 |
my $class = ref($invocant) || $invocant; |
24 |
|
25 |
# get constructor arguments |
26 |
my @args = (); |
27 |
@_ && (@args = @_); |
28 |
$logger->debug( __PACKAGE__ . "->new( @args )" ); |
29 |
|
30 |
my $self = { @_ }; |
31 |
return bless $self, $class; |
32 |
} |
33 |
|
34 |
sub addConfig { |
35 |
my $self = shift; |
36 |
my $name = shift; |
37 |
my $config = shift; |
38 |
$self->{config}->{$name} = $config; |
39 |
} |
40 |
|
41 |
sub addConfigByDsn { |
42 |
my $self = shift; |
43 |
my $name = shift; |
44 |
my $dsn = shift; |
45 |
|
46 |
# HACK: assume DBI as default type for now |
47 |
# TODO: guess type from dsn |
48 |
my $type = "DBI"; |
49 |
|
50 |
$self->addConfig($name, { type => $type, dsn => $dsn }); |
51 |
} |
52 |
|
53 |
sub initLocator { |
54 |
my $self = shift; |
55 |
my $name = shift; |
56 |
my $db_config = $self->{config}->{$name}; |
57 |
|
58 |
$logger->debug( __PACKAGE__ . "->initLocator( db_config $db_config )" ); |
59 |
|
60 |
my $cfg_locator = {}; |
61 |
|
62 |
# set default settings, if any |
63 |
my $default = deep_copy($self->{config}->{_default}); |
64 |
foreach (keys %$default) { |
65 |
$cfg_locator->{$_} = $default->{$_}; |
66 |
} |
67 |
|
68 |
# name it |
69 |
$cfg_locator->{name} = $name; |
70 |
|
71 |
# merge in specific settings |
72 |
foreach (keys %$db_config) { |
73 |
$cfg_locator->{$_} = $db_config->{$_}; |
74 |
} |
75 |
|
76 |
# HACK: transfer dsn from main to dbi settings |
77 |
$cfg_locator->{dbi}->{dsn} = $cfg_locator->{dsn} if $cfg_locator->{dsn}; |
78 |
|
79 |
# HACK: set classnames empty if none given from config |
80 |
$cfg_locator->{classnames} = [] if !$cfg_locator->{classnames}; |
81 |
|
82 |
# HACK: set errorhandler if dbi settings are present |
83 |
$cfg_locator->{dbi}->{HandleError} = \&_dbErrorHandler if $cfg_locator->{dbi}; |
84 |
|
85 |
# create new locator object |
86 |
$self->{locator}->{$name} = Data::Storage::Locator->new( $cfg_locator ); |
87 |
|
88 |
} |
89 |
|
90 |
sub initLocators { |
91 |
my $self = shift; |
92 |
foreach (keys %{$self->{config}}) { |
93 |
$self->initLocator($_, $self->{config}->{$_}) if !/^_/; |
94 |
} |
95 |
#print "locs: ", Dumper($self->{locator}); |
96 |
} |
97 |
|
98 |
sub getLocator { |
99 |
my $self = shift; |
100 |
my $name = shift; |
101 |
return $self->{locator}->{$name}; |
102 |
} |
103 |
|
104 |
sub initStorage { |
105 |
my $self = shift; |
106 |
my $name = shift; |
107 |
|
108 |
my $locator = $self->getLocator($name); |
109 |
$logger->info( __PACKAGE__ . " is booting storage declared by locator \"$name\"" ); |
110 |
|
111 |
my $storage = Data::Storage->new($locator); |
112 |
|
113 |
my $log_prefix = __PACKAGE__ . "->initStorage: "; |
114 |
$log_prefix .= "dsn=\"$self->{locator}->{$name}->{dsn}\"" if $self->{locator}->{$name}->{dsn}; |
115 |
|
116 |
# should we test availability of the storage before using it? |
117 |
if ($locator->{test_availability}) { |
118 |
if ( !$storage->testAvailability() ) { |
119 |
$logger->error( "$log_prefix is not available" ); |
120 |
return; |
121 |
} |
122 |
} |
123 |
|
124 |
# should we test integrity of the storage before using it? |
125 |
if ($locator->{test_integrity}) { |
126 |
#return unless $storage->testIntegrity(); |
127 |
$locator->{status}->{integrity} = $storage->testIntegrity(); |
128 |
# actions if integrity fails |
129 |
if (!$locator->{status}->{integrity}) { |
130 |
$logger->error( "testIntegrity failed on $log_prefix" ); |
131 |
print "Try a $locator->{name}.deploySchema()? (y/n) "; |
132 |
my $res = <STDIN>; |
133 |
if ($res =~ m/y/i) { |
134 |
$storage->deploySchema(); |
135 |
} |
136 |
} |
137 |
} |
138 |
|
139 |
# try to connect... |
140 |
# TODO: |
141 |
# don't connect right here, do an implicit connect on (later) usage |
142 |
# maybe set ->{meta}->{connectmethod} = "connect" here |
143 |
#return unless $storage->connect(); |
144 |
$storage->connect(); |
145 |
|
146 |
# should we check emptyness? |
147 |
if ( $locator->{test_emptyness} && $locator->{status}->{integrity} ) { |
148 |
if ( !@{$storage->getChildNodes()} ) { |
149 |
$locator->{status}->{empty} = 1; |
150 |
$logger->warning( "$log_prefix is empty"); |
151 |
#return; |
152 |
} |
153 |
} |
154 |
|
155 |
# expand logging? |
156 |
if ( $locator->{logger} && $locator->{status}->{integrity} ) { |
157 |
# expand logging (to Tangram-Database) |
158 |
# TODO: |
159 |
# - move configuration data from this code to db_config somehow |
160 |
# - do complete handling of this stuff in Data::Storage::* also |
161 |
# - just leave a simple on/off-trigger here and/or |
162 |
# - make it optionally configurable via e.g. get/set-methods |
163 |
# to satisfy "manual"-mode instead of "config"-mode |
164 |
# - (re-)add hierarchical logging (each event may have a parent) |
165 |
no strict; |
166 |
my $creator = sub { return new SystemEvent; }; |
167 |
use strict; |
168 |
$storage->addLogDispatchHandler("Tangram11", "Log::Dispatch::Tangram", $logger, $creator); |
169 |
#$storage_left->addLogDispatchHandler("Tangram11", "Log::Dispatch::Tangram", $logger); |
170 |
} |
171 |
|
172 |
$self->{storage}->{$name} = $storage; |
173 |
|
174 |
return 1; |
175 |
|
176 |
} |
177 |
|
178 |
sub initStorages { |
179 |
my $self = shift; |
180 |
foreach (keys %{$self->{locator}}) { |
181 |
$self->initStorage($_); |
182 |
} |
183 |
} |
184 |
|
185 |
sub _dbErrorHandler { |
186 |
my $message_db = shift; |
187 |
# get logger instance |
188 |
#my $logger = Log::Dispatch::Config->instance; |
189 |
#$logger->log(level => 'error', message => "Tangram DBI-Error: $message"); |
190 |
my $message_log = "DBI-Error []: $message_db"; |
191 |
print STDERR $message_log, "\n"; |
192 |
#$logger->error( $message_log ); |
193 |
$logger->log_to( name => 'file', level => 'error', message => $message_log); |
194 |
return 1; |
195 |
} |
196 |
|
197 |
1; |