protected ServerTemplate getServerTemplate(final String serverTemplateId){ final ServerTemplate serverTemplate = templateStorage.load(serverTemplateId); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + serverTemplateId); } return serverTemplate; }
@Override public ContainerSpecList listContainerSpec(String serverTemplateId) { ServerTemplate serverTemplate = templateStorage.load(serverTemplateId); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + serverTemplateId); } return new ContainerSpecList(serverTemplate.getContainersSpec()); }
@Override public synchronized void deleteServerTemplate(String serverTemplateId) { if (!templateStorage.exists(serverTemplateId)) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + serverTemplateId); } templateStorage.delete(serverTemplateId); notificationService.notify(new ServerTemplateDeleted(serverTemplateId)); }
@Override public synchronized void updateContainerSpec(final String serverTemplateId, final String containerId, final ContainerSpec containerSpec) { ServerTemplate serverTemplate = templateStorage.load(serverTemplateId); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + serverTemplateId); } if (!containerSpec.getId().equals(containerId)) { throw new KieServerControllerException("Cannot update container " + containerSpec.getId() + " on container " + containerId); } if (!serverTemplate.hasContainerSpec(containerSpec.getId())) { throw new KieServerControllerIllegalArgumentException("Server template with id " + serverTemplateId + " has no container with id " + containerSpec.getId()); } if (!serverTemplate.hasMatchingId(containerSpec.getServerTemplateKey())) { throw new KieServerControllerException("Cannot change container template key during update."); } // make sure correct server template is set containerSpec.setServerTemplateKey(new ServerTemplateKey(serverTemplate.getId(), serverTemplate.getName())); ContainerSpec currentVersion = serverTemplate.getContainerSpec(containerSpec.getId()); serverTemplate.deleteContainerSpec(currentVersion.getId()); serverTemplate.addContainerSpec(containerSpec); templateStorage.update(serverTemplate); notificationService.notify(new ServerTemplateUpdated(serverTemplate)); // in case container was started before it was update or update comes with status started update container in running servers if (currentVersion.getStatus().equals(KieContainerStatus.STARTED) || containerSpec.getStatus().equals(KieContainerStatus.STARTED)) { List<Container> containers = kieServerInstanceManager.upgradeAndStartContainer(serverTemplate, containerSpec); notificationService.notify(serverTemplate, containerSpec, containers); } }
@Override public synchronized void saveContainerSpec(String serverTemplateId, ContainerSpec containerSpec) { ServerTemplate serverTemplate = templateStorage.load(serverTemplateId); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + serverTemplateId); } if (serverTemplate.hasContainerSpec(containerSpec.getId())) { throw new KieServerControllerException("Server template with id " + serverTemplateId + " associated already with container " + containerSpec.getId()); } // make sure correct server template is set containerSpec.setServerTemplateKey(new ServerTemplateKey(serverTemplate.getId(), serverTemplate.getName())); serverTemplate.addContainerSpec(containerSpec); templateStorage.update(serverTemplate); notificationService.notify(new ServerTemplateUpdated(serverTemplate)); if (containerSpec.getStatus().equals(KieContainerStatus.STARTED)) { List<Container> containers = kieServerInstanceManager.startContainer(serverTemplate, containerSpec); notificationService.notify(serverTemplate, containerSpec, containers); } }
@Override public synchronized void stopContainer(ContainerSpecKey containerSpecKey) { ServerTemplate serverTemplate = templateStorage.load(containerSpecKey.getServerTemplateKey().getId()); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + containerSpecKey.getServerTemplateKey().getId()); } ContainerSpec containerSpec = serverTemplate.getContainerSpec(containerSpecKey.getId()); if (containerSpec == null) { throw new KieServerControllerIllegalArgumentException("No container spec found for id " + containerSpecKey.getId() + " within server template with id " + serverTemplate.getId()); } containerSpec.setStatus(KieContainerStatus.STOPPED); templateStorage.update(serverTemplate); List<Container> containers = kieServerInstanceManager.stopContainer(serverTemplate, containerSpec); notificationService.notify(serverTemplate, containerSpec, containers); }
@Override public void scanNow(final ContainerSpecKey containerSpecKey) { ServerTemplate serverTemplate = templateStorage.load(containerSpecKey.getServerTemplateKey().getId()); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + containerSpecKey.getServerTemplateKey().getId()); } ContainerSpec containerSpec = serverTemplate.getContainerSpec(containerSpecKey.getId()); if (containerSpec == null) { throw new KieServerControllerIllegalArgumentException("No container spec found for id " + containerSpecKey.getId()); } List<Container> containers = kieServerInstanceManager.scanNow(serverTemplate, containerSpec); notificationService.notify(serverTemplate, containerSpec, containers); }
@Override public synchronized void deleteContainerSpec(String serverTemplateId, String containerSpecId) { ServerTemplate serverTemplate = templateStorage.load(serverTemplateId); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + serverTemplateId); } if (serverTemplate.hasContainerSpec(containerSpecId)) { ContainerSpec containerSpec = serverTemplate.getContainerSpec(containerSpecId); kieServerInstanceManager.stopContainer(serverTemplate, containerSpec); serverTemplate.deleteContainerSpec(containerSpecId); templateStorage.update(serverTemplate); notificationService.notify(new ServerTemplateUpdated(serverTemplate)); } else { throw new KieServerControllerIllegalArgumentException("Container " + containerSpecId + " not found"); } }
@Override public synchronized void updateServerTemplateConfig(String serverTemplateId, Capability capability, ServerConfig serverTemplateConfig) { ServerTemplate serverTemplate = templateStorage.load(serverTemplateId); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + serverTemplateId); } Map<Capability, ServerConfig> configs = serverTemplate.getConfigs(); configs.put(capability, serverTemplateConfig); serverTemplate.setConfigs(configs); templateStorage.update(serverTemplate); notificationService.notify(new ServerTemplateUpdated(serverTemplate)); }
@Override public synchronized void disconnect(KieServerInfo serverInfo) { ServerTemplate serverTemplate = templateStorage.load(serverInfo.getServerId()); if (serverTemplate != null) { logger.debug("Server id {} known to the controller, checking if given server exists", serverInfo.getServerId()); if (serverTemplate != null) { logger.info("Server {} disconnected from controller", serverInfo.getLocation()); ServerInstanceKey serverInstanceKey = ModelFactory.newServerInstanceKey(serverInfo.getServerId(), serverInfo.getLocation()); serverTemplate.deleteServerInstance(serverInstanceKey.getServerInstanceId()); templateStorage.update(serverTemplate); notifyOnDisconnect(serverInstanceKey, serverTemplate); } } }
public ContainerList getServerTemplateContainers(final String serverTemplateId, final String containerSpecId) { final ServerTemplate serverTemplate = getServerTemplate(serverTemplateId); if (serverTemplate.hasContainerSpec(containerSpecId) == false) { throw new KieServerControllerIllegalArgumentException("Server template with id " + serverTemplateId + " has no container with id " + containerSpecId); } final ContainerSpec containerSpec = serverTemplate.getContainerSpec(containerSpecId); Collection<Container> containers = kieServerInstanceManager.getContainers(serverTemplate, containerSpec); return new ContainerList(containers); }
public ContainerList getServerInstanceContainers(final String serverTemplateId, final String serverInstanceId) { final ServerTemplate serverTemplate = getServerTemplate(serverTemplateId); if (serverTemplate.hasServerInstanceId(serverInstanceId) == false) { throw new KieServerControllerIllegalArgumentException("Server template with id " + serverTemplateId + " has no instance with id " + serverInstanceId); } final ServerInstanceKey serverInstanceKey = serverTemplate.getServerInstance(serverInstanceId); Collection<Container> containers = kieServerInstanceManager.getContainers(serverInstanceKey); return new ContainerList(containers); }
@Override public ContainerSpec getContainerInfo(String serverTemplateId, String containerId) { final ServerTemplate serverTemplate = getServerTemplate(serverTemplateId); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + serverTemplateId); } final ContainerSpec containerSpec = serverTemplate.getContainerSpec(containerId); if (containerSpec == null) { throw new KieServerControllerIllegalArgumentException("Server template " + serverTemplateId + " does not have container with id " + containerId); } return containerSpec; }
@ApiOperation(value = "Disposes a specified KIE container in a specified KIE Server template.", code = 204) @ApiResponses(value = { @ApiResponse(code = 204, message = "KIE container successfully disposed"), @ApiResponse(code = 404, message = "Container Specification or KIE Server template not found"), @ApiResponse(code = 400, message = "Controller exception"), @ApiResponse(code = 500, message = "Unexpected error") }) @DELETE @Path("servers/{serverTemplateId}/containers/{containerId}") @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Response deleteContainerSpec(@Context HttpHeaders headers, @ApiParam(name = "serverTemplateId", value = "ID of the KIE Server template associated with the KIE container", required = true, example = "test-kie-server") @PathParam("serverTemplateId") String serverTemplateId, @ApiParam(name = "containerId", value = "ID of the KIE container to be disposed", required = true, example = "evaluation_1.0.0-SNAPSHOT") @PathParam("containerId") String containerSpecId) { try { specManagementService.deleteContainerSpec(serverTemplateId, containerSpecId); // return null to produce 204 return null; } catch (KieServerControllerIllegalArgumentException e) { return createCorrectVariant(e.getMessage(), headers, Response.Status.NOT_FOUND); } catch (KieServerControllerException e){ return createCorrectVariant(REQUEST_FAILED_TOBE_PROCESSED + e.getMessage(), headers, Response.Status.BAD_REQUEST); } catch (Exception e) { logger.error("Remove container with id {} from server template with id {} failed due to {}", containerSpecId, serverTemplateId, e.getMessage(), e); return createCorrectVariant("Unknown error " + e.getMessage(), headers, Response.Status.INTERNAL_SERVER_ERROR); } }
protected KieServicesClient getClient(final String url) { KieServicesClientProvider clientProvider = clientProviders.stream().filter(provider -> provider.supports(url)).findFirst().orElseThrow(() -> new KieServerControllerIllegalArgumentException("Kie Services Client Provider not found for url: " + url)); logger.debug("Using client provider {}", clientProvider); KieServicesClient client = clientProvider.get(url); logger.debug("Using client {}", client); if(client == null){ throw new KieServerControllerIllegalArgumentException("Kie Services Client not found for url: " + url); } return client; }
public void destroy() { eventSupport.fireBeforeServerStopped(this); kieServerActive.set(false); policyManager.stop(); // disconnect from controller KieServerController kieController = getController(); kieController.disconnect(getInfoInternal()); for (KieServerExtension extension : context.getServerExtensions()) { try { extension.destroy(this, this.context); this.context.unregisterServerExtension(extension); logger.info("{} has been successfully unregistered as server extension", extension); } catch (Exception e) { logger.error("Error when destroying server extension of type {}", extension, e); } } eventSupport.fireAfterServerStopped(this); }
@Override public synchronized void startContainer(ContainerSpecKey containerSpecKey) { ServerTemplate serverTemplate = templateStorage.load(containerSpecKey.getServerTemplateKey().getId()); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + containerSpecKey.getServerTemplateKey().getId()); } final ContainerSpec containerSpec = serverTemplate.getContainerSpec(containerSpecKey.getId()); if (containerSpec == null) { throw new KieServerControllerIllegalArgumentException("No container spec found for id " + containerSpecKey.getId() + " within server template with id " + serverTemplate.getId()); } containerSpec.setStatus(KieContainerStatus.STARTED); templateStorage.update(serverTemplate); List<Container> containers = kieServerInstanceManager.startContainer(serverTemplate, containerSpec); notificationService.notify(serverTemplate, containerSpec, containers); }
@Override public ServerTemplate getServerTemplate(String serverTemplateId) { final ServerTemplate serverTemplate = templateStorage.load(serverTemplateId); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + serverTemplateId); } return serverTemplate; }
@ApiOperation(value = "Deletes a specified KIE Server template", code = 204) @ApiResponses(value = { @ApiResponse(code = 204, message = "KIE Server template successfully deleted"), @ApiResponse(code = 404, message = "KIE Server template not found"), @ApiResponse(code = 400, message = "Controller exception"), @ApiResponse(code = 500, message = "Unexpected error") }) @DELETE @Path("servers/{serverTemplateId}") @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Response deleteServerTemplate(@Context HttpHeaders headers, @ApiParam(name = "serverTemplateId", value = "ID of the KIE Server template to be deleted", required = true, example = "test-kie-server") @PathParam("serverTemplateId") String serverTemplateId) { try { specManagementService.deleteServerTemplate(serverTemplateId); // return null to produce 204 return null; } catch (KieServerControllerIllegalArgumentException e) { return createCorrectVariant(e.getMessage(), headers, Response.Status.NOT_FOUND); } catch (KieServerControllerException e){ return createCorrectVariant(REQUEST_FAILED_TOBE_PROCESSED + e.getMessage(), headers, Response.Status.BAD_REQUEST); } catch (Exception e) { logger.error("Remove server template with id {} failed due to {}", serverTemplateId, e.getMessage(), e); return createCorrectVariant("Unknown error " + e.getMessage(), headers, Response.Status.INTERNAL_SERVER_ERROR); } }
@ApiOperation(value = "Returns all KIE Server templates", response = ServerTemplateList.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "KIE Server templates", examples = @Example(value = { @ExampleProperty(mediaType = JSON, value = SERVER_TEMPLATE_LIST_JSON), @ExampleProperty(mediaType = XML, value = SERVER_TEMPLATE_LIST_XML) })), @ApiResponse(code = 404, message = "KIE Server template not found"), @ApiResponse(code = 400, message = "Controller exception"), @ApiResponse(code = 500, message = "Unexpected error") }) @GET @Path("servers") @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Response listServerTemplates(@Context HttpHeaders headers) { String contentType = getContentType(headers); try { logger.debug("Received get server templates"); String response = marshal(contentType, specManagementService.listServerTemplates()); logger.debug("Returning response for get server templates: {}", response); return createCorrectVariant(response, headers, Response.Status.OK); } catch (KieServerControllerIllegalArgumentException e) { return createCorrectVariant(e.getMessage(), headers, Response.Status.NOT_FOUND); } catch (KieServerControllerException e){ return createCorrectVariant(REQUEST_FAILED_TOBE_PROCESSED + e.getMessage(), headers, Response.Status.BAD_REQUEST); } catch (Exception e) { logger.error("Get server templates failed due to {}", e.getMessage(), e); return createCorrectVariant("Unknown error " + e.getMessage(), headers, Response.Status.INTERNAL_SERVER_ERROR); } }