View Javadoc

1   /*
2    * $Id: PropertiesContainerContext.java 7976 2007-08-21 14:26:13Z 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  
11  package org.mule.impl.container;
12  
13  import org.mule.MuleManager;
14  import org.mule.umo.manager.ContainerException;
15  import org.mule.umo.manager.ObjectNotFoundException;
16  import org.mule.util.TemplateParser;
17  
18  import java.io.Reader;
19  import java.util.Iterator;
20  import java.util.Map;
21  
22  /**
23   * Provides a facade for accessing System properties and properties on the
24   * ManagementContext. This container context serves 3 functions -
25   * <ol>
26   * <li>Allows System properties to be set in Mule Xml (by setting the
27   * #systemProperties Map)
28   * <li>Allows one to load System properties into the mule context so that MuleXml
29   * templates referring to System properties can be used (i.e. ${os.name}).
30   * <li>Provides a consistent way to set abitary properties on the Management
31   * Context. Setting properties on this container context is equivilent to using the
32   * <environment-properties> element in Mule Xml. The latter element may be removed in
33   * the future.
34   * </ol>
35   */
36  public class PropertiesContainerContext extends AbstractContainerContext
37  {
38  
39      protected Map systemProperties;
40      protected Map properties;
41      protected boolean loadSystemProperties = true;
42      protected boolean enableTemplates = false;
43  
44      protected TemplateParser templateParser = TemplateParser.createAntStyleParser();
45  
46      public PropertiesContainerContext()
47      {
48          super("properties");
49      }
50  
51      public void configure(Reader configuration) throws ContainerException
52      {
53          throw new UnsupportedOperationException("configure");
54      }
55  
56      /**
57       * Queries a component from the underlying container. For this container it will
58       * look up a property on the Mule Management Context.
59       * 
60       * @param key the key fo find the component with. It's up to the individual
61       *            implementation to check the type of this key and look up objects
62       *            accordingly
63       * @return the component found in the container
64       * @throws org.mule.umo.manager.ObjectNotFoundException if the component is not
65       *             found
66       */
67      public Object getComponent(Object key) throws ObjectNotFoundException
68      {
69          if (key == null)
70          {
71              throw new ObjectNotFoundException("null");
72          }
73          Object value = MuleManager.getInstance().getProperty(key.toString());
74          if (value == null)
75          {
76              throw new ObjectNotFoundException(key.toString());
77          }
78          if (value instanceof String && enableTemplates)
79          {
80              value = templateParser.parse(MuleManager.getInstance().getProperties(), value.toString());
81          }
82          return value;
83      }
84  
85      public Map getSystemProperties()
86      {
87          return systemProperties;
88      }
89  
90      public void setSystemProperties(Map properties)
91      {
92          this.systemProperties = properties;
93          String value;
94          Map.Entry entry;
95          if (systemProperties != null)
96          {
97              for (Iterator iterator = systemProperties.entrySet().iterator(); iterator.hasNext();)
98              {
99                  entry = (Map.Entry) iterator.next();
100                 value = entry.getValue().toString();
101                 value = templateParser.parse(systemProperties, value);
102                 value = templateParser.parse(MuleManager.getInstance().getProperties(), value);
103                 System.setProperty(entry.getKey().toString(), value);
104             }
105         }
106 
107         if (loadSystemProperties)
108         {
109             Map props = System.getProperties();
110 
111             for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();)
112             {
113                 entry = (Map.Entry) iterator.next();
114                 value = entry.getValue().toString();
115                 value = templateParser.parse(MuleManager.getInstance().getProperties(), value.toString());
116                 MuleManager.getInstance().setProperty(entry.getKey(), value);
117             }
118         }
119     }
120 
121     public Map getProperties()
122     {
123         return properties;
124     }
125 
126     public void setProperties(Map properties)
127     {
128         this.properties = properties;
129         if (properties != null)
130         {
131             Map.Entry entry;
132             String value;
133             for (Iterator iterator = properties.entrySet().iterator(); iterator.hasNext();)
134             {
135                 entry = (Map.Entry) iterator.next();
136                 value = entry.getValue().toString();
137                 value = templateParser.parse(MuleManager.getInstance().getProperties(), value);
138                 MuleManager.getInstance().setProperty(entry.getKey(), value);
139             }
140         }
141     }
142 }