String containerName = options.getName();
PortService portService = fabricService.get().getPortService();
Set<Integer> usedPorts = portService.findUsedPortByHost(parent);
CreateChildContainerMetadata metadata = new CreateChildContainerMetadata();
metadata.setCreateOptions(options);
metadata.setContainerName(containerName);
int minimumPort = parent.getMinimumPort();
int maximumPort = parent.getMaximumPort();
dataStore.setContainerAttribute(containerName, DataStore.ContainerAttribute.PortMin, String.valueOf(minimumPort));
dataStore.setContainerAttribute(containerName, DataStore.ContainerAttribute.PortMax, String.valueOf(maximumPort));
inheritAddresses(fabricService.get(), parent.getId(), containerName, options);
//We are creating a container instance, just for the needs of port registration.
Container child = new ContainerImpl(parent, containerName, fabricService.get()) {
@Override
public String getIp() {
return parent.getIp();
}
};
int sshFrom = mapPortToRange(Ports.DEFAULT_KARAF_SSH_PORT, minimumPort, maximumPort);
int sshTo = mapPortToRange(Ports.DEFAULT_KARAF_SSH_PORT + 100, minimumPort, maximumPort);
int sshPort = portService.registerPort(child, "org.apache.karaf.shell", "sshPort", sshFrom, sshTo, usedPorts);
int httpFrom = mapPortToRange(Ports.DEFAULT_HTTP_PORT, minimumPort, maximumPort);
int httpTo = mapPortToRange(Ports.DEFAULT_HTTP_PORT + 100, minimumPort, maximumPort);
portService.registerPort(child, "org.ops4j.pax.web", "org.osgi.service.http.port", httpFrom, httpTo, usedPorts);
int rmiServerFrom = mapPortToRange(Ports.DEFAULT_RMI_SERVER_PORT, minimumPort, maximumPort);
int rmiServerTo = mapPortToRange(Ports.DEFAULT_RMI_SERVER_PORT + 100, minimumPort, maximumPort);
int rmiServerPort = portService.registerPort(child, "org.apache.karaf.management", "rmiServerPort", rmiServerFrom, rmiServerTo, usedPorts);
int rmiRegistryFrom = mapPortToRange(Ports.DEFAULT_RMI_REGISTRY_PORT, minimumPort, maximumPort);
int rmiRegistryTo = mapPortToRange(Ports.DEFAULT_RMI_REGISTRY_PORT + 100, minimumPort, maximumPort);
int rmiRegistryPort = portService.registerPort(child, "org.apache.karaf.management", "rmiRegistryPort", rmiRegistryFrom, rmiRegistryTo, usedPorts);
try {
adminService.createInstance(containerName,
sshPort,
rmiRegistryPort,
rmiServerPort, null, jvmOptsBuilder.toString(), collectionAsString(features), featuresUrls);
adminService.startInstance(containerName, null);
} catch (Throwable t) {
metadata.setFailure(t);
}
return metadata;
}