001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.jaxws.context;
019:
020: import java.util.Collection;
021: import java.util.HashMap;
022: import java.util.Map;
023: import java.util.Set;
024:
025: import javax.xml.ws.handler.MessageContext;
026:
027: import org.apache.cxf.helpers.CastUtils;
028: import org.apache.cxf.message.Exchange;
029: import org.apache.cxf.message.Message;
030:
031: public class WrappedMessageContext implements MessageContext {
032: public static final String SCOPES = WrappedMessageContext.class
033: .getName()
034: + ".SCOPES";
035:
036: private final Map<String, Object> contextMap;
037: private final Message message;
038: private Map<String, Scope> scopes;
039: private Scope defaultScope;
040:
041: public WrappedMessageContext(Message m) {
042: this (m, m, Scope.HANDLER);
043: }
044:
045: public WrappedMessageContext(Message m, Scope defScope) {
046: this (m, m, defScope);
047: }
048:
049: public WrappedMessageContext(Map<String, Object> m, Scope defScope) {
050: this (null, m, defScope);
051: }
052:
053: public WrappedMessageContext(Message m, Map<String, Object> map,
054: Scope defScope) {
055: message = m;
056: contextMap = map;
057: defaultScope = defScope;
058: scopes = CastUtils.cast((Map<?, ?>) contextMap.get(SCOPES));
059: if (scopes == null && message != null
060: && message.getExchange() != null) {
061: if (isRequestor() && !isOutbound()
062: && m.getExchange().getOutMessage() != null) {
063: scopes = CastUtils.cast((Map<?, ?>) m.getExchange()
064: .getOutMessage().get(SCOPES));
065: copyScopedProperties(m.getExchange().getOutMessage());
066: m.put(SCOPES, scopes);
067: } else if (!isRequestor() && isOutbound()
068: && m.getExchange().getInMessage() != null) {
069: scopes = CastUtils.cast((Map<?, ?>) m.getExchange()
070: .getInMessage().get(SCOPES));
071: copyScopedProperties(m.getExchange().getInMessage());
072: m.put(SCOPES, scopes);
073: }
074: }
075: if (scopes == null) {
076: scopes = new HashMap<String, Scope>();
077: contextMap.put(SCOPES, scopes);
078: }
079: }
080:
081: protected final void copyScopedProperties(Message m) {
082: for (String k : scopes.keySet()) {
083: if (!contextMap.containsKey(k)
084: && !MessageContext.MESSAGE_OUTBOUND_PROPERTY
085: .equals(k)) {
086: contextMap.put(k, m.get(k));
087: }
088: }
089: }
090:
091: protected final boolean isRequestor() {
092: return Boolean.TRUE.equals(contextMap
093: .containsKey(Message.REQUESTOR_ROLE));
094: }
095:
096: protected final boolean isOutbound() {
097: Exchange ex = message.getExchange();
098: return message != null
099: && (message == ex.getOutMessage() || message == ex
100: .getOutFaultMessage());
101: }
102:
103: public final Message getWrappedMessage() {
104: return message;
105: }
106:
107: public void clear() {
108: contextMap.clear();
109: }
110:
111: public final boolean containsKey(Object key) {
112: return contextMap.containsKey(key);
113: }
114:
115: public final boolean containsValue(Object value) {
116: return contextMap.containsValue(value);
117: }
118:
119: public final Set<Entry<String, Object>> entrySet() {
120: return contextMap.entrySet();
121: }
122:
123: public final Object get(Object key) {
124: return contextMap.get(key);
125: }
126:
127: public final boolean isEmpty() {
128: return contextMap.isEmpty();
129: }
130:
131: public final Set<String> keySet() {
132: return contextMap.keySet();
133: }
134:
135: public final Object put(String key, Object value) {
136: if (!MessageContext.MESSAGE_OUTBOUND_PROPERTY.equals(key)
137: && !scopes.containsKey(key)) {
138: scopes.put(key, defaultScope);
139: }
140: return contextMap.put(key, value);
141: }
142:
143: public final Object put(String key, Object value, Scope scope) {
144: if (!MessageContext.MESSAGE_OUTBOUND_PROPERTY.equals(key)) {
145: scopes.put(key, scope);
146: }
147: return contextMap.put(key, value);
148: }
149:
150: public final void putAll(Map<? extends String, ? extends Object> t) {
151: for (Map.Entry<? extends String, ? extends Object> s : t
152: .entrySet()) {
153: put(s.getKey(), s.getValue());
154: }
155: }
156:
157: public final Object remove(Object key) {
158: scopes.remove(key);
159: return contextMap.remove(key);
160: }
161:
162: public final int size() {
163: return contextMap.size();
164: }
165:
166: public final Collection<Object> values() {
167: return contextMap.values();
168: }
169:
170: public final void setScope(String key, Scope arg1) {
171: if (!this .containsKey(key)) {
172: throw new IllegalArgumentException("non-existant property-"
173: + key + "is specified");
174: }
175: scopes.put(key, arg1);
176: }
177:
178: public final Scope getScope(String key) {
179: if (containsKey(key)) {
180: if (scopes.containsKey(key)) {
181: return scopes.get(key);
182: } else {
183: return defaultScope;
184: }
185: }
186: throw new IllegalArgumentException("non-existant property-"
187: + key + "is specified");
188: }
189:
190: public final Map<String, Scope> getScopes() {
191: return scopes;
192: }
193:
194: }
|