1 29 30 package com.caucho.config.types; 31 32 import com.caucho.config.ConfigException; 33 import com.caucho.config.LineConfigException; 34 import com.caucho.el.Expr; 35 import com.caucho.naming.Jndi; 36 import com.caucho.util.L10N; 37 38 import javax.annotation.PostConstruct; 39 import javax.naming.InitialContext ; 40 import javax.naming.NamingException ; 41 import java.util.logging.Level ; 42 import java.util.logging.Logger ; 43 44 47 public class EnvEntry implements Validator { 48 private static final L10N L = new L10N(EnvEntry.class); 49 private static final Logger log = Logger.getLogger(EnvEntry.class.getName()); 50 51 private String _location = ""; 52 53 private String _name; 54 private Class _type; 55 private String _value; 56 private String _description; 57 58 public void setId(String id) 59 { 60 } 61 62 65 public void setDescription(String description) 66 { 67 _description = description; 68 } 69 70 73 public void setConfigLocation(String filename, int line) 74 { 75 _location = filename + ":" + line + " "; 76 } 77 78 81 public void setEnvEntryName(String name) 82 { 83 _name = name; 84 } 85 86 89 public String getEnvEntryName() 90 { 91 return _name; 92 } 93 94 97 public void setEnvEntryType(Class type) 98 { 99 _type = type; 100 } 101 102 105 public Class getEnvEntryType() 106 { 107 return _type; 108 } 109 110 113 public void setEnvEntryValue(String value) 114 { 115 _value = value; 116 } 117 118 121 public String getEnvEntryValue() 122 { 123 return _value; 124 } 125 126 129 @PostConstruct 130 public void init() 131 throws Exception 132 { 133 if (_name == null) 134 throw new ConfigException(L.l("env-entry needs `env-entry-name' attribute")); 135 if (_type == null) 136 throw new ConfigException(L.l("env-entry needs `env-entry-type' attribute")); 137 138 142 143 if (_value == null) 145 return; 146 147 Object value = _value; 148 149 if (_type.equals(String .class)) { 150 } 151 else if (_type.equals(Boolean .class)) 152 value = new Boolean (Expr.toBoolean(_value, null)); 153 else if (_type.equals(Byte .class)) 154 value = new Byte ((byte) Expr.toLong(_value, null)); 155 else if (_type.equals(Short .class)) 156 value = new Short ((short) Expr.toLong(_value, null)); 157 else if (_type.equals(Integer .class)) 158 value = new Integer ((int) Expr.toLong(_value, null)); 159 else if (_type.equals(Long .class)) 160 value = new Long (Expr.toLong(_value, null)); 161 else if (_type.equals(Float .class)) 162 value = new Float ((float) Expr.toDouble(_value, null)); 163 else if (_type.equals(Double .class)) 164 value = new Double (Expr.toDouble(_value, null)); 165 else if (_type.equals(Character .class)) { 166 String v = Expr.toString(_value, null); 167 168 if (v == null || v.length() == 0) 169 value = null; 170 else 171 value = new Character (v.charAt(0)); 172 } 173 174 if (_name.startsWith("java:comp")) 175 Jndi.bindDeep(_name, value); 176 else 177 Jndi.bindDeep("java:comp/env/" + _name, value); 178 } 179 180 184 public void validate() 185 throws ConfigException 186 { 187 Object obj = null; 188 189 try { 190 obj = new InitialContext ().lookup("java:comp/env/" + _name); 191 } catch (NamingException e) { 192 log.log(Level.FINER, e.toString(), e); 193 } 194 195 if (obj == null) 196 throw error(L.l("env-entry '{0}' was not configured. All resources defined by <env-entry> tags must be defined in a configuration file.", 197 _name)); 198 } 199 200 public ConfigException error(String msg) 201 { 202 if (_location != null) 203 return new LineConfigException(_location + msg); 204 else 205 return new ConfigException(msg); 206 } 207 208 public String toString() 209 { 210 return "EnvEntry[" + _name + "]"; 211 } 212 } 213 214 | Popular Tags |