File Coverage

blib/lib/ResourcePool/Factory.pm
Criterion Covered Total %
statement 50 50 100.0
branch 4 4 100.0
condition 2 3 66.6
subroutine 12 12 100.0
pod 5 5 100.0
total 73 74 98.6


line stmt bran cond sub pod time code
1             #*********************************************************************
2             #*** ResourcePool::Factory
3             #*** Copyright (c) 2002,2003 by Markus Winand
4             #*** $Id: Factory.pm,v 1.34 2013-04-16 10:14:44 mws Exp $
5             #*********************************************************************
6              
7             package ResourcePool::Factory;
8              
9 6     6   8878 use strict;
  6         13  
  6         237  
10 6     6   33 use vars qw($VERSION @ISA);
  6         13  
  6         332  
11 6     6   542 use ResourcePool::Singleton;
  6         12  
  6         135  
12 6     6   3034 use ResourcePool::Resource;
  6         22  
  6         167  
13 6     6   1042 use Data::Dumper;
  6         11102  
  6         3126  
14              
15             push @ISA, "ResourcePool::Singleton";
16             $VERSION = "1.0107";
17              
18             ####
19             # Some notes about the singleton behavior of this class.
20             # 1. the constructor does not return a singleton reference!
21             # 2. there is a seperate function called singelton() which will return a
22             # singleton reference
23             # this change was introduces with ResourcePool 0.9909 to allow more flexible
24             # factories (e.g. factories which do not require all parameters to their
25             # constructor) an example of such an factory is the Net::LDAP factory.
26              
27              
28             sub new($$) {
29 29     29 1 366 my $proto = shift;
30 29   66     119 my $class = ref($proto) || $proto;
31 29         46 my $key = shift;
32 29         46 my $self = {};
33 29         62 $self->{key} = $key; # this is required to make different plain Factories to be different ;)
34 29         70 $self->{VALID} = 1;
35              
36 29         53 bless($self, $class);
37              
38 29         83 return $self;
39             }
40              
41             sub create_resource($) {
42 52     52 1 72 my ($self) = @_;
43 52         92 ++$self->{Used};
44 52 100       119 if ($self->{VALID}) {
45 49         314 return ResourcePool::Resource->new($self->{key});
46             } else {
47 3         10 return undef;
48             }
49             }
50              
51             sub info($) {
52 66     66 1 85 my ($self) = @_;
53 66         262 return $self->{key};
54             }
55              
56             sub singleton($) {
57 55     55 1 117 my ($self) = @_;
58 55         216 my $key = $self->mk_singleton_key();
59 55         3201 my $singleton = $self->SUPER::new($key); # parent is Singleton
60 55 100       187 if (!$singleton->{initialized}) {
61 20         31 %{$singleton} = %{$self};
  20         63  
  20         49  
62 20         50 $singleton->{initialized} = 1;
63             }
64 55         535 return $singleton;
65             }
66              
67             sub mk_singleton_key($) {
68 55     55 1 425 my $d = Data::Dumper->new([$_[0]]);
69 55         2064 $d->Indent(0);
70 55         669 $d->Terse(1);
71              
72             # Required to get stable results in presence of sort key randomization
73             # See https://rt.cpan.org/Public/Bug/Display.html?id=84265
74 55         372 $d->Sortkeys(1);
75              
76 55         466 return $d->Dump();
77             }
78              
79              
80             sub _my_very_private_and_secret_test_hook($) {
81 17     17   4812 my ($not_self) = @_;
82 17         46 my $self = $not_self->singleton();
83 17         120 return $self->{Used};
84             }
85              
86             sub _my_very_private_and_secret_test_hook2($$) {
87 6     6   3004204 my ($not_self, $mode) = @_;
88 6         36 my $self = $not_self->singleton();
89 6         106 $self->{VALID} = $mode;
90             }
91              
92             1;