KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > packtag > util > ContextConfiguration


1 /**
2  * Project pack:tag >> http://packtag.sf.net
3  *
4  * This software is published under the terms of the LGPL
5  * License version 2.1, a copy of which has been included with this
6  * distribution in the 'lgpl.txt' file.
7  *
8  * Creation date: 10.07.2007 - 23:52:52
9  * Last author: $Author: danielgalan $
10  * Last modified: $Date: 2007/07/11 23:01:31 $
11  * Revision: $Revision: 1.1 $
12  *
13  * $Log: ContextConfiguration.java,v $
14  * Revision 1.1 2007/07/11 23:01:31 danielgalan
15  * - 2.2
16  * - enhancement: caching header improved (servlet)
17  * - servlet url-mapping can be changed now (you have to set "packtag.cache.servlet.path" to the same value)
18  * - enhancement: polished the reference at http://www.galan.de/projects/packtag
19  *
20  */

21 package net.sf.packtag.util;
22
23 import javax.servlet.ServletContext JavaDoc;
24
25
26
27 /**
28  * Keeps track of the configuration settings in the web.xml file.
29  *
30  * @author Daniel Galán y Martins
31  * @version $Revision: 1.1 $
32  */

33 public class ContextConfiguration {
34
35     private final static String JavaDoc EMPTY_STRING = "";
36     private static final String JavaDoc SLASH = "/";
37
38     public final static String JavaDoc CACHE_TYPE_FILE = "file";
39     public final static String JavaDoc CACHE_TYPE_SERVLET = "servlet";
40     public final static String JavaDoc CACHE_TYPE_DISABLED = "disabled";
41
42     private static String JavaDoc cacheType;
43
44     /** Singleton for the cache servlet path */
45     private static String JavaDoc cacheServletPath;
46
47     /** Singleton for the cache file path */
48     private static String JavaDoc cacheFilePath;
49
50     /** Should the files timestamp be checked, so the resource is renewed on change? */
51     private static Boolean JavaDoc fileCheckTimestamps;
52
53     /** Should errors be shown, or hidden in the dark, resulting in unexpected behaviour/layout? */
54     private static Boolean JavaDoc hideErrors;
55
56
57     public static String JavaDoc getCacheType(ServletContext JavaDoc context) {
58         if (cacheType == null) {
59             synchronized (ContextConfiguration.class) {
60                 if (cacheType == null) {
61                     String JavaDoc type = context.getInitParameter("packtag.cache.type");
62                     if (type == null) {
63                         type = CACHE_TYPE_FILE;
64                     }
65                     cacheType = type;
66                 }
67             }
68         }
69         return cacheType;
70     }
71
72
73     public static boolean isCachetypeDisabled(ServletContext JavaDoc context) {
74         return CACHE_TYPE_DISABLED.equalsIgnoreCase(getCacheType(context));
75     }
76
77
78     public static boolean isCachetypeServlet(ServletContext JavaDoc context) {
79         return CACHE_TYPE_SERVLET.equalsIgnoreCase(getCacheType(context));
80     }
81
82
83     public static boolean isCachetypeFile(ServletContext JavaDoc context) {
84         return CACHE_TYPE_FILE.equalsIgnoreCase(getCacheType(context));
85     }
86
87
88     public static boolean isFileCheckTimestamps(ServletContext JavaDoc context) {
89         if (fileCheckTimestamps == null) {
90             synchronized (ContextConfiguration.class) {
91                 if (fileCheckTimestamps == null) {
92                     String JavaDoc parameter = context.getInitParameter("packtag.resources.checktimestamps");
93                     if (parameter == null || parameter.equals(EMPTY_STRING)) {
94                         fileCheckTimestamps = Boolean.TRUE; // default value
95
}
96                     else {
97                         fileCheckTimestamps = new Boolean JavaDoc(parameter);
98                     }
99                 }
100             }
101         }
102         return fileCheckTimestamps.booleanValue();
103     }
104
105
106     /** Should errors be hidden? (Not recommended) */
107     public static boolean isHideErros(ServletContext JavaDoc context) {
108         if (hideErrors == null) {
109             synchronized (ContextConfiguration.class) {
110                 if (hideErrors == null) {
111                     hideErrors = new Boolean JavaDoc(context.getInitParameter("packtag.hide.errors"));
112                 }
113             }
114         }
115         return hideErrors.booleanValue();
116     }
117
118
119     public static String JavaDoc getPackStrategyClassName(ServletContext JavaDoc context, String JavaDoc resourceType) {
120         return context.getInitParameter("packtag." + resourceType + ".strategy");
121     }
122
123
124     public static String JavaDoc getCacheServletPath(ServletContext JavaDoc context) {
125         if (cacheServletPath == null) {
126             synchronized (ContextConfiguration.class) {
127                 if (cacheServletPath == null) {
128                     String JavaDoc path = context.getInitParameter("packtag.cache.servlet.path");
129                     if ((path == null) || (path.length() == 0)) {
130                         path = "/pack";
131                     }
132                     if (!path.startsWith(SLASH)) {
133                         path = SLASH + path;
134                     }
135                     if (path.endsWith(SLASH)) {
136                         path = path.substring(0, path.length() - 1);
137                     }
138                     cacheServletPath = path;
139                 }
140             }
141         }
142         return cacheServletPath;
143     }
144
145
146     public static String JavaDoc getCacheFilePath(ServletContext JavaDoc context) {
147         if (cacheFilePath == null) {
148             synchronized (ContextConfiguration.class) {
149                 if (cacheFilePath == null) {
150                     String JavaDoc path = context.getInitParameter("packtag.cache.file.path");
151                     if ((path == null) || (path.length() == 0)) {
152                         path = "pack";
153                     }
154                     if (path.startsWith(SLASH)) {
155                         path = path.substring(1, path.length());
156                     }
157                     if (path.endsWith(SLASH)) {
158                         path = path.substring(0, path.length() - 1);
159                     }
160                     cacheFilePath = path;
161                 }
162             }
163         }
164         return cacheFilePath;
165     }
166
167 }
168
Popular Tags