001: /*
002: * $Id: AbstractResourceConfigurationBuilder.java 11343 2008-03-13 10:58:26Z tcarlson $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.config.builders;
012:
013: import org.mule.api.MuleContext;
014: import org.mule.api.config.ConfigurationBuilder;
015: import org.mule.api.config.ConfigurationException;
016: import org.mule.config.ConfigResource;
017: import org.mule.config.i18n.CoreMessages;
018: import org.mule.util.StringUtils;
019:
020: import java.io.IOException;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024:
025: /**
026: * Abstract {@link ConfigurationBuilder} implementation used for
027: * ConfigurationBuider's that use one of more configuration resources of the same
028: * type that are defined using strings or {@link ConfigResource} objects. It is recommended that
029: * {@link org.mule.config.ConfigResource} objects are used over strings since they can be more descriptive, but
030: * Strings will be supported for quite some time.
031: */
032: public abstract class AbstractResourceConfigurationBuilder extends
033: AbstractConfigurationBuilder {
034:
035: protected static final Log logger = LogFactory
036: .getLog(AutoConfigurationBuilder.class);
037:
038: protected ConfigResource[] configResources;
039:
040: /**
041: * @param configResources a comma separated list of configuration files to load,
042: * this should be accessible on the classpath or filesystem
043: * @throws org.mule.api.config.ConfigurationException usually if the config resources cannot be loaded
044: */
045: public AbstractResourceConfigurationBuilder(String configResources)
046: throws ConfigurationException {
047: this .configResources = loadConfigResources(StringUtils
048: .splitAndTrim(configResources, ",; "));
049: }
050:
051: /**
052: * @param configResources an array of configuration files to load, this should be
053: * accessible on the classpath or filesystem
054: * @throws org.mule.api.config.ConfigurationException usually if the config resources cannot be loaded
055: */
056: public AbstractResourceConfigurationBuilder(String[] configResources)
057: throws ConfigurationException {
058: this .configResources = loadConfigResources(configResources);
059: }
060:
061: /**
062: * @param configResources an array Reader oject that provides acces to a configuration either locally or remotely
063: */
064: public AbstractResourceConfigurationBuilder(
065: ConfigResource[] configResources) {
066: this .configResources = configResources;
067: }
068:
069: /**
070: * Override to check for existence of configResouce before invocation, and set
071: * resources n configuration afterwards.
072: */
073: public void configure(MuleContext muleContext)
074: throws ConfigurationException {
075: if (configResources == null) {
076: throw new ConfigurationException(CoreMessages
077: .objectIsNull("Configuration Resources"));
078: }
079:
080: super .configure(muleContext);
081:
082: logger.info(CoreMessages.configurationBuilderSuccess(this ,
083: createConfigResourcesString()));
084: }
085:
086: protected ConfigResource[] loadConfigResources(String[] configs)
087: throws ConfigurationException {
088: try {
089: configResources = new ConfigResource[configs.length];
090: for (int i = 0; i < configs.length; i++) {
091: configResources[i] = new ConfigResource(configs[i]);
092: }
093: return configResources;
094: } catch (IOException e) {
095: throw new ConfigurationException(e);
096: }
097: }
098:
099: protected String createConfigResourcesString() {
100: StringBuffer configResourcesString = new StringBuffer();
101: configResourcesString.append("[");
102: for (int i = 0; i < configResources.length; i++) {
103: configResourcesString.append(configResources[i]);
104: if (i < configResources.length - 1) {
105: configResourcesString.append(", ");
106: }
107: }
108: configResourcesString.append("]");
109: return configResourcesString.toString();
110: }
111: }
|