1 9 10 package org.jboss.deployment.cache; 11 12 import java.util.Map ; 13 import java.util.HashMap ; 14 import java.util.zip.CRC32 ; 15 16 import java.net.URL ; 17 import java.net.URLConnection ; 18 import java.net.MalformedURLException ; 19 20 import java.io.File ; 21 import java.io.FileNotFoundException ; 22 import java.io.IOException ; 23 24 import java.io.InputStream ; 25 import java.io.OutputStream ; 26 import java.io.BufferedInputStream ; 27 import java.io.BufferedOutputStream ; 28 import java.io.ObjectInputStream ; 29 import java.io.ObjectOutputStream ; 30 import java.io.FileInputStream ; 31 import java.io.FileOutputStream ; 32 33 import org.jboss.system.ServiceMBeanSupport; 34 import org.jboss.system.ConfigurationException; 35 import org.jboss.system.server.ServerConfigLocator; 36 37 import org.jboss.util.NullArgumentException; 38 import org.jboss.util.NestedRuntimeException; 39 import org.jboss.util.stream.Streams; 40 41 51 public class FileDeploymentStore 52 extends ServiceMBeanSupport 53 implements DeploymentStore, FileDeploymentStoreMBean 54 { 55 56 protected File dir; 57 58 59 protected File mapFile; 60 61 62 protected Map urlMap; 63 64 73 public void setDirectory(File dir) throws IOException 74 { 75 if (dir == null) 76 throw new NullArgumentException("dir"); 77 78 if (!dir.isAbsolute()) { 79 File serverHome = serverHome = ServerConfigLocator.locate().getServerHomeDir(); 80 dir = new File (serverHome, dir.getPath()); 81 } 82 83 if (!dir.exists()) { 84 if (!dir.mkdirs()) { 85 throw new IOException ("Failed to create directory: " + dir); 86 } 87 } 88 89 if (!dir.isDirectory()) { 90 throw new FileNotFoundException ("Given file reference is not a directory: " + dir); 91 } 92 93 if (!dir.canWrite()) { 94 throw new IOException ("Can not write to directory: " + dir); 95 } 96 97 if (!dir.canRead()) { 98 throw new IOException ("Can not read directory: " + dir); 99 } 100 101 103 this.dir = dir.getCanonicalFile(); 104 105 log.debug("Using directory for cache storage: " + dir); 106 107 this.mapFile = new File (dir, "state-map.ser"); 109 } 110 111 118 public File getDirectory() { 119 return this.dir; 120 } 121 122 133 public void setDirectoryName(String dirname) throws IOException 134 { 135 if (dirname == null) 136 throw new NullArgumentException("dirname"); 137 138 setDirectory(new File (dirname)); 139 } 140 141 148 public String getDirectoryName() { 149 return dir.getAbsolutePath(); 150 } 151 152 153 157 protected URL getURLFromFile(final File file) 158 { 159 try { 160 return file.toURL(); 161 } 162 catch (Exception e) { 163 throw new NestedRuntimeException(e); 165 } 166 } 167 168 public URL get(final URL url) 169 { 170 File file = (File )urlMap.get(url); 171 if (file == null) return null; 172 173 return getURLFromFile(file); 174 } 175 176 public URL put(final URL url) throws Exception 177 { 178 URL localURL = get(url); 179 File file; 180 181 if (localURL == null) { 182 CRC32 checksum = new CRC32 (); 185 checksum.update(url.toString().getBytes()); 186 187 String prefix = Long.toString(checksum.getValue(), Character.MAX_RADIX); 188 String filename = url.getFile(); 189 filename = filename.substring(filename.lastIndexOf("/") + 1, filename.length()); 190 191 file = new File (dir, prefix + "-" + filename); 192 urlMap.put(url, file); 193 writeMap(); 194 } 195 else { 196 file = (File )urlMap.get(url); 198 } 199 200 copyURL(url, file); 202 203 return getURLFromFile(file); 205 } 206 207 210 protected void copyURL(final URL source, final File dest) throws IOException 211 { 212 InputStream is = new BufferedInputStream (source.openConnection().getInputStream()); 213 OutputStream os = new BufferedOutputStream (new FileOutputStream (dest)); 214 215 try { 216 Streams.copy(is, os); 217 os.flush(); 218 } 219 finally { 220 os.close(); 221 is.close(); 222 } 223 } 224 225 228 protected Map readMap() throws ClassNotFoundException , IOException 229 { 230 if (mapFile.exists()) { 231 Map map; 232 233 InputStream is = new BufferedInputStream (new FileInputStream (mapFile)); 234 ObjectInputStream ois = new ObjectInputStream (is); 235 236 try { 237 map = (Map )ois.readObject(); 238 } 239 finally { 240 ois.close(); 241 } 242 243 return map; 244 } 245 else { 246 log.debug("Map file not found, creating new map"); 247 return new HashMap (); 248 } 249 } 250 251 254 protected void writeMap() throws IOException 255 { 256 OutputStream os = new BufferedOutputStream (new FileOutputStream (mapFile)); 257 ObjectOutputStream oos = new ObjectOutputStream (os); 258 259 try { 260 oos.writeObject(urlMap); 261 oos.flush(); 262 } 263 finally { 264 oos.close(); 265 } 266 } 267 268 269 273 276 protected void createService() throws Exception 277 { 278 if (dir == null) 279 throw new ConfigurationException("Missing attribute 'Directory'"); 280 281 urlMap = readMap(); 283 } 284 } 285 | Popular Tags |