use of org.wso2.carbon.registry.core.Registry in project carbon-business-process by wso2.
the class TenantRepository method deploy.
/**
* Deploys a BPMN package in the Activiti engine. Each BPMN package has an entry in the registry.
* Checksum of the latest version of the BPMN package is stored in this entry.
* This checksum is used to determine whether a package is a new deployment
* (or a new version of an existing package) or a redeployment of an existing package.
* We have to ignor the later case. If a package is a new deployment, it is deployed in the Activiti engine.
*
* @param deploymentContext DeploymentContext
* @return true, if artifact was deployed, false, if the artifact has not changed & hence not deployed
* @throws DeploymentException if deployment fails
*/
// public boolean deploy(BPMNDeploymentContext deploymentContext) throws DeploymentException {
// ZipInputStream archiveStream = null;
//
// try {
//
// String deploymentName =
// FilenameUtils.getBaseName(deploymentContext.getBpmnArchive().getName());
//
// // Compare the checksum of the BPMN archive with the currently available checksum in the registry
// // to determine whether this is a new deployment.
// String checksum = "";
// try {
// checksum = Utils.getMD5Checksum(deploymentContext.getBpmnArchive());
// } catch (IOException e) {
// log.error("Checksum genration failed for IO operation",e);
// } catch (NoSuchAlgorithmException e) {
// log.error("Checksum genration Algorithm not found",e);
// }
//
// DeploymentMetaDataModel deploymentMetaDataModel =
// activitiDAO.selectTenantAwareDeploymentModel(tenantId.toString(), deploymentName);
//
// if (log.isDebugEnabled()) {
// log.debug("deploymentName=" + deploymentName + " checksum=" + checksum);
// log.debug("deploymentMetaDataModel=" + deploymentMetaDataModel.toString());
// }
//
// if (deploymentMetaDataModel != null) {
// if (checksum.equalsIgnoreCase(deploymentMetaDataModel.getCheckSum())) {
// return false;
// }
// }
//
// ProcessEngineImpl engine =
// (ProcessEngineImpl) BPMNServerHolder.getInstance().getEngine();
//
// RepositoryService repositoryService = engine.getRepositoryService();
// DeploymentBuilder deploymentBuilder =
// repositoryService.createDeployment().tenantId(tenantId.toString()).
// name(deploymentName);
// try {
// archiveStream =
// new ZipInputStream(new FileInputStream(deploymentContext.getBpmnArchive()));
// } catch (FileNotFoundException e) {
// String errMsg = "Archive stream not found for BPMN repsoitory";
// throw new DeploymentException(errMsg, e);
// }
//
// deploymentBuilder.addZipInputStream(archiveStream);
// Deployment deployment = deploymentBuilder.deploy();
//
// if (deploymentMetaDataModel == null) {
//
// deploymentMetaDataModel = new DeploymentMetaDataModel();
// deploymentMetaDataModel.setPackageName(deploymentName);
// deploymentMetaDataModel.setCheckSum(checksum);
// deploymentMetaDataModel.setTenantID(tenantId.toString());
// deploymentMetaDataModel.setId(deployment.getId());
//
// //call for insertion
// this.activitiDAO.insertDeploymentMetaDataModel(deploymentMetaDataModel);
// } else {
// //call for update
// deploymentMetaDataModel.setCheckSum(checksum);
// this.activitiDAO.updateDeploymentMetaDataModel(deploymentMetaDataModel);
// }
//
// } finally {
// if (archiveStream != null) {
// try {
// archiveStream.close();
// } catch (IOException e) {
// log.error("Could not close archive stream", e);
// }
// }
// }
//
// return true;
// }
public void deploy(BPMNDeploymentContext deploymentContext) throws DeploymentException {
ZipInputStream archiveStream = null;
try {
String deploymentName = FilenameUtils.getBaseName(deploymentContext.getBpmnArchive().getName());
// Compare the checksum of the BPMN archive with the currently available checksum in the registry to determine whether this is a new deployment.
String checksum = Utils.getMD5Checksum(deploymentContext.getBpmnArchive());
RegistryService registryService = BPMNServerHolder.getInstance().getRegistryService();
Registry tenantRegistry = registryService.getConfigSystemRegistry(tenantId);
String deploymentRegistryPath = BPMNConstants.BPMN_REGISTRY_PATH + BPMNConstants.REGISTRY_PATH_SEPARATOR + deploymentName;
Resource deploymentEntry = null;
if (tenantRegistry.resourceExists(deploymentRegistryPath)) {
deploymentEntry = tenantRegistry.get(deploymentRegistryPath);
} else {
// This is a new deployment
deploymentEntry = tenantRegistry.newCollection();
}
String latestChecksum = deploymentEntry.getProperty(BPMNConstants.LATEST_CHECKSUM_PROPERTY);
if (latestChecksum != null && checksum.equals(latestChecksum)) {
// This is a server restart
return;
}
deploymentEntry.setProperty(BPMNConstants.LATEST_CHECKSUM_PROPERTY, checksum);
// Deploy the package in the Activiti engine
ProcessEngine engine = BPMNServerHolder.getInstance().getEngine();
RepositoryService repositoryService = engine.getRepositoryService();
DeploymentBuilder deploymentBuilder = repositoryService.createDeployment().tenantId(tenantId.toString()).name(deploymentName);
archiveStream = new ZipInputStream(new FileInputStream(deploymentContext.getBpmnArchive()));
deploymentBuilder.addZipInputStream(archiveStream);
deploymentBuilder.deploy();
tenantRegistry.put(deploymentRegistryPath, deploymentEntry);
} catch (Exception e) {
String errorMessage = "Failed to deploy the archive: " + deploymentContext.getBpmnArchive().getName();
log.error(errorMessage, e);
// Remove the deployment archive from the tenant's deployment folder
File deploymentArchive = new File(repoFolder, deploymentContext.getBpmnArchive().getName());
FileUtils.deleteQuietly(deploymentArchive);
log.info("Removing the faulty archive : " + deploymentContext.getBpmnArchive().getName());
throw new DeploymentException(errorMessage, e);
} finally {
if (archiveStream != null) {
try {
archiveStream.close();
} catch (IOException e) {
log.error("Could not close archive stream", e);
}
}
}
}
use of org.wso2.carbon.registry.core.Registry in project carbon-business-process by wso2.
the class TenantRepository method fixDeployments.
/**
* Information about BPMN deployments are recorded in 3 places:
* Activiti database, Registry and the file system (deployment folder).
* If information about a particular deployment is not recorded in all these 3 places, BPS may not work correctly.
* Therefore, this method checks whether deployments are recorded in all these places and undeploys packages, if
* they are missing in few places in an inconsistent way.
* <p/>
* As there are 3 places, there are 8 ways a package can be placed. These cases are handled as follows:
* (1) Whenever a package is not in the deployment folder, it is undeploye (this covers 4 combinations).
* (2) If a package is in all 3 places, it is a proper deployment and it is left untouched.
* (3) If a package is only in the deployment folder, it is a new deployment. This will be handled by the deployer.
* (4) If a package is in the deployment folder AND it is in either registry or Activiti DB (but not both), then it is an inconsistent deployment. This will be undeployed.
*/
// public void fixDeployments() {
//
// // get all deployments in the deployment folder
// List<String> fileArchiveNames = new ArrayList<String>();
// File[] fileDeployments = repoFolder.listFiles();
// if (fileDeployments != null) {
// for (File fileDeployment : fileDeployments) {
// String deploymentName = FilenameUtils.getBaseName(fileDeployment.getName());
// fileArchiveNames.add(deploymentName);
// }
// } else {
// log.error("File deployments returned null for tenant" + tenantId);
// }
//
//
// // get all deployments in the Activiti DB
// List<String> activitiDeploymentNames = new ArrayList<String>();
// ProcessEngine engine = BPMNServerHolder.getInstance().getEngine();
// RepositoryService repositoryService = engine.getRepositoryService();
// List<Deployment> tenantDeployments =
// repositoryService.createDeploymentQuery().deploymentTenantId(tenantId.toString())
// .list();
// for (Deployment deployment : tenantDeployments) {
// String deploymentName = deployment.getName();
// activitiDeploymentNames.add(deploymentName);
// }
//
// // get all deployments in the registry
// List<String> metaDataDeploymentNames = new ArrayList<String>();
// List<DeploymentMetaDataModel> deploymentMetaDataModelList =
// activitiDAO.selectAllDeploymentModel();
//
// int deploymentMetaDataModelListSize = deploymentMetaDataModelList.size();
//
// for (int i = 0; i < deploymentMetaDataModelListSize; i++) {
// DeploymentMetaDataModel deploymentMetaDataModel =
// deploymentMetaDataModelList.get(i);
//
// if (deploymentMetaDataModel != null) {
// String deploymentMetadataName = deploymentMetaDataModel.getPackageName();
// metaDataDeploymentNames.add(deploymentMetadataName);
// }
// }
//
// // construct the union of all deployments
// Set<String> allDeploymentNames = new HashSet<String>();
// allDeploymentNames.addAll(fileArchiveNames);
// allDeploymentNames.addAll(activitiDeploymentNames);
// allDeploymentNames.addAll(metaDataDeploymentNames);
//
// for (String deploymentName : allDeploymentNames) {
//
// if (!(fileArchiveNames.contains(deploymentName))) {
// if (log.isDebugEnabled()) {
// log.debug(deploymentName +
// " has been removed from the deployment folder. Undeploying the package...");
// }
// undeploy(deploymentName, true);
// } else {
// if (activitiDeploymentNames.contains(deploymentName) &&
// !metaDataDeploymentNames.contains(deploymentName)) {
// if (log.isDebugEnabled()) {
// log.debug(deploymentName +
// " is missing in the registry. Undeploying the package to avoid inconsistencies...");
// }
// undeploy(deploymentName, true);
// }
//
// if (!activitiDeploymentNames.contains(deploymentName) &&
// metaDataDeploymentNames.contains(deploymentName)) {
// if (log.isDebugEnabled()) {
// log.debug(deploymentName +
// " is missing in the BPS database. Undeploying the package to avoid inconsistencies...");
// }
// undeploy(deploymentName, true);
// }
// }
// }
// }
public void fixDeployments() throws BPSFault {
// get all deployments in the deployment folder
List<String> fileArchiveNames = new ArrayList<String>();
File[] fileDeployments = repoFolder.listFiles();
for (File fileDeployment : fileDeployments) {
String deploymentName = FilenameUtils.getBaseName(fileDeployment.getName());
fileArchiveNames.add(deploymentName);
}
// get all deployments in the Activiti DB
List<String> activitiDeploymentNames = new ArrayList<String>();
ProcessEngine engine = BPMNServerHolder.getInstance().getEngine();
RepositoryService repositoryService = engine.getRepositoryService();
List<Deployment> tenantDeployments = repositoryService.createDeploymentQuery().deploymentTenantId(tenantId.toString()).list();
for (Deployment deployment : tenantDeployments) {
String deploymentName = deployment.getName();
activitiDeploymentNames.add(deploymentName);
}
// get all deployments in the registry
List<String> registryDeploymentNames = new ArrayList<String>();
try {
RegistryService registryService = BPMNServerHolder.getInstance().getRegistryService();
Registry tenantRegistry = registryService.getConfigSystemRegistry(tenantId);
String deploymentRegistryPath = BPMNConstants.BPMN_REGISTRY_PATH;
if (tenantRegistry.resourceExists(deploymentRegistryPath)) {
Collection registryDeployments = (Collection) tenantRegistry.get(deploymentRegistryPath);
String[] deploymentPaths = registryDeployments.getChildren();
for (String deploymentPath : deploymentPaths) {
String deploymentName = deploymentPath.substring(deploymentPath.lastIndexOf("/") + 1, deploymentPath.length());
registryDeploymentNames.add(deploymentName);
}
}
} catch (RegistryException e) {
String msg = "Failed to obtain BPMN deployments from the Registry.";
log.error(msg, e);
throw new BPSFault(msg, e);
}
// construct the union of all deployments
Set<String> allDeploymentNames = new HashSet<String>();
allDeploymentNames.addAll(fileArchiveNames);
allDeploymentNames.addAll(activitiDeploymentNames);
allDeploymentNames.addAll(registryDeploymentNames);
for (String deploymentName : allDeploymentNames) {
try {
if (!(fileArchiveNames.contains(deploymentName))) {
if (log.isDebugEnabled()) {
log.debug(deploymentName + " has been removed from the deployment folder. Undeploying the package...");
}
undeploy(deploymentName, true);
} else {
if (activitiDeploymentNames.contains(deploymentName) && !registryDeploymentNames.contains(deploymentName)) {
if (log.isDebugEnabled()) {
log.debug(deploymentName + " is missing in the registry. Undeploying the package to avoid inconsistencies...");
}
undeploy(deploymentName, true);
}
if (!activitiDeploymentNames.contains(deploymentName) && registryDeploymentNames.contains(deploymentName)) {
if (log.isDebugEnabled()) {
log.debug(deploymentName + " is missing in the BPS database. Undeploying the package to avoid inconsistencies...");
}
undeploy(deploymentName, true);
}
}
} catch (BPSFault e) {
String msg = "Failed undeploy inconsistent deployment: " + deploymentName;
log.error(msg, e);
throw new BPSFault(msg, e);
}
}
}
use of org.wso2.carbon.registry.core.Registry in project carbon-business-process by wso2.
the class HumanTaskStore method findHumanTaskPackageInFileSystem.
/**
* This method provides the extracted human task package location in file system.
* If human task package does not exist in file system then package will be exported from registry.
* If human task package does not exist in registry then package will be imported to registry from file system.
* If registry and file location don't have extracted human task package content then exception will be thrown
*
* @param dudao
* @return File
* @throws HumanTaskDeploymentException
*/
private File findHumanTaskPackageInFileSystem(DeploymentUnitDAO dudao, File archiveFile) throws Exception {
String duName = dudao.getName();
log.info("Looking for HumanTask package in file system for deployment unit " + duName);
File humanTaskDUDirectory = new File(humanTaskDeploymentRepo, duName);
String registryCollectionPath = HumanTaskPackageRepositoryUtils.getResourcePathForHumanTaskPackageContent(dudao.getPackageName(), dudao.getName());
try {
if (humanTaskDUDirectory.exists()) {
if (!tenantConfigRegistry.resourceExists(registryCollectionPath)) {
// Import human task package content to registry from file system
repository.restoreHumanTaskPackageContentInRegistry(dudao, archiveFile);
}
return humanTaskDUDirectory;
} else {
if (tenantConfigRegistry.resourceExists(registryCollectionPath)) {
if (!humanTaskDUDirectory.exists() && !humanTaskDUDirectory.mkdirs()) {
String errMsg = "Error creating HumanTask deployment unit repository for " + "tenant " + tenantId;
log.error(errMsg);
log.error("Failed to load HumanTask deployment unit " + duName + " due to above error.");
throw new HumanTaskDeploymentException(errMsg);
}
// Export human task package content from registry to file system
RegistryClientUtils.exportFromRegistry(humanTaskDUDirectory, registryCollectionPath, tenantConfigRegistry);
return humanTaskDUDirectory;
} else {
String errMsg = "Expected resource: " + registryCollectionPath + " not found in the registry";
log.error(errMsg);
throw new HumanTaskDeploymentException(errMsg);
}
}
} catch (RegistryException re) {
String errMsg = "Error while exporting deployment unit: " + duName + " to file system from the " + "registry.";
log.error(errMsg, re);
throw new HumanTaskDeploymentException(errMsg, re);
}
}
use of org.wso2.carbon.registry.core.Registry in project carbon-business-process by wso2.
the class HumanTaskStore method deploy.
/**
* Handles the deployment steps for the master node and salve node in the cluster
* @param humanTaskFile
* @throws Exception
*/
public void deploy(File humanTaskFile) throws Exception {
// Currently using the registry read/write mount property to determine whether this node is a master node
// or a salve node.
// Handle this properly with hazelcast leader for cluster scenario TODO
boolean isMasterServer = !isServerReadOnly();
// Versions of this ht package is already deployed
boolean isExistingPackage = false;
// Exactly matching ht package already exists
boolean isPackageReload = false;
DeploymentUnitDAO currentlyActiveTaskPackage = null;
String md5sum = HumanTaskStoreUtils.getMD5Checksum(humanTaskFile);
String packageName = FilenameUtils.removeExtension(humanTaskFile.getName());
List<DeploymentUnitDAO> existingDeploymentUnitsForPackage = getExistingDeploymentUnitsForPackage(packageName.trim());
if (existingDeploymentUnitsForPackage != null && existingDeploymentUnitsForPackage.size() > 0) {
isExistingPackage = true;
for (DeploymentUnitDAO dao : existingDeploymentUnitsForPackage) {
if ((dao.getStatus() == (TaskPackageStatus.ACTIVE))) {
// extract the currently active task package
currentlyActiveTaskPackage = dao;
if (dao.getChecksum().equals(md5sum)) {
// Check whether the md5sum matches the active task package.
isPackageReload = true;
}
}
}
}
// We will only allow writes to db only for the master node to avoid duplicate version creation
if (isExistingPackage && isPackageReload) {
// Reload the existing versions of the human task package . No need of creating a new version of the package
// This could be due to server restart, deployment of the same package or master node has already deployed the
// new version of the package
// First check if the currently active task package is already loaded
String activePackageName = loadedPackages.get(currentlyActiveTaskPackage.getPackageName());
if (activePackageName != null && activePackageName.equals(currentlyActiveTaskPackage.getName())) {
if (log.isDebugEnabled()) {
log.debug("This task package and its previous versions are already loaded " + activePackageName);
}
// This task package and its previous versions are already loaded , hence return
return;
}
// Load the existing versions of the package
reloadExistingTaskVersions(existingDeploymentUnitsForPackage, humanTaskFile, md5sum, isMasterServer);
return;
}
// New version of the package is being deployed on top of the existing version
if (isExistingPackage && !isPackageReload) {
if (isMasterServer) {
// Retire the existing version of the package and deploy the new version
// This could be two scenarios. Server restart with new version and deploying on existing version.
String activePackageName = loadedPackages.get(currentlyActiveTaskPackage.getPackageName());
if (activePackageName == null) {
// This is a server restart, we need to load existing versions
reloadExistingTaskVersions(existingDeploymentUnitsForPackage, humanTaskFile, md5sum, isMasterServer);
}
long newVersion = getNextVersion();
HumanTaskDeploymentUnit newDeploymentUnit = createNewDeploymentUnit(humanTaskFile, tenantId, newVersion, md5sum);
validateTaskConfig(newDeploymentUnit);
retireTaskPackageConfigurations(currentlyActiveTaskPackage.getName());
currentlyActiveTaskPackage.setStatus(TaskPackageStatus.RETIRED);
updateDeploymentUnitDao(currentlyActiveTaskPackage);
// Retiring of currently active package is complete.
// Create and deploy new version
deployNewTaskVersion(newDeploymentUnit, newVersion);
// Add new version of human task package to registry
// Update the zip and package properties in the registry
repository.handleNewHumanTaskPackageAddition(newDeploymentUnit, humanTaskFile);
// Successfully deployed the packages.
return;
} else {
// Cannot allow creation of a new version from slave nodes, deploy the new version on the master node
// first to avoid duplicate version creation
// Write log, issue warning and return
log.warn("Cannot deploy new version of the task in slave node. Hence deploy the task archive in master" + "node fist");
return;
}
}
if (!isMasterServer) {
// Issue warning, write warn message and return as we cannot allow deployment of new versions on slave nodes
// before deployment of the ht package in the master node
log.warn("Cannot deploy a new version on the package on the salve node first, " + "Deploy the package on the master node first");
return;
}
// Create new version of deployment unit
// Process the human task configurations
// Store deployment unit information to the db
// Deploy axis2 services
// Adding HumanTask package the registry.
long newVersion = getNextVersion();
HumanTaskDeploymentUnit newDeploymentUnit = createNewDeploymentUnit(humanTaskFile, tenantId, newVersion, md5sum);
validateTaskConfig(newDeploymentUnit);
deployNewTaskVersion(newDeploymentUnit, newVersion);
repository.handleNewHumanTaskPackageAddition(newDeploymentUnit, humanTaskFile);
return;
}
use of org.wso2.carbon.registry.core.Registry in project carbon-business-process by wso2.
the class HumanTaskPackageRepository method updateHumanTaskPackageProperties.
/**
* Update the properties of existing human task package in the registry
*
* @param humanTaskDeploymentUnit
* @throws RegistryException
*/
private void updateHumanTaskPackageProperties(HumanTaskDeploymentUnit humanTaskDeploymentUnit) throws RegistryException {
String packageLocation = HumanTaskPackageRepositoryUtils.getResourcePathForHumanTaskPackage(humanTaskDeploymentUnit);
Resource humanTaskPackage = configRegistry.get(packageLocation);
humanTaskPackage.setProperty(HumanTaskConstants.HUMANTASK_PACKAGE_PROP_LATEST_CHECKSUM, humanTaskDeploymentUnit.getMd5sum());
if (log.isDebugEnabled()) {
log.debug(humanTaskDeploymentUnit.getPackageName() + " updated checksum to: " + humanTaskDeploymentUnit.getMd5sum());
}
humanTaskPackage.setProperty(HumanTaskConstants.HUMANTASK_PACKAGE_PROP_STATUS, String.valueOf(humanTaskDeploymentUnit.getTaskPackageStatus()));
humanTaskPackage.setProperty(HumanTaskConstants.HUMANTASK_PACKAGE_PROP_LATEST_VERSION, Long.toString(humanTaskDeploymentUnit.getVersion()));
configRegistry.put(packageLocation, humanTaskPackage);
}
Aggregations