| 1 | package Service::Engine::Data; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Carp; | ||
| 7 | use Service::Engine; | ||
| 8 | use Data::Dumper; | ||
| 9 | use Module::Runtime qw(require_module); | ||
| 10 | use threads; | ||
| 11 | use threads::shared; | ||
| 12 | |||
| 13 | our $Config; | ||
| 14 | our $Log; | ||
| 15 | our $Handles = {}; | ||
| 16 | |||
| 17 | # load desired classes | ||
| 18 | # Service::Engine::Data:* | ||
| 19 | |||
| 20 | sub new { | ||
| 21 | |||
| 22 | my ($class,$options) = @_; | ||
| 23 | |||
| 24 | # set some defaults | ||
| 25 | my $attributes = {'handles'=>{}}; | ||
| 26 | |||
| 27 | # load options | ||
| 28 | if (ref($options) eq 'HASH') { | ||
| 29 | foreach my $option (keys %{$options}) { | ||
| 30 | $attributes->{$option} = $options->{$option}; | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | # pull in some Service::Engine globals | ||
| 35 | $Config = $Service::Engine::Config; | ||
| 36 | $Log = $Service::Engine::Log; | ||
| 37 | |||
| 38 | # create our data handles | ||
| 39 | # these will be autoloaded from Service::Engine like this my $dbh = $Data->handlename(); | ||
| 40 | # this works by adding the connection handles to $attributes->{'handles'}->{handlename} | ||
| 41 | # save the handle label to $Handles->{$label} = $type; | ||
| 42 | if (ref($Config->get_config('data')) eq 'HASH') { | ||
| 43 | foreach my $service (keys $Config->get_config('data')) { | ||
| 44 | my $handles = $Config->get_config('data')->{$service}; | ||
| 45 | if (ref($handles) eq 'HASH' && keys %{$handles}) { | ||
| 46 | foreach my $handle (keys %{$handles}) { | ||
| 47 | next unless keys %{$handles->{$handle}}; | ||
| 48 | $Log->log({msg=>"loading handle $handle for $service",level=>2}); | ||
| 49 | my $package = 'Service::Engine::Data::' . ucfirst lc $service; | ||
| 50 | my $res = eval { require_module($package) }; | ||
| 51 | if ($@) { | ||
| 52 | $Log->log({msg=>"error loading $handle for $service: $@",level=>1}); | ||
| 53 | } else { | ||
| 54 | my $obj = $package->new($handles->{$handle}); | ||
| 55 | if ($obj->handle()) { | ||
| 56 | $attributes->{'handles'}->{$handle} = $obj->handle(); | ||
| 57 | $Handles->{$handle} = $service; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | } | ||
| 62 | } | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | $Log->log({msg=>Dumper($attributes->{'handles'}),level=>3}); | ||
| 67 | $Log->log({msg=>Dumper($Handles),level=>3}); | ||
| 68 | |||
| 69 | my $self = bless $attributes, $class; | ||
| 70 | |||
| 71 | return $self; | ||
| 72 | |||
| 73 | } | ||
| 74 | |||
| 75 | sub get_status { | ||
| 76 | my ($self, $handle) = @_; | ||
| 77 | |||
| 78 | my $type = $Handles->{$handle}; | ||
| 79 | my $status = ($type) ? 1 : 0; | ||
| 80 | my $result = {'status'=>$status, 'type'=>$type}; | ||
| 81 | |||
| 82 | return $result; | ||
| 83 | } | ||
| 84 | |||
| 85 | our $AUTOLOAD; | ||
| 86 | sub AUTOLOAD { | ||
| 87 | my $self = shift; | ||
| 88 | # Remove qualifier from original method name... | ||
| 89 | my $called = $AUTOLOAD =~ s/.*:://r; | ||
| 90 | # $Log->log({msg=>"AUTO loading data handle $called",level=>3}); | ||
| 91 | # Is there an attribute of that name? | ||
| 92 | croak "No such attribute: $called" | ||
| 93 | unless exists $self->{'handles'}->{$called}; | ||
| 94 | # If so, return it... | ||
| 95 | return $self->{'handles'}->{$called}; | ||
| 96 | } | ||
| 97 | |||
| 98 | 1; |