View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * 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 and
15   * limitations under the License.
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          // Try the name as provided first.
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                      // The named parameter didn't yield a value.
82                      if (usingNamespace)
83                      {
84                          path = defaultValue;
85                      }
86                      else
87                      {
88                          // Try again using Jetspeed's namespace.
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             // Create the logging directory
114             if (!logDir.mkdirs())
115             {
116                 throw new ServletException("Cannot create directory for logs!");
117             }
118         }
119     }
120  }