| 1 | =head1 LICENSE | ||
| 2 | |||
| 3 | See the NOTICE file distributed with this work for additional information | ||
| 4 | regarding copyright ownership. | ||
| 5 | |||
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 7 | you may not use this file except in compliance with the License. | ||
| 8 | You may obtain a copy of the License at | ||
| 9 | |||
| 10 | http://www.apache.org/licenses/LICENSE-2.0 | ||
| 11 | |||
| 12 | Unless required by applicable law or agreed to in writing, software | ||
| 13 | distributed under the License is distributed on an "AS IS" BASIS, | ||
| 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 15 | See the License for the specific language governing permissions and | ||
| 16 | limitations under the License. | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | |||
| 21 | =head1 CONTACT | ||
| 22 | |||
| 23 | Please email comments or questions to the public Ensembl | ||
| 24 | developers list at <http://lists.ensembl.org/mailman/listinfo/dev>. | ||
| 25 | |||
| 26 | Questions may also be sent to the Ensembl help desk at | ||
| 27 | <http://www.ensembl.org/Help/Contact>. | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | =head1 NAME | ||
| 32 | |||
| 33 | Bio::EnsEMBL::Utils::Proxy | ||
| 34 | |||
| 35 | =head1 SYNOPSIS | ||
| 36 | |||
| 37 | #Simple arounds logging proxy | ||
| 38 | package myproxy; | ||
| 39 | use base qw/Bio::EnsEMBL::Utils::Proxy/; | ||
| 40 | sub __resolver { | ||
| 41 | my ($invoker, $package, $method) = @_; | ||
| 42 | return sub { | ||
| 43 | my ($self, @args); | ||
| 44 | warn "Entering into ${package}::${method}"; | ||
| 45 | my @capture = $self->$method(@args); | ||
| 46 | warn "Exiting from ${package}::${method}"; | ||
| 47 | return @capture; | ||
| 48 | }; | ||
| 49 | } | ||
| 50 | |||
| 51 | 1; | ||
| 52 | |||
| 53 | =head1 DESCRIPTION | ||
| 54 | |||
| 55 | This class offers Proxy objects similar to those found in Java's | ||
| 56 | C<java.lang.reflect.Proxy> object. This class should be overriden and | ||
| 57 | then implement C<__resolver()>. The C<__resolver()> method returns a | ||
| 58 | subroutine to the intended action which the proxy object installs into | ||
| 59 | the calling class' scope. | ||
| 60 | |||
| 61 | All methods internal to the proxy are prefixed with a double underscore | ||
| 62 | to avoid corruption/intrusion into the normal public and private scope of | ||
| 63 | most classes. | ||
| 64 | |||
| 65 | =head1 METHODS | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | package Bio::EnsEMBL::Utils::Proxy; | ||
| 70 | $Bio::EnsEMBL::Utils::Proxy::VERSION = '114.0.0_50'; # TRIAL | ||
| 71 | $Bio::EnsEMBL::Utils::Proxy::VERSION = '114.0.050'; | ||
| 72 | use strict; | ||
| 73 | use warnings; | ||
| 74 | |||
| 75 | use Bio::EnsEMBL::Utils::Exception qw/throw/; | ||
| 76 | |||
| 77 | use vars '$AUTOLOAD'; | ||
| 78 | |||
| 79 | =head2 new | ||
| 80 | |||
| 81 | Arg [1] : The object to proxy | ||
| 82 | Example : my $newobj = Bio::EnsEMBL::Utils::Proxy->new($myobj); | ||
| 83 | Description : Provides a new instance of a proxy | ||
| 84 | Returntype : Bio::EnsEMBL::Utils::Proxy the new instance | ||
| 85 | Exceptions : None | ||
| 86 | Caller : public | ||
| 87 | Status : - | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | sub new { | ||
| 92 | my ($class, $proxy) = @_; | ||
| 93 | my $self = bless({}, ref($class)||$class); | ||
| 94 | $self->{__proxy} = $proxy; | ||
| 95 | return $self; | ||
| 96 | } | ||
| 97 | |||
| 98 | =head2 __proxy | ||
| 99 | |||
| 100 | Example : - | ||
| 101 | Description : The proxy accessor | ||
| 102 | Returntype : Any the proxied object | ||
| 103 | Exceptions : None | ||
| 104 | Caller : - | ||
| 105 | Status : - | ||
| 106 | |||
| 107 | =cut | ||
| 108 | |||
| 109 | sub __proxy { | ||
| 110 | my ($self) = @_; | ||
| 111 | return $_[0]->{__proxy}; | ||
| 112 | } | ||
| 113 | |||
| 114 | =head2 isa | ||
| 115 | |||
| 116 | Args : Object type to test | ||
| 117 | Example : $obj->isa('Bio::EnsEMBL::Utils::Proxy'); | ||
| 118 | Description : Overriden to provide C<isa()> support for proxies. Will return | ||
| 119 | true if this object is assignable to the given type or the | ||
| 120 | proxied object is | ||
| 121 | Returntype : Boolean; performs same as a normal can | ||
| 122 | Exceptions : None | ||
| 123 | Caller : caller | ||
| 124 | Status : status | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | |||
| 129 | sub isa { | ||
| 130 | my ($self, $class) = @_; | ||
| 131 | return 1 if $self->SUPER::isa($class); | ||
| 132 | return 1 if $self->__proxy()->isa($class); | ||
| 133 | return 0; | ||
| 134 | } | ||
| 135 | |||
| 136 | =head2 can | ||
| 137 | |||
| 138 | Args : Method name to test | ||
| 139 | Example : $obj->can('__proxy'); | ||
| 140 | Description : Overriden to provide C<can()> support for proxies. Will return | ||
| 141 | true if this object implements the given method or the | ||
| 142 | proxied object can | ||
| 143 | Returntype : Code; performs same as a normal can | ||
| 144 | Exceptions : None | ||
| 145 | Caller : caller | ||
| 146 | Status : status | ||
| 147 | |||
| 148 | =cut | ||
| 149 | |||
| 150 | sub can { | ||
| 151 | my ($self, $method) = @_; | ||
| 152 | my $super_can = $self->SUPER::can($method); | ||
| 153 | return $super_can if $super_can; | ||
| 154 | my $proxy_can = $self->__proxy()->can($method); | ||
| 155 | return $proxy_can if $proxy_can; | ||
| 156 | return; | ||
| 157 | } | ||
| 158 | |||
| 159 | =head2 DESTROY | ||
| 160 | |||
| 161 | Example : - | ||
| 162 | Description : Provided because of AutoLoad | ||
| 163 | Returntype : None | ||
| 164 | Exceptions : None | ||
| 165 | Caller : - | ||
| 166 | Status : - | ||
| 167 | |||
| 168 | =cut | ||
| 169 | |||
| 170 | |||
| 171 | |||
| 172 | sub DESTROY { | ||
| 173 | # left blank | ||
| 174 | } | ||
| 175 | |||
| 176 | =head2 AUTOLOAD | ||
| 177 | |||
| 178 | Example : - | ||
| 179 | Description : Performs calls to C<__resolver()> and installs the subroutine | ||
| 180 | into the current package scope. | ||
| 181 | Returntype : None | ||
| 182 | Exceptions : Thrown if C<__resolver()> could not return a subroutine | ||
| 183 | Caller : - | ||
| 184 | Status : - | ||
| 185 | |||
| 186 | =cut | ||
| 187 | |||
| 188 | sub AUTOLOAD { | ||
| 189 | my ($self, @args) = @_; | ||
| 190 | my ($package_name, $method_name) = $AUTOLOAD =~ m/ (.*) :: (.*) /xms; | ||
| 191 | my $sub = $self->__resolver($package_name, $method_name, @args); | ||
| 192 | if(! $sub) { | ||
| 193 | my $type = ref $self ? 'object' : 'class'; | ||
| 194 | throw qq{Can't locate $type method "$method_name" via package "$package_name". No subroutine was generated}; | ||
| 195 | } | ||
| 196 | { | ||
| 197 | no strict 'refs'; ## no critic ProhibitNoStrict | ||
| 198 | *{$AUTOLOAD} = $sub; | ||
| 199 | } | ||
| 200 | goto &$sub; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub __resolver { | ||
| 204 | my ($self, $package_name, $method, @args) = @_; | ||
| 205 | #override to provide the subroutine to install | ||
| 206 | throw "Unimplemented __resolver() in $package_name. Please implement"; | ||
| 207 | } | ||
| 208 | |||
| 209 | 1; | ||
| 210 | |||
| 211 | __END__ |