001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JServiceProxy.java 5852 2004-12-06 12:32:29Z sauthieg $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.ws.axis;
025:
026: import java.lang.reflect.InvocationHandler;
027: import java.lang.reflect.InvocationTargetException;
028: import java.lang.reflect.Method;
029: import java.util.Iterator;
030:
031: import javax.xml.namespace.QName;
032: import javax.xml.rpc.Service;
033: import javax.xml.rpc.ServiceException;
034:
035: /**
036: * <code>JServiceProxy</code>
037: *
038: * @author Guillaume Sauthier
039: */
040: public class JServiceProxy implements InvocationHandler {
041:
042: /**
043: * inner JService implementation.
044: * used for delegation
045: */
046: private JService service = null;
047:
048: /**
049: * Forbidden method : Service.getTypeMappingRegistry()
050: */
051: private static Method getTypeMappingRegistryMethod = null;
052:
053: /**
054: * Forbidden method : Service.getHandlerRegistry()
055: */
056: private static Method getHandlerRegistryMethod = null;
057:
058: /**
059: * adding behavior to method : Service.getPort(QName, Class)
060: */
061: private static Method getPortQNameClass = null;
062:
063: /**
064: * Constructs a new JServiceProxy wrapping given JService instance.
065: * @param service the wrapped JService instance
066: * @throws ServiceException should be never thrown
067: */
068: public JServiceProxy(JService service) throws ServiceException {
069: this .service = service;
070: try {
071: getTypeMappingRegistryMethod = Service.class
072: .getDeclaredMethod("getTypeMappingRegistry",
073: new Class[] {});
074: getHandlerRegistryMethod = Service.class.getDeclaredMethod(
075: "getHandlerRegistry", new Class[] {});
076: getPortQNameClass = Service.class
077: .getDeclaredMethod("getPort", new Class[] {
078: QName.class, Class.class });
079: } catch (Exception e) {
080: throw new ServiceException(e);
081: }
082: }
083:
084: /**
085: * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
086: */
087: public Object invoke(Object proxy, Method method, Object[] args)
088: throws Throwable {
089: // avoid getHandlerRegistry method call
090: if (getHandlerRegistryMethod.equals(method)) {
091: throw new UnsupportedOperationException(
092: "J2EE components shouldn't use getHandlerRegistry method");
093: }
094: // avoid getTypeMappingRegistry method call
095: if (getTypeMappingRegistryMethod.equals(method)) {
096: throw new UnsupportedOperationException(
097: "J2EE components shouldn't use getTypeMappingRegistry method");
098: }
099: // avoid getPort method call
100: if (getPortQNameClass.equals(method)) {
101: return getPort(args);
102: }
103: //delegate
104: try {
105: return method.invoke(service, args);
106: } catch (InvocationTargetException ite) {
107: throw ite.getTargetException();
108: }
109: }
110:
111: /**
112: * @param args Method call arguments
113: * @return Returns the correct Port
114: * @throws ServiceException if port's QName is an unknown Port (not defined in WSDL).
115: */
116: private Object getPort(Object[] args) throws ServiceException {
117: QName name = (QName) args[0];
118: Class clazz = (Class) args[1];
119: boolean portFound = false;
120: for (Iterator ports = service.getPorts(); ports.hasNext()
121: && !portFound;) {
122: QName portName = (QName) ports.next();
123: if (portName.equals(name)) {
124: return service.getPort(name, clazz);
125: }
126: }
127:
128: // if we come here, no ports have been found,
129: // so we fail with a ServiceException
130: throw new ServiceException("Unknown Port : " + name);
131: }
132:
133: }
|