KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > interceptor > Interceptor


1 /***
2  * Julia: France Telecom's implementation of the Fractal API
3  * Copyright (C) 2001-2002 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package interceptor;
25
26 import org.objectweb.fractal.api.Component;
27 import org.objectweb.fractal.api.NoSuchInterfaceException;
28 import org.objectweb.fractal.api.Type;
29 import org.objectweb.fractal.api.Interface;
30 import org.objectweb.fractal.api.control.LifeCycleController;
31 import org.objectweb.fractal.api.control.BindingController;
32
33 import org.objectweb.fractal.api.factory.GenericFactory;
34 import org.objectweb.fractal.api.type.TypeFactory;
35 import org.objectweb.fractal.api.type.ComponentType;
36 import org.objectweb.fractal.api.type.InterfaceType;
37
38 import org.objectweb.fractal.julia.control.binding.Util;
39
40 import org.objectweb.fractal.util.Fractal;
41
42 import stat.StatController;
43
44 import java.util.Map JavaDoc;
45 import java.util.HashMap JavaDoc;
46 import java.util.Set JavaDoc;
47 import java.util.Iterator JavaDoc;
48
49 public class Interceptor {
50
51   public static void main (final String JavaDoc[] args) throws Exception JavaDoc {
52     Component boot = Fractal.getBootstrapComponent();
53
54     TypeFactory tf = Fractal.getTypeFactory(boot);
55     ComponentType cType = tf.createFcType(new InterfaceType[] {
56       tf.createFcItfType("s", "interceptor.I", false, false, false)
57     });
58
59     GenericFactory cf = Fractal.getGenericFactory(boot);
60     Component cComp = cf.newFcInstance(
61         cType, "statPrimitive", "interceptor.IImpl");
62     Component rComp = cf.newFcInstance(
63         cType, "composite", null);
64     Fractal.getContentController(rComp).addFcSubComponent(cComp);
65     Fractal.getBindingController(rComp).bindFc("s", cComp.getFcInterface("s"));
66     Fractal.getLifeCycleController(rComp).startFc();
67
68     System.err.println("INITIAL CONFIGURATION");
69     test(rComp);
70
71     // adds a trace aspect in the interceptor
72
System.err.println("CHANGE THE INTERCEPTOR (ADD A TRACE ASPECT)");
73     cComp = changeComponent(cType, "statTracePrimitive", cComp);
74     test(rComp);
75     System.err.println("(note that the StatController state has been lost)\n");
76
77     // completely removes the interceptor and the controller
78
System.err.println("REMOVE THE INTERCEPTOR AND THE CONTROLLER");
79     cComp = changeComponent(cType, "emptyPrimitive", cComp);
80     test(rComp);
81
82     // adds the interceptor back
83
System.err.println("ADD BACK THE INTERCEPTOR AND THE CONTROLLER");
84     cComp = changeComponent(cType, "statPrimitive", cComp);
85     test(rComp);
86
87     // -----
88

89     // tests field interceptors (changeComponent can be used, but will not
90
// preserve the component's state, since the content and controller part are
91
// merged in 'fullStatPrimitive' components)
92
System.err.println("CREATE A NEW COMPONENT WITH A FIELD STAT CONTROLLER");
93     cComp = cf.newFcInstance(
94         cType, "fullStatPrimitive", "interceptor.IImpl");
95     rComp = cf.newFcInstance(
96         cType, "composite", null);
97     Fractal.getContentController(rComp).addFcSubComponent(cComp);
98     Fractal.getBindingController(rComp).bindFc("s", cComp.getFcInterface("s"));
99     Fractal.getLifeCycleController(rComp).startFc();
100     test(rComp);
101   }
102
103   private static void test (final Component rComp)
104     throws NoSuchInterfaceException
105   {
106     System.err.println();
107
108     I i = (I)rComp.getFcInterface("s");
109     i.m("a");
110     i.m("b");
111     try {
112       i.m(null);
113     } catch (NullPointerException JavaDoc e) {
114     }
115     i.m("c");
116
117     System.err.println();
118
119     StatController sc;
120     try {
121       Component c = Fractal.getContentController(rComp).getFcSubComponents()[0];
122       sc = (StatController)c.getFcInterface("stat-controller");
123     } catch (NoSuchInterfaceException e) {
124       System.err.println("No StatController!");
125       System.err.println();
126       return;
127     }
128
129     System.err.println("number of calls: " + sc.getNumberOfMethodCall());
130     System.err.println("number of success: " + sc.getNumberOfMethodSuccess());
131     System.err.println("number of reads: " + sc.getNumberOfFieldRead());
132     System.err.println("number of writes: " + sc.getNumberOfFieldWrite());
133     System.err.println("total execution time: " + sc.getTotalExecutionTime());
134     System.err.println();
135   }
136
137   /**
138    * Changes the type, the controller objects and the interceptors of the given
139    * primitive component. In fact a new component is created, with "physically"
140    * the same content as the old component, the old component is unbound and
141    * removed, and the new one is added and bound like the old one.
142    *
143    * @param newType the new component type.
144    * @param newControllerDesc the new controller descriptor.
145    * @param component the component that must be replaced.
146    * @return the new component.
147    * @throws Exception if a problem occurs
148    */

