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.spring.processors;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.config.PropertyFactory;
11  import org.mule.api.context.MuleContextAware;
12  import org.mule.config.i18n.CoreMessages;
13  
14  import java.io.IOException;
15  import java.util.HashMap;
16  import java.util.Iterator;
17  import java.util.Map;
18  import java.util.Properties;
19  
20  import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
21  
22  /**
23   * TODO
24   */
25  public class PropertyPlaceholderProcessor extends PropertyPlaceholderConfigurer implements MuleContextAware
26  {
27      private MuleContext muleContext;
28      private Map factories = new HashMap();
29      
30  
31      @Override
32      protected Properties mergeProperties() throws IOException
33      {
34          RegistryProperties props = new RegistryProperties();
35          props.putAll(super.mergeProperties());
36  
37          // MuleContext/MuleConfiguration properties
38          props.put("mule.working.dir", muleContext.getConfiguration().getWorkingDirectory());
39          
40          if (factories != null)
41          {
42              for (Iterator iterator = factories.entrySet().iterator(); iterator.hasNext();)
43              {
44                  Map.Entry entry = (Map.Entry) iterator.next();
45                  if (entry.getKey() == null)
46                  {
47                      throw new NullPointerException(CoreMessages.objectIsNull("Factories.Key").getMessage());
48                  }
49                  if (entry.getValue() == null)
50                  {
51                      throw new NullPointerException(CoreMessages.objectIsNull("Factories.Value").getMessage());
52                  }
53                  try
54                  {
55                      props.put(entry.getKey(), ((PropertyFactory) entry.getValue()).create(props));
56                  }
57                  catch (Exception e)
58                  {
59                      throw new IOException("Failed to invoke PropertyFactory: " + entry.getValue() + ". Error is: " + e.toString());
60                  }
61              }
62          }
63          return props;
64      }
65  
66      public Map getFactories()
67      {
68          return factories;
69      }
70  
71      public void setFactories(Map factories)
72      {
73          this.factories = factories;
74      }
75  
76      private class RegistryProperties extends Properties
77      {
78          public String getProperty(String key)
79          {
80              Object oval = super.get(key);
81              if (oval == null)
82              {
83                  oval = muleContext.getRegistry().lookupObject(key);
84              }
85              String sval = (oval instanceof String) ? (String)oval : null;
86              return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval;
87          }
88      }
89  
90      public void setMuleContext(MuleContext muleContext)
91      {
92          this.muleContext = muleContext;
93          
94      }
95  
96  }