KickJava   Java API By Example, From Geeks To Geeks.

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


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

27 public class Script extends AbstractRenderable {
28
29   /////////////////////////////////////////////////////////////////////////////////////////
30
////////////////////////////////// CLASS ATTRIBUTES ///////////////////////////////////
31
/////////////////////////////////////////////////////////////////////////////////////////
32

33   /** Defines the logger instance for this class. */
34   private static final Logger _theLogger = Logger.getLogger(Script.class);
35
36   /////////////////////////////////////////////////////////////////////////////////////////
37
///////////////////////////////// INSTANCE ATTRIBUTES /////////////////////////////////
38
/////////////////////////////////////////////////////////////////////////////////////////
39

40   /** The type of this script. */
41   private String JavaDoc _theType;
42
43   /** The profile name associated with this script. */
44   private String JavaDoc _theProfile;
45
46   /** The code of this script. */
47   private String JavaDoc _theCode;
48
49   /** The abort on error indicator of this script. */
50   private String JavaDoc _isAbortingOnError;
51
52   /////////////////////////////////////////////////////////////////////////////////////////
53
//////////////////////////////////// CONSTRUCTORS /////////////////////////////////////
54
/////////////////////////////////////////////////////////////////////////////////////////
55

56   /**
57    * Creates a new Script instance.
58    */

59   public Script() {
60     _isAbortingOnError = "false";
61   }
62
63   /////////////////////////////////////////////////////////////////////////////////////////
64
////////////////////////////////// ACCESSOR METHODS ///////////////////////////////////
65
/////////////////////////////////////////////////////////////////////////////////////////
66

67   /**
68    * Returns the type of this script.
69    *
70    * @return The type of this script.
71    */

72   public String JavaDoc getType() {
73     return _theType;
74   }
75
76   /**
77    * Returns the profile name associated with this script.
78    *
79    * @return The profile name associated with this script.
80    */

81   public String JavaDoc getProfile() {
82     return _theProfile;
83   }
84
85   /**
86    * Returns the code of this script.
87    *
88    * @return The code of this script.
89    */

90   public String JavaDoc getCode() {
91     return _theCode;
92   }
93
94   /**
95    * Returns the abort on error indicator of this script.
96    *
97    * @return The abort on error indicator of this script.
98    */

99   public boolean isAbortingOnError() {
100     return _isAbortingOnError.equals("true");
101   }
102
103   /////////////////////////////////////////////////////////////////////////////////////////
104
/////////////////////////////////// MUTATOR METHODS ///////////////////////////////////
105
/////////////////////////////////////////////////////////////////////////////////////////
106

107   /**
108    * Changes the type of this script.
109    *
110    * @param aType The new type.
111    */

112   public void setType(String JavaDoc aType) {
113     _theType = aType;
114   }
115
116   /**
117    * Changes the profile name of this script.
118    *
119    * @param aProfile The new profile name.
120    */

121   public void setProfile(String JavaDoc aProfile) {
122     _theProfile = aProfile;
123   }
124
125   /**
126    * Changes the code of this script.
127    *
128    * @param aCode The new code.
129    */

130   public void setCode(String JavaDoc aCode) {
131     _theCode = aCode;
132   }
133
134   /**
135    * Changes the text content of this script as the code of the script.
136    *
137    * @param aContent The new content.
138    */

139   public void setText(String JavaDoc aContent) {
140     _theCode = aContent;
141   }
142
143   /**
144    * Changes the abort on error indicator of this script.
145    *
146    * @param isAbortingOnError The new abort on error indicator value.
147    */

148   public void setIsAbortingOnError(String JavaDoc isAbortingOnError) {
149     _isAbortingOnError = isAbortingOnError;
150   }
151
152   /////////////////////////////////////////////////////////////////////////////////////////
153
////////////////////////////////// OVERRIDEN METHODS //////////////////////////////////
154
/////////////////////////////////////////////////////////////////////////////////////////
155

156   /**
157    * Renders this objects to the magnet context passed in.
158    *
159    * @param aContext The magnet context to use.
160    * @exception RenderingException If an error occurs while rendering this object.
161    */

162   public void render(MagnetContext aContext) throws RenderingException {
163     // Resolve the attributes
164
try {
165       _isAbortingOnError = resolveValue(aContext, _isAbortingOnError);
166       _theCode = resolveValue(aContext, _theCode);
167     } catch (RenderingException re) {
168       StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
169       aBuffer.append("Unable to resolve an attribute of the script of type ").
170               append(_theType);
171       if (_theProfile != null) {
172         aBuffer.append(" for the profile ").append(_theProfile);
173       } else {
174         aBuffer.append(" for the default profile");
175       }
176       
177       throw new RenderingException(aBuffer.toString(), re);
178     }
179
180     try {
181       // Get a script handler and delegate the execution
182
ScriptHandlerIF aHandler = HandlerFactory.getInstance().createScriptHandler(_theType);
183       aHandler.execute(_theCode);
184     } catch (ObjectCreationException oce) {
185       StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
186       aBuffer.append("Unable to create a script handler of type ").
187               append(_theType);
188       if (_theProfile != null) {
189         aBuffer.append(" for the profile ").append(_theProfile);
190       } else {
191         aBuffer.append(" for the default profile");
192       }
193
194       if (isAbortingOnError()) {
195         throw new RenderingException(aBuffer.toString(), oce);
196       } else {
197         aBuffer.append(": ").append(Log.extactMessage(oce));
198         Log.warn(aBuffer.toString());
199       }
200     } catch (MagnetException de) {
201       StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
202       aBuffer.append("Unable to execute the script of type ").
203               append(_theType);
204       if (_theProfile != null) {
205         aBuffer.append(" for the profile ").append(_theProfile);
206       } else {
207         aBuffer.append(" for the default profile");
208       }
209
210       if (isAbortingOnError()) {
211         throw new RenderingException(aBuffer.toString(), de);
212       } else {
213         aBuffer.append(": ").append(Log.extactMessage(de));
214         Log.warn(aBuffer.toString());
215       }
216     } catch (RuntimeException JavaDoc re) {
217       StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
218       aBuffer.append("System error executing the script of type ").
219               append(_theType);
220       if (_theProfile != null) {
221         aBuffer.append(" for the profile ").append(_theProfile);
222       } else {
223         aBuffer.append(" for the default profile");
224       }
225
226       if (isAbortingOnError()) {
227         throw new RenderingException(aBuffer.toString(), re);
228       } else {
229         aBuffer.append(": ").append(Log.extactMessage(re));
230         Log.warn(aBuffer.toString());
231       }
232     }
233   }
234
235   /**
236    * Returns a string representation of this script.
237    *
238    * @return A string representation of this script.
239    */

240   public String JavaDoc toString() {
241     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc(super.toString());
242     aBuffer.append("[type=").append(_theType).
243             append(" profile=").append(_theProfile).
244             append(" abortOnError=").append(_isAbortingOnError).
245             append(" code=").append(_theCode).
246             append("]");
247
248     return aBuffer.toString();
249   }
250 }
251
Popular Tags