|
Glassfish example source code file (InjectionManagerImpl.java)
The Glassfish InjectionManagerImpl.java source code/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License"). You * may not use this file except in compliance with the License. You can * obtain a copy of the License at * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html * or packager/legal/LICENSE.txt. See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file at packager/legal/LICENSE.txt. * * GPL Classpath Exception: * Oracle designates this particular file as subject to the "Classpath" * exception as provided by Oracle in the GPL Version 2 section of the License * file that accompanied this code. * * Modifications: * If applicable, add the following below the License Header, with the fields * enclosed by brackets [] replaced by your own identifying information: * "Portions Copyright [year] [name of copyright owner]" * * Contributor(s): * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding "[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license." If you don't indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above. However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. */ package com.sun.enterprise.container.common.impl.util; import com.sun.enterprise.container.common.spi.util.ComponentEnvManager; import com.sun.enterprise.container.common.spi.util.InjectionException; import com.sun.enterprise.container.common.spi.util.InjectionManager; import com.sun.enterprise.container.common.spi.ManagedBeanManager; import com.sun.enterprise.container.common.spi.JCDIService; import com.sun.enterprise.deployment.InjectionCapable; import com.sun.enterprise.deployment.InjectionInfo; import com.sun.enterprise.deployment.InjectionTarget; import com.sun.enterprise.deployment.JndiNameEnvironment; import com.sun.enterprise.util.LocalStringManagerImpl; import com.sun.enterprise.deployment.BundleDescriptor; import com.sun.enterprise.deployment.EjbDescriptor; import org.glassfish.api.invocation.ComponentInvocation; import org.glassfish.api.invocation.InvocationManager; import org.glassfish.api.naming.GlassfishNamingManager; import org.jvnet.hk2.annotations.Inject; import org.jvnet.hk2.annotations.Service; import javax.naming.InitialContext; import javax.naming.NamingException; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.LinkedList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.ManagedBean; import java.lang.reflect.Constructor; import org.glassfish.api.admin.ProcessEnvironment; import org.glassfish.api.admin.ProcessEnvironment.ProcessType; import org.jvnet.hk2.component.Habitat; import org.jvnet.hk2.component.PostConstruct; /** * Implementation of InjectionManager. * * @author Kenneth Saks */ @Service public class InjectionManagerImpl implements InjectionManager, PostConstruct { @Inject private Logger _logger; static private LocalStringManagerImpl localStrings = new LocalStringManagerImpl(InjectionManagerImpl.class); @Inject private ComponentEnvManager compEnvManager; @Inject private InvocationManager invocationMgr; @Inject private GlassfishNamingManager glassfishNamingManager; @Inject private Habitat habitat; @Inject private ProcessEnvironment processEnv; public void postConstruct() { // When in the server, register in JNDI to allow container code without // compile-time dependency on GlassFish to use injection services. // We know GlassFishNaming manager is available because it's an injected field. if( processEnv.getProcessType().isServer() ) { try { glassfishNamingManager.publishObject("com.sun.enterprise.container.common.spi.util.InjectionManager", this, true); } catch(NamingException ne) { throw new RuntimeException(ne); } } } public void injectInstance(Object instance) throws InjectionException { injectInstance(instance, true); } public void injectInstance(Object instance, boolean invokePostConstruct) throws InjectionException { ComponentInvocation inv = invocationMgr.getCurrentInvocation(); if( inv != null ) { JndiNameEnvironment componentEnv = compEnvManager.getJndiNameEnvironment(inv.getComponentId()); if( componentEnv != null ) { inject(instance.getClass(), instance, componentEnv, null, invokePostConstruct); } else { throw new InjectionException("No descriptor registered for " + " current invocation : " + inv); } } else { throw new InjectionException("null invocation context"); } } public void injectInstance(Object instance, JndiNameEnvironment componentEnv) throws InjectionException { inject(instance.getClass(), instance, componentEnv, null, true); } public void injectInstance(Object instance, JndiNameEnvironment componentEnv, boolean invokePostConstruct) throws InjectionException { inject(instance.getClass(), instance, componentEnv, null, invokePostConstruct); } public void injectInstance(Object instance, String componentId, boolean invokePostConstruct) throws InjectionException { ComponentInvocation inv = invocationMgr.getCurrentInvocation(); if( inv != null ) { JndiNameEnvironment componentEnv = compEnvManager.getJndiNameEnvironment(componentId); if( componentEnv != null ) { inject(instance.getClass(), instance, componentEnv, componentId, invokePostConstruct); } else { throw new InjectionException("No descriptor registered for " + " componentId : " + componentId); } } else { throw new InjectionException("null invocation context"); } } public void injectClass(Class clazz, JndiNameEnvironment componentEnv) throws InjectionException { injectClass(clazz, componentEnv, true); } public void injectClass(Class clazz, JndiNameEnvironment componentEnv, boolean invokePostConstruct) throws InjectionException { inject(clazz, null, componentEnv, null, invokePostConstruct); } public void invokeInstancePreDestroy(Object instance, JndiNameEnvironment componentEnv) throws InjectionException { invokePreDestroy(instance.getClass(), instance, componentEnv); } public void invokeInstancePostConstruct(Object instance, JndiNameEnvironment componentEnv) throws InjectionException { invokePostConstruct(instance.getClass(), instance, componentEnv); } public void invokeInstancePreDestroy(Object instance) throws InjectionException { ComponentInvocation inv = invocationMgr.getCurrentInvocation(); if( inv != null ) { JndiNameEnvironment componentEnv = compEnvManager.getJndiNameEnvironment(inv.getComponentId()); if( componentEnv != null ) { invokePreDestroy(instance.getClass(), instance, componentEnv); } else { throw new InjectionException("No descriptor registered for " + " current invocation : " + inv); } } else { throw new InjectionException("null invocation context"); } } public void invokeClassPreDestroy(Class clazz, JndiNameEnvironment componentEnv) throws InjectionException { invokePreDestroy(clazz, null, componentEnv); } /** * Create a managed object for the given class. The object will be * injected and any PostConstruct methods will be called. The returned * object can be cast to the clazz type but is not necessarily a direct * reference to the managed instance. All invocations on the returned * object should be on its public methods. * * It is the responsibility of the caller to destroy the returned object * by calling destroyManagedObject(Object managedObject). * * @param clazz Class to be instantiated * @return managed object * @throws InjectionException */ public Object createManagedObject(Class clazz) throws InjectionException { Object managedObject = null; try { ManagedBean managedBeanAnn = (ManagedBean) clazz.getAnnotation(ManagedBean.class); ManagedBeanManager managedBeanMgr = habitat.getByContract(ManagedBeanManager.class); if( managedBeanAnn != null ) { // EE style @ManagedBean // Create , inject, and call PostConstruct via managed bean manager managedObject = managedBeanMgr.createManagedBean(clazz); } else { JCDIService jcdiService = habitat.getByContract(JCDIService.class); if( (jcdiService != null) && jcdiService.isCurrentModuleJCDIEnabled() ) { // Create , inject, and call PostConstruct via managed bean manager managedObject = managedBeanMgr.createManagedBean(clazz); } else { // Not in a 299-enabled module and not annoated with @ManagedBean, so // just instantiate using new and perform injection Constructor noArgCtor = clazz.getConstructor(); managedObject = noArgCtor.newInstance(); // Inject and call PostConstruct injectInstance(managedObject); } } } catch(Exception e) { throw new InjectionException("Error creating managed object for " + clazz, e); } return managedObject; } /** * Create a managed object for the given class. The object will be * injected and if invokePostConstruct is true, any @PostConstruct * methods on the instance's class(and super-classes) will be invoked * after injection. The returned * object can be cast to the clazz type but is not necessarily a direct * reference to the managed instance. All invocations on the returned * object should be on its public methods. * * It is the responsibility of the caller to destroy the returned object * by calling destroyManagedObject(Object managedObject). * * @param clazz Class to be instantiated * @param invokePostConstruct if true, invoke any @PostConstruct methods * on the instance's class(and super-classes) after injection. * @return managed object * @throws InjectionException */ public Object createManagedObject(Class clazz, boolean invokePostConstruct) throws InjectionException { Object managedObject = null; try { ManagedBean managedBeanAnn = (ManagedBean) clazz.getAnnotation(ManagedBean.class); ManagedBeanManager managedBeanMgr = habitat.getByContract(ManagedBeanManager.class); if( managedBeanAnn != null ) { // EE style @ManagedBean // Create , inject, and call PostConstruct (if necessary) via managed bean manager managedObject = managedBeanMgr.createManagedBean(clazz, invokePostConstruct); } else { JCDIService jcdiService = habitat.getByContract(JCDIService.class); if( (jcdiService != null) && jcdiService.isCurrentModuleJCDIEnabled() ) { // Create , inject, and call PostConstruct (if necessary) via managed bean manager managedObject = managedBeanMgr.createManagedBean(clazz, invokePostConstruct); } else { // Not in a 299-enabled module and not annoated with @ManagedBean, so // just instantiate using new and perform injection Constructor noArgCtor = clazz.getConstructor(); managedObject = noArgCtor.newInstance(); // Inject and call PostConstruct if necessary injectInstance(managedObject, invokePostConstruct); } } } catch(Exception e) { throw new InjectionException("Error creating managed object for " + clazz, e); } return managedObject; } /** * Destroy a managed object that was created via createManagedObject. Any * PreDestroy methods will be called. * * @param managedObject * @throws InjectionException */ public void destroyManagedObject(Object managedObject) throws InjectionException { destroyManagedObject(managedObject, true); } /** * Destroy a managed object that was created via createManagedObject. Any * PreDestroy methods will be called. * * @param managedObject * @param validate if false, do nothing if the instance is not registred * @throws InjectionException */ public void destroyManagedObject(Object managedObject, boolean validate) throws InjectionException { Class managedObjectClass = managedObject.getClass(); ManagedBean managedBeanAnn = (ManagedBean) managedObjectClass.getAnnotation(ManagedBean.class); ManagedBeanManager managedBeanMgr = habitat.getByContract(ManagedBeanManager.class); JCDIService jcdiService = habitat.getByContract(JCDIService.class); if( (jcdiService != null) && jcdiService.isCurrentModuleJCDIEnabled() ) { // If 299-enabled always delegate to managed bean manager managedBeanMgr.destroyManagedBean(managedObject, validate); } else { // If the object's class has @ManagedBean it's a managed bean. Otherwise, ask // managed bean manager. boolean isManagedBean = (managedBeanAnn != null) || managedBeanMgr.isManagedBean(managedObject); if( isManagedBean ) { managedBeanMgr.destroyManagedBean(managedObject, validate); } else { this.invokeInstancePreDestroy(managedObject); } } } /** * @param instance Target instance for injection, or null if injection * is class-based. Any error encountered during any * portion of injection is propagated immediately. */ private void inject(final Class clazz, final Object instance, JndiNameEnvironment envDescriptor, String componentId, boolean invokePostConstruct) throws InjectionException { LinkedList<Method> postConstructMethods = new LinkedList Other Glassfish examples (source code examples)Here is a short list of links related to this Glassfish InjectionManagerImpl.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.