@Inject
private Instance<ServiceLoader> loader;
public void createRegistry(@Observes ArquillianDescriptor event)
{
LocalContainerRegistry reg = new LocalContainerRegistry(injector.get());
ServiceLoader serviceLoader = loader.get();
validateConfiguration(event);
String activeConfiguration = getActivatedConfiguration();
for(ContainerDef container : event.getContainers())
{
if(
(activeConfiguration != null && activeConfiguration.equals(container.getContainerName())) ||
(activeConfiguration == null && container.isDefault()))
{
reg.create(container, serviceLoader);
}
}
for(GroupDef group : event.getGroups())
{
if(
(activeConfiguration != null && activeConfiguration.equals(group.getGroupName())) ||
(activeConfiguration == null && group.isGroupDefault()))
{
for(ContainerDef container : group.getGroupContainers())
{
reg.create(container, serviceLoader);
}
}
}
if(activeConfiguration == null && reg.getContainers().size() == 0)
{
DeployableContainer<?> deployableContainer = null;
try
{
// 'check' if there are any DeployableContainers on CP
deployableContainer = serviceLoader.onlyOne(DeployableContainer.class);
}
catch (IllegalStateException e)
{
StringBuilder stringBuilder = new StringBuilder()
.append("Could not add a default container to registry because multiple instances of ")
.append(DeployableContainer.class.getName())
.append(" found on classpath (candidates are: ");
String separator = "";
for (DeployableContainer s : serviceLoader.all(DeployableContainer.class)) {
stringBuilder.append(separator)
.append(s.getConfigurationClass().getName());
separator = ", ";
}
stringBuilder.append(")");
throw new IllegalStateException(stringBuilder.toString());
}
catch (Exception e)
{
throw new IllegalStateException("Could not create the default container instance", e);
}
if(deployableContainer != null)
{
reg.create(new ContainerDefImpl("arquillian.xml").setContainerName("default"), serviceLoader);
}
}
else if (activeConfiguration != null && reg.getContainers().size() == 0)
{
throw new IllegalArgumentException("No container or group found that match given qualifier: " + activeConfiguration);
}
// export