/jboss-as-7.1.1.Final/ejb3/src/main/java/org/jboss/as/ejb3/component/entity/interceptors/EntityBeanIsIdenticalInterceptorFactory.java
Java | 84 lines | 45 code | 12 blank | 27 comment | 3 complexity | 344a93acca34b538b40ee4aaec390d88 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
1/*
2 * JBoss, Home of Professional Open Source.
3 * Copyright 2011, Red Hat, Inc., and individual contributors
4 * as indicated by the @author tags. See the copyright.txt file in the
5 * distribution for a full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */
22
23package org.jboss.as.ejb3.component.entity.interceptors;
24
25import javax.ejb.EJBLocalObject;
26import javax.ejb.EJBObject;
27
28import org.jboss.as.ee.component.ComponentInstance;
29import org.jboss.as.ee.component.ComponentView;
30import org.jboss.as.ejb3.component.entity.EntityBeanComponent;
31import org.jboss.as.ejb3.component.entity.EntityBeanComponentInstance;
32import org.jboss.invocation.Interceptor;
33import org.jboss.invocation.InterceptorContext;
34import org.jboss.invocation.InterceptorFactory;
35import org.jboss.invocation.InterceptorFactoryContext;
36
37/**
38 * Interceptor that handles the {@link javax.ejb.EJBLocalObject#isIdentical(javax.ejb.EJBLocalObject)}
39 * && {@link javax.ejb.EJBObject#isIdentical(javax.ejb.EJBObject)} methods
40 *
41 * @author Stuart Douglas
42 */
43public class EntityBeanIsIdenticalInterceptorFactory implements InterceptorFactory {
44
45 public static final EntityBeanIsIdenticalInterceptorFactory INSTANCE = new EntityBeanIsIdenticalInterceptorFactory();
46
47 private EntityBeanIsIdenticalInterceptorFactory() {
48 }
49
50 @Override
51 public Interceptor create(final InterceptorFactoryContext context) {
52 final ComponentView componentView = (ComponentView) context.getContextData().get(ComponentView.class);
53 return new EntityIsIdenticalInterceptor(componentView);
54
55 }
56
57 private class EntityIsIdenticalInterceptor implements Interceptor {
58
59 private final ComponentView componentView;
60
61 public EntityIsIdenticalInterceptor(final ComponentView componentView) {
62 this.componentView = componentView;
63 }
64
65 @Override
66 public Object processInvocation(final InterceptorContext context) throws Exception {
67 final EntityBeanComponentInstance instance = (EntityBeanComponentInstance) context.getPrivateData(ComponentInstance.class);
68 try {
69 final Object primaryKey = context.getPrivateData(EntityBeanComponent.PRIMARY_KEY_CONTEXT_KEY);
70 final Object other = context.getParameters()[0];
71 if (!componentView.getViewClass().isAssignableFrom(other.getClass())) {
72 return false;
73 }
74 if (context.getMethod().getParameterTypes()[0].equals(EJBLocalObject.class)) {
75 return ((EJBLocalObject) other).getPrimaryKey().equals(primaryKey);
76 } else {
77 return ((EJBObject) other).getPrimaryKey().equals(primaryKey);
78 }
79 } finally {
80 instance.getComponent().getCache().release(instance, true);
81 }
82 }
83 }
84}