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 javax.ejb.EJBLocalHome;
025: import javax.ejb.EJBLocalObject;
026: import javax.ejb.RemoveException;
027: import java.lang.reflect.InvocationHandler;
028: import java.lang.reflect.Method;
029:
030: /**
031: * The proxy for an EJBLocalHome object.
032: *
033: * @author <a href="mailto:docodan@mvcsoft.com">Daniel OConnor</a>
034: * @author <a href="mailto:scott.stark@jboss.org">Scott Stark</a>
035: * @version $Revision: 57209 $
036: */
037: public class LocalHomeProxy extends LocalProxy implements
038: InvocationHandler {
039: static final long serialVersionUID = 1762319499924478521L;
040:
041: /** {@link javax.ejb.EJBHome#remove(Object)} method reference. */
042: protected static final Method REMOVE_BY_PRIMARY_KEY;
043:
044: /** {@link javax.ejb.EJBObject#remove} method reference. */
045: protected static final Method REMOVE_OBJECT;
046:
047: /**
048: * Initialize {@link javax.ejb.EJBHome} and {@link javax.ejb.EJBObject} method references.
049: */
050: static {
051: try {
052: final Class empty[] = {};
053: final Class type = EJBLocalHome.class;
054:
055: REMOVE_BY_PRIMARY_KEY = type.getMethod("remove",
056: new Class[] { Object.class });
057:
058: // Get the "remove" method from the EJBObject
059: REMOVE_OBJECT = EJBLocalObject.class.getMethod("remove",
060: empty);
061: } catch (Exception e) {
062: e.printStackTrace();
063: throw new ExceptionInInitializerError(e);
064: }
065: }
066:
067: public LocalHomeProxy(String jndiName, BaseLocalProxyFactory factory) {
068: super (jndiName, factory);
069: }
070:
071: protected Object getId() {
072: return jndiName;
073: }
074:
075: /**
076: * InvocationHandler implementation.
077: *
078: * @param proxy The proxy object.
079: * @param m The method being invoked.
080: * @param args The arguments for the method.
081: *
082: * @throws Throwable Any exception or error thrown while processing.
083: */
084: public Object invoke(final Object proxy, final Method m,
085: Object[] args) throws Throwable {
086: Object retValue = null;
087:
088: if (args == null)
089: args = EMPTY_ARGS;
090:
091: // Implement local methods
092: if (m.equals(TO_STRING)) {
093: retValue = jndiName + "Home";
094: } else if (m.equals(EQUALS)) {
095: // equality of the proxy home is based on names...
096: Object temp = invoke(proxy, TO_STRING, args);
097: retValue = new Boolean(temp.equals(jndiName + "Home"));
098: } else if (m.equals(HASH_CODE)) {
099: retValue = new Integer(this .hashCode());
100: } else if (m.equals(REMOVE_BY_PRIMARY_KEY)) {
101: try {
102: // The trick is simple we trick the container in believe it
103: // is a remove() on the instance
104: Object id = args[0];
105: retValue = factory
106: .invoke(id, REMOVE_OBJECT, EMPTY_ARGS);
107: } catch (Exception e) {
108: RemoveException re = new RemoveException(e.getMessage());
109: re.initCause(e);
110: throw re;
111: }
112: }
113: // If not taken care of, go on and call the container
114: else {
115: retValue = factory.invokeHome(m, args);
116: }
117:
118: return retValue;
119: }
120: }
|