1 28 29 package org.objectweb.util.launcher.parser; 30 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.StringTokenizer ; 34 35 import org.objectweb.util.launcher.LauncherException; 36 import org.objectweb.util.trace.TraceSystem; 37 38 44 public class RepositoryZeus 45 implements Repository 46 { 47 48 49 public static final String separator = "@" ; 50 51 54 private final HashMap classpath_, argument_, property_, run_, context_ ; 55 56 59 public RepositoryZeus() { 60 this.classpath_ = new HashMap (); 61 this.argument_ = new HashMap (); 62 this.property_ = new HashMap (); 63 this.run_ = new HashMap (); 64 this.context_ = new HashMap (); 65 } 66 67 70 protected HashMap getArgument() { 71 return argument_; 72 } 73 74 77 protected HashMap getClasspath() { 78 return classpath_; 79 } 80 81 84 protected HashMap getContext() { 85 return context_; 86 } 87 88 91 protected HashMap getProperty() { 92 return property_; 93 } 94 95 98 protected HashMap getRun() { 99 return run_; 100 } 101 102 107 public void addClasspath(String identifier, Classpath description) { 108 TraceSystem.get("repository").debug("Registering classpath "+identifier+" with value "+description); 109 getClasspath().put(identifier, description); 110 } 111 112 117 public void addProperty(String identifier, Properties description) { 118 TraceSystem.get("repository").debug("Registering properties "+identifier+" with value "+description); 119 getProperty().put(identifier, description); 120 } 121 122 127 public void addArgument(String identifier, Arguments description) { 128 TraceSystem.get("repository").debug("Registering arguments "+identifier+" with value "+description); 129 getArgument().put(identifier, description); 130 } 131 132 137 public void addRun(String identifier, Run description) { 138 TraceSystem.get("repository").debug("Registering run "+identifier+" with value "+description); 139 getRun().put(identifier, description); 140 } 141 142 147 public void addContext(String identifier, Context description) { 148 TraceSystem.get("repository").debug("Registering context "+identifier+" with value "+description); 149 getContext().put(identifier, description); 150 } 151 152 160 protected String replaceTag(String tagged) { 161 StringBuffer result = new StringBuffer (); 162 int start = 0; 163 int end = tagged.indexOf(separator); 164 while(end != -1) { 165 result.append(tagged.substring(start,end)); 166 start=end+1; 167 end= tagged.indexOf(separator,start); 168 if (end != -1) { 169 String tag = tagged.substring(start,end); 170 String value = System.getProperty(tag); 171 if (value == null) 172 result.append("@"+tag+"@"); 173 else { 174 TraceSystem.get("repository").debug("Replacing tag @"+tag+"@ with value "+value); 175 result.append(value); 176 } 177 } 178 start = end+1; 179 end = tagged.indexOf(separator,start); 180 } 181 result.append(tagged.substring(start)); 182 return result.toString(); 183 } 184 185 190 protected void loadClasspath(String classpath, ContextDescription description) { 191 for (StringTokenizer st = new StringTokenizer (classpath!=null?classpath:"");st.hasMoreTokens();){ 192 String path = st.nextToken(); 193 if (!description.checkDescription(path+"_cp")) { 194 description.addDescription(path+"_cp"); 195 TraceSystem.get("repository").debug("Loading path "+path+" ..."); 196 Classpath cl = (Classpath)getClasspath().get(path); 197 if (cl != null) 198 loadClasspath(cl, description); 199 else 200 throw new LauncherException(new Exception ("Path " + path + " not found!")); 201 } 202 } 203 204 } 205 206 211 protected void loadClasspath(Classpath classpath, ContextDescription description) { 212 if (classpath == null) 213 throw new LauncherException(new Exception ("No path found!")); 214 for (Iterator i = classpath.getPathList().iterator() ; i.hasNext() ; ) { 215 Path path = (Path)i.next(); 216 if (path.getUrl() != null) 217 description.addClass(replaceTag(path.getUrl())); 218 else 219 loadClasspath(path.getClasspath(), description); 220 } 221 } 222 223 228 protected void loadArguments(String args, ContextDescription description) { 229 for (StringTokenizer st = new StringTokenizer (args!=null?args:"");st.hasMoreTokens();){ 230 String arg = st.nextToken(); 231 if (!description.checkDescription(arg+"_args")) { 232 description.addDescription(arg+"_args"); 233 TraceSystem.get("repository").debug("Loading argument "+arg+" ..."); 234 Arguments arguments = (Arguments)getArgument().get(arg); 235 if (arguments != null) 236 loadArguments(arguments, description); 237 else 238 throw new LauncherException(new Exception ("Argument " + arg + " not found!")); 239 } 240 } 241 } 242 243 248 protected void loadArguments(Arguments args, ContextDescription description) { 249 if (args == null) 250 throw new LauncherException(new Exception ("No argument found!")); 251 for(Iterator i = args.getArgumentList().iterator() ; i.hasNext() ; ) { 252 Argument arg = (Argument)i.next(); 253 if (arg.getValue() != null) { 254 String toAdd = replaceTag(arg.getValue()); 255 description.addArgument(toAdd); 256 } else if (arg.getLine() != null){ 257 String toAdd = replaceTag(arg.getLine()); 258 for (StringTokenizer st = new StringTokenizer (toAdd);st.hasMoreTokens();) 259 description.addArgument(st.nextToken()); 260 } else 261 loadArguments(arg.getArguments(), description ); 262 } 263 } 264 265 270 protected void loadProperties(String props, ContextDescription description) { 271 for (StringTokenizer st = new StringTokenizer (props!=null?props:"");st.hasMoreTokens();){ 272 String prop = st.nextToken(); 273 if (!description.checkDescription(prop+"_props")) { 274 description.addDescription(prop+"_props"); 275 TraceSystem.get("repository").debug("Loading property "+prop+" ..."); 276 Properties properties = (Properties)getProperty().get(prop); 277 if (properties != null) 278 loadProperties(properties, description); 279 else 280 throw new LauncherException(new Exception ("Property " + prop + " not found!")); 281 } 282 } 283 } 284 285 290 protected void loadProperties(Properties props, ContextDescription description) { 291 if (props == null) 292 throw new LauncherException(new Exception ("No property found!")); 293 for(Iterator i = props.getPropertyList().iterator() ; i.hasNext() ; ) { 294 Property prop = (Property)i.next(); 295 if ((prop.getName() != null) && (prop.getValue() != null)) 296 description.addProperty(prop.getName(), replaceTag(prop.getValue())); 297 else 298 loadProperties(prop.getProperties(), description); 299 } 300 } 301 302 303 308 protected void loadContexts(String ctx, ContextDescription description) { 309 for (StringTokenizer st = new StringTokenizer (ctx!=null?ctx:"");st.hasMoreTokens();){ 310 String context = st.nextToken(); 311 if (!description.checkDescription(context+"_ctx")) { 312 description.addDescription(context+"_ctx"); 313 TraceSystem.get("repository").debug("Loading context "+context+" ..."); 314 Context context_obj = (Context)getContext().get(context); 315 if (context_obj != null) 316 loadContexts(context_obj, description); 317 else 318 throw new LauncherException(new Exception ("Context " + context + " not found!")); 319 } 320 } 321 } 322 323 328 protected void loadContexts(Context ctx, ContextDescription description) { 329 if (ctx == null) 330 throw new LauncherException(new Exception ("No context found!")); 331 description.setName(ctx.getId()); 332 loadClasspath(ctx.getClasspath(),description); 333 loadArguments(ctx.getArguments(),description); 334 loadProperties(ctx.getProperties(),description); 335 loadContexts(ctx.getContexts(),description); 336 } 337 338 343 protected void loadRun(Run run, RunDescription description) { 344 if (run == null) 345 throw new LauncherException(new Exception ("No run found!")); 346 description.setMainclass(run.getMainclassname()); 347 description.setName(run.getId()); 348 description.setMode(run.getMode()); 349 loadClasspath(run.getClasspath(),description); 350 loadArguments(run.getArguments(),description); 351 loadProperties(run.getProperties(),description); 352 loadContexts(run.getContexts(),description); 353 } 354 355 360 public void completeDescription(String identifier, ContextDescription description) { 361 TraceSystem.get("repository").debug("Complete description with context "+identifier); 362 loadContexts((Context)getContext().get(identifier), description); 363 } 364 365 370 public ContextDescription getDescription(String identifier) { 371 Run element = (Run)getRun().get(identifier); 372 if (element != null) { 373 TraceSystem.get("repository").debug("Retrieving run "+identifier); 374 RunDescription desc = new RunDescription(); 375 loadRun((Run)element,desc); 376 return desc; 377 } else { 378 Context ctx = (Context)getContext().get(identifier); 379 if (ctx != null) { 380 TraceSystem.get("repository").debug("Retrieving context "+identifier); 381 ContextDescription desc = new ContextDescription(); 382 loadContexts((Context)ctx,desc); 383 return desc; 384 } else 385 throw new LauncherException(new Exception ("No description found for " + identifier + "!")); 386 } 387 } 388 } 389 | Popular Tags |