/[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.4 - (show annotations)
Wed Dec 4 07:38:07 2002 UTC (21 years, 7 months ago) by jonen
Branch: MAIN
Changes since 1.3: +8 -3 lines
+ deep copy

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

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