001: /*
002: * Copyright (c) 1998-2007 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 Emil Ong
028: */
029:
030: package com.caucho.soa.client;
031:
032: import com.caucho.config.ConfigException;
033: import com.caucho.naming.Jndi;
034: import com.caucho.naming.ObjectProxy;
035: import com.caucho.util.L10N;
036: import com.caucho.webbeans.cfg.AbstractBeanConfig;
037:
038: import javax.annotation.PostConstruct;
039: import javax.naming.NamingException;
040: import javax.xml.namespace.QName;
041: import java.lang.reflect.Constructor;
042: import java.net.URL;
043: import java.util.ArrayList;
044: import java.util.Hashtable;
045: import java.util.logging.Logger;
046:
047: /**
048: */
049: public class WebServiceClient extends AbstractBeanConfig implements
050: ObjectProxy, java.io.Serializable {
051: private static final Logger log = Logger
052: .getLogger(WebServiceClient.class.getName());
053: private static final L10N L = new L10N(WebServiceClient.class);
054:
055: private Class _serviceClass;
056: private String _url;
057:
058: private ArrayList<Class> _jaxbClasses = null;
059: private StringBuilder _jaxbPackages = null;
060:
061: public void setClass(Class serviceClass) {
062: setInterface(serviceClass);
063: }
064:
065: public void setInterface(Class serviceClass) {
066: _serviceClass = serviceClass;
067: }
068:
069: public void setUrl(String url) {
070: _url = url;
071: }
072:
073: public void addJaxbClass(Class jaxbClass) throws ConfigException {
074: if (_jaxbPackages != null) {
075: throw new ConfigException(
076: L
077: .l("cannot set <jaxb-class> and <jaxb-package> simultaneously"));
078: }
079:
080: if (_jaxbClasses == null)
081: _jaxbClasses = new ArrayList<Class>();
082:
083: _jaxbClasses.add(jaxbClass);
084: }
085:
086: public void addJaxbPackage(String jaxbPackage)
087: throws ConfigException {
088: if (_jaxbClasses != null) {
089: throw new ConfigException(
090: L
091: .l("cannot set <jaxb-class> and <jaxb-package> simultaneously"));
092: }
093:
094: if (_jaxbPackages == null)
095: _jaxbPackages = new StringBuilder();
096: else
097: _jaxbPackages.append(':');
098:
099: _jaxbPackages.append(jaxbPackage);
100: }
101:
102: /**
103: * Creates the object from the proxy.
104: *
105: * @param env the calling environment
106: *
107: * @return the object named by the proxy.
108: */
109: public Object createObject(Hashtable env) throws NamingException {
110: try {
111: Object proxy = null;
112:
113: if (_jaxbClasses != null) {
114: Class[] jaxbClasses = new Class[_jaxbClasses.size()];
115: _jaxbClasses.toArray(jaxbClasses);
116: proxy = ProxyManager.getWebServiceProxy(_serviceClass,
117: _url, jaxbClasses);
118: } else if (_jaxbPackages != null) {
119: String jaxbPackages = _jaxbPackages.toString();
120: proxy = ProxyManager.getWebServiceProxy(_serviceClass,
121: _url, jaxbPackages);
122: } else {
123: proxy = ProxyManager.getWebServiceProxy(_serviceClass,
124: _url);
125: }
126:
127: return proxy;
128: } catch (RuntimeException e) {
129: throw e;
130: } catch (Exception e) {
131: throw new RuntimeException(e);
132: }
133: }
134:
135: /**
136: * Creates the object from the proxy.
137: *
138: * @param env the calling environment
139: *
140: * @return the object named by the proxy.
141: */
142: public Object createService(Constructor ctor)
143: throws ConfigException {
144: try {
145: int p = _url.indexOf(':');
146:
147: String urlName = _url.substring(p + 1);
148:
149: URL url = new URL(urlName);
150: String action = "dummy-action";
151:
152: QName name = new QName(urlName, "dummy-action");
153:
154: return ctor.newInstance(url, name);
155: } catch (RuntimeException e) {
156: throw e;
157: } catch (Exception e) {
158: throw ConfigException.create(e);
159: }
160: }
161:
162: /**
163: * Creates the object from the proxy.
164: *
165: * @param env the calling environment
166: *
167: * @return the object named by the proxy.
168: */
169: public Object createProxy(Class api) throws NamingException {
170: try {
171: Object proxy = null;
172:
173: if (_jaxbClasses != null) {
174: Class[] jaxbClasses = _jaxbClasses
175: .toArray(new Class[0]);
176: proxy = ProxyManager.getWebServiceProxy(api, _url,
177: jaxbClasses);
178: } else if (_jaxbPackages != null) {
179: String jaxbPackages = _jaxbPackages.toString();
180: proxy = ProxyManager.getWebServiceProxy(api, _url,
181: jaxbPackages);
182: } else {
183: proxy = ProxyManager.getWebServiceProxy(api, _url);
184: }
185:
186: return proxy;
187: } catch (RuntimeException e) {
188: throw e;
189: } catch (Exception e) {
190: throw new RuntimeException(e);
191: }
192: }
193:
194: @PostConstruct
195: public void init() throws Throwable {
196: register(createObject(new Hashtable()), _serviceClass);
197: }
198: }
|