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