1 /***
2 * The contents of this file are subject to the Mozilla Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/MPL/
6 *
7 * Software distributed under the License is distributed on an "AS
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 * implied. See the License for the specific language governing
10 * rights and limitations under the License.
11 *
12 * The Original Code is pow2toolkit library.
13 *
14 * The Initial Owner of the Original Code is
15 * Power Of Two S.R.L. (www.pow2.com)
16 *
17 * Portions created by Power Of Two S.R.L. are
18 * Copyright (C) Power Of Two S.R.L.
19 * All Rights Reserved.
20 *
21 * Contributor(s):
22 */
23
24 package com.pow2.resources;
25
26
27 import java.io.File;
28
29 import javax.servlet.http.*;
30
31 import org.apache.log4j.Category;
32
33 import com.pow2.util.Util;
34
35
36 /***
37 * Pow2 init servlet.
38 *
39 * @author Luca Fossato
40 * @created 2 maggio 2002
41 */
42 public abstract class InitServlet extends HttpServlet
43 {
44 /*** log4j category */
45 protected final Category cat = Category.getInstance(this.getClass());
46
47 /***
48 * Get the absolute filepath of the file declared by the input
49 * <code>initParameter</code> parameter.
50 *
51 * @param initParameter the name of the init-parameter declared into the
52 * web application's web.xml configuration file.
53 * The initParameter value must be a valid filename.
54 *
55 * @return the absolute filepath of the file name declared by the input
56 * <code>initParameter</code> parameter.
57 */
58 protected String getInitParameterFilePath(String initParameter)
59 {
60 String fileName = null;
61 String filePrefix = null;
62
63
64 if (Util.isNull(fileName = getInitParameter(initParameter)))
65 {
66 cat.error("::init - the file name of the file declared by the init-parameter ["
67 + initParameter + "] is null or empty");
68 return null;
69 }
70
71
72
73 if (filePathIsRelative(fileName))
74 return (getWebAppBasePath() + fileName);
75
76
77 return (fileExists(fileName)) ? fileName : null;
78 }
79
80
81 /***
82 * PRIVATE METHODS here
83 */
84
85
86 /***
87 * Check if the file declared by the input <code>fileName</code>
88 * parameter exists into the web application directory.
89 *
90 * @return true if the file exists, false otherwise.
91 */
92 private boolean filePathIsRelative(String fileName)
93 {
94 String filePrefix = null;
95 File file = null;
96
97 filePrefix = getWebAppBasePath();
98 fileName = (filePrefix + "/" + fileName);
99
100 return fileExists(fileName);
101 }
102
103
104 /***
105 * Check if the input file name exists.
106 *
107 * @param fileName the full path name of the file to check
108 * @return true if the file exists, false otherwise.
109 */
110 private boolean fileExists(String fileName)
111 {
112 return (new File(fileName).exists());
113 }
114
115
116 /***
117 * Return the web application absolute filepath.
118 *
119 * @return the web application absolute filepath.
120 */
121 private String getWebAppBasePath()
122 {
123 return (getServletContext().getRealPath("/"));
124 }
125 }