From e91a7c1b77d8e36a7268cc6c7cf8a6aba72f449f Mon Sep 17 00:00:00 2001 From: mcentner Date: Thu, 27 May 2010 15:21:57 +0000 Subject: Added 'failOnMissingResource' property to ConfigurationFactoryBean. git-svn-id: https://joinup.ec.europa.eu/svn/mocca/trunk@745 8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4 --- .../egiz/bku/conf/MoccaConfigurationException.java | 45 ++++++++++++++++++++++ .../egiz/bku/spring/ConfigurationFactoryBean.java | 43 +++++++++++++++++---- 2 files changed, 80 insertions(+), 8 deletions(-) create mode 100644 bkucommon/src/main/java/at/gv/egiz/bku/conf/MoccaConfigurationException.java (limited to 'bkucommon/src/main/java/at') diff --git a/bkucommon/src/main/java/at/gv/egiz/bku/conf/MoccaConfigurationException.java b/bkucommon/src/main/java/at/gv/egiz/bku/conf/MoccaConfigurationException.java new file mode 100644 index 00000000..3846e88a --- /dev/null +++ b/bkucommon/src/main/java/at/gv/egiz/bku/conf/MoccaConfigurationException.java @@ -0,0 +1,45 @@ +/* +* Copyright 2009 Federal Chancellery Austria and +* Graz University of Technology +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/** + * + */ +package at.gv.egiz.bku.conf; + +/** + * @author mcentner + */ +public class MoccaConfigurationException extends Exception { + + private static final long serialVersionUID = 1L; + + public MoccaConfigurationException() { + } + + public MoccaConfigurationException(String message) { + super(message); + } + + public MoccaConfigurationException(Throwable cause) { + super(cause); + } + + public MoccaConfigurationException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/bkucommon/src/main/java/at/gv/egiz/bku/spring/ConfigurationFactoryBean.java b/bkucommon/src/main/java/at/gv/egiz/bku/spring/ConfigurationFactoryBean.java index a6a7c346..dd0aa1a6 100644 --- a/bkucommon/src/main/java/at/gv/egiz/bku/spring/ConfigurationFactoryBean.java +++ b/bkucommon/src/main/java/at/gv/egiz/bku/spring/ConfigurationFactoryBean.java @@ -36,6 +36,7 @@ import org.springframework.context.ResourceLoaderAware; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; +import at.gv.egiz.bku.conf.MoccaConfigurationException; import at.gv.egiz.bku.slcommands.impl.CreateXMLSignatureCommandImpl; /** @@ -59,6 +60,12 @@ public class ConfigurationFactoryBean implements FactoryBean, ResourceLoaderAwar * The URL of the configuration file. */ protected Resource configurationResource; + + /** + * Should building the configuration fail if the given configuration resource + * is not available? + */ + protected boolean failOnMissingResource = false; /** * The ResourceLoader. @@ -84,6 +91,14 @@ public class ConfigurationFactoryBean implements FactoryBean, ResourceLoaderAwar this.configurationResource = configurationResource; } + /** + * @param failOnMissingConfigurationResource the failOnMissingConfigurationResource to set + */ + public void setfailOnMissingResource( + Boolean failOnMissingResource) { + this.failOnMissingResource = failOnMissingResource; + } + protected Configuration getDefaultConfiguration() throws ConfigurationException, IOException { Resource resource = resourceLoader.getResource(DEFAULT_CONFIG); @@ -141,18 +156,30 @@ public class ConfigurationFactoryBean implements FactoryBean, ResourceLoaderAwar log.info("Configuration resource is {}.", configurationResource); - CompositeConfiguration configuration; - if (configurationResource == null) { + CompositeConfiguration configuration = null; + if (configurationResource != null) { + if (configurationResource.exists()) { + // initialize with writable configuration + URL url = configurationResource.getURL(); + XMLConfiguration writableConfiguration = new XMLConfiguration(url); + configuration = new CompositeConfiguration(writableConfiguration); + log.info("Initialized with configuration from '{}'.", url); + } else if (failOnMissingResource) { + StringBuilder message = new StringBuilder(); + message.append("ConfigurationResource '"); + message.append(configurationResource.getDescription()); + message.append("' does not exist!"); + log.error(message.toString()); + throw new MoccaConfigurationException(message.toString()); + } + } + + if (configuration == null) { // initialize default configuration log.warn("Initializing with default configuration."); configuration = new CompositeConfiguration(); - } else { - // initialize with writable configuration - URL url = configurationResource.getURL(); - XMLConfiguration writableConfiguration = new XMLConfiguration(url); - configuration = new CompositeConfiguration(writableConfiguration); - log.info("Initialized with configuration from '{}'.", url); } + configuration.addConfiguration(getDefaultConfiguration()); configuration.addConfiguration(getVersionConfiguration()); return configuration; -- cgit v1.2.3