1 17 package org.apache.ws.jaxme.webapp; 18 19 import java.io.File ; 20 21 import javax.servlet.ServletException ; 22 import javax.servlet.UnavailableException ; 23 import javax.servlet.http.HttpServlet ; 24 25 26 public abstract class BaseServlet extends HttpServlet { 27 private static File workDir; 28 29 public File getWorkDir() { 30 return workDir; 31 } 32 33 public void cleanDirectory(File pDirectory) throws ServletException { 34 File [] files = pDirectory.listFiles(); 35 for (int i = 0; i < files.length; i++) { 36 File f = files[i]; 37 if (f.isFile()) { 38 if (!f.delete()) { 39 throw new UnavailableException ("Unable to delete file " + f.getAbsolutePath()); 40 } 41 } else if (f.isDirectory()) { 42 cleanDirectory(f); 43 if (!f.delete()) { 44 throw new UnavailableException ("Unable to delete directory " + f.getAbsolutePath()); 45 } 46 } else { 47 throw new UnavailableException ("Unable to determine how to remove " + f.getAbsolutePath()); 48 } 49 } 50 } 51 52 public void createDirectory(File pDirectory) throws ServletException { 53 if (!pDirectory.mkdir()) { 54 throw new UnavailableException ("Unable to create working directory " + pDirectory); 55 } 56 } 57 58 public void init() throws ServletException { 59 synchronized (BaseServlet.class) { 60 if (workDir == null) { 61 String p = getServletContext().getInitParameter("http.proxyHost"); 62 if (p != null && p.length() > 0) { 63 String v = getServletContext().getInitParameter("http.proxyPort"); 64 if (v != null && v.length() > 0) { 65 log("http.proxyHost parameter detected, setting host=" + p + ", port=" + v); 66 System.setProperty("http.proxyHost", p); 67 System.setProperty("http.proxyPort", v); 68 } else { 69 throw new UnavailableException ("The http.proxyHost parameter is set, but the http.proxyPort parameter is not set."); 70 } 71 } else { 72 log("http.proxyHost parameter is not set"); 73 } 74 String s = getServletContext().getInitParameter("work.dir"); 75 File f; 76 if (s == null || s.length() == 0) { 77 s = System.getProperty("java.io.tmpdir"); 78 if (s == null || s.length() == 0) { 79 throw new UnavailableException ("Neither the servlet context parameter work.dir nor the system property java.io.tmpdir are set."); 80 } 81 f = new File (s); 82 if (!f.isDirectory()) { 83 throw new UnavailableException ("The directory " + s + " (specified by the system property java.io.tmpdir) does not exist."); 84 } 85 } else { 86 f = new File (s); 87 if (!f.isDirectory()) { 88 throw new UnavailableException ("The directory " + s + " (specified by the servlet context parameter work.dir) does not exist."); 89 } 90 } 91 92 File g = new File (f, "jaxme"); 93 if (g.isDirectory()) { 94 cleanDirectory(g); 95 } else { 96 createDirectory(g); 97 } 98 workDir = g; 99 } 100 } 101 } 102 } 103 | Popular Tags |