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