KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > containers > EJBLocalObjectInvocationHandlerDelegate


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.ejb.containers;
25
26 import java.io.IOException JavaDoc;
27 import java.lang.reflect.InvocationHandler JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29
30 import com.sun.ejb.containers.EJBLocalObjectImpl.SerializableLocalObject;
31 import com.sun.ejb.spi.io.IndirectlySerializable;
32 import com.sun.ejb.spi.io.SerializableObjectFactory;
33 import com.sun.enterprise.Switch;
34
35 /**
36  * This class is used as a "proxy" or adapter between the business interface
37  * proxy and the EJBLocalObjectInvocationHandler. An instance of this class
38  * is created for each business interface of a bean. All java.lang.Object
39  * methods and mthods of IndirectlySerializable are handled by this
40  * InvocationHandler itself while the business interface methods are delegated
41  * to the delegate (which is the EJBLocalObjectInvocaionHandler).
42  *
43  * @author Mahesh Kannan
44  *
45  */

46 public class EJBLocalObjectInvocationHandlerDelegate
47     implements InvocationHandler JavaDoc {
48
49     private Class JavaDoc intfClass;
50     private long containerId;
51     private EJBLocalObjectInvocationHandler delegate;
52     
53     EJBLocalObjectInvocationHandlerDelegate(Class JavaDoc intfClass, long containerId,
54             EJBLocalObjectInvocationHandler delegate) {
55         this.intfClass = intfClass;
56         this.containerId = containerId;
57         this.delegate = delegate;
58     }
59     
60     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
61         throws Throwable JavaDoc {
62         
63         Class JavaDoc methodClass = method.getDeclaringClass();
64         Object JavaDoc result = null;
65         if( methodClass == java.lang.Object JavaDoc.class ) {
66             result = InvocationHandlerUtil.invokeJavaObjectMethod
67                 (this, method, args);
68         } else if( methodClass == IndirectlySerializable.class ) {
69             result = this.getSerializableObjectFactory();
70         }else {
71             result = delegate.invoke(intfClass, method, args);
72         }
73         
74         return result;
75     }
76     
77     
78     public int hashCode() {
79         return (int) containerId;
80     }
81     
82     public boolean equals(Object JavaDoc other) {
83         boolean result = false;
84         
85         if ((other != null)
86         && (other instanceof EJBLocalObjectInvocationHandlerDelegate)) {
87             EJBLocalObjectInvocationHandlerDelegate otherDelegate
88                     = (EJBLocalObjectInvocationHandlerDelegate) other;
89             if ((containerId == otherDelegate.containerId)
90             && (intfClass == otherDelegate.intfClass)) {
91                 EJBLocalObjectInvocationHandler otherHandler
92                     = otherDelegate.delegate;
93                 result = (delegate.getKey() != null)
94                     ? delegate.getKey().equals(otherHandler.getKey())
95                     : (otherHandler.getKey() == null);
96             }
97         }
98         
99         return result;
100     }
101
102     public String JavaDoc toString() {
103         return intfClass.getName() + "_" + System.identityHashCode(this);
104     }
105     
106     public SerializableObjectFactory getSerializableObjectFactory() {
107         // Note: for stateful SessionBeans, the EJBLocalObjectImpl contains
108
// a pointer to the EJBContext. We should not serialize it here.
109

110         return new SerializableLocalObjectDelegate(
111             containerId, intfClass.getName(), delegate.getKey());
112     }
113     
114     public static final class SerializableLocalObjectDelegate
115         implements SerializableObjectFactory
116     {
117         private long containerId;
118         private String JavaDoc intfClassName;
119         private Object JavaDoc primaryKey;
120
121         SerializableLocalObjectDelegate(long containerId,
122                 String JavaDoc intfClassName, Object JavaDoc primaryKey) {
123             this.containerId = containerId;
124             this.intfClassName = intfClassName;
125             this.primaryKey = primaryKey;
126         }
127         
128         public Object JavaDoc createObject()
129             throws IOException JavaDoc
130         {
131             BaseContainer container = (BaseContainer) Switch.getSwitch().
132                 getContainerFactory().getContainer(containerId);
133             EJBLocalObjectImpl ejbLocalBusinessObjectImpl =
134                 container.getEJBLocalBusinessObjectImpl(primaryKey);
135             // Return the client EJBLocalObject.
136
return ejbLocalBusinessObjectImpl.getClientObject(intfClassName);
137         }
138     }
139 }
140
Popular Tags