| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | #=============================================================================== | 
| 2 |  |  |  |  |  |  | # | 
| 3 |  |  |  |  |  |  | #         FILE:  Abstract.pm | 
| 4 |  |  |  |  |  |  | # | 
| 5 |  |  |  |  |  |  | #  DESCRIPTION:  Abstract Class for other NetSDS code | 
| 6 |  |  |  |  |  |  | # | 
| 7 |  |  |  |  |  |  | #       AUTHOR:  Michael Bochkaryov (Rattler), | 
| 8 |  |  |  |  |  |  | #      COMPANY:  Net.Style | 
| 9 |  |  |  |  |  |  | #      CREATED:  24.04.2008 11:42:42 EEST | 
| 10 |  |  |  |  |  |  | #=============================================================================== | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | =head1 NAME | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | NetSDS::Class::Abstract - superclass for all NetSDS APIs | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | package MyClass; | 
| 19 |  |  |  |  |  |  | use base 'NetSDS::Class::Abstract'; | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | __PACKAGE__->mk_accessors(qw/my_field/); | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub error_sub { | 
| 24 |  |  |  |  |  |  | my ($self) = @_; | 
| 25 |  |  |  |  |  |  | if (!$self->my_field) { | 
| 26 |  |  |  |  |  |  | return $self->error("No my_field defined"); | 
| 27 |  |  |  |  |  |  | } | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | 1; | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | C is a superclass for all other NetSDS classes, containing the following functionality: | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | =over | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | =item * common class constructor | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | =item * safe modules inclusion | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | =item * class and objects accessors | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | =item * logging | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | =item * error handling; | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | =back | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | All other class/object APIs should inherit this class to use it's functionality in standard way. | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | =cut | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | package NetSDS::Class::Abstract; | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 3 |  |  | 3 |  | 30205 | use 5.8.0; | 
|  | 3 |  |  |  |  | 11 |  | 
|  | 3 |  |  |  |  | 133 |  | 
| 57 | 3 |  |  | 3 |  | 17 | use strict; | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 95 |  | 
| 58 | 3 |  |  | 3 |  | 18 | use warnings; | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 115 |  | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 3 |  |  |  |  | 2663 | use base qw( | 
| 61 |  |  |  |  |  |  | Class::Accessor::Class | 
| 62 | 3 |  |  | 3 |  | 17 | ); | 
|  | 3 |  |  |  |  | 9 |  | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | # Error handling class variables | 
| 65 |  |  |  |  |  |  | our $_ERRSTR;     # error string | 
| 66 |  |  |  |  |  |  | our $_ERRCODE;    # error code | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 3 |  |  | 3 |  | 17723 | use Data::Structure::Util;    # unblessing objects | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | use version; our $VERSION = '1.301'; | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | #*********************************************************************** | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | =head1 CONSTRUCTOR, INITIALIZATION, APPLICATION | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | =over | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | =item B - common constructor | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | C method implements common constructor for NetSDS classes. | 
| 81 |  |  |  |  |  |  | Constructor may be overwriten in inherited classes and usually | 
| 82 |  |  |  |  |  |  | this happens to implement module specific functionality. | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | Constructor requres parameters as hash that are set as object properties. | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | my $object = NetSDS::SomeClass->new( | 
| 87 |  |  |  |  |  |  | foo => 'abc', | 
| 88 |  |  |  |  |  |  | bar => 'def', | 
| 89 |  |  |  |  |  |  | ); | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | =cut | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | #----------------------------------------------------------------------- | 
| 94 |  |  |  |  |  |  | sub new { | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | my ( $proto, %params ) = @_; | 
| 97 |  |  |  |  |  |  | my $class = ref($proto) || $proto; | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | my $self = \%params; | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | bless( $self, $class ); | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | return $self; | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | } | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | #*********************************************************************** | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | =item B - class properties accessor | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | See L for details. | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | __PACKAGE__->mk_class_accessors('foo', 'bar'); | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | =item B - object properties accessors | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | See L for details. | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | $self->mk_accessors('foo', 'bar'); | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | Other C methods available as well. | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | =cut | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | #----------------------------------------------------------------------- | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | #*********************************************************************** | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | =item B - load modules on demand | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | C provides safe on demand modules loader. | 
| 132 |  |  |  |  |  |  | It requires list of modules names as parameters | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | Return 1 in case of success or C if faied. Error messages in case | 
| 135 |  |  |  |  |  |  | of failure are available using C call. | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | Example: | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | # Load modules for daemonization | 
| 140 |  |  |  |  |  |  | if ($daemon_mode) { | 
| 141 |  |  |  |  |  |  | $self->use_modules("Proc::Daemon", "Proc::PID::File"); | 
| 142 |  |  |  |  |  |  | } | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | =cut | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | #----------------------------------------------------------------------- | 
| 147 |  |  |  |  |  |  | sub use_modules { | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | my $self = shift(@_); | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | foreach my $mod (@_) { | 
| 152 |  |  |  |  |  |  | eval "use $mod;"; | 
| 153 |  |  |  |  |  |  | if ($@) { | 
| 154 |  |  |  |  |  |  | return $self->error($@); | 
| 155 |  |  |  |  |  |  | } | 
| 156 |  |  |  |  |  |  | } | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | return 1; | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | } | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | #*********************************************************************** | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | =item B - return unblessed object | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | Return unblessed data structure of object that may be used when some | 
| 167 |  |  |  |  |  |  | code requires non blessed structures (like JSON serialization). | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | Example: | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | my $var = $obj->unbless(); | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  | =cut | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | #----------------------------------------------------------------------- | 
| 176 |  |  |  |  |  |  | sub unbless { | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  | my ($self) = @_; | 
| 179 |  |  |  |  |  |  | return Data::Structure::Util::unbless($self); | 
| 180 |  |  |  |  |  |  | } | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | #*********************************************************************** | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  | =back | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | =head1 LOGGING | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | =over | 
| 189 |  |  |  |  |  |  |  | 
| 190 |  |  |  |  |  |  | =item B - get/set logging handler | 
| 191 |  |  |  |  |  |  |  | 
| 192 |  |  |  |  |  |  | C property is an object that should provide functionality | 
| 193 |  |  |  |  |  |  | handling log messaging. Usually it's object of L | 
| 194 |  |  |  |  |  |  | class or C. However it may another object implementing | 
| 195 |  |  |  |  |  |  | non-standard features like sending log to e-mail or to DBMS. | 
| 196 |  |  |  |  |  |  |  | 
| 197 |  |  |  |  |  |  | Example: | 
| 198 |  |  |  |  |  |  |  | 
| 199 |  |  |  |  |  |  | # Set logger and send log message | 
| 200 |  |  |  |  |  |  | $obj->logger(NetSDS::Logger->new()); | 
| 201 |  |  |  |  |  |  | $obj->log("info", "Logger connected"); | 
| 202 |  |  |  |  |  |  |  | 
| 203 |  |  |  |  |  |  | =cut | 
| 204 |  |  |  |  |  |  |  | 
| 205 |  |  |  |  |  |  | #----------------------------------------------------------------------- | 
| 206 |  |  |  |  |  |  |  | 
| 207 |  |  |  |  |  |  | __PACKAGE__->mk_accessors('logger');    # Logger | 
| 208 |  |  |  |  |  |  |  | 
| 209 |  |  |  |  |  |  | #*********************************************************************** | 
| 210 |  |  |  |  |  |  |  | 
| 211 |  |  |  |  |  |  | =item B - write log message | 
| 212 |  |  |  |  |  |  |  | 
| 213 |  |  |  |  |  |  | Paramters: log level, log message | 
| 214 |  |  |  |  |  |  |  | 
| 215 |  |  |  |  |  |  | $obj->log("info", "We still alive"); | 
| 216 |  |  |  |  |  |  |  | 
| 217 |  |  |  |  |  |  | =cut | 
| 218 |  |  |  |  |  |  |  | 
| 219 |  |  |  |  |  |  | #----------------------------------------------------------------------- | 
| 220 |  |  |  |  |  |  |  | 
| 221 |  |  |  |  |  |  | sub log { | 
| 222 |  |  |  |  |  |  |  | 
| 223 |  |  |  |  |  |  | my ( $self, $level, $msg ) = @_; | 
| 224 |  |  |  |  |  |  |  | 
| 225 |  |  |  |  |  |  | # Logger expected to provide "log()" method | 
| 226 |  |  |  |  |  |  | if ( $self->logger() and $self->logger()->can('log') ) { | 
| 227 |  |  |  |  |  |  | $self->logger->log( $level, $msg ); | 
| 228 |  |  |  |  |  |  | } else { | 
| 229 |  |  |  |  |  |  | warn "[$level] $msg\n"; | 
| 230 |  |  |  |  |  |  | } | 
| 231 |  |  |  |  |  |  | } | 
| 232 |  |  |  |  |  |  |  | 
| 233 |  |  |  |  |  |  | #*********************************************************************** | 
| 234 |  |  |  |  |  |  |  | 
| 235 |  |  |  |  |  |  | =back | 
| 236 |  |  |  |  |  |  |  | 
| 237 |  |  |  |  |  |  | =head1 ERROR HANDLING | 
| 238 |  |  |  |  |  |  |  | 
| 239 |  |  |  |  |  |  | =over | 
| 240 |  |  |  |  |  |  |  | 
| 241 |  |  |  |  |  |  | =item B - set error message and code | 
| 242 |  |  |  |  |  |  |  | 
| 243 |  |  |  |  |  |  | C method set error message and optional error code. | 
| 244 |  |  |  |  |  |  | It can be invoked in both class and object contexts. | 
| 245 |  |  |  |  |  |  |  | 
| 246 |  |  |  |  |  |  | Example 1: set class error | 
| 247 |  |  |  |  |  |  |  | 
| 248 |  |  |  |  |  |  | NetSDS::Foo->error("Mistake found"); | 
| 249 |  |  |  |  |  |  |  | 
| 250 |  |  |  |  |  |  | Example 2: set object error with code | 
| 251 |  |  |  |  |  |  |  | 
| 252 |  |  |  |  |  |  | $obj->error("Can't launch rocket", BUG_STUPID); | 
| 253 |  |  |  |  |  |  |  | 
| 254 |  |  |  |  |  |  | =cut | 
| 255 |  |  |  |  |  |  |  | 
| 256 |  |  |  |  |  |  | #----------------------------------------------------------------------- | 
| 257 |  |  |  |  |  |  |  | 
| 258 |  |  |  |  |  |  | sub error { | 
| 259 |  |  |  |  |  |  |  | 
| 260 |  |  |  |  |  |  | my ( $self, $msg, $code ) = @_; | 
| 261 |  |  |  |  |  |  |  | 
| 262 |  |  |  |  |  |  | $msg  ||= '';    # error message | 
| 263 |  |  |  |  |  |  | $code ||= '';    # error code | 
| 264 |  |  |  |  |  |  |  | 
| 265 |  |  |  |  |  |  | if ( ref($self) ) { | 
| 266 |  |  |  |  |  |  | $self->{_errstr}  = $msg; | 
| 267 |  |  |  |  |  |  | $self->{_errcode} = $code; | 
| 268 |  |  |  |  |  |  | } else { | 
| 269 |  |  |  |  |  |  | $_ERRSTR  = $msg; | 
| 270 |  |  |  |  |  |  | $_ERRCODE = $code; | 
| 271 |  |  |  |  |  |  | } | 
| 272 |  |  |  |  |  |  |  | 
| 273 |  |  |  |  |  |  | return undef; | 
| 274 |  |  |  |  |  |  | } | 
| 275 |  |  |  |  |  |  |  | 
| 276 |  |  |  |  |  |  | #*********************************************************************** | 
| 277 |  |  |  |  |  |  |  | 
| 278 |  |  |  |  |  |  | =item B - retrieve error message | 
| 279 |  |  |  |  |  |  |  | 
| 280 |  |  |  |  |  |  | C method returns error string in both object and class contexts. | 
| 281 |  |  |  |  |  |  |  | 
| 282 |  |  |  |  |  |  | Example: | 
| 283 |  |  |  |  |  |  |  | 
| 284 |  |  |  |  |  |  | warn "We have an error: " . $obj->errstr; | 
| 285 |  |  |  |  |  |  |  | 
| 286 |  |  |  |  |  |  | =cut | 
| 287 |  |  |  |  |  |  |  | 
| 288 |  |  |  |  |  |  | #----------------------------------------------------------------------- | 
| 289 |  |  |  |  |  |  |  | 
| 290 |  |  |  |  |  |  | sub errstr { | 
| 291 |  |  |  |  |  |  |  | 
| 292 |  |  |  |  |  |  | my $self = shift; | 
| 293 |  |  |  |  |  |  | return ref($self) ? $self->{_errstr} : $_ERRSTR; | 
| 294 |  |  |  |  |  |  |  | 
| 295 |  |  |  |  |  |  | } | 
| 296 |  |  |  |  |  |  |  | 
| 297 |  |  |  |  |  |  | #*********************************************************************** | 
| 298 |  |  |  |  |  |  |  | 
| 299 |  |  |  |  |  |  | =item B - retrieve error code | 
| 300 |  |  |  |  |  |  |  | 
| 301 |  |  |  |  |  |  | C method returns error code in both object and class contexts. | 
| 302 |  |  |  |  |  |  |  | 
| 303 |  |  |  |  |  |  | Example: | 
| 304 |  |  |  |  |  |  |  | 
| 305 |  |  |  |  |  |  | if ($obj->errcode == 42) { | 
| 306 |  |  |  |  |  |  | print "Epic fail! We've found an answer!"; | 
| 307 |  |  |  |  |  |  | } | 
| 308 |  |  |  |  |  |  |  | 
| 309 |  |  |  |  |  |  | =cut | 
| 310 |  |  |  |  |  |  |  | 
| 311 |  |  |  |  |  |  | #----------------------------------------------------------------------- | 
| 312 |  |  |  |  |  |  |  | 
| 313 |  |  |  |  |  |  | sub errcode { | 
| 314 |  |  |  |  |  |  |  | 
| 315 |  |  |  |  |  |  | my $self = shift; | 
| 316 |  |  |  |  |  |  | return ref($self) ? $self->{_errcode} : $_ERRCODE; | 
| 317 |  |  |  |  |  |  |  | 
| 318 |  |  |  |  |  |  | } | 
| 319 |  |  |  |  |  |  |  | 
| 320 |  |  |  |  |  |  | 1; | 
| 321 |  |  |  |  |  |  |  | 
| 322 |  |  |  |  |  |  | __END__ |