OpenInteract/ApacheStartup.pm untested

filter: Statementstmt 0.0% 0 / 111 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 51 MC/DCmcdc 0.0% 0 / 34 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 241 CC 16 SCAR 38.7
CC
16
Cov
0%
initialize
110.0
child_init
20.0
cleanup_cache
12.0
CRAP 47.8
1 package OpenInteract::ApacheStartup;
2  
3 # $Id: ApacheStartup.pm,v 1.26 2002/09/16 20:16:40 lachoy Exp $
4  
5 use strict;
6 use Apache;
7 #use Apache::DBI;
8 use File::Path;
9 use OpenInteract::DBI;
10 use OpenInteract::Startup;
11  
12 use constant DEBUG => 0;
13  
14 $OpenInteract::ApacheStartup::VERSION = sprintf("%d.%02d", q$Revision: 1.26 $ =~ /(\d+)\.(\d+)/);
15  
16 # Create a handler to put the X-Forwarded-For header into the IP
17 # address -- thanks Stas! (perl.apache.org/guide/)
18  
19 my $PROXY_SUB = <<'PROXY';
20  
21 sub OpenInteract::ProxyRemoteAddr ($) {
22 my ( $r ) = @_;
23 return unless ( ref $r );
24 if ( my ( $ip ) = $r->header_in( 'X-Forwarded-For' ) =~ /([^,\s]+)$/ ) {
25 $r->connection->remote_ip( $ip );
26 }
27 return Apache::Constants::OK;
28 }
29 PROXY
30  
31 sub initialize {
32 my ( $class, $bc_file ) = @_;
33  
34 DEBUG && _w( 1, "ApacheStartup: Reading in information for configuration: $bc_file" );
35  
36 # We read the base config in first, so we can snag the apache modules
37  
38 my $BASE_CONFIG = OpenInteract::Startup->read_base_config({ filename => $bc_file });
39 unless ( $BASE_CONFIG ) { die "Cannot create base configuration from ($bc_file)!"; }
40 DEBUG && _w( 1, "SERVER INIT: base configuration read in ok." );
41  
42 # Read in all the Apache classes -- do this separately since we need
43 # to ensure that Apache::DBI gets included before DBI
44 #
45 # Word of warning! We also need to do this before using the class
46 # 'OpenInteract::DBI', since it 'use's the DBI module itself. Keeping
47 # Apache::DBI before DBI is quite important, and if there's any
48 # confusing in the future about it we might as well put the
49 # 'PerlModule Apache::DBI' before the PerlRequire statement into the
50 # modperl httpd.conf
51  
52 my $config_dir = join( '/', $BASE_CONFIG->{website_dir},
53 $BASE_CONFIG->{config_dir} );
54 OpenInteract::Startup->require_module({ filename => "$config_dir/apache.dat" });
55 DEBUG && _w( 1, "SERVER INIT: apache modules read in ok." );
56  
57 my $C = OpenInteract::Startup->create_config({ base_config => $BASE_CONFIG });
58  
59 # Initialize the DBI drivers. Stas Beckman (stas@stason.org) wrote
60 # up a section in the mod_perl developer guide
61 # (perl.apache.org/guide/) about how this 'DBI->install_driver()' thing
62 # saves memory.
63  
64 my $db_info = $C->{db_info};
65 my %dbd_installed = ();
66 foreach my $db_key ( keys %{ $db_info } ) {
67 next if ( $dbd_installed{ $db_info->{ $db_key }{driver_name} } );
68 DBI->install_driver( $db_info->{ $db_key }{driver_name} );
69 DEBUG && _w( 1, "SERVER INIT: installed DBD driver ($db_info->{ $db_key }{driver_name}) ok." );
70 }
71  
72 # Check to see if the proxy subroutine has been loaded; if not, create it
73  
74 eval { OpenInteract->ProxyRemoteAddr() };
75 if ( $@ =~ /^Can\'t locate object method "ProxyRemoteAddr"/ ) {
76 DEBUG && _w( 1, "Creating proxy subroutine" );
77 eval $PROXY_SUB;
78 die "Cannot create proxy subroutine! $@" if ( $@ );
79 DEBUG && _w( 1, "SERVER INIT: installed proxy subroutine ok." );
80 }
81  
82 # If we're told, cleanup the Template Toolkit compile directory
83  
84 if ( $C->{template_info}{compile_cleanup} ) {
85 $class->cleanup_cache( $C->get_dir( 'cache_tt' ) );
86 }
87  
88 if ( $C->{cache_info}{data}{cleanup} ) {
89 $class->cleanup_cache( $C->get_dir( 'cache_content' ) );
90 }
91  
92 # Now copy all of each package's modules into a separate
93 # directory.
94  
95 OpenInteract::Startup->create_temp_lib( $BASE_CONFIG );
96  
97 my $OS = $^O;
98 unless ( $OS ) {
99 require Config;
100 $OS = $Config::Config{ 'osname' };
101 }
102  
103 push @OpenInteract::to_initialize, [ $BASE_CONFIG->{website_dir},
104 $BASE_CONFIG->{stash_class} ];
105  
106 if ( $OS =~ /Win32/i ) {
107 DEBUG && _w( 1, "SERVER INIT: Running initialization for Windows." );
108 child_init();
109 }
110 else {
111 unless ( $OpenInteract::is_initialized ) {
112 DEBUG && _w( 1, "SERVER INIT: Adding ChildInitHandler for initialization" );
113 Apache->push_handlers( PerlChildInitHandler => \&child_init );
114 $OpenInteract::is_initialized++;
115 }
116 }
117 }
118  
119  
120 sub child_init {
121 my ( $r ) = @_;
122 DEBUG && _w( 1, "CHILD INIT: Being executed for child ($$)" );
123  
124 # seed the random number generator per child -- note that we can
125 # probably take this out as of mod_perl >= 1.25 but since it
126 # doesn't do any harm...
127  
128 srand;
129  
130 # Whenever we run a startup.pl for a vhost we push information
131 # into the package variable @OpenInteract::to_initialize;
132  
133 foreach my $entry ( @OpenInteract::to_initialize ) {
134 my $website_dir = $entry->[0];
135 my $stash_class = $entry->[1];
136  
137 DEBUG && _w( 1, "CHILD INIT: In site root ($website_dir) with stash ($stash_class)" );
138  
139 my $BASE_CONFIG = OpenInteract::Startup->read_base_config({
140 website_dir => $website_dir });
141 unless ( $BASE_CONFIG ) {
142 die "Cannot create base configuration from ($website_dir)!";
143 }
144 DEBUG && _w( 1, "CHILD INIT: base configuration read in ok." );
145  
146  
147 # The big enchilada -- do just about everything here and get
148 # back the list of classes that need to be initialized along
149 # with the config object.
150  
151 my ( $init_class, $C ) = OpenInteract::Startup->main_initialize({
152 base_config => $BASE_CONFIG });
153 unless ( $C ) { die "No configuration object returned from initialization!\n"; }
154 DEBUG && _w( 1, "CHILD INIT: main initialization completed ok." );
155  
156 OpenInteract::Startup->require_module({ class => $C->{session_info}{class} });
157 DEBUG && _w( 1, "CHILD INIT: require the session class ok" );
158  
159 # Figure out how to do this more cleanly in the near future --
160 # maybe just do it by hand for this special class?
161  
162 push @{ $init_class }, 'OpenInteract::PackageRepository';
163  
164 # Tell OpenInteract::Request to setup aliases if it hasn't
165 # already
166  
167 my $REQUEST_CLASS = $BASE_CONFIG->{request_class};
168 $REQUEST_CLASS->setup_aliases;
169 DEBUG && _w( 1, "CHILD INIT: setup \$R aliases ok" );
170  
171 # Initialize all the SPOPS object classes
172  
173 OpenInteract::Startup->initialize_spops({ config => $C,
174 class => $init_class });
175 DEBUG && _w( 1, "CHILD INIT: initialize SPOPS classes ok" );
176  
177 # Create a list of error handlers for our website
178  
179 eval { OpenInteract::Error::Main->initialize({ config => $C }) };
180 my $err_status = ( $@ ) ? $@ : 'ok';
181 DEBUG && _w( 1, sprintf( "CHILD INIT: %-40s: %-30s","init: Error Dispatcher", $err_status ) );
182 }
183 DEBUG && _w( 1, "CHILD INIT: All items initialized. Done." );
184 }
185  
186  
187 sub cleanup_cache {
188 my ( $class, $dir ) = @_;
189 return unless ( $dir );
190 File::Path::rmtree( $dir ) if ( -d $dir );
191 File::Path::mkpath( $dir, 0, 0777 );
192 chmod( 0777, $dir ); # this may be bad, but...
193 }
194  
195  
196 sub _w {
197 return unless ( DEBUG >= shift );
198 my ( $pkg, $file, $line ) = caller;
199 my @ci = caller(1);
200 warn "$ci[3] ($line) >> ", join( ' ', @_ ), "\n";
201 }
202  
203 1;
204  
205 __END__