View Javadoc

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