2001-05-05 11:12:56 +04:00
|
|
|
# -*- Mode: perl; tab-width: 4; indent-tabs-mode: nil; -*-
|
|
|
|
#
|
|
|
|
# This file is MPL/GPL dual-licensed under the following terms:
|
|
|
|
#
|
|
|
|
# The contents of this file are subject to the Mozilla Public License
|
|
|
|
# Version 1.1 (the "License"); you may not use this file except in
|
|
|
|
# compliance with the License. You may obtain a copy of the License at
|
|
|
|
# http://www.mozilla.org/MPL/
|
|
|
|
#
|
|
|
|
# Software distributed under the License is distributed on an "AS IS"
|
|
|
|
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
|
|
|
|
# the License for the specific language governing rights and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
# The Original Code is PLIF 1.0.
|
|
|
|
# The Initial Developer of the Original Code is Ian Hickson.
|
|
|
|
#
|
|
|
|
# Alternatively, the contents of this file may be used under the terms
|
|
|
|
# of the GNU General Public License Version 2 or later (the "GPL"), in
|
|
|
|
# which case the provisions of the GPL are applicable instead of those
|
|
|
|
# above. If you wish to allow use of your version of this file only
|
|
|
|
# under the terms of the GPL and not to allow others to use your
|
|
|
|
# version of this file under the MPL, indicate your decision by
|
|
|
|
# deleting the provisions above and replace them with the notice and
|
|
|
|
# other provisions required by the GPL. If you do not delete the
|
|
|
|
# provisions above, a recipient may use your version of this file
|
|
|
|
# under either the MPL or the GPL.
|
|
|
|
|
|
|
|
package PLIF;
|
|
|
|
use strict; # require strict adherence to perl standards
|
2002-11-02 20:56:51 +03:00
|
|
|
use vars qw($AUTOLOAD); # it's a package global
|
|
|
|
use POSIX qw(strftime); # timestamps in debug output
|
2002-12-30 11:04:29 +03:00
|
|
|
use PLIF::Exception;
|
2002-09-09 23:22:10 +04:00
|
|
|
my $DEBUG = 9; # level of warnings and dumps to print to STDERR (none go to user)
|
2003-03-14 10:50:18 +03:00
|
|
|
my %MODULES = ('PLIF' => 1);
|
2001-05-05 11:12:56 +04:00
|
|
|
1;
|
|
|
|
|
|
|
|
# PLIF = Program Logic Insulation Framework
|
|
|
|
|
|
|
|
# Levels are assumed to be something along the following:
|
2003-01-02 23:39:49 +03:00
|
|
|
# 0 = debugging remarks for the section currently under test
|
|
|
|
# 1 =
|
2003-03-14 16:08:43 +03:00
|
|
|
# 2 = perl warnings
|
2003-01-02 23:39:49 +03:00
|
|
|
# 3 =
|
2001-05-05 11:12:56 +04:00
|
|
|
# 4 = important warnings (e.g. unexpected but possibly legitimate lack of data)
|
|
|
|
# 5 = important events (e.g. application started)
|
2003-01-02 23:39:49 +03:00
|
|
|
# 6 =
|
2001-05-05 11:12:56 +04:00
|
|
|
# 7 = typical checkpoints (e.g. someone tried to do some output)
|
2001-11-09 19:50:47 +03:00
|
|
|
# 8 = frequently hit typical checkpoints
|
2001-05-05 11:12:56 +04:00
|
|
|
# 9 = verbose debugging information
|
|
|
|
# 10 = ridiculously verbose debugging spam
|
|
|
|
|
2003-03-27 22:59:32 +03:00
|
|
|
# Note. All of the methods described in this class except for
|
|
|
|
# propertyGet, propertySet, the init and load methods, and AUTOLOAD
|
|
|
|
# are class methods. You can call "$class->notImplemented" without a
|
|
|
|
# problem.
|
2001-05-05 11:12:56 +04:00
|
|
|
|
|
|
|
# provide a standard virtual constructor
|
|
|
|
# if already created, merely return $self
|
|
|
|
sub create {
|
|
|
|
my $class = shift;
|
|
|
|
if (ref($class)) {
|
|
|
|
return $class; # already created, return self
|
|
|
|
} else {
|
2001-05-13 22:04:52 +04:00
|
|
|
my $self = $class->bless(@_); # call our real constructor
|
2001-11-22 18:46:42 +03:00
|
|
|
$self->serviceInit(@_);
|
2001-05-13 22:04:52 +04:00
|
|
|
return $self;
|
2001-05-05 11:12:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-19 21:56:15 +04:00
|
|
|
# provide a constructor that always constructs a new copy of the
|
|
|
|
# class. This is used to create service instances.
|
|
|
|
sub serviceCreate {
|
|
|
|
my $class = shift;
|
|
|
|
if (ref($class)) {
|
|
|
|
$class = ref($class);
|
|
|
|
}
|
|
|
|
my $self = $class->bless(@_); # call our real constructor
|
2001-11-22 18:46:42 +03:00
|
|
|
$self->serviceInstanceInit(@_);
|
2001-09-19 21:56:15 +04:00
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub init {} # stub for services
|
2001-05-13 22:04:52 +04:00
|
|
|
|
2001-11-22 18:46:42 +03:00
|
|
|
sub serviceInit {
|
|
|
|
my $self = shift;
|
|
|
|
$self->init(@_);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub serviceInstanceInit {
|
|
|
|
my $self = shift;
|
|
|
|
$self->init(@_);
|
|
|
|
}
|
|
|
|
|
2001-05-05 11:12:56 +04:00
|
|
|
# provide a constructor that always constructs a new copy of the
|
|
|
|
# class. This is used by services that implement factories for objects
|
|
|
|
# implemented in the same class (e.g., session objects do this).
|
2001-05-13 22:04:52 +04:00
|
|
|
sub objectCreate {
|
2001-05-05 11:12:56 +04:00
|
|
|
my $class = shift;
|
|
|
|
if (ref($class)) {
|
|
|
|
$class = ref($class);
|
|
|
|
}
|
2001-05-13 22:04:52 +04:00
|
|
|
my $self = $class->bless(@_); # call our real constructor
|
|
|
|
$self->objectInit(@_);
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
2001-09-19 21:56:15 +04:00
|
|
|
sub objectInit {} # stub for objects
|
2001-05-13 22:04:52 +04:00
|
|
|
|
|
|
|
# internals of create and objectCreate
|
|
|
|
sub bless {
|
|
|
|
my $class = shift;
|
2001-05-05 11:12:56 +04:00
|
|
|
my $self = {};
|
|
|
|
CORE::bless($self, $class);
|
2003-03-14 10:50:18 +03:00
|
|
|
$self->load($class);
|
2001-05-05 11:12:56 +04:00
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
2003-03-14 10:50:18 +03:00
|
|
|
sub load {
|
|
|
|
my $self = shift;
|
|
|
|
my($package) = @_;
|
2004-02-25 20:41:30 +03:00
|
|
|
|
2003-03-27 22:59:32 +03:00
|
|
|
if (defined $MODULES{$package}) {
|
|
|
|
syntaxError "$package->create() called despite failing to load package" if $MODULES{$package} == 0;
|
|
|
|
return;
|
|
|
|
}
|
2004-02-25 20:41:30 +03:00
|
|
|
|
2003-03-27 22:59:32 +03:00
|
|
|
$MODULES{$package} = -1;
|
2003-03-14 10:50:18 +03:00
|
|
|
foreach (eval "\@$package\::ISA") {
|
2004-02-25 20:41:30 +03:00
|
|
|
$self->load($_) unless $_ eq __PACKAGE__ || $_ eq 'Exporter';
|
2003-03-14 10:50:18 +03:00
|
|
|
}
|
2004-02-25 20:41:30 +03:00
|
|
|
$MODULES{$package} = 1;
|
|
|
|
|
|
|
|
# bail early if there is no __DATA__ section
|
|
|
|
return unless defined fileno("$package\::DATA");
|
|
|
|
|
2003-03-14 10:50:18 +03:00
|
|
|
local $/ = undef;
|
2003-03-14 16:08:43 +03:00
|
|
|
my $data = "package $package;use strict;" . eval "<$package\::DATA>";
|
2004-02-25 20:41:30 +03:00
|
|
|
evalString $data, "${package} on-demand section" unless $@;
|
2003-03-14 10:50:18 +03:00
|
|
|
if ($@) {
|
|
|
|
$self->error(1, "Error while loading '$package': $@");
|
2003-03-27 22:59:32 +03:00
|
|
|
$MODULES{$package} = 0;
|
2003-03-14 10:50:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-27 22:59:32 +03:00
|
|
|
# turn the magic AUTOLOAD into the slightly more useful and less
|
|
|
|
# magical implyMethod().
|
2001-05-05 11:12:56 +04:00
|
|
|
sub AUTOLOAD {
|
|
|
|
my $self = shift;
|
|
|
|
my $name = $AUTOLOAD;
|
2003-03-27 22:59:32 +03:00
|
|
|
syntaxError "$name() called without object" if not ref($self);
|
|
|
|
$name =~ s/^(.*):://os; # strip fully-qualified portion
|
|
|
|
my $package = $1;
|
|
|
|
if ($package =~ /::SUPER$/os) {
|
|
|
|
# handle calling inherited methods
|
|
|
|
$package =~ s/::SUPER$//os;
|
|
|
|
my @ISA = eval "if (defined(\@$package\::ISA)) { return \@$package\::ISA }";
|
|
|
|
if (@ISA == 1) {
|
|
|
|
$package = $ISA[0];
|
|
|
|
} else {
|
|
|
|
syntaxError "$package\::SUPER->$name() called but $package has multiple ancestors";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (not exists $MODULES{$package}) {
|
|
|
|
syntaxError "$package->$name() called without loading package";
|
|
|
|
} elsif ($MODULES{$package} == 1) {
|
|
|
|
my $method = $package->can('implyMethod'); # get a function pointer
|
|
|
|
@_ = ($self, $name, @_); # set the arguments
|
|
|
|
goto &$method; # invoke the method using deep magic
|
|
|
|
} elsif ($MODULES{$package} == 0) {
|
|
|
|
syntaxError "$package->$name() called despite failing to load package";
|
|
|
|
} else {
|
|
|
|
syntaxError "$package->$name() called while loading package";
|
|
|
|
}
|
2001-05-05 11:12:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
sub propertySet {
|
|
|
|
# this is not a class method
|
|
|
|
my $self = shift;
|
|
|
|
my($name, $value) = @_;
|
|
|
|
return $self->{$name} = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub propertyGet {
|
|
|
|
# this is not a class method
|
|
|
|
my $self = shift;
|
|
|
|
my($name) = @_;
|
|
|
|
return $self->{$name};
|
|
|
|
}
|
|
|
|
|
2003-03-14 16:08:43 +03:00
|
|
|
sub implyMethod {
|
2001-05-05 11:12:56 +04:00
|
|
|
my $self = shift;
|
|
|
|
my($method) = @_;
|
2003-03-27 22:59:32 +03:00
|
|
|
syntaxError "Tried to access non-existent method '$method' in object '$self'";
|
2001-05-05 11:12:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# DEBUGGING AIDS
|
|
|
|
|
|
|
|
sub dump {
|
|
|
|
my $self = shift;
|
|
|
|
my($level, @data) = @_;
|
|
|
|
if ($self->isAtDebugLevel($level)) {
|
2002-11-02 20:56:51 +03:00
|
|
|
my $time = strftime('%Y-%m-%d %H:%M:%S UTC', gmtime());
|
2001-05-05 11:12:56 +04:00
|
|
|
foreach (@data) {
|
2002-11-02 20:56:51 +03:00
|
|
|
print STDERR "$0.$$ \@ $time: ($level) $_\n";
|
2001-05-05 11:12:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub warn {
|
|
|
|
my $self = shift;
|
|
|
|
my($level, @data) = @_;
|
|
|
|
if ($self->isAtDebugLevel($level)) {
|
2003-01-02 23:39:49 +03:00
|
|
|
$self->dump($level, ('-'x20).' Start of Warning Stack Trace '.('-'x20));
|
2002-12-30 11:04:29 +03:00
|
|
|
report PLIF::Exception ('message' => join("\n", @data));
|
2003-01-02 23:39:49 +03:00
|
|
|
$self->dump($level, ('-'x20). ('-'x30) .('-'x20));
|
2001-05-05 11:12:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-02 23:39:49 +03:00
|
|
|
# raises a generic error with the arguments passed as the message
|
|
|
|
# use this for internal errors that don't have their own exception objects
|
|
|
|
# this should not be called with the @data containing a trailing dot
|
2001-05-05 11:12:56 +04:00
|
|
|
sub error {
|
|
|
|
my $self = shift;
|
|
|
|
my($level, @data) = @_;
|
2003-01-02 23:39:49 +03:00
|
|
|
# the next three lines are a highly magical incantation to remove
|
|
|
|
# this call from the stack so that the stack trace looks like the
|
|
|
|
# previous function raised the exception itself
|
2003-03-14 16:08:43 +03:00
|
|
|
my $raise = PLIF::Exception->can('raise'); # get a function pointer
|
|
|
|
@_ = ('PLIF::Exception', 'message', join("\n", @data)); # set the arguments
|
|
|
|
goto &$raise; # and invoke the call using deep call stack magic
|
2001-05-05 11:12:56 +04:00
|
|
|
}
|
|
|
|
|
2001-11-09 23:09:22 +03:00
|
|
|
# this should not be called with the @data containing a trailing dot
|
2001-05-05 11:12:56 +04:00
|
|
|
sub assert {
|
|
|
|
my $self = shift;
|
|
|
|
my($condition, $level, @data) = @_;
|
|
|
|
if (not $condition) {
|
2003-01-02 23:39:49 +03:00
|
|
|
# the next three lines are a highly magical incantation to remove
|
|
|
|
# this call from the stack so that the stack trace looks like the
|
|
|
|
# previous function raised the exception itself
|
|
|
|
my $raise = PLIF::Exception->can('raise');
|
|
|
|
@_ = ('PLIF::Exception', 'message', join("\n", @data));
|
|
|
|
goto &$raise;
|
2001-05-05 11:12:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub notImplemented {
|
|
|
|
my $self = shift;
|
|
|
|
$self->error(0, 'Internal Error: Method not implemented');
|
|
|
|
}
|
|
|
|
|
|
|
|
# returns true only if the argument is a debug level that is at least
|
|
|
|
# as important as the local value of $DEBUG.
|
|
|
|
sub isAtDebugLevel {
|
|
|
|
my $self = shift;
|
|
|
|
my($level) = @_;
|
|
|
|
return ($level <= $DEBUG);
|
|
|
|
}
|
|
|
|
|
|
|
|
# returns a reference to the $DEBUG variable for configuration
|
|
|
|
# purposes
|
|
|
|
sub getDebugLevel {
|
|
|
|
return \$DEBUG;
|
|
|
|
}
|
|
|
|
|
2003-03-15 17:14:21 +03:00
|
|
|
sub DESTROY {}
|
|
|
|
# my $self = shift;
|
|
|
|
# $self->dump(10, "Called destructor of object $self...");
|
|
|
|
#}
|