1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import org.apache.tools.ant.Project; 22 import org.apache.tools.ant.Task; 23 import org.apache.tools.ant.MagicNames; 24 import org.apache.tools.ant.BuildException; 25 import org.apache.tools.ant.AntClassLoader; 26 import org.apache.tools.ant.types.Reference; 27 import org.apache.tools.ant.types.Path; 28 29 import java.io.File ; 30 31 62 public class Classloader extends Task { 63 64 public static final String SYSTEM_LOADER_REF = MagicNames.SYSTEM_LOADER_REF; 65 66 private String name = null; 67 private Path classpath; 68 private boolean reset = false; 69 private boolean parentFirst = true; 70 private String parentName = null; 71 72 75 public Classloader() { 76 } 77 78 82 public void setName(String name) { 83 this.name = name; 84 } 85 86 94 public void setReset(boolean b) { 95 this.reset = b; 96 } 97 98 102 public void setReverse(boolean b) { 103 this.parentFirst = !b; 104 } 105 106 110 public void setParentFirst(boolean b) { 111 this.parentFirst = b; 112 } 113 114 118 public void setParentName(String name) { 119 this.parentName = name; 120 } 121 122 123 129 public void setClasspathRef(Reference pathRef) throws BuildException { 130 classpath = (Path) pathRef.getReferencedObject(getProject()); 131 } 132 133 138 public void setClasspath(Path classpath) { 139 if (this.classpath == null) { 140 this.classpath = classpath; 141 } else { 142 this.classpath.append(classpath); 143 } 144 } 145 146 150 public Path createClasspath() { 151 if (this.classpath == null) { 152 this.classpath = new Path(null); 153 } 154 return this.classpath.createPath(); 155 } 156 157 158 161 public void execute() { 162 try { 163 if ("only".equals(getProject().getProperty("build.sysclasspath")) 165 && (name == null || SYSTEM_LOADER_REF.equals(name))) { 166 log("Changing the system loader is disabled " 167 + "by build.sysclasspath=only", Project.MSG_WARN); 168 return; 169 } 170 171 String loaderName = (name == null) ? SYSTEM_LOADER_REF : name; 172 173 Object obj = getProject().getReference(loaderName); 174 if (reset) { 175 obj = null; } 179 180 if (obj != null && !(obj instanceof AntClassLoader)) { 182 log("Referenced object is not an AntClassLoader", 183 Project.MSG_ERR); 184 return; 185 } 186 187 AntClassLoader acl = (AntClassLoader) obj; 188 189 if (acl == null) { 190 Object parent = null; 192 if (parentName != null) { 193 parent = getProject().getReference(parentName); 194 if (!(parent instanceof ClassLoader )) { 195 parent = null; 196 } 197 } 198 if (parent == null) { 200 parent = this.getClass().getClassLoader(); 201 } 202 203 if (name == null) { 204 } 207 getProject().log("Setting parent loader " + name + " " 208 + parent + " " + parentFirst, Project.MSG_DEBUG); 209 210 acl = new AntClassLoader((ClassLoader ) parent, 212 getProject(), classpath, parentFirst); 213 214 getProject().addReference(loaderName, acl); 215 216 if (name == null) { 217 acl.addLoaderPackageRoot("org.apache.tools.ant.taskdefs.optional"); 220 getProject().setCoreLoader(acl); 221 } 222 } 223 if (classpath != null) { 224 String [] list = classpath.list(); 225 for (int i = 0; i < list.length; i++) { 226 File f = new File (list[i]); 227 if (f.exists()) { 228 acl.addPathElement(f.getAbsolutePath()); 229 log("Adding to class loader " + acl + " " + f.getAbsolutePath(), 230 Project.MSG_DEBUG); 231 } 232 } 233 } 234 235 237 } catch (Exception ex) { 238 ex.printStackTrace(); 239 } 240 } 241 } 242 | Popular Tags |