| 1 | package OpenInteract2::DatasourceManager; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # $Id: DatasourceManager.pm,v 1.17 2005/03/17 14:57:58 sjn Exp $ | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 87 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 87 | ▶ | use Log::Log4perl qw( get_logger ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 87 | ▶ | use OpenInteract2::Constants qw( :log ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 87 | ▶ | use OpenInteract2::Context qw( CTX ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 87 | ▶ | use OpenInteract2::Exception qw( oi_error ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 87 | $OpenInteract2::DatasourceManager::VERSION = sprintf("%d.%02d", q$Revision: 1.17 $ =~ /(\d+)\.(\d+)/); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 87 | my ( $log ); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # TODO: Right now these are all class methods, but we may change them | ||||||||||||||||||||||||||||
| 16 | # to object methods in the future... | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # Holds the connection + metadata in a hashref indexed by the | ||||||||||||||||||||||||||||
| 20 | # datasource name | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 87 | my %DS = (); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # Holds the manager class name indexed by the manager type | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 87 | my %DS_MANAGER = (); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub datasource { | ||||||||||||||||||||||||||
|
Subroutine
datasource: not called CC=11 SCAR=48.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 29 | 0 | my ( $class, $ds_name ) = @_; | |||||||||||||||||||||||||||
| 30 | 0 | ▶ | $log ||= get_logger( LOG_DS ); | ||||||||||||||||||||||||||
|
Condition0%0/3
$log ||= get_logger("OI2.DATASOURCE")
MC/DC0%0/2
$log ||= get_logger("OI2.DATASOURCE")
$log
get_logger("OI2.DATASOURCE")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.DATASOURCE")
| |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | unless ( $ds_name ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | oi_error "Cannot get a datasource without a name"; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | if ( $log->is_debug ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | my @call_info = caller(2); | |||||||||||||||||||||||||||
| 37 | 0 | ▶ | unless ( $call_info[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | @call_info = caller(1); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 0 | $log->debug( "Trying to find datasource '$ds_name' from ", | |||||||||||||||||||||||||||
| 41 | join( ' - ', $call_info[0], $call_info[3], $call_info[2] ) ); | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # If it already exists, return it | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | my $connection = $class->get_connection( $ds_name ); | |||||||||||||||||||||||||||
| 47 | 0 | ▶ | if ( $connection ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | my $rv = eval { $connection->ping }; | |||||||||||||||||||||||||||
| 49 | 0 | ▶ | if ( $@ or ! $rv ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$@ or not $rv
MC/DC0%0/2
$@ or not $rv
$@
not $rv
Truth table0%0/3
A or not(B)
A= $@B= not $rv
| |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | $rv = "undef()" unless defined $rv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 0 | $log->warn( "Cannot ping connection '$ds_name': [Error: $@] ", | |||||||||||||||||||||||||||
| 52 | "[Return: $rv]; will try to reopen..." ); | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | else { | ||||||||||||||||||||||||||||
| 55 | 0 | $log->debug( "Returning existing connection '$ds_name'" ); | |||||||||||||||||||||||||||
| 56 | 0 | return $connection; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | else { | ||||||||||||||||||||||||||||
| 60 | 0 | ▶ | $log->is_info && | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | $log->info( "Datasource '$ds_name' not connected yet" ); | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # Find out what type of datasource this is from the configuration | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | my $ds_info = $class->get_datasource_info( $ds_name ); | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | my $ds_type = $ds_info->{type}; | |||||||||||||||||||||||||||
| 69 | 0 | ▶ | unless ( $ds_type ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | $log->error( "No type defined in config for '$ds_name'" ); | |||||||||||||||||||||||||||
| 71 | 0 | oi_error "Datasource '$ds_name' must have 'type' defined"; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # Once the type is found, map it to a manager class and connect, | ||||||||||||||||||||||||||||
| 75 | # saving the connection for later use | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | my $mgr_class = $class->get_manager_class({ type => $ds_type }); | |||||||||||||||||||||||||||
| 78 | 0 | my $handle = $mgr_class->connect( $ds_name, $ds_info ); | |||||||||||||||||||||||||||
| 79 | 0 | $class->set_connection_info( $handle, $mgr_class, $ds_info ); | |||||||||||||||||||||||||||
| 80 | 0 | return $class->get_connection( $ds_name ); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | sub get_datasource_info { | ||||||||||||||||||||||||||
|
Subroutine
get_datasource_info: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 85 | 0 | my ( $class, $ds_name ) = @_; | |||||||||||||||||||||||||||
| 86 | 0 | ▶ | $log ||= get_logger( LOG_DS ); | ||||||||||||||||||||||||||
|
Condition0%0/3
$log ||= get_logger("OI2.DATASOURCE")
MC/DC0%0/2
$log ||= get_logger("OI2.DATASOURCE")
$log
get_logger("OI2.DATASOURCE")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.DATASOURCE")
| |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | unless ( $ds_name ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | $log->error( "Cannot return datasource information without a name" ); | |||||||||||||||||||||||||||
| 89 | 0 | oi_error "No datasource name specified for lookup"; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 0 | my $ds_info = CTX->lookup_datasource_config( $ds_name ); | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | unless ( ref $ds_info eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | $log->error( "Config for '$ds_name' does not exist" ); | |||||||||||||||||||||||||||
| 94 | 0 | oi_error "No information defined for datasource '$ds_name'"; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 0 | ▶ | $ds_info->{name} ||= $ds_name; | ||||||||||||||||||||||||||
|
Condition0%0/3
$ds_info->{"name"} ||= $ds_name
MC/DC0%0/2
$ds_info->{"name"} ||= $ds_name
$ds_info->{"name"}
$ds_name
Truth table0%0/3
A ||= B
A= $ds_info->{"name"}B= $ds_name
| |||||||||||||||||||||||||||||
| 97 | 0 | return $ds_info; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub disconnect { | ||||||||||||||||||||||||||
|
Subroutine
disconnect: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 101 | 0 | my ( $class, $ds_name ) = @_; | |||||||||||||||||||||||||||
| 102 | 0 | ▶ | unless ( $DS{ lc $ds_name } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | oi_error "No datasource by name '$ds_name' available"; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 0 | my $mgr_class = $class->get_manager_class({ name => $ds_name }); | |||||||||||||||||||||||||||
| 106 | 0 | my $rv = $mgr_class->disconnect( $DS{ lc $ds_name }->{connection} ); | |||||||||||||||||||||||||||
| 107 | 0 | delete $DS{ lc $ds_name }; | |||||||||||||||||||||||||||
| 108 | 0 | return $rv; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | sub shutdown { | ||||||||||||||||||||||||||
|
Subroutine
shutdown: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 113 | 0 | my ( $class ) = @_; | |||||||||||||||||||||||||||
| 114 | 0 | ▶ | $log ||= get_logger( LOG_DS ); | ||||||||||||||||||||||||||
|
Condition0%0/3
$log ||= get_logger("OI2.DATASOURCE")
MC/DC0%0/2
$log ||= get_logger("OI2.DATASOURCE")
$log
get_logger("OI2.DATASOURCE")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.DATASOURCE")
| |||||||||||||||||||||||||||||
| 115 | 0 | for ( keys %DS ) { | |||||||||||||||||||||||||||
| 116 | 0 | ▶ | $log->is_info && | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | $log->info( "Disconnecting datasource $_ from manager shutdown" ); | ||||||||||||||||||||||||||||
| 118 | 0 | $class->disconnect( $_ ); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # Make this private? | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | sub get_connection { | ||||||||||||||||||||||||||
|
Subroutine
get_connection: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 126 | 0 | my ( $class, $ds_name ) = @_; | |||||||||||||||||||||||||||
| 127 | return ( $DS{ lc $ds_name } ) | ||||||||||||||||||||||||||||
| 128 | 0 | ▶ | ? $DS{ lc $ds_name }->{connection} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # Use for connections created external to OI -- they can still be | ||||||||||||||||||||||||||||
| 133 | # parceled out by the DS manager, but they can't be disconnected, | ||||||||||||||||||||||||||||
| 134 | # pinged, etc. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | sub set_connection { | ||||||||||||||||||||||||||
|
Subroutine
set_connection: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 137 | 0 | my ( $class, $conn, $ds_name ) = @_; | |||||||||||||||||||||||||||
| 138 | 0 | return $DS{ lc $ds_name } = { connection => $conn }; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | sub set_connection_info { | ||||||||||||||||||||||||||
|
Subroutine
set_connection_info: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 143 | 0 | my ( $class, $conn, $mgr_class, $ds_info ) = @_; | |||||||||||||||||||||||||||
| 144 | 0 | my %conn_info = ( connection => $conn, | |||||||||||||||||||||||||||
| 145 | manager => $mgr_class, | ||||||||||||||||||||||||||||
| 146 | config => $ds_info ); | ||||||||||||||||||||||||||||
| 147 | 0 | $DS{ lc $ds_info->{name} } = \%conn_info; | |||||||||||||||||||||||||||
| 148 | 0 | return \%conn_info; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | ▶ | sub get_manager_class { | ||||||||||||||||||||||||||
|
Subroutine
get_manager_class: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 153 | 0 | my ( $class, $params ) = @_; | |||||||||||||||||||||||||||
| 154 | 0 | ▶ | if ( $params->{name} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 0 | ▶ | unless ( $DS{ lc $params->{name} } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | oi_error "No datasource available by name '$params->{name}'"; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | 0 | my $ds_info = $class->get_datasource_info( $params->{name} ); | |||||||||||||||||||||||||||
| 159 | 0 | $params->{type} = $ds_info->{type}; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | 0 | ▶ | if ( $params->{type} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | ▶ | if ( $DS_MANAGER{ $params->{type} } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 0 | return $DS_MANAGER{ $params->{type} }; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | my $type_info = CTX->lookup_datasource_type_config( $params->{type} ); | |||||||||||||||||||||||||||
| 167 | 0 | my $mgr_class = $type_info->{connection_manager}; | |||||||||||||||||||||||||||
| 168 | 0 | ▶ | unless ( $mgr_class ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 0 | oi_error "Cannot create connection manager of type '$params->{type}'"; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | 0 | return $class->set_manager_class( $params->{type}, $mgr_class ); | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | else { | ||||||||||||||||||||||||||||
| 174 | 0 | oi_error "Please specify either 'name' or 'type' in the parameters"; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 0 | ▶ | sub set_manager_class { | ||||||||||||||||||||||||||
|
Subroutine
set_manager_class: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 180 | 0 | my ( $class, $ds_type, $mgr_class ) = @_; | |||||||||||||||||||||||||||
| 181 | 0 | ▶ | $log ||= get_logger( LOG_DS ); | ||||||||||||||||||||||||||
|
Condition0%0/3
$log ||= get_logger("OI2.DATASOURCE")
MC/DC0%0/2
$log ||= get_logger("OI2.DATASOURCE")
$log
get_logger("OI2.DATASOURCE")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.DATASOURCE")
| |||||||||||||||||||||||||||||
| 182 | 0 | ▶ | unless ( $ds_type ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 0 | my $msg = "Cannot set manager class: no type specified"; | |||||||||||||||||||||||||||
| 184 | 0 | $log->error( $msg ); | |||||||||||||||||||||||||||
| 185 | 0 | oi_error $msg; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | 0 | ▶ | unless ( $mgr_class ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 0 | my $msg = "Cannot set manager class: none specified for [$ds_type]"; | |||||||||||||||||||||||||||
| 189 | 0 | $log->error( $msg ); | |||||||||||||||||||||||||||
| 190 | 0 | oi_error $msg; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | 0 | ▶ | if ( $DS_MANAGER{ $ds_type } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 0 | $log->warn( "Attempt to add manager class for [$ds_type] ", | |||||||||||||||||||||||||||
| 194 | "redundant; type already exists with class ", | ||||||||||||||||||||||||||||
| 195 | "[$DS_MANAGER{ $ds_type }]" ); | ||||||||||||||||||||||||||||
| 196 | 0 | return undef; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 0 | eval "require $mgr_class"; | |||||||||||||||||||||||||||
| 200 | 0 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 0 | $log->error( "Cannot add manager [$ds_type]: $@" ); | |||||||||||||||||||||||||||
| 202 | 0 | oi_error "Failed to add manager [$ds_type] [$mgr_class]: $@"; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | 0 | return $DS_MANAGER{ $ds_type } = $mgr_class; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 87 | 1; | |||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | __END__ | ||||||||||||||||||||||||||||