| 1 | package SADI::Simple::Base; | ||
| 2 | { | ||
| 3 | $SADI::Simple::Base::VERSION = '0.15'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | |||
| 8 | use HTTP::Date; | ||
| 9 | use URI; | ||
| 10 | use Log::Log4perl qw(:no_extra_logdie_message); | ||
| 11 | use File::Spec; | ||
| 12 | use File::Spec::Functions; | ||
| 13 | |||
| 14 | our $AUTOLOAD; | ||
| 15 | |||
| 16 | # names of attribute's types | ||
| 17 | use constant STRING => 'string'; | ||
| 18 | use constant INTEGER => 'integer'; | ||
| 19 | use constant FLOAT => 'float'; | ||
| 20 | use constant BOOLEAN => 'boolean'; | ||
| 21 | use constant DATETIME => 'datetime'; | ||
| 22 | |||
| 23 | # names of attribute's properties | ||
| 24 | use constant TYPE => 'type'; | ||
| 25 | use constant POST => 'post'; | ||
| 26 | use constant ISARRAY => 'is_array'; | ||
| 27 | use constant READONLY => 'readonly'; | ||
| 28 | |||
| 29 | #----------------------------------------------------------------- | ||
| 30 | # These methods are called by set/get methods of the sub-classes. If | ||
| 31 | # it comes here, it indicates that an attribute being get/set does not | ||
| 32 | # exist. | ||
| 33 | #----------------------------------------------------------------- | ||
| 34 | { | ||
| 35 | my %_allowed = (); | ||
| 36 | |||
| 37 | sub _accessible { | ||
| 38 | my ( $self, $attr ) = @_; | ||
| 39 | exists $_allowed{$attr}; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub _attr_prop { | ||
| 43 | my ( $self, $attr_name, $prop_name ) = @_; | ||
| 44 | my $attr = $_allowed{$attr_name}; | ||
| 45 | return ref($attr) ? $attr->{$prop_name} : $attr if $attr; | ||
| 46 | return undef; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | #----------------------------------------------------------------- | ||
| 51 | # new | ||
| 52 | #----------------------------------------------------------------- | ||
| 53 | sub new { | ||
| 54 | my ( $class, @args ) = @_; | ||
| 55 | |||
| 56 | # $LOG->debug ("NEW: $class - " . join (", ", @args)) if $LOG->is_debug; | ||
| 57 | # create an object | ||
| 58 | my $self = bless {}, ref($class) || $class; | ||
| 59 | |||
| 60 | # initialize the object | ||
| 61 | $self->init(); | ||
| 62 | |||
| 63 | # set all @args into this object with 'set' values | ||
| 64 | my (%args) = ( @args == 1 ? ( value => $args[0] ) : @args ); | ||
| 65 | foreach my $key ( keys %args ) { | ||
| 66 | no strict 'refs'; | ||
| 67 | $self->$key( $args{$key} ); | ||
| 68 | } | ||
| 69 | |||
| 70 | # done | ||
| 71 | return $self; | ||
| 72 | } | ||
| 73 | |||
| 74 | #----------------------------------------------------------------- | ||
| 75 | # init | ||
| 76 | #----------------------------------------------------------------- | ||
| 77 | sub init { | ||
| 78 | my ($self) = shift; | ||
| 79 | } | ||
| 80 | |||
| 81 | #----------------------------------------------------------------- | ||
| 82 | # toString | ||
| 83 | #----------------------------------------------------------------- | ||
| 84 | sub toString { | ||
| 85 | my ( $self, $something_else ) = @_; | ||
| 86 | require Data::Dumper; | ||
| 87 | if ($something_else) { | ||
| 88 | return Data::Dumper->Dump( [$something_else], ['M'] ); | ||
| 89 | } else { | ||
| 90 | return Data::Dumper->Dump( [$self], ['M'] ); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | #----------------------------------------------------------------- | ||
| 95 | # | ||
| 96 | # Error handling | ||
| 97 | # | ||
| 98 | #----------------------------------------------------------------- | ||
| 99 | my $DEFAULT_THROW_WITH_LOG = 0; | ||
| 100 | my $DEFAULT_THROW_WITH_STACK = 1; | ||
| 101 | |||
| 102 | #----------------------------------------------------------------- | ||
| 103 | # throw | ||
| 104 | #----------------------------------------------------------------- | ||
| 105 | sub throw { | ||
| 106 | my ( $self, $msg ) = @_; | ||
| 107 | $msg .= "\n" unless $msg =~ /\n$/; | ||
| 108 | |||
| 109 | my $LOG = Log::Log4perl->get_logger(__PACKAGE__); | ||
| 110 | |||
| 111 | # make an instance, if called as a class method | ||
| 112 | unless ( ref $self ) { | ||
| 113 | no strict 'refs'; | ||
| 114 | $self = $self->new; | ||
| 115 | } | ||
| 116 | |||
| 117 | # add (optionally) stack trace | ||
| 118 | $msg ||= 'An error.'; | ||
| 119 | my $with_stack = ( | ||
| 120 | defined $self->enable_throw_with_stack | ||
| 121 | ? $self->enable_throw_with_stack | ||
| 122 | : $DEFAULT_THROW_WITH_STACK | ||
| 123 | ); | ||
| 124 | my $result = ( $with_stack ? $self->format_stack($msg) : $msg ); | ||
| 125 | |||
| 126 | # die or log and die? | ||
| 127 | my $with_log = ( | ||
| 128 | defined $self->enable_throw_with_log | ||
| 129 | ? $self->enable_throw_with_log | ||
| 130 | : $DEFAULT_THROW_WITH_LOG | ||
| 131 | ); | ||
| 132 | if ($with_log) { | ||
| 133 | $LOG->logdie($result); | ||
| 134 | } else { | ||
| 135 | die($result); | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | #----------------------------------------------------------------- | ||
| 140 | # Some throwing options | ||
| 141 | # | ||
| 142 | # These options are not set by using AUTOLOAD (as other regular | ||
| 143 | # attributes) because AUTOLOAD could raise exception and we would be | ||
| 144 | # in a deep..., well deep recursion. | ||
| 145 | # | ||
| 146 | # Default values are: NO enable_throw_with_log | ||
| 147 | # YES enable_throw_with_stack | ||
| 148 | # (but they are globally changeable by calling | ||
| 149 | # default_throw_with_log and default_throw_with_stack) | ||
| 150 | # | ||
| 151 | #----------------------------------------------------------------- | ||
| 152 | sub enable_throw_with_log { | ||
| 153 | my ( $self, $value ) = @_; | ||
| 154 | $self->{enable_throw_with_log} = ( $value ? 1 : 0 ) | ||
| 155 | if ( defined $value ); | ||
| 156 | return $self->{enable_throw_with_log}; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub default_throw_with_log { | ||
| 160 | my ( $self, $value ) = @_; | ||
| 161 | $DEFAULT_THROW_WITH_LOG = ( $value ? 1 : 0 ) | ||
| 162 | if defined $value; | ||
| 163 | return $DEFAULT_THROW_WITH_LOG; | ||
| 164 | } | ||
| 165 | |||
| 166 | sub enable_throw_with_stack { | ||
| 167 | my ( $self, $value ) = @_; | ||
| 168 | $self->{enable_throw_with_stack} = ( $value ? 1 : 0 ) | ||
| 169 | if defined $value; | ||
| 170 | return $self->{enable_throw_with_stack}; | ||
| 171 | } | ||
| 172 | |||
| 173 | sub default_throw_with_stack { | ||
| 174 | my ( $self, $value ) = @_; | ||
| 175 | $DEFAULT_THROW_WITH_STACK = ( $value ? 1 : 0 ) | ||
| 176 | if defined $value; | ||
| 177 | return $DEFAULT_THROW_WITH_STACK; | ||
| 178 | } | ||
| 179 | |||
| 180 | #----------------------------------------------------------------- | ||
| 181 | # format_stack | ||
| 182 | #----------------------------------------------------------------- | ||
| 183 | sub format_stack { | ||
| 184 | my ( $self, $msg ) = @_; | ||
| 185 | my $stack = $self->_reformat_stacktrace($msg); | ||
| 186 | my $class = ref($self) || $self; | ||
| 187 | my $title = "------------- EXCEPTION: $class -------------"; | ||
| 188 | my $footer = "\n" . '-' x CORE::length($title); | ||
| 189 | return "\n$title\nMSG: $msg\n" . $stack . $footer . "\n"; | ||
| 190 | } | ||
| 191 | |||
| 192 | #----------------------------------------------------------------- | ||
| 193 | # _reformat_stacktrace | ||
| 194 | # Taken from bioperl. | ||
| 195 | # | ||
| 196 | # Takes one argument - an error message. It uses it to remove its | ||
| 197 | # repeated occurences from each line (not to print it). | ||
| 198 | # | ||
| 199 | # Reformatting of the stack: | ||
| 200 | # 1. Shift the file:line data in line i to line i+1. | ||
| 201 | # 2. change xxx::__ANON__() to "try{} block" | ||
| 202 | # 3. skip the "require" and "Error::subs::try" stack entries (boring) | ||
| 203 | # This means that the first line in the stack won't have | ||
| 204 | # any file:line data. | ||
| 205 | #----------------------------------------------------------------- | ||
| 206 | sub _reformat_stacktrace { | ||
| 207 | my ( $self, $msg ) = @_; | ||
| 208 | my $stack = Carp->longmess; | ||
| 209 | $stack =~ s/\Q$msg//; | ||
| 210 | my @stack = split( /\n/, $stack ); | ||
| 211 | my @new_stack = (); | ||
| 212 | my ( $method, $file, $linenum, $prev_file, $prev_linenum ); | ||
| 213 | my $stack_count = 0; | ||
| 214 | foreach my $i ( 0 .. $#stack ) { | ||
| 215 | |||
| 216 | if ( ( $stack[$i] =~ /^\s*([^(]+)\s*\(.*\) called at (\S+) line (\d+)/ ) | ||
| 217 | || ( $stack[$i] =~ /^\s*(require 0) called at (\S+) line (\d+)/ ) ) | ||
| 218 | { | ||
| 219 | ( $method, $file, $linenum ) = ( $1, $2, $3 ); | ||
| 220 | $stack_count++; | ||
| 221 | } else { | ||
| 222 | next; | ||
| 223 | } | ||
| 224 | if ( $stack_count == 1 ) { | ||
| 225 | push @new_stack, "STACK: $method"; | ||
| 226 | ( $prev_file, $prev_linenum ) = ( $file, $linenum ); | ||
| 227 | next; | ||
| 228 | } | ||
| 229 | if ( $method =~ /__ANON__/ ) { | ||
| 230 | $method = "try{} block"; | ||
| 231 | } | ||
| 232 | if ( ( $method =~ /^require/ and $file =~ /Error\.pm/ ) | ||
| 233 | || ( $method =~ /^Error::subs::try/ ) ) | ||
| 234 | { | ||
| 235 | last; | ||
| 236 | } | ||
| 237 | push @new_stack, "STACK: $method $prev_file:$prev_linenum"; | ||
| 238 | ( $prev_file, $prev_linenum ) = ( $file, $linenum ); | ||
| 239 | } | ||
| 240 | push @new_stack, "STACK: $prev_file:$prev_linenum"; | ||
| 241 | return join "\n", @new_stack; | ||
| 242 | } | ||
| 243 | |||
| 244 | #----------------------------------------------------------------- | ||
| 245 | # Set methods test whether incoming value is of a correct type. | ||
| 246 | # Here we return message explaining that it isn't. | ||
| 247 | #----------------------------------------------------------------- | ||
| 248 | sub _wrong_type_msg { | ||
| 249 | my ( $self, $given_type_or_value, $expected_type, $method ) = @_; | ||
| 250 | my $msg = 'In method '; | ||
| 251 | if ( defined $method ) { | ||
| 252 | $msg .= $method; | ||
| 253 | } else { | ||
| 254 | $msg .= ( caller(1) )[3]; | ||
| 255 | } | ||
| 256 | return ( | ||
| 257 | "$msg: Trying to set '$given_type_or_value' but '$expected_type' is expected." | ||
| 258 | ); | ||
| 259 | } | ||
| 260 | |||
| 261 | #----------------------------------------------------------------- | ||
| 262 | # Deal with 'set', 'get' and 'add_' methods. | ||
| 263 | #----------------------------------------------------------------- | ||
| 264 | sub AUTOLOAD { | ||
| 265 | my ( $self, @new_values ) = @_; | ||
| 266 | my $ref_sub; | ||
| 267 | if ( $AUTOLOAD =~ /.*::(\w+)/ && $self->_accessible("$1") ) { | ||
| 268 | |||
| 269 | # get/set method | ||
| 270 | my $attr_name = "$1"; | ||
| 271 | my $attr_type = $self->_attr_prop( $attr_name, TYPE ) || STRING; | ||
| 272 | my $attr_post = $self->_attr_prop( $attr_name, POST ); | ||
| 273 | my $attr_is_array = $self->_attr_prop( $attr_name, ISARRAY ); | ||
| 274 | my $attr_readonly = $self->_attr_prop( $attr_name, READONLY ); | ||
| 275 | $ref_sub = sub { | ||
| 276 | local *__ANON__ = "__ANON__$attr_name" . "_" . ref($self); | ||
| 277 | my ( $this, @values ) = @_; | ||
| 278 | return $this->_getter($attr_name) unless @values; | ||
| 279 | $self->throw("Sorry, the attribute '$attr_name' is read-only.") | ||
| 280 | if $attr_readonly; | ||
| 281 | |||
| 282 | # here we continue with 'set' method: | ||
| 283 | if ($attr_is_array) { | ||
| 284 | my @result = | ||
| 285 | ( ref( $values[0] ) eq 'ARRAY' ? @{ $values[0] } : @values ); | ||
| 286 | foreach my $value (@result) { | ||
| 287 | $value = $this->check_type( $AUTOLOAD, $attr_type, $value ); | ||
| 288 | } | ||
| 289 | $this->_setter( $attr_name, $attr_type, \@result ); | ||
| 290 | } else { | ||
| 291 | $this->_setter( | ||
| 292 | $attr_name, | ||
| 293 | $attr_type, | ||
| 294 | $this->check_type( | ||
| 295 | $AUTOLOAD, $attr_type, @values | ||
| 296 | ) | ||
| 297 | ); | ||
| 298 | } | ||
| 299 | |||
| 300 | # call post-procesing (if defined) | ||
| 301 | $this->$attr_post( $this->{$attr_name} ) if $attr_post; | ||
| 302 | return $this->{$attr_name}; | ||
| 303 | }; | ||
| 304 | } elsif ( $AUTOLOAD =~ /.*::add_(\w+)/ && $self->_accessible("$1") ) { | ||
| 305 | |||
| 306 | # add_XXXX method | ||
| 307 | my $attr_name = "$1"; | ||
| 308 | my $attr_post = $self->_attr_prop( $attr_name, POST ); | ||
| 309 | if ( $self->_attr_prop( $attr_name, ISARRAY ) ) { | ||
| 310 | my $attr_type = $self->_attr_prop( $attr_name, TYPE ) || STRING; | ||
| 311 | $ref_sub = sub { | ||
| 312 | local *__ANON__ = "__ANON__$attr_name" . "_" . ref($self); | ||
| 313 | my ( $this, @values ) = @_; | ||
| 314 | if (@values) { | ||
| 315 | my @result = ( | ||
| 316 | ref( $values[0] ) eq 'ARRAY' | ||
| 317 | ? @{ $values[0] } | ||
| 318 | : @values ); | ||
| 319 | foreach my $value (@result) { | ||
| 320 | $value = | ||
| 321 | $this->check_type( $AUTOLOAD, $attr_type, $value ); | ||
| 322 | } | ||
| 323 | $this->_adder( $attr_name, $attr_type, @result ); | ||
| 324 | } | ||
| 325 | |||
| 326 | # call post-procesing (if defined) | ||
| 327 | $this->$attr_post( $this->{$attr_name} ) if $attr_post; | ||
| 328 | return $this; | ||
| 329 | } | ||
| 330 | } else { | ||
| 331 | $self->throw( | ||
| 332 | "Method '$AUTOLOAD' is allowed only for array-type attributes." | ||
| 333 | ); | ||
| 334 | } | ||
| 335 | } else { | ||
| 336 | $self->throw("No such method: $AUTOLOAD"); | ||
| 337 | } | ||
| 338 | no strict 'refs'; | ||
| 339 | *{$AUTOLOAD} = $ref_sub; | ||
| 340 | use strict 'refs'; | ||
| 341 | return $ref_sub->( $self, @new_values ); | ||
| 342 | } | ||
| 343 | |||
| 344 | #----------------------------------------------------------------- | ||
| 345 | # The low level get/set methods. They are called from AUTOLOAD, and | ||
| 346 | # they are separated here so they can be overriten - as they are in | ||
| 347 | # the service skeletons, for example. Also, there may be situation | ||
| 348 | # that one can call them if other features (such as type checking) are | ||
| 349 | # not required. | ||
| 350 | #----------------------------------------------------------------- | ||
| 351 | sub _getter { | ||
| 352 | my ( $self, $attr_name ) = @_; | ||
| 353 | return $self->{$attr_name}; | ||
| 354 | } | ||
| 355 | |||
| 356 | sub _setter { | ||
| 357 | my ( $self, $attr_name, $attr_type, $value ) = @_; | ||
| 358 | $self->{$attr_name} = $value; | ||
| 359 | } | ||
| 360 | |||
| 361 | sub _adder { | ||
| 362 | my ( $self, $attr_name, $attr_type, @values ) = @_; | ||
| 363 | push( @{ $self->{$attr_name} }, @values ); | ||
| 364 | } | ||
| 365 | |||
| 366 | #----------------------------------------------------------------- | ||
| 367 | # Keep it here! The reason is the existence of AUTOLOAD... | ||
| 368 | #----------------------------------------------------------------- | ||
| 369 | sub DESTROY { | ||
| 370 | } | ||
| 371 | |||
| 372 | #----------------------------------------------------------------- | ||
| 373 | # | ||
| 374 | # Check type of @value against $expected_type. Return checked $value | ||
| 375 | # (perhaps trimmed, or otherwise corrected - e.g. wrapped in an | ||
| 376 | # appropriate object), or undef if the $value is of a wrong type. | ||
| 377 | # | ||
| 378 | #----------------------------------------------------------------- | ||
| 379 | sub check_type { | ||
| 380 | my ( $self, $name, $expected_type, @values ) = @_; | ||
| 381 | my $value = $values[0]; | ||
| 382 | |||
| 383 | # first process cases when an expected type is a simple string, | ||
| 384 | # integer etc. (not SADI::Data::String etc.) - e.g. when an ID | ||
| 385 | # attribute is being set | ||
| 386 | if ( $expected_type eq STRING ) { | ||
| 387 | return $value; | ||
| 388 | } elsif ( $expected_type eq INTEGER ) { | ||
| 389 | $self->throw( $self->_wrong_type_msg( $value, $expected_type, $name ) ) | ||
| 390 | unless $value =~ m/^\s*[+-]?\s*\d+\s*$/; | ||
| 391 | $value =~ s/\s//g; | ||
| 392 | return $value; | ||
| 393 | } elsif ( $expected_type eq FLOAT ) { | ||
| 394 | $self->throw( $self->_wrong_type_msg( $value, $expected_type, $name ) ) | ||
| 395 | unless $value =~ | ||
| 396 | m/^\s*[+-]?\s*(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?\s*$/; | ||
| 397 | $value =~ s/\s//g; | ||
| 398 | return $value; | ||
| 399 | } elsif ( $expected_type eq BOOLEAN ) { | ||
| 400 | return ( $value =~ /true|\+|1|yes|ano/ ? '1' : '0' ); | ||
| 401 | } elsif ( $expected_type eq DATETIME ) { | ||
| 402 | my $iso; | ||
| 403 | eval { | ||
| 404 | $iso = ( | ||
| 405 | HTTP::Date::time2isoz( | ||
| 406 | HTTP::Date::str2time( HTTP::Date::parse_date($value) ) | ||
| 407 | ) | ||
| 408 | ); | ||
| 409 | }; | ||
| 410 | $self->throw( $self->_wrong_type_msg( $value, 'ISO-8601', $name ) ) | ||
| 411 | if $@; | ||
| 412 | return $iso; ### $iso =~ s/ /T/; ??? TBD | ||
| 413 | } else { | ||
| 414 | |||
| 415 | # Then process cases when the expected type is a name of a | ||
| 416 | # real object (e.g. SADI::Data::String); for these cases the | ||
| 417 | # $value[0] can be already such object - in which case nothing | ||
| 418 | # to be done; or $value[0] can be HASH, or @values can be a | ||
| 419 | # list of name/value pairs, in which case a new object (of | ||
| 420 | # type $expected_type) has to be created and initialized by | ||
| 421 | # @values; and, still in the latter case, if the @values has | ||
| 422 | # just one element (XX), this element is considered a 'value': | ||
| 423 | # it is treated as a a hash {value => XX}. | ||
| 424 | return $value if UNIVERSAL::isa( $value, $expected_type ); | ||
| 425 | $value = { value => $value } | ||
| 426 | unless ref($value) || @values > 1; | ||
| 427 | my ($value_ref_type) = ref($value); | ||
| 428 | if ( $value_ref_type eq 'HASH' ) { | ||
| 429 | |||
| 430 | # e.g. $sequence->Length ( { value => 12, id => 'IR64'} ) | ||
| 431 | return $self->create_member( $name, $expected_type, %$value ); | ||
| 432 | } elsif ( $value_ref_type eq 'ARRAY' ) { | ||
| 433 | |||
| 434 | # e.g. $sequence->Length ( [ value => 12, id => 'IR64'] ) | ||
| 435 | return $self->create_member( $name, $expected_type, @$value ); | ||
| 436 | } elsif ($value_ref_type) { | ||
| 437 | |||
| 438 | # e.g. $sequence->Length ( new SADI::Data::Integer ( value => 12) ) | ||
| 439 | $self->throw( | ||
| 440 | $self->_wrong_type_msg( | ||
| 441 | $value_ref_type, $expected_type, $name | ||
| 442 | ) | ||
| 443 | ) unless UNIVERSAL::isa( $value, $expected_type ); | ||
| 444 | return $value; | ||
| 445 | } else { | ||
| 446 | |||
| 447 | # e.g. $sequence->Length (value => 12, id => 'IR64') | ||
| 448 | return $self->create_member( $name, $expected_type, @values ); | ||
| 449 | } | ||
| 450 | } | ||
| 451 | } | ||
| 452 | |||
| 453 | #----------------------------------------------------------------- | ||
| 454 | # | ||
| 455 | #----------------------------------------------------------------- | ||
| 456 | sub create_member { | ||
| 457 | my ( $self, $name, $expected_type, @values ) = @_; | ||
| 458 | eval "require $expected_type"; | ||
| 459 | $self->throw( $self->_wrong_type_msg( $values[0], $expected_type, $name ) ) | ||
| 460 | if $@; | ||
| 461 | return "$expected_type"->new(@values); | ||
| 462 | } | ||
| 463 | |||
| 464 | 1; | ||
| 465 | |||
| 466 | __END__ |