1 22 package org.jboss.injection; 23 24 import org.jboss.logging.Logger; 25 import org.jboss.naming.Util; 26 27 33 public class EnvEntryEncInjector implements EncInjector 34 { 35 private static final Logger log = Logger.getLogger(EnvEntryEncInjector.class); 36 37 private String name; 38 private String entryType; 39 private String value; 40 41 42 public EnvEntryEncInjector(String encName, String entryType, String value) 43 { 44 this.name = encName; 45 this.entryType = entryType; 46 this.value = value; 47 } 48 49 public void inject(InjectionContainer container) 50 { 51 try 52 { 53 Util.rebind(container.getEnc(), 54 name, 55 getEnvEntryValue()); 56 } 57 catch (Exception e) 58 { 59 throw new RuntimeException ("Invalid <env-entry> name: " + name, e); 60 } 61 } 62 63 64 protected Object getEnvEntryValue() throws ClassNotFoundException 65 { 66 Class type = Thread.currentThread().getContextClassLoader().loadClass(entryType); 67 if (type == String .class) 68 { 69 return value; 70 } 71 else if (type == Integer .class) 72 { 73 return new Integer (value); 74 } 75 else if (type == Long .class) 76 { 77 return new Long (value); 78 } 79 else if (type == Double .class) 80 { 81 return new Double (value); 82 } 83 else if (type == Float .class) 84 { 85 return new Float (value); 86 } 87 else if (type == Byte .class) 88 { 89 return new Byte (value); 90 } 91 else if (type == Character .class) 92 { 93 String input = value; 94 if (input == null || input.length() == 0) 95 { 96 return new Character ((char) 0); 97 } 98 else 99 { 100 if (input.length() > 1) 101 log.warn("Warning character env-entry is too long: binding=" 103 + name + " value=" + input); 104 return new Character (input.charAt(0)); 105 } 106 } 107 else if (type == Short .class) 108 { 109 return new Short (value); 110 } 111 else if (type == Boolean .class) 112 { 113 return new Boolean (value); 114 } 115 else 116 { 117 return value; 118 } 119 } 120 } 121 | Popular Tags |