PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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