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
/*
* 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-2021] [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.security.AccessController;
import java.security.PrivilegedExceptionAction;
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 jakarta.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.InterceptorDescriptor;
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<>();
@Override
public void postConstruct() {
events.register(this);
processType = processEnv.getProcessType();
}
@Override
public void event(Event> event) {
Deployment.APPLICATION_LOADED.onMatch(event, info -> {
loadManagedBeans(info);
registerAppLevelDependencies(info);
});
Deployment.APPLICATION_UNLOADED.onMatch(event, info -> doCleanup(info.getMetaData(Application.class)));
Deployment.DEPLOYMENT_FAILURE.onMatch(event, info -> doCleanup(info.getModuleMetaData(Application.class)));
}
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);
}
@Override
public void loadManagedBeans(Application app) {
JCDIService jcdiService = habitat.getService(JCDIService.class);
for(BundleDescriptor bundle : app.getBundleDescriptors()) {
if (!bundleEligible(bundle)) {
continue;
}
boolean isCDIBundle = (jcdiService != null && jcdiService.isJCDIEnabled(bundle));
for(ManagedBeanDescriptor next : bundle.getManagedBeans()) {
try {
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<>()));
}
}
private static Constructor> getConstructor(Class> clz, boolean isCDIBundle) throws Exception {
if (isCDIBundle) {
Constructor>[] ctors = clz.getDeclaredConstructors();
for(Constructor> ctor : ctors) {
if (ctor.getAnnotation(jakarta.inject.Inject.class) != null) {
// @Inject constructor
return ctor;
}
}
}
// Not a CDI bundle or no @Inject constructor - use no-arg constructor
return clz.getConstructor();
}
@Override
public Object getManagedBean(String globalJndiName) throws Exception {
NamingObjectProxy proxy = appClientManagedBeans.get(globalJndiName);
return proxy == null ? null : proxy.create(new InitialContext());
}
/**
* Apply a runtime interceptor instance to all managed beans in the given module
* @param interceptorInstance
* @param bundle bundle descripto
*
*/
@Override
public void registerRuntimeInterceptor(Object interceptorInstance, BundleDescriptor bundle) {
for(ManagedBeanDescriptor next : bundle.getManagedBeans()) {
JavaEEInterceptorBuilder builder = (JavaEEInterceptorBuilder) next.getInterceptorBuilder();
builder.addRuntimeInterceptor(interceptorInstance);
}
}
@Override
public void unloadManagedBeans(Application app) {
for(BundleDescriptor bundle : app.getBundleDescriptors()) {
if (!bundleEligible(bundle)) {
continue;
}
Map