/[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.2 - (show annotations)
Sun Dec 1 07:08:35 2002 UTC (21 years, 7 months ago) by joko
Branch: MAIN
Changes since 1.1: +6 -2 lines
+ needs to "use Data::Storage;"

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

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