KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > proxy > ejb > StatefulSessionInterceptor


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.proxy.ejb;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.rmi.RemoteException JavaDoc;
26
27 import org.jboss.invocation.Invocation;
28 import org.jboss.invocation.InvocationContext;
29 import org.jboss.invocation.InvocationKey;
30 import org.jboss.invocation.InvocationType;
31 import org.jboss.invocation.Invoker;
32 import org.jboss.proxy.ejb.handle.StatefulHandleImpl;
33
34 /**
35  *
36  * @author <a HREF="mailto:marc.fleury@jboss.org">Marc Fleury</a>
37  * @version $Revision: 37459 $
38  */

39 public class StatefulSessionInterceptor
40    extends GenericEJBInterceptor
41 {
42    /** Serial Version Identifier. @since 1.5 */
43    private static final long serialVersionUID = -4333233488946091285L;
44
45    /**
46     * No-argument constructor for externalization.
47     */

48    public StatefulSessionInterceptor()
49    {
50    }
51
52    // Public --------------------------------------------------------
53

54    /**
55     * InvocationHandler implementation.
56     *
57     * @throws Throwable Any exception or error thrown while processing.
58     */

59    public Object JavaDoc invoke(Invocation invocation)
60       throws Throwable JavaDoc
61    {
62       InvocationContext ctx = invocation.getInvocationContext();
63       
64       Method JavaDoc m = invocation.getMethod();
65       
66       // Implement local methods
67
if (m.equals(TO_STRING))
68       {
69          return toString(ctx);
70       }
71       else if (m.equals(EQUALS))
72       {
73          Object JavaDoc[] args = invocation.getArguments();
74          String JavaDoc argsString = args[0] != null ? args[0].toString() : "";
75          String JavaDoc thisString = toString(ctx);
76          return new Boolean JavaDoc(thisString.equals(argsString));
77       }
78       else if (m.equals(HASH_CODE))
79       {
80          return new Integer JavaDoc(ctx.getCacheId().hashCode());
81       }
82       // Implement local EJB calls
83
else if (m.equals(GET_HANDLE))
84       {
85          int objectName = ((Integer JavaDoc) ctx.getObjectName()).intValue();
86          String JavaDoc jndiName = (String JavaDoc) ctx.getValue(InvocationKey.JNDI_NAME);
87          Invoker invoker = ctx.getInvoker();
88          Object JavaDoc id = ctx.getCacheId();
89          return new StatefulHandleImpl(
90                objectName,
91                jndiName,
92                invoker,
93                ctx.getInvokerProxyBinding(),
94                id,
95                ctx.getValue("InvokerID"));
96       }
97       else if (m.equals(GET_EJB_HOME))
98       {
99          return getEJBHome(invocation);
100       }
101       else if (m.equals(GET_PRIMARY_KEY))
102       {
103          throw new RemoteException JavaDoc("Call to getPrimaryKey not allowed on session bean");
104       }
105       else if (m.equals(IS_IDENTICAL))
106       {
107          Object JavaDoc[] args = invocation.getArguments();
108          String JavaDoc argsString = args[0].toString();
109          String JavaDoc thisString = toString(ctx);
110          return new Boolean JavaDoc(thisString.equals(argsString));
111       }
112       // If not taken care of, go on and call the container
113
else
114       {
115          // It is a remote invocation
116
invocation.setType(InvocationType.REMOTE);
117          
118          // On this entry in cache
119
invocation.setId(ctx.getCacheId());
120          
121          return getNext().invoke(invocation);
122       }
123    }
124
125    private String JavaDoc toString(InvocationContext ctx)
126    {
127       return ctx.getValue(InvocationKey.JNDI_NAME) + ":" +
128             ctx.getCacheId().toString();
129    }
130 }
131
Popular Tags