1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 * 9 * http://www.apache.org/licenses/LICENSE-2.010 * 11 * Unless required by applicable law or agreed to in writing, software12 * distributed under the License is distributed on an "AS IS" BASIS,13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14 * See the License for the specific language governing permissions and15 * limitations under the License.16 */17packageorg.apache.jetspeed.engine.servlet;
1819import java.io.File;
2021import javax.servlet.ServletConfig;
22import javax.servlet.ServletContext;
23import javax.servlet.ServletException;
2425import org.apache.commons.lang.StringUtils;
2627/***28 * Servlet Helper functions29 *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 */33publicclassServletHelper34 {
35publicstaticfinal String CONFIG_NAMESPACE = "org.apache.jetspeed";
3637/*** Default Value for the Logging Directory, relative to the webroot */38publicstaticfinal String LOGGING_ROOT_DEFAULT = "/logs";
39publicstaticfinal String LOGGING_ROOT = "loggingRoot";
4041/***42 * Used to get the real path of configuration and resource43 * information. 44 *45 * @param path path translated to the application root46 * @return the real path47 */48publicstatic String getRealPath(ServletConfig config, String path)
49 {
50if (path.startsWith("/"))
51 {
52 path = path.substring(1);
53 }
5455returnnew File(config.getServletContext().getRealPath(""), path).getAbsolutePath();
56 }
5758/***59 * Finds the specified servlet configuration/initialization60 * parameter, looking first for a servlet-specific parameter, then61 * for a global parameter, and using the provided default if not62 * found.63 */64publicstaticfinal String findInitParameter(ServletContext context,
65 ServletConfig config,
66 String name,
67 String defaultValue)
68 {
69 String path = null;
7071// Try the name as provided first.72boolean usingNamespace = name.startsWith(CONFIG_NAMESPACE);
73while (true)
74 {
75 path = config.getInitParameter(name);
76if (StringUtils.isEmpty(path))
77 {
78 path = context.getInitParameter(name);
79if (StringUtils.isEmpty(path))
80 {
81// The named parameter didn't yield a value.82if (usingNamespace)
83 {
84 path = defaultValue;
85 }
86else87 {
88// Try again using Jetspeed's namespace.89 name = CONFIG_NAMESPACE + '.' + name;
90 usingNamespace = true;
91continue;
92 }
93 }
94 }
95break;
96 }
9798return path;
99 }
100101/***102 * Create any directories that might be needed during103 *104 */105publicstaticvoid 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));
111if (!logDir.exists())
112 {
113// Create the logging directory114if (!logDir.mkdirs())
115 {
116thrownew ServletException("Cannot create directory for logs!");
117 }
118 }
119 }
120 }