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