KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > ws > axis > JServiceProxy


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JServiceProxy.java,v 1.3 2004/12/06 12:32:29 sauthieg Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas.ws.axis;
26
27 import java.lang.reflect.InvocationHandler JavaDoc;
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import javax.xml.namespace.QName JavaDoc;
33 import javax.xml.rpc.Service JavaDoc;
34 import javax.xml.rpc.ServiceException JavaDoc;
35
36
37 /**
38  * <code>JServiceProxy</code>
39  *
40  * @author Guillaume Sauthier
41  */

42 public class JServiceProxy implements InvocationHandler JavaDoc {
43
44     /**
45      * inner JService implementation.
46      * used for delegation
47      */

48     private JService service = null;
49
50     /**
51      * Forbidden method : Service.getTypeMappingRegistry()
52      */

53     private static Method JavaDoc getTypeMappingRegistryMethod = null;
54
55     /**
56      * Forbidden method : Service.getHandlerRegistry()
57      */

58     private static Method JavaDoc getHandlerRegistryMethod = null;
59
60     /**
61      * adding behavior to method : Service.getPort(QName, Class)
62      */

63     private static Method JavaDoc getPortQNameClass = null;
64
65     /**
66      * Constructs a new JServiceProxy wrapping given JService instance.
67      * @param service the wrapped JService instance
68      * @throws ServiceException should be never thrown
69      */

70     public JServiceProxy(JService service) throws ServiceException JavaDoc {
71         this.service = service;
72         try {
73             getTypeMappingRegistryMethod = Service JavaDoc.class.getDeclaredMethod("getTypeMappingRegistry", new Class JavaDoc[]{});
74             getHandlerRegistryMethod = Service JavaDoc.class.getDeclaredMethod("getHandlerRegistry", new Class JavaDoc[]{});
75             getPortQNameClass = Service JavaDoc.class.getDeclaredMethod("getPort", new Class JavaDoc[]{QName JavaDoc.class, Class JavaDoc.class});
76         } catch (Exception JavaDoc e) {
77             throw new ServiceException JavaDoc(e);
78         }
79     }
80
81     /**
82      * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
83      */

84     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
85         // avoid getHandlerRegistry method call
86
if (getHandlerRegistryMethod.equals(method)) {
87             throw new UnsupportedOperationException JavaDoc("J2EE components shouldn't use getHandlerRegistry method");
88         }
89         // avoid getTypeMappingRegistry method call
90
if (getTypeMappingRegistryMethod.equals(method)) {
91             throw new UnsupportedOperationException JavaDoc("J2EE components shouldn't use getTypeMappingRegistry method");
92         }
93         // avoid getPort method call
94
if (getPortQNameClass.equals(method)) {
95             return getPort(args);
96         }
97         //delegate
98
try {
99             return method.invoke(service, args);
100         } catch (InvocationTargetException JavaDoc ite) {
101             throw ite.getTargetException();
102         }
103     }
104
105     /**
106      * @param args Method call arguments
107      * @return Returns the correct Port
108      * @throws ServiceException if port's QName is an unknown Port (not defined in WSDL).
109      */

110     private Object JavaDoc getPort(Object JavaDoc[] args) throws ServiceException JavaDoc {
111         QName JavaDoc name = (QName JavaDoc) args[0];
112         Class JavaDoc clazz = (Class JavaDoc) args[1];
113         boolean portFound = false;
114         for (Iterator JavaDoc ports = service.getPorts(); ports.hasNext() && !portFound;) {
115             QName JavaDoc portName = (QName JavaDoc) ports.next();
116             if (portName.equals(name)) {
117                 return service.getPort(name, clazz);
118             }
119         }
120
121         // if we come here, no ports have been found,
122
// so we fail with a ServiceException
123
throw new ServiceException JavaDoc("Unknown Port : " + name);
124     }
125
126 }
127
Popular Tags