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 java.lang.reflect.Field ; 41 import java.util.logging.Level ; 42 import java.util.logging.Logger ; 43 44 45 public class JndiWebServiceFieldInjectProgram extends BuilderProgram { 46 private static final Logger log 47 = Logger.getLogger(JndiWebServiceFieldInjectProgram.class.getName()); 48 private static final L10N L 49 = new L10N(JndiWebServiceFieldInjectProgram.class); 50 51 private String _jndiName; 52 private Class _type; 53 private Field _field; 54 55 JndiWebServiceFieldInjectProgram(String jndiName, 56 Class type, 57 Field field) 58 { 59 _jndiName = jndiName; 60 _type = type; 61 _field = field; 62 } 63 64 public void configureImpl(NodeBuilder builder, Object bean) 65 throws ConfigException 66 { 67 try { 68 Object value = new InitialContext ().lookupLink(_jndiName); 69 70 if (value == null) 71 return; 72 73 if (value instanceof WebServiceClient) { 74 WebServiceClient client = (WebServiceClient) value; 75 76 value = client.createProxy(_type); 77 } 78 79 if (! _field.getType().isAssignableFrom(value.getClass())) { 80 throw new ConfigException(L.l("Resource at '{0}' of type {1} is not assignable to field '{2}' of type {3}.", 81 _jndiName, 82 value.getClass().getName(), 83 _field.getName(), 84 _field.getType().getName())); 85 } 86 87 _field.setAccessible(true); 88 _field.set(bean, value); 89 } catch (RuntimeException e) { 90 throw e; 91 } catch (NamingException e) { 92 log.finer(String.valueOf(e)); 93 log.log(Level.FINEST, e.toString(), e); 94 } catch (Exception e) { 95 throw new ConfigException(e); 96 } 97 } 98 99 public Object configure(NodeBuilder builder, Class type) 100 throws ConfigException 101 { 102 throw new UnsupportedOperationException (getClass().getName()); 103 } 104 } 105 | Popular Tags |