KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > magnet > domain > system > Environment


1 package org.sapia.magnet.domain.system;
2
3 // Import of Sun's JDK classes
4
// ---------------------------
5
import java.util.Collection JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.Map JavaDoc;
8 import java.util.TreeMap JavaDoc;
9
10
11 // Import of Sapia's magnet classes
12
// --------------------------------
13
import org.sapia.magnet.render.AbstractRenderable;
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 Environment extends AbstractRenderable {
30
31   /////////////////////////////////////////////////////////////////////////////////////////
32
///////////////////////////////// INSTANCE ATTRIBUTES /////////////////////////////////
33
/////////////////////////////////////////////////////////////////////////////////////////
34

35   /** The identifier of this enironment. */
36   private String JavaDoc _theId;
37
38   /** The name of the parent of this environment. */
39   private String JavaDoc _theParent;
40
41   /** The list of environment variables of this environment . */
42   private Map JavaDoc _theVariables;
43
44   /////////////////////////////////////////////////////////////////////////////////////////
45
//////////////////////////////////// CONSTRUCTORS /////////////////////////////////////
46
/////////////////////////////////////////////////////////////////////////////////////////
47

48   /**
49    * Creates a new Environment instance.d
50    */

51   public Environment() {
52     _theVariables = new TreeMap JavaDoc();
53   }
54
55   /////////////////////////////////////////////////////////////////////////////////////////
56
////////////////////////////////// ACCESSOR METHODS ///////////////////////////////////
57
/////////////////////////////////////////////////////////////////////////////////////////
58

59   /**
60    * Returns the identifier of this environment.
61    *
62    * @return The identifier of this environment.
63    */

64   public String JavaDoc getId() {
65     return _theId;
66   }
67
68   /**
69    * Returns the parent name of this environment.
70    *
71    * @return The parent name of this environment.
72    */

73   public String JavaDoc getParent() {
74     return _theParent;
75   }
76
77   /**
78    * Returns the collection of variables of this environment.
79    *
80    * @return The collection of <CODE>Variable</CODE> objects.
81    * @see Variable
82    */

83   public Collection JavaDoc getVariables() {
84     return _theVariables.values();
85   }
86
87   /////////////////////////////////////////////////////////////////////////////////////////
88
/////////////////////////////////// MUTATOR METHODS ///////////////////////////////////
89
/////////////////////////////////////////////////////////////////////////////////////////
90

91   /**
92    * Changes the identifier of this environment.
93    *
94    * @param anId The new identifier.
95    */

96   public void setId(String JavaDoc anId) {
97     _theId = anId;
98   }
99
100   /**
101    * Changes the parent name of this environment.
102    *
103    * @param aParent The new parent name.
104    */

105   public void setParent(String JavaDoc aParent) {
106     _theParent = aParent;
107   }
108
109   /**
110    * Adds the variable passed in to this environment.
111    *
112    * @param aVariable The variable to add.
113    */

114   public void addVariable(Variable aVariable) {
115     _theVariables.put(aVariable.getName(), aVariable);
116   }
117
118   /**
119    * Removes the variable passed in from this environment.
120    *
121    * @param aVariable The variable to remove.
122    */

123   public void removeVariable(Variable aVariable) {
124     _theVariables.remove(aVariable.getName());
125   }
126
127   /**
128    * Removes all the variables of this environment.
129    */

130   public void clearVariables() {
131     _theVariables.clear();
132   }
133
134   /////////////////////////////////////////////////////////////////////////////////////////
135
/////////////////////////////// INTERACE IMPLEMENTATION ///////////////////////////////
136
/////////////////////////////////////////////////////////////////////////////////////////
137

138   /**
139    * Renders this objects to the magnet context passed in.
140    *
141    * @param aContext The magnet context to use.
142    * @exception RenderingException If an error occurs while rendering this object.
143    */

144   public void render(MagnetContext aContext) throws RenderingException {
145     // Resolve the attributes
146
try {
147       _theId = resolveValue(aContext, _theId);
148       _theParent = resolveValue(aContext, _theParent);
149     } catch (RenderingException re) {
150       StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
151       aBuffer.append("Unable to resolve the attribute of the environment '").
152               append(_theId).append("'");
153       
154       throw new RenderingException(aBuffer.toString(), re);
155     }
156
157     // Render the path
158
Variable aVariable = null;
159     try {
160       for (Iterator JavaDoc it = _theVariables.values().iterator(); it.hasNext(); ) {
161         aVariable = (Variable) it.next();
162         aVariable.render(aContext);
163       }
164     } catch (RenderingException re) {
165       StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc("Unable to render the variable");
166       if (aVariable != null) {
167         aBuffer.append(" '").append(aVariable.getName()).append("'");
168       }
169       aBuffer.append(" of the environment '").append(_theId).append("'");
170       
171       throw new RenderingException(aBuffer.toString(), re);
172     }
173   }
174
175   /**
176    * Returns a string representation of this classpath.
177    *
178    * @return A string representation of this classpath.
179    */

180   public String JavaDoc toString() {
181     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc(super.toString());
182     aBuffer.append("[id=").append(_theId).
183             append(" parent=").append(_theParent).
184             append(" variables=").append(_theVariables).
185             append("]");
186
187     return aBuffer.toString();
188   }
189 }
190
Popular Tags