1 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 ; 45 import java.util.HashMap ; 46 import java.util.Set ; 47 import java.util.Iterator ; 48 49 public class Interceptor { 50 51 public static void main (final String [] args) throws Exception { 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 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 System.err.println("REMOVE THE INTERCEPTOR AND THE CONTROLLER"); 79 cComp = changeComponent(cType, "emptyPrimitive", cComp); 80 test(rComp); 81 82 System.err.println("ADD BACK THE INTERCEPTOR AND THE CONTROLLER"); 84 cComp = changeComponent(cType, "statPrimitive", cComp); 85 test(rComp); 86 87 89 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 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 149 150 public static Component changeComponent ( 151 final Type newType, 152 final Object newControllerDesc, 153 final Component component) throws Exception  154 { 155 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 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 Map bindingsFrom = new HashMap (); 192 BindingController bc; 193 try { 194 bc = Fractal.getBindingController(component); 195 } catch (NoSuchInterfaceException e) { 196 bc = null; 197 } 198 if (bc != null) { 199 String [] 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 Map bindingsTo = new HashMap (); 208 Object [] 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 clients = Util.getFcClientItfsBoundTo(itf); 213 Iterator 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 for (int i = 0; i < parents.length; ++i) { 225 Fractal.getContentController(parents[i]).removeFcSubComponent(component); 226 } 227 228 Object 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 for (int i = 0; i < parents.length; ++i) { 237 Fractal.getContentController(parents[i]).addFcSubComponent(newComponent); 238 } 239 240 Iterator it = bindingsTo.entrySet().iterator(); 242 while (it.hasNext()) { 243 Map.Entry e = (Map.Entry )it.next(); 244 Interface clientItf = (Interface)e.getKey(); 245 String serverItf = (String )e.getValue(); 246 Fractal.getBindingController(clientItf.getFcItfOwner()).bindFc( 247 clientItf.getFcItfName(), newComponent.getFcInterface(serverItf)); 248 } 249 250 try { 252 bc = Fractal.getBindingController(newComponent); 253 } catch (NoSuchInterfaceException e) { 254 bc = null; 255 } 256 if (bc != null) { 257 Iterator i = bindingsFrom.entrySet().iterator(); 258 while (i.hasNext()) { 259 Map.Entry e = (Map.Entry )i.next(); 260 String clientItf = (String )e.getKey(); 261 Object serverItf = e.getValue(); 262 bc.bindFc(clientItf, serverItf); 263 } 264 } 265 266 for (int i = 0; i < parents.length; ++i) { 268 if (parentStates[i]) { 269 parentLCs[i].startFc(); 270 } 271 } 272 273 if (isStarted) { 275 try { 276 Fractal.getLifeCycleController(newComponent).startFc(); 277 } catch (NoSuchInterfaceException ignored) { 278 } 279 } 280 281 return newComponent; 283 } 284 } 285
| Popular Tags
|