001: /*
002: * $Id: MessagePropertiesContext.java 10489 2008-01-23 17:53:38Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010: package org.mule.transport;
011:
012: import org.mule.api.transport.PropertyScope;
013: import org.mule.util.MapUtils;
014: import org.mule.util.ObjectUtils;
015:
016: import java.io.Serializable;
017: import java.util.Collections;
018: import java.util.HashMap;
019: import java.util.Iterator;
020: import java.util.Map;
021: import java.util.Set;
022: import java.util.TreeMap;
023: import java.util.TreeSet;
024:
025: import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
026:
027: /** TODO */
028: public class MessagePropertiesContext implements Serializable {
029: protected Map scopedMap;
030: protected Set keySet;
031:
032: //TODO RM*: Map these to a RegistryMapView, currently in another branch :(
033: //Treat Application properites as a special call
034: Map applicationProperties = new ConcurrentHashMap(0);
035:
036: private PropertyScope defaultScope = PropertyScope.OUTBOUND;
037:
038: public MessagePropertiesContext() {
039: keySet = new TreeSet();
040: scopedMap = new TreeMap(new PropertyScope.ScopeComarator());
041:
042: scopedMap.put(PropertyScope.INVOCATION, new HashMap(6));
043: scopedMap.put(PropertyScope.INBOUND, new HashMap(6));
044: scopedMap.put(PropertyScope.OUTBOUND, new HashMap(6));
045: scopedMap.put(PropertyScope.SESSION, new HashMap(6));
046:
047: }
048:
049: public MessagePropertiesContext(PropertyScope defaultScope) {
050: this ();
051: //We can't set a read only scope as default
052: checkScopeForWriteAccess(defaultScope);
053: this .defaultScope = defaultScope;
054: }
055:
056: /**
057: * if a property is not available in any ther scope, should we check the registry.
058: * Note there will be performance implementations is this is enabled
059: */
060: private boolean fallbackToRegistry = false;
061:
062: protected Map getScopedProperties(PropertyScope scope) {
063: Map map = (Map) scopedMap.get(scope);
064: if (map == null) {
065: throw new IllegalArgumentException("Scope not registered: "
066: + scope);
067: }
068: return map;
069: }
070:
071: void registerInvocationProperties(Map properties) {
072: if (properties != null) {
073: getScopedProperties(PropertyScope.INVOCATION).putAll(
074: properties);
075: keySet.addAll(properties.keySet());
076: }
077: }
078:
079: void addInboundProperties(Map properties) {
080: if (properties != null) {
081: getScopedProperties(PropertyScope.INBOUND).putAll(
082: properties);
083: keySet.addAll(properties.keySet());
084: }
085: }
086:
087: void registerSessionProperties(Map properties) {
088: if (properties != null) {
089: getScopedProperties(PropertyScope.SESSION).putAll(
090: properties);
091: keySet.addAll(properties.keySet());
092: }
093: }
094:
095: public Object getProperty(String key) {
096: Object value = null;
097: for (Iterator iterator = scopedMap.values().iterator(); iterator
098: .hasNext();) {
099: Map props = (Map) iterator.next();
100: value = props.get(key);
101: if (value != null) {
102: break;
103: }
104: }
105: if (value == null && fallbackToRegistry) {
106: value = applicationProperties.get(key);
107: }
108: return value;
109: }
110:
111: public Object getProperty(String key, PropertyScope scope) {
112: if (PropertyScope.APPLICATION.equals(scope)) {
113: return applicationProperties.get(key);
114: }
115:
116: Map props = getScopedProperties(scope);
117: return props.get(key);
118: }
119:
120: public void clearProperties() {
121: Map props = getScopedProperties(PropertyScope.INVOCATION);
122: keySet.removeAll(props.keySet());
123: props.clear();
124: props = getScopedProperties(PropertyScope.OUTBOUND);
125: keySet.removeAll(props.keySet());
126: props.clear();
127: props = getScopedProperties(PropertyScope.SESSION);
128: keySet.removeAll(props.keySet());
129: props.clear();
130: //inbound are read Only
131: }
132:
133: public void clearProperties(PropertyScope scope) {
134: checkScopeForWriteAccess(scope);
135: Map props = getScopedProperties(scope);
136: keySet.removeAll(props.keySet());
137: props.clear();
138: }
139:
140: /**
141: * Removes a property on this message
142: *
143: * @param key the property key to remove
144: * @return the removed property value or null if the property did not exist
145: */
146: public Object removeProperty(String key) {
147: Object value = getScopedProperties(PropertyScope.INVOCATION)
148: .remove(key);
149: if (value == null) {
150: value = getScopedProperties(PropertyScope.OUTBOUND).remove(
151: key);
152: }
153: if (value == null) {
154: value = getScopedProperties(PropertyScope.SESSION).remove(
155: key);
156: }
157: if (value != null) {
158: keySet.remove(key);
159: }
160: return value;
161: }
162:
163: /**
164: * Set a property on the message
165: *
166: * @param key the key on which to associate the value
167: * @param value the property value
168: */
169: public void setProperty(String key, Object value) {
170: getScopedProperties(defaultScope).put(key, value);
171: keySet.add(key);
172: }
173:
174: /**
175: * Set a property on the message
176: *
177: * @param key the key on which to associate the value
178: * @param value the property value
179: * @param scope the scope to se the property on
180: * @see org.mule.api.transport.PropertyScope
181: */
182: public void setProperty(String key, Object value,
183: PropertyScope scope) {
184: checkScopeForWriteAccess(scope);
185: getScopedProperties(scope).put(key, value);
186: keySet.add(key);
187: }
188:
189: /** @return all property keys on this message */
190: public Set getPropertyNames() {
191: return Collections.unmodifiableSet(keySet);
192: }
193:
194: /** @return all property keys on this message for the given scope */
195: public Set getPropertyNames(PropertyScope scope) {
196: return Collections.unmodifiableSet(getScopedProperties(scope)
197: .keySet());
198: }
199:
200: protected void checkScopeForWriteAccess(PropertyScope scope) {
201: if (scope == null || PropertyScope.INBOUND.equals(scope)
202: || PropertyScope.APPLICATION.equals(scope)) {
203: throw new IllegalArgumentException(
204: "Scope is invalid for writing properties: " + scope);
205: }
206: }
207:
208: public Object getProperty(String key, Object defaultValue) {
209: Object value = getProperty(key);
210: if (value == null) {
211: value = defaultValue;
212: }
213: return value;
214: }
215:
216: public byte getByteProperty(String name, byte defaultValue) {
217: return ObjectUtils.getByte(getProperty(name), defaultValue);
218: }
219:
220: public short getShortProperty(String name, short defaultValue) {
221: return ObjectUtils.getShort(getProperty(name), defaultValue);
222: }
223:
224: public int getIntProperty(String name, int defaultValue) {
225: return ObjectUtils.getInt(getProperty(name), defaultValue);
226: }
227:
228: public long getLongProperty(String name, long defaultValue) {
229: return ObjectUtils.getLong(getProperty(name), defaultValue);
230: }
231:
232: public float getFloatProperty(String name, float defaultValue) {
233: return ObjectUtils.getFloat(getProperty(name), defaultValue);
234: }
235:
236: public double getDoubleProperty(String name, double defaultValue) {
237: return ObjectUtils.getDouble(getProperty(name), defaultValue);
238: }
239:
240: public boolean getBooleanProperty(String name, boolean defaultValue) {
241: return ObjectUtils.getBoolean(getProperty(name), defaultValue);
242: }
243:
244: public String getStringProperty(String name, String defaultValue) {
245: return ObjectUtils.getString(getProperty(name), defaultValue);
246: }
247:
248: public String toString() {
249: StringBuffer buf = new StringBuffer(128);
250: buf.append("Properites{");
251: for (Iterator iterator = scopedMap.entrySet().iterator(); iterator
252: .hasNext();) {
253: Map.Entry entry = (Map.Entry) iterator.next();
254: buf.append(entry.getKey()).append(":");
255: buf
256: .append(MapUtils.toString((Map) entry.getValue(),
257: false));
258: buf.append(", ");
259: }
260: buf.append("}");
261: return buf.toString();
262: }
263: }
|