File Coverage

blib/lib/Siebel/Srvrmgr/ListParser/OutputFactory.pm
Criterion Covered Total %
statement 30 30 100.0
branch 5 6 83.3
condition n/a
subroutine 9 9 100.0
pod 3 3 100.0
total 47 48 97.9


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