KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > naming > BindingWrapper


1 /*
2   Copyright (C) 2001 Renaud Pawlak <renaud@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.naming;
19
20 import org.aopalliance.intercept.ConstructorInvocation;
21 import org.aopalliance.intercept.MethodInvocation;
22 import org.objectweb.jac.core.*;
23 import org.objectweb.jac.wrappers.ForwardingWrapper;
24
25 /**
26  * This wrapper class binds an object to the actual named object by
27  * forwarder.
28  *
29  * <p><code>BindingWrapper</code> wraps the JAC object that that has not
30  * been resolved yet by the binding aspect. When a call is performed
31  * on the wrappee, the binder wrapper gets the actual JAC object that
32  * corresponds to the logical name by asking the name repository of
33  * the naming aspect. Then, it creates a forwarding wrapper to this
34  * object and replaces itself with it.
35  *
36  * <p>The binding aspect uses the naming aspect. Do not try to use it
37  * alone.
38  *
39  * @author <a HREF="http://cedric.cnam.fr/~pawlak/index-english.html">Renaud Pawlak</a>
40  *
41  * @see ForwardingWrapper */

42
43 public class BindingWrapper extends Wrapper {
44
45     /**
46      * Construct a new binding wrapper.
47      *
48      * @param logicalName the name of the wrappee within the naming
49      * aspect */

50
51     public BindingWrapper(AspectComponent ac, String JavaDoc logicalName) {
52         super(ac);
53         this.logicalName = logicalName;
54     }
55
56     /** The name of the Jac object the binber must bind to. */
57     protected String JavaDoc logicalName;
58
59     /**
60      * The getter method for the Jac object's name (role method).
61      *
62      * @return the name
63      */

64
65     public String JavaDoc getLogicalName() {
66         return logicalName;
67     }
68
69     /**
70      * This wrapping method binds the wrappee.
71      *
72      * <p>Binds the wrappee to its actual location by creating a new
73      * forwarder wrapper to wrap it. This wrapping method is called
74      * only once since the binder wrapper unwraps itself once the new
75      * wrapper is created.
76      *
77      * @see ForwardingWrapper */

78
79     public Object JavaDoc bind(Interaction interaction) throws BindingErrorException {
80
81         /** Get the name repository */
82         NameRepository repository = (NameRepository) NameRepository.get();
83
84         if (repository == null) {
85             throw new BindingErrorException("Binding aspect cannot work without the naming aspect.");
86         }
87
88         /** Get the actual reference of the object by invoking the
89             repository. */

90
91         Object JavaDoc object = repository.getObject(logicalName);
92
93         //if (true) return null;
94

95         if (object == null) {
96             throw new BindingErrorException(
97                 "Object '" + logicalName + "' not found in the repository.");
98         }
99
100         // RemoteRef rr = null;
101
Object JavaDoc to_forward = null;
102
103         /** If their is several objects for the name, then it is a
104             replicated object. We try to bind to the local one if
105             exist. It not, we will bind to the first one. This is the
106             only interaction with the distribution aspect. */

107
108         // if (objects.length > 1) {
109
// for (int i = 0; i < objects.length; i++) {
110
// if ( ! ((Wrappee)objects[i]).isWrappedByClass ( StubWrapper.class ) ) {
111
// to_forward = objects[i];
112
// } else {
113
// if ( ((RemoteRef) ((Wrappee)objects[i]).invokeRoleMethod (
114
// "org.objectweb.jac.dist.StubWrapper", "getRemoteRef", new Class[] {}, new Object[] {} ))
115
// . getRemCont().isLocal () ) {
116
// to_forward = objects[i];
117
// }
118
// }
119
// }
120
// }
121

122         // if ( to_forward == null ) {
123
// to_forward = objects[0];
124
// }
125

126         /** We replace the binder with a forwarder. */
127
128         ForwardingWrapper forwarder =
129             new ForwardingWrapper(getAspectComponent(), object);
130         Wrapping.unwrapAll(interaction.wrappee, null, this);
131         Wrapping.wrapAll(interaction.wrappee, null, forwarder);
132
133         /** We redo the call (that will be forwarded this time */
134         Object JavaDoc ret =
135             interaction.method.invoke(interaction.wrappee, interaction.args);
136         return ret;
137
138     }
139
140     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
141         return bind((Interaction) invocation);
142     }
143
144     public Object JavaDoc construct(ConstructorInvocation invocation)
145         throws Throwable JavaDoc {
146         throw new Exception JavaDoc("Wrapper "+this+" does not support construction interception.");
147     }
148
149 }
150
Popular Tags