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:
026: import javax.ejb.EJBHome;
027: import javax.ejb.EJBMetaData;
028: import javax.ejb.RemoveException;
029: import javax.ejb.Handle;
030: import javax.ejb.EJBHome;
031: import javax.ejb.EJBObject;
032: import javax.ejb.HomeHandle;
033:
034: import org.jboss.proxy.ejb.handle.HomeHandleImpl;
035: import org.jboss.invocation.Invocation;
036: import org.jboss.invocation.InvocationContext;
037: import org.jboss.invocation.InvocationKey;
038: import org.jboss.invocation.InvocationType;
039:
040: /**
041: * The client-side proxy for an EJB Home object.
042: *
043: * @author <a href="mailto:marc.fleury@jboss.org">Marc Fleury</a>
044: * @version $Revision: 57209 $
045: */
046: public class HomeInterceptor extends GenericEJBInterceptor {
047: /** Serial Version Identifier. @since 1.6 */
048: private static final long serialVersionUID = 1333656107035759718L;
049:
050: // Static --------------------------------------------------------
051:
052: protected static final Object[] EMPTY_ARGS = {};
053:
054: /** {@link EJBHome#getEJBMetaData} method reference. */
055: protected static final Method GET_EJB_META_DATA;
056:
057: /** {@link EJBHome#getHomeHandle} method reference. */
058: protected static final Method GET_HOME_HANDLE;
059:
060: /** {@link EJBHome#remove(Handle)} method reference. */
061: protected static final Method REMOVE_BY_HANDLE;
062:
063: /** {@link EJBHome#remove(Object)} method reference. */
064: protected static final Method REMOVE_BY_PRIMARY_KEY;
065:
066: /** {@link EJBObject#remove} method reference. */
067: protected static final Method REMOVE_OBJECT;
068:
069: static {
070: try {
071: final Class empty[] = {};
072: final Class type = EJBHome.class;
073:
074: GET_EJB_META_DATA = type.getMethod("getEJBMetaData", empty);
075: GET_HOME_HANDLE = type.getMethod("getHomeHandle", empty);
076: REMOVE_BY_HANDLE = type.getMethod("remove",
077: new Class[] { Handle.class });
078: REMOVE_BY_PRIMARY_KEY = type.getMethod("remove",
079: new Class[] { Object.class });
080:
081: // Get the "remove" method from the EJBObject
082: REMOVE_OBJECT = EJBObject.class.getMethod("remove", empty);
083: } catch (Exception e) {
084: e.printStackTrace();
085: throw new ExceptionInInitializerError(e);
086: }
087: }
088:
089: // Attributes ----------------------------------------------------
090:
091: // Constructors --------------------------------------------------
092:
093: /**
094: * No-argument constructor for externalization.
095: */
096: public HomeInterceptor() {
097: }
098:
099: // Public --------------------------------------------------------
100:
101: /**
102: * InvocationHandler implementation.
103: *
104: * @throws Throwable Any exception or error thrown while processing.
105: */
106: public Object invoke(Invocation invocation) throws Throwable {
107: InvocationContext ctx = invocation.getInvocationContext();
108:
109: Method m = invocation.getMethod();
110:
111: // Implement local methods
112: if (m.equals(TO_STRING)) {
113: return ctx.getValue(InvocationKey.JNDI_NAME).toString()
114: + "Home";
115: } else if (m.equals(EQUALS)) {
116: // equality of the proxy home is based on names...
117: Object[] args = invocation.getArguments();
118: String argsString = args[0] != null ? args[0].toString()
119: : "";
120: String this String = ctx.getValue(InvocationKey.JNDI_NAME)
121: .toString()
122: + "Home";
123: return new Boolean(this String.equals(argsString));
124: } else if (m.equals(HASH_CODE)) {
125: return new Integer(this .hashCode());
126: }
127:
128: // Implement local EJB calls
129: else if (m.equals(GET_HOME_HANDLE)) {
130: return new HomeHandleImpl((String) ctx
131: .getValue(InvocationKey.JNDI_NAME));
132: } else if (m.equals(GET_EJB_META_DATA)) {
133: return ctx.getValue(InvocationKey.EJB_METADATA);
134: } else if (m.equals(REMOVE_BY_HANDLE)) {
135: // First get the EJBObject
136: EJBObject object = ((Handle) invocation.getArguments()[0])
137: .getEJBObject();
138:
139: // remove the object from here
140: object.remove();
141:
142: // Return Void
143: return Void.TYPE;
144: } else if (m.equals(REMOVE_BY_PRIMARY_KEY)) {
145: // Session beans must throw RemoveException (EJB 1.1, 5.3.2)
146: if (((EJBMetaData) ctx.getValue(InvocationKey.EJB_METADATA))
147: .isSession())
148: throw new RemoveException(
149: "Session beans cannot be removed "
150: + "by primary key.");
151:
152: // The trick is simple we trick the container in believe it
153: // is a remove() on the instance
154: Object id = invocation.getArguments()[0];
155:
156: // Just override the Invocation going out
157: invocation.setId(id);
158: invocation.setType(InvocationType.REMOTE);
159: invocation.setMethod(REMOVE_OBJECT);
160: invocation.setArguments(EMPTY_ARGS);
161: return getNext().invoke(invocation);
162: }
163:
164: // If not taken care of, go on and call the container
165: else {
166:
167: invocation.setType(InvocationType.HOME);
168: // Create an Invocation
169: return getNext().invoke(invocation);
170: }
171: }
172: }
|