KickJava   Java API By Example, From Geeks To Geeks.

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


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
26 import org.jboss.invocation.Invocation;
27 import org.jboss.invocation.InvocationContext;
28 import org.jboss.invocation.InvocationKey;
29 import org.jboss.invocation.InvocationType;
30 import org.jboss.proxy.ejb.handle.EntityHandleImpl;
31
32 /**
33  * An EJB entity bean proxy class.
34  * @author <a HREF="mailto:marc.fleury@jboss.org">Marc Fleury</a>
35  * @version $Revision: 37459 $
36  */

37 public class EntityInterceptor
38    extends GenericEJBInterceptor
39 {
40    /** Serial Version Identifier. @since 1.6 */
41    private static final long serialVersionUID = 4399705304832568350L;
42
43    /**
44     * No-argument constructor for externalization.
45     */

46    public EntityInterceptor()
47    {}
48    
49    // Public --------------------------------------------------------
50

51    /**
52     * InvocationHandler implementation.
53     *
54     * @param proxy The proxy object.
55     * @param m The method being invoked.
56     * @param args The arguments for the method.
57     *
58     * @throws Throwable Any exception or error thrown while processing.
59     */

60    public Object JavaDoc invoke(Invocation invocation)
61       throws Throwable JavaDoc
62    {
63       InvocationContext ctx = invocation.getInvocationContext();
64       
65       Method JavaDoc m = invocation.getMethod();
66       
67       // Implement local methods
68
if (m.equals(TO_STRING))
69       {
70          return toString(ctx);
71       }
72       else if (m.equals(EQUALS))
73       {
74          Object JavaDoc[] args = invocation.getArguments();
75          String JavaDoc argsString = args[0] != null ? args[0].toString() : "";
76          String JavaDoc thisString = toString(ctx);
77          return new Boolean JavaDoc(thisString.equals(argsString));
78       }
79       else if (m.equals(HASH_CODE))
80       {
81          return new Integer JavaDoc(ctx.getCacheId().hashCode());
82       }
83       // Implement local EJB calls
84
else if (m.equals(GET_HANDLE))
85       {
86          String JavaDoc jndiName = (String JavaDoc) ctx.getValue(InvocationKey.JNDI_NAME);
87          Object JavaDoc id = ctx.getCacheId();
88          return new EntityHandleImpl(jndiName, id);
89       }
90       else if (m.equals(GET_PRIMARY_KEY))
91       {
92          return ctx.getCacheId();
93       }
94       else if (m.equals(GET_EJB_HOME))
95       {
96          return getEJBHome(invocation);
97       }
98       else if (m.equals(IS_IDENTICAL))
99       {
100          Object JavaDoc[] args = invocation.getArguments();
101          String JavaDoc argsString = args[0].toString();
102          String JavaDoc thisString = toString(ctx);
103          return new Boolean JavaDoc(thisString.equals(argsString));
104       }
105       // If not taken care of, go on and call the container
106
else
107       {
108          // We are a Remote invocation
109
invocation.setType(InvocationType.REMOTE);
110          // We pertain to this ID (represented by cache ID)
111
invocation.setId(ctx.getCacheId());
112          return getNext().invoke(invocation);
113       }
114    }
115    
116    // Package protected ---------------------------------------------
117

118    // Protected -----------------------------------------------------//////
119
private String JavaDoc toString(InvocationContext ctx)
120    {
121       return ctx.getValue(InvocationKey.JNDI_NAME) + ":" +
122             ctx.getCacheId().toString();
123    }
124    
125 }
126
Popular Tags