comparison servlet/src/digilib/servlet/DigilibConfiguration.java @ 153:4980c969be4c

Servlet version 1.16a1 - cleanup of DigilibConfig class - now uses new Parameter and ParameterMap classes - new parameter default-quality
author robcast
date Wed, 03 Sep 2003 00:56:16 +0200
parents 04ad64b2137a
children 688ad0b8b0fa
comparison
equal deleted inserted replaced
152:f4a5cfe37469 153:4980c969be4c
20 20
21 package digilib.servlet; 21 package digilib.servlet;
22 22
23 import java.io.File; 23 import java.io.File;
24 import java.util.HashMap; 24 import java.util.HashMap;
25 import java.util.StringTokenizer; 25 import java.util.Iterator;
26 26
27 import javax.servlet.ServletConfig; 27 import javax.servlet.ServletConfig;
28 import javax.servlet.ServletException; 28 import javax.servlet.ServletException;
29 29
30 import digilib.Utils; 30 import digilib.Utils;
31 import digilib.auth.AuthOps; 31 import digilib.auth.AuthOps;
32 import digilib.auth.XMLAuthOps; 32 import digilib.auth.XMLAuthOps;
33 import digilib.image.DocuImage; 33 import digilib.image.DocuImage;
34 import digilib.image.DocuImageImpl; 34 import digilib.image.DocuImageImpl;
35 import digilib.io.DocuDirCache; 35 import digilib.io.DocuDirCache;
36 import digilib.io.FileOps;
36 import digilib.io.XMLListLoader; 37 import digilib.io.XMLListLoader;
37 38
38 /** Class to hold the digilib servlet configuration parameters. 39 /** Class to hold the digilib servlet configuration parameters.
39 * The parameters can be read from the digilib-config file and be passed to 40 * The parameters can be read from the digilib-config file and be passed to
40 * other servlets or beans.<br> 41 * other servlets or beans.<br>
48 * ...<br> 49 * ...<br>
49 * 50 *
50 * @author casties 51 * @author casties
51 * 52 *
52 */ 53 */
53 public class DigilibConfiguration { 54 public class DigilibConfiguration extends ParameterMap {
54 // digilib servlet version 55
55 private String servletVersion = digilib.servlet.Scaler.dlVersion; 56 /** DocuImage class instance */
56 // configuration file location
57 private String dlConfPath = "";
58 // image file to send in case of error
59 private String errorImgFileName = "/docuserver/images/icons/scalerror.gif";
60 private String errorImgParam = "error-image";
61 // image file to send if access is denied
62 private String denyImgFileName = "/docuserver/images/icons/denied.gif";
63 private String denyImgParam = "denied-image";
64 // base directories in order of preference (prescaled versions last)
65 private String[] baseDirs =
66 { "/docuserver/images", "/docuserver/scaled/small" };
67 private String baseDirParam = "basedir-list";
68 // use authentication information
69 private boolean useAuthentication = true;
70 private String useAuthParam = "use-authorization";
71 // authentication configuration file
72 private String authConfPath =
73 "/docuserver/www/digitallibrary/WEB-INF/digilib-auth.xml";
74 private String authConfParam = "auth-file";
75 // sending image files as-is allowed
76 private boolean sendFileAllowed = true;
77 private String sendFileAllowedParam = "sendfile-allowed";
78 // AuthOps instance for authentication
79 private AuthOps authOp;
80 // Debug level
81 private int debugLevel = 5;
82 private String debugLevelParam = "debug-level";
83 // Utils instance
84 private Utils util = new Utils(debugLevel);
85 // HashTable for parameters
86 private HashMap confTable = null;
87 // Type of DocuImage instance
88 private String docuImageType = "digilib.image.JAIDocuImage";
89 private String docuImageTypeParam = "docuimage-class";
90 // part of URL used to indicate authorized access
91 private String authURLPath = "authenticated/";
92 private String AuthURLPathParam = "auth-url-path";
93 // degree of subsampling on image load
94 private float minSubsample = 2;
95 private String minSubsampleParam = "subsample-minimum";
96 // DocuDirCache instance
97 private DocuDirCache dirCache = null;
98 // DocuImage class instance
99 private Class docuImageClass = null; 57 private Class docuImageClass = null;
58
59 /** Utils instance */
60 private Utils util = new Utils(5);
61
62
63 /** Default constructor defines all parameters and their default values.
64 *
65 */
66 public DigilibConfiguration() {
67 // create HashMap(20)
68 super(20);
69
70 /*
71 * Definition of parameters and default values.
72 * System parameters that are not read from config file have a type 's'.
73 */
74
75 // digilib servlet version
76 putParameter("servlet.version", digilib.servlet.Scaler.dlVersion, null, 's');
77 // configuration file location
78 putParameter("servlet.config.file", null, null, 's');
79 // Utils instance
80 putParameter("servlet.util", util, null, 's');
81 // DocuDirCache instance
82 putParameter("servlet.dir.cache", null, null, 's');
83 // DocuImage class instance
84 putParameter("servlet.docuimage.class", digilib.image.JAIDocuImage.class, null, 's');
85 // AuthOps instance for authentication
86 putParameter("servlet.auth.op", null, null, 's');
87
88 /*
89 * parameters that can be read from config file have a type 'f'
90 */
91
92 // image file to send in case of error
93 putParameter("error-image", "/docuserver/images/icons/scalerror.gif", null, 'f');
94 // image file to send if access is denied
95 putParameter("denied-image", "/docuserver/images/icons/denied.gif", null, 'f');
96 // base directories in order of preference (prescaled versions last)
97 String[] bd = { "/docuserver/images", "/docuserver/scaled/small" };
98 putParameter("basedir-list", bd, null, 'f');
99 // use authentication information
100 putParameter("use-authorization", Boolean.TRUE, null, 'f');
101 // authentication configuration file
102 putParameter("auth-file", "/docuserver/www/digitallibrary/WEB-INF/digilib-auth.xml", null, 'f');
103 // sending image files as-is allowed
104 putParameter("sendfile-allowed", Boolean.TRUE, null, 'f');
105 // Debug level
106 putParameter("debug-level", new Integer(5), null, 'f');
107 // Type of DocuImage instance
108 putParameter("docuimage-class", "digilib.image.JAIDocuImage", null, 'f');
109 // part of URL used to indicate authorized access
110 putParameter("auth-url-path", "authenticated/", null, 'f');
111 // degree of subsampling on image load
112 putParameter("subsample-minimum", new Float(2f), null, 'f');
113 // default scaling quality
114 putParameter("default-quality", new Integer(1), null, 'f');
115 }
100 116
101 /** Constructor taking a ServletConfig. 117 /** Constructor taking a ServletConfig.
102 * Reads the config file location from an init parameter and loads the 118 * Reads the config file location from an init parameter and loads the
103 * config file. Calls <code>init</code>. 119 * config file. Calls <code>readConfig()</code>.
104 * 120 *
105 * @see init() 121 * @see readConfig()
106 */ 122 */
107 public DigilibConfiguration(ServletConfig c) throws Exception { 123 public DigilibConfiguration(ServletConfig c) throws Exception {
108 init(c); 124 this();
109 } 125 readConfig(c);
110 126 }
127
111 /** 128 /**
112 * read parameter list from the XML file in init parameter "config-file" 129 * read parameter list from the XML file in init parameter "config-file"
113 */ 130 */
114 public void init(ServletConfig c) throws Exception { 131 public void readConfig(ServletConfig c) throws Exception {
115 // reset parameter table 132
116 confTable = null; 133 /*
134 * Get config file name.
135 * The file name is first looked for as an init parameter, then in a fixed location
136 * in the webapp.
137 */
117 if (c == null) { 138 if (c == null) {
139 // no config no file...
118 return; 140 return;
119 } 141 }
120 // get config file name
121 String fn = c.getInitParameter("config-file"); 142 String fn = c.getInitParameter("config-file");
122 if (fn == null) { 143 if (fn == null) {
123 util.dprintln(4, "setConfig: no param config-file"); 144 fn = c.getServletContext().getRealPath("WEB-INF/digilib-config.xml");
124 throw new ServletException("ERROR no digilib config file!"); 145 if (fn == null) {
146 util.dprintln(4, "setConfig: no param config-file");
147 throw new ServletException("ERROR no digilib config file!");
148 }
125 } 149 }
126 File f = new File(fn); 150 File f = new File(fn);
127 // setup config file list reader 151 // setup config file list reader
128 XMLListLoader lilo = 152 XMLListLoader lilo =
129 new XMLListLoader("digilib-config", "parameter", "name", "value"); 153 new XMLListLoader("digilib-config", "parameter", "name", "value");
130 confTable = lilo.loadURL(f.toURL().toString()); 154 // read config file into HashMap
131 dlConfPath = f.getCanonicalPath(); 155 HashMap confTable = lilo.loadURL(f.toURL().toString());
156
157 // set config file path parameter
158 setValue("servlet.config.file", f.getCanonicalPath());
132 159
133 /* 160 /*
134 * read parameters 161 * read parameters
135 */ 162 */
136 163
164 for (Iterator i = confTable.keySet().iterator(); i.hasNext();) {
165 String key = (String) i.next();
166 String val = (String) confTable.get(key);
167 Parameter p = get(key);
168 if (p != null) {
169 if (p.getType() == 's') {
170 // type 's' Parameters are not overwritten.
171 continue;
172 }
173 if (! p.setValueFromString(val)) {
174 /*
175 * automatic conversion failed -- try special cases
176 */
177
178 // basedir-list
179 if (key == "basedir-list") {
180 // split list into directories
181 String[] sa = FileOps.pathToArray(val);
182 if (sa != null) {
183 p.setValue(sa);
184 }
185 }
186
187 }
188 } else {
189 // parameter unknown -- just add
190 putParameter(key, null, val, 'f');
191 }
192 }
193
194 /*
195 * further initialization
196 */
197
137 // debugLevel 198 // debugLevel
138 debugLevel = tryToGetInitParam(debugLevelParam, debugLevel); 199 util.setDebugLevel(getAsInt("debug-level"));
139 util.setDebugLevel(debugLevel);
140 // errorImgFileName
141 errorImgFileName = tryToGetInitParam(errorImgParam, errorImgFileName);
142 // denyImgFileName
143 denyImgFileName = tryToGetInitParam(denyImgParam, denyImgFileName);
144 // docuImageType
145 docuImageType = tryToGetInitParam(docuImageTypeParam, docuImageType);
146 // sendFileAllowed
147 sendFileAllowed =
148 tryToGetInitParam(sendFileAllowedParam, sendFileAllowed);
149 // baseDirs
150 String baseDirList =
151 tryToGetInitParam(
152 baseDirParam,
153 "/docuserver/images/:/docuserver/scaled/small/");
154 // split list into directories
155 String[] sa = splitPathArray(baseDirList);
156 baseDirs = (sa != null) ? sa : baseDirs;
157 // directory cache 200 // directory cache
158 dirCache = new DocuDirCache(baseDirs); 201 String[] bd = (String[]) getValue("basedir-list");
202 DocuDirCache dirCache = new DocuDirCache(bd);
203 setValue("servlet.dir.cache", dirCache);
159 // useAuthentication 204 // useAuthentication
160 useAuthentication = tryToGetInitParam(useAuthParam, useAuthentication); 205 if (getAsBoolean("use-authorization")) {
161 if (useAuthentication) {
162 // DB version 206 // DB version
163 //authOp = new DBAuthOpsImpl(util); 207 //authOp = new DBAuthOpsImpl(util);
164 // XML version 208 // XML version
165 authConfPath = tryToGetInitParam(authConfParam, authConfPath); 209 String authConfPath = getAsString("auth-file");
166 authOp = new XMLAuthOps(util, authConfPath); 210 AuthOps authOp = new XMLAuthOps(util, authConfPath);
167 } 211 setValue("servlet.auth.op", authOp);
168 // minSubsample 212 }
169 minSubsample = 213 // DocuImage class
170 tryToGetInitParam(minSubsampleParam, minSubsample); 214 docuImageClass = (Class) getValue("servlet.docuimage.class");
171 }
172
173 /**
174 * convert a string with a list of pathnames into an array of strings
175 * using the system's path separator string
176 */
177 public String[] splitPathArray(String paths) {
178 // split list into directories
179 StringTokenizer dirs =
180 new StringTokenizer(paths, File.pathSeparator);
181 int n = dirs.countTokens();
182 if (n < 1) {
183 return null;
184 }
185 // add directories into array
186 String[] pathArray = new String[n];
187 for (int i = 0; i < n; i++) {
188 String s = dirs.nextToken();
189 // make shure the dir name ends with a directory separator
190 if (s.endsWith(File.separator)) {
191 pathArray[i] = s;
192 } else {
193 pathArray[i] = s + File.separator;
194 }
195 }
196 return pathArray;
197 }
198
199 /**
200 * get an init parameter from config and return it if set, otherwise return default
201 */
202 public int tryToGetInitParam(String s, int i) {
203 //System.out.println("trytogetInitParam("+s+", "+i+")");
204 try {
205 //System.out.println("trytogetInitParam: "+(String)confTable.get(s));
206 i = Integer.parseInt((String) confTable.get(s));
207 } catch (Exception e) {
208 util.dprintln(4, "trytogetInitParam(int) failed on param " + s);
209 //e.printStackTrace();
210 }
211 return i;
212 }
213 public float tryToGetInitParam(String s, float f) {
214 try {
215 f = Float.parseFloat((String) confTable.get(s));
216 } catch (Exception e) {
217 util.dprintln(4, "trytoGetInitParam(float) failed on param " + s);
218 //e.printStackTrace();
219 }
220 return f;
221 }
222 public String tryToGetInitParam(String s, String x) {
223 if ((confTable != null) && ((String) confTable.get(s) != null)) {
224 x = (String) confTable.get(s);
225 } else {
226 util.dprintln(4, "trytoGetInitParam(string) failed on param " + s);
227 }
228 return x;
229 }
230 public boolean tryToGetInitParam(String s, boolean b) {
231 String bs;
232 boolean bb = b;
233 if ((confTable != null) && ((String) confTable.get(s) != null)) {
234 bs = (String) confTable.get(s);
235
236 if ((bs.indexOf("false") > -1) || (bs.indexOf("FALSE") > -1)) {
237 bb = false;
238 } else if (
239 (bs.indexOf("true") > -1) || (bs.indexOf("TRUE") > -1)) {
240 bb = true;
241 } else {
242 util.dprintln(
243 4,
244 "trytoGetInitParam(string) failed on param " + s);
245 }
246 } else {
247 util.dprintln(4, "trytoGetInitParam(string) failed on param " + s);
248 }
249 return bb;
250 } 215 }
251 216
252 /** Creates a new DocuImage instance. 217 /** Creates a new DocuImage instance.
253 * 218 *
254 * The type of DocuImage is specified by docuImageType. 219 * The type of DocuImage is specified by docuImageType.
257 */ 222 */
258 public DocuImage getDocuImageInstance() { 223 public DocuImage getDocuImageInstance() {
259 DocuImageImpl di = null; 224 DocuImageImpl di = null;
260 try { 225 try {
261 if (docuImageClass == null) { 226 if (docuImageClass == null) {
262 docuImageClass = Class.forName(docuImageType); 227 docuImageClass = Class.forName(getAsString("docuimage-class"));
263 } 228 }
264 di = (DocuImageImpl) docuImageClass.newInstance(); 229 di = (DocuImageImpl) docuImageClass.newInstance();
265 di.setUtils(util); 230 di.setUtils(util);
266 } catch (Exception e) { 231 } catch (Exception e) {
267 } 232 }
268 return di; 233 return di;
269 } 234 }
270 235
271 /** 236 /**
272 * Returns the authConfPath. 237 * @return
273 * @return String
274 */
275 public String getAuthConfPath() {
276 return authConfPath;
277 }
278
279 /**
280 * Returns the authOp.
281 * @return AuthOps
282 */
283 public AuthOps getAuthOp() {
284 return authOp;
285 }
286
287 /**
288 * Returns the denyImgFileName.
289 * @return String
290 */
291 public String getDenyImgFileName() {
292 return denyImgFileName;
293 }
294
295 /**
296 * Returns the errorImgFileName.
297 * @return String
298 */
299 public String getErrorImgFileName() {
300 return errorImgFileName;
301 }
302
303 /**
304 * Returns the useAuth.
305 * @return boolean
306 */
307 public boolean isUseAuthentication() {
308 return useAuthentication;
309 }
310
311 /**
312 * Sets the authConfPath.
313 * @param authConfPath The authConfPath to set
314 */
315 public void setAuthConfPath(String authConfPath) {
316 this.authConfPath = authConfPath;
317 }
318
319 /**
320 * Sets the authOp.
321 * @param authOp The authOp to set
322 */
323 public void setAuthOp(AuthOps authOp) {
324 this.authOp = authOp;
325 }
326
327 /**
328 * Sets the denyImgFileName.
329 * @param denyImgFileName The denyImgFileName to set
330 */
331 public void setDenyImgFileName(String denyImgFileName) {
332 this.denyImgFileName = denyImgFileName;
333 }
334
335 /**
336 * Sets the errorImgFileName.
337 * @param errorImgFileName The errorImgFileName to set
338 */
339 public void setErrorImgFileName(String errorImgFileName) {
340 this.errorImgFileName = errorImgFileName;
341 }
342
343 /**
344 * Returns the baseDirs.
345 * @return String[]
346 */
347 public String[] getBaseDirs() {
348 return baseDirs;
349 }
350
351 /**
352 * Returns the baseDirs as String.
353 * @return String
354 */
355 public String getBaseDirList() {
356 String s = "";
357 java.util.Iterator i = java.util.Arrays.asList(baseDirs).iterator();
358 while (i.hasNext()) {
359 s += (i.next() + "; ");
360 }
361 return s;
362 }
363
364 /**
365 * Sets the baseDirs.
366 * @param baseDirs The baseDirs to set
367 */
368 public void setBaseDirs(String[] baseDirs) {
369 this.baseDirs = baseDirs;
370 dirCache = new DocuDirCache(baseDirs);
371 }
372
373 /**
374 * Returns the debugLevel.
375 * @return int
376 */
377 public int getDebugLevel() {
378 return debugLevel;
379 }
380
381 /**
382 * Sets the debugLevel.
383 * @param debugLevel The debugLevel to set
384 */
385 public void setDebugLevel(int debugLevel) {
386 this.debugLevel = debugLevel;
387 }
388
389 /**
390 * Returns the util.
391 * @return Utils
392 */ 238 */
393 public Utils getUtil() { 239 public Utils getUtil() {
394 return util; 240 return util;
395 } 241 }
396 242
397 /**
398 * Sets the util.
399 * @param util The util to set
400 */
401 public void setUtil(Utils util) {
402 this.util = util;
403 }
404
405 /**
406 * Returns the servletVersion.
407 * @return String
408 */
409 public String getServletVersion() {
410 return servletVersion;
411 }
412
413 /**
414 * Sets the servletVersion.
415 * @param servletVersion The servletVersion to set
416 */
417 public void setServletVersion(String servletVersion) {
418 this.servletVersion = servletVersion;
419 }
420
421 /**
422 * Returns the docuImageType.
423 * @return String
424 */
425 public String getDocuImageType() {
426 return docuImageType;
427 }
428
429 /**
430 * Sets the docuImageType.
431 * @param docuImageType The docuImageType to set
432 */
433 public void setDocuImageType(String docuImageType) {
434 this.docuImageType = docuImageType;
435 }
436
437 /**
438 * Returns the sendFileAllowed.
439 * @return boolean
440 */
441 public boolean isSendFileAllowed() {
442 return sendFileAllowed;
443 }
444
445 /**
446 * Sets the sendFileAllowed.
447 * @param sendFileAllowed The sendFileAllowed to set
448 */
449 public void setSendFileAllowed(boolean sendFileAllowed) {
450 this.sendFileAllowed = sendFileAllowed;
451 }
452
453 /**
454 * Returns the authURLPath.
455 * @return String
456 */
457 public String getAuthURLPath() {
458 return authURLPath;
459 }
460
461 /**
462 * Sets the authURLPath.
463 * @param authURLPath The authURLPath to set
464 */
465 public void setAuthURLPath(String authURLPath) {
466 this.authURLPath = authURLPath;
467 }
468
469 /**
470 * Sets the useAuthentication.
471 * @param useAuthentication The useAuthentication to set
472 */
473 public void setUseAuthentication(boolean useAuthentication) {
474 this.useAuthentication = useAuthentication;
475 }
476
477 /**
478 * Returns the dlConfPath.
479 * @return String
480 */
481 public String getDlConfPath() {
482 return dlConfPath;
483 }
484
485 /**
486 * @return float
487 */
488 public float getMinSubsample() {
489 return minSubsample;
490 }
491
492 /**
493 * Sets the minSubsample.
494 * @param minSubsample The minSubsample to set
495 */
496 public void setMinSubsample(float f) {
497 this.minSubsample = f;
498 }
499
500 /**
501 * @return DocuDirCache
502 */
503 public DocuDirCache getDirCache() {
504 return dirCache;
505 }
506
507 /**
508 * Sets the dirCache.
509 * @param dirCache The dirCache to set
510 */
511 public void setDirCache(DocuDirCache dirCache) {
512 this.dirCache = dirCache;
513 }
514
515 } 243 }