comparison 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
comparison
equal deleted inserted replaced
1157:7a4f27ee087f 1158:2ee261676828
22 package digilib.servlet; 22 package digilib.servlet;
23 23
24 import java.io.File; 24 import java.io.File;
25 import java.util.Map; 25 import java.util.Map;
26 import java.util.Map.Entry; 26 import java.util.Map.Entry;
27 import java.util.concurrent.atomic.AtomicInteger;
27 28
28 import javax.servlet.ServletContext; 29 import javax.servlet.ServletContext;
29 import javax.servlet.ServletException; 30 import javax.servlet.ServletException;
30 31
31 import digilib.image.DocuImageImpl; 32 import digilib.image.DocuImageImpl;
34 import digilib.util.XMLListLoader; 35 import digilib.util.XMLListLoader;
35 36
36 /** 37 /**
37 * Class to hold the digilib servlet configuration parameters. The parameters 38 * Class to hold the digilib servlet configuration parameters. The parameters
38 * can be read from the digilib-config file and be passed to other servlets or 39 * can be read from the digilib-config file and be passed to other servlets or
39 * beans. <br>errorImgFileName: image file to send in case of error. <br> 40 * beans. <br>
40 * denyImgFileName: image file to send if access is denied. <br>baseDirs: 41 * errorImgFileName: image file to send in case of error. <br>
41 * array of base directories in order of preference (prescaled versions first). 42 * denyImgFileName: image file to send if access is denied. <br>
42 * <br>useAuth: use authentication information. <br>authConfPath: 43 * baseDirs: array of base directories in order of preference (prescaled
43 * authentication configuration file. <br>... <br> 44 * versions first). <br>
45 * useAuth: use authentication information. <br>
46 * authConfPath: authentication configuration file. <br>
47 * ... <br>
44 * 48 *
45 * @author casties 49 * @author casties
46 * 50 *
47 */ 51 */
48 public class DigilibServletConfiguration extends DigilibConfiguration { 52 public class DigilibServletConfiguration extends DigilibConfiguration {
49 53
50 /** 54 /** time the webapp (i.e. this class) was loaded */
51 * Definition of parameters and default values. 55 public final Long webappStartTime = System.currentTimeMillis();
52 */ 56
53 protected void initParams() { 57 /** counter for HttpRequests (mostly for debugging) */
54 /* 58 public AtomicInteger webappRequestCnt = new AtomicInteger(0);
55 * Definition of parameters and default values. System parameters that 59
56 * are not read from config file have a type 's'. 60 /** counter for open HttpRequests (mostly for debugging) */
57 */ 61 public AtomicInteger openRequestCnt = new AtomicInteger(0);
58 62
59 // digilib servlet version 63 /**
60 newParameter( 64 * Definition of parameters and default values.
61 "servlet.version", 65 */
62 digilib.servlet.Scaler.getVersion(), 66 protected void initParams() {
63 null, 67 /*
64 's'); 68 * Definition of parameters and default values. System parameters that
65 // configuration file location 69 * are not read from config file have a type 's'.
66 newParameter("servlet.config.file", null, null, 's'); 70 */
67 // DocuDirCache instance 71
68 newParameter("servlet.dir.cache", null, null, 's'); 72 // digilib servlet version
69 // DocuImage class instance 73 newParameter("servlet.version", digilib.servlet.Scaler.getVersion(), null, 's');
70 newParameter( 74 // configuration file location
71 "servlet.docuimage.class", 75 newParameter("servlet.config.file", null, null, 's');
72 digilib.image.ImageLoaderDocuImage.class, 76 // DocuDirCache instance
73 null, 77 newParameter("servlet.dir.cache", null, null, 's');
74 's'); 78 // DocuImage class instance
75 // DocuImage version 79 newParameter("servlet.docuimage.class", digilib.image.ImageLoaderDocuImage.class, null, 's');
76 newParameter("servlet.docuimage.version", 80 // DocuImage version
77 "?", null, 's'); 81 newParameter("servlet.docuimage.version", "?", null, 's');
78 // AuthOps instance for authentication 82 // AuthOps instance for authentication
79 newParameter("servlet.auth.op", null, null, 's'); 83 newParameter("servlet.auth.op", null, null, 's');
80 // Executor for image operations 84 // Executor for image operations
81 newParameter("servlet.worker.imageexecutor", null, null, 's'); 85 newParameter("servlet.worker.imageexecutor", null, null, 's');
82 // Executor for PDF operations 86 // Executor for PDF operations
83 newParameter("servlet.worker.pdfexecutor", null, null, 's'); 87 newParameter("servlet.worker.pdfexecutor", null, null, 's');
84 // Executor for PDF-image operations 88 // Executor for PDF-image operations
85 newParameter("servlet.worker.pdfimageexecutor", null, null, 's'); 89 newParameter("servlet.worker.pdfimageexecutor", null, null, 's');
86 90
87 /* 91 /*
88 * parameters that can be read from config file have a type 'f' 92 * parameters that can be read from config file have a type 'f'
89 */ 93 */
90 94
91 // image file to send in case of error 95 // image file to send in case of error
92 newParameter( 96 newParameter("error-image", new File("img/digilib-error.png"), null, 'f');
93 "error-image", 97 // image file to send if access is denied
94 new File("img/digilib-error.png"), 98 newParameter("denied-image", new File("img/digilib-denied.png"), null, 'f');
95 null, 99 // image file to send if image file not found
96 'f'); 100 newParameter("notfound-image", new File("img/digilib-notfound.png"), null, 'f');
97 // image file to send if access is denied 101 // base directories in order of preference (prescaled versions last)
98 newParameter( 102 String[] bd = { "sample-images" };
99 "denied-image", 103 newParameter("basedir-list", bd, null, 'f');
100 new File("img/digilib-denied.png"), 104 // use authentication information
101 null, 105 newParameter("use-authorization", Boolean.FALSE, null, 'f');
102 'f'); 106 // authentication configuration file
103 // image file to send if image file not found 107 newParameter("auth-file", new File("digilib-auth.xml"), null, 'f');
104 newParameter( 108 // sending image files as-is allowed
105 "notfound-image", 109 newParameter("sendfile-allowed", Boolean.TRUE, null, 'f');
106 new File("img/digilib-notfound.png"), 110 // Type of DocuImage instance
107 null, 111 newParameter("docuimage-class", "digilib.image.ImageLoaderDocuImage", null, 'f');
108 'f'); 112 // part of URL used to indicate authorized access
109 // base directories in order of preference (prescaled versions last) 113 newParameter("auth-url-path", "authenticated/", null, 'f');
110 String[] bd = { "/docuserver/images", "/docuserver/scaled/small" }; 114 // degree of subsampling on image load
111 newParameter("basedir-list", bd, null, 'f'); 115 newParameter("subsample-minimum", new Float(2f), null, 'f');
112 // use authentication information 116 // default scaling quality
113 newParameter("use-authorization", Boolean.FALSE, null, 'f'); 117 newParameter("default-quality", new Integer(2), null, 'f');
114 // authentication configuration file 118 // use mapping file to translate paths
115 newParameter("auth-file", new File("digilib-auth.xml"), null, 'f'); 119 newParameter("use-mapping", Boolean.FALSE, null, 'f');
116 // sending image files as-is allowed 120 // mapping file location
117 newParameter("sendfile-allowed", Boolean.TRUE, null, 'f'); 121 newParameter("mapping-file", new File("digilib-map.xml"), null, 'f');
118 // Type of DocuImage instance 122 // log4j config file location
119 newParameter( 123 newParameter("log-config-file", new File("log4j-config.xml"), null, 'f');
120 "docuimage-class", 124 // maximum destination image size (0 means no limit)
121 "digilib.image.ImageLoaderDocuImage", 125 newParameter("max-image-size", new Integer(0), null, 'f');
122 null, 126 // number of working threads
123 'f'); 127 newParameter("worker-threads", new Integer(1), null, 'f');
124 // part of URL used to indicate authorized access 128 // max number of waiting threads
125 newParameter("auth-url-path", "authenticated/", null, 'f'); 129 newParameter("max-waiting-threads", new Integer(20), null, 'f');
126 // degree of subsampling on image load 130 // timeout for worker threads (ms)
127 newParameter("subsample-minimum", new Float(2f), null, 'f'); 131 newParameter("worker-timeout", new Integer(60000), null, 'f');
128 // default scaling quality 132 // number of pdf-generation threads
129 newParameter("default-quality", new Integer(1), null, 'f'); 133 newParameter("pdf-worker-threads", new Integer(1), null, 'f');
130 // use mapping file to translate paths 134 // max number of waiting pdf-generation threads
131 newParameter("use-mapping", Boolean.FALSE, null, 'f'); 135 newParameter("pdf-max-waiting-threads", new Integer(20), null, 'f');
132 // mapping file location 136 // number of pdf-image generation threads
133 newParameter("mapping-file", new File("digilib-map.xml"), null, 'f'); 137 newParameter("pdf-image-worker-threads", new Integer(1), null, 'f');
134 // log4j config file location 138 // max number of waiting pdf-image generation threads
135 newParameter("log-config-file", new File("log4j-config.xml"), null, 'f'); 139 newParameter("pdf-image-max-waiting-threads", new Integer(10), null, 'f');
136 // maximum destination image size (0 means no limit)
137 newParameter("max-image-size", new Integer(0), null, 'f');
138 // number of working threads
139 newParameter("worker-threads", new Integer(1), null, 'f');
140 // max number of waiting threads
141 newParameter("max-waiting-threads", new Integer(20), null, 'f');
142 // number of pdf-generation threads
143 newParameter("pdf-worker-threads", new Integer(1), null, 'f');
144 // max number of waiting pdf-generation threads
145 newParameter("pdf-max-waiting-threads", new Integer(20), null, 'f');
146 // number of pdf-image generation threads
147 newParameter("pdf-image-worker-threads", new Integer(1), null, 'f');
148 // max number of waiting pdf-image generation threads
149 newParameter("pdf-image-max-waiting-threads", new Integer(10), null, 'f');
150 // PDF generation temp directory 140 // PDF generation temp directory
151 newParameter("pdf-temp-dir", "pdf_temp", null, 'f'); 141 newParameter("pdf-temp-dir", "pdf_temp", null, 'f');
152 // PDF generation cache directory 142 // PDF generation cache directory
153 newParameter("pdf-cache-dir", "pdf_cache", null, 'f'); 143 newParameter("pdf-cache-dir", "pdf_cache", null, 'f');
154 // allow image toolkit to use disk cache 144 // allow image toolkit to use disk cache
155 newParameter("img-diskcache-allowed", Boolean.TRUE, null, 'f'); 145 newParameter("img-diskcache-allowed", Boolean.TRUE, null, 'f');
156 // default type of error message (image, text, code) 146 // default type of error message (image, text, code)
157 newParameter("default-errmsg-type", "image", null, 'f'); 147 newParameter("default-errmsg-type", "image", null, 'f');
158 } 148 }
159 149
160 /** 150 /**
161 * Constructor taking a ServletConfig. Reads the config file location from 151 * Constructor taking a ServletConfig. Reads the config file location from
162 * an init parameter and loads the config file. Calls <code>readConfig()</code>. 152 * an init parameter and loads the config file. Calls
163 * 153 * <code>readConfig()</code>.
164 * @see readConfig() 154 *
165 */ 155 * @see readConfig()
166 public DigilibServletConfiguration(ServletContext c) throws Exception { 156 */
167 readConfig(c); 157 public DigilibServletConfiguration(ServletContext c) throws Exception {
168 } 158 readConfig(c);
159 }
169 160
170 /** 161 /**
171 * read parameter list from the XML file in init parameter "config-file" or 162 * read parameter list from the XML file in init parameter "config-file" or
172 * file digilib-config.xml 163 * file digilib-config.xml
173 */ 164 */
182 // no config no file... 173 // no config no file...
183 return; 174 return;
184 } 175 }
185 String fn = c.getInitParameter("config-file"); 176 String fn = c.getInitParameter("config-file");
186 if (fn == null) { 177 if (fn == null) {
178 logger.debug("readConfig: no param config-file");
187 fn = ServletOps.getConfigFile("digilib-config.xml", c); 179 fn = ServletOps.getConfigFile("digilib-config.xml", c);
188 if (fn == null) {
189 logger.fatal("readConfig: no param config-file");
190 throw new ServletException("ERROR: no digilib config file!");
191 }
192 } 180 }
193 File f = new File(fn); 181 File f = new File(fn);
194 // setup config file list reader 182 if (f.canRead()) {
195 XMLListLoader lilo = new XMLListLoader("digilib-config", "parameter", 183 // setup config file list reader
196 "name", "value"); 184 XMLListLoader lilo = new XMLListLoader("digilib-config", "parameter", "name", "value");
197 // read config file into HashMap 185 // read config file into HashMap
198 Map<String, String> confTable = lilo.loadURL(f.toURL().toString()); 186 Map<String, String> confTable = lilo.loadURL(f.toURL().toString());
199 187
200 // set config file path parameter 188 // set config file path parameter
201 setValue("servlet.config.file", f.getCanonicalPath()); 189 setValue("servlet.config.file", f.getCanonicalPath());
202 190
203 /* 191 /*
204 * read parameters 192 * read parameters
205 */ 193 */
206 194
207 for (Entry<String, String> confEntry : confTable.entrySet()) { 195 for (Entry<String, String> confEntry : confTable.entrySet()) {
208 Parameter p = get(confEntry.getKey()); 196 Parameter p = get(confEntry.getKey());
209 if (p != null) { 197 if (p != null) {
210 if (p.getType() == 's') { 198 if (p.getType() == 's') {
211 // type 's' Parameters are not overwritten. 199 // type 's' Parameters are not overwritten.
212 continue; 200 continue;
213 } 201 }
214 if (!p.setValueFromString(confEntry.getValue())) { 202 if (!p.setValueFromString(confEntry.getValue())) {
215 /* 203 /*
216 * automatic conversion failed -- try special cases 204 * automatic conversion failed -- try special cases
217 */ 205 */
218 206
219 // basedir-list 207 // basedir-list
220 if (confEntry.getKey().equals("basedir-list")) { 208 if (confEntry.getKey().equals("basedir-list")) {
221 // split list into directories 209 // split list into directories
222 String[] dirs = FileOps.pathToArray(confEntry.getValue()); 210 String[] dirs = FileOps.pathToArray(confEntry.getValue());
223 for (int j = 0; j < dirs.length; j++) { 211 for (int j = 0; j < dirs.length; j++) {
224 // make relative directory paths be inside the webapp 212 // make relative directory paths be inside the
225 dirs[j] = ServletOps.getFile(dirs[j], c); 213 // webapp
226 } 214 dirs[j] = ServletOps.getFile(dirs[j], c);
227 if (dirs != null) { 215 }
228 p.setValue(dirs); 216 if (dirs != null) {
217 p.setValue(dirs);
218 }
229 } 219 }
230 } 220 }
221 } else {
222 // parameter unknown -- just add
223 newParameter(confEntry.getKey(), null, confEntry.getValue(), 'f');
231 } 224 }
232 } else { 225 }
233 // parameter unknown -- just add 226 } else {
234 newParameter(confEntry.getKey(), null, confEntry.getValue(), 227 logger.warn("No digilib config file! Using defaults!");
235 'f'); 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);
236 } 234 }
237 } 235 }
238 // initialise static DocuImage class instance 236 // initialise static DocuImage class instance
239 DigilibServletConfiguration.docuImageClass = (Class<DocuImageImpl>) Class 237 DigilibServletConfiguration.docuImageClass = (Class<DocuImageImpl>) Class.forName(getAsString("docuimage-class"));
240 .forName(getAsString("docuimage-class"));
241 setValue("servlet.docuimage.version", getDocuImageInstance().getVersion()); 238 setValue("servlet.docuimage.version", getDocuImageInstance().getVersion());
242 } 239 }
243 240
244 } 241 }