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.systest.handlers;
019:
020: import java.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.List;
023: import java.util.Map;
024: import java.util.logging.Logger;
025:
026: import javax.xml.ws.handler.MessageContext;
027:
028: import org.apache.cxf.helpers.CastUtils;
029: import org.apache.handler_test.PingException;
030:
031: /**
032: * Describe class TestHandlerBase here.
033: *
034: *
035: * Created: Fri Oct 21 14:02:50 2005
036: *
037: * @author <a href="mailto:codea@iona.com">codea</a>
038: * @version 1.0
039: */
040: public abstract class TestHandlerBase {
041:
042: private static final Logger LOG = Logger
043: .getLogger(TestHandlerBase.class.getName());
044:
045: private static int sid;
046: private static int sinvokedOrder;
047:
048: private boolean handleMessageRet = true;
049:
050: private int invokeOrderOfHandleMessage;
051: private int invokeOrderOfHandleFault;
052: private int invokeOrderOfClose;
053:
054: private Map<String, Integer> methodCallCount = new HashMap<String, Integer>();
055: private final int id;
056: private final boolean isServerSideHandler;
057:
058: public TestHandlerBase(boolean serverSide) {
059: id = ++sid;
060: isServerSideHandler = serverSide;
061: }
062:
063: protected void methodCalled(String methodName) {
064: int val = 0;
065: if (methodCallCount.keySet().contains(methodName)) {
066: val = methodCallCount.get(methodName);
067: }
068: if ("handleMessage".equals(methodName)) {
069: invokeOrderOfHandleMessage = ++sinvokedOrder;
070: } else if ("handleFault".equals(methodName)) {
071: invokeOrderOfHandleFault = ++sinvokedOrder;
072: } else if ("close".equals(methodName)) {
073: invokeOrderOfClose = ++sinvokedOrder;
074: }
075:
076: val++;
077: methodCallCount.put(methodName, val);
078: }
079:
080: public int getInvokeOrderOfHandleMessage() {
081: return invokeOrderOfHandleMessage;
082: }
083:
084: public int getInvokeOrderOfHandleFault() {
085: return invokeOrderOfHandleFault;
086: }
087:
088: public int getInvokeOrderOfClose() {
089: return invokeOrderOfClose;
090: }
091:
092: public int getId() {
093: return id;
094: }
095:
096: public abstract String getHandlerId();
097:
098: public boolean isCloseInvoked() {
099:
100: return methodCallCount.containsKey("close");
101: }
102:
103: public boolean isDestroyInvoked() {
104: return methodCallCount.containsKey("destroy");
105: }
106:
107: public boolean isHandleFaultInvoked() {
108: return methodCallCount.containsKey("handleFault");
109: }
110:
111: public int getHandleFaultInvoked() {
112: return getMethodCallCount("handleFault");
113: }
114:
115: public int getCloseInvoked() {
116: return getMethodCallCount("close");
117: }
118:
119: public boolean isHandleMessageInvoked() {
120: return methodCallCount.containsKey("handleMessage");
121: }
122:
123: public int getHandleMessageInvoked() {
124: return getMethodCallCount("handleMessage");
125: }
126:
127: public boolean isInitInvoked() {
128: return methodCallCount.containsKey("init");
129: }
130:
131: public boolean isPostConstructInvoked() {
132: return methodCallCount.containsKey("doPostConstruct");
133: }
134:
135: public void setHandleMessageRet(boolean ret) {
136: handleMessageRet = ret;
137: }
138:
139: public boolean getHandleMessageRet() {
140: return handleMessageRet;
141: }
142:
143: public boolean isServerSideHandler() {
144: return isServerSideHandler;
145: }
146:
147: public void verifyJAXWSProperties(MessageContext ctx)
148: throws PingException {
149: if (isServerSideHandler() && isOutbound(ctx)) {
150: /*
151: QName operationName = (QName)ctx.get(MessageContext.WSDL_OPERATION);
152: if (operationName == null) {
153: throw new PingException("WSDL_OPERATION not found");
154: }
155: URI wsdlDescription = (URI)ctx.get(MessageContext.WSDL_DESCRIPTION);
156: if (!wsdlDescription.toString().equals("http://localhost:9005/HandlerTest/SoapPort?wsdl")) {
157: throw new PingException("WSDL_DESCRIPTION not found");
158: }
159: QName wsdlPort = (QName)ctx.get(MessageContext.WSDL_PORT);
160: if (!wsdlPort.getLocalPart().equals("SoapPort")) {
161: throw new PingException("WSDL_PORT not found");
162: }
163: QName wsdlInterface = (QName)ctx.get(MessageContext.WSDL_INTERFACE);
164: if (!wsdlInterface.getLocalPart().equals("HandlerTest")) {
165: throw new PingException("WSDL_INTERFACE not found");
166: }
167: QName wsdlService = (QName)ctx.get(MessageContext.WSDL_SERVICE);
168: if (!wsdlService.getLocalPart().equals("HandlerTestService")) {
169: throw new PingException("WSDL_SERVICE not found");
170: }
171: */
172: }
173: }
174:
175: protected void printHandlerInfo(String methodName, boolean outbound) {
176: String info = getHandlerId() + " "
177: + (outbound ? "outbound" : "inbound") + " "
178: + methodName + " " + Thread.currentThread().getName();
179: LOG.info(info);
180: }
181:
182: protected List<String> getHandlerInfoList(MessageContext ctx) {
183: List<String> handlerInfoList = null;
184: if (ctx.containsKey("handler.info")) {
185: handlerInfoList = CastUtils.cast((List) ctx
186: .get("handler.info"));
187: } else {
188: handlerInfoList = new ArrayList<String>();
189: ctx.put("handler.info", handlerInfoList);
190: ctx.setScope("handler.info",
191: MessageContext.Scope.APPLICATION);
192: }
193: return handlerInfoList;
194: }
195:
196: protected boolean isOutbound(MessageContext ctx) {
197: return (Boolean) ctx
198: .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
199: }
200:
201: private int getMethodCallCount(String methodName) {
202: int ret = 0;
203: if (methodCallCount.containsKey(methodName)) {
204: ret = methodCallCount.get(methodName);
205: }
206: return ret;
207: }
208:
209: }
|