1 25 26 package org.objectweb.easybeans.container.archive; 27 28 import static org.objectweb.easybeans.util.url.URLUtils.fileToURL2; 29 30 import java.io.File ; 31 import java.net.URL ; 32 import java.util.ArrayList ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 36 import org.objectweb.easybeans.api.EZBArchive; 37 import org.objectweb.easybeans.api.EZBArchiveException; 38 import org.objectweb.easybeans.util.url.URLUtilsException; 39 43 public class EZBDirectoryArchiveImpl implements EZBArchive { 44 45 48 private File directory = null; 49 50 54 protected EZBDirectoryArchiveImpl(final File directory) { 55 this.directory = directory; 56 } 57 58 61 public String getName() { 62 return directory.getPath(); 63 } 64 65 70 private String encode(final String resourceName) { 71 String [] tokens = resourceName.split("/"); 72 StringBuilder sb = new StringBuilder (); 73 for (String token : tokens) { 74 if (sb.length() > 0) { 75 sb.append(File.separator); 76 } 77 sb.append(token); 78 } 79 return sb.toString(); 80 } 81 82 83 88 public boolean close() { 89 return true; 91 } 92 93 99 public URL getResource(final String resourceName) throws EZBArchiveException { 100 URL resourceURL = null; 101 File f = new File (directory, encode(resourceName)); 103 try { 104 if (f.exists()) { 105 resourceURL = fileToURL2(f); 106 } 107 } catch (URLUtilsException e) { 108 throw new EZBArchiveException("Invalid url", e); 109 } 110 return resourceURL; 111 } 112 113 117 public Iterator <URL > getResources() throws EZBArchiveException { 118 List <URL > listResources = new ArrayList <URL >(); 119 addFiles(directory, listResources); 121 return listResources.iterator(); 122 } 123 124 129 private void addFiles(final File file, final List <URL > listResources) { 130 if (!file.exists()) { 131 return; 132 } 133 134 if (file.isDirectory()) { 135 File [] files = file.listFiles(); 137 for (File f : files) { 138 addFiles(f, listResources); 140 } 141 } else { 142 try { 144 listResources.add(fileToURL2(file)); 145 } catch (URLUtilsException e) { 146 throw new IllegalStateException ("Invalid url", e); 147 } 148 } 149 } 150 151 152 157 public Iterator <URL > getResources(final String resourceName) throws EZBArchiveException{ 158 List <URL > listResources = new ArrayList <URL >(); 159 File f = new File (directory, encode(resourceName)); 160 if (f.exists()) { 161 try { 162 listResources.add(fileToURL2(f)); 163 } catch (URLUtilsException e) { 164 throw new EZBArchiveException("Invalid url", e); 165 } 166 } 167 168 return listResources.iterator(); 169 170 } 171 172 176 public URL getURL() throws EZBArchiveException { 177 try { 178 return fileToURL2(directory); 179 } catch (URLUtilsException e) { 180 throw new IllegalStateException ("Invalid url", e); 181 } 182 } 183 184 189 @Override 190 public boolean equals(final Object o) { 191 if (!(o instanceof EZBDirectoryArchiveImpl)) { 192 return false; 193 } 194 EZBDirectoryArchiveImpl other = (EZBDirectoryArchiveImpl) o; 195 return this.directory.equals(other.directory); 196 } 197 198 202 @Override 203 public int hashCode() { 204 return directory.hashCode(); 205 } 206 207 210 @Override 211 public String toString() { 212 return getName(); 213 } 214 } 215 | Popular Tags |