1 package org.sapia.magnet.domain; 2 3 import java.util.ArrayList ; 6 import java.util.Collection ; 7 import java.util.HashMap ; 8 import java.util.Iterator ; 9 import java.util.List ; 10 import java.util.Map ; 11 12 import org.sapia.magnet.render.MagnetContext; 15 import org.sapia.magnet.render.RenderingException; 16 17 18 29 public class Magnet extends AbstractObjectHandler { 30 31 35 36 private static final Object _theNullProfile = new Object (); 37 38 42 43 private String _theName; 44 45 46 private String _theDescription; 47 48 49 private String _theExtends; 50 51 52 private List _theParents; 53 54 55 private Map _theScriptsByProfile; 56 57 58 private Map _theParametersByProfile; 59 60 61 private List _theLaunchers; 62 63 67 70 public Magnet() { 71 super(); 72 _theParametersByProfile = new HashMap (); 73 _theScriptsByProfile = new HashMap (); 74 _theParents = new ArrayList (); 75 _theLaunchers = new ArrayList (); 76 } 77 78 82 87 public String getName() { 88 return _theName; 89 } 90 91 96 public String getDescription() { 97 return _theDescription; 98 } 99 100 105 public String getExtends() { 106 return _theExtends; 107 } 108 109 114 public Collection getParents() { 115 return _theParents; 116 } 117 118 124 public Collection getScripts() { 125 return _theScriptsByProfile.values(); 126 } 127 128 134 public Collection getParameters() { 135 return _theParametersByProfile.values(); 136 } 137 138 144 public Collection getLaunchers() { 145 return _theLaunchers; 146 } 147 148 152 157 public void setName(String aName) { 158 _theName = aName; 159 } 160 161 166 public void setDescription(String aDescription) { 167 _theDescription = aDescription; 168 } 169 170 175 public void setExtends(String anExtends) { 176 _theExtends = anExtends; 177 } 178 179 185 public void insertParent(Magnet aMagnet) { 186 _theParents.add(0, aMagnet); 187 } 188 189 194 public void addScript(Script aScript) { 195 if (aScript == null) { 196 throw new IllegalArgumentException ("The script passed in is null"); 197 } 198 199 if (aScript.getProfile() == null) { 200 _theScriptsByProfile.put(_theNullProfile, aScript); 201 } else { 202 _theScriptsByProfile.put(aScript.getProfile(), aScript); 203 } 204 } 205 206 211 public void removeScript(Script aScript) { 212 if (aScript == null) { 213 throw new IllegalArgumentException ("The script passed in is null"); 214 } 215 216 if (aScript.getProfile() == null) { 217 _theScriptsByProfile.remove(_theNullProfile); 218 } else { 219 _theScriptsByProfile.remove(aScript.getProfile()); 220 } 221 } 222 223 226 public void clearScripts() { 227 _theScriptsByProfile.clear(); 228 } 229 230 235 public void addParameters(Parameters aParameters) { 236 if (aParameters == null) { 237 throw new IllegalArgumentException ("The parameters passed in is null"); 238 } 239 240 Object aKey; 242 if (aParameters.getProfile() == null) { 243 aKey = _theNullProfile; 244 } else { 245 aKey = aParameters.getProfile(); 246 } 247 248 if (_theParametersByProfile.containsKey(aKey)) { 250 Parameters aMasterParameters = (Parameters) _theParametersByProfile.get(aKey); 251 for (Iterator it = aParameters.getParams().iterator(); it.hasNext(); ) { 252 aMasterParameters.addParam((Param) it.next()); 253 } 254 } else { 255 _theParametersByProfile.put(aKey, aParameters); 257 } 258 } 259 260 265 public void removeParameters(Parameters aParameters) { 266 if (aParameters == null) { 267 throw new IllegalArgumentException ("The parameters passed in is null"); 268 } 269 270 if (aParameters.getProfile() == null) { 271 _theParametersByProfile.remove(_theNullProfile); 272 } else { 273 _theParametersByProfile.remove(aParameters.getProfile()); 274 } 275 } 276 277 280 public void clearParameters() { 281 _theParametersByProfile.clear(); 282 } 283 284 289 public void addLauncher(Launcher aLauncher) { 290 if (aLauncher == null) { 291 throw new IllegalArgumentException ("The launcher passed in is null"); 292 } 293 294 _theLaunchers.add(aLauncher); 295 aLauncher.getLaunchHandler().setMagnet(this); 296 } 297 298 303 public void removeLauncher(Launcher aLauncher) { 304 if (aLauncher == null) { 305 throw new IllegalArgumentException ("The launcher passed in is null"); 306 } 307 308 aLauncher.getLaunchHandler().setMagnet(null); 309 _theLaunchers.remove(aLauncher); 310 } 311 312 315 public void clearLaunchers() { 316 _theLaunchers.clear(); 317 } 318 319 323 329 public void render(MagnetContext aContext) throws RenderingException { 330 try { 332 Parameters aParameters = (Parameters) _theParametersByProfile.get(_theNullProfile); 333 if (aParameters != null) { 334 aParameters.render(aContext); 335 } 336 } catch (RenderingException re) { 337 StringBuffer aBuffer = new StringBuffer (); 338 throw new RenderingException("Unable to render the global properties", re); 339 } 340 341 try { 343 Parameters aParameters = (Parameters) _theParametersByProfile.get(aContext.getProfile()); 344 if (aParameters != null) { 345 aParameters.render(aContext); 346 } 347 } catch (RenderingException re) { 348 StringBuffer aBuffer = new StringBuffer (); 349 aBuffer.append("Unable to render the properties of the profile "). 350 append(aContext.getProfile()); 351 throw new RenderingException(aBuffer.toString(), re); 352 } 353 354 _theName = resolveValue(aContext, _theName); 356 357 try { 359 super.renderHandlerDefs(aContext); 360 } catch (RenderingException re) { 361 StringBuffer aBuffer = new StringBuffer (); 362 aBuffer.append("Unable to render the handler defs of the magnet "). 363 append(_theName); 364 throw new RenderingException(aBuffer.toString(), re); 365 } 366 367 try { 369 Script aScript = (Script) _theScriptsByProfile.get(aContext.getProfile()); 370 if (aScript != null) { 371 aScript.render(aContext); 372 } else { 373 aScript = (Script) _theScriptsByProfile.get(_theNullProfile); 374 if (aScript != null) { 375 aScript.render(aContext); 376 } 377 } 378 } catch (RenderingException re) { 379 StringBuffer aBuffer = new StringBuffer (); 380 aBuffer.append("Unable to render the script of the magnet "). 381 append(_theName); 382 throw new RenderingException(aBuffer.toString(), re); 383 } 384 385 try { 387 super.render(aContext); 388 } catch (RenderingException re) { 389 StringBuffer aBuffer = new StringBuffer (); 390 aBuffer.append("Unable to render an object of the magnet "). 391 append(_theName); 392 throw new RenderingException(aBuffer.toString(), re); 393 } 394 395 Launcher aLauncher = null; 397 try { 398 for (Iterator it = _theLaunchers.iterator(); it.hasNext(); ) { 399 aLauncher = (Launcher) it.next(); 400 aLauncher.render(aContext); 401 } 402 } catch (RenderingException re) { 403 StringBuffer aBuffer = new StringBuffer ("Unable to render the launcher "); 404 if (aLauncher != null) { 405 aBuffer.append(aLauncher.getLaunchHandler().getName()); 406 } 407 throw new RenderingException(aBuffer.toString(), re); 408 } 409 } 410 411 416 public String toString() { 417 StringBuffer aBuffer = new StringBuffer (super.toString()); 418 aBuffer.append("[name=").append(_theName). 419 append(" description=").append(_theDescription). 420 append(" extends=").append(_theExtends). 421 append(" parents=").append(_theParents). 422 append(" scripts=").append(_theScriptsByProfile.values()). 423 append(" parameters=").append(_theParametersByProfile.values()). 424 append(" launchers=").append(_theLaunchers). 425 append("]"); 426 427 return aBuffer.toString(); 428 } 429 } 430 | Popular Tags |