1 18 19 package org.objectweb.jac.core; 20 21 import java.io.FileInputStream ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.StreamTokenizer ; 25 import java.io.StringReader ; 26 import java.lang.NoSuchMethodException ; 27 import java.lang.reflect.InvocationTargetException ; 28 import java.lang.reflect.Method ; 29 import java.lang.reflect.Modifier ; 30 import java.net.URLClassLoader ; 31 import java.util.Arrays ; 32 import java.util.HashMap ; 33 import java.util.Hashtable ; 34 import java.util.Properties ; 35 import java.util.StringTokenizer ; 36 import org.apache.log4j.Logger; 37 import org.objectweb.jac.core.rtti.*; 38 import org.objectweb.jac.util.*; 39 40 45 46 public class ApplicationRepository { 47 static Logger logger = Logger.getLogger("jac"); 48 static Logger loggerCP = Logger.getLogger("classpath"); 49 static Logger loggerAspects = Logger.getLogger("aspects"); 50 51 static Hashtable owningApplications = new Hashtable (); 52 53 public static Application getOwningApplication(Wrappee wrappee, 54 ClassItem cl) { 55 if (wrappee!=null) 56 return (Application)owningApplications.get(wrappee); 57 else if (cl!=null) 58 return (Application)owningApplications.get(cl); 59 else 60 return null; 61 } 62 63 public static String getOwningApplicationName(Wrappee wrappee, 64 ClassItem cl) { 65 Application app = getOwningApplication(wrappee,cl); 66 if (app!=null) 67 return app.getName(); 68 else 69 return null; 70 } 71 72 public static void setOwningApplication(Wrappee wrappee,ClassItem cl, 73 Application application) { 74 if(wrappee!=null) 75 owningApplications.put(wrappee,application); 76 else if(cl!=null) 77 owningApplications.put(cl,application); 78 } 79 80 HashMap applications = new HashMap (); 81 82 static ApplicationRepository applicationRepository; 83 84 88 89 public static Application getCurrentApplication() { 90 String appName = (String )Collaboration.get().getCurApp(); 91 if( appName == null ) return null; 92 return (Application)get().applications.get(appName); 93 } 94 95 99 100 public static void launchProgram(String [] args) { 101 InputStream fis = null; 102 Properties applicationDescriptor = new Properties (); 103 104 try { 105 fis = new FileInputStream (args[0]); 106 applicationDescriptor.load(fis); 107 } catch(Exception e) { 108 ClassLoader loader = ApplicationRepository.class.getClassLoader(); 109 110 loggerCP.debug("trying to load "+args[0]+" with "+loader); 111 fis = loader.getResourceAsStream(args[0]); 112 if (fis == null) { 113 logger.error("cannot find application descriptor "+args[0]); 114 if (loader instanceof URLClassLoader ) 115 logger.error(" classpath="+Arrays.asList(((URLClassLoader )loader).getURLs())); 116 return; 117 } 118 try { 119 applicationDescriptor.load(fis); 120 } catch (IOException ioe) { 121 logger.error("failed to read applicationDescriptor descriptor "+ 122 args[0]+": "+ioe); 123 return; 124 } 125 } 126 127 ClassLoader loader = ApplicationRepository.class.getClassLoader(); 130 try { 131 loader.getClass().getMethod("readProperties", 132 new Class [] {Properties .class}) 133 .invoke(loader,new Object [] {applicationDescriptor}); 134 } catch (Exception e) { 135 logger.error("failed to read JAC properties from application descriptor "+args[0]); 136 } 137 138 JacPropLoader.addProps(applicationDescriptor); 139 140 String applicationName = 141 applicationDescriptor.getProperty("applicationName"); 142 String launchingClass = 143 applicationDescriptor.getProperty("launchingClass"); 144 String aspects = applicationDescriptor.getProperty("aspects"); 145 String topology = applicationDescriptor.getProperty("topology"); 146 147 if (applicationName==null) { 148 logger.warn("bad application descriptor "+args[0]); 149 return; 150 } 151 152 logger.info("launching application "+applicationName); 153 154 if (topology!=null) { 156 Class remoteContainerClass = null; 157 try { 158 remoteContainerClass = 159 Class.forName("org.objectweb.jac.core.dist.RemoteContainer"); 160 } catch (ClassNotFoundException e) { 161 logger.fatal("Could not find class org.objectweb.jac.core.dist.RemoteContainer"); 162 System.exit(1); 163 } 164 StringTokenizer st1 = new StringTokenizer (topology); 165 while (st1.hasMoreElements()) { 166 String container = (String )st1.nextElement(); 167 try { 168 remoteContainerClass.getMethod("bindNewContainer", 169 new Class [] {String .class}) 170 .invoke(null,new Object [] {container}); 171 } catch (InvocationTargetException e) { 172 logger.error("Failed to bind to "+container+": "+ 173 e.getTargetException().getMessage()); 174 } catch (Exception e) { 175 logger.error("Failed to bind to "+container+": "+e.getMessage()); 176 } 177 } 178 logger.debug("application topology successfully bound"); 179 } 180 181 Application app = new Application(applicationName,null, 183 launchingClass,null); 184 185 if (aspects!=null) { 187 StreamTokenizer tokens = 188 new StreamTokenizer (new StringReader (aspects)); 189 tokens.wordChars('/','/'); 190 tokens.wordChars('_','_'); 191 tokens.wordChars('-','-'); 192 tokens.wordChars(':',':'); 193 tokens.wordChars('$','$'); 194 char fileSep = System.getProperty("file.separator").charAt(0); 195 tokens.wordChars(fileSep,fileSep); 196 try { 197 while (tokens.nextToken()!=StreamTokenizer.TT_EOF) { 198 String ac_name = tokens.sval; 199 tokens.nextToken(); 200 String ac_path = tokens.sval; 201 tokens.nextToken(); 202 String ac_weave = tokens.sval; 203 loggerAspects.info( 204 "adding aspect "+ac_name+"["+ac_path+ 205 "]:"+ac_weave+" to "+app); 206 app.addAcConfiguration( 207 new ACConfiguration(app, ac_name, 208 ac_path, 209 (ac_weave.equals("true")?true:false))); 210 } 211 } catch (IOException e) { 212 e.printStackTrace(); 213 } 214 } 215 216 logger.debug("adding application "+app); 217 ApplicationRepository.get().addApplication(app); 218 219 try { 220 String [] appargs = new String [args.length - 1]; 221 if (appargs.length > 0) { 222 System.arraycopy(args, 1, appargs, 0, args.length - 1); 223 } 224 Class lc = Class.forName(launchingClass); 225 try { 226 Method mainMethod = lc.getMethod("main",new Class [] {String [].class}); 227 if (!Modifier.isStatic(mainMethod.getModifiers())) { 228 logger.error("main method of class "+launchingClass+" is not static"); 229 return; 230 } 231 mainMethod.invoke(null,new Object [] {appargs}); 232 } catch (NoSuchMethodException e) { 233 logger.error("No such method "+launchingClass+".main(String[])"); 234 return; 235 } 236 } catch(Exception e) { 237 logger.error(applicationName+" launch failed",e); 238 } 239 ((ACManager)ACManager.get()).afterApplicationStarted(); 240 } 241 242 247 248 public static void main(String [] args) throws Throwable { 249 if (applicationRepository == null) 250 applicationRepository = new ApplicationRepository(); 251 launchProgram(args); 252 } 253 254 public ApplicationRepository() { 255 } 256 257 262 263 public static ApplicationRepository get() { 264 if (applicationRepository == null) { 265 applicationRepository = new ApplicationRepository(); 266 } 267 return applicationRepository; 268 } 269 270 277 278 public void addApplication(Application app) { 279 logger.info("--- Launching " +app+ " ---"); 280 applications.put(app.getName(), app); 281 app.init(); 282 } 283 284 289 290 public HashMap getApplications() { 291 return applications; 292 } 293 294 300 301 public Application getApplication(String name) { 302 return (Application) applications.get(name); 303 } 304 305 312 313 public void extend(String applicationName, String aspectName) { 314 if (ACManager.get().getObject(applicationName+"."+aspectName)!=null) 315 return; 316 loggerAspects.info("extending application "+applicationName+ 317 " with "+aspectName); 318 Application application = getApplication(applicationName); 319 if (application == null) { 320 logger.error("No such application to extend: "+applicationName); 321 } 322 ACConfiguration acConf = application.getAcConfiguration(aspectName); 323 if (acConf == null) { 324 logger.error("No such AC configuration: "+aspectName+ 325 " found in "+applicationName); 326 } 327 acConf.weave(); 328 } 329 330 337 338 public void unextend(String applicationName, String aspectName) { 339 loggerAspects.info("un-extending application "+applicationName+ 340 " with "+aspectName); 341 Application application = getApplication(applicationName); 342 if (application == null) { 343 logger.error("No such application to extend: "+applicationName); 344 } 345 ACConfiguration acConf = application.getAcConfiguration(aspectName); 346 if (acConf == null) { 347 logger.error("No such AC configuration: "+aspectName+ 348 " found in "+applicationName); 349 } 350 acConf.unweave(); 351 } 352 353 } 354 | Popular Tags |