/[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.6 - (show annotations)
Thu Jan 30 21:44:00 2003 UTC (21 years, 5 months ago) by joko
Branch: MAIN
Changes since 1.5: +9 -2 lines
+ temporary fix: (FIXME) now connecting to storage on storagehandle-instantiation

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

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