PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/jboss-as-7.1.1.Final/weld/src/main/java/org/jboss/as/weld/injection/WeldManagedReferenceFactory.java

#
Java | 150 lines | 104 code | 19 blank | 27 comment | 11 complexity | cfdc7a41db5dbcb9e785e50d7f8cfbf2 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2010, Red Hat Inc., and individual contributors as indicated
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * 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.weld.injection;
  23. import org.jboss.as.naming.ManagedReference;
  24. import org.jboss.as.naming.ManagedReferenceFactory;
  25. import org.jboss.as.weld.WeldContainer;
  26. import org.jboss.msc.service.Service;
  27. import org.jboss.msc.service.StartContext;
  28. import org.jboss.msc.service.StartException;
  29. import org.jboss.msc.service.StopContext;
  30. import org.jboss.msc.value.InjectedValue;
  31. import org.jboss.weld.ejb.spi.EjbDescriptor;
  32. import org.jboss.weld.manager.BeanManagerImpl;
  33. import javax.enterprise.context.spi.CreationalContext;
  34. import javax.enterprise.inject.spi.Bean;
  35. import java.util.HashMap;
  36. import java.util.Map;
  37. import java.util.Set;
  38. /**
  39. * Managed reference factory that can be used to create and inject components.
  40. *
  41. * @author Stuart Douglas
  42. */
  43. public class WeldManagedReferenceFactory implements ManagedReferenceFactory, Service<WeldManagedReferenceFactory> {
  44. private final Class<?> componentClass;
  45. private final InjectedValue<WeldContainer> weldContainer;
  46. private final String ejbName;
  47. private final Set<Class<?>> interceptorClasses;
  48. private final Map<Class<?>, WeldEEInjection> interceptorInjections = new HashMap<Class<?>, WeldEEInjection>();
  49. private final ClassLoader classLoader;
  50. private final String beanDeploymentArchiveId;
  51. private WeldEEInjection injectionTarget;
  52. private Bean<?> bean;
  53. private BeanManagerImpl beanManager;
  54. public WeldManagedReferenceFactory(Class<?> componentClass, String ejbName, final Set<Class<?>> interceptorClasses, final ClassLoader classLoader, final String beanDeploymentArchiveId) {
  55. this.componentClass = componentClass;
  56. this.ejbName = ejbName;
  57. this.beanDeploymentArchiveId = beanDeploymentArchiveId;
  58. this.weldContainer = new InjectedValue<WeldContainer>();
  59. this.interceptorClasses = interceptorClasses;
  60. this.classLoader = classLoader;
  61. }
  62. @Override
  63. public ManagedReference getReference() {
  64. final CreationalContext<?> ctx;
  65. if (bean == null) {
  66. ctx = beanManager.createCreationalContext(null);
  67. } else {
  68. ctx = beanManager.createCreationalContext(bean);
  69. }
  70. final Object instance = injectionTarget.produce(ctx);
  71. return new WeldManagedReference(ctx, instance, injectionTarget, interceptorInjections);
  72. }
  73. public ManagedReference injectExistingReference(final ManagedReference existing) {
  74. final CreationalContext<?> ctx;
  75. if (bean == null) {
  76. ctx = beanManager.createCreationalContext(null);
  77. } else {
  78. ctx = beanManager.createCreationalContext(bean);
  79. }
  80. final Object instance = existing.getInstance();
  81. injectionTarget.inject(instance, ctx);
  82. return new ManagedReference() {
  83. @Override
  84. public void release() {
  85. try {
  86. existing.release();
  87. } finally {
  88. ctx.release();
  89. }
  90. }
  91. @Override
  92. public Object getInstance() {
  93. return instance;
  94. }
  95. };
  96. }
  97. @Override
  98. public synchronized void start(final StartContext context) throws StartException {
  99. final ClassLoader cl = SecurityActions.getContextClassLoader();
  100. try {
  101. SecurityActions.setContextClassLoader(classLoader);
  102. beanManager = (BeanManagerImpl) weldContainer.getValue().getBeanManager(beanDeploymentArchiveId);
  103. for (final Class<?> interceptor : interceptorClasses) {
  104. interceptorInjections.put(interceptor, WeldEEInjection.createWeldEEInjection(interceptor, null, beanManager));
  105. }
  106. if (ejbName != null) {
  107. EjbDescriptor<Object> descriptor = beanManager.getEjbDescriptor(ejbName);
  108. //may happen if the EJB was vetoed
  109. if (descriptor != null) {
  110. bean = beanManager.getBean(descriptor);
  111. }
  112. }
  113. injectionTarget = WeldEEInjection.createWeldEEInjection(componentClass, bean, beanManager);
  114. } finally {
  115. SecurityActions.setContextClassLoader(cl);
  116. }
  117. }
  118. @Override
  119. public synchronized void stop(final StopContext context) {
  120. injectionTarget = null;
  121. interceptorInjections.clear();
  122. bean = null;
  123. }
  124. @Override
  125. public synchronized WeldManagedReferenceFactory getValue() throws IllegalStateException, IllegalArgumentException {
  126. return this;
  127. }
  128. public InjectedValue<WeldContainer> getWeldContainer() {
  129. return weldContainer;
  130. }
  131. }