/jboss-as-7.1.1.Final/cmp/src/main/java/org/jboss/as/cmp/component/CmpProxyFactory.java
Java | 88 lines | 55 code | 9 blank | 24 comment | 11 complexity | cb420103b83f49bfefb7b676c3da7711 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.cmp.component;
24
25import java.lang.reflect.Method;
26import java.lang.reflect.Modifier;
27import java.util.Arrays;
28import java.util.concurrent.atomic.AtomicInteger;
29
30import javax.ejb.EntityBean;
31
32import org.jboss.invocation.proxy.MethodBodyCreator;
33import org.jboss.invocation.proxy.MethodIdentifier;
34import org.jboss.invocation.proxy.ProxyConfiguration;
35import org.jboss.invocation.proxy.ProxyFactory;
36
37/**
38 * @author John Bailey
39 */
40public class CmpProxyFactory extends ProxyFactory<EntityBean> {
41 private static final AtomicInteger PROXY_ID = new AtomicInteger(0);
42
43 public static ProxyFactory<EntityBean> createProxyFactory(final Class<EntityBean> beanClass, final Class<?>... viewClasses) {
44 final ProxyConfiguration<EntityBean> proxyConfiguration = new ProxyConfiguration<EntityBean>();
45 proxyConfiguration.setProxyName(beanClass.getName() + "$$$cmp" + PROXY_ID.incrementAndGet());
46 proxyConfiguration.setClassLoader(beanClass.getClassLoader());
47 proxyConfiguration.setProtectionDomain(beanClass.getProtectionDomain());
48 proxyConfiguration.setSuperClass(beanClass);
49 if (viewClasses != null) {
50 for (final Class<?> viewClass : viewClasses) {
51 if (viewClass != null) {
52 proxyConfiguration.addAdditionalInterface(viewClass);
53 }
54 }
55 }
56 proxyConfiguration.addAdditionalInterface(CmpProxy.class);
57 return new CmpProxyFactory(beanClass, proxyConfiguration);
58 }
59
60 private final Class<?> beanClass;
61
62 public CmpProxyFactory(final Class<?> beanClass, final ProxyConfiguration<EntityBean> proxyConfiguration) {
63 super(proxyConfiguration);
64 this.beanClass = beanClass;
65 }
66
67 protected boolean overrideMethod(final Method method, final MethodIdentifier identifier, final MethodBodyCreator creator) {
68 Class<?> c = beanClass;
69 boolean override = true;
70 boolean found = false;
71 while (c != null) {
72 for (final Method m : c.getDeclaredMethods()) {
73 if (m.getName().equals(method.getName()) &&
74 m.getReturnType().equals(method.getReturnType()) &&
75 Arrays.equals(m.getParameterTypes(), method.getParameterTypes())) {
76 override = Modifier.isAbstract(m.getModifiers());
77 found = true;
78 break;
79 }
80 }
81 if (found) {
82 break;
83 }
84 c = c.getSuperclass();
85 }
86 return override && super.overrideMethod(method, identifier, creator);
87 }
88}