mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-16 14:33:09 +00:00
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
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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();
|
||||
|
Reference in New Issue
Block a user