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.ejb.plugins.local;
023:
024: import java.io.IOException;
025: import java.io.ObjectInputStream;
026: import java.io.ObjectOutputStream;
027: import java.io.Serializable;
028: import java.lang.reflect.Method;
029: import javax.ejb.EJBLocalObject;
030: import javax.naming.InitialContext;
031:
032: /** Abstract superclass of local interface proxies.
033:
034: @author <a href="mailto:docodan@mvcsoft.com">Daniel OConnor</a>
035: @author <a href="mailto:scott.stark@jboss.org">Scott Stark</a>
036: @version $Revision: 57209 $
037: */
038: public abstract class LocalProxy implements Serializable {
039: // Constants -----------------------------------------------------
040: static final long serialVersionUID = 8387750757101826407L;
041: // Attributes ----------------------------------------------------
042:
043: // Static --------------------------------------------------------
044:
045: /** An empty method parameter list. */
046: protected static final Object[] EMPTY_ARGS = {};
047:
048: /** {@link Object#toString} method reference. */
049: protected static final Method TO_STRING;
050:
051: /** {@link Object#hashCode} method reference. */
052: protected static final Method HASH_CODE;
053:
054: /** {@link Object#equals} method reference. */
055: protected static final Method EQUALS;
056:
057: /** {@link EJBLocalObject#getPrimaryKey} method reference. */
058: protected static final Method GET_PRIMARY_KEY;
059:
060: /** {@link EJBLocalObject#getEJBLocalHome} method reference. */
061: protected static final Method GET_EJB_HOME;
062:
063: /** {@link EJBLocalObject#isIdentical} method reference. */
064: protected static final Method IS_IDENTICAL;
065:
066: /** {@link EJBLocalObject#remove} method reference. */
067: protected static final Method REMOVE;
068:
069: protected String jndiName;
070: protected transient BaseLocalProxyFactory factory;
071:
072: /**
073: * Initialize {@link EJBLocalObject} method references.
074: */
075: static {
076: try {
077: final Class[] empty = {};
078: final Class type = EJBLocalObject.class;
079:
080: GET_PRIMARY_KEY = type.getMethod("getPrimaryKey", empty);
081: GET_EJB_HOME = type.getMethod("getEJBLocalHome", empty);
082: IS_IDENTICAL = type.getMethod("isIdentical",
083: new Class[] { type });
084: REMOVE = type.getMethod("remove", empty);
085: } catch (Exception e) {
086: e.printStackTrace();
087: throw new ExceptionInInitializerError(e);
088: }
089: }
090:
091: /**
092: * Initialize {@link Object} method references.
093: */
094: static {
095: try {
096: final Class[] empty = {};
097: final Class type = Object.class;
098:
099: TO_STRING = type.getMethod("toString", empty);
100: HASH_CODE = type.getMethod("hashCode", empty);
101: EQUALS = type.getMethod("equals", new Class[] { type });
102: } catch (Exception e) {
103: e.printStackTrace();
104: throw new ExceptionInInitializerError(e);
105: }
106: }
107:
108: protected String getJndiName() {
109: return jndiName;
110: }
111:
112: protected abstract Object getId();
113:
114: public LocalProxy(String jndiName, BaseLocalProxyFactory factory) {
115: this .jndiName = jndiName;
116: this .factory = factory;
117: }
118:
119: /**
120: * Test the identitiy of an <tt>EJBObject</tt>.
121: *
122: * @param a <tt>EJBObject</tt>.
123: * @param b Object to test identity with.
124: * @return True if objects are identical.
125: *
126: * @throws ClassCastException Not an EJBObject instance.
127: */
128: Boolean isIdentical(final Object a, final Object b) {
129: final EJBLocalObject ejb = (EJBLocalObject) a;
130: Boolean isIdentical = Boolean.FALSE;
131: if (ejb != null) {
132: isIdentical = new Boolean(ejb.toString().equals(b));
133: }
134: return isIdentical;
135: }
136:
137: /**
138: * Implementation of toString for EJBLocalObject.
139: * @return String representation of EJBLocalObject.
140: */
141: String toStringImpl() {
142: return jndiName + ":" + getId();
143: }
144:
145: public Object invoke(final Object proxy, final Method m,
146: Object[] args) throws Throwable {
147: Object id = getId();
148: Object retValue = null;
149:
150: // Implement local methods
151: if (m.equals(TO_STRING)) {
152: retValue = toStringImpl();
153: } else if (m.equals(EQUALS)) {
154: retValue = invoke(proxy, IS_IDENTICAL, args);
155: } else if (m.equals(HASH_CODE)) {
156: retValue = new Integer(id.hashCode());
157: }
158:
159: // Implement local EJB calls
160: else if (m.equals(GET_PRIMARY_KEY)) {
161: retValue = id;
162: } else if (m.equals(GET_EJB_HOME)) {
163: InitialContext ctx = new InitialContext();
164: return ctx.lookup(jndiName);
165: } else if (m.equals(IS_IDENTICAL)) {
166: retValue = isIdentical(args[0], toStringImpl());
167: }
168: return retValue;
169: }
170:
171: /** Restore the jndiName using default serialization and then lookup
172: the BaseLocalProxyFactory using the jndiName
173: */
174: private void readObject(ObjectInputStream in) throws IOException,
175: ClassNotFoundException {
176: in.defaultReadObject();
177: factory = (BaseLocalProxyFactory) BaseLocalProxyFactory.invokerMap
178: .get(jndiName);
179: }
180:
181: private void writeObject(ObjectOutputStream out) throws IOException {
182: out.defaultWriteObject();
183: }
184:
185: }
|