1 29 30 package com.caucho.config.j2ee; 31 32 import com.caucho.config.BuilderProgram; 33 import com.caucho.config.ConfigException; 34 import com.caucho.config.NodeBuilder; 35 import com.caucho.soa.client.WebServiceClient; 36 import com.caucho.util.L10N; 37 38 import javax.naming.InitialContext ; 39 import javax.naming.NamingException ; 40 import javax.xml.namespace.QName ; 41 import javax.xml.ws.Service; 42 import java.lang.reflect.Constructor ; 43 import java.net.URL ; 44 import java.util.logging.Level ; 45 import java.util.logging.Logger ; 46 47 48 public class ServiceInjectProgram extends BuilderProgram { 49 private static final Logger log 50 = Logger.getLogger(ServiceInjectProgram.class.getName()); 51 private static final L10N L 52 = new L10N(ServiceInjectProgram.class); 53 54 private String _jndiName; 55 private Class _type; 56 private Constructor _constructor; 57 private AccessibleInject _field; 58 59 ServiceInjectProgram(String jndiName, 60 Class type, 61 AccessibleInject field) 62 throws ConfigException 63 { 64 if (! Service.class.isAssignableFrom(type)) 65 throw new ConfigException(L.l("'{0}' needs to extend javax.xml.ws.Service.", 66 type.getName())); 67 68 try { 69 _jndiName = jndiName; 70 71 _type = type; 72 73 _constructor = type.getConstructor(new Class [] { URL .class, 74 QName .class }); 75 76 _field = field; 77 } catch (RuntimeException e) { 78 throw e; 79 } catch (Exception e) { 80 throw new ConfigException(e); 81 } 82 } 83 84 public void configureImpl(NodeBuilder builder, Object bean) 85 throws ConfigException 86 { 87 try { 88 Object value = new InitialContext ().lookupLink(_jndiName); 89 90 if (value == null) 91 return; 92 93 if (value instanceof WebServiceClient) { 94 WebServiceClient client = (WebServiceClient) value; 95 96 value = client.createService(_constructor); 97 } 98 99 if (! _field.getType().isAssignableFrom(value.getClass())) { 100 throw new ConfigException(L.l("Resource at '{0}' of type {1} is not assignable to field '{2}' of type {3}.", 101 _jndiName, 102 value.getClass().getName(), 103 _field.getName(), 104 _field.getType().getName())); 105 } 106 107 _field.inject(bean, value); 108 } catch (RuntimeException e) { 109 throw e; 110 } catch (NamingException e) { 111 log.finer(String.valueOf(e)); 112 log.log(Level.FINEST, e.toString(), e); 113 } catch (Exception e) { 114 throw new ConfigException(e); 115 } 116 } 117 118 public Object configure(NodeBuilder builder, Class type) 119 throws ConfigException 120 { 121 throw new UnsupportedOperationException (getClass().getName()); 122 } 123 } 124 | Popular Tags |