View Javadoc

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