KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.magnet.domain;
2
3 // Import of Sun's JDK classes
4
// ---------------------------
5
import java.util.Collection JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.Map JavaDoc;
8
9 // Import of Apache's log4j
10
// ------------------------
11
import org.apache.log4j.Logger;
12
13 // Import of Sapia's magnet classes
14
// --------------------------------
15
import org.sapia.magnet.render.MagnetContext;
16 import org.sapia.magnet.render.RenderingException;
17
18
19 /**
20  *
21  *
22  * @author Jean-Cedric Desrochers
23  *
24  * <dl>
25  * <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>
26  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
27  * <a HREF="http://www.sapia-oss.org/license.html" target="sapia-license">license page</a> at the Sapia OSS web site</dd></dt>
28  * </dl>
29  */

30 public class DefaultLaunchHandler extends AbstractObjectHandler implements LaunchHandlerIF {
31
32   /////////////////////////////////////////////////////////////////////////////////////////
33
////////////////////////////////// CLASS ATTRIBUTES ///////////////////////////////////
34
/////////////////////////////////////////////////////////////////////////////////////////
35

36   /** Defines the logger instance for this class. */
37   private static final Logger _theLogger = Logger.getLogger(DefaultLaunchHandler.class);
38
39   /** The default time to wait after executing this launcher. */
40   public static final int DEFAULT_WAIT_TIME = 1000;
41
42   /////////////////////////////////////////////////////////////////////////////////////////
43
///////////////////////////////// INSTANCE ATTRIBUTES /////////////////////////////////
44
/////////////////////////////////////////////////////////////////////////////////////////
45

46   /** The type of this default launcher. */
47   private String JavaDoc _theType;
48
49   /** The name of this default launcher. */
50   private String JavaDoc _theName;
51
52   /** The default profile name of this launcher. */
53   private String JavaDoc _theDefault;
54   
55   /** The number of milliseconds to wait after executing this launcher. */
56   private int _theWaitTime;
57
58   /** The magnet of this default launcher. */
59   private Magnet _theMagnet;
60
61   /** The list of profiles of this default launcher. */
62   private Map JavaDoc _theProfiles;
63
64   /////////////////////////////////////////////////////////////////////////////////////////
65
//////////////////////////////////// CONSTRUCTORS /////////////////////////////////////
66
/////////////////////////////////////////////////////////////////////////////////////////
67

68   /**
69    * Creates a new DefaultLaunchHandler instance.
70    */

71   protected DefaultLaunchHandler() {
72     _theProfiles = new HashMap JavaDoc();
73     _theWaitTime = DEFAULT_WAIT_TIME;
74   }
75
76   /////////////////////////////////////////////////////////////////////////////////////////
77
////////////////////////////////// ACCESSOR METHODS ///////////////////////////////////
78
/////////////////////////////////////////////////////////////////////////////////////////
79

80   /**
81    * Returns the type of this default launch handler.
82    *
83    * @return The type of this default launch handler.
84    */

85   public String JavaDoc getType() {
86     return _theType;
87   }
88
89   /**
90    * Returns the name of this default launch handler.
91    *
92    * @return The name of this default launch handler.
93    */

94   public String JavaDoc getName() {
95     return _theName;
96   }
97
98   /**
99    * Returns the default profile name of this default launch handler.
100    *
101    * @return The default program name of this default launch handler.
102    */

103   public String JavaDoc getDefault() {
104     return _theDefault;
105   }
106   
107   /**
108    * Returns the number of milliseoncds to wait after executing this launcher.
109    *
110    * @return the number of milliseoncds to wait after executing this launcher.
111    */

112   public int geWaitTime() {
113     return _theWaitTime;
114   }
115
116   /**
117    * Returns the parent magnet of this default laucn handler.
118    *
119    * @return The parent magnet of this default laucn handler.
120    */

121   public Magnet getMagnet() {
122     return _theMagnet;
123   }
124
125   /**
126    * Returns the collection of profiles of this default launch handler.
127    *
128    * @return The collection of <CODE>Profile</CODE> objects of this default launch handler.
129    */

130   public Collection JavaDoc getProfiles() {
131     return _theProfiles.values();
132   }
133
134   /**
135    * Returns the profile for the profile name passed in.
136    *
137    * @param aProfileName The name of the profile to retrieve.
138    * @return The profile for the profile name passed in.
139    */

140   public Profile getProfile(String JavaDoc aProfileName) {
141     return (Profile) _theProfiles.get(aProfileName);
142   }
143
144   /**
145    * Searches for the profile object of this launcher identified by the profile name
146    * passed in. If no profile is found, it searches for the default profile.
147    *
148    * @param aProfileName The name of the profile to search.
149    * @return The asspciated profile object or null if no profile is found.
150    * @exception IllegalArgumentException If the profile name passed in is null.
151    */

152   public Profile findProfile(String JavaDoc aProfileName) {
153     // Validate the argument
154
if (aProfileName == null) {
155       throw new IllegalArgumentException JavaDoc("The profile name passed in is null");
156     }
157
158     // Search for the profile
159
Profile aProfile = getProfile(aProfileName);
160     if (aProfile == null && _theDefault != null) {
161       aProfile = getProfile(_theDefault);
162     }
163
164     return aProfile;
165   }
166   
167   /////////////////////////////////////////////////////////////////////////////////////////
168
/////////////////////////////////// MUTATOR METHODS ///////////////////////////////////
169
/////////////////////////////////////////////////////////////////////////////////////////
170

171   /**
172    * Changes the type of this system launcher to the one passed in.
173    *
174    * @param aType The new type.
175    */

176   public void setType(String JavaDoc aType) {
177     _theType = aType;
178   }
179
180   /**
181    * Changes the name of this system launcher to the one passed in.
182    *
183    * @param aName The new name.
184    */

185   public void setName(String JavaDoc aName) {
186     _theName = aName;
187   }
188
189   /**
190    * Changes the default profile name of this default launch handler to the one passed in.
191    *
192    * @param aProfileName The name of the new default profile.
193    */

194   public void setDefault(String JavaDoc aProfileName) {
195     _theDefault = aProfileName;
196   }
197
198   /**
199    * Changes the value of the time to wait after executing this launcher.
200    *
201    * @param aWaitTime The new wait time value in milliseconds.
202    */

203   public void setWaitTime(int aWaitTime) {
204     _theWaitTime = aWaitTime;
205   }
206
207   /**
208    * Changes the parent magnet of this launch handler.
209    *
210    * @param aMagnet the new Magnet.
211    */

212   public void setMagnet(Magnet aMagnet) {
213     _theMagnet = aMagnet;
214   }
215
216   /**
217    * Adds the profile passed in to this default launch handler.
218    *
219    * @param aProfile the profile to add.
220    */

221   public void addProfile(Profile aProfile) {
222     if (aProfile == null) {
223       throw new IllegalArgumentException JavaDoc("The profile passed in is null");
224     } else if (aProfile.getName() == null) {
225       throw new IllegalArgumentException JavaDoc("The name of the profile passed in is null");
226     }
227
228     _theProfiles.put(aProfile.getName(), aProfile);
229   }
230
231   /**
232    * Executes this launch handler for the passed in profile.
233    *
234    * @param aProfile The profile to execute.
235    */

236   public void execute(String JavaDoc aProfile) {
237     if (_theLogger.isInfoEnabled()) {
238       _theLogger.info("Executing this launch handler for the profile " +
239               aProfile + "\n\t" + toString());
240     }
241   }
242
243   /////////////////////////////////////////////////////////////////////////////////////////
244
/////////////////////////////// INTERACE IMPLEMENTATION ///////////////////////////////
245
/////////////////////////////////////////////////////////////////////////////////////////
246

247   /**
248    * Renders this objects to the magnet context passed in.
249    *
250    * @param aContext The magnet context to use.
251    * @exception RenderingException If an error occurs while rendering this object.
252    */

253   public void render(MagnetContext aContext) throws RenderingException {
254     super.renderHandlerDefs(aContext);
255   }
256
257   /////////////////////////////////////////////////////////////////////////////////////////
258
////////////////////////////////// OVERRIDEN METHODS //////////////////////////////////
259
/////////////////////////////////////////////////////////////////////////////////////////
260

261   /**
262    * Returns a string representation of this system launcher.
263    *
264    * @return A string representation of this system launcher.
265    */

266   public String JavaDoc toString() {
267     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc(super.toString());
268     aBuffer.append("[type=").append(_theType).
269             append(" name=").append(_theName).
270             append(" default=").append(_theDefault).
271             append(" waitTime=").append(_theWaitTime).
272             append(" profiles=").append(_theProfiles.values()).
273             append("]");
274
275     return aBuffer.toString();
276   }
277 }
278
Popular Tags