Skip to content

Commit

Permalink
[WELD-687]; initial impl.
Browse files Browse the repository at this point in the history
Conflicts:

	impl/src/main/java/org/jboss/weld/bean/proxy/ProxyFactory.java
	impl/src/main/java/org/jboss/weld/bootstrap/BeanDeployment.java
	impl/src/main/java/org/jboss/weld/bootstrap/WeldBootstrap.java
  • Loading branch information
alesj authored and jharting committed Mar 18, 2012
1 parent 655f05d commit b0401fc
Show file tree
Hide file tree
Showing 15 changed files with 892 additions and 555 deletions.
Expand Up @@ -221,14 +221,15 @@ public void addInterface(Class<?> newInterface) {
/**
* Method to create a new proxy that wraps the bean instance.
*
* @param beanInstance the bean instance
* @return a new proxy object
*/

public T create(BeanInstance beanInstance) {
T proxy = null;
T proxy;
Class<T> proxyClass = getProxyClass();
try {
if (InstantiatorFactory.useInstantiators()) {
InstantiatorFactory factory = Container.instance().services().get(InstantiatorFactory.class);
if (factory != null && factory.useInstantiators()) {
proxy = SecureReflections.newUnsafeInstance(proxyClass);
} else {
proxy = SecureReflections.newInstance(proxyClass);
Expand Down
20 changes: 16 additions & 4 deletions impl/src/main/java/org/jboss/weld/bootstrap/BeanDeployment.java
Expand Up @@ -73,6 +73,8 @@
import org.jboss.weld.transaction.spi.TransactionServices;
import org.jboss.weld.util.BeansClosure;
import org.jboss.weld.util.reflection.Reflections;
import org.jboss.weld.util.reflection.instantiation.DefaultInstantiatorFactory;
import org.jboss.weld.util.reflection.instantiation.InstantiatorFactory;
import org.jboss.weld.validation.spi.ValidationServices;
import org.jboss.weld.ws.WSApiAbstraction;
import org.slf4j.cal10n.LocLogger;
Expand All @@ -83,6 +85,7 @@
/**
* @author Pete Muir
* @author Jozef Hartinger
* @author alesj
*/
public class BeanDeployment {

Expand All @@ -97,15 +100,24 @@ public class BeanDeployment {
public BeanDeployment(BeanDeploymentArchive beanDeploymentArchive, BeanManagerImpl deploymentManager, ServiceRegistry deploymentServices, Collection<ContextHolder<? extends Context>> contexts) {
this.beanDeploymentArchive = beanDeploymentArchive;
EjbDescriptors ejbDescriptors = new EjbDescriptors();
beanDeploymentArchive.getServices().add(EjbDescriptors.class, ejbDescriptors);
ResourceLoader resourceLoader = beanDeploymentArchive.getServices().get(ResourceLoader.class);

ServiceRegistry registry = beanDeploymentArchive.getServices();
registry.add(EjbDescriptors.class, ejbDescriptors);

ResourceLoader resourceLoader = registry.get(ResourceLoader.class);
if (resourceLoader == null) {
resourceLoader = DefaultResourceLoader.INSTANCE;
beanDeploymentArchive.getServices().add(ResourceLoader.class, resourceLoader);
registry.add(ResourceLoader.class, resourceLoader);
}

InstantiatorFactory factory = registry.get(InstantiatorFactory.class);
if (factory == null) {
registry.add(InstantiatorFactory.class, new DefaultInstantiatorFactory());
}

ServiceRegistry services = new SimpleServiceRegistry();
services.addAll(deploymentServices.entrySet());
services.addAll(beanDeploymentArchive.getServices().entrySet());
services.addAll(registry.entrySet());

services.add(EJBApiAbstraction.class, new EJBApiAbstraction(resourceLoader));
services.add(JsfApiAbstraction.class, new JsfApiAbstraction(resourceLoader));
Expand Down
93 changes: 53 additions & 40 deletions impl/src/main/java/org/jboss/weld/bootstrap/WeldBootstrap.java
Expand Up @@ -16,6 +16,32 @@
*/
package org.jboss.weld.bootstrap;

import static org.jboss.weld.logging.Category.BOOTSTRAP;
import static org.jboss.weld.logging.Category.VERSION;
import static org.jboss.weld.logging.LoggerFactory.loggerFactory;
import static org.jboss.weld.logging.messages.BootstrapMessage.DEPLOYMENT_ARCHIVE_NULL;
import static org.jboss.weld.logging.messages.BootstrapMessage.DEPLOYMENT_REQUIRED;
import static org.jboss.weld.logging.messages.BootstrapMessage.JTA_UNAVAILABLE;
import static org.jboss.weld.logging.messages.BootstrapMessage.MANAGER_NOT_INITIALIZED;
import static org.jboss.weld.logging.messages.BootstrapMessage.UNSPECIFIED_REQUIRED_SERVICE;
import static org.jboss.weld.logging.messages.BootstrapMessage.VALIDATING_BEANS;
import static org.jboss.weld.manager.Enabled.EMPTY_ENABLED;

import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import javax.enterprise.context.spi.Context;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.CDI;
import javax.enterprise.inject.spi.Extension;

import org.jboss.weld.Container;
import org.jboss.weld.ContainerState;
import org.jboss.weld.Weld;
Expand Down Expand Up @@ -89,34 +115,11 @@
import org.jboss.weld.util.ServiceLoader;
import org.jboss.weld.util.reflection.Formats;
import org.jboss.weld.util.reflection.Reflections;
import org.jboss.weld.util.reflection.instantiation.InstantiatorFactory;
import org.jboss.weld.util.reflection.instantiation.LoaderInstantiatorFactory;
import org.jboss.weld.xml.BeansXmlParser;
import org.slf4j.cal10n.LocLogger;

import javax.enterprise.context.spi.Context;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.CDI;
import javax.enterprise.inject.spi.Extension;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import static org.jboss.weld.logging.Category.BOOTSTRAP;
import static org.jboss.weld.logging.Category.VERSION;
import static org.jboss.weld.logging.LoggerFactory.loggerFactory;
import static org.jboss.weld.logging.messages.BootstrapMessage.DEPLOYMENT_ARCHIVE_NULL;
import static org.jboss.weld.logging.messages.BootstrapMessage.DEPLOYMENT_REQUIRED;
import static org.jboss.weld.logging.messages.BootstrapMessage.JTA_UNAVAILABLE;
import static org.jboss.weld.logging.messages.BootstrapMessage.MANAGER_NOT_INITIALIZED;
import static org.jboss.weld.logging.messages.BootstrapMessage.UNSPECIFIED_REQUIRED_SERVICE;
import static org.jboss.weld.logging.messages.BootstrapMessage.VALIDATING_BEANS;
import static org.jboss.weld.manager.Enabled.EMPTY_ENABLED;

/**
* Common bootstrapping functionality that is run at application startup and
* detects and register beans
Expand Down Expand Up @@ -157,13 +160,19 @@ public Map<BeanDeploymentArchive, BeanDeployment> visit() {
return managerAwareBeanDeploymentArchives;
}

private BeanDeployment visit(BeanDeploymentArchive beanDeploymentArchive, Map<BeanDeploymentArchive, BeanDeployment> managerAwareBeanDeploymentArchives, Set<BeanDeploymentArchive> seenBeanDeploymentArchives, boolean validate) {
private <T extends Service> void copyService(BeanDeploymentArchive archive, Class<T> serviceClass) {
// for certain services we can fall back to deployment-level settings or defaults
if (!beanDeploymentArchive.getServices().contains(ResourceLoader.class)) {
ResourceLoader loader = deployment.getServices().get(ResourceLoader.class);
if (loader != null)
beanDeploymentArchive.getServices().add(ResourceLoader.class, loader);
ServiceRegistry registry = archive.getServices();
if (registry.contains(serviceClass) == false) {
T service = deployment.getServices().get(serviceClass);
if (service != null)
registry.add(serviceClass, service);
}
}

private BeanDeployment visit(BeanDeploymentArchive beanDeploymentArchive, Map<BeanDeploymentArchive, BeanDeployment> managerAwareBeanDeploymentArchives, Set<BeanDeploymentArchive> seenBeanDeploymentArchives, boolean validate) {
copyService(beanDeploymentArchive, ResourceLoader.class);
copyService(beanDeploymentArchive, InstantiatorFactory.class);
// Check that the required services are specified
if (validate) {
verifyServices(beanDeploymentArchive.getServices(), environment.getRequiredBeanDeploymentArchiveServices());
Expand Down Expand Up @@ -221,19 +230,23 @@ public Bootstrap startContainer(Environment environment, Deployment deployment)
if (deployment == null) {
throw new IllegalArgumentException(DEPLOYMENT_REQUIRED);
}
if (!deployment.getServices().contains(ResourceLoader.class)) {
deployment.getServices().add(ResourceLoader.class, DefaultResourceLoader.INSTANCE);
final ServiceRegistry registry = deployment.getServices();
if (!registry.contains(ResourceLoader.class)) {
registry.add(ResourceLoader.class, DefaultResourceLoader.INSTANCE);
}
if (!registry.contains(InstantiatorFactory.class)) {
registry.add(InstantiatorFactory.class, new LoaderInstantiatorFactory());
}
if (!deployment.getServices().contains(ScheduledExecutorServiceFactory.class)) {
deployment.getServices().add(ScheduledExecutorServiceFactory.class, new SingleThreadScheduledExecutorServiceFactory());
if (!registry.contains(ScheduledExecutorServiceFactory.class)) {
registry.add(ScheduledExecutorServiceFactory.class, new SingleThreadScheduledExecutorServiceFactory());
}
if (!deployment.getServices().contains(ProxyServices.class)) {
deployment.getServices().add(ProxyServices.class, new SimpleProxyServices());
if (!registry.contains(ProxyServices.class)) {
registry.add(ProxyServices.class, new SimpleProxyServices());
}

verifyServices(deployment.getServices(), environment.getRequiredDeploymentServices());
verifyServices(registry, environment.getRequiredDeploymentServices());

if (!deployment.getServices().contains(TransactionServices.class)) {
if (!registry.contains(TransactionServices.class)) {
log.info(JTA_UNAVAILABLE);
}
// TODO Reinstate if we can find a good way to detect.
Expand All @@ -254,7 +267,7 @@ public Bootstrap startContainer(Environment environment, Deployment deployment)
this.deployment = deployment;
ServiceRegistry implementationServices = getImplementationServices();

deployment.getServices().addAll(implementationServices.entrySet());
registry.addAll(implementationServices.entrySet());

ServiceRegistry deploymentServices = new SimpleServiceRegistry();
deploymentServices.add(ClassTransformer.class, implementationServices.get(ClassTransformer.class));
Expand All @@ -264,7 +277,7 @@ public Bootstrap startContainer(Environment environment, Deployment deployment)
this.environment = environment;
this.deploymentManager = BeanManagerImpl.newRootManager("deployment", deploymentServices, EMPTY_ENABLED);

Container.initialize(deploymentManager, ServiceRegistries.unmodifiableServiceRegistry(deployment.getServices()));
Container.initialize(deploymentManager, ServiceRegistries.unmodifiableServiceRegistry(registry));
Container.instance().setState(ContainerState.STARTING);

this.contexts = createContexts(deploymentServices);
Expand Down
22 changes: 13 additions & 9 deletions impl/src/main/java/org/jboss/weld/util/Proxies.java
Expand Up @@ -16,13 +16,6 @@
*/
package org.jboss.weld.util;

import org.jboss.weld.exceptions.IllegalArgumentException;
import org.jboss.weld.exceptions.UnproxyableResolutionException;
import org.jboss.weld.util.reflection.Reflections;
import org.jboss.weld.util.reflection.SecureReflections;
import org.jboss.weld.util.reflection.instantiation.InstantiatorFactory;

import javax.enterprise.inject.spi.Bean;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
Expand All @@ -32,6 +25,15 @@
import java.util.LinkedHashSet;
import java.util.Set;

import javax.enterprise.inject.spi.Bean;

import org.jboss.weld.Container;
import org.jboss.weld.exceptions.IllegalArgumentException;
import org.jboss.weld.exceptions.UnproxyableResolutionException;
import org.jboss.weld.util.reflection.Reflections;
import org.jboss.weld.util.reflection.SecureReflections;
import org.jboss.weld.util.reflection.instantiation.InstantiatorFactory;

import static org.jboss.weld.logging.messages.UtilMessage.CANNOT_PROXY_NON_CLASS_TYPE;
import static org.jboss.weld.logging.messages.ValidatorMessage.NOT_PROXYABLE_ARRAY_TYPE;
import static org.jboss.weld.logging.messages.ValidatorMessage.NOT_PROXYABLE_FINAL_TYPE_OR_METHOD;
Expand Down Expand Up @@ -207,7 +209,8 @@ private static UnproxyableResolutionException getUnproxyableClassException(Class
try {
constructor = SecureReflections.getDeclaredConstructor(clazz);
} catch (NoSuchMethodException e) {
if (!InstantiatorFactory.useInstantiators()) {
InstantiatorFactory factory = Container.instance().services().get(InstantiatorFactory.class);
if (factory == null || factory.useInstantiators() == false) {
return new UnproxyableResolutionException(NOT_PROXYABLE_NO_CONSTRUCTOR, clazz, getDeclaringBeanInfo(declaringBean));
} else {
return null;
Expand All @@ -216,7 +219,8 @@ private static UnproxyableResolutionException getUnproxyableClassException(Class
if (constructor == null) {
return new UnproxyableResolutionException(NOT_PROXYABLE_NO_CONSTRUCTOR, clazz, getDeclaringBeanInfo(declaringBean));
} else if (Modifier.isPrivate(constructor.getModifiers())) {
if (!InstantiatorFactory.useInstantiators()) {
InstantiatorFactory factory = Container.instance().services().get(InstantiatorFactory.class);
if (factory == null || factory.useInstantiators() == false) {
return new UnproxyableResolutionException(NOT_PROXYABLE_PRIVATE_CONSTRUCTOR, clazz, constructor, getDeclaringBeanInfo(declaringBean));
} else {
return null;
Expand Down

0 comments on commit b0401fc

Please sign in to comment.