1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.engine.servlet;
18
19 import java.io.File;
20
21 import javax.servlet.ServletConfig;
22 import javax.servlet.ServletContext;
23 import javax.servlet.ServletException;
24
25 import org.apache.commons.lang.StringUtils;
26
27 /***
28 * Servlet Helper functions
29 *
30 * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>
31 * @version $Id: ServletHelper.java 516448 2007-03-09 16:25:47Z ate $
32 */
33 public class ServletHelper
34 {
35 public static final String CONFIG_NAMESPACE = "org.apache.jetspeed";
36
37 /*** Default Value for the Logging Directory, relative to the webroot */
38 public static final String LOGGING_ROOT_DEFAULT = "/logs";
39 public static final String LOGGING_ROOT = "loggingRoot";
40
41 /***
42 * Used to get the real path of configuration and resource
43 * information.
44 *
45 * @param path path translated to the application root
46 * @return the real path
47 */
48 public static String getRealPath(ServletConfig config, String path)
49 {
50 if (path.startsWith("/"))
51 {
52 path = path.substring(1);
53 }
54
55 return new File(config.getServletContext().getRealPath(""), path).getAbsolutePath();
56 }
57
58 /***
59 * Finds the specified servlet configuration/initialization
60 * parameter, looking first for a servlet-specific parameter, then
61 * for a global parameter, and using the provided default if not
62 * found.
63 */
64 public static final String findInitParameter(ServletContext context,
65 ServletConfig config,
66 String name,
67 String defaultValue)
68 {
69 String path = null;
70
71
72 boolean usingNamespace = name.startsWith(CONFIG_NAMESPACE);
73 while (true)
74 {
75 path = config.getInitParameter(name);
76 if (StringUtils.isEmpty(path))
77 {
78 path = context.getInitParameter(name);
79 if (StringUtils.isEmpty(path))
80 {
81
82 if (usingNamespace)
83 {
84 path = defaultValue;
85 }
86 else
87 {
88
89 name = CONFIG_NAMESPACE + '.' + name;
90 usingNamespace = true;
91 continue;
92 }
93 }
94 }
95 break;
96 }
97
98 return path;
99 }
100
101 /***
102 * Create any directories that might be needed during
103 *
104 */
105 public static void createRuntimeDirectories(ServletContext context,
106 ServletConfig config)
107 throws ServletException
108 {
109 String path = findInitParameter(context, config, LOGGING_ROOT, LOGGING_ROOT_DEFAULT);
110 File logDir = new File(getRealPath(config, path));
111 if (!logDir.exists())
112 {
113
114 if (!logDir.mkdirs())
115 {
116 throw new ServletException("Cannot create directory for logs!");
117 }
118 }
119 }
120 }