| 1 | |
package org.jbehave.core.configuration.weld; |
| 2 | |
|
| 3 | |
import java.util.HashMap; |
| 4 | |
import java.util.Map; |
| 5 | |
|
| 6 | |
import javax.enterprise.context.ApplicationScoped; |
| 7 | |
import javax.enterprise.inject.Any; |
| 8 | |
import javax.enterprise.inject.Instance; |
| 9 | |
import javax.inject.Inject; |
| 10 | |
|
| 11 | |
import org.jbehave.core.annotations.weld.UsingWeld; |
| 12 | |
import org.jbehave.core.annotations.weld.WeldConfiguration; |
| 13 | |
import org.jbehave.core.configuration.Configuration; |
| 14 | |
import org.jbehave.core.steps.weld.WeldStepsFactory; |
| 15 | |
import org.jboss.weld.bootstrap.api.Bootstrap; |
| 16 | |
import org.jboss.weld.bootstrap.spi.Deployment; |
| 17 | |
import org.jboss.weld.environment.se.Weld; |
| 18 | |
import org.jboss.weld.environment.se.WeldContainer; |
| 19 | |
import org.jboss.weld.resources.spi.ResourceLoader; |
| 20 | |
|
| 21 | |
@ApplicationScoped |
| 22 | |
public class WeldBootstrap extends Weld { |
| 23 | |
private WeldContainer weld; |
| 24 | |
|
| 25 | 8 | public WeldBootstrap() { |
| 26 | 8 | } |
| 27 | |
|
| 28 | |
@Override |
| 29 | |
protected Deployment createDeployment(ResourceLoader resourceLoader, Bootstrap bootstrap) { |
| 30 | 8 | Deployment deployment = super.createDeployment(resourceLoader, bootstrap); |
| 31 | 8 | return deployment; |
| 32 | |
} |
| 33 | |
|
| 34 | |
@Override |
| 35 | |
public WeldContainer initialize() { |
| 36 | 8 | weld = super.initialize(); |
| 37 | 8 | return weld; |
| 38 | |
} |
| 39 | |
|
| 40 | |
public WeldAnnotationBuilder findAnnotationBuilder(Class<?> annotatedClass) { |
| 41 | 8 | return weld.instance().select(InstanceManager.class).get().findBuilder(annotatedClass); |
| 42 | |
} |
| 43 | |
|
| 44 | |
@ApplicationScoped |
| 45 | 16 | public static class InstanceManager { |
| 46 | |
|
| 47 | |
@Inject |
| 48 | |
@Any |
| 49 | |
@UsingWeld |
| 50 | |
private Instance<Object> instances; |
| 51 | |
|
| 52 | |
@Inject |
| 53 | |
@WeldConfiguration |
| 54 | |
private Configuration configuration; |
| 55 | |
|
| 56 | |
@Inject |
| 57 | |
private WeldStepsFactory stepsFactory; |
| 58 | |
|
| 59 | 16 | private Map<Class<?>, WeldAnnotationBuilder> builders = null; |
| 60 | |
|
| 61 | |
public void build() { |
| 62 | 8 | builders = new HashMap<Class<?>, WeldAnnotationBuilder>(); |
| 63 | 8 | for (Object o : instances) { |
| 64 | 24 | Class<?> instanceClass = o.getClass(); |
| 65 | 24 | WeldAnnotationBuilder builder = new WeldAnnotationBuilder(instanceClass, configuration, stepsFactory); |
| 66 | 24 | builders.put(instanceClass, builder); |
| 67 | 24 | } |
| 68 | 8 | } |
| 69 | |
|
| 70 | |
public WeldAnnotationBuilder findBuilder(Class<?> annotatedClass) { |
| 71 | 8 | if (builders == null) { |
| 72 | 8 | build(); |
| 73 | |
} |
| 74 | |
|
| 75 | 8 | if (builders.containsKey(annotatedClass)) { |
| 76 | 5 | return builders.get(annotatedClass); |
| 77 | |
} else { |
| 78 | 3 | return new WeldAnnotationBuilder(annotatedClass, configuration, stepsFactory); |
| 79 | |
} |
| 80 | |
} |
| 81 | |
|
| 82 | |
} |
| 83 | |
|
| 84 | |
} |