1 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 ; 39 import javax.xml.namespace.QName ; 40 import java.lang.reflect.Constructor ; 41 import java.net.URL ; 42 import java.util.ArrayList ; 43 import java.util.Hashtable ; 44 import java.util.logging.Logger ; 45 46 48 public class WebServiceClient implements ObjectProxy, java.io.Serializable { 49 private static final Logger log 50 = Logger.getLogger(WebServiceClient.class.getName()); 51 private static final L10N L = new L10N(WebServiceClient.class); 52 53 private Class _serviceClass; 54 private String _jndiName; 55 private String _url; 56 57 private ArrayList <Class > _jaxbClasses = null; 58 private StringBuilder _jaxbPackages = null; 59 60 public void setJndiName(String jndiName) 61 { 62 _jndiName = jndiName; 63 } 64 65 public void setInterface(Class serviceClass) 66 { 67 _serviceClass = serviceClass; 68 } 69 70 public void setUrl(String url) 71 { 72 _url = url; 73 } 74 75 public void addJaxbClass(Class 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 <Class >(); 84 85 _jaxbClasses.add(jaxbClass); 86 } 87 88 public void addJaxbPackage(String 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 (); 97 else 98 _jaxbPackages.append(':'); 99 100 _jaxbPackages.append(jaxbPackage); 101 } 102 103 110 public Object createObject(Hashtable env) 111 throws NamingException 112 { 113 try { 114 Object proxy = null; 115 116 if (_jaxbClasses != null) { 117 Class [] jaxbClasses = _jaxbClasses.toArray(new Class [0]); 118 proxy = ProxyManager.getWebServiceProxy(_serviceClass, _url, jaxbClasses); 119 } 120 else if (_jaxbPackages != null) { 121 String 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 e) { 131 throw e; 132 } catch (Exception e) { 133 throw new RuntimeException (e); 134 } 135 } 136 137 144 public Object createService(Constructor ctor) 145 throws ConfigException 146 { 147 try { 148 int p = _url.indexOf(':'); 149 150 String urlName = _url.substring(p + 1); 151 152 URL url = new URL (urlName); 153 String action = "dummy-action"; 154 155 QName name = new QName (urlName, "dummy-action"); 156 157 return ctor.newInstance(url, name); 158 } catch (RuntimeException e) { 159 throw e; 160 } catch (Exception e) { 161 throw new ConfigException(e); 162 } 163 } 164 165 172 public Object createProxy(Class api) 173 throws NamingException 174 { 175 try { 176 Object proxy = null; 177 178 if (_jaxbClasses != null) { 179 Class [] jaxbClasses = _jaxbClasses.toArray(new Class [0]); 180 proxy = ProxyManager.getWebServiceProxy(api, _url, jaxbClasses); 181 } 182 else if (_jaxbPackages != null) { 183 String 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 e) { 193 throw e; 194 } catch (Exception e) { 195 throw new RuntimeException (e); 196 } 197 } 198 199 @PostConstruct 200 public void init() 201 throws Throwable 202 { 203 if (_jndiName == null) 204 throw new ConfigException(L.l("jndi-name not set for <web-service-client>")); 205 206 210 211 Jndi.bindDeepShort(_jndiName, this); 212 } 213 } 214 215 | Popular Tags |