1 18 19 package org.objectweb.jac.core; 20 21 22 import java.io.Serializable ; 23 import java.lang.reflect.Array ; 24 import java.net.URL ; 25 import org.apache.log4j.Logger; 26 import org.objectweb.jac.aspects.gui.Length; 27 import org.objectweb.jac.core.rtti.*; 28 import org.objectweb.jac.util.Classes; 29 import org.objectweb.jac.util.Strings; 30 31 45 46 public class ACConfiguration implements Serializable { 47 static Logger logger = Logger.getLogger("jac"); 48 static Logger loggerAspects = Logger.getLogger("aspects"); 49 static Logger loggerConf = Logger.getLogger("aspects.config"); 50 static Logger loggerPerf = Logger.getLogger("perf"); 51 52 53 protected Application application; 54 55 protected String name; 56 59 transient AspectComponent instance = null; 60 61 62 protected URL filePath; 63 64 String acPath = null; 65 66 72 73 public void setURL(URL filePath) { 74 this.filePath = filePath; 75 } 76 77 82 83 public URL getURL() { 84 return filePath; 85 } 86 87 97 98 protected boolean weaveOnDemand = true; 99 100 103 104 public boolean getWeaveOnDemand() { 105 return weaveOnDemand; 106 } 107 108 111 112 public void setWeaveOnDemand(boolean b) { 113 weaveOnDemand = b; 114 } 115 116 130 131 public ACConfiguration(Application application, String name, 132 String filePath, boolean weaveNow) { 133 this.name = name; 134 this.application = application; 135 this.weaveOnDemand = ! weaveNow; 136 if (filePath!=null) { 137 try { 138 if (filePath.startsWith("file:")) { 139 filePath = filePath.substring(5); 140 } 141 this.filePath = new URL ("file:"+filePath); 142 } catch (Exception e) { 143 e.printStackTrace(); 144 } 145 } 146 147 ACManager acm = ACManager.getACM(); 150 if (!acm.isACDeclared(name)) { 151 acm.declareAC(name,name); 152 } 153 } 154 155 162 163 public String getName() { 164 return name; 165 } 166 167 173 174 public void setName(String name) { 175 this.name = name; 176 } 177 178 183 184 public AspectComponent getInstance() { 185 return instance; 186 } 187 188 192 193 public Application getApplication() { 194 return application; 195 } 196 197 201 202 public ClassItem getAspectClass() { 203 String acPath = ((ACManager)ACManager.get()).getACPathFromName(name); 204 return ClassRepository.get().getClass(acPath); 205 } 206 207 212 213 protected AspectComponent instantiate() { 214 loggerAspects.debug("instantiating "+name); 215 ACManager acm = (ACManager)ACManager.get(); 216 try { 220 instance = (AspectComponent) acm.getObject( 221 application.getName()+"."+name); 222 if (instance == null) { 223 if (acPath == null) 224 acPath = acm.getACPathFromName(name); 225 loggerAspects.debug(name + " : " + acPath); 226 if( acPath == null ) return null; 227 Class cl = Class.forName( acPath ); 228 loggerAspects.debug("instantiating "+cl); 229 instance = (AspectComponent) cl.newInstance(); 230 instance.setApplication(getApplication().getName()); 231 } 232 return instance; 233 } catch(Exception e) { 234 e.printStackTrace(); 235 return null; 236 } finally { 237 } 239 } 240 241 250 251 252 protected void configure() { 253 logger.info("--- configuring "+name+" aspect ---"); 254 long start = System.currentTimeMillis(); 255 String [] defaults = instance.getDefaultConfigs(); 256 for (int i=0; i<defaults.length; i++) { 257 instance.configure(name,defaults[i]); 258 } 259 instance.configure(name,filePath.getFile()); 260 instance.whenConfigured(); 261 loggerPerf.info("aspect "+name+" configured in "+ 262 (System.currentTimeMillis()-start)+"ms"); 263 } 264 265 public static Object convertArray(Object [] array, Class componentType, Imports imports) 266 throws Exception 267 { 268 Object result = Array.newInstance(componentType, array.length); 269 for (int i=0; i<array.length; i++) { 270 Array.set(result,i,convertValue(array[i],componentType,imports)); 271 } 272 return result; 273 } 274 275 public static Object convertValue(Object object, Class type) 276 throws Exception 277 { 278 return convertValue(object,type,null); 279 } 280 281 public static Object convertValue(Object object, Class type, Imports imports) 282 throws Exception 283 { 284 Object result = object; 285 if (object != null && object.getClass() != type) { 286 try { 287 if (type.isArray()) { 288 result = convertArray((Object [])object,type.getComponentType(),imports); 289 } else if (type==double.class || type==Double .class) { 290 result = new Double ((String )object); 291 } else if (type==int.class || type==Integer .class) { 292 result = new Integer ((String )object); 293 } else if (type==long.class || type==Long .class) { 294 result = new Long ((String )object); 295 } else if (type==float.class || type==Float .class) { 296 result = new Float ((String )object); 297 } else if (type==boolean.class || type==Boolean .class) { 298 result = Boolean.valueOf((String )object); 299 } else if (type==short.class || type==Short .class) { 300 result = new Short ((String )object); 301 } else if (type==Class .class) { 302 result = Class.forName((String )object); 303 } else if (type==ClassItem.class) { 304 result = getClass((String )object,imports); 305 } else if (type==VirtualClassItem.class) { 306 result = ClassRepository.get().getVirtualClassStrict((String )object); 307 } else if (AbstractMethodItem.class.isAssignableFrom(type)) { 308 String str = (String )object; 309 int index = str.indexOf("("); 310 try { 311 ClassItem classItem; 312 if (index==-1) { 313 classItem = getClass(str,imports); 314 result = classItem.getConstructor(""); 315 } else { 316 classItem = getClass( 317 str.substring(0,index),imports); 318 319 String [] paramTypes = 321 Strings.split(str.substring(index+1,str.length()-1), ","); 322 resolveTypes(paramTypes,imports); 323 String fullName = str.substring(0,index+1)+Strings.join(paramTypes,",")+")"; 324 result = classItem.getConstructor(fullName.substring(index)); 325 } 326 } catch (NoSuchClassException e) { 327 if (index!=-1) { 328 String [] paramTypes = 330 Strings.split(str.substring(index+1,str.length()-1), ","); 331 resolveTypes(paramTypes,imports); 332 str = str.substring(0,index+1)+Strings.join(paramTypes,",")+")"; 333 index = str.lastIndexOf(".",index); 334 } else { 335 index = str.lastIndexOf("."); 336 } 337 if (index!=-1) { 338 try { 339 ClassItem classItem = getClass( 340 str.substring(0,index),imports); 341 result = classItem.getAbstractMethod( 342 str.substring(index+1)); 343 } catch (NoSuchClassException e2) { 344 throw new Exception ("Failed to convert "+str+ 345 " into a "+type.getName()); 346 } 347 } else { 348 throw new Exception ("Failed to convert "+str+ 349 " into a "+type.getName()); 350 } 351 } 352 } else if (FieldItem.class.isAssignableFrom(type)) { 353 String str = (String )object; 354 loggerConf.debug("Trying to convert "+str+" into a FieldItem"); 355 int index = str.length(); 356 result = null; 357 while (index!=-1 && result==null) { 358 index = str.lastIndexOf(".",index-1); 359 if (index!=-1) { 360 try { 361 loggerConf.debug( 362 " Trying class="+str.substring(0,index)+ 363 " and field="+str.substring(index+1)); 364 ClassItem classItem = getClass( 365 str.substring(0,index),imports); 366 result = classItem.getField(str.substring(index+1)); 367 loggerConf.debug(" -> "+result); 368 } catch (NoSuchClassException e) { 369 loggerConf.info( 370 " Failed conversion of "+object+ 371 " to FieldItem with class="+str.substring(0,index)+ 372 " and field="+str.substring(index+1)); 373 } 374 } 375 } 376 if (index==-1 || result==null) { 377 throw new Exception ("Failed to convert "+str+ 378 " into a "+type.getName()); 379 } 380 } else if (MemberItem.class.isAssignableFrom(type)) { 381 String str = (String )object; 382 int index = -1; 383 int paren = str.indexOf("("); 384 if (paren==-1) 385 index = str.length(); 386 else 387 index = paren; 388 389 result = null; 390 while (index!=-1 && result==null) { 391 index = str.lastIndexOf(".",index-1); 392 if (index!=-1) { 393 try { 394 ClassItem classItem = getClass( 395 str.substring(0,index),imports); 396 result = classItem.getMember(str.substring(index+1)); 397 } catch (NoSuchClassException e) { 398 } 399 } 400 } 401 402 if (index==-1 || result==null) { 403 throw new Exception ("Failed to convert "+str+ 404 " into a "+type.getName()); 405 } 406 } else if (type == Length.class) { 407 return new Length((String )object); 408 } else { 409 throw new Exception ("Don't know how to convert "+object+" into a "+type); 410 } 411 } catch (Exception e) { 412 loggerConf.info("Failed to convert "+object+" into "+type.getName(),e); 413 throw new Exception ( 414 "Failed to convert "+object+" into "+type.getName()+" : "+e); 415 } 416 } 417 loggerConf.debug("Converted "+object+" into "+type+": "+result); 418 return result; 419 } 420 421 protected static void resolveTypes(String [] types, Imports imports) { 422 for (int i=0; i<types.length; i++) { 423 String type = types[i]; 424 boolean isArray = false; 425 if (type.endsWith("[]")) { 426 type = type.substring(0,type.length()-2); 427 } 428 try { 429 if (isArray) 430 types[i] = 431 imports.getClass( 432 types[i].substring(0,types[i].length()-2)).getName()+"[]"; 433 else 434 types[i] = imports.getClass(types[i]).getName(); 435 } catch (NoSuchClassException nse) { 436 if (!Classes.isPrimitiveType(type)) 437 throw nse; 438 } 439 } 440 } 441 442 protected static ClassItem getClass(String name, Imports imports) { 443 if (imports!=null) { 444 try { 445 return imports.getClass(name); 446 } catch (NoSuchClassException e) { 447 return ClassRepository.get().getClass(name); 448 } 449 } else { 450 return ClassRepository.get().getClass(name); 451 } 452 } 453 454 461 public void weave() { 462 instantiate(); 464 if (instance == null) { 465 logger.error("could not instantiate aspect "+name); 466 return; 467 } 468 try { 469 instance.beforeConfiguration(); 470 configure(); 471 } catch (Exception e) { 472 e.printStackTrace(); 473 return; 474 } 475 loggerAspects.debug( 476 "registering " + instance + 477 " on application " + getApplication() ); 478 instance.doRegister(); 480 ACManager.get().register( getApplication().getName() + 481 "." + name, instance ); 482 } 483 484 488 public void unweave() { 489 instance.doUnregister(); 491 ((ACManager)ACManager.get()).unregister( application.getName() + "." + name ); 492 instance = null; 493 } 494 495 499 public String toString() { 500 return "AC " + name + " configuration"; 501 } 502 503 } 504 | Popular Tags |