001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.engine.servlet;
018:
019: import java.io.File;
020:
021: import javax.servlet.ServletConfig;
022: import javax.servlet.ServletContext;
023: import javax.servlet.ServletException;
024:
025: import org.apache.commons.lang.StringUtils;
026:
027: /**
028: * Servlet Helper functions
029: *
030: * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>
031: * @version $Id: ServletHelper.java 516448 2007-03-09 16:25:47Z ate $
032: */
033: public class ServletHelper {
034: public static final String CONFIG_NAMESPACE = "org.apache.jetspeed";
035:
036: /** Default Value for the Logging Directory, relative to the webroot */
037: public static final String LOGGING_ROOT_DEFAULT = "/logs";
038: public static final String LOGGING_ROOT = "loggingRoot";
039:
040: /**
041: * Used to get the real path of configuration and resource
042: * information.
043: *
044: * @param path path translated to the application root
045: * @return the real path
046: */
047: public static String getRealPath(ServletConfig config, String path) {
048: if (path.startsWith("/")) {
049: path = path.substring(1);
050: }
051:
052: return new File(config.getServletContext().getRealPath(""),
053: path).getAbsolutePath();
054: }
055:
056: /**
057: * Finds the specified servlet configuration/initialization
058: * parameter, looking first for a servlet-specific parameter, then
059: * for a global parameter, and using the provided default if not
060: * found.
061: */
062: public static final String findInitParameter(
063: ServletContext context, ServletConfig config, String name,
064: String defaultValue) {
065: String path = null;
066:
067: // Try the name as provided first.
068: boolean usingNamespace = name.startsWith(CONFIG_NAMESPACE);
069: while (true) {
070: path = config.getInitParameter(name);
071: if (StringUtils.isEmpty(path)) {
072: path = context.getInitParameter(name);
073: if (StringUtils.isEmpty(path)) {
074: // The named parameter didn't yield a value.
075: if (usingNamespace) {
076: path = defaultValue;
077: } else {
078: // Try again using Jetspeed's namespace.
079: name = CONFIG_NAMESPACE + '.' + name;
080: usingNamespace = true;
081: continue;
082: }
083: }
084: }
085: break;
086: }
087:
088: return path;
089: }
090:
091: /**
092: * Create any directories that might be needed during
093: *
094: */
095: public static void createRuntimeDirectories(ServletContext context,
096: ServletConfig config) throws ServletException {
097: String path = findInitParameter(context, config, LOGGING_ROOT,
098: LOGGING_ROOT_DEFAULT);
099: File logDir = new File(getRealPath(config, path));
100: if (!logDir.exists()) {
101: // Create the logging directory
102: if (!logDir.mkdirs()) {
103: throw new ServletException(
104: "Cannot create directory for logs!");
105: }
106: }
107: }
108: }
|