Skip to content

Commit

Permalink
Complain if included default configuration files are not accessable v…
Browse files Browse the repository at this point in the history
…ia file system.

Referencing war-packaged files only works in Tomcat 7.0.5x. All other deployments need to either unpack the war file or configure external configuration files via context parameters.
  • Loading branch information
claussni committed Dec 15, 2014
1 parent 79c7d66 commit 2e6110f
Showing 1 changed file with 39 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,41 +128,53 @@ public void contextInitialized(ServletContextEvent sce) {
public void contextDestroyed(ServletContextEvent sce) {
}

private void initLog4j() {
String logConfigPath = getAbsolutePathToResource(
orDefaultIfNull(getFirstValid(
System.getProperty("log4j.configuration"),
context.getInitParameter("log-config")),
"WEB-INF/log4j.xml"));
log.info("Taking log4j config from: " + logConfigPath);
configure(logConfigPath);
private String getAbsolutePathToResource(String resourcePath) throws Exception {
File file;
if (resourcePath.startsWith("/")) {
file = new File(resourcePath);
} else {
final String realPath = context.getRealPath(resourcePath);
if (realPath == null) {
throw new Exception("Cannot obtain absolute path to file if wrapped in war-archive.");
}
file = new File(realPath);
}
return file.getAbsolutePath();
}

private void initPropertiesLocation() {
propertiesLocation = getAbsolutePathToResource(
orDefaultIfNull(getFirstValid(
System.getProperty("project.properties"),
context.getInitParameter("project.properties")),
"WEB-INF/properties.xml"));
log.info("Loading properties files from: " + propertiesLocation);
private String getFirstValid(String... values) {
for (String s : values) if (s != null) return s;
return null;
}

private String getAbsolutePathToResource(String resourcePath) {
if (resourcePath.startsWith("/")) {
File file = new File(resourcePath);
return file.getAbsolutePath();
} else {
// assume file is in servlet context
return context.getRealPath(resourcePath);
private void initLog4j() {
try {
String logConfigPath = getAbsolutePathToResource(
orDefaultIfNull(getFirstValid(
System.getProperty("log4j.configuration"),
context.getInitParameter("log-config")),
"WEB-INF/log4j.xml"));
log.info("Taking log4j config from: " + logConfigPath);
configure(logConfigPath);
} catch (Exception e) {
log.error("Error initializing logging: " + e.getMessage());
}
}

private String orDefaultIfNull(String s, String defaultValue) {
return (s == null) ? defaultValue : s;
private void initPropertiesLocation() {
try {
propertiesLocation = getAbsolutePathToResource(
orDefaultIfNull(getFirstValid(
System.getProperty("project.properties"),
context.getInitParameter("project.properties")),
"WEB-INF/properties.xml"));
log.info("Loading properties files from: " + propertiesLocation);
} catch (Exception e) {
log.fatal("Fatal Error initializing properties: " + e.getMessage());
}
}

private String getFirstValid(String... values) {
for (String s : values) if (s != null) return s;
return null;
private String orDefaultIfNull(String s, String defaultValue) {
return (s == null) ? defaultValue : s;
}
}

0 comments on commit 2e6110f

Please sign in to comment.