1 package alt.jiapi.util; 2 3 import java.lang.reflect.Constructor ; 4 import java.lang.reflect.Field ; 5 import java.lang.reflect.InvocationTargetException ; 6 import java.util.StringTokenizer ; 7 8 import org.apache.log4j.Category; 9 10 import alt.jiapi.instrumentor.ChainInstrumentor; 11 import alt.jiapi.instrumentor.InstrumentorChain; 12 import alt.jiapi.JiapiException; 13 import alt.jiapi.Runtime; 14 15 import alt.jiapi.instrumentor.Strategy; 16 import alt.jiapi.instrumentor.Hook; 17 18 93 public class ChainBuilder { 94 private static Category log = Runtime.getLogCategory(ChainBuilder.class); 95 96 private static final Class instrumentorClass = ChainInstrumentor.class; 97 private static final Class strategyClass = Strategy.class; 98 private static final Class hookClass = Hook.class; 99 100 private SymbolMap map; 101 102 105 public static void main(String [] args) throws Exception { 106 SymbolMap map = new SymbolMap("alt.jiapi.instrumentor"); 107 108 ChainBuilder cb = new ChainBuilder(map); 109 InstrumentorChain chain = cb.createChain(args[0]); 110 System.out.println(chain); 111 } 112 113 public ChainBuilder() { 114 this.map = new SymbolMap("alt.jiapi.instrumentor"); 115 } 116 117 public ChainBuilder(SymbolMap map) { 118 this.map = map; 119 } 120 121 124 public InstrumentorChain createChain(String chainSpec) throws JiapiException, ClassNotFoundException , InstantiationException { 125 return createChain(chainSpec, map); 126 } 127 128 131 public InstrumentorChain createChain(String chainSpec, SymbolMap symbols) throws JiapiException, ClassNotFoundException , InstantiationException { 132 InstrumentorChain chain = new InstrumentorChain(); 133 134 StringTokenizer st = new StringTokenizer (chainSpec, "|"); 135 while (st.hasMoreTokens()) { 136 String instrumentorSpec = st.nextToken().trim(); 137 138 ChainInstrumentor i = createInstrumentor(instrumentorSpec, symbols); 139 if (i == null) { 140 throw new JiapiException("ChainBuilder.createInstrumentor(...) returned null"); 141 } 142 143 chain.add(i); 144 } 145 146 return chain; 147 } 148 149 164 public ChainInstrumentor createInstrumentor(String instrumentorSpec, 165 SymbolMap symbols) throws JiapiException, ClassNotFoundException , InstantiationException { 166 log.debug("Creating Instrumentor from spec: " + instrumentorSpec); 167 168 ChainInstrumentor instrumentor = null; 169 170 String instrumentorClassName = null; 171 String childClassName = null; 172 String childHints = null; 173 174 StringTokenizer st = new StringTokenizer (instrumentorSpec, "#"); 175 if (!st.hasMoreTokens()) { 176 throw new JiapiException("Illegal insrumentorSpec " + 177 instrumentorSpec); 178 } 179 180 String instrumentorSymbol = st.nextToken().trim(); 182 instrumentorClassName = symbols.getSymbol(instrumentorSymbol); 183 if (instrumentorClassName == null) { 184 instrumentorClassName = instrumentorSymbol; 185 } 186 187 if (st.hasMoreTokens()) { 188 String childSymbol = st.nextToken().trim(); 189 childClassName = symbols.getSymbol(childSymbol); 190 if (childClassName == null) { 191 childClassName = childSymbol; 192 } 193 } 194 195 if (st.hasMoreTokens()) { 196 childHints = st.nextToken().trim(); 197 } 198 199 200 Class instrumentorClass = null; 202 Class childClass = null; 203 Object child = null; 204 205 instrumentorClass = Class.forName(instrumentorClassName); 206 if (childClassName != null) { 207 try { 208 childClass = Class.forName(childClassName); 209 child = createChild(childClass, childHints); 210 } 211 catch (ClassNotFoundException e) { 212 child = childClassName; } 217 } 218 219 instrumentor = createInstrumentor(instrumentorClass, child); 220 221 log.info("Created instrumentor: " + instrumentor); 222 return instrumentor; 223 } 224 225 226 private Object createChild(Class child, String childHint) throws InstantiationException { 227 System.out.println("Creating child : " + child); 228 Object childObject = null; 229 log.debug("Creating child: " + child.getName() + ", hints " + 230 childHint); 231 232 if (childHint == null) { 233 try { 235 childObject = child.newInstance(); 236 } 237 catch (IllegalAccessException iae) { 238 throw new InstantiationException ("Illegal access: " + 239 iae.getMessage()); 240 } 241 } 242 else { 243 Field hintField = null; 246 try { 247 hintField = child.getField(childHint); 248 } 249 catch (Exception e) { 250 log.debug("Exception occured: " + e.getMessage()); 251 } 252 253 if (hintField != null) { 254 log.debug("hint field: " + hintField); 255 Class [] parameterTypes = new Class [1]; 258 parameterTypes[0] = hintField.getType(); 259 try { 260 Constructor c = child.getConstructor(parameterTypes); 261 log.debug("Child constructor: " + c); 262 263 Object [] params = new Object [1]; 264 params[0] = new Integer (hintField.getInt(null)); 266 childObject = c.newInstance(params); 267 } 268 catch (NoSuchMethodException nsme) { 269 throw new InstantiationException ("No such method: " + 270 nsme.getMessage()); 271 } 272 catch (IllegalAccessException iae) { 273 throw new InstantiationException ("Illegal access: " + 274 iae.getMessage()); 275 } 276 catch (InvocationTargetException ite) { 277 throw new InstantiationException ("Exception in constructor: "+ 278 ite.getMessage()); 279 } 280 281 } 282 } 283 284 return childObject; 285 } 286 287 private ChainInstrumentor createInstrumentor(Class instrumentorClass, Object child) throws InstantiationException { 288 log.debug("Creating Instrumentor: " + instrumentorClass.getName() + 289 ", child: " + child); 290 291 ChainInstrumentor instrumentor = null; 292 293 if (child == null) { 294 try { 296 instrumentor = (ChainInstrumentor)instrumentorClass.newInstance(); 297 } 298 catch (IllegalAccessException iae) { 299 throw new InstantiationException ("Illegal access: " + 300 iae.getMessage()); 301 } 302 } 303 else { 304 309 Class [] parameterTypes = new Class [1]; 311 if (instrumentorClass.isAssignableFrom(child.getClass())) { 312 parameterTypes[0] = instrumentorClass; 313 } 314 else if (strategyClass.isAssignableFrom(child.getClass())) { 315 parameterTypes[0] = strategyClass; 316 } 317 else if (hookClass.isAssignableFrom(child.getClass())){ 318 parameterTypes[0] = hookClass; 319 } 320 else { 321 parameterTypes[0] = child.getClass(); 322 } 323 324 try { 327 Constructor c = 328 instrumentorClass.getConstructor(parameterTypes); 329 330 log.debug("Using constructor: " + c); 331 332 Object [] params = new Object [] {child}; 333 instrumentor = (ChainInstrumentor)c.newInstance(params); 334 } 335 catch (NoSuchMethodException nsme) { 336 throw new InstantiationException ("No such method: " + 338 nsme.getMessage()); 339 } 340 catch (IllegalAccessException iae) { 341 throw new InstantiationException ("Illegal access: " + 342 iae.getMessage()); 343 } 344 catch (InvocationTargetException ite) { 345 throw new InstantiationException ("Exception in constructor: "+ 346 ite.getMessage()); 347 } 348 } 349 350 return instrumentor; 351 } 352 } 353 | Popular Tags |