Mercurial > hg > digilib-old
comparison servlet3/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 |
---|---|
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 import java.util.concurrent.atomic.AtomicInteger; |
28 | 28 |
29 import javax.servlet.ServletContext; | 29 import javax.servlet.ServletContext; |
30 import javax.servlet.ServletException; | |
31 | 30 |
32 import digilib.image.DocuImageImpl; | 31 import digilib.image.DocuImageImpl; |
33 import digilib.io.FileOps; | 32 import digilib.io.FileOps; |
34 import digilib.util.Parameter; | 33 import digilib.util.Parameter; |
35 import digilib.util.XMLListLoader; | 34 import digilib.util.XMLListLoader; |
51 */ | 50 */ |
52 public class DigilibServletConfiguration extends DigilibConfiguration { | 51 public class DigilibServletConfiguration extends DigilibConfiguration { |
53 | 52 |
54 /** time the webapp (i.e. this class) was loaded */ | 53 /** time the webapp (i.e. this class) was loaded */ |
55 public final Long webappStartTime = System.currentTimeMillis(); | 54 public final Long webappStartTime = System.currentTimeMillis(); |
56 | 55 |
57 /** counter for HttpRequests (mostly for debugging) */ | 56 /** counter for HttpRequests (mostly for debugging) */ |
58 public AtomicInteger webappRequestCnt = new AtomicInteger(0); | 57 public AtomicInteger webappRequestCnt = new AtomicInteger(0); |
59 | 58 |
60 /** counter for open HttpRequests (mostly for debugging) */ | 59 /** counter for open HttpRequests (mostly for debugging) */ |
61 public AtomicInteger openRequestCnt = new AtomicInteger(0); | 60 public AtomicInteger openRequestCnt = new AtomicInteger(0); |
68 * Definition of parameters and default values. System parameters that | 67 * Definition of parameters and default values. System parameters that |
69 * are not read from config file have a type 's'. | 68 * are not read from config file have a type 's'. |
70 */ | 69 */ |
71 | 70 |
72 // digilib servlet version | 71 // digilib servlet version |
73 newParameter("servlet.version", digilib.servlet.Scaler.getVersion(), | 72 newParameter("servlet.version", digilib.servlet.Scaler.getVersion(), null, 's'); |
74 null, 's'); | |
75 // configuration file location | 73 // configuration file location |
76 newParameter("servlet.config.file", null, null, 's'); | 74 newParameter("servlet.config.file", null, null, 's'); |
77 // DocuDirCache instance | 75 // DocuDirCache instance |
78 newParameter("servlet.dir.cache", null, null, 's'); | 76 newParameter("servlet.dir.cache", null, null, 's'); |
79 // DocuImage class instance | 77 // DocuImage class instance |
80 newParameter("servlet.docuimage.class", | 78 newParameter("servlet.docuimage.class", digilib.image.ImageLoaderDocuImage.class, null, 's'); |
81 digilib.image.ImageLoaderDocuImage.class, null, 's'); | |
82 // DocuImage version | 79 // DocuImage version |
83 newParameter("servlet.docuimage.version", | 80 newParameter("servlet.docuimage.version", "?", null, 's'); |
84 "?", null, 's'); | |
85 // AuthOps instance for authentication | 81 // AuthOps instance for authentication |
86 newParameter("servlet.auth.op", null, null, 's'); | 82 newParameter("servlet.auth.op", null, null, 's'); |
87 // Executor for image operations | 83 // Executor for image operations |
88 newParameter("servlet.worker.imageexecutor", null, null, 's'); | 84 newParameter("servlet.worker.imageexecutor", null, null, 's'); |
89 // Executor for PDF operations | 85 // Executor for PDF operations |
94 /* | 90 /* |
95 * parameters that can be read from config file have a type 'f' | 91 * parameters that can be read from config file have a type 'f' |
96 */ | 92 */ |
97 | 93 |
98 // image file to send in case of error | 94 // image file to send in case of error |
99 newParameter("error-image", new File("img/digilib-error.png"), null, | 95 newParameter("error-image", new File("img/digilib-error.png"), null, 'f'); |
100 'f'); | |
101 // image file to send if access is denied | 96 // image file to send if access is denied |
102 newParameter("denied-image", new File("img/digilib-denied.png"), null, | 97 newParameter("denied-image", new File("img/digilib-denied.png"), null, 'f'); |
103 'f'); | |
104 // image file to send if image file not found | 98 // image file to send if image file not found |
105 newParameter("notfound-image", new File("img/digilib-notfound.png"), | 99 newParameter("notfound-image", new File("img/digilib-notfound.png"), null, 'f'); |
106 null, 'f'); | |
107 // base directories in order of preference (prescaled versions last) | 100 // base directories in order of preference (prescaled versions last) |
108 String[] bd = { "/docuserver/images", "/docuserver/scaled/small" }; | 101 String[] bd = { "sample-images" }; |
109 newParameter("basedir-list", bd, null, 'f'); | 102 newParameter("basedir-list", bd, null, 'f'); |
110 // use authentication information | 103 // use authentication information |
111 newParameter("use-authorization", Boolean.FALSE, null, 'f'); | 104 newParameter("use-authorization", Boolean.FALSE, null, 'f'); |
112 // authentication configuration file | 105 // authentication configuration file |
113 newParameter("auth-file", new File("digilib-auth.xml"), null, 'f'); | 106 newParameter("auth-file", new File("digilib-auth.xml"), null, 'f'); |
118 // part of URL used to indicate authorized access | 111 // part of URL used to indicate authorized access |
119 newParameter("auth-url-path", "authenticated/", null, 'f'); | 112 newParameter("auth-url-path", "authenticated/", null, 'f'); |
120 // degree of subsampling on image load | 113 // degree of subsampling on image load |
121 newParameter("subsample-minimum", new Float(2f), null, 'f'); | 114 newParameter("subsample-minimum", new Float(2f), null, 'f'); |
122 // default scaling quality | 115 // default scaling quality |
123 newParameter("default-quality", new Integer(1), null, 'f'); | 116 newParameter("default-quality", new Integer(2), null, 'f'); |
124 // use mapping file to translate paths | 117 // use mapping file to translate paths |
125 newParameter("use-mapping", Boolean.FALSE, null, 'f'); | 118 newParameter("use-mapping", Boolean.FALSE, null, 'f'); |
126 // mapping file location | 119 // mapping file location |
127 newParameter("mapping-file", new File("digilib-map.xml"), null, 'f'); | 120 newParameter("mapping-file", new File("digilib-map.xml"), null, 'f'); |
128 // log4j config file location | 121 // log4j config file location |
140 // max number of waiting pdf-generation threads | 133 // max number of waiting pdf-generation threads |
141 newParameter("pdf-max-waiting-threads", new Integer(20), null, 'f'); | 134 newParameter("pdf-max-waiting-threads", new Integer(20), null, 'f'); |
142 // number of pdf-image generation threads | 135 // number of pdf-image generation threads |
143 newParameter("pdf-image-worker-threads", new Integer(1), null, 'f'); | 136 newParameter("pdf-image-worker-threads", new Integer(1), null, 'f'); |
144 // max number of waiting pdf-image generation threads | 137 // max number of waiting pdf-image generation threads |
145 newParameter("pdf-image-max-waiting-threads", new Integer(10), null, | 138 newParameter("pdf-image-max-waiting-threads", new Integer(10), null, 'f'); |
146 'f'); | |
147 // PDF generation temp directory | 139 // PDF generation temp directory |
148 newParameter("pdf-temp-dir", "pdf_temp", null, 'f'); | 140 newParameter("pdf-temp-dir", "pdf_temp", null, 'f'); |
149 // PDF generation cache directory | 141 // PDF generation cache directory |
150 newParameter("pdf-cache-dir", "pdf_cache", null, 'f'); | 142 newParameter("pdf-cache-dir", "pdf_cache", null, 'f'); |
151 // allow image toolkit to use disk cache | 143 // allow image toolkit to use disk cache |
180 // no config no file... | 172 // no config no file... |
181 return; | 173 return; |
182 } | 174 } |
183 String fn = c.getInitParameter("config-file"); | 175 String fn = c.getInitParameter("config-file"); |
184 if (fn == null) { | 176 if (fn == null) { |
177 logger.debug("readConfig: no param config-file"); | |
185 fn = ServletOps.getConfigFile("digilib-config.xml", c); | 178 fn = ServletOps.getConfigFile("digilib-config.xml", c); |
186 if (fn == null) { | |
187 logger.fatal("readConfig: no param config-file"); | |
188 throw new ServletException("ERROR: no digilib config file!"); | |
189 } | |
190 } | 179 } |
191 File f = new File(fn); | 180 File f = new File(fn); |
192 // setup config file list reader | 181 if (f.canRead()) { |
193 XMLListLoader lilo = new XMLListLoader("digilib-config", "parameter", | 182 // setup config file list reader |
194 "name", "value"); | 183 XMLListLoader lilo = new XMLListLoader("digilib-config", "parameter", "name", "value"); |
195 // read config file into HashMap | 184 // read config file into HashMap |
196 Map<String, String> confTable = lilo.loadURL(f.toURL().toString()); | 185 Map<String, String> confTable = lilo.loadURL(f.toURL().toString()); |
197 | 186 |
198 // set config file path parameter | 187 // set config file path parameter |
199 setValue("servlet.config.file", f.getCanonicalPath()); | 188 setValue("servlet.config.file", f.getCanonicalPath()); |
200 | 189 |
201 /* | 190 /* |
202 * read parameters | 191 * read parameters |
203 */ | 192 */ |
204 | 193 |
205 for (Entry<String, String> confEntry : confTable.entrySet()) { | 194 for (Entry<String, String> confEntry : confTable.entrySet()) { |
206 Parameter p = get(confEntry.getKey()); | 195 Parameter p = get(confEntry.getKey()); |
207 if (p != null) { | 196 if (p != null) { |
208 if (p.getType() == 's') { | 197 if (p.getType() == 's') { |
209 // type 's' Parameters are not overwritten. | 198 // type 's' Parameters are not overwritten. |
210 continue; | 199 continue; |
211 } | 200 } |
212 if (!p.setValueFromString(confEntry.getValue())) { | 201 if (!p.setValueFromString(confEntry.getValue())) { |
213 /* | 202 /* |
214 * automatic conversion failed -- try special cases | 203 * automatic conversion failed -- try special cases |
215 */ | 204 */ |
216 | 205 |
217 // basedir-list | 206 // basedir-list |
218 if (confEntry.getKey().equals("basedir-list")) { | 207 if (confEntry.getKey().equals("basedir-list")) { |
219 // split list into directories | 208 // split list into directories |
220 String[] dirs = FileOps.pathToArray(confEntry.getValue()); | 209 String[] dirs = FileOps.pathToArray(confEntry.getValue()); |
221 for (int j = 0; j < dirs.length; j++) { | 210 for (int j = 0; j < dirs.length; j++) { |
222 // make relative directory paths be inside the webapp | 211 // make relative directory paths be inside the |
223 dirs[j] = ServletOps.getFile(dirs[j], c); | 212 // webapp |
224 } | 213 dirs[j] = ServletOps.getFile(dirs[j], c); |
225 if (dirs != null) { | 214 } |
226 p.setValue(dirs); | 215 if (dirs != null) { |
216 p.setValue(dirs); | |
217 } | |
227 } | 218 } |
228 } | 219 } |
220 } else { | |
221 // parameter unknown -- just add | |
222 newParameter(confEntry.getKey(), null, confEntry.getValue(), 'f'); | |
229 } | 223 } |
230 } else { | 224 } |
231 // parameter unknown -- just add | 225 } else { |
232 newParameter(confEntry.getKey(), null, confEntry.getValue(), 'f'); | 226 logger.warn("No digilib config file! Using defaults!"); |
227 // update basedir-list | |
228 String[] dirs = (String[]) this.getValue("basedir-list"); | |
229 for (int j = 0; j < dirs.length; j++) { | |
230 // make relative directory paths be inside the | |
231 // webapp | |
232 dirs[j] = ServletOps.getFile(dirs[j], c); | |
233 } | 233 } |
234 } | 234 } |
235 // initialise static DocuImage class instance | 235 // initialise static DocuImage class instance |
236 DigilibServletConfiguration.docuImageClass = (Class<DocuImageImpl>) Class | 236 DigilibServletConfiguration.docuImageClass = (Class<DocuImageImpl>) Class.forName(getAsString("docuimage-class")); |
237 .forName(getAsString("docuimage-class")); | |
238 setValue("servlet.docuimage.version", getDocuImageInstance().getVersion()); | 237 setValue("servlet.docuimage.version", getDocuImageInstance().getVersion()); |
239 } | 238 } |
240 | 239 |
241 } | 240 } |