1 17 18 19 package org.apache.catalina.startup; 20 21 import java.io.BufferedOutputStream ; 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileOutputStream ; 25 import java.io.InputStream ; 26 import java.io.IOException ; 27 import java.net.JarURLConnection ; 28 import java.net.URL ; 29 import java.nio.channels.FileChannel ; 30 import java.util.Enumeration ; 31 import java.util.jar.JarEntry ; 32 import java.util.jar.JarFile ; 33 34 import org.apache.catalina.Host; 35 import org.apache.catalina.util.StringManager; 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 39 47 48 public class ExpandWar { 49 50 private static Log log = LogFactory.getLog(ExpandWar.class); 51 52 55 protected static final StringManager sm = 56 StringManager.getManager(Constants.Package); 57 58 59 72 public static String expand(Host host, URL war) 73 throws IOException { 74 75 if (host.getLogger().isDebugEnabled()) { 77 host.getLogger().debug("expand(" + war.toString() + ")"); 78 } 79 String pathname = war.toString().replace('\\', '/'); 80 if (pathname.endsWith("!/")) { 81 pathname = pathname.substring(0, pathname.length() - 2); 82 } 83 int period = pathname.lastIndexOf('.'); 84 if (period >= pathname.length() - 4) 85 pathname = pathname.substring(0, period); 86 int slash = pathname.lastIndexOf('/'); 87 if (slash >= 0) { 88 pathname = pathname.substring(slash + 1); 89 } 90 if (host.getLogger().isDebugEnabled()) { 91 host.getLogger().debug(" Proposed directory name: " + pathname); 92 } 93 return expand(host, war, pathname); 94 95 } 96 97 98 112 public static String expand(Host host, URL war, String pathname) 113 throws IOException { 114 115 File appBase = new File (host.getAppBase()); 117 if (!appBase.isAbsolute()) { 118 appBase = new File (System.getProperty("catalina.base"), 119 host.getAppBase()); 120 } 121 if (!appBase.exists() || !appBase.isDirectory()) { 122 throw new IOException 123 (sm.getString("hostConfig.appBase", 124 appBase.getAbsolutePath())); 125 } 126 File docBase = new File (appBase, pathname); 127 if (docBase.exists()) { 128 return (docBase.getAbsolutePath()); 130 } 131 132 docBase.mkdir(); 134 135 JarURLConnection juc = (JarURLConnection ) war.openConnection(); 137 juc.setUseCaches(false); 138 JarFile jarFile = null; 139 InputStream input = null; 140 try { 141 jarFile = juc.getJarFile(); 142 Enumeration jarEntries = jarFile.entries(); 143 while (jarEntries.hasMoreElements()) { 144 JarEntry jarEntry = (JarEntry ) jarEntries.nextElement(); 145 String name = jarEntry.getName(); 146 int last = name.lastIndexOf('/'); 147 if (last >= 0) { 148 File parent = new File (docBase, 149 name.substring(0, last)); 150 parent.mkdirs(); 151 } 152 if (name.endsWith("/")) { 153 continue; 154 } 155 input = jarFile.getInputStream(jarEntry); 156 157 File expandedFile = expand(input, docBase, name); 159 long lastModified = jarEntry.getTime(); 160 if ((lastModified != -1) && (lastModified != 0) && (expandedFile != null)) { 161 expandedFile.setLastModified(lastModified); 162 } 163 164 input.close(); 165 input = null; 166 } 167 } catch (IOException e) { 168 deleteDir(docBase); 171 throw e; 172 } finally { 173 if (input != null) { 174 try { 175 input.close(); 176 } catch (Throwable t) { 177 ; 178 } 179 input = null; 180 } 181 if (jarFile != null) { 182 try { 183 jarFile.close(); 184 } catch (Throwable t) { 185 ; 186 } 187 jarFile = null; 188 } 189 } 190 191 return (docBase.getAbsolutePath()); 193 194 } 195 196 197 203 public static boolean copy(File src, File dest) { 204 205 boolean result = true; 206 207 String files[] = null; 208 if (src.isDirectory()) { 209 files = src.list(); 210 result = dest.mkdir(); 211 } else { 212 files = new String [1]; 213 files[0] = ""; 214 } 215 if (files == null) { 216 files = new String [0]; 217 } 218 for (int i = 0; (i < files.length) && result; i++) { 219 File fileSrc = new File (src, files[i]); 220 File fileDest = new File (dest, files[i]); 221 if (fileSrc.isDirectory()) { 222 result = copy(fileSrc, fileDest); 223 } else { 224 FileChannel ic = null; 225 FileChannel oc = null; 226 try { 227 ic = (new FileInputStream (fileSrc)).getChannel(); 228 oc = (new FileOutputStream (fileDest)).getChannel(); 229 ic.transferTo(0, ic.size(), oc); 230 } catch (IOException e) { 231 log.error(sm.getString 232 ("expandWar.copy", fileSrc, fileDest), e); 233 result = false; 234 } finally { 235 if (ic != null) { 236 try { 237 ic.close(); 238 } catch (IOException e) { 239 } 240 } 241 if (oc != null) { 242 try { 243 oc.close(); 244 } catch (IOException e) { 245 } 246 } 247 } 248 } 249 } 250 return result; 251 252 } 253 254 255 261 public static boolean delete(File dir) { 262 if (dir.isDirectory()) { 263 return deleteDir(dir); 264 } else { 265 return dir.delete(); 266 } 267 } 268 269 270 276 public static boolean deleteDir(File dir) { 277 278 String files[] = dir.list(); 279 if (files == null) { 280 files = new String [0]; 281 } 282 for (int i = 0; i < files.length; i++) { 283 File file = new File (dir, files[i]); 284 if (file.isDirectory()) { 285 deleteDir(file); 286 } else { 287 file.delete(); 288 } 289 } 290 return dir.delete(); 291 292 } 293 294 295 306 protected static File expand(InputStream input, File docBase, String name) 307 throws IOException { 308 309 File file = new File (docBase, name); 310 BufferedOutputStream output = null; 311 try { 312 output = 313 new BufferedOutputStream (new FileOutputStream (file)); 314 byte buffer[] = new byte[2048]; 315 while (true) { 316 int n = input.read(buffer); 317 if (n <= 0) 318 break; 319 output.write(buffer, 0, n); 320 } 321 } finally { 322 if (output != null) { 323 try { 324 output.close(); 325 } catch (IOException e) { 326 } 328 } 329 } 330 331 return file; 332 } 333 334 335 } 336 | Popular Tags |