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;
023:
024: import java.lang.reflect.Method;
025: import java.rmi.RemoteException;
026:
027: import org.jboss.invocation.Invocation;
028: import org.jboss.invocation.InvocationContext;
029: import org.jboss.invocation.InvocationKey;
030: import org.jboss.invocation.InvocationType;
031: import org.jboss.invocation.Invoker;
032: import org.jboss.proxy.ejb.handle.StatefulHandleImpl;
033:
034: /**
035: *
036: * @author <a href="mailto:marc.fleury@jboss.org">Marc Fleury</a>
037: * @version $Revision: 57209 $
038: */
039: public class StatefulSessionInterceptor extends GenericEJBInterceptor {
040: /** Serial Version Identifier. @since 1.5 */
041: private static final long serialVersionUID = -4333233488946091285L;
042:
043: /**
044: * No-argument constructor for externalization.
045: */
046: public StatefulSessionInterceptor() {
047: }
048:
049: // Public --------------------------------------------------------
050:
051: /**
052: * InvocationHandler implementation.
053: *
054: * @throws Throwable Any exception or error thrown while processing.
055: */
056: public Object invoke(Invocation invocation) throws Throwable {
057: InvocationContext ctx = invocation.getInvocationContext();
058:
059: Method m = invocation.getMethod();
060:
061: // Implement local methods
062: if (m.equals(TO_STRING)) {
063: return toString(ctx);
064: } else if (m.equals(EQUALS)) {
065: Object[] args = invocation.getArguments();
066: String argsString = args[0] != null ? args[0].toString()
067: : "";
068: String this String = toString(ctx);
069: return new Boolean(this String.equals(argsString));
070: } else if (m.equals(HASH_CODE)) {
071: return new Integer(ctx.getCacheId().hashCode());
072: }
073: // Implement local EJB calls
074: else if (m.equals(GET_HANDLE)) {
075: int objectName = ((Integer) ctx.getObjectName()).intValue();
076: String jndiName = (String) ctx
077: .getValue(InvocationKey.JNDI_NAME);
078: Invoker invoker = ctx.getInvoker();
079: Object id = ctx.getCacheId();
080: return new StatefulHandleImpl(objectName, jndiName,
081: invoker, ctx.getInvokerProxyBinding(), id, ctx
082: .getValue("InvokerID"));
083: } else if (m.equals(GET_EJB_HOME)) {
084: return getEJBHome(invocation);
085: } else if (m.equals(GET_PRIMARY_KEY)) {
086: throw new RemoteException(
087: "Call to getPrimaryKey not allowed on session bean");
088: } else if (m.equals(IS_IDENTICAL)) {
089: Object[] args = invocation.getArguments();
090: String argsString = args[0].toString();
091: String this String = toString(ctx);
092: return new Boolean(this String.equals(argsString));
093: }
094: // If not taken care of, go on and call the container
095: else {
096: // It is a remote invocation
097: invocation.setType(InvocationType.REMOTE);
098:
099: // On this entry in cache
100: invocation.setId(ctx.getCacheId());
101:
102: return getNext().invoke(invocation);
103: }
104: }
105:
106: private String toString(InvocationContext ctx) {
107: return ctx.getValue(InvocationKey.JNDI_NAME) + ":"
108: + ctx.getCacheId().toString();
109: }
110: }
|