| 1 | ## ---------------------------------------------------------------------- | 
| 2 | ##  $Id: Process.pm,v 1.4 2003/12/03 00:21:38 joko Exp $ | 
| 3 | ## ---------------------------------------------------------------------- | 
| 4 | ##  $Log: Process.pm,v $ | 
| 5 | ##  Revision 1.4  2003/12/03 00:21:38  joko | 
| 6 | ##  + minor fix to '_bootDatabases': instantiate the DSC-container if no ref to it exists | 
| 7 | ## | 
| 8 | ##  Revision 1.3  2003/06/06 03:14:16  joko | 
| 9 | ##  enhanced database connection bootstrapping: | 
| 10 | ##    - boot default ones | 
| 11 | ##    - boot-on-demand | 
| 12 | ##    - protection for multiple redundant connections | 
| 13 | ## | 
| 14 | ##  Revision 1.2  2003/03/27 15:43:05  joko | 
| 15 | ##  minor fix: new comment/remark | 
| 16 | ## | 
| 17 | ##  Revision 1.1  2003/02/20 18:41:48  joko | 
| 18 | ##  + initial commit | 
| 19 | ## | 
| 20 | ## ---------------------------------------------------------------------- | 
| 21 |  | 
| 22 |  | 
| 23 | =pod | 
| 24 |  | 
| 25 | This could also be called App::Component, App::Bean or whatever. | 
| 26 | Since it conducts various things of the OEF framework, | 
| 27 | it should probably be better renamed to OEF::App::Xyz, hmmm!? | 
| 28 | OEF::App::Bean? OEF::App::Bimbo? Yakka and Bimbo... | 
| 29 |  | 
| 30 | =cut | 
| 31 |  | 
| 32 |  | 
| 33 | package App::Process; | 
| 34 |  | 
| 35 | use strict; | 
| 36 | use warnings; | 
| 37 |  | 
| 38 | use base qw( | 
| 39 | DesignPattern::Object | 
| 40 | DesignPattern::Logger | 
| 41 | ); | 
| 42 |  | 
| 43 | use Data::Dumper; | 
| 44 |  | 
| 45 | use shortcuts qw( now ); | 
| 46 |  | 
| 47 |  | 
| 48 | # ---------------------------------------------------   main   ------------ | 
| 49 | DesignPattern::Object->log_basic("Package " . __PACKAGE__ . " loaded."); | 
| 50 |  | 
| 51 |  | 
| 52 | sub constructor { | 
| 53 | my $self = shift; | 
| 54 |  | 
| 55 | $self->log( __PACKAGE__ . "->constructor", 'info'); | 
| 56 | #$self->log( "INIT", 'info'); | 
| 57 |  | 
| 58 | # trace | 
| 59 | #print Dumper($self); | 
| 60 | #exit; | 
| 61 |  | 
| 62 | # just boot database called "oefcore" | 
| 63 | # TODO: REVIEW: just boot if not booted yet..... | 
| 64 | # FIXME: this is hardcoded! | 
| 65 | #$self->{boot} = BizWorks::Boot->new( use_databases => [qw( oefcore )] ); | 
| 66 | #$self->{app}->{use_databases} =|| [qw( oefcore )]; | 
| 67 | $self->_bootDatabases(); | 
| 68 |  | 
| 69 | return; | 
| 70 |  | 
| 71 | # if no "guid" is given .... | 
| 72 | if (!$self->{guid}) { | 
| 73 |  | 
| 74 | # ... create fresh task-object, ... | 
| 75 | $self->{TASK} = Task->new( | 
| 76 | stamp => now(), | 
| 77 | #context => , | 
| 78 | ); | 
| 79 | $self->{app}->{storage}->{oefcore}->insert($self->{TASK}); | 
| 80 |  | 
| 81 | =pod | 
| 82 | # ... create fresh report-object, ... | 
| 83 | $self->{REPORT} = Report->new( | 
| 84 | stamp => now(), | 
| 85 | #context => , | 
| 86 | ); | 
| 87 | $self->{app}->{storage}->{oefcore}->insert($self->{REPORT}); | 
| 88 |  | 
| 89 | # ... link them together | 
| 90 | =cut | 
| 91 |  | 
| 92 | # ... and (finally) assign the Task's GUID to an object attribute here | 
| 93 | $self->{guid} = $self->{TASK}->{guid}; | 
| 94 |  | 
| 95 | } else { | 
| 96 |  | 
| 97 | # load context from (resumed) task | 
| 98 | # FIXME: move this to OEF::Component::Task::Persistable | 
| 99 | my $task = $self->{app}->{storage}->{oefcore}->getObjectByGuid($self->{guid}, { classname => 'Task' }); | 
| 100 | $self->{_context_raw} = $task->{context}; | 
| 101 |  | 
| 102 | } | 
| 103 |  | 
| 104 |  | 
| 105 | } | 
| 106 |  | 
| 107 |  | 
| 108 | sub _check { | 
| 109 | my $self = shift; | 
| 110 | if (!$self->getGuid()) { | 
| 111 | return 1; | 
| 112 | } | 
| 113 | } | 
| 114 |  | 
| 115 | sub perform { | 
| 116 | my $self = shift; | 
| 117 |  | 
| 118 | return if ($self->skip("task has no guid", $self->_check())); | 
| 119 |  | 
| 120 | #print Dumper($self); | 
| 121 | #exit; | 
| 122 |  | 
| 123 | $self->log("task " . $self->getGuid() . " starting."); | 
| 124 | $self->{TASK}->{begin} = now(); | 
| 125 | $self->run() if $self->can('run'); | 
| 126 | #$self->_run(); | 
| 127 | $self->{TASK}->{end} = now(); | 
| 128 | $self->log("task " . $self->getGuid() . " finished."); | 
| 129 | } | 
| 130 |  | 
| 131 | sub getGuid { | 
| 132 | my $self = shift; | 
| 133 | return $self->{guid}; | 
| 134 | } | 
| 135 |  | 
| 136 | sub _bootDatabases { | 
| 137 | my $self = shift; | 
| 138 |  | 
| 139 | # re-enabled as of 2003-05-16: Now accepts db-keys via method-args again! | 
| 140 | my $dbkeys = shift; | 
| 141 |  | 
| 142 | #my $dbkeys; | 
| 143 | my $dbcfg; | 
| 144 |  | 
| 145 | # A - Build list of db-keys to boot | 
| 146 |  | 
| 147 | # FIXME: CACHE THIS! JUST BOOT STORAGES INTO CONTAINER IF NOT ALREADY DONE! | 
| 148 | # WATCH OUT FOR GLOBAL USED RESOURCES! | 
| 149 | if (not ref $self->{DSC} eq 'Data::Storage::Container') { | 
| 150 | $self->{DSC} = DesignPattern::Object->fromPackage('Data::Storage::Container'); | 
| 151 | } | 
| 152 |  | 
| 153 | # Check if database keys were specified explicitely | 
| 154 | # as default inside the application container ... | 
| 155 | # new of 2003-05-16: Just do this if no dbkeys have been passed in via args. | 
| 156 | $dbkeys ||= $self->_get_dbkeys_app_defaults(); | 
| 157 |  | 
| 158 | # ... if yes, just boot specified databases... | 
| 159 | if ($dbkeys) { | 
| 160 |  | 
| 161 | $self->log("Using database(s): " . join(', ', @{$dbkeys}), 'info'); | 
| 162 | foreach (@$dbkeys) { | 
| 163 | $dbcfg->{$_} = $self->{app}->{config}->{databases}->{$_}; | 
| 164 | } | 
| 165 |  | 
| 166 | # ... otherwise boot all databases. | 
| 167 | } else { | 
| 168 | $self->log("Using all databases.", 'info'); | 
| 169 | $dbcfg = $self->{app}->{config}->{databases}; | 
| 170 | } | 
| 171 |  | 
| 172 |  | 
| 173 | # B - Propagate stuff to application -config and -resource slots etc. | 
| 174 | # TODO: refactor: abstract this out | 
| 175 |  | 
| 176 | # B.1 - Initialize config | 
| 177 | foreach (keys %$dbcfg) { | 
| 178 | $self->{DSC}->addConfig($_, $dbcfg->{$_}); | 
| 179 | } | 
| 180 |  | 
| 181 | # B.2 - Initialize resources | 
| 182 | $self->{DSC}->initLocators(); | 
| 183 | $self->{DSC}->initStorages(); | 
| 184 |  | 
| 185 | # B.3 - Establish symbols inside the application container | 
| 186 | # as references to the storage handle instances inside the | 
| 187 | # storage container singleton. | 
| 188 | # In other words: spread the refs | 
| 189 | # FIXME: This should be cleared up somehow!    ;-) | 
| 190 | foreach (keys %{$self->{DSC}->{storage}}) { | 
| 191 | $self->{app}->{storage}->{$_} = $self->{DSC}->{storage}->{$_}; | 
| 192 | } | 
| 193 |  | 
| 194 | } | 
| 195 |  | 
| 196 | sub _get_dbkeys_app_defaults { | 
| 197 | my $self = shift; | 
| 198 | my $dbkeys; | 
| 199 | if (my $dbkey_raw = $self->{app}->{use_databases}) { | 
| 200 | if (ref $dbkey_raw eq 'ARRAY') { | 
| 201 | $dbkeys = $dbkey_raw; | 
| 202 | } else { | 
| 203 | #$self->{app}->{instance}->_bootDatabases(); | 
| 204 | my @dbkeys = split(/,\s|,/, $dbkey_raw); | 
| 205 | #$self->_bootDatabases(\@dbkeys); | 
| 206 | $dbkeys = \@dbkeys; | 
| 207 | } | 
| 208 | } | 
| 209 | return $dbkeys; | 
| 210 | } | 
| 211 |  | 
| 212 | sub _shutdownDatabases { | 
| 213 | my $self = shift; | 
| 214 | foreach my $dbkey (keys %{$self->{app}->{storage}}) { | 
| 215 | #print "SHUTDOWN $dbkey", "\n"; | 
| 216 | #print Dumper($self->{app}->{storage}->{$dbkey}); | 
| 217 | my $handle = $self->{app}->{storage}->{$dbkey}; | 
| 218 | #print ref $handle, "\n"; | 
| 219 | #next if not $handle or not ref $handle or ref($handle) =~ m/(ARRAY|HASH)/; | 
| 220 | next if not $handle or not ref $handle or ref($handle) !~ m/(Data::Storage|DBI)/; | 
| 221 | $handle->disconnect(); | 
| 222 | } | 
| 223 | } | 
| 224 |  | 
| 225 | #=pod | 
| 226 | sub DESTROY { | 
| 227 | my $self = shift; | 
| 228 | $self->_shutdownDatabases(); | 
| 229 | } | 
| 230 | #=cut | 
| 231 |  | 
| 232 |  | 
| 233 | sub activate_resources { | 
| 234 | my $self = shift; | 
| 235 | my $args = shift; | 
| 236 | $self->_bootDatabases($args); | 
| 237 | } | 
| 238 |  | 
| 239 | sub resource_is_active { | 
| 240 | my $self = shift; | 
| 241 | my $key = shift; | 
| 242 | # FIXME: Enhance this! | 
| 243 | #print "key: $key", "\n"; | 
| 244 | #print Dumper($self->{DSC}->{storage}); | 
| 245 | return 1 if exists $self->{DSC}->{storage}->{$key}; | 
| 246 | } | 
| 247 |  | 
| 248 | 1; | 
| 249 | __END__ |