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.ejb3.stateful;
023:
024: import java.rmi.RemoteException;
025: import java.rmi.ServerException;
026: import java.security.AccessControlException;
027: import java.util.Hashtable;
028: import java.lang.reflect.InvocationHandler;
029: import java.lang.reflect.Method;
030: import java.lang.reflect.Proxy;
031:
032: import javax.ejb.EJBObject;
033: import javax.ejb.Handle;
034: import javax.naming.InitialContext;
035:
036: import org.jboss.invocation.Invoker;
037: import org.jboss.logging.Logger;
038: import org.jboss.naming.NamingContextFactory;
039:
040: /**
041: * An EJB stateful session bean handle.
042: *
043: * @author <a href="mailto:marc.fleury@jboss.org">Marc Fleury</a>
044: * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
045: * @author <a href="bill@burkecentral.com">Bill Burke</a>
046: * @author <a href="bdecoste@jboss.com">William DeCoste</a>
047: * @version $Revision: 60233 $
048: */
049: public class StatefulHandleImpl implements Handle {
050: private static final Logger log = Logger
051: .getLogger(StatefulHandleImpl.class);
052:
053: /** Serial Version Identifier. */
054: static final long serialVersionUID = -6324520755180597156L;
055:
056: /** A reference to {@link Handle#getEJBObject}. */
057: protected static final Method GET_EJB_OBJECT;
058:
059: /** The value of our local Invoker.ID to detect when we are local. */
060: private Object invokerID = null;
061:
062: /**
063: * Initialize <tt>Handle</tt> method references.
064: */
065: static {
066: try {
067: GET_EJB_OBJECT = Handle.class.getMethod("getEJBObject",
068: new Class[0]);
069: } catch (Exception e) {
070: e.printStackTrace();
071: throw new ExceptionInInitializerError(e);
072: }
073: }
074:
075: public StatefulHandleImpl() {
076:
077: }
078:
079: /** The identity of the bean. */
080: public int objectName;
081: public String jndiName;
082: public String invokerProxyBinding;
083: public Invoker invoker;
084: public Object id;
085:
086: /** The JNDI env in effect when the home handle was created */
087: protected Hashtable jndiEnv;
088:
089: /** Create an ejb handle for a stateful session bean.
090: * @param objectName - the session container jmx name
091: * @param jndiName - the session home ejb name
092: * @param invoker - the invoker to request the EJBObject from
093: * @param invokerProxyBinding - the type of invoker binding
094: * @param id - the session id
095: */
096: public StatefulHandleImpl(int objectName, String jndiName,
097: Invoker invoker, String invokerProxyBinding, Object id,
098: Object invokerID) {
099: this .jndiName = jndiName;
100: this .id = id;
101: this .jndiEnv = (Hashtable) NamingContextFactory.lastInitialContextEnv
102: .get();
103: try {
104: String property = System
105: .getProperty("org.jboss.ejb.sfsb.handle.V327");
106: if (property != null) {
107: this .invokerProxyBinding = invokerProxyBinding;
108: this .invokerID = invokerID;
109: this .objectName = objectName;
110: this .invoker = invoker;
111: }
112: } catch (AccessControlException ignored) {
113: }
114:
115: }
116:
117: /**
118: * @return the internal session identifier
119: */
120: public Object getID() {
121: return id;
122: }
123:
124: /**
125: * @return the jndi name
126: */
127: public String getJNDIName() {
128: return jndiName;
129: }
130:
131: /**
132: * Handle implementation.
133: *
134: * This differs from Stateless and Entity handles which just invoke
135: * standard methods (<tt>create</tt> and <tt>findByPrimaryKey</tt>
136: * respectively) on the Home interface (proxy).
137: * There is no equivalent option for stateful SBs, so a direct invocation
138: * on the container has to be made to locate the bean by its id (the
139: * stateful SB container provides an implementation of
140: * <tt>getEJBObject</tt>).
141: *
142: * This means the security context has to be set here just as it would
143: * be in the Proxy.
144: *
145: * @return <tt>EJBObject</tt> reference.
146: *
147: * @throws ServerException Could not get EJBObject.
148: */
149: public EJBObject getEJBObject() throws RemoteException {
150: try {
151: InitialContext ic = null;
152: if (jndiEnv != null)
153: ic = new InitialContext(jndiEnv);
154: else
155: ic = new InitialContext();
156:
157: Proxy proxy = (Proxy) ic.lookup(jndiName);
158:
159: return (EJBObject) proxy;
160: } catch (Throwable t) {
161: t.printStackTrace();
162: throw new RemoteException("Error during getEJBObject", t);
163: }
164: }
165: }
|