001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JHome.java 8098 2006-03-09 14:15:42Z durieuxp $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ejb.container;
025:
026: import java.rmi.NoSuchObjectException;
027: import java.rmi.RemoteException;
028:
029: import javax.ejb.EJBHome;
030: import javax.ejb.EJBMetaData;
031: import javax.ejb.Handle;
032: import javax.ejb.HomeHandle;
033: import javax.ejb.RemoveException;
034: import javax.naming.NamingException;
035: import javax.rmi.PortableRemoteObject;
036:
037: import org.objectweb.carol.cmi.DistributedEquiv;
038: import org.objectweb.carol.cmi.ServerConfigException;
039: import org.objectweb.carol.util.configuration.ConfigurationRepository;
040: import org.objectweb.carol.util.csiv2.SasHelper;
041:
042: import org.objectweb.jonas_ejb.deployment.api.BeanDesc;
043: import org.objectweb.jonas_ejb.deployment.api.EntityDesc;
044: import org.objectweb.jonas_ejb.deployment.api.SessionDesc;
045: import org.objectweb.jonas_ejb.deployment.api.SessionStatelessDesc;
046: import org.objectweb.jonas_ejb.lib.EJBInvocation;
047: import org.objectweb.jonas_ejb.svc.JHomeHandleIIOP;
048: import org.objectweb.jonas_ejb.svc.JMetaData;
049:
050: import org.objectweb.util.monolog.api.BasicLevel;
051:
052: /**
053: * This class represents an EJBHome Remote Interface
054: * It is shared between Sessions and Entities.
055: * @author Philippe Coq, Philippe Durieux
056: */
057: public abstract class JHome extends PortableRemoteObject implements
058: EJBHome {
059:
060: protected JMetaData ejbMetaData = null;
061: protected BeanDesc dd;
062: protected JFactory bf;
063: protected boolean unregistered = true;
064:
065: /**
066: * Constructor for the base class of the specific generated Home object.
067: * @param dd The Been Deployment Descriptor
068: * @param bf The Bean Factory
069: * @throws RemoteException exception
070: */
071: public JHome(BeanDesc dd, JFactory bf) throws RemoteException {
072: if (TraceEjb.isDebugIc()) {
073: TraceEjb.interp.log(BasicLevel.DEBUG, "JHome constructor");
074: }
075: this .dd = dd;
076: this .bf = bf;
077:
078: // For CMI needs, we have to keep the link between jndiname/classloader
079: String protocol = ConfigurationRepository
080: .getCurrentConfiguration().getProtocol().getName();
081: if (protocol.equals("cmi")) {
082: String key = DistributedEquiv.CL_PREFIX + dd.getJndiName();
083: if (key != null) { // cluster object
084: // register its classloader in the local cache
085: try {
086: DistributedEquiv.putLocalCLByKey(key, bf
087: .getContainer().getClassLoader());
088: } catch (ServerConfigException e) {
089: throw new RemoteException("", e);
090: }
091: }
092:
093: }
094: }
095:
096: // ---------------------------------------------------------------
097: // EJBHome Implementation
098: // ---------------------------------------------------------------
099:
100: /**
101: * Obtains the EJBMetaData for the enterprise Bean.
102: * @return The enterprise Bean's EJBMetaData
103: * @throws RemoteException exception
104: */
105: public EJBMetaData getEJBMetaData() throws RemoteException {
106:
107: if (TraceEjb.isDebugIc()) {
108: TraceEjb.interp.log(BasicLevel.DEBUG,
109: "JHome getEJBMetaData");
110: }
111:
112: /*
113: * try/catch block is commented because the encapsulated code don't
114: * throw a RemoteException
115: * If the code changes and throws a such exception, let's think
116: * to uncomment it
117: *
118: * try {
119: */
120: if (ejbMetaData == null) {
121:
122: if (dd instanceof SessionDesc) {
123: boolean isSession = true;
124: boolean isStatelessSession = (dd instanceof SessionStatelessDesc);
125: Class primaryKeyClass = null;
126: ejbMetaData = new JMetaData(this , dd.getHomeClass(), dd
127: .getRemoteClass(), isSession,
128: isStatelessSession, primaryKeyClass);
129: } else if (dd instanceof EntityDesc) {
130: boolean isSession = false;
131: boolean isStatelessSession = false;
132: Class primaryKeyClass = ((EntityDesc) dd)
133: .getPrimaryKeyClass();
134: ejbMetaData = new JMetaData(this , dd.getHomeClass(), dd
135: .getRemoteClass(), isSession,
136: isStatelessSession, primaryKeyClass);
137: }
138: }
139: return ejbMetaData;
140: /*
141: * } catch (RemoteException e) {
142: * // check if rmi exception mapping is needed - if yes the method rethrows it
143: * RmiUtility.rethrowRmiException(e);
144: * // if not, throws the exception just as it is
145: * throw e;
146: * }
147: */
148: }
149:
150: /**
151: * Obtain a handle for the home object. The handle can be used at later time to re-obtain a
152: * reference to the home object, possibly in a different Java Virtual Machine.
153: * @return A handle for the home object.
154: * @exception java.rmi.RemoteException - Thrown when the method failed due to a system-level
155: * failure.
156: */
157: public HomeHandle getHomeHandle() throws java.rmi.RemoteException {
158: if (TraceEjb.isDebugIc()) {
159: TraceEjb.interp.log(BasicLevel.DEBUG, "");
160: }
161:
162: /*
163: * try/catch block is commented because the encapsulated code don't
164: * throw a RemoteException
165: * If the code changes and throws a such exception, let's think
166: * to uncomment it
167: *
168: * try {
169: */
170: // for iiop, a specific interoperable Handle is created with the use of
171: // HandleDelegate
172: String protocol = ConfigurationRepository
173: .getCurrentConfiguration().getProtocol().getName();
174: if (TraceEjb.isDebugIc()) {
175: TraceEjb.interp.log(BasicLevel.DEBUG, "Current protocol = "
176: + protocol);
177: }
178: if (protocol.equals("iiop")) {
179: return new JHomeHandleIIOP(this , bf.myClassLoader());
180: } else {
181: return new JHomeHandle(dd.getJndiName());
182: }
183:
184: /*
185: * } catch (RemoteException e) {
186: * // check if rmi exception mapping is needed - if yes the method rethrows it
187: * RmiUtility.rethrowRmiException(e);
188: * // if not, throws the exception just as it is
189: * throw e;
190: * }
191: */
192: }
193:
194: /**
195: * Removes an EJB object identified by its handle.
196: * @param handle The EJB Handle
197: * @throws RemoteException exception
198: * @throws RemoveException exception
199: *
200: */
201: public abstract void remove(Handle handle) throws RemoteException,
202: RemoveException;
203:
204: /**
205: * Removes an EJB object identified by its primary key.
206: * @param primaryKey The Primary Key
207: * @throws RemoteException exception
208: * @throws RemoveException exception
209: */
210: public abstract void remove(Object primaryKey)
211: throws RemoteException, RemoveException;
212:
213: // ---------------------------------------------------------------
214: // other public methods
215: // ---------------------------------------------------------------
216:
217: /**
218: * register this bean to JNDI (rebind)
219: * @throws NamingException exception
220: */
221: protected void register() throws NamingException {
222: if (TraceEjb.isDebugIc()) {
223: TraceEjb.interp.log(BasicLevel.DEBUG, "");
224: }
225: String name = dd.getJndiName();
226:
227: // register in registry
228: SasHelper.rebind(name, this , dd.getSasComponent());
229:
230: unregistered = false;
231: }
232:
233: /**
234: * unregister this bean in JNDI (unbind)
235: * @throws NamingException exception
236: */
237: protected void unregister() throws NamingException {
238: if (TraceEjb.isDebugIc()) {
239: TraceEjb.interp.log(BasicLevel.DEBUG, "");
240: }
241: String name = dd.getJndiName();
242: // unregister in default InitialContext
243: bf.getInitialContext().unbind(name);
244:
245: try {
246: PortableRemoteObject.unexportObject(this );
247: } catch (NoSuchObjectException e) {
248: TraceEjb.interp.log(BasicLevel.ERROR,
249: "unexportObject(JHome) failed", e);
250: }
251: unregistered = true;
252: }
253:
254: /**
255: * Get the jndi name for the bean
256: * @return The jndi name
257: * @throws RemoteException
258: */
259: public String getJndiName() {
260: return dd.getJndiName();
261: }
262:
263: /**
264: * preInvoke is called before any request.
265: * @param txa Transaction Attribute (Supports, Required, ...)
266: * @return A RequestCtx object
267: * @throws RemoteException unexpected exception in preinvoke
268: */
269: public RequestCtx preInvoke(int txa) throws RemoteException {
270: if (TraceEjb.isDebugIc()) {
271: TraceEjb.interp.log(BasicLevel.DEBUG, "");
272: }
273: return bf.preInvokeRemote(txa);
274: }
275:
276: /**
277: * Check if the access to the bean is authorized
278: * @param ejbInv object containing security signature of the method, args of
279: * method, etc
280: */
281: public void checkSecurity(EJBInvocation ejbInv) {
282: if (TraceEjb.isDebugIc()) {
283: TraceEjb.interp.log(BasicLevel.DEBUG, "");
284: }
285: bf.checkSecurity(ejbInv);
286: }
287:
288: /**
289: * postInvoke is called after any request.
290: * @param rctx The RequestCtx that was returned at preInvoke()
291: * @throws RemoteException unexpected exception in postinvoke
292: */
293: public void postInvoke(RequestCtx rctx) throws RemoteException {
294: if (TraceEjb.isDebugIc()) {
295: TraceEjb.interp.log(BasicLevel.DEBUG, "");
296: }
297: bf.postInvokeRemote(rctx);
298: }
299:
300: /**
301: * @return Returns the dd.
302: */
303: public BeanDesc getDd() {
304: return dd;
305: }
306:
307: /**
308: * @return the bean factory
309: */
310: public JFactory getBf() {
311: return bf;
312: }
313: }
|