From 2e6110f33408f3d212cec754a21a204ec7ecad7d Mon Sep 17 00:00:00 2001 From: Ralf Claussnitzer Date: Mon, 15 Dec 2014 17:13:00 +0100 Subject: [PATCH] Complain if included default configuration files are not accessable via 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. --- .../server/fedora/utils/StartupListener.java | 66 +++++++++++-------- 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/purl/sword/server/fedora/utils/StartupListener.java b/src/main/java/org/purl/sword/server/fedora/utils/StartupListener.java index 24ac685..640c3ba 100644 --- a/src/main/java/org/purl/sword/server/fedora/utils/StartupListener.java +++ b/src/main/java/org/purl/sword/server/fedora/utils/StartupListener.java @@ -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; } }