com.sun.enterprise.container.common.impl.managedbean.ManagedBeanManagerImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of payara-micro Show documentation
Show all versions of payara-micro Show documentation
Micro Distribution of the Payara Project for IBM JDK
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2013 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.
*/
// Portions Copyright [2016] [Payara Foundation]
package com.sun.enterprise.container.common.impl.managedbean;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.inject.Inject;
import javax.naming.InitialContext;
import org.glassfish.api.admin.ProcessEnvironment;
import org.glassfish.api.admin.ProcessEnvironment.ProcessType;
import org.glassfish.api.event.EventListener;
import org.glassfish.api.event.Events;
import org.glassfish.api.naming.GlassfishNamingManager;
import org.glassfish.api.naming.NamingObjectProxy;
import org.glassfish.hk2.api.PostConstruct;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.runlevel.RunLevel;
import org.glassfish.internal.api.PostStartupRunLevel;
import org.glassfish.internal.data.ApplicationInfo;
import org.glassfish.internal.deployment.Deployment;
import org.jvnet.hk2.annotations.Service;
import com.sun.enterprise.container.common.spi.InterceptorInvoker;
import com.sun.enterprise.container.common.spi.JCDIService;
import com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilder;
import com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilderFactory;
import com.sun.enterprise.container.common.spi.ManagedBeanManager;
import com.sun.enterprise.container.common.spi.util.ComponentEnvManager;
import com.sun.enterprise.container.common.spi.util.InjectionManager;
import com.sun.enterprise.container.common.spi.util.InterceptorInfo;
import com.sun.enterprise.deployment.Application;
import com.sun.enterprise.deployment.ApplicationClientDescriptor;
import com.sun.enterprise.deployment.BundleDescriptor;
import com.sun.enterprise.deployment.EjbBundleDescriptor;
import com.sun.enterprise.deployment.EjbDescriptor;
import com.sun.enterprise.deployment.JndiNameEnvironment;
import com.sun.enterprise.deployment.LifecycleCallbackDescriptor;
import com.sun.enterprise.deployment.ManagedBeanDescriptor;
import com.sun.enterprise.deployment.WebBundleDescriptor;
import com.sun.logging.LogDomains;
/**
*/
@Service(name="ManagedBeanManagerImpl")
@RunLevel(value=PostStartupRunLevel.VAL, mode=RunLevel.RUNLEVEL_MODE_NON_VALIDATING)
public class ManagedBeanManagerImpl implements ManagedBeanManager, PostConstruct, EventListener {
private static final Logger _logger = LogDomains.getLogger(ManagedBeanManagerImpl.class,
LogDomains.CORE_LOGGER, false);
@Inject
private ComponentEnvManager compEnvManager;
@Inject
private InjectionManager injectionMgr;
@Inject
private GlassfishNamingManager namingManager;
@Inject
private ServiceLocator habitat;
@Inject
private Events events;
@Inject
private ProcessEnvironment processEnv;
private ProcessType processType;
// Keep track of contexts for managed bean instances instantiated via JCDI
private Map> jcdiManagedBeanInstanceMap =
new HashMap>();
// Used to hold managed beans in app client container
private Map appClientManagedBeans = new HashMap();
public void postConstruct() {
events.register(this);
processType = processEnv.getProcessType();
}
public void event(Event event) {
if (event.is(Deployment.APPLICATION_LOADED) ) {
ApplicationInfo info = Deployment.APPLICATION_LOADED.getHook(event);
loadManagedBeans(info);
registerAppLevelDependencies(info);
} else if( event.is(Deployment.APPLICATION_UNLOADED) ) {
ApplicationInfo info = Deployment.APPLICATION_UNLOADED.getHook(event);
Application app = info.getMetaData(Application.class);
doCleanup(app);
} else if( event.is(Deployment.DEPLOYMENT_FAILURE) ) {
Application app = Deployment.DEPLOYMENT_FAILURE.getHook(event).getModuleMetaData(Application.class);
doCleanup(app);
}
}
private void doCleanup(Application app) {
if( app != null ) {
unloadManagedBeans(app);
unregisterAppLevelDependencies(app);
}
}
private void registerAppLevelDependencies(ApplicationInfo appInfo) {
Application app = appInfo.getMetaData(Application.class);
if( app == null ) {
return;
}
try {
compEnvManager.bindToComponentNamespace(app);
} catch(Exception e) {
throw new RuntimeException("Error binding app-level env dependencies " +
app.getAppName(), e);
}
}
private void unregisterAppLevelDependencies(Application app) {
if( app != null ) {
try {
compEnvManager.unbindFromComponentNamespace(app);
} catch(Exception e) {
_logger.log(Level.FINE, "Exception unbinding app objects", e);
}
}
}
private void loadManagedBeans(ApplicationInfo appInfo) {
Application app = appInfo.getMetaData(Application.class);
if( app == null ) {
return;
}
loadManagedBeans(app);
}
public void loadManagedBeans(Application app) {
JCDIService jcdiService = habitat.getService(JCDIService.class);
for(BundleDescriptor bundle : app.getBundleDescriptors()) {
if (!bundleEligible(bundle)) {
continue;
}
boolean validationRequired = (bundle instanceof EjbBundleDescriptor)
|| (bundle instanceof ApplicationClientDescriptor);
boolean isCDIBundle = (jcdiService != null && jcdiService.isJCDIEnabled(bundle));
for(ManagedBeanDescriptor next : bundle.getManagedBeans()) {
try {
// TODO Should move this to regular DOL processing stage
if( validationRequired ) {
next.validate();
}
Set interceptorClasses = next.getAllInterceptorClasses();
Class targetClass = bundle.getClassLoader().loadClass(next.getBeanClassName());
InterceptorInfo interceptorInfo = new InterceptorInfo();
interceptorInfo.setTargetClass(targetClass);
interceptorInfo.setInterceptorClassNames(interceptorClasses);
interceptorInfo.setAroundConstructInterceptors
(next.getAroundConstructCallbackInterceptors(targetClass,
getConstructor(targetClass, isCDIBundle)));
interceptorInfo.setPostConstructInterceptors
(next.getCallbackInterceptors(LifecycleCallbackDescriptor.CallbackType.POST_CONSTRUCT));
interceptorInfo.setPreDestroyInterceptors
(next.getCallbackInterceptors(LifecycleCallbackDescriptor.CallbackType.PRE_DESTROY));
if( next.hasAroundInvokeMethod() ) {
interceptorInfo.setHasTargetClassAroundInvoke(true);
}
Map interceptorChains = new HashMap();
for(Method m : targetClass.getMethods()) {
interceptorChains.put(m, next.getAroundInvokeInterceptors(m) );
}
interceptorInfo.setAroundInvokeInterceptorChains(interceptorChains);
// TODO can optimize this out for the non-JAXRS, non-application specified interceptor case
interceptorInfo.setSupportRuntimeDelegate(true);
JavaEEInterceptorBuilderFactory interceptorBuilderFactory =
habitat.getService(JavaEEInterceptorBuilderFactory.class);
JavaEEInterceptorBuilder builder = interceptorBuilderFactory.createBuilder(interceptorInfo);
next.setInterceptorBuilder(builder);
compEnvManager.bindToComponentNamespace(next);
String jndiName = next.getGlobalJndiName();
ManagedBeanNamingProxy namingProxy =
new ManagedBeanNamingProxy(next, habitat);
if( processType.isServer() ) {
namingManager.publishObject(jndiName, namingProxy, true);
} else {
// Can't store them in server's global naming service so keep
// them in local map.
appClientManagedBeans.put(jndiName, namingProxy);
}
} catch(Exception e) {
throw new RuntimeException("Error binding ManagedBean " + next.getBeanClassName() +
" with name = " + next.getName(), e);
}
}
jcdiManagedBeanInstanceMap.put(bundle,
Collections.synchronizedMap(new HashMap
© 2015 - 2024 Weber Informatics LLC | Privacy Policy