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 org.jboss.invocation.Invocation;
027: import org.jboss.invocation.InvocationContext;
028: import org.jboss.invocation.InvocationKey;
029: import org.jboss.invocation.InvocationType;
030: import org.jboss.proxy.ejb.handle.EntityHandleImpl;
031:
032: /**
033: * An EJB entity bean proxy class.
034: * @author <a href="mailto:marc.fleury@jboss.org">Marc Fleury</a>
035: * @version $Revision: 57209 $
036: */
037: public class EntityInterceptor extends GenericEJBInterceptor {
038: /** Serial Version Identifier. @since 1.6 */
039: private static final long serialVersionUID = 4399705304832568350L;
040:
041: /**
042: * No-argument constructor for externalization.
043: */
044: public EntityInterceptor() {
045: }
046:
047: // Public --------------------------------------------------------
048:
049: /**
050: * InvocationHandler implementation.
051: *
052: * @param proxy The proxy object.
053: * @param m The method being invoked.
054: * @param args The arguments for the method.
055: *
056: * @throws Throwable Any exception or error thrown while processing.
057: */
058: public Object invoke(Invocation invocation) throws Throwable {
059: InvocationContext ctx = invocation.getInvocationContext();
060:
061: Method m = invocation.getMethod();
062:
063: // Implement local methods
064: if (m.equals(TO_STRING)) {
065: return toString(ctx);
066: } else if (m.equals(EQUALS)) {
067: Object[] args = invocation.getArguments();
068: String argsString = args[0] != null ? args[0].toString()
069: : "";
070: String this String = toString(ctx);
071: return new Boolean(this String.equals(argsString));
072: } else if (m.equals(HASH_CODE)) {
073: return new Integer(ctx.getCacheId().hashCode());
074: }
075: // Implement local EJB calls
076: else if (m.equals(GET_HANDLE)) {
077: String jndiName = (String) ctx
078: .getValue(InvocationKey.JNDI_NAME);
079: Object id = ctx.getCacheId();
080: return new EntityHandleImpl(jndiName, id);
081: } else if (m.equals(GET_PRIMARY_KEY)) {
082: return ctx.getCacheId();
083: } else if (m.equals(GET_EJB_HOME)) {
084: return getEJBHome(invocation);
085: } else if (m.equals(IS_IDENTICAL)) {
086: Object[] args = invocation.getArguments();
087: String argsString = args[0].toString();
088: String this String = toString(ctx);
089: return new Boolean(this String.equals(argsString));
090: }
091: // If not taken care of, go on and call the container
092: else {
093: // We are a Remote invocation
094: invocation.setType(InvocationType.REMOTE);
095: // We pertain to this ID (represented by cache ID)
096: invocation.setId(ctx.getCacheId());
097: return getNext().invoke(invocation);
098: }
099: }
100:
101: // Package protected ---------------------------------------------
102:
103: // Protected -----------------------------------------------------//////
104: private String toString(InvocationContext ctx) {
105: return ctx.getValue(InvocationKey.JNDI_NAME) + ":"
106: + ctx.getCacheId().toString();
107: }
108:
109: }
|