/[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.5 - (show annotations)
Sun Jan 19 02:39:57 2003 UTC (21 years, 5 months ago) by joko
Branch: MAIN
Changes since 1.4: +19 -3 lines
+ moved 'deep_copy' from module 'libp' to module 'Data::Transform::Deep'
+ preserved order for hashes '$self->{config}' and '$self->{locator}' by using Tie::IxHash

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

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