1 29 30 package com.caucho.config.types; 31 32 import com.caucho.config.BuilderProgram; 33 import com.caucho.config.Config; 34 import com.caucho.config.ConfigException; 35 import com.caucho.config.LineConfigException; 36 import com.caucho.loader.ClassLoaderListener; 37 import com.caucho.loader.DynamicClassLoader; 38 import com.caucho.loader.EnvironmentClassLoader; 39 import com.caucho.naming.Jndi; 40 import com.caucho.util.L10N; 41 42 import javax.annotation.PostConstruct; 43 import javax.naming.InitialContext ; 44 import javax.naming.NamingException ; 45 import java.util.HashMap ; 46 import java.util.Iterator ; 47 import java.util.logging.Level ; 48 import java.util.logging.Logger ; 49 50 53 public class ResourceRef implements Validator { 54 private static Logger log = Logger.getLogger(ResourceRef.class.getName()); 55 private static L10N L = new L10N(ResourceRef.class); 56 57 private String _location = ""; 58 59 private String _name; 60 private Class _type; 61 private String _description; 62 private boolean _sharing; 63 64 private BuilderProgram _init; 65 private HashMap <String ,String > _params = new HashMap <String ,String >(); 66 67 70 public void setId(String id) 71 { 72 } 73 74 77 public void setConfigLocation(String filename, int line) 78 { 79 _location = filename + ":" + line + " "; 80 } 81 82 85 public void setDescription(String description) 86 { 87 _description = description; 88 } 89 90 93 public void setResRefName(String name) 94 { 95 _name = name; 96 } 97 98 101 public String getResRefName() 102 { 103 return _name; 104 } 105 106 109 public void setResType(Class type) 110 { 111 _type = type; 112 } 113 114 117 public void setResAuth(String auth) 118 { 119 } 120 121 124 public void setResSharingScope(String share) 125 { 126 } 127 128 131 public void setClassName(Class type) 132 { 133 _type = type; 134 } 135 136 139 public Class getResType() 140 { 141 return _type; 142 } 143 144 147 public void setInit(BuilderProgram init) 148 { 149 _init = init; 150 } 151 152 155 public BuilderProgram getInit() 156 { 157 return _init; 158 } 159 160 163 public void setInitParam(InitParam initParam) 164 { 165 _params.putAll(initParam.getParameters()); 166 } 167 168 171 @PostConstruct 172 public void init() 173 throws Throwable 174 { 175 if (_init == null && _params.size() == 0) { 176 return; 177 } 178 179 Class cl = _type; 180 181 if (javax.sql.DataSource .class.equals(_type)) 182 cl = com.caucho.sql.DBPool.class; 183 187 188 Object obj = cl.newInstance(); 189 190 if (_init != null) 191 _init.configure(obj); 192 193 Iterator iter = _params.keySet().iterator(); 194 while (iter.hasNext()) { 195 String key = (String ) iter.next(); 196 String value = (String ) _params.get(key); 197 198 Config.setAttribute(obj, key, value); 199 } 200 201 if (obj instanceof ClassLoaderListener) { 202 ClassLoaderListener listener = (ClassLoaderListener) obj; 203 204 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 205 for (; loader != null; loader = loader.getParent()) { 206 if (loader instanceof EnvironmentClassLoader) { 207 ((DynamicClassLoader) loader).addListener(listener); 208 break; 209 } 210 } 211 } 212 213 Jndi.bindDeep(_name, obj); 214 } 215 216 220 public void validate() 221 throws ConfigException 222 { 223 Object obj = null; 224 225 try { 226 obj = new InitialContext ().lookup("java:comp/env/" + _name); 227 } catch (NamingException e) { 228 log.log(Level.FINEST, e.toString(), e); 229 } 230 231 if (obj == null) 232 throw error(L.l("resource-ref '{0}' was not configured. All resources defined by <resource-ref> tags must be defined in a configuration file.", 233 _name)); 234 } 235 236 public ConfigException error(String msg) 237 { 238 if (_location != null) 239 return new LineConfigException(_location + msg); 240 else 241 return new ConfigException(msg); 242 } 243 244 public String toString() 245 { 246 return "ResourceRef[" + _name + "]"; 247 } 248 } 249 | Popular Tags |