KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > impl > container > PropertiesContainerContext


1 /*
2  * $Id: PropertiesContainerContext.java 4259 2006-12-14 03:12:07Z aperepel $
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 MuleSource MPL
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 JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
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  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
37  * @version $Revision: 4259 $
38  */

39 public class PropertiesContainerContext extends AbstractContainerContext
40 {
41
42     protected Map JavaDoc systemProperties;
43     protected Map JavaDoc properties;
44     protected boolean loadSystemProperties = true;
45     protected boolean enableTemplates = false;
46
47     protected TemplateParser templateParser = TemplateParser.createAntStyleParser();
48
49     public PropertiesContainerContext()
50     {
51         super("properties");
52     }
53
54     public void configure(Reader JavaDoc configuration) throws ContainerException
55     {
56         throw new UnsupportedOperationException JavaDoc("configure");
57     }
58
59     /**
60      * Queries a component from the underlying container. For this container it will
61      * look up a property on the Mule Management Context.
62      *
63      * @param key the key fo find the component with. It's up to the individual
64      * implementation to check the type of this key and look up objects
65      * accordingly
66      * @return the component found in the container
67      * @throws org.mule.umo.manager.ObjectNotFoundException if the component is not
68      * found
69      */

70     public Object JavaDoc getComponent(Object JavaDoc key) throws ObjectNotFoundException
71     {
72         if (key == null)
73         {
74             throw new ObjectNotFoundException("null");
75         }
76         Object JavaDoc value = MuleManager.getInstance().getProperty(key.toString());
77         if (value == null)
78         {
79             throw new ObjectNotFoundException(key.toString());
80         }
81         if (value instanceof String JavaDoc && enableTemplates)
82         {
83             value = templateParser.parse(MuleManager.getInstance().getProperties(), value.toString());
84         }
85         return value;
86     }
87
88     public Map JavaDoc getSystemProperties()
89     {
90         return systemProperties;
91     }
92
93     public void setSystemProperties(Map JavaDoc properties)
94     {
95         this.systemProperties = properties;
96         String JavaDoc value;
97         Map.Entry JavaDoc entry;
98         if (systemProperties != null)
99         {
100             for (Iterator JavaDoc iterator = systemProperties.entrySet().iterator(); iterator.hasNext();)
101             {
102                 entry = (Map.Entry JavaDoc)iterator.next();
103                 value = entry.getValue().toString();
104                 value = templateParser.parse(systemProperties, value);
105                 value = templateParser.parse(MuleManager.getInstance().getProperties(), value);
106                 System.setProperty(entry.getKey().toString(), value);
107             }
108         }
109
110         if (loadSystemProperties)
111         {
112             Map JavaDoc props = System.getProperties();
113
114             for (Iterator JavaDoc iterator = props.entrySet().iterator(); iterator.hasNext();)
115             {
116                 entry = (Map.Entry JavaDoc)iterator.next();
117                 value = entry.getValue().toString();
118                 value = templateParser.parse(MuleManager.getInstance().getProperties(), value.toString());
119                 MuleManager.getInstance().setProperty(entry.getKey(), value);
120             }
121         }
122     }
123
124     public Map JavaDoc getProperties()
125     {
126         return properties;
127     }
128
129     public void setProperties(Map JavaDoc properties)
130     {
131         this.properties = properties;
132         if (properties != null)
133         {
134             Map.Entry JavaDoc entry;
135             String JavaDoc value;
136             for (Iterator JavaDoc iterator = properties.entrySet().iterator(); iterator.hasNext();)
137             {
138                 entry = (Map.Entry JavaDoc)iterator.next();
139                 value = entry.getValue().toString();
140                 value = templateParser.parse(MuleManager.getInstance().getProperties(), value);
141                 MuleManager.getInstance().setProperty(entry.getKey(), value);
142             }
143         }
144     }
145 }
146
Popular Tags