use of io.fabric8.api.Container in project fabric8 by jboss-fuse.
the class ZookeeperPortService method findUsedPortByHost.
private Set<Integer> findUsedPortByHost(Container container, Lease existingLease) {
assertValid();
String ip = container.getIp();
assertValidIp(container, ip);
Set<Integer> ports = new HashSet<Integer>();
String path = ZkPath.PORTS_IP.getPath(ip);
Lease lease = null;
try {
if (existingLease != null) {
lease = existingLease;
} else {
lease = interProcessLock.acquire(60, TimeUnit.SECONDS);
}
if (lease != null) {
createDefault(curator.get(), path, "");
String boundPorts = getStringData(curator.get(), path);
if (boundPorts != null && !boundPorts.isEmpty()) {
for (String port : boundPorts.split(" ")) {
try {
ports.add(Integer.parseInt(port.trim()));
} catch (NumberFormatException ex) {
// ignore
}
}
}
} else {
throw new FabricException("Could not acquire port lock");
}
} catch (InterruptedException ex) {
cleanUpDirtyZKNodes(interProcessLock);
throw FabricException.launderThrowable(ex);
} catch (Exception ex) {
throw FabricException.launderThrowable(ex);
} finally {
if (existingLease == null) {
releaseLock(lease);
}
}
return ports;
}
use of io.fabric8.api.Container in project fabric8 by jboss-fuse.
the class FabricCxfRegistrationHandler method registerApiEndpoint.
protected void registerApiEndpoint(Container container, ObjectName oName, String address, boolean started) {
String actualEndpointUrl = null;
try {
String url;
String id = container.getId();
if (isFullAddress(address)) {
url = toPublicAddress(id, address);
} else {
String cxfBus = getCxfServletPath(oName);
url = "${zk:" + id + "/http}" + cxfBus + address;
}
actualEndpointUrl = ZooKeeperUtils.getSubstitutedData(curator.get(), url);
// lets assume these locations are hard coded
// may be nice to discover from JMX one day
String apiDocPath = "/api-docs";
String wsdlPath = "?wsdl";
String wadlPath = "?_wadl";
Version version = container.getVersion();
String versionId = version != null ? version.getId() : null;
String json = "{\"id\":" + JsonHelper.jsonEncodeString(id) + ", \"container\":" + JsonHelper.jsonEncodeString(id) + ", \"version\":" + JsonHelper.jsonEncodeString(versionId) + ", \"services\":[" + JsonHelper.jsonEncodeString(url) + "]" + ", \"objectName\":" + JsonHelper.jsonEncodeString(oName.toString()) + "";
boolean rest = false;
if (booleanAttribute(oName, "isWADL")) {
rest = true;
json += ", \"wadl\":" + JsonHelper.jsonEncodeString(wadlPath);
}
if (booleanAttribute(oName, "isSwagger")) {
rest = true;
json += ", \"apidocs\":" + JsonHelper.jsonEncodeString(apiDocPath);
}
if (booleanAttribute(oName, "isWSDL")) {
json += ", \"wsdl\":" + JsonHelper.jsonEncodeString(wsdlPath);
}
json += "}";
String path = getPath(container, oName, address, rest);
LOGGER.info("Registered CXF API at " + path + " JSON: " + json);
if (!started && !rest) {
LOGGER.warn("Since the CXF service isn't started, this could really be a REST endpoint rather than WSDL at " + path);
}
registeredZkPaths.add(path);
registeredUrls.put(oName.toString(), address);
ZooKeeperUtils.setData(curator.get(), path, json, CreateMode.EPHEMERAL);
} catch (Exception e) {
LOGGER.error("Failed to register API endpoint for {}.", actualEndpointUrl, e);
}
}
use of io.fabric8.api.Container in project fabric8 by jboss-fuse.
the class ZookeeperPortService method findUsedPortByContainer.
@Override
public Set<Integer> findUsedPortByContainer(Container container) {
assertValid();
Set<Integer> ports = new HashSet<Integer>();
String path = ZkPath.PORTS_CONTAINER.getPath(container.getId());
Lease lease = null;
try {
lease = interProcessLock.acquire(60, TimeUnit.SECONDS);
if (lease != null) {
if (exists(curator.get(), path) != null) {
for (String pid : getChildren(curator.get(), path)) {
for (String key : getChildren(curator.get(), ZkPath.PORTS_CONTAINER_PID.getPath(container.getId(), pid))) {
String port = getStringData(curator.get(), ZkPath.PORTS_CONTAINER_PID_KEY.getPath(container.getId(), pid, key));
try {
ports.add(Integer.parseInt(port));
} catch (Exception ex) {
// ignore
}
}
}
}
} else {
throw new FabricException("Could not acquire port lock");
}
} catch (InterruptedException ex) {
cleanUpDirtyZKNodes(interProcessLock);
throw FabricException.launderThrowable(ex);
} catch (Exception ex) {
throw FabricException.launderThrowable(ex);
} finally {
releaseLock(lease);
}
return ports;
}
use of io.fabric8.api.Container in project fabric8 by jboss-fuse.
the class ChildContainerProvider method createKarafContainerController.
private ChildContainerController createKarafContainerController() {
return new ChildContainerController() {
@Override
public CreateChildContainerMetadata create(final CreateChildContainerOptions options, final CreationStateListener listener) {
final Container parent = fabricService.get().getContainer(options.getParent());
ContainerTemplate containerTemplate = new ContainerTemplate(parent, options.getJmxUser(), options.getJmxPassword(), false);
return containerTemplate.execute(new ContainerTemplate.AdminServiceCallback<CreateChildContainerMetadata>() {
public CreateChildContainerMetadata doWithAdminService(AdminServiceMBean adminService) throws Exception {
return doCreateKaraf(adminService, options, listener, parent);
}
});
}
@Override
public void start(final Container container) {
getContainerTemplateForChild(container).execute(new ContainerTemplate.AdminServiceCallback<Object>() {
public Object doWithAdminService(AdminServiceMBean adminService) throws Exception {
// update jvm options if they have have changed
CreateChildContainerMetadata metadata = (CreateChildContainerMetadata) container.getMetadata();
CreateChildContainerOptions createOptions = metadata.getCreateOptions();
String jvmOpts = createOptions.getJvmOpts();
TabularData instances = adminService.getInstances();
Collection<CompositeDataSupport> values = (Collection<CompositeDataSupport>) instances.values();
for (CompositeDataSupport o : values) {
if (container.getId().equals(o.get("Name"))) {
if (o.containsKey("JavaOpts")) {
String oldJavaOpts = (String) o.get("JavaOpts");
StringBuilder stringBuilder = ChildContainerProvider.buildJvmOpts(createOptions, fabricService.get());
String extendendJvmOpts = stringBuilder.toString();
if (jvmOpts != null && !extendendJvmOpts.equals(oldJavaOpts)) {
adminService.changeJavaOpts(container.getId(), extendendJvmOpts);
}
}
break;
}
}
adminService.startInstance(container.getId(), null);
return null;
}
});
}
@Override
public void stop(final Container container) {
getContainerTemplateForChild(container).execute(new ContainerTemplate.AdminServiceCallback<Object>() {
public Object doWithAdminService(AdminServiceMBean adminService) throws Exception {
String prevProvisionResult = container.getProvisionResult();
container.setProvisionResult(Container.PROVISION_STOPPING);
try {
adminService.stopInstance(container.getId());
container.setProvisionResult(Container.PROVISION_STOPPED);
} catch (Throwable t) {
container.setProvisionResult(prevProvisionResult);
LOG.error("Failed to stop container: " + container.getId(), t);
throw t;
}
return null;
}
});
}
@Override
public void destroy(final Container container) {
getContainerTemplateForChild(container).execute(new ContainerTemplate.AdminServiceCallback<Object>() {
public Object doWithAdminService(AdminServiceMBean adminService) throws Exception {
adminService.destroyInstance(container.getId());
return null;
}
});
}
};
}
use of io.fabric8.api.Container in project fabric8 by jboss-fuse.
the class FabricServiceImpl method destroyContainer.
public void destroyContainer(String containerId, boolean force) {
assertValid();
Container container = getContainer(containerId);
if (container != null) {
destroyContainer(container, force);
}
}
Aggregations