Skip to content

Commit

Permalink
Use Servlet Context parameter for default override
Browse files Browse the repository at this point in the history
Tomcat Servlet Context Parameter elements can override
Servlet Config defaults as defined in WEB-INF/web.xml to
externlize configuration.
  • Loading branch information
claussni committed Aug 26, 2014
1 parent 421732f commit 3fd6f62
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 123 deletions.
2 changes: 1 addition & 1 deletion conf/log4j.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</category>

<root>
<priority value ="ERROR" />
<priority value ="INFO" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
6 changes: 3 additions & 3 deletions src/main/org/purl/sword/server/fedora/utils/FindMimeType.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,19 @@ public class FindMimeType {

/**
* Get the mime type from the file extension
* @param String the file extension
* @param pExtension The file extension
* @return String the mime type
*/
public static String getMimeType(final String pExtension) {
LOG.debug("Loading " + StartupServlet.getPropertiesLocation());
if (_mimeTypes == null) {
try {
SAXBuilder tBuilder = new SAXBuilder();
Document tProps = tBuilder.build(new FileInputStream(StartupServlet.getPropertiesLocation()));
Document tProps = tBuilder.build(new FileInputStream(StartupServlet.getPropertiesLocation().toFile()));
LOG.debug("Building props");
String tMimeTypesFilePath = tProps.getRootElement().getChild("files").getChild("mime-type").getText();
LOG.debug("Loading props file from " + tMimeTypesFilePath);
_mimeTypes = tBuilder.build(new FileInputStream(StartupServlet.getRealPath(tMimeTypesFilePath)));
_mimeTypes = tBuilder.build(new FileInputStream(StartupServlet.realPathHelper(tMimeTypesFilePath)));
LOG.debug("Built mime-types xml");
} catch (IOException tIOExcpt) {
LOG.error("Couldn't open properties file " + tIOExcpt.toString());
Expand Down
236 changes: 128 additions & 108 deletions src/main/org/purl/sword/server/fedora/utils/StartupServlet.java
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:
*
* &lt;servlet&gt;
* &lt;servlet-name&gt;StartupServlet&lt;/servlet-name&gt;
* &lt;servlet-class&gt;org.purl.sword.server.fedora.utils.StartupServlet&lt;/servlet-class&gt;
* &lt;init-param&gt;
* &lt;param-name&gt;log-config&lt;/param-name&gt;
* &lt;param-value&gt;WEB-INF/log4j.xml&lt;/param-value&gt;
* &lt;/init-param&gt;
* &lt;init-param&gt;
* &lt;param-name&gt;project.properties&lt;/param-name&gt;
* &lt;param-value&gt;WEB-INF/properties.xml&lt;/param-value&gt;
* &lt;/init-param&gt;
* &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
* &lt;/servlet&gt;
*/

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);
}
}
21 changes: 10 additions & 11 deletions src/main/org/purl/sword/server/fedora/utils/XMLProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

import java.io.FileInputStream;
import java.io.File;
Expand All @@ -80,7 +79,7 @@ public XMLProperties() {
SAXBuilder tBuilder = new SAXBuilder();
LOG.debug("Loading " + StartupServlet.getPropertiesLocation());
try {
_props = tBuilder.build(new FileInputStream(StartupServlet.getPropertiesLocation()));
_props = tBuilder.build(new FileInputStream(StartupServlet.getPropertiesLocation().toFile()));
} catch (IOException tIOExcpt) {
LOG.error("Couldn't open properties file " + tIOExcpt.toString());
} catch (JDOMException tJDOMExcpt) {
Expand Down Expand Up @@ -164,7 +163,7 @@ public String getRepositoryUri() throws SWORDException {
/**
* This returns the URL to the object metadata page for a specific PID
*
* @param String the object's pid
* @param pPID The object's pid
* @return String the URL to the object's metadata page
* @throws SWORDException if there was a problem reading the config file
*/
Expand All @@ -188,8 +187,8 @@ public String getExternalURL(final String pPID) throws SWORDException {
/**
* This returns the URL for the file that was ingested
*
* @param String the object's pid
* @param String the datastream name
* @param pPID The object's pid
* @param pDSId The datastream name
* @return String the URL to the file that was ingested
* @throws SWORDException if there was a problem reading the config file
*/
Expand Down Expand Up @@ -294,8 +293,8 @@ public String getEntryStoreLocation() throws SWORDException {
throw new SWORDException(tMessage, tJDOMExcpt);
}

return StartupServlet.getRealPath(tEntryLoc.getText());
}
return StartupServlet.realPathHelper(tEntryLoc.getText());
}
/**
* This returns the directory where the sub service documents are stored relative to the web app directory
*
Expand Down Expand Up @@ -323,7 +322,7 @@ public String getSubSDDir() throws SWORDException {
/**
* This is the method which retrieves the Service document from the properties file.
*
* @param String the user that is requesting the ServiceDocument
* @param pOnBehalfOf The user that is requesting the ServiceDocument
* @return ServiceDocument the service document
* @throws SWORDException if there was a problem reading the config file
*/
Expand All @@ -349,7 +348,7 @@ public ServiceDocument getServiceDocument(final String pOnBehalfOf, final String
SAXBuilder tBuilder = new SAXBuilder();
Document tChildDocs = null;
try {
tChildDocs = tBuilder.build(StartupServlet.getRealPath(new File(this.getSubSDDir(), pLocation).getPath()));
tChildDocs = tBuilder.build(StartupServlet.realPathHelper(new File(this.getSubSDDir(), pLocation).getPath()));
} catch (IOException tIOExcpt) {
String tMessage = "IO Exception occured on doServiceDocument method due to a problem accessing the properties file";
LOG.error(tMessage);
Expand Down Expand Up @@ -385,11 +384,11 @@ private void addLocationToService(final XMLServiceDocument pServiceDoc) throws S
public Document getProps() {
return _props;
}

/**
* Set props.
* ** Use only when you don't have access to the source for XMLProperties **
* @param props the value to set.
* @param pProps The value to set.
*/
public void setProps(final Document pProps) {
_props = pProps;
Expand Down

0 comments on commit 3fd6f62

Please sign in to comment.