KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > webapp > BaseServlet


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.webapp;
18
19 import java.io.File JavaDoc;
20
21 import javax.servlet.ServletException JavaDoc;
22 import javax.servlet.UnavailableException JavaDoc;
23 import javax.servlet.http.HttpServlet JavaDoc;
24
25
26 public abstract class BaseServlet extends HttpServlet JavaDoc {
27     private static File JavaDoc workDir;
28     
29     public File JavaDoc getWorkDir() {
30         return workDir;
31     }
32
33     public void cleanDirectory(File JavaDoc pDirectory) throws ServletException JavaDoc {
34         File JavaDoc[] files = pDirectory.listFiles();
35         for (int i = 0; i < files.length; i++) {
36             File JavaDoc f = files[i];
37             if (f.isFile()) {
38                 if (!f.delete()) {
39                     throw new UnavailableException JavaDoc("Unable to delete file " + f.getAbsolutePath());
40                 }
41             } else if (f.isDirectory()) {
42                 cleanDirectory(f);
43                 if (!f.delete()) {
44                     throw new UnavailableException JavaDoc("Unable to delete directory " + f.getAbsolutePath());
45                 }
46             } else {
47                 throw new UnavailableException JavaDoc("Unable to determine how to remove " + f.getAbsolutePath());
48             }
49         }
50     }
51
52     public void createDirectory(File JavaDoc pDirectory) throws ServletException JavaDoc {
53         if (!pDirectory.mkdir()) {
54             throw new UnavailableException JavaDoc("Unable to create working directory " + pDirectory);
55         }
56     }
57
58     public void init() throws ServletException JavaDoc {
59         synchronized (BaseServlet.class) {
60             if (workDir == null) {
61                 String JavaDoc p = getServletContext().getInitParameter("http.proxyHost");
62                 if (p != null && p.length() > 0) {
63                     String JavaDoc 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 JavaDoc("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 JavaDoc s = getServletContext().getInitParameter("work.dir");
75                 File JavaDoc 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 JavaDoc("Neither the servlet context parameter work.dir nor the system property java.io.tmpdir are set.");
80                     }
81                     f = new File JavaDoc(s);
82                     if (!f.isDirectory()) {
83                         throw new UnavailableException JavaDoc("The directory " + s + " (specified by the system property java.io.tmpdir) does not exist.");
84                     }
85                 } else {
86                     f = new File JavaDoc(s);
87                     if (!f.isDirectory()) {
88                         throw new UnavailableException JavaDoc("The directory " + s + " (specified by the servlet context parameter work.dir) does not exist.");
89                     }
90                 }
91
92                 File JavaDoc g = new File JavaDoc(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