149
150   public static Component changeComponent (
151     final Type newType,
152     final Object JavaDoc newControllerDesc,
153     final Component component) throws Exception JavaDoc
154   {
155     // 1 stops "component"
156
boolean isStarted = false;
157     try {
158       LifeCycleController lc = Fractal.getLifeCycleController(component);
159       isStarted = lc.getFcState().equals("STARTED");
160       if (isStarted) {
161         Fractal.getLifeCycleController(component).stopFc();
162       }
163     } catch (NoSuchInterfaceException ignored) {
164     }
165
166     // 2 stops the parent components of "component"
167
Component[] parents;
168     LifeCycleController[] parentLCs;
169     boolean[] parentStates;
170     try {
171       parents = Fractal.getSuperController(component).getFcSuperComponents();
172       parentLCs = new LifeCycleController[parents.length];
173       parentStates = new boolean[parents.length];
174       for (int i = 0; i < parents.length; ++i) {
175         try {
176           parentLCs[i] = Fractal.getLifeCycleController(parents[i]);
177           parentStates[i] = parentLCs[i].getFcState().equals("STARTED");
178           if (parentStates[i]) {
179             parentLCs[i].stopFc();
180           }
181         } catch (NoSuchInterfaceException ignored) {
182         }
183       }
184     } catch (NoSuchInterfaceException e) {
185       parents = new Component[0];
186       parentLCs = null;
187       parentStates = null;
188     }
189
190     // 3 unbinds "component"
191
Map JavaDoc bindingsFrom = new HashMap JavaDoc();
192     BindingController bc;
193     try {
194       bc = Fractal.getBindingController(component);
195     } catch (NoSuchInterfaceException e) {
196       bc = null;
197     }
198     if (bc != null) {
199       String JavaDoc[] names = bc.listFc();
200       for (int i = 0; i < names.length; ++i) {
201         bindingsFrom.put(names[i], bc.lookupFc(names[i]));
202         bc.unbindFc(names[i]);
203       }
204     }
205
206     // 4 removes the bindings to "component"
207
Map JavaDoc bindingsTo = new HashMap JavaDoc();
208     Object JavaDoc[] itfs = component.getFcInterfaces();
209     for (int i = 0; i < itfs.length; ++i) {
210       Interface itf = (Interface)itfs[i];
211       if (!((InterfaceType)itf.getFcItfType()).isFcClientItf()) {
212         Set JavaDoc clients = Util.getFcClientItfsBoundTo(itf);
213         Iterator JavaDoc it = clients.iterator();
214         while (it.hasNext()) {
215           Interface citf = (Interface)it.next();
216           Fractal.getBindingController(
217             citf.getFcItfOwner()).unbindFc(citf.getFcItfName());
218           bindingsTo.put(citf, itf.getFcItfName());
219         }
220       }
221     }
222
223     // 5 removes "component" from its parent components
224
for (int i = 0; i < parents.length; ++i) {
225       Fractal.getContentController(parents[i]).removeFcSubComponent(component);
226     }
227
228     // creates the new component with the current component's content
229
// (this part is the only Julia specific part)
230
Object JavaDoc impl = component.getFcInterface("/content");
231     Component boot = Fractal.getBootstrapComponent();
232     GenericFactory gf = Fractal.getGenericFactory(boot);
233     Component newComponent = gf.newFcInstance(newType, newControllerDesc, impl);
234
235     // 5' adds "newComponent" to the parent components
236
for (int i = 0; i < parents.length; ++i) {
237       Fractal.getContentController(parents[i]).addFcSubComponent(newComponent);
238     }
239
240     // 4' adds the bindings to "newComponent"
241
Iterator JavaDoc it = bindingsTo.entrySet().iterator();
242     while (it.hasNext()) {
243       Map.Entry JavaDoc e = (Map.Entry JavaDoc)it.next();
244       Interface clientItf = (Interface)e.getKey();
245       String JavaDoc serverItf = (String JavaDoc)e.getValue();
246       Fractal.getBindingController(clientItf.getFcItfOwner()).bindFc(
247         clientItf.getFcItfName(), newComponent.getFcInterface(serverItf));
248     }
249
250     // 3' binds "newComponent"
251
try {
252       bc = Fractal.getBindingController(newComponent);
253     } catch (NoSuchInterfaceException e) {
254       bc = null;
255     }
256     if (bc != null) {
257       Iterator JavaDoc i = bindingsFrom.entrySet().iterator();
258       while (i.hasNext()) {
259         Map.Entry JavaDoc e = (Map.Entry JavaDoc)i.next();
260         String JavaDoc clientItf = (String JavaDoc)e.getKey();
261         Object JavaDoc serverItf = e.getValue();
262         bc.bindFc(clientItf, serverItf);
263       }
264     }
265
266     // 2' starts the parent components of "newComponent"
267
for (int i = 0; i < parents.length; ++i) {
268       if (parentStates[i]) {
269         parentLCs[i].startFc();
270       }
271     }
272
273     // 1' starts "newComponent"
274
if (isStarted) {
275       try {
276         Fractal.getLifeCycleController(newComponent).startFc();
277       } catch (NoSuchInterfaceException ignored) {
278       }
279     }
280
281     // returns the new component
282
return newComponent;
283   }
284 }
285
Popular Tags