From b7439f253c83cbd5dfe87d80cce53d258a1fa7c3 Mon Sep 17 00:00:00 2001 From: Mark Diggory Date: Fri, 29 Aug 2008 03:03:15 +0000 Subject: [PATCH] Move DSpace Initialization Code to Share DSpaceContextListener in dspace-api. add deprecation to Existing InitServlets and remove obsolete initialization cases from web.xml. git-svn-id: http://scm.dspace.org/svn/repo/branches/dspace-1_5_x@3055 9c30dcfa-912a-0410-8fc2-9e0234be79fd --- .../app/util/DSpaceContextListener.java | 81 ++++++++++- .../org/dspace/core/ConfigurationManager.java | 20 ++- .../app/webui/servlet/LoadDSpaceConfig.java | 21 ++- .../src/main/webapp/WEB-INF/web.xml | 30 ++-- .../dspace/app/dav/LoadDSpaceLNIConfig.java | 22 ++- .../src/main/webapp/WEB-INF/web.xml | 24 ++-- .../dspace/app/oai/LoadDSpaceOAIConfig.java | 23 +++- .../src/main/webapp/WEB-INF/web.xml | 20 ++- .../org/dspace/sword/LoadDSpaceConfig.java | 25 ++-- .../src/main/webapp/WEB-INF/web.xml | 69 +++++++--- .../app/xmlui/cocoon/DSpaceCocoonServlet.java | 129 +++++++++++------- .../src/main/webapp/WEB-INF/web.xml | 55 ++++++-- 12 files changed, 360 insertions(+), 159 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/app/util/DSpaceContextListener.java b/dspace-api/src/main/java/org/dspace/app/util/DSpaceContextListener.java index 6c9538afa6..8809c7d5a5 100644 --- a/dspace-api/src/main/java/org/dspace/app/util/DSpaceContextListener.java +++ b/dspace-api/src/main/java/org/dspace/app/util/DSpaceContextListener.java @@ -40,18 +40,22 @@ package org.dspace.app.util; +import org.dspace.core.ConfigurationManager; import org.dspace.storage.rdbms.DatabaseManager; import org.apache.log4j.Logger; import javax.servlet.ServletContextListener; import javax.servlet.ServletContextEvent; + import java.beans.Introspector; +import java.net.URL; +import java.net.URLConnection; import java.sql.Driver; import java.sql.DriverManager; import java.util.Enumeration; /** - * Class to initialise / cleanup resources used by DSpace when the web application + * Class to initialize / cleanup resources used by DSpace when the web application * is started or stopped */ public class DSpaceContextListener implements ServletContextListener @@ -59,11 +63,82 @@ public class DSpaceContextListener implements ServletContextListener private static Logger log = Logger.getLogger(DSpaceContextListener.class); /** - * Initialise any resources required by the application + * The DSpace config parameter, this is where the path to the DSpace + * configuration file can be obtained + */ + public static final String DSPACE_CONFIG_PARAMETER = "dspace-config"; + + /** + * Initialize any resources required by the application * @param event */ public void contextInitialized(ServletContextEvent event) { + + // On Windows, URL caches can cause problems, particularly with undeployment + // So, here we attempt to disable them if we detect that we are running on Windows + try + { + String osName = System.getProperty("os.name"); + + if (osName != null && osName.toLowerCase().contains("windows")) + { + URL url = new URL("http://localhost/"); + URLConnection urlConn = url.openConnection(); + urlConn.setDefaultUseCaches(false); + } + } + catch (Throwable t) + { + log.error(t.getMessage(), t); + // Any errors thrown in disabling the caches aren't significant to + // the normal execution of the application, so we ignore them + } + + // Paths to the various config files + String dspaceConfig = null; + + /** + * Stage 1 + * + * Locate the dspace config + */ + + // first check the local per webapp parameter, then check the global parameter. + dspaceConfig = event.getServletContext().getInitParameter(DSPACE_CONFIG_PARAMETER); + + // Finaly, if no config parameter found throw an error + if (dspaceConfig == null || "".equals(dspaceConfig)) + { + throw new RuntimeException( + "\n\nDSpace has failed to initialize. This has occurred because it was unable to determine \n" + + "where the dspace.cfg file is located. The path to the configuration file should be stored \n" + + "in a context variable, '"+DSPACE_CONFIG_PARAMETER+"', in the global context. \n" + + "No context variable was found in either location.\n\n"); + } + + /** + * Stage 2 + * + * Load the dspace config. Also may load log4j configuration. + * (Please rely on ConfigurationManager or Log4j to configure logging) + * + */ + try + { + ConfigurationManager.loadConfig(dspaceConfig); + } + catch (Throwable t) + { + throw new RuntimeException( + "\n\nDSpace has failed to initialize, during stage 2. Error while attempting to read the \n" + + "DSpace configuration file (Path: '"+dspaceConfig+"'). \n" + + "This has likely occurred because either the file does not exist, or it's permissions \n" + + "are set incorrectly, or the path to the configuration file is incorrect. The path to \n" + + "the DSpace configuration file is stored in a context variable, 'dspace-config', in \n" + + "either the local servlet or global context.\n\n",t); + } + } /** @@ -93,7 +168,7 @@ public class DSpaceContextListener implements ServletContextListener } catch (Throwable e) { - log.error("Failled to cleanup ClassLoader for webapp", e); + log.error("Failed to cleanup ClassLoader for webapp", e); } } } diff --git a/dspace-api/src/main/java/org/dspace/core/ConfigurationManager.java b/dspace-api/src/main/java/org/dspace/core/ConfigurationManager.java index dc175aeb8e..b5da9c83a9 100644 --- a/dspace-api/src/main/java/org/dspace/core/ConfigurationManager.java +++ b/dspace-api/src/main/java/org/dspace/core/ConfigurationManager.java @@ -79,6 +79,7 @@ import org.apache.log4j.helpers.OptionConverter; * * @author Robert Tansley * @author Larry Stone - Interpolated values. + * @author Mark Diggory - General Improvements to detection, logging and loading. * @version $Revision$ */ public class ConfigurationManager @@ -96,6 +97,15 @@ public class ConfigurationManager // configuration; anything greater than this is very likely to be a loop. private final static int RECURSION_LIMIT = 9; + /** + * Identify if DSpace is properly configured + * @return boolean true if configured, false otherwise + */ + public static boolean isConfigured() + { + return properties != null; + } + /** * */ @@ -800,7 +810,7 @@ public class ConfigurationManager private static void info(String string) { - if (!isConfigured()) + if (!isLog4jConfigured()) { System.out.println("INFO: " + string); } @@ -812,7 +822,7 @@ public class ConfigurationManager private static void warn(String string) { - if (!isConfigured()) + if (!isLog4jConfigured()) { System.out.println("WARN: " + string); } @@ -824,7 +834,7 @@ public class ConfigurationManager private static void fatal(String string, Exception e) { - if (!isConfigured()) + if (!isLog4jConfigured()) { System.out.println("FATAL: " + string); e.printStackTrace(); @@ -837,7 +847,7 @@ public class ConfigurationManager private static void fatal(String string) { - if (!isConfigured()) + if (!isLog4jConfigured()) { System.out.println("FATAL: " + string); } @@ -851,7 +861,7 @@ public class ConfigurationManager * Only current solution available to detect * if log4j is truly configured. */ - private static boolean isConfigured() + private static boolean isLog4jConfigured() { Enumeration en = org.apache.log4j.LogManager.getRootLogger() .getAllAppenders(); diff --git a/dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/servlet/LoadDSpaceConfig.java b/dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/servlet/LoadDSpaceConfig.java index b1e19c44ee..0076c8b816 100644 --- a/dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/servlet/LoadDSpaceConfig.java +++ b/dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/servlet/LoadDSpaceConfig.java @@ -49,6 +49,16 @@ import java.net.URLConnection; * Simple servlet to load in DSpace and log4j configurations. Should always be * started up before other servlets (use ) * + * This class holds code to be removed in the next version of the DSpace XMLUI, + * it is now managed by a Shared Context Listener inthe dspace-api project. + * + * It is deprecated, rather than removed to maintain backward compatibility for + * local DSpace 1.5.x customized overlays. + * + * TODO: Remove in trunk + * + * @deprecated Use Servlet Context Listener provided in dspace-api (remove in > + * 1.5.x) * @author Robert Tansley * @version $Revision$ */ @@ -77,11 +87,14 @@ public class LoadDSpaceConfig extends HttpServlet // the normal execution of the application, so we ignore them } - // Get config parameter - String config = getServletContext().getInitParameter("dspace-config"); + if(!ConfigurationManager.isConfigured()) + { + // Get config parameter + String config = getServletContext().getInitParameter("dspace-config"); - // Load in DSpace config - ConfigurationManager.loadConfig(config); + // Load in DSpace config + ConfigurationManager.loadConfig(config); + } } } diff --git a/dspace-jspui/dspace-jspui-webapp/src/main/webapp/WEB-INF/web.xml b/dspace-jspui/dspace-jspui-webapp/src/main/webapp/WEB-INF/web.xml index f6f9e9c08b..7c1247ea76 100644 --- a/dspace-jspui/dspace-jspui-webapp/src/main/webapp/WEB-INF/web.xml +++ b/dspace-jspui/dspace-jspui-webapp/src/main/webapp/WEB-INF/web.xml @@ -124,26 +124,22 @@ /view-workspaceitem - - - org.apache.commons.fileupload.servlet.FileCleanerCleanup - - - - org.dspace.app.util.DSpaceContextListener - + + + + org.apache.commons.fileupload.servlet.FileCleanerCleanup + + + + + + + org.dspace.app.util.DSpaceContextListener + + - - - - load-dspace-config - org.dspace.app.webui.servlet.LoadDSpaceConfig - 1 - - authorize org.dspace.app.webui.servlet.admin.AuthorizeAdminServlet diff --git a/dspace-lni/dspace-lni-core/src/main/java/org/dspace/app/dav/LoadDSpaceLNIConfig.java b/dspace-lni/dspace-lni-core/src/main/java/org/dspace/app/dav/LoadDSpaceLNIConfig.java index 2559aef509..dff37e7446 100644 --- a/dspace-lni/dspace-lni-core/src/main/java/org/dspace/app/dav/LoadDSpaceLNIConfig.java +++ b/dspace-lni/dspace-lni-core/src/main/java/org/dspace/app/dav/LoadDSpaceLNIConfig.java @@ -47,6 +47,16 @@ import org.dspace.core.ConfigurationManager; * Simple servlet to load in DSpace and log4j configurations. Should always be * started up before other servlets (use ) * + * This class holds code to be removed in the next version of the DSpace XMLUI, + * it is now managed by a Shared Context Listener inthe dspace-api project. + * + * It is deprecated, rather than removed to maintain backward compatibility for + * local DSpace 1.5.x customized overlays. + * + * TODO: Remove in trunk + * + * @deprecated Use Servlet Context Listener provided in dspace-api (remove in > + * 1.5.x) * @author Robert Tansley * @version $Revision: 1407 $ */ @@ -54,11 +64,13 @@ public class LoadDSpaceLNIConfig extends HttpServlet { public void init() { - // Get config parameter - String config = getServletContext().getInitParameter("dspace-config"); - - // Load in DSpace config - ConfigurationManager.loadConfig(config); + if(!ConfigurationManager.isConfigured()) + { + // Get config parameter + String config = getServletContext().getInitParameter("dspace-config"); + // Load in DSpace config + ConfigurationManager.loadConfig(config); + } } } diff --git a/dspace-lni/dspace-lni-webapp/src/main/webapp/WEB-INF/web.xml b/dspace-lni/dspace-lni-webapp/src/main/webapp/WEB-INF/web.xml index fcb304339f..d053f29eba 100644 --- a/dspace-lni/dspace-lni-webapp/src/main/webapp/WEB-INF/web.xml +++ b/dspace-lni/dspace-lni-webapp/src/main/webapp/WEB-INF/web.xml @@ -2,7 +2,7 @@ - - org.dspace.app.util.DSpaceContextListener - - - - - load-dspace-config - org.dspace.app.dav.LoadDSpaceLNIConfig - 1 - + + + + org.dspace.app.util.DSpaceContextListener + + + + LNI-DAV diff --git a/dspace-oai/dspace-oai-api/src/main/java/org/dspace/app/oai/LoadDSpaceOAIConfig.java b/dspace-oai/dspace-oai-api/src/main/java/org/dspace/app/oai/LoadDSpaceOAIConfig.java index edc2827627..bc5ceb7fdc 100644 --- a/dspace-oai/dspace-oai-api/src/main/java/org/dspace/app/oai/LoadDSpaceOAIConfig.java +++ b/dspace-oai/dspace-oai-api/src/main/java/org/dspace/app/oai/LoadDSpaceOAIConfig.java @@ -47,6 +47,17 @@ import org.dspace.core.ConfigurationManager; * Simple servlet to load in DSpace and log4j configurations. Should always be * started up before other servlets (use ) * + * This class holds code to be removed in the next version of the DSpace XMLUI, + * it is now managed by a Shared Context Listener inthe dspace-api project. + * + * It is deprecated, rather than removed to maintain backward compatibility for + * local DSpace 1.5.x customized overlays. + * + * TODO: Remove in trunk + * + * @deprecated Use Servlet Context Listener provided in dspace-api (remove in > + * 1.5.x) + * * @author Robert Tansley * @version $Revision: 1407 $ */ @@ -54,11 +65,13 @@ public class LoadDSpaceOAIConfig extends HttpServlet { public void init() { - // Get config parameter - String config = getServletContext().getInitParameter("dspace-config"); - - // Load in DSpace config - ConfigurationManager.loadConfig(config); + if(!ConfigurationManager.isConfigured()) + { + // Get config parameter + String config = getServletContext().getInitParameter("dspace-config"); + // Load in DSpace config + ConfigurationManager.loadConfig(config); + } } } diff --git a/dspace-oai/dspace-oai-webapp/src/main/webapp/WEB-INF/web.xml b/dspace-oai/dspace-oai-webapp/src/main/webapp/WEB-INF/web.xml index a1a7f28029..47077f9401 100644 --- a/dspace-oai/dspace-oai-webapp/src/main/webapp/WEB-INF/web.xml +++ b/dspace-oai/dspace-oai-webapp/src/main/webapp/WEB-INF/web.xml @@ -70,18 +70,14 @@ - - - org.dspace.app.util.DSpaceContextListener - - - - - load-dspace-config - org.dspace.app.oai.LoadDSpaceOAIConfig - 1 - + + + + org.dspace.app.util.DSpaceContextListener + + oai-handler diff --git a/dspace-sword/dspace-sword-api/src/main/java/org/dspace/sword/LoadDSpaceConfig.java b/dspace-sword/dspace-sword-api/src/main/java/org/dspace/sword/LoadDSpaceConfig.java index 17e7cc5e13..5107a47863 100644 --- a/dspace-sword/dspace-sword-api/src/main/java/org/dspace/sword/LoadDSpaceConfig.java +++ b/dspace-sword/dspace-sword-api/src/main/java/org/dspace/sword/LoadDSpaceConfig.java @@ -43,21 +43,30 @@ import org.dspace.core.ConfigurationManager; * Simple servlet to load in DSpace and log4j configurations. Should always be * started up before other servlets (use ) * - * This class has been duplicated into the DSpace SWORD module from its - * original home in the DSpace JSPUI, but authorship and copyright - * ownership are as dictated in this file. + * This class holds code to be removed in the next version of the DSpace XMLUI, + * it is now managed by a Shared Context Listener inthe dspace-api project. * + * It is deprecated, rather than removed to maintain backward compatibility for + * local DSpace 1.5.x customized overlays. + * + * TODO: Remove in trunk + * + * @deprecated Use Servlet Context Listener provided in dspace-api (remove in > + * 1.5.x) + * * @author Robert Tansley */ public class LoadDSpaceConfig extends HttpServlet { public void init() { - // Get config parameter - String config = getServletContext().getInitParameter("dspace-config"); - - // Load in DSpace config - ConfigurationManager.loadConfig(config); + if(!ConfigurationManager.isConfigured()) + { + // Get config parameter + String config = getServletContext().getInitParameter("dspace-config"); + // Load in DSpace config + ConfigurationManager.loadConfig(config); + } } } diff --git a/dspace-sword/dspace-sword-webapp/src/main/webapp/WEB-INF/web.xml b/dspace-sword/dspace-sword-webapp/src/main/webapp/WEB-INF/web.xml index e2fd27396d..0c1f1ef73c 100644 --- a/dspace-sword/dspace-sword-webapp/src/main/webapp/WEB-INF/web.xml +++ b/dspace-sword/dspace-sword-webapp/src/main/webapp/WEB-INF/web.xml @@ -1,12 +1,44 @@ - - - + DSpace SWORD Server @@ -37,21 +69,16 @@ - - - - - org.dspace.app.util.DSpaceContextListener - + + + + org.dspace.app.util.DSpaceContextListener + + - - - load-dspace-config - org.dspace.sword.LoadDSpaceConfig - 1 - - + servicedocument org.purl.sword.server.ServiceDocumentServlet diff --git a/dspace-xmlui/dspace-xmlui-api/src/main/java/org/dspace/app/xmlui/cocoon/DSpaceCocoonServlet.java b/dspace-xmlui/dspace-xmlui-api/src/main/java/org/dspace/app/xmlui/cocoon/DSpaceCocoonServlet.java index b9d117a1d9..c94a814528 100644 --- a/dspace-xmlui/dspace-xmlui-api/src/main/java/org/dspace/app/xmlui/cocoon/DSpaceCocoonServlet.java +++ b/dspace-xmlui/dspace-xmlui-api/src/main/java/org/dspace/app/xmlui/cocoon/DSpaceCocoonServlet.java @@ -75,13 +75,21 @@ public class DSpaceCocoonServlet extends CocoonServlet */ public static final String DSPACE_CONFIG_PARAMETER = "dspace-config"; - - - /** - * Before this servlet will become functional replace + * This method holds code to be removed in the next version + * of the DSpace XMLUI, it is now managed by a Shared Context + * Listener inthe dspace-api project. + * + * It is deprecated, rather than removed to maintain backward + * compatibility for local DSpace 1.5.x customized overlays. + * + * TODO: Remove in trunk + * + * @deprecated Use Servlet Context Listener provided + * in dspace-api (remove in > 1.5.x) + * @throws ServletException */ - public void init() throws ServletException + private void initDSpace() throws ServletException { // On Windows, URL caches can cause problems, particularly with undeployment // So, here we attempt to disable them if we detect that we are running on Windows @@ -103,59 +111,78 @@ public class DSpaceCocoonServlet extends CocoonServlet // Any errors thrown in disabling the caches aren't significant to // the normal execution of the application, so we ignore them } + + /** + * Previous stages moved to shared ServletListener available in dspace-api + */ + String dspaceConfig = null; + String log4jConfig = null; + + /** + * Stage 1 + * + * Locate the dspace config + */ + + // first check the local per webapp parameter, then check the global parameter. + dspaceConfig = super.getInitParameter(DSPACE_CONFIG_PARAMETER); + if (dspaceConfig == null) + dspaceConfig = super.getServletContext().getInitParameter(DSPACE_CONFIG_PARAMETER); + + // Finaly, if no config parameter found throw an error + if (dspaceConfig == null || "".equals(dspaceConfig)) + { + throw new ServletException( + "\n\nDSpace has failed to initialize. This has occurred because it was unable to determine \n" + + "where the dspace.cfg file is located. The path to the configuration file should be stored \n" + + "in a context variable, '"+DSPACE_CONFIG_PARAMETER+"', in either the local servlet or global contexts. \n" + + "No context variable was found in either location.\n\n"); + } + + /** + * Stage 2 + * + * Load the dspace config. Also may load log4j configuration. + * (Please rely on ConfigurationManager or Log4j to configure logging) + * + */ + try + { + if(!ConfigurationManager.isConfigured()) + { + // Load in DSpace config + ConfigurationManager.loadConfig(dspaceConfig); + } + + + } + catch (Throwable t) + { + throw new ServletException( + "\n\nDSpace has failed to initialize, during stage 2. Error while attempting to read the \n" + + "DSpace configuration file (Path: '"+dspaceConfig+"'). \n" + + "This has likely occurred because either the file does not exist, or it's permissions \n" + + "are set incorrectly, or the path to the configuration file is incorrect. The path to \n" + + "the DSpace configuration file is stored in a context variable, 'dspace-config', in \n" + + "either the local servlet or global context.\n\n",t); + } + } + + /** + * Before this servlet will become functional replace + */ + public void init() throws ServletException + { + this.initDSpace(); + // Check if cocoon needs to do anything at init time? super.init(); // Paths to the various config files - String dspaceConfig = null; - String log4jConfig = null; String webappConfigPath = null; String installedConfigPath = null; - - /** - * Stage 1 - * - * Locate the dspace config - */ - - // first check the local per webapp parameter, then check the global parameter. - dspaceConfig = super.getInitParameter(DSPACE_CONFIG_PARAMETER); - if (dspaceConfig == null) - dspaceConfig = super.getServletContext().getInitParameter(DSPACE_CONFIG_PARAMETER); - - // Finaly, if no config parameter found throw an error - if (dspaceConfig == null || "".equals(dspaceConfig)) - { - throw new ServletException( - "\n\nDSpace has failed to initialize. This has occurred because it was unable to determine \n" + - "where the dspace.cfg file is located. The path to the configuration file should be stored \n" + - "in a context variable, '"+DSPACE_CONFIG_PARAMETER+"', in either the local servlet or global contexts. \n" + - "No context variable was found in either location.\n\n"); - } - - /** - * Stage 2 - * - * Load the dspace config. Also may load log4j configuration. - * (Please rely on ConfigurationManager or Log4j to configure logging) - * - */ - try - { - ConfigurationManager.loadConfig(dspaceConfig); - } - catch (Throwable t) - { - throw new ServletException( - "\n\nDSpace has failed to initialize, during stage 2. Error while attempting to read the \n" + - "DSpace configuration file (Path: '"+dspaceConfig+"'). \n" + - "This has likely occurred because either the file does not exist, or it's permissions \n" + - "are set incorrectly, or the path to the configuration file is incorrect. The path to \n" + - "the DSpace configuration file is stored in a context variable, 'dspace-config', in \n" + - "either the local servlet or global context.\n\n",t); - } - + /** * Stage 3 * diff --git a/dspace-xmlui/dspace-xmlui-webapp/src/main/webapp/WEB-INF/web.xml b/dspace-xmlui/dspace-xmlui-webapp/src/main/webapp/WEB-INF/web.xml index c8d39344dc..722ac6f8f4 100644 --- a/dspace-xmlui/dspace-xmlui-webapp/src/main/webapp/WEB-INF/web.xml +++ b/dspace-xmlui/dspace-xmlui-webapp/src/main/webapp/WEB-INF/web.xml @@ -2,22 +2,47 @@ + - + Manakin