-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Servlet Context parameter for default override
Tomcat Servlet Context Parameter elements can override Servlet Config defaults as defined in WEB-INF/web.xml to externlize configuration.
- Loading branch information
Showing
4 changed files
with
142 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
236 changes: 128 additions & 108 deletions
236
src/main/org/purl/sword/server/fedora/utils/StartupServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,123 +1,143 @@ | ||
/* | ||
* Copyright (c) 2007, Aberystwyth University | ||
* | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* - Redistributions of source code must retain the above | ||
* copyright notice, this list of conditions and the | ||
* following disclaimer. | ||
* | ||
* - Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in | ||
* the documentation and/or other materials provided with the | ||
* distribution. | ||
* | ||
* - Neither the name of the Centre for Advanced Software and | ||
* Intelligent Systems (CASIS) nor the names of its | ||
* contributors may be used to endorse or promote products derived | ||
* from this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | ||
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF | ||
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
* SUCH DAMAGE. | ||
*/ | ||
package org.purl.sword.server.fedora.utils; | ||
|
||
/** | ||
* Copyright (c) 2007, Aberystwyth University | ||
* | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* - Redistributions of source code must retain the above | ||
* copyright notice, this list of conditions and the | ||
* following disclaimer. | ||
* | ||
* - Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in | ||
* the documentation and/or other materials provided with the | ||
* distribution. | ||
* | ||
* - Neither the name of the Centre for Advanced Software and | ||
* Intelligent Systems (CASIS) nor the names of its | ||
* contributors may be used to endorse or promote products derived | ||
* from this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | ||
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF | ||
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
* SUCH DAMAGE. | ||
* | ||
* @author Glen Robson | ||
* @version 1.0 | ||
* Date: 18 October 2007 | ||
* | ||
* This servlet sets up the log4j configuration and reads in the properties file | ||
* for the application. | ||
* | ||
* In the web.conf ensure the load-on-startup property is set to 1 so this servlet runs first. | ||
* The init-properties are shown below: | ||
* | ||
* <servlet> | ||
* <servlet-name>StartupServlet</servlet-name> | ||
* <servlet-class>org.purl.sword.server.fedora.utils.StartupServlet</servlet-class> | ||
* <init-param> | ||
* <param-name>log-config</param-name> | ||
* <param-value>WEB-INF/log4j.xml</param-value> | ||
* </init-param> | ||
* <init-param> | ||
* <param-name>project.properties</param-name> | ||
* <param-value>WEB-INF/properties.xml</param-value> | ||
* </init-param> | ||
* <load-on-startup>1</load-on-startup> | ||
* </servlet> | ||
*/ | ||
|
||
import org.apache.log4j.Logger; | ||
import org.apache.log4j.xml.DOMConfigurator; | ||
|
||
import javax.servlet.ServletConfig; | ||
import javax.servlet.http.HttpServlet; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
import org.apache.log4j.Logger; | ||
|
||
/** | ||
* This servlet sets up the log4j configuration and reads in the properties file | ||
* for the application. | ||
* <p/> | ||
* In the web.conf ensure the load-on-startup property is set to 1 so this servlet runs first. | ||
* The init-properties are shown below: | ||
* <p/> | ||
* <pre> | ||
* {@code | ||
* <servlet> | ||
* <servlet-name>StartupServlet</servlet-name> | ||
* <servlet-class>org.purl.sword.server.fedora.utils.StartupServlet</servlet-class> | ||
* <init-param> | ||
* <param-name>log-config</param-name> | ||
* <param-value>WEB-INF/log4j.xml</param-value> | ||
* </init-param> | ||
* <init-param> | ||
* <param-name>project.properties</param-name> | ||
* <param-value>WEB-INF/properties.xml</param-value> | ||
* </init-param> | ||
* <load-on-startup>1</load-on-startup> | ||
* </servlet> | ||
* } | ||
* </pre> | ||
* If you give relative file names, the path is resolved against the | ||
* context of the application. To resolve against file system, use | ||
* absolute paths, e.g.: | ||
* <p/> | ||
* <pre> | ||
* {@code | ||
* <init-param> | ||
* <param-name>log-config</param-name> | ||
* <param-value>/etc/swordapp/config/log4j.xml</param-value> | ||
* </init-param> | ||
* } | ||
* </pre> | ||
* | ||
* @author Glen Robson | ||
* @version 1.0 | ||
* @since 2007-10-18 | ||
*/ | ||
public class StartupServlet extends HttpServlet { | ||
protected static HttpServlet SERVLET = null; | ||
protected static String _properties = null; | ||
|
||
public StartupServlet() { | ||
} | ||
private static ServletConfig CONFIG; | ||
|
||
private static Path propertiesLocation; | ||
private final Logger log = Logger.getLogger(StartupServlet.class); | ||
|
||
/** | ||
* This method loads in the properties file and log4j configuration | ||
*/ | ||
public void init() { | ||
CONFIG = this.getServletConfig(); | ||
initializeLogging(getInitParameterConsideringOverride("log-config")); | ||
initializePropertiesLocation(getInitParameterConsideringOverride("project.properties")); | ||
} | ||
|
||
private void initializeLogging(String pathToLog4JConfig) { | ||
Path logConfigPath = getAbsolutePathToResource(pathToLog4JConfig); | ||
DOMConfigurator.configure(logConfigPath.toString()); | ||
log.info("Taking log4j config from: " + logConfigPath.toString()); | ||
} | ||
|
||
private void initializePropertiesLocation(String pathToProjectProperties) { | ||
propertiesLocation = getAbsolutePathToResource(pathToProjectProperties); | ||
log.info("loading properties files from: " + propertiesLocation.toString()); | ||
} | ||
|
||
/** | ||
* If you want to run this project from the command line you can | ||
* call this method | ||
* @param String the properties file | ||
*/ | ||
public StartupServlet(final String pPropsFile) { | ||
_properties = pPropsFile; | ||
} | ||
private String getInitParameterConsideringOverride(String name) { | ||
String contextParameterOverride = getServletContext().getInitParameter(name); | ||
return isPresentAndSet(contextParameterOverride) ? contextParameterOverride : super.getInitParameter(name); | ||
} | ||
|
||
private boolean isPresentAndSet(String initParameter) { | ||
return (initParameter != null) && (!initParameter.isEmpty()); | ||
} | ||
|
||
/** | ||
* This method loads in the properties file and log4j configuration | ||
*/ | ||
public void init() { | ||
if (_properties == null) { | ||
_properties = this.getServletConfig().getInitParameter("project.properties"); | ||
if (SERVLET == null) { | ||
SERVLET = this; | ||
} | ||
String tLog4jConf = this.getServletConfig().getInitParameter("log-config"); | ||
|
||
DOMConfigurator.configure(StartupServlet.getRealPath(tLog4jConf)); | ||
private Path getAbsolutePathToResource(String resourcePath) { | ||
if (resourcePath.startsWith("/")) { | ||
return Paths.get(resourcePath); | ||
} else { | ||
// assume file is in servlet context | ||
return Paths.get(this.getServletContext().getRealPath(resourcePath)); | ||
} | ||
} | ||
|
||
Logger LOG = Logger.getLogger(StartupServlet.class); | ||
LOG.debug("loading properties files: " + _properties); | ||
LOG.debug("Taking log4j config from: " + StartupServlet.getRealPath(tLog4jConf)); | ||
} | ||
} | ||
public static Path getPropertiesLocation() { | ||
return propertiesLocation; | ||
} | ||
|
||
/** | ||
* Allows static access to the properties location | ||
* | ||
* @return String the full path to the properties file | ||
*/ | ||
public static String getPropertiesLocation() { | ||
return StartupServlet.getRealPath(_properties); | ||
} | ||
public static String realPathHelper(String resourcePath) { | ||
return Paths.get(CONFIG.getServletContext().getRealPath(resourcePath)) | ||
.toAbsolutePath() | ||
.toString(); | ||
} | ||
|
||
/** | ||
* Allows classes to get real path of files on the file system | ||
* @param String path to convert | ||
* @param String the absolute path to a file | ||
*/ | ||
public static String getRealPath(final String pPath) { | ||
return SERVLET.getServletContext().getRealPath(pPath); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters