1 18 package org.apache.tools.ant.types.resources; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 23 import org.apache.tools.ant.util.FileUtils; 24 import org.apache.tools.ant.types.Path; 25 import org.apache.tools.ant.types.Resource; 26 import org.apache.tools.ant.types.Reference; 27 28 32 public class JavaResource extends Resource { 33 34 private Path classpath; 35 private Reference loader; 36 37 40 public JavaResource() { 41 } 42 43 50 public JavaResource(String name, Path path) { 51 setName(name); 52 classpath = path; 53 } 54 55 59 public void setClasspath(Path classpath) { 60 checkAttributesAllowed(); 61 if (this.classpath == null) { 62 this.classpath = classpath; 63 } else { 64 this.classpath.append(classpath); 65 } 66 } 67 68 72 public Path createClasspath() { 73 checkChildrenAllowed(); 74 if (this.classpath == null) { 75 this.classpath = new Path(getProject()); 76 } 77 return this.classpath.createPath(); 78 } 79 80 85 public void setClasspathRef(Reference r) { 86 checkAttributesAllowed(); 87 createClasspath().setRefid(r); 88 } 89 90 94 public Path getClasspath() { 95 return isReference() 96 ? ((JavaResource) getCheckedRef()).getClasspath() : classpath; 97 } 98 99 110 public void setLoaderRef(Reference r) { 111 checkAttributesAllowed(); 112 loader = r; 113 } 114 115 119 public void setRefid(Reference r) { 120 if (loader != null || classpath != null) { 121 throw tooManyAttributes(); 122 } 123 super.setRefid(r); 124 } 125 126 130 public boolean isExists() { 131 InputStream is = null; 132 try { 133 return isReference() ? ((Resource) getCheckedRef()).isExists() 134 : (is = getInputStream()) != null; 135 } catch (IOException ex) { 136 return false; 137 } finally { 138 FileUtils.close(is); 139 } 140 } 141 142 147 public InputStream getInputStream() throws IOException { 148 if (isReference()) { 149 return ((Resource) getCheckedRef()).getInputStream(); 150 } 151 ClassLoader cl = null; 152 if (loader != null) { 153 cl = (ClassLoader ) loader.getReferencedObject(); 154 } 155 if (cl == null) { 156 if (getClasspath() != null) { 157 cl = getProject().createClassLoader(classpath); 158 } else { 159 cl = JavaResource.class.getClassLoader(); 160 } 161 if (loader != null && cl != null) { 162 getProject().addReference(loader.getRefId(), cl); 163 } 164 } 165 166 return cl == null ? ClassLoader.getSystemResourceAsStream(getName()) 167 : cl.getResourceAsStream(getName()); 168 } 169 170 177 public int compareTo(Object another) { 178 if (isReference()) { 179 return ((Comparable ) getCheckedRef()).compareTo(another); 180 } 181 if (another.getClass().equals(getClass())) { 182 JavaResource otherjr = (JavaResource) another; 183 if (!getName().equals(otherjr.getName())) { 184 return getName().compareTo(otherjr.getName()); 185 } 186 if (loader != otherjr.loader) { 187 if (loader == null) { 188 return -1; 189 } 190 if (otherjr.loader == null) { 191 return 1; 192 } 193 return loader.getRefId().compareTo(otherjr.loader.getRefId()); 194 } 195 Path p = getClasspath(); 196 Path op = otherjr.getClasspath(); 197 if (p != op) { 198 if (p == null) { 199 return -1; 200 } 201 if (op == null) { 202 return 1; 203 } 204 return p.toString().compareTo(op.toString()); 205 } 206 return 0; 207 } 208 return super.compareTo(another); 209 } 210 211 } 212 | Popular Tags |