File Coverage

blib/lib/OpenPlugin/Datasource.pm
Criterion Covered Total %
statement 10 49 20.4
branch 0 18 0.0
condition n/a
subroutine 4 8 50.0
pod 0 5 0.0
total 14 80 17.5


line stmt bran cond sub pod time code
1             package OpenPlugin::Datasource;
2              
3             # $Id: Datasource.pm,v 1.20 2003/04/28 17:43:48 andreychek Exp $
4              
5 1     1   918 use strict;
  1         3  
  1         50  
6 1     1   6 use base qw( OpenPlugin::Plugin );
  1         1  
  1         106  
7 1     1   6 use Data::Dumper qw( Dumper );
  1         2  
  1         3423  
8              
9             $OpenPlugin::Datasource::VERSION = sprintf("%d.%02d", q$Revision: 1.20 $ =~ /(\d+)\.(\d+)/);
10              
11             my %DS = (); # Holds a copy of all our datasource information/handles
12             my %LOADED = (); # Holds a copy of all loaded datasource drivers
13              
14 0     0 0 0 sub type { return 'datasource' }
15 1     1 0 13 sub OP { return $_[0]->{_m}{OP} }
16              
17             sub connect {
18 0     0 0   my ( $self, $ds_name ) = @_;
19              
20             # There's nothing we can do if we weren't give a datasource to connect to
21 0 0         unless ( $ds_name ) {
22 0           $self->OP->exception->throw( "No datasource specified!");
23             }
24              
25 0           $self->OP->log->info( "Trying to find datasource ($ds_name)" );
26              
27             # If we don't alreay have a datasource handle for this datasource, create
28             # one
29 0 0         unless ( $DS{ $ds_name } ) {
30              
31 0           $self->OP->log->info( "Datasource ($ds_name) not connected yet" );
32              
33             # Get info on this particular datasource from the config
34 0           my $ds_info = $self->OP->config->{datasource}{ $ds_name };
35              
36 0 0         unless ( ref $ds_info eq 'HASH' ) {
37 0           $self->OP->exception->throw
38             ( "No information defined for datasource [$ds_name]" );
39             }
40              
41             # A 'type' is something like 'DBI' or 'LDAP'
42 0 0         unless ( $ds_info->{type} ) {
43 0           $self->OP->exception->throw
44             ( "Datasource ($ds_name) must have 'type' defined" );
45             }
46              
47 0           my $mgr_class = $self->OP->get_plugin_class( "datasource",
48             $ds_info->{'type'} );
49              
50 0           $mgr_class =~ m/^([\w:]+)$/g;
51 0           $mgr_class = $1;
52              
53 0 0         unless ( $mgr_class ) {
54 0           $self->OP->exception->throw( "Specified datasource type ",
55             "[$ds_info->{type}] for datasource ",
56             "[$ds_name] does not map to a ",
57             "known driver." );
58             }
59              
60             # Checks to see if a given driver class is loaded -- for example,
61             # OpenPlugin::Datasource::DBI or OpenPlugin::Datasource::LDAP
62 0 0         unless ( $LOADED{ $mgr_class } ) {
63              
64 0           $self->OP->log->info( "Loading driver [$mgr_class]." );
65 0           eval "require $mgr_class";
66              
67 0 0         if ( $@ ) {
68 0           $self->OP->exception->throw( "Cannot load datasource ",
69             "driver class: $@" );
70             }
71              
72 0           $self->OP->log->info( "Driver [$mgr_class] loaded ok" );
73 0           $LOADED{ $mgr_class }++;
74             }
75              
76 0           my $item = eval { $mgr_class->connect( $self->OP, $ds_name,
  0            
77             $ds_info ); };
78 0 0         if ( $@ ) {
79 0           $self->OP->exception->throw( $@ );
80             }
81              
82             # Store the info for this particular datasource for future reference
83 0           $DS{ $ds_name } = { manager => $mgr_class,
84             connection => $item,
85             config => $ds_info };
86             }
87              
88             # Return the datasource handle
89 0           return $DS{ $ds_name }->{'connection'};
90             }
91              
92             sub disconnect {
93 0     0 0   my ( $self, $ds_name ) = @_;
94              
95 0 0         unless ( $DS{ $ds_name } ) {
96 0           $self->OP->exception->throw( "No datasource by name [$ds_name] ",
97             "available" );
98             }
99              
100 0           my $mgr_class = $DS{ $ds_name }->{'manager'};
101              
102             # Pass in am OpenPlugin object and the datasource handle to the driver
103 0           return $mgr_class->disconnect( $self->OP, $DS{ $ds_name }->{connection} );
104             }
105              
106             # Disconnect all datasources
107             sub shutdown {
108 0     0 0   my ( $self ) = @_;
109 0           foreach my $ds_name ( keys %DS ) {
110 0           $self->disconnect( $ds_name );
111             }
112 0           return 1;
113             }
114              
115             1;
116              
117             __END__