KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > magnet > domain > Magnet


1 package org.sapia.magnet.domain;
2
3 // Import of Sun's JDK classes
4
// ---------------------------
5
import java.util.ArrayList JavaDoc;
6 import java.util.Collection JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Map JavaDoc;
11
12 // Import of Sapia's Corus classes
13
// --------------------------------
14
import org.sapia.magnet.render.MagnetContext;
15 import org.sapia.magnet.render.RenderingException;
16
17
18 /**
19  *
20  *
21  * @author Jean-Cedric Desrochers
22  *
23  * <dl>
24  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
25  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
26  * <a HREF="http://www.sapia-oss.org/license.html" target="sapia-license">license page</a> at the Sapia OSS web site</dd></dt>
27  * </dl>
28  */

29 public class Magnet extends AbstractObjectHandler {
30
31   /////////////////////////////////////////////////////////////////////////////////////////
32
////////////////////////////////// CLASS ATTRIBUTES ///////////////////////////////////
33
/////////////////////////////////////////////////////////////////////////////////////////
34

35   /** Defines a null object for mapping of null profiles. */
36   private static final Object JavaDoc _theNullProfile = new Object JavaDoc();
37
38   /////////////////////////////////////////////////////////////////////////////////////////
39
///////////////////////////////// INSTANCE ATTRIBUTES /////////////////////////////////
40
/////////////////////////////////////////////////////////////////////////////////////////
41

42   /** The name of this magnet. */
43   private String JavaDoc _theName;
44
45   /** The description of this magnet. */
46   private String JavaDoc _theDescription;
47
48   /** The extends of this magnet. */
49   private String JavaDoc _theExtends;
50   
51   /** The parent magnets of this magnet. */
52   private List JavaDoc _theParents;
53
54   /** The map of scripts objects of this magnet by the profile name. */
55   private Map JavaDoc _theScriptsByProfile;
56
57   /** The map of parameters objects of this magnet by the profile name. */
58   private Map JavaDoc _theParametersByProfile;
59
60   /** The list of launchers of this magnet. */
61   private List JavaDoc _theLaunchers;
62
63   /////////////////////////////////////////////////////////////////////////////////////////
64
//////////////////////////////////// CONSTRUCTORS /////////////////////////////////////
65
/////////////////////////////////////////////////////////////////////////////////////////
66

67   /**
68    * Creates a new Magnet instance.
69    */

70   public Magnet() {
71     super();
72     _theParametersByProfile = new HashMap JavaDoc();
73     _theScriptsByProfile = new HashMap JavaDoc();
74     _theParents = new ArrayList JavaDoc();
75     _theLaunchers = new ArrayList JavaDoc();
76   }
77
78   /////////////////////////////////////////////////////////////////////////////////////////
79
////////////////////////////////// ACCESSOR METHODS ///////////////////////////////////
80
/////////////////////////////////////////////////////////////////////////////////////////
81

82   /**
83    * Returns the name of this magnet.
84    *
85    * @return The name of this magnet.
86    */

87   public String JavaDoc getName() {
88     return _theName;
89   }
90
91   /**
92    * Returns the description of this magnet.
93    *
94    * @return The description of this magnet.
95    */

96   public String JavaDoc getDescription() {
97     return _theDescription;
98   }
99
100   /**
101    * Returns the magnets that are extended by this magnet.
102    *
103    * @return The magnets that are extended by this magnet.
104    */

105   public String JavaDoc getExtends() {
106     return _theExtends;
107   }
108   
109   /**
110    * Returns the parent magnets of this magnet.
111    *
112    * @return The parent magnets of this magnet.
113    */

114   public Collection JavaDoc getParents() {
115     return _theParents;
116   }
117
118   /**
119    * Returns the collection of scripts of this magnet.
120    *
121    * @return The collection of <CODE>Script</CODE> objects.
122    * @see Script
123    */

124   public Collection JavaDoc getScripts() {
125     return _theScriptsByProfile.values();
126   }
127
128   /**
129    * Returns the collection of parameters of this magnet.
130    *
131    * @return The collection of <CODE>Parameters</CODE> objects.
132    * @see Parameters
133    */

134   public Collection JavaDoc getParameters() {
135     return _theParametersByProfile.values();
136   }
137
138   /**
139    * Returns the collection of launcher of this magnet.
140    *
141    * @return The collection of <CODE>Launcher</CODE> objects.
142    * @see Launcher
143    */

144   public Collection JavaDoc getLaunchers() {
145     return _theLaunchers;
146   }
147
148   /////////////////////////////////////////////////////////////////////////////////////////
149
/////////////////////////////////// MUTATOR METHODS ///////////////////////////////////
150
/////////////////////////////////////////////////////////////////////////////////////////
151

152   /**
153    * Changes the name of this magnet for the value passed in.
154    *
155    * @param aName The new name of this magnet.
156    */

157   public void setName(String JavaDoc aName) {
158     _theName = aName;
159   }
160
161   /**
162    * Changes the description of this magnet for the value passed in.
163    *
164    * @param aDescription The new name of this magnet.
165    */

166   public void setDescription(String JavaDoc aDescription) {
167     _theDescription = aDescription;
168   }
169
170   /**
171    * Changes the extends of this magnet for the value passed in.
172    *
173    * @param anExtends The new extends of this magnet.
174    */

175   public void setExtends(String JavaDoc anExtends) {
176     _theExtends = anExtends;
177   }
178
179   /**
180    * Insert the magnet passed in at the beginning of
181    * th list of parent magnets.
182    *
183    * @param aMagnet The new parent magnet to insert.
184    */

185   public void insertParent(Magnet aMagnet) {
186     _theParents.add(0, aMagnet);
187   }
188
189   /**
190    * Adds the script passed in to this magnet.
191    *
192    * @param aScript The script to add.
193    */

194   public void addScript(Script aScript) {
195     if (aScript == null) {
196       throw new IllegalArgumentException JavaDoc("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   /**
207    * Removes the script passed in from this magnet.
208    *
209    * @param aScript The script to remove.
210    */

211   public void removeScript(Script aScript) {
212     if (aScript == null) {
213       throw new IllegalArgumentException JavaDoc("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   /**
224    * Removes all the scripts from this magnet.
225    */

226   public void clearScripts() {
227     _theScriptsByProfile.clear();
228   }
229
230   /**
231    * Adds the parameters passed in to this magnet.
232    *
233    * @param aParameters The parameters to add.
234    */

235   public void addParameters(Parameters aParameters) {
236     if (aParameters == null) {
237       throw new IllegalArgumentException JavaDoc("The parameters passed in is null");
238     }
239
240     // Look if we are getting the global parameters or not
241
Object JavaDoc aKey;
242     if (aParameters.getProfile() == null) {
243       aKey = _theNullProfile;
244     } else {
245       aKey = aParameters.getProfile();
246     }
247
248     // Add all the param tot he existing parameters
249
if (_theParametersByProfile.containsKey(aKey)) {
250       Parameters aMasterParameters = (Parameters) _theParametersByProfile.get(aKey);
251       for (Iterator JavaDoc it = aParameters.getParams().iterator(); it.hasNext(); ) {
252         aMasterParameters.addParam((Param) it.next());
253       }
254     } else {
255       // Add the new parameters
256
_theParametersByProfile.put(aKey, aParameters);
257     }
258   }
259
260   /**
261    * Removes the parameters passed in from this magnet.
262    *
263    * @param aParameters The parameters to remove.
264    */

265   public void removeParameters(Parameters aParameters) {
266     if (aParameters == null) {
267       throw new IllegalArgumentException JavaDoc("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   /**
278    * Removes all the parameters from this magnet.
279    */

280   public void clearParameters() {
281     _theParametersByProfile.clear();
282   }
283
284   /**
285    * Adds the launcher passed in to this magnet.
286    *
287    * @param aLauncher The launcher to add.
288    */

289   public void addLauncher(Launcher aLauncher) {
290     if (aLauncher == null) {
291       throw new IllegalArgumentException JavaDoc("The launcher passed in is null");
292     }
293
294     _theLaunchers.add(aLauncher);
295     aLauncher.getLaunchHandler().setMagnet(this);
296   }
297
298   /**
299    * Removes the launcher passed in from this magnet.
300    *
301    * @param aLauncher The launcher to remove.
302    */

303   public void removeLauncher(Launcher aLauncher) {
304     if (aLauncher == null) {
305       throw new IllegalArgumentException JavaDoc("The launcher passed in is null");
306     }
307
308     aLauncher.getLaunchHandler().setMagnet(null);
309     _theLaunchers.remove(aLauncher);
310   }
311
312   /**
313    * Removes all the launchers from this magnet.
314    */

315   public void clearLaunchers() {
316     _theLaunchers.clear();
317   }
318
319   /////////////////////////////////////////////////////////////////////////////////////////
320
////////////////////////////////// OVERRIDEN METHODS //////////////////////////////////
321
/////////////////////////////////////////////////////////////////////////////////////////
322

323   /**
324    * Renders this objects to the magnet context passed in.
325    *
326    * @param aContext The magnet context to use.
327    * @exception RenderingException If an error occurs while rendering this object.
328    */

329   public void render(MagnetContext aContext) throws RenderingException {
330     // Rendering the parameters of the default profile
331
try {
332       Parameters aParameters = (Parameters) _theParametersByProfile.get(_theNullProfile);
333       if (aParameters != null) {
334         aParameters.render(aContext);
335       }
336     } catch (RenderingException re) {
337       StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
338       throw new RenderingException("Unable to render the global properties", re);
339     }
340
341     // Rendering the parameters of the profile passed in
342
try {
343       Parameters aParameters = (Parameters) _theParametersByProfile.get(aContext.getProfile());
344       if (aParameters != null) {
345         aParameters.render(aContext);
346       }
347     } catch (RenderingException re) {
348       StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
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     // Resolving the attributes of this magnet
355
_theName = resolveValue(aContext, _theName);
356
357     // Render the handler definitions
358
try {
359       super.renderHandlerDefs(aContext);
360     } catch (RenderingException re) {
361       StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
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     // Render the script of the profile passed in or, if not found, of the default profile
368
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 JavaDoc aBuffer = new StringBuffer JavaDoc();
380       aBuffer.append("Unable to render the script of the magnet ").
381               append(_theName);
382       throw new RenderingException(aBuffer.toString(), re);
383     }
384
385     // Render all the objects handled by this magnet
386
try {
387       super.render(aContext);
388     } catch (RenderingException re) {
389       StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
390       aBuffer.append("Unable to render an object of the magnet ").
391               append(_theName);
392       throw new RenderingException(aBuffer.toString(), re);
393     }
394
395     // Render the launchers of this magnet
396
Launcher aLauncher = null;
397     try {
398       for (Iterator JavaDoc it = _theLaunchers.iterator(); it.hasNext(); ) {
399         aLauncher = (Launcher) it.next();
400         aLauncher.render(aContext);
401       }
402     } catch (RenderingException re) {
403       StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc("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   /**
412    * Returns a string representation of this magnet.
413    *
414    * @return A string representation of this magnet.
415    */

416   public String JavaDoc toString() {
417     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc(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