/[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.7 - (show annotations)
Thu Jan 30 22:21:52 2003 UTC (21 years, 5 months ago) by joko
Branch: MAIN
Changes since 1.6: +7 -4 lines
+ changed 'connect'-behaviour back to old state
+ renamed log-output (now using 'initializing' instead of 'booting' - corresponding to 'initStorage')

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

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