changeset 806:d811204ce5a4 stream

Initialiser is now a ServletContextListener (doesn't need web.xml).
author robcast
date Sun, 20 Feb 2011 00:02:51 +0100
parents 5591163f47ee
children 5de6c7a73855
files client/digitallibrary/WEB-INF/web.xml servlet/src/digilib/servlet/DigilibConfiguration.java servlet/src/digilib/servlet/Initialiser.java servlet/src/digilib/servlet/Scaler.java servlet/src/digilib/servlet/ServletOps.java
diffstat 5 files changed, 38 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/client/digitallibrary/WEB-INF/web.xml	Sat Feb 19 22:59:53 2011 +0100
+++ b/client/digitallibrary/WEB-INF/web.xml	Sun Feb 20 00:02:51 2011 +0100
@@ -12,19 +12,6 @@
   <description>
         This is the web frontend of the Digital Document Library.
   </description>
-  <!-- The Initaliser servlet (must run first) -->
-  <servlet>
-        <servlet-name>
-            Initialiser
-        </servlet-name>
-        <servlet-class>
-            digilib.servlet.Initialiser
-        </servlet-class>
-        <!-- Load this servlet at server startup time -->
-        <load-on-startup>
-            1
-        </load-on-startup>
-  </servlet>
   <!-- The Scaler servlet -->
   <servlet>
         <servlet-name>
--- a/servlet/src/digilib/servlet/DigilibConfiguration.java	Sat Feb 19 22:59:53 2011 +0100
+++ b/servlet/src/digilib/servlet/DigilibConfiguration.java	Sun Feb 20 00:02:51 2011 +0100
@@ -27,6 +27,7 @@
 import java.util.Map.Entry;
 
 import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 
 import org.apache.log4j.BasicConfigurator;
@@ -182,7 +183,7 @@
 	 * 
 	 * @see readConfig()
 	 */
-	public DigilibConfiguration(ServletConfig c) throws Exception {
+	public DigilibConfiguration(ServletContext c) throws Exception {
 		this();
 		readConfig(c);
 	}
@@ -192,7 +193,7 @@
 	 * or file digilib-config.xml
 	 */
 	@SuppressWarnings("unchecked")
-    public void readConfig(ServletConfig c) throws Exception {
+    public void readConfig(ServletContext c) throws Exception {
 
 		/*
 		 * Get config file name. The file name is first looked for as an init
@@ -215,7 +216,7 @@
 		XMLListLoader lilo =
 			new XMLListLoader("digilib-config", "parameter", "name", "value");
 		// read config file into HashMap
-		Map<String,String> confTable = lilo.loadURL(f.toURL().toString());
+		Map<String,String> confTable = lilo.loadURL(f.toString());
 
 		// set config file path parameter
 		setValue("servlet.config.file", f.getCanonicalPath());
--- a/servlet/src/digilib/servlet/Initialiser.java	Sat Feb 19 22:59:53 2011 +0100
+++ b/servlet/src/digilib/servlet/Initialiser.java	Sun Feb 20 00:02:51 2011 +0100
@@ -24,10 +24,10 @@
 import java.io.OutputStream;
 import java.util.List;
 
-import javax.servlet.ServletConfig;
 import javax.servlet.ServletContext;
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServlet;
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+import javax.servlet.annotation.WebListener;
 
 import org.apache.log4j.Logger;
 import org.apache.log4j.xml.DOMConfigurator;
@@ -41,16 +41,17 @@
 import digilib.util.DigilibJobCenter;
 
 /**
- * Singleton initalisation servlet for setup tasks and resources.
+ * Singleton initalisation listener for setup tasks and resources.
  * 
  * @author casties
  *  
  */
-@SuppressWarnings("serial")
-public class Initialiser extends HttpServlet {
+@WebListener
+public class Initialiser implements ServletContextListener {
+
 
 	/** servlet version */
-	public static final String version = "0.2";
+	public static final String version = "0.3";
 
 	/** gengeral logger for this class */
 	private static Logger logger = Logger.getLogger("digilib.init");
@@ -72,24 +73,19 @@
 	
 	/**
 	 * Initialisation on first run.
-	 * 
-	 * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
 	 */
-	public void init(ServletConfig config) throws ServletException {
-		super.init(config);
+    public void contextInitialized(ServletContextEvent cte) {
+        ServletContext context = cte.getServletContext();
 
-		System.out
-				.println("***** Digital Image Library Initialisation Servlet (version "
+		System.out.println("***** Digital Image Library Initialiser (version "
 						+ version + ") *****");
 
-		// get our ServletContext
-		ServletContext context = config.getServletContext();
 		// see if there is a Configuration instance
 		dlConfig = (DigilibConfiguration) context.getAttribute("digilib.servlet.configuration");
 		if (dlConfig == null) {
 			// create new Configuration
 			try {
-				dlConfig = new DigilibConfiguration(config);
+				dlConfig = new DigilibConfiguration(context);
 
 				/*
 				 * further initialization
@@ -97,12 +93,12 @@
 
 				// set up the logger
 				File logConf = ServletOps.getConfigFile((File) dlConfig
-						.getValue("log-config-file"), config);
+						.getValue("log-config-file"), context);
 				DOMConfigurator.configure(logConf.getAbsolutePath());
 				dlConfig.setValue("log-config-file", logConf);
 				// say hello in the log file
 				logger
-						.info("***** Digital Image Library Initialisation Servlet (version "
+						.info("***** Digital Image Library Initialiser (version "
 								+ version + ") *****");
 				// directory cache
 				String[] bd = (String[]) dlConfig.getValue("basedir-list");
@@ -110,7 +106,7 @@
 				if (dlConfig.getAsBoolean("use-mapping")) {
 					// with mapping file
 					File mapConf = ServletOps.getConfigFile((File) dlConfig
-							.getValue("mapping-file"), config);
+							.getValue("mapping-file"), context);
 					dirCache = new AliasingDocuDirCache(bd, fcs, mapConf,
 							dlConfig);
 					dlConfig.setValue("mapping-file", mapConf);
@@ -125,7 +121,7 @@
 					//authOp = new DBAuthOpsImpl(util);
 					// XML version
 					File authConf = ServletOps.getConfigFile((File) dlConfig
-							.getValue("auth-file"), config);
+							.getValue("auth-file"), context);
 					AuthOps authOp = new XMLAuthOps(authConf);
 					dlConfig.setValue("servlet.auth.op", authOp);
 					dlConfig.setValue("auth-file", authConf);
@@ -152,22 +148,20 @@
 				context.setAttribute("digilib.servlet.configuration", dlConfig);
 
 			} catch (Exception e) {
-				throw new ServletException(e);
+				logger.error("Error in initialisation: ", e);
 			}
 		} else {
 			// say hello in the log file
-			logger
-					.info("***** Digital Image Library Initialisation Servlet (version "
+			logger.info("***** Digital Image Library Initialisation Servlet (version "
 							+ version + ") *****");
 			logger.warn("Already initialised!");
 		}
 	}
 
     /** clean up local resources
-     * @see javax.servlet.GenericServlet#destroy()
+     * 
      */
-    @Override
-    public void destroy() {
+    public void contextDestroyed(ServletContextEvent arg0) {
         if (dirCache != null) {
             // shut down dirCache?
             dirCache = null;
@@ -196,7 +190,6 @@
                 logger.error("Still running threads when shutting down PDF-image job queue: "+nrj);
             }
         }
-        super.destroy();
     }
 
 }
--- a/servlet/src/digilib/servlet/Scaler.java	Sat Feb 19 22:59:53 2011 +0100
+++ b/servlet/src/digilib/servlet/Scaler.java	Sun Feb 20 00:02:51 2011 +0100
@@ -31,7 +31,7 @@
     private static final long serialVersionUID = 5289386646192471549L;
 
     /** digilib servlet version (for all components) */
-    public static final String version = "1.9.1a2";
+    public static final String version = "1.9.1a3";
 
     /** servlet error codes */
     public static enum Error {UNKNOWN, AUTH, FILE, IMAGE};
@@ -95,8 +95,7 @@
         // get our ServletContext
         ServletContext context = config.getServletContext();
         // see if there is a Configuration instance
-        dlConfig = (DigilibConfiguration) context
-                .getAttribute("digilib.servlet.configuration");
+        dlConfig = (DigilibConfiguration) context.getAttribute("digilib.servlet.configuration");
         if (dlConfig == null) {
             // no Configuration
             throw new ServletException("No Configuration!");
@@ -113,11 +112,11 @@
                 .getValue("servlet.worker.imageexecutor");
 
         denyImgFile = ServletOps.getFile(
-                (File) dlConfig.getValue("denied-image"), config);
+                (File) dlConfig.getValue("denied-image"), context);
         errorImgFile = ServletOps.getFile(
-                (File) dlConfig.getValue("error-image"), config);
+                (File) dlConfig.getValue("error-image"), context);
         notfoundImgFile = ServletOps.getFile(
-                (File) dlConfig.getValue("notfound-image"), config);
+                (File) dlConfig.getValue("notfound-image"), context);
         sendFileAllowed = dlConfig.getAsBoolean("sendfile-allowed");
     }
 
--- a/servlet/src/digilib/servlet/ServletOps.java	Sat Feb 19 22:59:53 2011 +0100
+++ b/servlet/src/digilib/servlet/ServletOps.java	Sun Feb 20 00:02:51 2011 +0100
@@ -28,7 +28,7 @@
 import java.io.PrintWriter;
 import java.util.StringTokenizer;
 
-import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletResponse;
 
@@ -72,11 +72,11 @@
      * @param sc
      * @return
      */
-    public static File getFile(File f, ServletConfig sc) {
+    public static File getFile(File f, ServletContext sc) {
         // is the filename absolute?
         if (!f.isAbsolute()) {
             // relative path -> use getRealPath to resolve in WEB-INF
-            String fn = sc.getServletContext().getRealPath(f.getPath());
+            String fn = sc.getRealPath(f.getPath());
             f = new File(fn);
         }
         return f;
@@ -92,12 +92,12 @@
      * @param sc
      * @return
      */
-    public static String getFile(String filename, ServletConfig sc) {
+    public static String getFile(String filename, ServletContext sc) {
         File f = new File(filename);
         // is the filename absolute?
         if (!f.isAbsolute()) {
             // relative path -> use getRealPath to resolve in WEB-INF
-            filename = sc.getServletContext().getRealPath(filename);
+            filename = sc.getRealPath(filename);
         }
         return filename;
     }
@@ -112,7 +112,7 @@
      * @param sc
      * @return
      */
-    public static File getConfigFile(File f, ServletConfig sc) {
+    public static File getConfigFile(File f, ServletContext sc) {
         String fn = f.getPath();
         // is the filename absolute?
         if (f.isAbsolute()) {
@@ -126,7 +126,7 @@
             }
         }
         // relative path -> use getRealPath to resolve in WEB-INF
-        String newfn = sc.getServletContext().getRealPath("WEB-INF/" + fn);
+        String newfn = sc.getRealPath("WEB-INF/" + fn);
         f = new File(newfn);
         return f;
     }
@@ -141,13 +141,12 @@
      * @param sc
      * @return
      */
-    public static String getConfigFile(String filename, ServletConfig sc) {
+    public static String getConfigFile(String filename, ServletContext sc) {
         File f = new File(filename);
         // is the filename absolute?
         if (!f.isAbsolute()) {
             // relative path -> use getRealPath to resolve in WEB-INF
-            filename = sc.getServletContext()
-                    .getRealPath("WEB-INF/" + filename);
+            filename = sc.getRealPath("WEB-INF/" + filename);
         }
         return filename;
     }