1 |
## -------------------------------------------------------------------------------- |
2 |
## $Id: Bridge.pm,v 1.3 2002/12/16 19:57:12 joko Exp $ |
3 |
## -------------------------------------------------------------------------------- |
4 |
## $Log: Bridge.pm,v $ |
5 |
## Revision 1.3 2002/12/16 19:57:12 joko |
6 |
## + sub unload |
7 |
## |
8 |
## Revision 1.2 2002/12/15 02:06:15 joko |
9 |
## + feature to be able to specify module in non-perl-style when loading plugins: e.g. $process->load('Setup/Storage') |
10 |
## |
11 |
## Revision 1.1 2002/12/13 21:46:29 joko |
12 |
## + initial check-in |
13 |
## |
14 |
## -------------------------------------------------------------------------------- |
15 |
|
16 |
|
17 |
package DesignPattern::Bridge; |
18 |
|
19 |
use strict; |
20 |
use warnings; |
21 |
|
22 |
use base qw( DesignPattern::Object ); |
23 |
|
24 |
use Data::Dumper; |
25 |
|
26 |
## ======== object inheritance ======== |
27 |
|
28 |
# TODO: |
29 |
# - this is no inheritance and it doesn't have to be |
30 |
# - implement this module as a bridge to its sub-modules |
31 |
# - use the BridgePattern (http://c2.com/cgi/wiki?BridgePattern) |
32 |
# Using patterns was already successfully done with Data::Storage::Handler |
33 |
# by implementing the AdapterPattern (http://c2.com/cgi/wiki?AdapterPattern) |
34 |
# with Perl's AUTOLOAD-mechanism |
35 |
# - try to use Perl's "tie" command to implement this functionality here instead of using AUTOLOAD! |
36 |
# - sub getChildNodes |
37 |
# - sub run |
38 |
|
39 |
# get logger instance |
40 |
my $logger = Log::Dispatch::Config->instance; |
41 |
|
42 |
my $meta; |
43 |
|
44 |
## ======== object constructor ======== |
45 |
sub new { |
46 |
my $invocant = shift; |
47 |
my $class = ref($invocant) || $invocant; |
48 |
my @args = (); |
49 |
@_ && (@args = @_); |
50 |
$logger->debug( __PACKAGE__ . "->new(@args)" ); |
51 |
my $self = { @_ }; |
52 |
#print "class: $class", "\n"; |
53 |
bless $self, $class; |
54 |
##if (my $bizWorks = shift) { |
55 |
##$self->boot($bizWorks); |
56 |
##} |
57 |
|
58 |
return $self; |
59 |
} |
60 |
|
61 |
|
62 |
## ======== method overrider ======== |
63 |
sub AUTOLOAD2 { |
64 |
|
65 |
my $self = shift; |
66 |
our $AUTOLOAD; |
67 |
|
68 |
##print "AUTOLOAD\n"; |
69 |
|
70 |
my $method = $AUTOLOAD; |
71 |
$method =~ s/^.*:://; |
72 |
|
73 |
$logger->debug( __PACKAGE__ . "->" . $method . "(@_)" . " (AUTOLOAD called, not dispatched)" ); |
74 |
|
75 |
## ->DESTROY would - if not declared - trigger an AUTOLOAD also |
76 |
return if $method =~ m/::DESTROY$/; |
77 |
|
78 |
## if ($self->_filter_AUTOLOAD($method)) { |
79 |
## $self->_accessStorage(); |
80 |
## $self->{STORAGEHANDLE}->$method(@_); |
81 |
## } |
82 |
|
83 |
} |
84 |
|
85 |
sub _getPluginPackage { |
86 |
my $self = shift; |
87 |
my $modulename_load = shift; |
88 |
|
89 |
# substitute slashes through double double-colons to load modules perl-style |
90 |
$modulename_load =~ s/\//::/g; |
91 |
|
92 |
# build full package name |
93 |
my $self_classname = ref $self; |
94 |
my $package = $self_classname . '::' . $modulename_load; |
95 |
return $package; |
96 |
} |
97 |
|
98 |
sub load { |
99 |
|
100 |
my $self = shift; |
101 |
my $modulename_load = shift; |
102 |
|
103 |
my $self_modulename = ref $self; |
104 |
my $package = $self->_getPluginPackage($modulename_load); |
105 |
|
106 |
if ($meta->{loaded}->{$package}) { |
107 |
return 1; |
108 |
} |
109 |
|
110 |
#$logger->info( __PACKAGE__ . "->load: $package" ); |
111 |
#$logger->info( __PACKAGE__ . "->load: $self_modulename" ); |
112 |
$logger->debug( $self_modulename . "->load: $package\t[via " . __PACKAGE__ . "]" ); |
113 |
|
114 |
# this is the module testing phase - use mixin doesn't seem to propagate errors by default |
115 |
eval("use $package;"); |
116 |
if ($@) { |
117 |
$meta->{loaded}->{$package} = 0; |
118 |
# include caller information |
119 |
my @caller = caller; |
120 |
my $caller_msg = $caller[1] . ':' . $caller[2]; |
121 |
$logger->error( __PACKAGE__ . "->load: $@ ($caller_msg)" ); |
122 |
} |
123 |
|
124 |
#print "ref-1: ", ref $self, "\n"; |
125 |
#print "ref-2: ", ref $self::SUPER, "\n"; |
126 |
|
127 |
# V1: |
128 |
#bless $self, $package; |
129 |
|
130 |
# V2: |
131 |
# switch into foreign package and mixin plugin-module |
132 |
$self->mixinPackage($package); |
133 |
|
134 |
return 1; |
135 |
|
136 |
} |
137 |
|
138 |
# TODO: maybe refactor to DesignPattern::Object? what about the '$logger'? |
139 |
sub mixinPackage { |
140 |
my $self = shift; |
141 |
my $package = shift; |
142 |
# switch into foreign package and mixin plugin-module |
143 |
my $self_classname = ref $self; |
144 |
eval("package $self_classname; use mixin '$package';"); |
145 |
#eval("use mixin_all '$package';"); |
146 |
if ($@) { |
147 |
$meta->{loaded}->{$package} = 0; |
148 |
$logger->error( __PACKAGE__ . "->load: $@" ); |
149 |
} else { |
150 |
$meta->{loaded}->{$package} = 1; |
151 |
} |
152 |
} |
153 |
|
154 |
sub unload { |
155 |
|
156 |
my $self = shift; |
157 |
my $modulename_unload = shift; |
158 |
|
159 |
my $package = $self->_getPluginPackage($modulename_unload); |
160 |
|
161 |
if ($meta->{loaded}->{$package}) { |
162 |
$meta->{loaded}->{$package} = 0; |
163 |
my $where = __PACKAGE__ . ':' . __LINE__; |
164 |
$logger->debug( __PACKAGE__ . "->unload: FIXME: DESTROY object is not implemented at '$where'." ); |
165 |
} |
166 |
|
167 |
} |
168 |
|
169 |
|
170 |
sub boot { |
171 |
my $self = shift; |
172 |
$self->_abstract_function('boot'); |
173 |
} |
174 |
|
175 |
sub include { |
176 |
my $self = shift; |
177 |
my $includefile = shift; |
178 |
my $package = shift; |
179 |
# TODO: do better error-detection here / prevent dies under all circumstances! |
180 |
require $includefile; |
181 |
$self->mixinPackage($package) if $package; |
182 |
} |
183 |
|
184 |
1; |