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.ClassUtils;
15  import org.mule.util.StringUtils;
16  
17  import java.util.ArrayList;
18  import java.util.Iterator;
19  import java.util.LinkedHashMap;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.Properties;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  /**
28   * Configures Mule from a configuration resource or comma seperated list of configuration resources by
29   * auto-detecting the ConfigurationBuilder to use for each resource. This is resolved by either checking the
30   * classpath for config modules e.g. spring-config or by using the file extention or a combination.
31   */
32  public class AutoConfigurationBuilder extends AbstractResourceConfigurationBuilder
33  {
34      protected static final Log logger = LogFactory.getLog(AutoConfigurationBuilder.class);
35  
36      public AutoConfigurationBuilder(String resource) throws ConfigurationException
37      {
38          super(resource);
39      }
40  
41      public AutoConfigurationBuilder(String[] resources) throws ConfigurationException
42      {
43          super(resources);
44      }
45  
46      public AutoConfigurationBuilder(ConfigResource[] resources)
47      {
48          super(resources);
49      }
50  
51      protected void doConfigure(MuleContext muleContext) throws ConfigurationException
52      {
53          autoConfigure(muleContext, configResources);
54      }
55  
56      protected void autoConfigure(MuleContext muleContext, ConfigResource[] configResources) throws ConfigurationException
57      {
58  
59          Map configsMap = new LinkedHashMap();
60  
61          for (int i = 0; i < configResources.length; i++)
62          {
63              String configExtension = StringUtils.substringAfterLast(
64                  (configResources[i]).getUrl().getFile(), ".");
65              List configs = (List) configsMap.get(configExtension);
66              if (configs == null)
67              {
68                  configs = new ArrayList();
69                  configsMap.put(configExtension, configs);
70              }
71              configs.add(configResources[i]);
72          }
73  
74          try
75          {
76              Properties props = new Properties();
77              props.load(ClassUtils.getResource("configuration-builders.properties", this.getClass()).openStream());
78  
79              Iterator iterator = configsMap.entrySet().iterator();
80              while (iterator.hasNext())
81              {
82                  Map.Entry e = (Map.Entry) iterator.next();
83                  String extension = (String) e.getKey();
84                  List configs = (List) e.getValue();
85  
86                  String className = (String) props.get(extension);
87  
88                  if (className == null || !ClassUtils.isClassOnPath(className, this.getClass()))
89                  {
90                      throw new ConfigurationException(
91                          CoreMessages.configurationBuilderNoMatching(createConfigResourcesString()));
92                  }
93  
94                  ConfigResource[] constructorArg = new ConfigResource[configs.size()];
95                  System.arraycopy(configs.toArray(), 0, constructorArg, 0, configs.size());
96                  ConfigurationBuilder cb = (ConfigurationBuilder) ClassUtils.instanciateClass(className, new Object[] {constructorArg});
97                  cb.configure(muleContext);
98              }
99          }
100         catch (ConfigurationException e)
101         {
102             throw e;
103         }
104         catch (Exception e)
105         {
106             throw new ConfigurationException(e);
107         }
108     }
109 
110 }