| 1 | # $Id: /mirror/gungho/lib/Gungho/Component/Setup.pm 41086 2008-02-01T16:49:41.285355Z lestrrat $ | ||
| 2 | # | ||
| 3 | # Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp> | ||
| 4 | # All rights reserved. | ||
| 5 | |||
| 6 | package Gungho::Component::Setup; | ||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use base qw(Gungho::Component); | ||
| 10 | use Config::Any; | ||
| 11 | |||
| 12 | my @INTERNAL_PARAMS = qw(bootstrap_finished setup_finished config); | ||
| 13 | my @CONFIGURABLE_PARAMS = qw(user_agent); | ||
| 14 | my %CONFIGURABLE_PARAM_DEFAULTS = ( | ||
| 15 | map { ($_ => 0) } @CONFIGURABLE_PARAMS | ||
| 16 | ); | ||
| 17 | |||
| 18 | __PACKAGE__->mk_classdata($_) for ( | ||
| 19 | qw(log provider handler engine is_running features observer), | ||
| 20 | @INTERNAL_PARAMS, | ||
| 21 | @CONFIGURABLE_PARAMS, | ||
| 22 | ); | ||
| 23 | |||
| 24 | sub new | ||
| 25 | { | ||
| 26 | warn "Gungho::new() has been deprecated in favor of Gungho->run()"; | ||
| 27 | my $class = shift; | ||
| 28 | $class->bootstrap(@_); | ||
| 29 | return $class; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub run | ||
| 33 | { | ||
| 34 | my $c = shift; | ||
| 35 | $c->bootstrap(@_); | ||
| 36 | $c->is_running(1); | ||
| 37 | |||
| 38 | my $banner = sprintf(<<EOBANNER, eval('$Gungho' . '::VERSION'), join(', ', map { my $name = $_; $name =~ s/Gungho::Component:://; $name } @Gungho::ISA ) ); | ||
| 39 | Starting $c | ||
| 40 | Gungho Version: %s | ||
| 41 | Components: %s | ||
| 42 | EOBANNER | ||
| 43 | $c->log->info($_) for split(/\n/, $banner); | ||
| 44 | $c->engine->run($c); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub bootstrap | ||
| 48 | { | ||
| 49 | my $c = shift; | ||
| 50 | |||
| 51 | return $c if $c->bootstrap_finished(); | ||
| 52 | |||
| 53 | my $config = $c->load_config($_[0]); | ||
| 54 | if (exists $ENV{GUNGHO_DEBUG}) { | ||
| 55 | $config->{debug} = $ENV{GUNGHO_DEBUG}; | ||
| 56 | } | ||
| 57 | |||
| 58 | $c->config($config); | ||
| 59 | |||
| 60 | for my $key (@CONFIGURABLE_PARAMS) { | ||
| 61 | $c->$key( $config->{$key} || $CONFIGURABLE_PARAM_DEFAULTS{$key} ); | ||
| 62 | } | ||
| 63 | |||
| 64 | if (! $config->{user_agent}) { | ||
| 65 | warn "No user agent specified. You should specify one today!"; | ||
| 66 | $c->user_agent( "Gungho/$Gungho::VERSION (http://code.google.com/p/gungho-crawler/wiki/Index)"); | ||
| 67 | } | ||
| 68 | $c->features({}); | ||
| 69 | |||
| 70 | my $components = $c->config->{components} || []; | ||
| 71 | push @$components, 'Core'; | ||
| 72 | Gungho->load_components(@$components); | ||
| 73 | $c->bootstrap_finished(1); | ||
| 74 | $c->setup; | ||
| 75 | |||
| 76 | return $c; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub load_config | ||
| 80 | { | ||
| 81 | my $c = shift; | ||
| 82 | my $config = shift; | ||
| 83 | |||
| 84 | if ($config && ! ref $config) { | ||
| 85 | my $filename = $config; | ||
| 86 | # In the future, we may support multiple configs, but for now | ||
| 87 | # just load a single file via Config::Any | ||
| 88 | |||
| 89 | my $list = Config::Any->load_files( { | ||
| 90 | files => [ $filename ], | ||
| 91 | use_ext => 1, | ||
| 92 | } ); | ||
| 93 | ($config) = $list->[0]->{$filename}; | ||
| 94 | } | ||
| 95 | |||
| 96 | if (! $config) { | ||
| 97 | Carp::croak("Could not load config"); | ||
| 98 | } | ||
| 99 | |||
| 100 | if (ref $config ne 'HASH') { | ||
| 101 | Carp::croak("Gungho expectes config that can be decoded to a HASH"); | ||
| 102 | } | ||
| 103 | |||
| 104 | return $config; | ||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | 1; | ||
| 109 | |||
| 110 | __END__ |