/[cvs]/nfo/perl/libs/Data/Container.pm
ViewVC logotype

Contents of /nfo/perl/libs/Data/Container.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations)
Fri Nov 29 04:55:22 2002 UTC (21 years, 5 months ago) by joko
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +5 -2 lines
FILE REMOVED
- moved to Storage/Container.pm

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

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