001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.proxy.ejb.handle;
023:
024: import java.rmi.RemoteException;
025: import java.rmi.ServerException;
026: import java.lang.reflect.InvocationHandler;
027: import java.lang.reflect.Method;
028: import java.lang.reflect.Proxy;
029: import java.security.AccessControlException;
030: import java.util.Hashtable;
031: import javax.ejb.Handle;
032: import javax.ejb.EJBObject;
033: import javax.naming.InitialContext;
034:
035: import org.jboss.invocation.Invoker;
036: import org.jboss.logging.Logger;
037: import org.jboss.naming.NamingContextFactory;
038:
039: /**
040: * An EJB stateful session bean handle.
041: *
042: * @author <a href="mailto:marc.fleury@jboss.org">Marc Fleury</a>
043: * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
044: * @author <a href="bill@burkecentral.com">Bill Burke</a>
045: * @version $Revision: 57209 $
046: */
047: public class StatefulHandleImpl implements Handle {
048: private static final Logger log = Logger
049: .getLogger(StatefulHandleImpl.class);
050:
051: /** Serial Version Identifier. */
052: static final long serialVersionUID = -6324520755180597156L;
053:
054: /** A reference to {@link Handle#getEJBObject}. */
055: protected static final Method GET_EJB_OBJECT;
056:
057: /** The value of our local Invoker.ID to detect when we are local. */
058: private Object invokerID = null;
059:
060: /**
061: * Initialize <tt>Handle</tt> method references.
062: */
063: static {
064: try {
065: GET_EJB_OBJECT = Handle.class.getMethod("getEJBObject",
066: new Class[0]);
067: } catch (Exception e) {
068: e.printStackTrace();
069: throw new ExceptionInInitializerError(e);
070: }
071: }
072:
073: /** The identity of the bean. */
074: public int objectName;
075: public String jndiName;
076: public String invokerProxyBinding;
077: public Invoker invoker;
078: public Object id;
079:
080: /** The JNDI env in effect when the home handle was created */
081: protected Hashtable jndiEnv;
082:
083: public StatefulHandleImpl() {
084:
085: }
086:
087: /** Create an ejb handle for a stateful session bean.
088: * @param objectName - the session container jmx name
089: * @param jndiName - the session home ejb name
090: * @param invoker - the invoker to request the EJBObject from
091: * @param invokerProxyBinding - the type of invoker binding
092: * @param id - the session id
093: */
094: public StatefulHandleImpl(int objectName, String jndiName,
095: Invoker invoker, String invokerProxyBinding, Object id,
096: Object invokerID) {
097: this .jndiName = jndiName;
098: this .id = id;
099: this .jndiEnv = (Hashtable) NamingContextFactory.lastInitialContextEnv
100: .get();
101: try {
102: String property = System
103: .getProperty("org.jboss.ejb.sfsb.handle.V327");
104: if (property != null) {
105: this .invokerProxyBinding = invokerProxyBinding;
106: this .invokerID = invokerID;
107: this .objectName = objectName;
108: this .invoker = invoker;
109: }
110: } catch (AccessControlException ignored) {
111: }
112:
113: }
114:
115: /**
116: * @return the internal session identifier
117: */
118: public Object getID() {
119: return id;
120: }
121:
122: /**
123: * @return the jndi name
124: */
125: public String getJNDIName() {
126: return jndiName;
127: }
128:
129: /**
130: * Handle implementation.
131: *
132: * This differs from Stateless and Entity handles which just invoke
133: * standard methods (<tt>create</tt> and <tt>findByPrimaryKey</tt>
134: * respectively) on the Home interface (proxy).
135: * There is no equivalent option for stateful SBs, so a direct invocation
136: * on the container has to be made to locate the bean by its id (the
137: * stateful SB container provides an implementation of
138: * <tt>getEJBObject</tt>).
139: *
140: * This means the security context has to be set here just as it would
141: * be in the Proxy.
142: *
143: * @return <tt>EJBObject</tt> reference.
144: *
145: * @throws ServerException Could not get EJBObject.
146: */
147: public EJBObject getEJBObject() throws RemoteException {
148: try {
149: InitialContext ic = null;
150: if (jndiEnv != null)
151: ic = new InitialContext(jndiEnv);
152: else
153: ic = new InitialContext();
154:
155: Proxy proxy = (Proxy) ic.lookup(jndiName);
156:
157: // call findByPrimary on the target
158: InvocationHandler ih = Proxy.getInvocationHandler(proxy);
159: return (EJBObject) ih.invoke(proxy, GET_EJB_OBJECT,
160: new Object[] { id });
161: } catch (RemoteException e) {
162: throw e;
163: } catch (Throwable t) {
164: t.printStackTrace();
165: throw new RemoteException("Error during getEJBObject", t);
166: }
167: }
168: }
|