final DeploymentUnit depUnit = phaseContext.getDeploymentUnit();
final String runtimeName = depUnit.getName();
// Check if {@link BundleInstallIntegration} provided the {@link Deployment}
Deployment deployment = BundleLifecycleIntegration.removeDeployment(runtimeName);
if (deployment != null) {
deployment.setAutoStart(false);
}
// Check for attached BundleInfo
BundleInfo info = depUnit.getAttachment(OSGiConstants.BUNDLE_INFO_KEY);
if (deployment == null && info != null) {
deployment = DeploymentFactory.createDeployment(info);
deployment.addAttachment(BundleInfo.class, info);
OSGiMetaData metadata = info.getOSGiMetadata();
deployment.setAutoStart(!metadata.isFragment());
// Set the start level and prevent autostart if greater than the Framwork startlevel
AnnotationInstance slAware = getAnnotation(depUnit, "org.jboss.arquillian.osgi.StartLevelAware");
if (slAware != null) {
MethodInfo slTarget = (MethodInfo) slAware.target();
for (AnnotationInstance anDeployment : getAnnotations(depUnit, "org.jboss.arquillian.container.test.api.Deployment")) {
AnnotationValue namevalue = anDeployment.value("name");
Object deploymentName = namevalue != null ? namevalue.value() : null;
if (slTarget == anDeployment.target() && depUnit.getName().equals(deploymentName)) {
int startLevel = slAware.value("startLevel").asInt();
deployment.setStartLevel(startLevel);
deployment.setAutoStart(false);
}
}
}
// Prevent autostart for marked deployments
AnnotationInstance marker = getAnnotation(depUnit, "org.jboss.as.arquillian.api.DeploymentMarker");
if (marker != null) {
AnnotationValue value = marker.value("autoStart");
if (value != null && deployment.isAutoStart()) {
deployment.setAutoStart(value.asBoolean());
}
value = marker.value("startLevel");
if (value != null && deployment.getStartLevel() == null) {
deployment.setStartLevel(value.asInt());
}
}
}
// Attach the deployment
if (deployment != null) {
// Make sure the framework uses the same module id as the server
ModuleIdentifier identifier = depUnit.getAttachment(Attachments.MODULE_IDENTIFIER);
deployment.addAttachment(ModuleIdentifier.class, identifier);
// Allow additional dependencies for the set of supported deployemnt types
if (allowAdditionalModuleDependencies(depUnit)) {
ModuleSpecification moduleSpec = depUnit.getAttachment(Attachments.MODULE_SPECIFICATION);
deployment.addAttachment(ModuleSpecification.class, moduleSpec);
} else {
// Make this module private so that other modules in the deployment don't create a direct dependency
ModuleSpecification moduleSpec = depUnit.getAttachment(Attachments.MODULE_SPECIFICATION);
moduleSpec.setPrivateModule(true);
}
// Attach the bundle deployment
depUnit.putAttachment(OSGiConstants.DEPLOYMENT_KEY, deployment);
deployment.addAttachment(DeploymentUnit.class, depUnit);
}
}