1 package org.sapia.magnet.domain.java; 2 3 import java.net.URL ; 6 import java.net.URLClassLoader ; 7 import java.net.MalformedURLException ; 8 import java.util.ArrayList ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 12 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 33 public class Classpath extends AbstractRenderable { 34 35 39 40 private String _theId; 41 42 43 private String _theParent; 44 45 46 private List _thePaths; 47 48 49 private ClassLoader _theClassLoader; 50 51 55 58 public Classpath() { 59 _thePaths = new ArrayList (); 60 } 61 62 66 71 public String getId() { 72 return _theId; 73 } 74 75 80 public String getParent() { 81 return _theParent; 82 } 83 84 89 public List getPaths() { 90 return _thePaths; 91 } 92 93 97 102 public void setId(String anId) { 103 _theId = anId; 104 } 105 106 111 public void setParent(String aParent) { 112 _theParent = aParent; 113 } 114 115 120 public void addPath(Path aPath) { 121 _thePaths.add(aPath); 122 } 123 124 129 public void removePath(Path aPath) { 130 _thePaths.remove(aPath); 131 } 132 133 136 public void clearPaths() { 137 _thePaths.clear(); 138 } 139 140 144 151 public ClassLoader createClassLoader(ClassLoader aParent) throws MagnetException { 152 153 if (_theClassLoader == null) { 154 try { 155 ArrayList someURLs = new ArrayList (); 156 for (Iterator somePaths = _thePaths.iterator(); somePaths.hasNext(); ) { 157 Path aPath = (Path) somePaths.next(); 158 for (Iterator 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 []) someURLs.toArray(new URL [0]), aParent); 167 } 168 169 } catch (MalformedURLException mue) { 170 throw new MagnetException("Error creating a classloader for this classpath", mue); 171 } 172 } 173 174 return _theClassLoader; 175 } 176 177 181 187 public void render(MagnetContext aContext) throws RenderingException { 188 try { 190 _theId = resolveValue(aContext, _theId); 191 _theParent = resolveValue(aContext, _theParent); 192 } catch (RenderingException re) { 193 StringBuffer aBuffer = new StringBuffer ("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 try { 205 for (Iterator it = _thePaths.iterator(); it.hasNext(); ) { 206 Path aPath = (Path) it.next(); 207 aPath.render(aContext); 208 } 209 } catch (RenderingException re) { 210 StringBuffer aBuffer = new StringBuffer ("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 226 public String toString() { 227 StringBuffer aBuffer = new StringBuffer (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 |