View Javadoc

1   /*
2    * $Id: MuleClasspathConfigurationBuilder.java 10623 2008-01-30 14:29:00Z aperepel $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.config.ConfigurationException;
14  import org.mule.config.ReaderResource;
15  import org.mule.config.i18n.CoreMessages;
16  import org.mule.umo.manager.UMOManager;
17  import org.mule.util.ObjectUtils;
18  import org.mule.util.StringUtils;
19  
20  import java.io.IOException;
21  import java.io.InputStreamReader;
22  import java.net.URL;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  /**
30   * <code>MuleClasspathConfigurationBuilder</code> can be used to configure a
31   * MuleManager based on the configuration files on the classpath. the default config
32   * resource name is <b>mule-config.xml</b> but this can be overrided by passing a
33   * config resourse name or a list of resource names (comma separated) to the
34   * configure method.
35   * 
36   * @deprecated The functionality of this configuration builder (loading resources
37   *             from the classpath) is now available in the standard
38   *             MuleXmlConfigurationBuilder. If you are using this builder, please
39   *             verify whether your configuration will work with
40   *             org.mule.config.builders.MuleXmlConfigurationBuilder as this class is
41   *             deprecated and is soon to be removed.
42   */
43  public class MuleClasspathConfigurationBuilder extends MuleXmlConfigurationBuilder
44  {
45      /**
46       * logger used by this class
47       */
48      protected static final Log logger = LogFactory.getLog(MuleClasspathConfigurationBuilder.class);
49  
50      public static final String MULE_CONFIGURATION_RESOURCE = "mule-config.xml";
51  
52      public MuleClasspathConfigurationBuilder() throws ConfigurationException
53      {
54          super();
55      }
56  
57      /**
58       * Will configure a UMOManager based on the configuration file(s) provided.
59       * 
60       * @param configResources can be null or a comma separated resources name string
61       *            that will be used to search the classpath. The default is
62       *            mule-config.xml.
63       * @return A configured UMOManager
64       * @throws org.mule.config.ConfigurationException if the configResources param is
65       *             invalid or the configurations fail to load
66       */
67      public UMOManager configure(String configResources, String startupPropertiesFile)
68          throws ConfigurationException
69      {
70          if (StringUtils.isBlank(configResources))
71          {
72              configResources = MULE_CONFIGURATION_RESOURCE;
73          }
74  
75          URL url = null;
76          List list = new ArrayList();
77          String[] resString;
78          int i = 0;
79  
80          try
81          {
82              resString = StringUtils.splitAndTrim(configResources, ",");
83              for (i = 0; i < resString.length; i++)
84              {
85                  final ClassLoader cl = Thread.currentThread().getContextClassLoader();
86                  if (cl == null)
87                  {
88                      break;
89                  }
90                  
91                  url = cl.getResource(resString[i]);
92                  if (url == null)
93                  {
94                      break;
95                  }
96                  list.add(new ReaderResource(url.toExternalForm(), new InputStreamReader(url.openStream())));
97              }
98          }
99          catch (IOException ioe)
100         {
101             throw new ConfigurationException(
102                 CoreMessages.failedToLoad("Config: " + ObjectUtils.toString(url, "null")), ioe);
103         }
104 
105         if (list.size() != resString.length)
106         {
107             throw new ConfigurationException(
108                 CoreMessages.failedToLoad("Not all resources specified loaded: " + resString[i]));
109         }
110 
111         ReaderResource[] resources = new ReaderResource[list.size()];
112         resources = (ReaderResource[])list.toArray(resources);
113         configure(resources, null);
114         return manager;
115     }
116 }