1 29 30 package com.caucho.config.types; 31 32 import com.caucho.config.ConfigException; 33 import com.caucho.ejb.AbstractServer; 34 import com.caucho.ejb.EJBServer; 35 import com.caucho.naming.Jndi; 36 import com.caucho.naming.ObjectProxy; 37 import com.caucho.util.L10N; 38 import com.caucho.vfs.Path; 39 import com.caucho.vfs.Vfs; 40 41 import javax.annotation.PostConstruct; 42 import javax.naming.InitialContext ; 43 import javax.naming.NamingException ; 44 import java.util.Hashtable ; 45 import java.util.logging.Level ; 46 import java.util.logging.Logger ; 47 48 54 public class EjbRef implements ObjectProxy { 55 private static final L10N L = new L10N(EjbRef.class); 56 private static final Logger log 57 = Logger.getLogger(EjbRef.class.getName()); 58 59 private String _ejbRefName; 60 private String _type; 61 private Class _home; 62 private Class _remote; 63 private String _jndiName; 64 private String _ejbLink; 65 66 private Object _target; 67 68 public EjbRef() 69 { 70 } 71 72 protected String getTagName() 73 { 74 return "<ejb-ref>"; 75 } 76 77 public void setId(String id) 78 { 79 } 80 81 public void setDescription(String description) 82 { 83 } 84 85 96 public void setEjbRefName(String name) 97 { 98 _ejbRefName = name; 99 } 100 101 104 public String getEjbRefName() 105 { 106 return _ejbRefName; 107 } 108 109 public void setEjbRefType(String type) 110 { 111 _type = type; 112 } 113 114 public void setHome(Class home) 115 { 116 _home = home; 117 } 118 119 122 public Class getHome() 123 { 124 return _home; 125 } 126 127 public void setRemote(Class remote) 128 { 129 _remote = remote; 130 } 131 132 135 public Class getRemote() 136 { 137 return _remote; 139 } 140 141 148 public void setJndiName(String jndiName) 149 { 150 _jndiName = jndiName; 151 } 152 153 159 public void setEjbLink(String ejbLink) 160 { 161 _ejbLink = ejbLink; 162 } 163 164 @PostConstruct 165 public void init() 166 throws Exception 167 { 168 boolean bind = false; 169 170 if (_ejbRefName == null) 171 throw new ConfigException(L.l("{0} is required", "<ejb-ref-name>")); 172 173 if (_ejbLink != null && _jndiName != null) 174 throw new ConfigException(L.l("either {0} or {1} can be used, but not both", "<ejb-link>", "<jndi-name>")); 175 176 if (_ejbLink == null && _jndiName == null) { 177 EJBServer server = EJBServer.getLocal(); 178 179 if (server != null) 180 _ejbLink = _ejbRefName; 181 else 182 _jndiName = _ejbRefName; 183 } 184 185 String fullEjbRefName = Jndi.getFullName(_ejbRefName); 186 187 if (_ejbLink != null) { 188 EJBServer server = EJBServer.getLocal(); 189 190 if (server == null) 191 throw new ConfigException(L.l("{0} requires a local {1}", "<ejb-link>", "<ejb-server>")); 192 193 bind = true; 194 } 195 else { 196 if (_jndiName != null) { 197 _jndiName = Jndi.getFullName(_jndiName); 198 199 if (! _jndiName.equals(fullEjbRefName)) 200 bind = true; 201 } 202 } 203 204 if (bind) { 205 try { 206 (new InitialContext ()).unbind(fullEjbRefName); 207 } 208 catch (Exception ex) { 209 log.log(Level.FINEST, ex.toString(), ex); 210 } 211 212 Jndi.bindDeep(fullEjbRefName, this); 213 } 214 215 if (log.isLoggable(Level.FINER)) 216 log.log(Level.FINER, L.l("{0} init", this)); 217 } 218 219 224 public Object createObject(Hashtable env) 225 throws NamingException 226 { 227 if (_target == null) 228 resolve(); 229 230 return _target; 231 } 232 233 private void resolve() 234 throws NamingException 235 { 236 if (log.isLoggable(Level.FINEST)) 237 log.log(Level.FINEST, L.l("{0} resolving", this)); 238 239 if (_jndiName != null) { 240 _target = Jndi.lookup(_jndiName); 241 242 if (_target == null) 243 if (_jndiName.equals(_ejbRefName)) 244 throw new NamingException (L.l("{0} '{1}' cannot be resolved", 245 getTagName(), _ejbRefName)); 246 else 247 throw new NamingException (L.l("{0} '{1}' jndi-name '{2}' not found", 248 getTagName(), _ejbRefName, _jndiName)); 249 } 250 else { 251 String archiveName; 252 String ejbName; 253 254 int hashIndex = _ejbLink.indexOf('#'); 255 256 if (hashIndex < 0) { 257 archiveName = null; 258 ejbName = _ejbLink; 259 } 260 else { 261 archiveName = _ejbLink.substring(0, hashIndex); 262 ejbName = _ejbLink.substring(hashIndex + 1); 263 } 264 265 try { 266 Path path = archiveName == null ? Vfs.getPwd() : Vfs.lookup(archiveName); 267 268 AbstractServer server = EJBServer.getLocal().getServer(path, ejbName); 269 270 if (server == null) { 271 if (_ejbLink.equals(_ejbRefName)) 272 throw new NamingException (L.l("{0} '{1}' cannot be resolved", 273 getTagName(), _ejbRefName)); 274 else 275 throw new NamingException (L.l("{0} '{1}' ejb-link '{2}' not found", 276 getTagName(), _ejbRefName, _ejbLink)); 277 } 278 else { 279 Object localHome = server.getEJBLocalHome(); 280 281 if (localHome != null) 282 _target = localHome; 283 284 if (_target == null) { 285 Object remoteHome = server.getEJBHome(); 286 287 if (remoteHome != null) 288 _target = remoteHome; 289 } 290 291 if (_target == null) { 292 log.log(Level.FINE, L.l("no home interface is available for '{0}'", server)); 293 294 throw new NamingException (L.l("{0} '{1}' ejb bean found with ejb-link '{2}' has no home interface", 295 getTagName(), _ejbRefName, _ejbLink)); 296 } 297 } 298 } 299 catch (NamingException e) { 300 throw e; 301 } 302 catch (Exception e) { 303 throw new NamingException (L.l("{0} '{1}' ejb-link '{2}' invalid ", 304 getTagName(), _ejbRefName, _ejbLink)); 305 } 306 307 if (log.isLoggable(Level.CONFIG)) 308 log.log(Level.CONFIG, L.l("{0} resolved", this)); 309 } 310 } 311 312 public String toString() 313 { 314 return getClass().getSimpleName() 315 + "[" + _ejbRefName + ", " + _ejbLink + ", " + _jndiName + "]"; 316 } 317 } 318 | Popular Tags |