Skip to content

Commit

Permalink
WELD-2225 stick to new CDI 2.0-EDR2 SE API.
Browse files Browse the repository at this point in the history
  • Loading branch information
tremes committed Aug 25, 2016
1 parent 9dd3d45 commit e0f6e32
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.UnsatisfiedResolutionException;
import javax.enterprise.inject.Vetoed;
import javax.enterprise.inject.se.SeContainerInitializer;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.Extension;
Expand Down Expand Up @@ -86,6 +88,7 @@
import org.jboss.weld.environment.util.BeanArchives;
import org.jboss.weld.environment.util.DevelopmentMode;
import org.jboss.weld.environment.util.Files;
import org.jboss.weld.exceptions.UnsupportedOperationException;
import org.jboss.weld.experimental.InterceptorBuilder;
import org.jboss.weld.manager.api.WeldManager;
import org.jboss.weld.metadata.BeansXmlImpl;
Expand Down Expand Up @@ -172,7 +175,7 @@
* @see WeldContainer
*/
@Vetoed
public class Weld implements ContainerInstanceFactory {
public class Weld extends SeContainerInitializer implements ContainerInstanceFactory {

/**
* By default, bean archive isolation is enabled. If set to false, Weld will use a "flat" deployment structure - all bean classes share the same bean
Expand Down Expand Up @@ -304,9 +307,7 @@ public String getContainerId() {
*/
public Weld beanClasses(Class<?>... classes) {
beanClasses.clear();
for (Class<?> beanClass : classes) {
addBeanClass(beanClass);
}
addBeanClasses(classes);
return this;
}

Expand All @@ -321,6 +322,14 @@ public Weld addBeanClass(Class<?> beanClass) {
return this;
}

@Override
public Weld addBeanClasses(Class<?>... classes) {
for (Class<?> aClass : classes) {
addBeanClass(aClass);
}
return this;
}

/**
* All classes from the packages of the specified classes will be added to the set of bean classes for the synthetic bean archive.
*
Expand Down Expand Up @@ -355,6 +364,36 @@ public Weld addPackages(boolean scanRecursively, Class<?>... packageClasses) {
return this;
}

@Override
public Weld addPackages(Class<?>... packageClasses) {
addPackages(false, packageClasses);
return this;
}

@Override
public Weld addPackages(Package... packages) {
addPackages(false, packages);
return this;
}

@Override
public Weld addPackages(boolean scanRecursively, Package... packages) {
for (Package pack : packages) {
this.packages.add(new PackInfo(pack, scanRecursively));
}
return this;
}

@Override
public Weld addAnnotatedTypes(AnnotatedType<?>... annotatedTypes) {
throw new UnsupportedOperationException();
}

@Override
public Weld addBeans(Bean<?>... beans) {
throw new UnsupportedOperationException();
}

/**
* A package of the specified class will be scanned and found classes will be added to the set of bean classes for the synthetic bean archive.
*
Expand Down Expand Up @@ -391,6 +430,28 @@ public Weld addExtension(Extension extension) {
return this;
}

@Override
public Weld addExtensions(Extension... extensions) {
for (Extension extension : extensions) {
addExtension(extension);
}
return this;
}

@Override
public Weld addExtensions(Class<? extends Extension>... extensionClasses) {
Object[] constructorParams = new Object[] {};
for (Class<? extends Extension> extensionClass : extensionClasses) {
try {
Extension extension = SecurityActions.newInstance(extensionClass);
addExtension(extension);
} catch (Exception ex) {
CommonLogger.LOG.unableToInstantiate(extensionClass, constructorParams, ex);
}
}
return this;
}

/**
* Add a synthetic container lifecycle event observer.
*
Expand All @@ -414,9 +475,7 @@ public Weld addContainerLifecycleObserver(ContainerLifecycleObserver<?> observer
*/
public Weld interceptors(Class<?>... interceptorClasses) {
enabledInterceptors.clear();
for (Class<?> interceptorClass : interceptorClasses) {
addInterceptor(interceptorClass);
}
addInterceptors(interceptorClasses);
return this;
}

Expand All @@ -433,6 +492,15 @@ public Weld addInterceptor(Class<?> interceptorClass) {
enabledInterceptors.add(syntheticMetadata(interceptorClass));
return this;
}

@Override
public Weld addInterceptors(Class<?>... interceptorClasses) {
for (Class<?> interceptorClass : interceptorClasses) {
addInterceptor(interceptorClass);
}
return this;
}

/**
* Enable decorators for the synthetic bean archive, all previous values are removed.
* <p>
Expand All @@ -444,9 +512,7 @@ public Weld addInterceptor(Class<?> interceptorClass) {
*/
public Weld decorators(Class<?>... decoratorClasses) {
enabledDecorators.clear();
for (Class<?> decoratorClass : decoratorClasses) {
addDecorator(decoratorClass);
}
addDecorators(decoratorClasses);
return this;
}

Expand All @@ -464,6 +530,14 @@ public Weld addDecorator(Class<?> decoratorClass) {
return this;
}

@Override
public Weld addDecorators(Class<?>... decoratorClasses) {
for (Class<?> decoratorClass : decoratorClasses) {
addDecorator(decoratorClass);
}
return this;
}

/**
* Select alternatives for the synthetic bean archive, all previous values are removed.
* <p>
Expand All @@ -475,9 +549,7 @@ public Weld addDecorator(Class<?> decoratorClass) {
*/
public Weld alternatives(Class<?>... alternativeClasses) {
selectedAlternatives.clear();
for (Class<?> alternativeClass : alternativeClasses) {
addAlternative(alternativeClass);
}
addAlternatives(alternativeClasses);
return this;
}

Expand All @@ -495,6 +567,14 @@ public Weld addAlternative(Class<?> alternativeClass) {
return this;
}

@Override
public Weld addAlternatives(Class<?>... alternativeClasses) {
for (Class<?> alternativeClass : alternativeClasses) {
addAlternative(alternativeClass);
}
return this;
}

/**
* Select alternative stereotypes for the synthetic bean archive, all previous values are removed.
* <p>
Expand All @@ -507,6 +587,12 @@ public Weld addAlternative(Class<?> alternativeClass) {
@SafeVarargs
public final Weld alternativeStereotypes(Class<? extends Annotation>... alternativeStereotypeClasses) {
selectedAlternativeStereotypes.clear();
addAlternativeStereotypes(alternativeStereotypeClasses);
return this;
}

@Override
public Weld addAlternativeStereotypes(Class<? extends Annotation>... alternativeStereotypeClasses) {
for (Class<? extends Annotation> alternativeStereotypeClass : alternativeStereotypeClasses) {
addAlternativeStereotype(alternativeStereotypeClass);
}
Expand Down Expand Up @@ -554,6 +640,19 @@ public Weld properties(Map<String, Object> properties) {
return this;
}

@Override
public Weld addProperty(String key, Object value) {
property(key, value);
return this;
}

@Override
public Weld setProperties(Map<String, Object> propertiesMap) {
properties.clear();
properties.putAll(propertiesMap);
return this;
}

/**
* The resulting bean is registered automatically during container initialization.
*
Expand Down Expand Up @@ -916,12 +1015,8 @@ private Set<String> scanPackages() {

for (PackInfo packInfo : packages) {

ClassLoader cl = packInfo.getClassLoaderRef().get();
if (cl == null) {
continue;
}
String packName = packInfo.getPackName();
URL resourceUrl = cl.getResource(packInfo.getPackClassName().replace('.', '/') + Files.CLASS_FILE_EXTENSION);
URL resourceUrl = packInfo.getResourceUrl(resourceLoader);

if (resourceUrl != null) {

Expand All @@ -931,8 +1026,8 @@ private Set<String> scanPackages() {
URI resourceUri = resourceUrl.toURI();

if (PROCOTOL_FILE.equals(resourceUrl.getProtocol())) {
// Get the package directory, e.g. "file:///home/weld/org/jboss
handleDir(new File(resourceUri).getParentFile(), packInfo.isScanRecursively(), packName, foundClasses);
File file = new File(resourceUri);
handleDir(file.isDirectory() ? file : file.getParentFile(), packInfo.isScanRecursively(), packName, foundClasses);
} else if (PROCOTOL_JAR.equals(resourceUrl.getProtocol())) {
handleJar(resourceUri, packInfo.isScanRecursively(), packName, foundClasses);
} else {
Expand Down Expand Up @@ -1037,13 +1132,29 @@ private static class PackInfo {

private final WeakReference<ClassLoader> classLoaderRef;


PackInfo(Class<?> packClass, boolean recursiveScan) {
this.packName = packClass.getPackage().getName();
this.packClassName = packClass.getName();
this.scanRecursively = recursiveScan;
this.classLoaderRef = new WeakReference<ClassLoader>(AccessController.doPrivileged(new GetClassLoaderAction(packClass)));
}

PackInfo(Package pack, boolean recursiveScan) {
this.packName = pack.getName();
this.scanRecursively = recursiveScan;
this.packClassName = null;
this.classLoaderRef = null;
}

public URL getResourceUrl(ResourceLoader resourceLoader) {
if (classLoaderRef != null) {
return classLoaderRef.get().getResource(this.getPackClassName().replace('.', '/') + Files.CLASS_FILE_EXTENSION);
} else {
return resourceLoader.getResource(getPackName().replace('.', '/'));
}
}

public String getPackName() {
return packName;
}
Expand All @@ -1056,10 +1167,6 @@ public boolean isScanRecursively() {
return scanRecursively;
}

public WeakReference<ClassLoader> getClassLoaderRef() {
return classLoaderRef;
}

@Override
public int hashCode() {
final int prime = 31;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import javax.enterprise.event.Event;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.Vetoed;
import javax.enterprise.inject.se.SeContainer;
import javax.enterprise.inject.spi.BeanManager;

import org.jboss.weld.AbstractCDI;
Expand Down Expand Up @@ -89,7 +90,7 @@
* @see Weld
*/
@Vetoed
public class WeldContainer extends AbstractCDI<Object> implements AutoCloseable, ContainerInstance {
public class WeldContainer extends AbstractCDI<Object> implements AutoCloseable, ContainerInstance, SeContainer {

private static final Singleton<WeldContainer> SINGLETON;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.jboss.weld.environment.se.Weld
4 changes: 1 addition & 3 deletions jboss-tck-runner/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,7 @@
<stripVersion>true</stripVersion>
<overWriteReleases>true</overWriteReleases>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<includeGroupIds>
org.jboss.weld.se
</includeGroupIds>
<includeGroupIds>org.jboss.weld.se</includeGroupIds>
</configuration>
</execution>
</executions>
Expand Down

0 comments on commit e0f6e32

Please sign in to comment.