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

Contents of /nfo/perl/libs/Data/Storage/Container.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.10 - (show annotations)
Fri Jun 6 03:26:24 2003 UTC (21 years, 1 month ago) by joko
Branch: MAIN
Changes since 1.9: +13 -1 lines
+ sub existsStorage

1 ################################################
2 #
3 # $Id: Container.pm,v 1.9 2003/03/27 15:31:08 joko Exp $
4 #
5 # $Log: Container.pm,v $
6 # Revision 1.9 2003/03/27 15:31:08 joko
7 # fixes to modules regarding new namespace(s) below Data::Mungle::*
8 #
9 # Revision 1.8 2003/02/18 19:19:47 joko
10 # + modified locator handling
11 #
12 # Revision 1.7 2003/01/30 22:21:52 joko
13 # + changed 'connect'-behaviour back to old state
14 # + renamed log-output (now using 'initializing' instead of 'booting' - corresponding to 'initStorage')
15 #
16 # Revision 1.6 2003/01/30 21:44:00 joko
17 # + temporary fix: (FIXME) now connecting to storage on storagehandle-instantiation
18 #
19 # Revision 1.5 2003/01/19 02:39:57 joko
20 # + moved 'deep_copy' from module 'libp' to module 'Data.Transform.Deep'
21 # + preserved order for hashes '$self->{config}' and '$self->{locator}' by using Tie::IxHash
22 #
23 # Revision 1.4 2002/12/04 07:38:07 jonen
24 # + deep copy
25 #
26 # Revision 1.3 2002/12/01 22:18:28 joko
27 # - no interactive implicit deploy
28 # + only test integrity if storage available
29 #
30 # Revision 1.2 2002/12/01 07:08:35 joko
31 # + needs to "use Data::Storage;"
32 #
33 # Revision 1.1 2002/11/29 04:54:54 joko
34 # + initial check-in
35 #
36 # Revision 1.1 2002/11/17 06:20:41 joko
37 # + initial check in
38 #
39 #
40 ################################################
41
42
43 package Data::Storage::Container;
44
45 use strict;
46 use warnings;
47
48 # get logger instance
49 my $logger = Log::Dispatch::Config->instance;
50
51
52 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - main
53 use Tie::IxHash;
54 use Data::Dumper;
55
56 use Data::Storage;
57 use Data::Storage::Locator;
58 use Data::Mungle::Transform::Deep qw( deep_copy );
59 use Data::Mungle::Compare::Struct qw( isEmpty );
60
61 sub new {
62 my $invocant = shift;
63 my $class = ref($invocant) || $invocant;
64
65 # get constructor arguments
66 my @args = ();
67 @_ && (@args = @_);
68 $logger->debug( __PACKAGE__ . "->new( @args )" );
69
70 my $self = { @_ };
71 bless $self, $class;
72
73 # preserve order of configuration variables
74 #$self->{config} = {};
75 tie %{$self->{config}}, 'Tie::IxHash';
76 tie %{$self->{locator}}, 'Tie::IxHash';
77
78 return $self;
79 }
80
81 sub addConfig {
82 my $self = shift;
83 my $name = shift;
84 my $config = shift;
85 $self->{config}->{$name} = $config;
86 }
87
88 sub addConfigByDsn {
89 my $self = shift;
90 my $name = shift;
91 my $dsn = shift;
92
93 # HACK: assume DBI as default type for now
94 # TODO: guess type from dsn
95 my $type = "DBI";
96
97 $self->addConfig($name, { type => $type, dsn => $dsn });
98 }
99
100 sub addStorage {
101 my $self = shift;
102 my $name = shift;
103 my $storage = shift;
104 $self->{storage}->{$name} = $storage;
105 }
106
107 sub existsStorage {
108 my $self = shift;
109 my $name = shift;
110 return exists $self->{storage}->{$name};
111 }
112
113 sub initLocator {
114 my $self = shift;
115 my $name = shift;
116 my $db_config = $self->{config}->{$name};
117 $db_config ||= {};
118
119 $logger->debug( __PACKAGE__ . "->initLocator( name='$name' )" );
120
121 my $cfg_locator = {};
122
123 # set default settings, if any
124 my $default = deep_copy($self->{config}->{_default});
125 foreach (keys %$default) {
126 $cfg_locator->{$_} = $default->{$_};
127 }
128
129 # merge in specific settings
130 my $specific = deep_copy($db_config);
131 foreach (keys %$specific) {
132 $cfg_locator->{$_} = $specific->{$_};
133 }
134
135 # HACK: transfer dsn from main to dbi settings
136 $cfg_locator->{dbi}->{dsn} = $cfg_locator->{dsn} if $cfg_locator->{dsn};
137
138 # HACK: set classnames empty if none given from config
139 $cfg_locator->{classnames} = [] if !$cfg_locator->{classnames};
140
141 # HACK: set errorhandler if dbi settings are present
142 $cfg_locator->{dbi}->{HandleError} = \&_dbErrorHandler if $cfg_locator->{dbi};
143
144 # trace
145 #print "locator:", "\n";
146 #print Dumper($cfg_locator);
147
148 # check locator metadata
149 if (isEmpty($cfg_locator)) {
150 $logger->warning( __PACKAGE__ . "->initLocator( name='$name' ): Metadata was empty. Please check configuration." );
151 return;
152 }
153
154 # name it
155 $cfg_locator->{name} = $name;
156
157 # create new locator object
158 $self->{locator}->{$name} = Data::Storage::Locator->new( $cfg_locator );
159
160 }
161
162 sub initLocators {
163 my $self = shift;
164 foreach (keys %{$self->{config}}) {
165 #print $_, "\n";
166 $self->initLocator($_, $self->{config}->{$_}) if !/^_/;
167 }
168 #print "locs: ", Dumper($self->{locator});
169 }
170
171 sub getLocator {
172 my $self = shift;
173 my $name = shift;
174 return $self->{locator}->{$name};
175 }
176
177 sub initStorage {
178 my $self = shift;
179 my $name = shift;
180
181 return if $self->existsStorage($name);
182
183 my $locator = $self->getLocator($name);
184 $logger->info( __PACKAGE__ . " is initializing storage declared by locator \"$name\"" );
185
186 my $storage = Data::Storage->new($locator);
187
188 # TODO: do below (after 'testAvailability' and 'testIntegrity') again!!!
189 #$storage->connect();
190
191 my $log_prefix = __PACKAGE__ . "->initStorage: ";
192 $log_prefix .= "dsn=\"$self->{locator}->{$name}->{dsn}\"" if $self->{locator}->{$name}->{dsn};
193
194 # should we test availability of the storage before using it?
195 if ($locator->{test_availability}) {
196 $locator->{status}->{availability} = $storage->testAvailability();
197 if ( !$storage->testAvailability() ) {
198 $logger->error( "$log_prefix: testAvailability failed" );
199 }
200 }
201
202 # should we test integrity of the storage before using it?
203 if ($locator->{status}->{availability} && $locator->{test_integrity}) {
204 #return unless $storage->testIntegrity();
205 $locator->{status}->{integrity} = $storage->testIntegrity();
206 # actions if integrity fails
207 if (!$locator->{status}->{integrity}) {
208 $logger->error( "$log_prefix: testIntegrity failed" );
209 }
210 }
211
212 # try to connect...
213 # TODO:
214 # don't connect right here, do an implicit connect on (later) usage
215 # maybe set ->{meta}->{connectmethod} = "connect" here
216 #return unless $storage->connect();
217 $storage->connect() if $locator->{status}->{integrity};
218
219 # should we check emptyness?
220 if ($locator->{status}->{availability} && $locator->{test_emptyness}) {
221 #print "test empty", "\n";
222 if ( !@{$storage->getChildNodes()} ) {
223 $locator->{status}->{empty} = 1;
224 $logger->warning( "$log_prefix: Storage is empty.");
225 #return;
226 }
227 }
228
229 # expand logging?
230 if ( $locator->{status}->{integrity} && $locator->{logger} ) {
231 # expand logging (to Tangram-Database)
232 # TODO:
233 # - move configuration data from this code to db_config somehow
234 # - do complete handling of this stuff in Data::Storage::* also
235 # - just leave a simple on/off-trigger here and/or
236 # - make it optionally configurable via e.g. get/set-methods
237 # to satisfy "manual"-mode instead of "config"-mode
238 # - (re-)add hierarchical logging (each event may have a parent)
239 no strict;
240 my $creator = sub { return new SystemEvent; };
241 use strict;
242 $storage->addLogDispatchHandler("Tangram11", "Log::Dispatch::Tangram", $logger, $creator);
243 #$storage_left->addLogDispatchHandler("Tangram11", "Log::Dispatch::Tangram", $logger);
244 }
245
246 #$self->{storage}->{$name} = $storage;
247 #print "add storage: $name", "\n";
248 $self->addStorage($name, $storage);
249
250 return 1;
251
252 }
253
254 sub initStorages {
255 my $self = shift;
256 foreach (keys %{$self->{locator}}) {
257 #print $_, "\n";
258 $self->initStorage($_);
259 }
260 }
261
262 sub _dbErrorHandler {
263 my $message_db = shift;
264 # get logger instance
265 #my $logger = Log::Dispatch::Config->instance;
266 #$logger->log(level => 'error', message => "Tangram DBI-Error: $message");
267 my $message_log = "DBI-Error: $message_db";
268 print STDERR $message_log, "\n";
269 #$logger->error( $message_log );
270 #$logger->log_to( name => 'file', level => 'error', message => $message_log);
271 $logger->log_to( name => 'file', level => 'warning', message => $message_log);
272 return 1;
273 }
274
275 1;
276 __END__

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