View Javadoc

1   /*
2    * $Id: AbstractResourceConfigurationBuilder.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.config.builders;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.config.ConfigurationBuilder;
15  import org.mule.api.config.ConfigurationException;
16  import org.mule.config.ConfigResource;
17  import org.mule.config.i18n.CoreMessages;
18  import org.mule.util.StringUtils;
19  
20  import java.io.IOException;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  /**
26   * Abstract {@link ConfigurationBuilder} implementation used for
27   * ConfigurationBuider's that use one of more configuration resources of the same
28   * type that are defined using strings or {@link ConfigResource} objects.  It is recommended that
29   * {@link org.mule.config.ConfigResource} objects are used over strings since they can be more descriptive, but
30   * Strings will be supported for quite some time.
31   */
32  public abstract class AbstractResourceConfigurationBuilder extends AbstractConfigurationBuilder
33  {
34  
35      protected static final Log logger = LogFactory.getLog(AutoConfigurationBuilder.class);
36  
37      protected ConfigResource[] configResources;
38  
39      /**
40       * @param configResources a comma separated list of configuration files to load,
41       *            this should be accessible on the classpath or filesystem
42       * @throws org.mule.api.config.ConfigurationException usually if the config resources cannot be loaded
43       */
44      public AbstractResourceConfigurationBuilder(String configResources) throws ConfigurationException
45      {
46          this.configResources = loadConfigResources(StringUtils.splitAndTrim(configResources, ",; "));
47      }
48  
49      /**
50       * @param configResources an array of configuration files to load, this should be
51       *            accessible on the classpath or filesystem
52       * @throws org.mule.api.config.ConfigurationException usually if the config resources cannot be loaded
53       */
54      public AbstractResourceConfigurationBuilder(String[] configResources) throws ConfigurationException
55      {
56          this.configResources = loadConfigResources(configResources);
57      }
58  
59      /**
60       * @param configResources an array Reader oject that provides acces to a configuration either locally or remotely
61       */
62      public AbstractResourceConfigurationBuilder(ConfigResource[] configResources)
63      {
64          this.configResources = configResources;
65      }
66  
67      /**
68       * Override to check for existence of configResouce before invocation, and set
69       * resources n configuration afterwards.
70       */
71      public void configure(MuleContext muleContext) throws ConfigurationException
72      {
73          if (configResources == null)
74          {
75              throw new ConfigurationException(CoreMessages.objectIsNull("Configuration Resources"));
76          }
77  
78          super.configure(muleContext);
79  
80          logger.info(CoreMessages.configurationBuilderSuccess(this, createConfigResourcesString()));
81      }
82  
83      protected ConfigResource[] loadConfigResources(String[] configs) throws ConfigurationException
84      {
85          try
86          {
87              configResources = new ConfigResource[configs.length];
88              for (int i = 0; i < configs.length; i++)
89              {
90                  configResources[i] = new ConfigResource(configs[i]);
91              }
92              return configResources;
93          }
94          catch (IOException e)
95          {
96              throw new ConfigurationException(e);
97          }
98      }
99  
100     protected String createConfigResourcesString()
101     {
102         StringBuffer configResourcesString = new StringBuffer();
103         configResourcesString.append("[");
104         for (int i = 0; i < configResources.length; i++)
105         {
106             configResourcesString.append(configResources[i]);
107             if (i < configResources.length - 1)
108             {
109                 configResourcesString.append(", ");
110             }
111         }
112         configResourcesString.append("]");
113         return configResourcesString.toString();
114     }
115 }