KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > magnet > domain > java > Classpath


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

33 public class Classpath extends AbstractRenderable {
34
35   /////////////////////////////////////////////////////////////////////////////////////////
36
///////////////////////////////// INSTANCE ATTRIBUTES /////////////////////////////////
37
/////////////////////////////////////////////////////////////////////////////////////////
38

39   /** The identifier of this classpath. */
40   private String JavaDoc _theId;
41
42   /** The parent identifier of this classpath. */
43   private String JavaDoc _theParent;
44
45   /** The path elements of this classpath. */
46   private List JavaDoc _thePaths;
47
48   /** The class loader that is created by this classpath. */
49   private ClassLoader JavaDoc _theClassLoader;
50   
51   /////////////////////////////////////////////////////////////////////////////////////////
52
//////////////////////////////////// CONSTRUCTORS /////////////////////////////////////
53
/////////////////////////////////////////////////////////////////////////////////////////
54

55   /**
56    * Creates a new Classpath instance.
57    */

58   public Classpath() {
59     _thePaths = new ArrayList JavaDoc();
60   }
61
62   /////////////////////////////////////////////////////////////////////////////////////////
63
////////////////////////////////// ACCESSOR METHODS ///////////////////////////////////
64
/////////////////////////////////////////////////////////////////////////////////////////
65

66   /**
67    * Returns the identifier of this classpath.
68    *
69    * @return The identifier of this classpath.
70    */

71   public String JavaDoc getId() {
72     return _theId;
73   }
74
75   /**
76    * Returns the parent identifier of this classpath.
77    *
78    * @return The parent identifier of this classpath.
79    */

80   public String JavaDoc getParent() {
81     return _theParent;
82   }
83
84   /**
85    * Returns the path elements of this classpath.
86    *
87    * @return The path elements of this classpath.
88    */

89   public List JavaDoc getPaths() {
90     return _thePaths;
91   }
92
93   /////////////////////////////////////////////////////////////////////////////////////////
94
/////////////////////////////////// MUTATOR METHODS ///////////////////////////////////
95
/////////////////////////////////////////////////////////////////////////////////////////
96

97   /**
98    * Changes the identifier of this classpath.
99    *
100    * @param anId The new identifier.
101    */

102   public void setId(String JavaDoc anId) {
103     _theId = anId;
104   }
105
106   /**
107    * Changes the parent identifier of this classpath.
108    *
109    * @param aParent The new parent identifier.
110    */

111   public void setParent(String JavaDoc aParent) {
112     _theParent = aParent;
113   }
114
115   /**
116    * Adds the path element passed in to this classpath.
117    *
118    * @param aPath The path element to add.
119    */

120   public void addPath(Path aPath) {
121     _thePaths.add(aPath);
122   }
123
124   /**
125    * Removes the path element passed in from this classpath.
126    *
127    * @param aPath The path element to remove.
128    */

129   public void removePath(Path aPath) {
130     _thePaths.remove(aPath);
131   }
132
133   /**
134    * Removes all the paths elements of this classpath.
135    */

136   public void clearPaths() {
137     _thePaths.clear();
138   }
139
140   /////////////////////////////////////////////////////////////////////////////////////////
141
/////////////////////////////////// HELPER METHODS ////////////////////////////////////
142
/////////////////////////////////////////////////////////////////////////////////////////
143

144   /**
145    * Creates a ClassLoader with the resources of this classpath.
146    *
147    * @param aParent The parent classloader of one to create.
148    * @return The created classloader.
149    * @exception MagnetException If an error occurs creating the classloader.
150    */

151   public ClassLoader JavaDoc createClassLoader(ClassLoader JavaDoc aParent) throws MagnetException {
152
153     if (_theClassLoader == null) {
154       try {
155         ArrayList JavaDoc someURLs = new ArrayList JavaDoc();
156         for (Iterator JavaDoc somePaths = _thePaths.iterator(); somePaths.hasNext(); ) {
157           Path aPath = (Path) somePaths.next();
158           for (Iterator JavaDoc it = aPath.getSelectedResources().iterator(); it.hasNext(); ) {
159             someURLs.add(((Resource) it.next()).toURL());
160           }
161         }
162   
163         if (someURLs.size() == 0) {
164           _theClassLoader = aParent;
165         } else {
166           _theClassLoader = URLClassLoader.newInstance((URL JavaDoc[]) someURLs.toArray(new URL JavaDoc[0]), aParent);
167         }
168   
169       } catch (MalformedURLException JavaDoc mue) {
170         throw new MagnetException("Error creating a classloader for this classpath", mue);
171       }
172     }
173     
174     return _theClassLoader;
175   }
176
177   /////////////////////////////////////////////////////////////////////////////////////////
178
////////////////////////////////// OVERRIDEN METHODS //////////////////////////////////
179
/////////////////////////////////////////////////////////////////////////////////////////
180

181   /**
182    * Renders this objects to the magnet context passed in.
183    *
184    * @param aContext The magnet context to use.
185    * @exception RenderingException If an error occurs while rendering this object.
186    */

187   public void render(MagnetContext aContext) throws RenderingException {
188     // Resolve the attributes
189
try {
190       _theId = resolveValue(aContext, _theId);
191       _theParent = resolveValue(aContext, _theParent);
192     } catch (RenderingException re) {
193       StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc("Unable to resolve an attribute of the");
194       if (_theId != null) {
195         aBuffer.append(" classpath \'").append(_theId).append("\'");
196       } else {
197         aBuffer.append(" classpath");
198       }
199         
200       throw new RenderingException(aBuffer.toString(), re);
201     }
202   
203     // Render the paths
204
try {
205       for (Iterator JavaDoc it = _thePaths.iterator(); it.hasNext(); ) {
206         Path aPath = (Path) it.next();
207         aPath.render(aContext);
208       }
209     } catch (RenderingException re) {
210       StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc("Unable to render the");
211       if (_theId != null) {
212         aBuffer.append(" classpath \'").append(_theId).append("\'");
213       } else {
214         aBuffer.append(" classpath");
215       }
216       
217       throw new RenderingException(aBuffer.toString(), re);
218     }
219   }
220
221   /**
222    * Returns a string representation of this classpath.
223    *
224    * @return A string representation of this classpath.
225    */

226   public String JavaDoc toString() {
227     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc(super.toString());
228     aBuffer.append("[id=").append(_theId).
229             append(" parent=").append(_theParent).
230             append(" paths=").append(_thePaths).
231             append("]");
232
233     return aBuffer.toString();
234   }
235 }
236
237
Popular Tags