File Coverage

blib/lib/Hardware/UPS/Perl/Driver.pm
Criterion Covered Total %
statement 58 98 59.1
branch 11 38 28.9
condition 1 9 11.1
subroutine 11 15 73.3
pod 8 8 100.0
total 89 168 52.9


line stmt bran cond sub pod time code
1             package Hardware::UPS::Perl::Driver;
2              
3             #==============================================================================
4             # package description:
5             #==============================================================================
6             # This package supplies a set of methods to load an UPS driver. For a detailed
7             # description see the pod documentation included at the end of this file.
8             #
9             # List of public methods:
10             # -----------------------
11             # new - initializing a Hardware::UPS::Perl::Driver object
12             # setLogger - setting the current logger
13             # getLogger - getting the current logger
14             # setDriverOptions - setting the UPS driver options
15             # getDriverOptions - getting the current UPS driver options
16             # setDriverHandle - setting the UPS driver handle
17             # getDriverHandle - getting the current UPS driver handle
18             # getErrorMessage - getting internal error messages
19             #
20             #==============================================================================
21              
22             #==============================================================================
23             # Copyright:
24             #==============================================================================
25             # Copyright (c) 2007 Christian Reile, . All
26             # rights reserved. This program is free software; you can redistribute it
27             # and/or modify it under the same terms as Perl itself.
28             #==============================================================================
29              
30             #==============================================================================
31             # Entries for Revision Control:
32             #==============================================================================
33             # Revision : $Revision: 1.8 $
34             # Author : $Author: creile $
35             # Last Modified On: $Date: 2007/04/17 19:45:29 $
36             # Status : $State: Exp $
37             #------------------------------------------------------------------------------
38             # Modifications :
39             #------------------------------------------------------------------------------
40             #
41             # $Log: Driver.pm,v $
42             # Revision 1.8 2007/04/17 19:45:29 creile
43             # missing import Hardware::UPS::Perl::Logging added.
44             #
45             # Revision 1.7 2007/04/14 09:37:26 creile
46             # documentation update.
47             #
48             # Revision 1.6 2007/04/07 15:14:21 creile
49             # adaptations to "best practices" style;
50             # update of documentation.
51             #
52             # Revision 1.5 2007/03/13 17:19:06 creile
53             # options as anonymous hashes.
54             #
55             # Revision 1.4 2007/03/03 21:20:23 creile
56             # new variable $UPSERROR added;
57             # "return undef" replaced by "return";
58             # adaptations to new Constants.pm.
59             #
60             # Revision 1.3 2007/02/25 17:04:56 creile
61             # methods setDriver() and getDriver renamed to
62             # setDriverHandle() and getDriverHandle();
63             # option handling redesigned.
64             #
65             # Revision 1.2 2007/02/05 20:35:09 creile
66             # pod documentation revised.
67             #
68             # Revision 1.1 2007/02/04 18:23:50 creile
69             # initial revision.
70             #
71             #
72             #==============================================================================
73              
74             #==============================================================================
75             # module preamble:
76             #==============================================================================
77              
78 1     1   6 use strict;
  1         2  
  1         50  
