comparison servlet2/src/main/java/digilib/servlet/Initialiser.java @ 903:7779b37d1d05

refactored into maven modules per servlet type. can build servlet-api 2.3 and 3.0 via profile now!
author robcast
date Tue, 26 Apr 2011 20:24:31 +0200
parents
children d5d99a9eb5dd
comparison
equal deleted inserted replaced
902:89ba3ffcf552 903:7779b37d1d05
1 /* Initialiser.java -- initalisation servlet for setup tasks
2 *
3 * Digital Image Library servlet components
4 *
5 * Copyright (C) 2004 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 Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * Please read license.txt for the full details. A copy of the GPL may be found
13 * at http://www.gnu.org/copyleft/lgpl.html
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * Created on 18.10.2004
20 */
21 package digilib.servlet;
22
23 import java.io.File;
24 import java.io.OutputStream;
25 import java.util.List;
26
27 import javax.imageio.ImageIO;
28 import javax.servlet.ServletContext;
29 import javax.servlet.ServletContextEvent;
30 import javax.servlet.ServletContextListener;
31
32 import org.apache.log4j.Logger;
33 import org.apache.log4j.xml.DOMConfigurator;
34
35 import digilib.auth.AuthOps;
36 import digilib.auth.XMLAuthOps;
37 import digilib.image.DocuImage;
38 import digilib.io.AliasingDocuDirCache;
39 import digilib.io.DocuDirCache;
40 import digilib.io.FileOps.FileClass;
41 import digilib.util.DigilibJobCenter;
42
43 /**
44 * Singleton initalisation listener for setup tasks and resources.
45 *
46 * @author casties
47 *
48 */
49 public class Initialiser implements ServletContextListener {
50
51
52 /** servlet version */
53 public static final String version = "0.3";
54
55 /** gengeral logger for this class */
56 private static Logger logger = Logger.getLogger("digilib.init");
57
58 /** DocuDirCache instance */
59 DocuDirCache dirCache;
60
61 /** DigilibConfiguration instance */
62 DigilibServletConfiguration dlConfig;
63
64 /** Executor for digilib image jobs (AsyncServletWorker doesn't return anything) */
65 DigilibJobCenter<DocuImage> imageEx;
66
67 /** Executor for PDF jobs */
68 DigilibJobCenter<OutputStream> pdfEx;
69
70 /** Executor for PDF image jobs */
71 DigilibJobCenter<DocuImage> pdfImageEx;
72
73 /**
74 * Initialisation on first run.
75 */
76 public void contextInitialized(ServletContextEvent cte) {
77 ServletContext context = cte.getServletContext();
78
79 System.out.println("***** Digital Image Library Initialiser (version "
80 + version + ") *****");
81
82 // see if there is a Configuration instance
83 dlConfig = (DigilibServletConfiguration) context.getAttribute("digilib.servlet.configuration");
84 if (dlConfig == null) {
85 // create new Configuration
86 try {
87 dlConfig = new DigilibServletConfiguration(context);
88
89 /*
90 * further initialization
91 */
92
93 // set up the logger
94 File logConf = ServletOps.getConfigFile((File) dlConfig
95 .getValue("log-config-file"), context);
96 DOMConfigurator.configure(logConf.getAbsolutePath());
97 dlConfig.setValue("log-config-file", logConf);
98 // say hello in the log file
99 logger
100 .info("***** Digital Image Library Initialiser (version "
101 + version + ") *****");
102 // directory cache
103 String[] bd = (String[]) dlConfig.getValue("basedir-list");
104 FileClass[] fcs = { FileClass.IMAGE, FileClass.TEXT };
105 if (dlConfig.getAsBoolean("use-mapping")) {
106 // with mapping file
107 File mapConf = ServletOps.getConfigFile((File) dlConfig
108 .getValue("mapping-file"), context);
109 dirCache = new AliasingDocuDirCache(bd, fcs, mapConf,
110 dlConfig);
111 dlConfig.setValue("mapping-file", mapConf);
112 } else {
113 // without mapping
114 dirCache = new DocuDirCache(bd, fcs, dlConfig);
115 }
116 dlConfig.setValue("servlet.dir.cache", dirCache);
117 // useAuthentication
118 if (dlConfig.getAsBoolean("use-authorization")) {
119 // DB version
120 //authOp = new DBAuthOpsImpl(util);
121 // XML version
122 File authConf = ServletOps.getConfigFile((File) dlConfig
123 .getValue("auth-file"), context);
124 AuthOps authOp = new XMLAuthOps(authConf);
125 dlConfig.setValue("servlet.auth.op", authOp);
126 dlConfig.setValue("auth-file", authConf);
127 }
128 // DocuImage class
129 DocuImage di = DigilibServletConfiguration.getDocuImageInstance();
130 dlConfig.setValue("servlet.docuimage.class", di.getClass().getName());
131 // disk cache for image toolkit
132 boolean dc = dlConfig.getAsBoolean("img-diskcache-allowed");
133 // TODO: methods for all toolkits?
134 ImageIO.setUseCache(dc);
135 // digilib worker threads
136 int nt = dlConfig.getAsInt("worker-threads");
137 int mt = dlConfig.getAsInt("max-waiting-threads");
138 imageEx = new DigilibJobCenter<DocuImage>(nt, mt, false, "servlet.worker.imageexecutor");
139 dlConfig.setValue("servlet.worker.imageexecutor", imageEx);
140 // PDF worker threads
141 int pnt = dlConfig.getAsInt("pdf-worker-threads");
142 int pmt = dlConfig.getAsInt("pdf-max-waiting-threads");
143 pdfEx = new DigilibJobCenter<OutputStream>(pnt, pmt, false, "servlet.worker.pdfexecutor");
144 dlConfig.setValue("servlet.worker.pdfexecutor", pdfEx);
145 // PDF image worker threads
146 int pint = dlConfig.getAsInt("pdf-image-worker-threads");
147 int pimt = dlConfig.getAsInt("pdf-image-max-waiting-threads");
148 pdfImageEx = new DigilibJobCenter<DocuImage>(pint, pimt, false, "servlet.worker.pdfimageexecutor");
149 dlConfig.setValue("servlet.worker.pdfimageexecutor", pdfImageEx);
150 // set as the servlets main config
151 context.setAttribute("digilib.servlet.configuration", dlConfig);
152
153 } catch (Exception e) {
154 logger.error("Error in initialisation: ", e);
155 }
156 } else {
157 // say hello in the log file
158 logger.info("***** Digital Image Library Initialiser (version "
159 + version + ") *****");
160 logger.warn("Already initialised!");
161 }
162 }
163
164 /** clean up local resources
165 *
166 */
167 public void contextDestroyed(ServletContextEvent arg0) {
168 logger.info("Initialiser shutting down.");
169 if (dirCache != null) {
170 // shut down dirCache?
171 dirCache = null;
172 }
173 if (imageEx != null) {
174 // shut down image thread pool
175 List<Runnable> rj = imageEx.shutdownNow();
176 int nrj = rj.size();
177 if (nrj > 0) {
178 logger.error("Still running threads when shutting down image job queue: "+nrj);
179 }
180 }
181 if (pdfEx != null) {
182 // shut down pdf thread pool
183 List<Runnable> rj = pdfEx.shutdownNow();
184 int nrj = rj.size();
185 if (nrj > 0) {
186 logger.error("Still running threads when shutting down PDF job queue: "+nrj);
187 }
188 }
189 if (pdfImageEx != null) {
190 // shut down pdf image thread pool
191 List<Runnable> rj = pdfImageEx.shutdownNow();
192 int nrj = rj.size();
193 if (nrj > 0) {
194 logger.error("Still running threads when shutting down PDF-image job queue: "+nrj);
195 }
196 }
197 }
198
199 }