001: /*
002: * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.soap.jaxws;
031:
032: import com.caucho.soap.skeleton.Skeleton;
033: import com.caucho.util.L10N;
034:
035: import javax.xml.ws.Binding;
036: import javax.xml.ws.BindingProvider;
037: import javax.xml.ws.handler.Handler;
038:
039: import java.io.*;
040: import java.lang.reflect.InvocationHandler;
041: import java.lang.reflect.Method;
042: import java.net.ProtocolException;
043: import java.util.HashMap;
044: import java.util.List;
045: import java.util.Map;
046: import java.util.logging.Logger;
047:
048: /**
049: * Port handler
050: */
051: public class PortProxyHandler implements InvocationHandler,
052: BindingProvider {
053: private final static Logger log = Logger
054: .getLogger(PortProxyHandler.class.getName());
055: private final static L10N L = new L10N(PortProxyHandler.class);
056:
057: private final static HashMap<Method, SpecialMethod> _specialMethods = new HashMap<Method, SpecialMethod>();
058:
059: private final Skeleton _skeleton;
060:
061: private final HashMap<String, Object> _requestContext = new HashMap<String, Object>();
062:
063: private final HashMap<String, Object> _responseContext = new HashMap<String, Object>();
064:
065: private final Binding _binding;
066:
067: public PortProxyHandler(Skeleton skeleton, String endpointAddress,
068: Binding binding) {
069: _skeleton = skeleton;
070: _requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
071: endpointAddress);
072: _binding = binding;
073: }
074:
075: public Binding getBinding() {
076: return _binding;
077: }
078:
079: public Map<String, Object> getRequestContext() {
080: return _requestContext;
081: }
082:
083: public Map<String, Object> getResponseContext() {
084: return _responseContext;
085: }
086:
087: public Object invoke(Object proxy, Method method, Object[] args)
088: throws Throwable {
089: SpecialMethod specialMethod = _specialMethods.get(method);
090:
091: if (specialMethod != null) {
092: switch (specialMethod) {
093: case TO_STRING:
094: return "PortProxyHandler[]";
095: case EQUALS:
096: return false;
097: case HASH_CODE:
098: return System.identityHashCode(this );
099:
100: case GET_BINDING:
101: return _binding;
102:
103: case GET_REQUEST_CONTEXT:
104: return _requestContext;
105:
106: case GET_RESPONSE_CONTEXT:
107: return _responseContext;
108: }
109: }
110:
111: Object url = _requestContext
112: .get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY);
113:
114: if (url == null)
115: throw new ProtocolException(
116: "No service endpoint address specified");
117:
118: if (!(url instanceof String))
119: throw new IllegalArgumentException(
120: "Invalid service endpoint address specified");
121:
122: // XXX cache this and the HandlerChainInvoker
123: List<Handler> chain = _binding.getHandlerChain();
124:
125: if (chain == null || chain.size() == 0)
126: return _skeleton.invoke(method, (String) url, args);
127: else
128: return _skeleton.invoke(method, (String) url, args,
129: new HandlerChainInvoker(chain, this ));
130: }
131:
132: static {
133: try {
134: _specialMethods.put(Object.class.getMethod("toString",
135: new Class[0]), SpecialMethod.TO_STRING);
136:
137: _specialMethods
138: .put(Object.class.getMethod("equals",
139: new Class[] { Object.class }),
140: SpecialMethod.EQUALS);
141:
142: _specialMethods.put(Object.class.getMethod("hashCode",
143: new Class[0]), SpecialMethod.HASH_CODE);
144:
145: _specialMethods.put(BindingProvider.class.getMethod(
146: "getBinding", new Class[0]),
147: SpecialMethod.GET_BINDING);
148:
149: _specialMethods.put(BindingProvider.class.getMethod(
150: "getRequestContext", new Class[0]),
151: SpecialMethod.GET_REQUEST_CONTEXT);
152:
153: _specialMethods.put(BindingProvider.class.getMethod(
154: "getResponseContext", new Class[0]),
155: SpecialMethod.GET_RESPONSE_CONTEXT);
156: } catch (Exception e) {
157: e.printStackTrace();
158: }
159: }
160:
161: enum SpecialMethod {
162: TO_STRING, EQUALS, HASH_CODE,
163:
164: GET_BINDING, GET_REQUEST_CONTEXT, GET_RESPONSE_CONTEXT
165: };
166: }
|