File Coverage

blib/lib/Siebel/Srvrmgr/ListParser/OutputFactory.pm
Criterion Covered Total %
statement 34 34 100.0
branch 5 6 83.3
condition n/a
subroutine 9 9 100.0
pod 3 3 100.0
total 51 52 98.0


line stmt bran cond sub pod time code
1             package Siebel::Srvrmgr::ListParser::OutputFactory;
2              
3             =pod
4              
5             =head1 NAME
6              
7             Siebel::Srvrmgr::ListParser::OutputFactory - abstract factory class to create Siebel::Srvrmgr::ListParser::Output objects
8              
9             =cut
10              
11 22     22   2070 use warnings;
  22         77  
  22         814  
12 22     22   118 use strict;
  22         46  
  22         487  
13 22     22   13167 use MooseX::AbstractFactory;
  22         877537  
  22         128  
14 22     22   4327590 use Carp;
  22         60  
  22         1715  
15 22     22   12743 use Siebel::Srvrmgr::Regexes qw(CONN_GREET);
  22         213  
  22         1468  
16 22     22   28013 use Hash::Util qw(lock_hash);
  22         65641  
  22         151  
17              
18             =pod
19              
20             =head1 SYNOPSIS
21              
22             use Siebel::Srvrmgr::ListParser::OutputFactory;
23              
24             my $output = Siebel::Srvrmgr::ListParser::OutputFactory->create(
25             $type,
26             {
27             data_type => $type,
28             raw_data => \@data,
29             cmd_line => 'list something'
30             }
31             );
32              
33             if (Siebel::Srvrmgr::ListParser::OutputFactory->can_create('weirdo')) ? print "can\n" : print "cannot\n";
34              
35             =head1 DESCRIPTION
36              
37             This is an abstract factory class to create instances of subclass of L<Siebel::Srvrmgr::ListParser::Output> superclass.
38              
39             It has the mapping between the types parsed by L<Siebel::Srvrmgr::ListParser> class to the respective class of output. See
40             C<Siebel::Srvrmgr::ListParser::OutputFactory::table_mapping> for the mapping between types and classes.
41              
42             =head1 METHODS
43              
44             All methods below are class methods.
45              
46             =head2 build
47              
48             Returns the instance of the class defined by the type given as parameter. Expects two parameters: an string with the type
49             of output and an hash reference with the parameters expected by the C<new> method of L<Siebel::Srvrmgr::ListParser::Output> subclasses.
50              
51             A third, optional parameter, is required if the desired instance is a subclass of L<Siebel::Srvrmgr::ListParser::Output::Tabular>: one must
52             pass a single character as the field separator, if exists in the output to be parsed.
53              
54             Despite using L<MooseX::AbstractFactory> to implement the Abstract Factory pattern, this method must be invoked instead of C<create>
55             so the class will be able to make additional checkings necessary to define defaults for subclasses of L<Siebel::Srvrmgr::ListParser::Output::Tabular>.
56              
57             =head2 can_create
58              
59             Expects a string as the output type.
60              
61             Returns true if there is a mapping between the given type and a subclass of L<Siebel::Srvrmgr::ListParser::Output>;
62             otherwise it returns false;
63              
64             =head2 get_mapping
65              
66             Returns an hash reference with the mapping between the parsed types and subclasses of L<Siebel::Srvrmgr::ListParser::Ouput>.
67              
68             The values are array references with the partial classes names and a compiled regular expression to validate the command.
69              
70             =head1 SEE ALSO
71              
72             =over
73              
74             =item *
75              
76             L<MooseX::AbstractFactory>
77              
78             =item *
79              
80             L<Siebel::Srvrmgr::ListParser::Output>
81              
82             =item *
83              
84             L<Siebel::Srvrmgr::ListParser>
85              
86             =back
87              
88             =cut
89              
90             my %table_mapping = (
91             'list_comp' => [ 'Tabular::ListComp', qr/^list\scomps?$/ ],
92             'set_delimiter' => [ 'Set', qr/^set\sdelimiter/ ],
93             'list_params' => [
94             'Tabular::ListParams',
95             qr/list\s(advanced\s)?param(eter)?s?(\s(\w+\s)?(for\sserver\s\w+)?(\sfor\s((comp(nent)?)|named\ssubsystem|task)\s\w+)?)?/
96             ],
97             'list_comp_def' =>
98             [ 'Tabular::ListCompDef', qr/list\scomp\sdefs?(\s\w+)?/ ],
99             'greetings' => [ 'Enterprise', CONN_GREET ],
100             'list_comp_types' =>
101             [ 'Tabular::ListCompTypes', qr/list\scomp(nent)?\stypes?$/ ],
102             'load_preferences' => [ 'LoadPreferences', qr/^load\spreferences$/ ],
103             'list_tasks' => [
104             'Tabular::ListTasks',
105             qr/list\stasks(\sfor\sserver\s\w+\scomponent\sgroup?\s\w+)?/
106             ],
107             'list_servers' => [ 'Tabular::ListServers', qr/list\sserver(s)?.*/ ],
108             'list_sessions' =>
109             [ 'Tabular::ListSessions', qr/^list\s(active|hung)?\s?sessions$/ ],
110             'list_procs' => [ 'Tabular::ListProcs', qr/^list\sprocs/ ]
111             );
112              
113             lock_hash(%table_mapping);
114              
115             sub get_mapping {
116              
117 80     80 1 1852 my %copy = %table_mapping;
118              
119 80         333 return \%copy;
120              
121             }
122              
123             sub can_create {
124              
125 142     142 1 4958 my $class = shift;
126 142         219 my $type = shift;
127              
128 142         669 return ( exists( $table_mapping{$type} ) );
129              
130             }
131              
132             sub build {
133              
134 135     135 1 270 my $class = shift;
135 135         221 my $last_cmd_type = shift;
136 135         235 my $object_data = shift; # hash ref
137              
138 135 50       370 confess 'object data is required' unless ( defined($object_data) );
139              
140 135 100       791 if ( $table_mapping{$last_cmd_type}->[0] =~ /^Tabular/ ) {
141              
142 111         214 my $field_del = shift;
143              
144 111 100       285 if ( defined($field_del) ) {
145              
146 36         99 $object_data->{col_sep} = $field_del;
147 36         91 $object_data->{structure_type} = 'delimited';
148             }
149             else {
150              
151 75         226 $object_data->{structure_type} = 'fixed';
152              
153             }
154              
155             }
156              
157 135         858 $class->create( $last_cmd_type, $object_data );
158              
159             }
160              
161             implementation_class_via sub {
162              
163             my $last_cmd_type = shift;
164              
165             if ( exists( $table_mapping{$last_cmd_type} ) ) {
166              
167             return 'Siebel::Srvrmgr::ListParser::Output::'
168             . $table_mapping{$last_cmd_type}->[0];
169              
170             }
171             else {
172              
173             confess "Cannot defined a class for command '$last_cmd_type'";
174              
175             }
176              
177             };
178              
179             =pod
180              
181             =head1 AUTHOR
182              
183             Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>.
184              
185             =head1 COPYRIGHT AND LICENSE
186              
187             This software is copyright (c) 2012 of Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>.
188              
189             This file is part of Siebel Monitoring Tools.
190              
191             Siebel Monitoring Tools is free software: you can redistribute it and/or modify
192             it under the terms of the GNU General Public License as published by
193             the Free Software Foundation, either version 3 of the License, or
194             (at your option) any later version.
195              
196             Siebel Monitoring Tools is distributed in the hope that it will be useful,
197             but WITHOUT ANY WARRANTY; without even the implied warranty of
198             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
199             GNU General Public License for more details.
200              
201             You should have received a copy of the GNU General Public License
202             along with Siebel Monitoring Tools. If not, see L<http://www.gnu.org/licenses/>.
203              
204             =cut
205              
206             1;