KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > config > types > EjbRef


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Sam
28  */

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 JavaDoc;
43 import javax.naming.NamingException JavaDoc;
44 import java.util.Hashtable JavaDoc;
45 import java.util.logging.Level JavaDoc;
46 import java.util.logging.Logger JavaDoc;
47
48 /**
49  * Configuration for the ejb-ref.
50  *
51  * An ejb-ref is used to make an ejb available within the environment
52  * in which the ejb-ref is declared.
53  */

54 public class EjbRef implements ObjectProxy {
55   private static final L10N L = new L10N(EjbRef.class);
56   private static final Logger JavaDoc log
57     = Logger.getLogger(EjbRef.class.getName());
58
59   private String JavaDoc _ejbRefName;
60   private String JavaDoc _type;
61   private Class JavaDoc _home;
62   private Class JavaDoc _remote;
63   private String JavaDoc _jndiName;
64   private String JavaDoc _ejbLink;
65
66   private Object JavaDoc _target;
67
68   public EjbRef()
69   {
70   }
71
72   protected String JavaDoc getTagName()
73   {
74     return "<ejb-ref>";
75   }
76
77   public void setId(String JavaDoc id)
78   {
79   }
80
81   public void setDescription(String JavaDoc description)
82   {
83   }
84
85   /**
86    * Sets the name to use in the local jndi context.
87    * This is the jndi lookup name that code uses to obtain the home for
88    * the bean when doing a jndi lookup.
89    *
90    * <pre>
91    * <ejb-ref-name>ejb/Gryffindor</ejb-ref-name>
92    * ...
93    * (new InitialContext()).lookup("java:comp/env/ejb/Gryffindor");
94    * </pre>
95    */

96   public void setEjbRefName(String JavaDoc name)
97   {
98     _ejbRefName = name;
99   }
100
101   /**
102    * Returns the ejb name.
103    */

104   public String JavaDoc getEjbRefName()
105   {
106     return _ejbRefName;
107   }
108
109   public void setEjbRefType(String JavaDoc type)
110   {
111     _type = type;
112   }
113
114   public void setHome(Class JavaDoc home)
115   {
116     _home = home;
117   }
118
119   /**
120    * Returns the home class.
121    */

122   public Class JavaDoc getHome()
123   {
124     return _home;
125   }
126
127   public void setRemote(Class JavaDoc remote)
128   {
129     _remote = remote;
130   }
131
132   /**
133    * Returns the remote class.
134    */

135   public Class JavaDoc getRemote()
136   {
137     // XXX: should distinguish
138
return _remote;
139   }
140
141   /**
142    * Sets the canonical jndi name to use to find the bean that
143    * is the target of the reference.
144    * For remote beans, a &lt;jndi-link> {@link com.caucho.naming.LinkProxy} is
145    * used to link the local jndi context referred to in this name to
146    * a remote context.
147    */

148   public void setJndiName(String JavaDoc jndiName)
149   {
150     _jndiName = jndiName;
151   }
152
153   /**
154    * Set the target of the reference, an alternative to {@link #setJndiName(String)}.
155    * The format of the ejbLink is "bean", or "jarname#bean", where <i>bean</i> is the
156    * ejb-name of a bean within the same enterprise application, and <i>jarname</i>
157    * further qualifies the identity of the target.
158    */

159   public void setEjbLink(String JavaDoc ejbLink)
160   {
161     _ejbLink = ejbLink;
162   }
163
164   @PostConstruct
165   public void init()
166     throws Exception JavaDoc
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 JavaDoc 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 JavaDoc()).unbind(fullEjbRefName);
207       }
208       catch (Exception JavaDoc 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   /**
220    * Creates the object from the proxy.
221    *
222    * @return the object named by the proxy.
223    */

224   public Object JavaDoc createObject(Hashtable JavaDoc env)
225     throws NamingException JavaDoc
226   {
227     if (_target == null)
228       resolve();
229
230     return _target;
231   }
232
233   private void resolve()
234     throws NamingException JavaDoc
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 JavaDoc(L.l("{0} '{1}' cannot be resolved",
245                                         getTagName(), _ejbRefName));
246         else
247           throw new NamingException JavaDoc(L.l("{0} '{1}' jndi-name '{2}' not found",
248                                         getTagName(), _ejbRefName, _jndiName));
249     }
250     else {
251       String JavaDoc archiveName;
252       String JavaDoc 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 JavaDoc(L.l("{0} '{1}' cannot be resolved",
273                                           getTagName(), _ejbRefName));
274           else
275             throw new NamingException JavaDoc(L.l("{0} '{1}' ejb-link '{2}' not found",
276                                           getTagName(), _ejbRefName, _ejbLink));
277         }
278         else {
279           Object JavaDoc localHome = server.getEJBLocalHome();
280
281           if (localHome != null)
282             _target = localHome;
283
284           if (_target == null) {
285             Object JavaDoc 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 JavaDoc(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 JavaDoc e) {
300         throw e;
301       }
302       catch (Exception JavaDoc e) {
303         throw new NamingException JavaDoc(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 JavaDoc toString()
313   {
314     return getClass().getSimpleName()
315            + "[" + _ejbRefName + ", " + _ejbLink + ", " + _jndiName + "]";
316   }
317 }
318
Popular Tags