PageRenderTime 31ms CodeModel.GetById 20ms app.highlight 10ms RepoModel.GetById 0ms 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 */
 22package org.jboss.as.weld.injection;
 23
 24import org.jboss.as.naming.ManagedReference;
 25import org.jboss.as.naming.ManagedReferenceFactory;
 26import org.jboss.as.weld.WeldContainer;
 27import org.jboss.msc.service.Service;
 28import org.jboss.msc.service.StartContext;
 29import org.jboss.msc.service.StartException;
 30import org.jboss.msc.service.StopContext;
 31import org.jboss.msc.value.InjectedValue;
 32import org.jboss.weld.ejb.spi.EjbDescriptor;
 33import org.jboss.weld.manager.BeanManagerImpl;
 34
 35import javax.enterprise.context.spi.CreationalContext;
 36import javax.enterprise.inject.spi.Bean;
 37import java.util.HashMap;
 38import java.util.Map;
 39import java.util.Set;
 40
 41/**
 42 * Managed reference factory that can be used to create and inject components.
 43 *
 44 * @author Stuart Douglas
 45 */
 46public class WeldManagedReferenceFactory implements ManagedReferenceFactory, Service<WeldManagedReferenceFactory> {
 47
 48    private final Class<?> componentClass;
 49    private final InjectedValue<WeldContainer> weldContainer;
 50    private final String ejbName;
 51    private final Set<Class<?>> interceptorClasses;
 52    private final Map<Class<?>, WeldEEInjection> interceptorInjections = new HashMap<Class<?>, WeldEEInjection>();
 53    private final ClassLoader classLoader;
 54    private final String beanDeploymentArchiveId;
 55
 56    private WeldEEInjection injectionTarget;
 57    private Bean<?> bean;
 58    private BeanManagerImpl beanManager;
 59
 60    public WeldManagedReferenceFactory(Class<?> componentClass, String ejbName, final Set<Class<?>> interceptorClasses, final ClassLoader classLoader, final String beanDeploymentArchiveId) {
 61        this.componentClass = componentClass;
 62        this.ejbName = ejbName;
 63        this.beanDeploymentArchiveId = beanDeploymentArchiveId;
 64        this.weldContainer = new InjectedValue<WeldContainer>();
 65        this.interceptorClasses = interceptorClasses;
 66        this.classLoader = classLoader;
 67    }
 68
 69    @Override
 70    public ManagedReference getReference() {
 71        final CreationalContext<?> ctx;
 72        if (bean == null) {
 73            ctx = beanManager.createCreationalContext(null);
 74        } else {
 75            ctx = beanManager.createCreationalContext(bean);
 76        }
 77        final Object instance = injectionTarget.produce(ctx);
 78        return new WeldManagedReference(ctx, instance, injectionTarget, interceptorInjections);
 79    }
 80
 81    public ManagedReference injectExistingReference(final ManagedReference existing) {
 82        final CreationalContext<?> ctx;
 83        if (bean == null) {
 84            ctx = beanManager.createCreationalContext(null);
 85        } else {
 86            ctx = beanManager.createCreationalContext(bean);
 87        }
 88        final Object instance = existing.getInstance();
 89
 90        injectionTarget.inject(instance, ctx);
 91
 92        return new ManagedReference() {
 93            @Override
 94            public void release() {
 95                try {
 96                    existing.release();
 97                } finally {
 98                    ctx.release();
 99                }
100            }
101
102            @Override
103            public Object getInstance() {
104                return instance;
105            }
106        };
107    }
108
109    @Override
110    public synchronized void start(final StartContext context) throws StartException {
111        final ClassLoader cl = SecurityActions.getContextClassLoader();
112        try {
113            SecurityActions.setContextClassLoader(classLoader);
114            beanManager = (BeanManagerImpl) weldContainer.getValue().getBeanManager(beanDeploymentArchiveId);
115
116            for (final Class<?> interceptor : interceptorClasses) {
117                interceptorInjections.put(interceptor, WeldEEInjection.createWeldEEInjection(interceptor, null, beanManager));
118            }
119
120            if (ejbName != null) {
121                EjbDescriptor<Object> descriptor = beanManager.getEjbDescriptor(ejbName);
122                //may happen if the EJB was vetoed
123                if (descriptor != null) {
124                    bean = beanManager.getBean(descriptor);
125                }
126            }
127            injectionTarget = WeldEEInjection.createWeldEEInjection(componentClass, bean, beanManager);
128
129        } finally {
130            SecurityActions.setContextClassLoader(cl);
131        }
132
133    }
134
135    @Override
136    public synchronized void stop(final StopContext context) {
137        injectionTarget = null;
138        interceptorInjections.clear();
139        bean = null;
140    }
141
142    @Override
143    public synchronized WeldManagedReferenceFactory getValue() throws IllegalStateException, IllegalArgumentException {
144        return this;
145    }
146
147    public InjectedValue<WeldContainer> getWeldContainer() {
148        return weldContainer;
149    }
150}