View Javadoc

1   /*
2    * $Id: PropertiesMuleConfigurationFactory.java 22865 2011-09-05 17:23:45Z dfeist $
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;
12  
13  import org.mule.MuleServer;
14  import org.mule.api.config.MuleConfiguration;
15  import org.mule.util.BeanUtils;
16  import org.mule.util.ClassUtils;
17  import org.mule.util.FilenameUtils;
18  
19  import java.io.FileNotFoundException;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.lang.reflect.InvocationTargetException;
23  import java.net.URL;
24  import java.util.Map;
25  import java.util.Map.Entry;
26  import java.util.Properties;
27  
28  import org.apache.commons.io.IOUtils;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  public class PropertiesMuleConfigurationFactory
33  {
34  
35      private static Log logger = LogFactory.getLog(PropertiesMuleConfigurationFactory.class);
36      
37      private Properties properties;
38  
39      public static String getMuleAppConfiguration(String muleConfig)
40      {
41          String directory = FilenameUtils.getFullPath(muleConfig);
42          String muleAppConfiguration = directory + MuleServer.DEFAULT_APP_CONFIGURATION;
43          return muleAppConfiguration;
44      }
45      
46      public PropertiesMuleConfigurationFactory(String muleAppConfiguration)
47      {
48          URL muleAppURL = ClassUtils.getResource(muleAppConfiguration, getClass());
49          if (muleAppURL != null)
50          {
51              this.properties = new Properties();
52              InputStream inputStream = null;
53              try
54              {
55                  inputStream = muleAppURL.openStream();
56                  this.properties.load(inputStream);
57              }
58              catch (FileNotFoundException e)
59              {
60                  logger.debug(e);
61              }
62              catch (IOException e)
63              {
64                  logger.debug(e);
65              }
66              finally
67              {
68                  IOUtils.closeQuietly(inputStream);
69              }
70          }
71      }
72  
73      public DefaultMuleConfiguration createConfiguration()
74      {
75          DefaultMuleConfiguration configuration = new DefaultMuleConfiguration();
76          if (this.properties != null)
77          {
78              this.initializeFromProperties(configuration); 
79          }
80          return configuration;
81      }
82  
83      private void initializeFromProperties(MuleConfiguration configuration)
84      {
85          initializeFromProperties(configuration, this.properties);
86      }
87      
88      public static void initializeFromProperties(MuleConfiguration configuration, Map properties)
89      {
90          for (Object entryObject : properties.entrySet())
91          {
92              Entry entry = (Entry) entryObject;
93              String key = (String) entry.getKey();
94              String value = (String) entry.getValue();
95  
96              if (key.startsWith("sys."))
97              {
98                  String systemProperty = key.substring(4);
99                  System.setProperty(systemProperty, value);
100             }
101             else if (key.startsWith("mule.config."))
102             {
103                 String configProperty = key.substring(12);
104                 try
105                 {
106                     BeanUtils.setProperty(configuration, configProperty, value);
107                 }
108                 catch (IllegalAccessException e)
109                 {
110                     logger.error(e);
111                 }
112                 catch (InvocationTargetException e)
113                 {
114                     logger.error(e);
115                 }
116             }
117         }
118     }
119 }