73
|
1 /* DigilibConfiguration -- Holding all parameters for digilib servlet.
|
|
2
|
|
3 Digital Image Library servlet components
|
|
4
|
|
5 Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de)
|
|
6
|
|
7 This program is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2 of the License, or (at your
|
|
10 option) any later version.
|
|
11
|
|
12 Please read license.txt for the full details. A copy of the GPL
|
|
13 may be found at http://www.gnu.org/copyleft/lgpl.html
|
|
14
|
|
15 You should have received a copy of the GNU General Public License
|
|
16 along with this program; if not, write to the Free Software
|
|
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
18
|
|
19 */
|
|
20
|
|
21 package digilib.servlet;
|
|
22
|
86
|
23 import java.io.File;
|
92
|
24 import java.util.HashMap;
|
153
|
25 import java.util.Iterator;
|
86
|
26
|
|
27 import javax.servlet.ServletConfig;
|
|
28 import javax.servlet.ServletException;
|
|
29
|
73
|
30 import digilib.Utils;
|
86
|
31 import digilib.auth.AuthOps;
|
|
32 import digilib.auth.XMLAuthOps;
|
|
33 import digilib.image.DocuImage;
|
|
34 import digilib.image.DocuImageImpl;
|
|
35 import digilib.io.DocuDirCache;
|
153
|
36 import digilib.io.FileOps;
|
86
|
37 import digilib.io.XMLListLoader;
|
73
|
38
|
|
39 /** Class to hold the digilib servlet configuration parameters.
|
|
40 * The parameters can be read from the digilib-config file and be passed to
|
|
41 * other servlets or beans.<br>
|
|
42 * errorImgFileName: image file to send in case of error.<br>
|
|
43 * denyImgFileName: image file to send if access is denied.<br>
|
|
44 * baseDirs: array of base directories in order of preference (prescaled
|
|
45 * versions first).<br>
|
|
46 * useAuth: use authentication information.<br>
|
|
47 * authConfPath: authentication configuration file.<br>
|
|
48 * authOp: AuthOps instance for authentication.<br>
|
|
49 * ...<br>
|
|
50 *
|
|
51 * @author casties
|
|
52 *
|
|
53 */
|
153
|
54 public class DigilibConfiguration extends ParameterMap {
|
|
55
|
|
56 /** DocuImage class instance */
|
149
|
57 private Class docuImageClass = null;
|
153
|
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 }
|
73
|
116
|
|
117 /** Constructor taking a ServletConfig.
|
|
118 * Reads the config file location from an init parameter and loads the
|
153
|
119 * config file. Calls <code>readConfig()</code>.
|
73
|
120 *
|
153
|
121 * @see readConfig()
|
73
|
122 */
|
|
123 public DigilibConfiguration(ServletConfig c) throws Exception {
|
153
|
124 this();
|
|
125 readConfig(c);
|
73
|
126 }
|
153
|
127
|
73
|
128 /**
|
|
129 * read parameter list from the XML file in init parameter "config-file"
|
|
130 */
|
153
|
131 public void readConfig(ServletConfig c) throws Exception {
|
|
132
|
|
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 */
|
73
|
138 if (c == null) {
|
153
|
139 // no config no file...
|
73
|
140 return;
|
|
141 }
|
|
142 String fn = c.getInitParameter("config-file");
|
|
143 if (fn == null) {
|
153
|
144 fn = c.getServletContext().getRealPath("WEB-INF/digilib-config.xml");
|
|
145 if (fn == null) {
|
|
146 util.dprintln(4, "setConfig: no param config-file");
|
|
147 throw new ServletException("ERROR no digilib config file!");
|
|
148 }
|
73
|
149 }
|
|
150 File f = new File(fn);
|
|
151 // setup config file list reader
|
|
152 XMLListLoader lilo =
|
|
153 new XMLListLoader("digilib-config", "parameter", "name", "value");
|
153
|
154 // read config file into HashMap
|
|
155 HashMap confTable = lilo.loadURL(f.toURL().toString());
|
|
156
|
|
157 // set config file path parameter
|
|
158 setValue("servlet.config.file", f.getCanonicalPath());
|
73
|
159
|
|
160 /*
|
|
161 * read parameters
|
|
162 */
|
86
|
163
|
153
|
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
|
73
|
198 // debugLevel
|
153
|
199 util.setDebugLevel(getAsInt("debug-level"));
|
86
|
200 // directory cache
|
153
|
201 String[] bd = (String[]) getValue("basedir-list");
|
|
202 DocuDirCache dirCache = new DocuDirCache(bd);
|
|
203 setValue("servlet.dir.cache", dirCache);
|
73
|
204 // useAuthentication
|
153
|
205 if (getAsBoolean("use-authorization")) {
|
73
|
206 // DB version
|
|
207 //authOp = new DBAuthOpsImpl(util);
|
|
208 // XML version
|
153
|
209 String authConfPath = getAsString("auth-file");
|
|
210 AuthOps authOp = new XMLAuthOps(util, authConfPath);
|
|
211 setValue("servlet.auth.op", authOp);
|
73
|
212 }
|
153
|
213 // DocuImage class
|
|
214 docuImageClass = (Class) getValue("servlet.docuimage.class");
|
73
|
215 }
|
|
216
|
|
217 /** Creates a new DocuImage instance.
|
|
218 *
|
|
219 * The type of DocuImage is specified by docuImageType.
|
|
220 *
|
|
221 * @return DocuImage
|
|
222 */
|
|
223 public DocuImage getDocuImageInstance() {
|
|
224 DocuImageImpl di = null;
|
|
225 try {
|
149
|
226 if (docuImageClass == null) {
|
153
|
227 docuImageClass = Class.forName(getAsString("docuimage-class"));
|
149
|
228 }
|
|
229 di = (DocuImageImpl) docuImageClass.newInstance();
|
73
|
230 di.setUtils(util);
|
|
231 } catch (Exception e) {
|
|
232 }
|
|
233 return di;
|
|
234 }
|
|
235
|
|
236 /**
|
153
|
237 * @return
|
73
|
238 */
|
|
239 public Utils getUtil() {
|
|
240 return util;
|
|
241 }
|
|
242
|
|
243 }
|