Mercurial > hg > digilib-old
annotate servlet2/src/main/java/digilib/servlet/DigilibServletConfiguration.java @ 1158:2ee261676828 default tip
better out-of-the box experience:
* digilib works without config files using sensible defaults
* new sample images folder used by default
* config files moved to templates
author | robcast |
---|---|
date | Tue, 19 Feb 2013 17:32:25 +0100 |
parents | 4e368c85cce4 |
children |
rev | line source |
---|---|
903 | 1 /* |
2 * DigilibConfiguration -- Holding all parameters for digilib servlet. | |
3 * | |
4 * Digital Image Library servlet components | |
5 * | |
6 * Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de) | |
7 * | |
8 * This program is free software; you can redistribute it and/or modify it | |
9 * under the terms of the GNU General Public License as published by the Free | |
10 * Software Foundation; either version 2 of the License, or (at your option) | |
11 * any later version. | |
12 * | |
13 * Please read license.txt for the full details. A copy of the GPL may be found | |
14 * at http://www.gnu.org/copyleft/lgpl.html | |
15 * | |
16 * You should have received a copy of the GNU General Public License along with | |
17 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple | |
18 * Place, Suite 330, Boston, MA 02111-1307 USA | |
19 * | |
20 */ | |
21 | |
22 package digilib.servlet; | |
23 | |
24 import java.io.File; | |
25 import java.util.Map; | |
26 import java.util.Map.Entry; | |
1158 | 27 import java.util.concurrent.atomic.AtomicInteger; |
903 | 28 |
29 import javax.servlet.ServletContext; | |
30 import javax.servlet.ServletException; | |
31 | |
32 import digilib.image.DocuImageImpl; | |
33 import digilib.io.FileOps; | |
34 import digilib.util.Parameter; | |
35 import digilib.util.XMLListLoader; | |
36 | |
37 /** | |
38 * Class to hold the digilib servlet configuration parameters. The parameters | |
39 * can be read from the digilib-config file and be passed to other servlets or | |
1158 | 40 * beans. <br> |
41 * errorImgFileName: image file to send in case of error. <br> | |
42 * denyImgFileName: image file to send if access is denied. <br> | |
43 * baseDirs: array of base directories in order of preference (prescaled | |
44 * versions first). <br> | |
45 * useAuth: use authentication information. <br> | |
46 * authConfPath: authentication configuration file. <br> | |
47 * ... <br> | |
903 | 48 * |
49 * @author casties | |
1158 | 50 * |
903 | 51 */ |
52 public class DigilibServletConfiguration extends DigilibConfiguration { | |
53 | |
1158 | 54 /** time the webapp (i.e. this class) was loaded */ |
55 public final Long webappStartTime = System.currentTimeMillis(); | |
56 | |
57 /** counter for HttpRequests (mostly for debugging) */ | |
58 public AtomicInteger webappRequestCnt = new AtomicInteger(0); | |
59 | |
60 /** counter for open HttpRequests (mostly for debugging) */ | |
61 public AtomicInteger openRequestCnt = new AtomicInteger(0); | |
903 | 62 |
1158 | 63 /** |
64 * Definition of parameters and default values. | |
65 */ | |
66 protected void initParams() { | |
67 /* | |
68 * Definition of parameters and default values. System parameters that | |
69 * are not read from config file have a type 's'. | |
70 */ | |
71 | |
72 // digilib servlet version | |
73 newParameter("servlet.version", digilib.servlet.Scaler.getVersion(), null, 's'); | |
74 // configuration file location | |
75 newParameter("servlet.config.file", null, null, 's'); | |
76 // DocuDirCache instance | |
77 newParameter("servlet.dir.cache", null, null, 's'); | |
78 // DocuImage class instance | |
79 newParameter("servlet.docuimage.class", digilib.image.ImageLoaderDocuImage.class, null, 's'); | |
80 // DocuImage version | |
81 newParameter("servlet.docuimage.version", "?", null, 's'); | |
82 // AuthOps instance for authentication | |
83 newParameter("servlet.auth.op", null, null, 's'); | |
903 | 84 // Executor for image operations |
85 newParameter("servlet.worker.imageexecutor", null, null, 's'); | |
86 // Executor for PDF operations | |
87 newParameter("servlet.worker.pdfexecutor", null, null, 's'); | |
88 // Executor for PDF-image operations | |
89 newParameter("servlet.worker.pdfimageexecutor", null, null, 's'); | |
90 | |
1158 | 91 /* |
92 * parameters that can be read from config file have a type 'f' | |
93 */ | |
903 | 94 |
1158 | 95 // image file to send in case of error |
96 newParameter("error-image", new File("img/digilib-error.png"), null, 'f'); | |
97 // image file to send if access is denied | |
98 newParameter("denied-image", new File("img/digilib-denied.png"), null, 'f'); | |
99 // image file to send if image file not found | |
100 newParameter("notfound-image", new File("img/digilib-notfound.png"), null, 'f'); | |
101 // base directories in order of preference (prescaled versions last) | |
102 String[] bd = { "sample-images" }; | |
103 newParameter("basedir-list", bd, null, 'f'); | |
104 // use authentication information | |
105 newParameter("use-authorization", Boolean.FALSE, null, 'f'); | |
106 // authentication configuration file | |
107 newParameter("auth-file", new File("digilib-auth.xml"), null, 'f'); | |
108 // sending image files as-is allowed | |
109 newParameter("sendfile-allowed", Boolean.TRUE, null, 'f'); | |
110 // Type of DocuImage instance | |
111 newParameter("docuimage-class", "digilib.image.ImageLoaderDocuImage", null, 'f'); | |
112 // part of URL used to indicate authorized access | |
113 newParameter("auth-url-path", "authenticated/", null, 'f'); | |
114 // degree of subsampling on image load | |
115 newParameter("subsample-minimum", new Float(2f), null, 'f'); | |
116 // default scaling quality | |
117 newParameter("default-quality", new Integer(2), null, 'f'); | |
118 // use mapping file to translate paths | |
119 newParameter("use-mapping", Boolean.FALSE, null, 'f'); | |
120 // mapping file location | |
121 newParameter("mapping-file", new File("digilib-map.xml"), null, 'f'); | |
122 // log4j config file location | |
123 newParameter("log-config-file", new File("log4j-config.xml"), null, 'f'); | |
124 // maximum destination image size (0 means no limit) | |
125 newParameter("max-image-size", new Integer(0), null, 'f'); | |
126 // number of working threads | |
127 newParameter("worker-threads", new Integer(1), null, 'f'); | |
128 // max number of waiting threads | |
129 newParameter("max-waiting-threads", new Integer(20), null, 'f'); | |
130 // timeout for worker threads (ms) | |
131 newParameter("worker-timeout", new Integer(60000), null, 'f'); | |
132 // number of pdf-generation threads | |
133 newParameter("pdf-worker-threads", new Integer(1), null, 'f'); | |
134 // max number of waiting pdf-generation threads | |
135 newParameter("pdf-max-waiting-threads", new Integer(20), null, 'f'); | |
136 // number of pdf-image generation threads | |
137 newParameter("pdf-image-worker-threads", new Integer(1), null, 'f'); | |
138 // max number of waiting pdf-image generation threads | |
139 newParameter("pdf-image-max-waiting-threads", new Integer(10), null, 'f'); | |
903 | 140 // PDF generation temp directory |
141 newParameter("pdf-temp-dir", "pdf_temp", null, 'f'); | |
142 // PDF generation cache directory | |
143 newParameter("pdf-cache-dir", "pdf_cache", null, 'f'); | |
144 // allow image toolkit to use disk cache | |
145 newParameter("img-diskcache-allowed", Boolean.TRUE, null, 'f'); | |
146 // default type of error message (image, text, code) | |
147 newParameter("default-errmsg-type", "image", null, 'f'); | |
1158 | 148 } |
903 | 149 |
1158 | 150 /** |
151 * Constructor taking a ServletConfig. Reads the config file location from | |
152 * an init parameter and loads the config file. Calls | |
153 * <code>readConfig()</code>. | |
154 * | |
155 * @see readConfig() | |
156 */ | |
157 public DigilibServletConfiguration(ServletContext c) throws Exception { | |
158 readConfig(c); | |
159 } | |
903 | 160 |
911 | 161 /** |
162 * read parameter list from the XML file in init parameter "config-file" or | |
163 * file digilib-config.xml | |
164 */ | |
165 @SuppressWarnings("unchecked") | |
903 | 166 public void readConfig(ServletContext c) throws Exception { |
167 | |
911 | 168 /* |
169 * Get config file name. The file name is first looked for as an init | |
170 * parameter, then in a fixed location in the webapp. | |
171 */ | |
172 if (c == null) { | |
173 // no config no file... | |
174 return; | |
175 } | |
176 String fn = c.getInitParameter("config-file"); | |
177 if (fn == null) { | |
1158 | 178 logger.debug("readConfig: no param config-file"); |
911 | 179 fn = ServletOps.getConfigFile("digilib-config.xml", c); |
180 } | |
181 File f = new File(fn); | |
1158 | 182 if (f.canRead()) { |
183 // setup config file list reader | |
184 XMLListLoader lilo = new XMLListLoader("digilib-config", "parameter", "name", "value"); | |
185 // read config file into HashMap | |
186 Map<String, String> confTable = lilo.loadURL(f.toURL().toString()); | |
903 | 187 |
1158 | 188 // set config file path parameter |
189 setValue("servlet.config.file", f.getCanonicalPath()); | |
903 | 190 |
1158 | 191 /* |
192 * read parameters | |
193 */ | |
903 | 194 |
1158 | 195 for (Entry<String, String> confEntry : confTable.entrySet()) { |
196 Parameter p = get(confEntry.getKey()); | |
197 if (p != null) { | |
198 if (p.getType() == 's') { | |
199 // type 's' Parameters are not overwritten. | |
200 continue; | |
201 } | |
202 if (!p.setValueFromString(confEntry.getValue())) { | |
203 /* | |
204 * automatic conversion failed -- try special cases | |
205 */ | |
903 | 206 |
1158 | 207 // basedir-list |
208 if (confEntry.getKey().equals("basedir-list")) { | |
209 // split list into directories | |
210 String[] dirs = FileOps.pathToArray(confEntry.getValue()); | |
211 for (int j = 0; j < dirs.length; j++) { | |
212 // make relative directory paths be inside the | |
213 // webapp | |
214 dirs[j] = ServletOps.getFile(dirs[j], c); | |
215 } | |
216 if (dirs != null) { | |
217 p.setValue(dirs); | |
218 } | |
911 | 219 } |
220 } | |
1158 | 221 } else { |
222 // parameter unknown -- just add | |
223 newParameter(confEntry.getKey(), null, confEntry.getValue(), 'f'); | |
911 | 224 } |
1158 | 225 } |
226 } else { | |
227 logger.warn("No digilib config file! Using defaults!"); | |
228 // update basedir-list | |
229 String[] dirs = (String[]) this.getValue("basedir-list"); | |
230 for (int j = 0; j < dirs.length; j++) { | |
231 // make relative directory paths be inside the | |
232 // webapp | |
233 dirs[j] = ServletOps.getFile(dirs[j], c); | |
911 | 234 } |
235 } | |
236 // initialise static DocuImage class instance | |
1158 | 237 DigilibServletConfiguration.docuImageClass = (Class<DocuImageImpl>) Class.forName(getAsString("docuimage-class")); |
1032
4e368c85cce4
CLOSED - # 22: wrong contrast setting. dito #23 (at least on OSX 10.7)
robcast
parents:
960
diff
changeset
|
238 setValue("servlet.docuimage.version", getDocuImageInstance().getVersion()); |
911 | 239 } |
903 | 240 |
241 } |