KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > soa > client > WebServiceClient


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Emil Ong
28  */

29
30 package com.caucho.soa.client;
31
32 import com.caucho.config.ConfigException;
33 import com.caucho.naming.Jndi;
34 import com.caucho.naming.ObjectProxy;
35 import com.caucho.util.L10N;
36
37 import javax.annotation.PostConstruct;
38 import javax.naming.NamingException JavaDoc;
39 import javax.xml.namespace.QName JavaDoc;
40 import java.lang.reflect.Constructor JavaDoc;
41 import java.net.URL JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.Hashtable JavaDoc;
44 import java.util.logging.Logger JavaDoc;
45
46 /**
47  */

48 public class WebServiceClient implements ObjectProxy, java.io.Serializable JavaDoc {
49   private static final Logger JavaDoc log
50     = Logger.getLogger(WebServiceClient.class.getName());
51   private static final L10N L = new L10N(WebServiceClient.class);
52
53   private Class JavaDoc _serviceClass;
54   private String JavaDoc _jndiName;
55   private String JavaDoc _url;
56
57   private ArrayList JavaDoc<Class JavaDoc> _jaxbClasses = null;
58   private StringBuilder JavaDoc _jaxbPackages = null;
59
60   public void setJndiName(String JavaDoc jndiName)
61   {
62     _jndiName = jndiName;
63   }
64
65   public void setInterface(Class JavaDoc serviceClass)
66   {
67     _serviceClass = serviceClass;
68   }
69
70   public void setUrl(String JavaDoc url)
71   {
72     _url = url;
73   }
74
75   public void addJaxbClass(Class JavaDoc jaxbClass)
76     throws ConfigException
77   {
78     if (_jaxbPackages != null) {
79       throw new ConfigException(L.l("cannot set <jaxb-class> and <jaxb-package> simultaneously"));
80     }
81
82     if (_jaxbClasses == null)
83       _jaxbClasses = new ArrayList JavaDoc<Class JavaDoc>();
84
85     _jaxbClasses.add(jaxbClass);
86   }
87
88   public void addJaxbPackage(String JavaDoc jaxbPackage)
89     throws ConfigException
90   {
91     if (_jaxbClasses != null) {
92       throw new ConfigException(L.l("cannot set <jaxb-class> and <jaxb-package> simultaneously"));
93     }
94
95     if (_jaxbPackages == null)
96       _jaxbPackages = new StringBuilder JavaDoc();
97     else
98       _jaxbPackages.append(':');
99
100     _jaxbPackages.append(jaxbPackage);
101   }
102
103   /**
104    * Creates the object from the proxy.
105    *
106    * @param env the calling environment
107    *
108    * @return the object named by the proxy.
109    */

110   public Object JavaDoc createObject(Hashtable JavaDoc env)
111     throws NamingException JavaDoc
112   {
113     try {
114       Object JavaDoc proxy = null;
115
116       if (_jaxbClasses != null) {
117     Class JavaDoc[] jaxbClasses = _jaxbClasses.toArray(new Class JavaDoc[0]);
118     proxy = ProxyManager.getWebServiceProxy(_serviceClass, _url, jaxbClasses);
119       }
120       else if (_jaxbPackages != null) {
121     String JavaDoc jaxbPackages = _jaxbPackages.toString();
122     proxy =
123       ProxyManager.getWebServiceProxy(_serviceClass, _url, jaxbPackages);
124       }
125       else {
126     proxy = ProxyManager.getWebServiceProxy(_serviceClass, _url);
127       }
128
129       return proxy;
130     } catch (RuntimeException JavaDoc e) {
131       throw e;
132     } catch (Exception JavaDoc e) {
133       throw new RuntimeException JavaDoc(e);
134     }
135   }
136
137   /**
138    * Creates the object from the proxy.
139    *
140    * @param env the calling environment
141    *
142    * @return the object named by the proxy.
143    */

144   public Object JavaDoc createService(Constructor JavaDoc ctor)
145     throws ConfigException
146   {
147     try {
148       int p = _url.indexOf(':');
149
150       String JavaDoc urlName = _url.substring(p + 1);
151       
152       URL JavaDoc url = new URL JavaDoc(urlName);
153       String JavaDoc action = "dummy-action";
154       
155       QName JavaDoc name = new QName JavaDoc(urlName, "dummy-action");
156
157       return ctor.newInstance(url, name);
158     } catch (RuntimeException JavaDoc e) {
159       throw e;
160     } catch (Exception JavaDoc e) {
161       throw new ConfigException(e);
162     }
163   }
164
165   /**
166    * Creates the object from the proxy.
167    *
168    * @param env the calling environment
169    *
170    * @return the object named by the proxy.
171    */

172   public Object JavaDoc createProxy(Class JavaDoc api)
173     throws NamingException JavaDoc
174   {
175     try {
176       Object JavaDoc proxy = null;
177
178       if (_jaxbClasses != null) {
179     Class JavaDoc[] jaxbClasses = _jaxbClasses.toArray(new Class JavaDoc[0]);
180     proxy = ProxyManager.getWebServiceProxy(api, _url, jaxbClasses);
181       }
182       else if (_jaxbPackages != null) {
183     String JavaDoc jaxbPackages = _jaxbPackages.toString();
184     proxy =
185       ProxyManager.getWebServiceProxy(api, _url, jaxbPackages);
186       }
187       else {
188     proxy = ProxyManager.getWebServiceProxy(api, _url);
189       }
190
191       return proxy;
192     } catch (RuntimeException JavaDoc e) {
193       throw e;
194     } catch (Exception JavaDoc e) {
195       throw new RuntimeException JavaDoc(e);
196     }
197   }
198
199   @PostConstruct
200   public void init()
201     throws Throwable JavaDoc
202   {
203     if (_jndiName == null)
204       throw new ConfigException(L.l("jndi-name not set for <web-service-client>"));
205
206     /*
207     if (_serviceClass == null)
208       throw new ConfigException("interface not set for <web-service-client>");
209     */

210
211     Jndi.bindDeepShort(_jndiName, this);
212   }
213 }
214
215
Popular Tags