79              
80             BEGIN {
81            
82 1     1   5 use vars qw($VERSION @ISA);
  1         17  
  1         100  
83              
84 1     1   12 $VERSION = sprintf( "%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/ );
85              
86 1         35 @ISA = qw();
87              
88             }
89              
90             #==============================================================================
91             # end of module preamble
92             #==============================================================================
93              
94             #==============================================================================
95             # packages required:
96             #------------------------------------------------------------------------------
97             #
98             # Hardware::UPS::Perl::General - importing Hardware::UPS::Perl variables
99             # and functions for scripts
100             # Hardware::UPS::Perl::Logging - importing Hardware::UPS::Perl methods
101             # dealing with logfiles
102             # Hardware::UPS::Perl::Utils - importing Hardware::UPS::Perl utility
103             # functions for packages
104             #
105             #==============================================================================
106              
107 1         159 use Hardware::UPS::Perl::General qw(
108             $UPSERROR
109 1     1   6 );
  1         2  
110 1     1   1000 use Hardware::UPS::Perl::Logging;
  1         5  
  1         47  
111 1         1176 use Hardware::UPS::Perl::Utils qw(
112             error
113 1     1   8 );
  1         3  
114              
115             #==============================================================================
116             # public methods:
117             #==============================================================================
118              
119             sub new {
120              
121             # public method to construct a driver object
122             #
123             # parameters: $class (input) - class
124             # $options (input) - anonymous hash; options
125             #
126             # The following option keys are recognized:
127             #
128             # Driver ($) - string; the driver to load; optional
129             # Options ($) - anonymous hash; the options of the driver to load;
130             # optional
131             # Logger ($) - Hardware::UPS::Perl::Logging object; the logger to use;
132             # optional
133              
134             # input as hidden local variables
135 1     1 1 492 my $class = shift;
136 1 50       5 my $options = @_ ? shift : {};
137              
138             # hidden local variables
139 1         3 my $self = {}; # referent to be blessed
140 1         2 my $refType; # a reference type
141             my $option; # an option
142 0         0 my $logger; # the logger object
143 0         0 my $driverName; # the driver name
144 0         0 my $driverOptions; # the driver options
145              
146             # blessing driver object
147 1         4 bless $self, $class;
148              
149             # checking options
150 1         4 $refType = ref($options);
151 1 50       4 if ($refType ne 'HASH') {
152 0         0 error("not a hash reference -- <$refType>");
153             }
154              
155             # the logger; if we don't have one, we have to create our own with output
156             # on STDERR
157 1         4 $logger = delete $options->{Logger};
158              
159 1 50       5 if (!defined $logger) {
160 0 0       0 $logger = Hardware::UPS::Perl::Logging->new()
161             or return;
162             }
163              
164             # the driver name
165 1         2 $driverName = delete $options->{Driver};
166              
167             # the driver options
168 1         3 $driverOptions = delete $options->{Options};
169              
170 1 50       3 if (defined $driverOptions) {
171 0         0 $refType = ref($driverOptions);
172 0 0       0 if ($refType ne 'HASH') {
173 0         0 error("no hash reference -- <$refType>");
174             }
175             } else {
176 1         3 $driverOptions = {};
177             }
178              
179             # checking for misspelled options
180 1         2 foreach $option (keys %{$options}) {
  1         5  
181 0         0 error("option unknown -- $option");
182             }
183              
184             # initializing
185             #
186             # the error message
187 1         7 $self->{errorMessage} = q{};
188              
189             # the logger
190 1         7 $self->setLogger($logger);
191              
192             # setting the driver
193 1         4 $self->setDriverOptions($driverOptions);
194              
195 1 50       4 if (defined $driverName) {
196             $self->setDriverHandle($driverName)
197 0 0       0 or do {
198 0         0 $UPSERROR = $self->getErrorMessage();
199 0         0 return;
200             };
201             }
202              
203             # returning blessed driver object
204 1         4 return $self;
205              
206             } # end of public method "new"
207              
208 0     0   0 sub DESTROY {
209              
210             # the destructor will do nothing, actually
211              
212             } # end of the destructor
213              
214             sub getErrorMessage {
215              
216             # public method to get the current error message
217             #
218             # parameters: $self (input) - referent to a driver object
219              
220             # input as hidden local variable
221 0     0 1 0 my $self = shift;
222              
223             # getting the error message
224 0 0       0 if (exists $self->{errorMessage}) {
225 0         0 return $self->{errorMessage};
226             }
227             else {
228 0         0 return;
229             }
230              
231             } # end of public method "getErrorMessage"
232              
233             sub getLogger {
234              
235             # public method to get the logger
236             #
237             # parameters: $self (input) - referent to a driver object
238              
239             # input as hidden local variable
240 1     1 1 3 my $self = shift;
241              
242             # getting logger
243 1 50       5 if (exists $self->{logger}) {
244 0         0 return $self->{logger};
245             }
246             else {
247 1         3 return;
248             }
249              
250             } # end of public method "getLogger"
251              
252             sub setLogger {
253              
254             # public method to set the logger
255             #
256             # parameters: $self (input) - referent to a driver object
257             # $logger (input) - the logging object
258              
259             # input as hidden local variables
260 1     1 1 3 my $self = shift;
261              
262 1 50       4 1 == @_ or error("usage: setLogger(LOGGER)");
263 1         3 my $logger = shift;
264              
265 1 50       5 if (defined $logger) {
266 1         22 my $loggerRefType = ref($logger);
267 1 50       9 if ($loggerRefType ne 'Hardware::UPS::Perl::Logging') {
268 0         0 error("no logger -- <$loggerRefType>");
269             }
270             }
271              
272             # getting old logger
273 1         5 my $oldLogger = $self->getLogger();
274              
275             # setting the logger
276 1         3 $self->{logger} = $logger;
277              
278             # returning old logger
279 1         2 return $oldLogger;
280              
281             } # end of public method "setLogger"
282              
283             sub getDriverOptions {
284              
285             # public method to get the options of the driver
286             #
287             # parameters: $self (input) - referent to a driver object
288              
289             # input as hidden local variable
290 1     1 1 2 my $self = shift;
291              
292             # getting driver options
293 1 50       5 if (exists $self->{options}) {
294 0         0 return $self->{options};
295             } else {
296 1         3 return;
297             }
298              
299             } # end of public method "getDriverOptions"
300              
301             sub setDriverOptions {
302              
303             # public method to set the options for the UPS driver to load
304             #
305             # parameters: $self (input) - referent to a driver object
306             # $options (input) - anonymous array; the driver options
307              
308             # input as hidden local variables
309 1     1 1 3 my $self = shift;
310              
311 1 50 33     25 ( (1 == @_) and (ref($_[0]) eq 'HASH'))
312             or error("usage: setDriverOptions(\%options)");
313              
314 1         2 my $options = shift;
315              
316             # getting old driver options
317 1         5 my $oldDriverOptions = $self->getDriverOptions();
318              
319             # setting driver options
320 1         2 $self->{options} = $options;
321              
322             # returning old driver options
323 1         4 return $oldDriverOptions;
324              
325             } # end of public method "setDriverOptions"
326              
327             sub getDriverHandle {
328              
329             # public method to get the UPS driver handle
330             #
331             # parameters: $self (input) - referent to a driver object
332              
333             # input as hidden local variable
334 0     0 1   my $self = shift;
335              
336             # getting driver handle
337 0 0         if (exists $self->{driver}) {
338 0           return $self->{driver};
339             }
340             else {
341 0           return;
342             }
343              
344             } # end of public method "getDriverHandle"
345            
346             sub setDriverHandle {
347              
348             # public method to load an UPS driver handle
349             #
350             # parameters: $self (input) - referent to a driver object
351             # $driver (input) - the driver to load
352              
353             # input as hidden local variables
354 0     0 1   my $self = shift;
355              
356 0 0         (1 == @_) or error("usage: setDriverHandle(driver)");
357 0           my $driver = shift;
358              
359             # hidden local variables
360 0           my $driverClass; # the driver class
361             my $driverHandle; # the driver handle
362              
363             # getting driver class, making allowance for case-insensitivity
364 0           $driverClass = "Hardware::UPS::Perl::Driver::".ucfirst(lc($driver));
365 0           eval qq{
366             use $driverClass; # load the driver
367             };
368              
369             # checking eval error
370 0 0         if ($@) {
371 0           $self->{errorMessage} = "eval failed -- $@";
372 0           return 0;
373             }
374              
375             # setting up driver object
376 0           $driverHandle = eval {
377 0           $driverClass->new($self->getDriverOptions())
378             };
379              
380 0 0 0       if (!$driverHandle or !ref($driverHandle) or $@) {
      0        
381 0           $self->{errorMessage} = "$driverClass initialisation failed -- $@";
382 0           return 0;
383             }
384              
385 0           $self->{driver} = $driverHandle;
386              
387 0           return 1;
388              
389             } # end of public method "setDriverHandle"
390              
391             #==============================================================================
392             # package return:
393             #==============================================================================
394             1;
395              
396             __END__