Search in sources :

Example 1 with ResourceEntity

use of org.activiti.engine.impl.persistence.entity.ResourceEntity in project Activiti by Activiti.

the class DeployCmd method deploymentsDiffer.

protected boolean deploymentsDiffer(DeploymentEntity deployment, DeploymentEntity saved) {
    if (deployment.getResources() == null || saved.getResources() == null) {
        return true;
    }
    Map<String, ResourceEntity> resources = deployment.getResources();
    Map<String, ResourceEntity> savedResources = saved.getResources();
    for (String resourceName : resources.keySet()) {
        ResourceEntity savedResource = savedResources.get(resourceName);
        if (savedResource == null)
            return true;
        if (!savedResource.isGenerated()) {
            ResourceEntity resource = resources.get(resourceName);
            byte[] bytes = resource.getBytes();
            byte[] savedBytes = savedResource.getBytes();
            if (!Arrays.equals(bytes, savedBytes)) {
                return true;
            }
        }
    }
    return false;
}
Also used : ResourceEntity(org.activiti.engine.impl.persistence.entity.ResourceEntity)

Example 2 with ResourceEntity

use of org.activiti.engine.impl.persistence.entity.ResourceEntity in project Activiti by Activiti.

the class DeploymentManager method getBpmnModelById.

public BpmnModel getBpmnModelById(String processDefinitionId) {
    if (processDefinitionId == null) {
        throw new ActivitiIllegalArgumentException("Invalid process definition id : null");
    }
    // first try the cache
    BpmnModel bpmnModel = bpmnModelCache.get(processDefinitionId);
    if (bpmnModel == null) {
        ProcessDefinitionEntity processDefinition = findDeployedProcessDefinitionById(processDefinitionId);
        if (processDefinition == null) {
            throw new ActivitiObjectNotFoundException("no deployed process definition found with id '" + processDefinitionId + "'", ProcessDefinition.class);
        }
        // Fetch the resource
        String resourceName = processDefinition.getResourceName();
        ResourceEntity resource = Context.getCommandContext().getResourceEntityManager().findResourceByDeploymentIdAndResourceName(processDefinition.getDeploymentId(), resourceName);
        if (resource == null) {
            if (Context.getCommandContext().getDeploymentEntityManager().findDeploymentById(processDefinition.getDeploymentId()) == null) {
                throw new ActivitiObjectNotFoundException("deployment for process definition does not exist: " + processDefinition.getDeploymentId(), Deployment.class);
            } else {
                throw new ActivitiObjectNotFoundException("no resource found with name '" + resourceName + "' in deployment '" + processDefinition.getDeploymentId() + "'", InputStream.class);
            }
        }
        // Convert the bpmn 2.0 xml to a bpmn model
        BpmnXMLConverter bpmnXMLConverter = new BpmnXMLConverter();
        bpmnModel = bpmnXMLConverter.convertToBpmnModel(new BytesStreamSource(resource.getBytes()), false, false);
        bpmnModelCache.add(processDefinition.getId(), bpmnModel);
    }
    return bpmnModel;
}
Also used : BytesStreamSource(org.activiti.engine.impl.util.io.BytesStreamSource) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) ResourceEntity(org.activiti.engine.impl.persistence.entity.ResourceEntity) ProcessDefinitionEntity(org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) BpmnModel(org.activiti.bpmn.model.BpmnModel) BpmnXMLConverter(org.activiti.bpmn.converter.BpmnXMLConverter)

Example 3 with ResourceEntity

use of org.activiti.engine.impl.persistence.entity.ResourceEntity in project Activiti by Activiti.

the class JuelFormEngine method getFormTemplateString.

protected String getFormTemplateString(FormData formInstance, String formKey) {
    String deploymentId = formInstance.getDeploymentId();
    ResourceEntity resourceStream = Context.getCommandContext().getResourceEntityManager().findResourceByDeploymentIdAndResourceName(deploymentId, formKey);
    if (resourceStream == null) {
        throw new ActivitiObjectNotFoundException("Form with formKey '" + formKey + "' does not exist", String.class);
    }
    byte[] resourceBytes = resourceStream.getBytes();
    String encoding = "UTF-8";
    String formTemplateString = "";
    try {
        formTemplateString = new String(resourceBytes, encoding);
    } catch (UnsupportedEncodingException e) {
        throw new ActivitiException("Unsupported encoding of :" + encoding, e);
    }
    return formTemplateString;
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) ResourceEntity(org.activiti.engine.impl.persistence.entity.ResourceEntity) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException)

Example 4 with ResourceEntity

use of org.activiti.engine.impl.persistence.entity.ResourceEntity in project Activiti by Activiti.

the class BpmnDeployer method deploy.

public void deploy(DeploymentEntity deployment, Map<String, Object> deploymentSettings) {
    log.debug("Processing deployment {}", deployment.getName());
    List<ProcessDefinitionEntity> processDefinitions = new ArrayList<ProcessDefinitionEntity>();
    Map<String, ResourceEntity> resources = deployment.getResources();
    Map<String, BpmnModel> bpmnModelMap = new HashMap<String, BpmnModel>();
    final ProcessEngineConfigurationImpl processEngineConfiguration = Context.getProcessEngineConfiguration();
    for (String resourceName : resources.keySet()) {
        log.info("Processing resource {}", resourceName);
        if (isBpmnResource(resourceName)) {
            ResourceEntity resource = resources.get(resourceName);
            byte[] bytes = resource.getBytes();
            ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
            BpmnParse bpmnParse = bpmnParser.createParse().sourceInputStream(inputStream).setSourceSystemId(resourceName).deployment(deployment).name(resourceName);
            if (deploymentSettings != null) {
                // Schema validation if needed
                if (deploymentSettings.containsKey(DeploymentSettings.IS_BPMN20_XSD_VALIDATION_ENABLED)) {
                    bpmnParse.setValidateSchema((Boolean) deploymentSettings.get(DeploymentSettings.IS_BPMN20_XSD_VALIDATION_ENABLED));
                }
                // Process validation if needed
                if (deploymentSettings.containsKey(DeploymentSettings.IS_PROCESS_VALIDATION_ENABLED)) {
                    bpmnParse.setValidateProcess((Boolean) deploymentSettings.get(DeploymentSettings.IS_PROCESS_VALIDATION_ENABLED));
                }
            } else {
                // On redeploy, we assume it is validated at the first deploy
                bpmnParse.setValidateSchema(false);
                bpmnParse.setValidateProcess(false);
            }
            bpmnParse.execute();
            for (ProcessDefinitionEntity processDefinition : bpmnParse.getProcessDefinitions()) {
                processDefinition.setResourceName(resourceName);
                if (deployment.getTenantId() != null) {
                    // process definition inherits the tenant id
                    processDefinition.setTenantId(deployment.getTenantId());
                }
                String diagramResourceName = getDiagramResourceForProcess(resourceName, processDefinition.getKey(), resources);
                // time the process definition is added to the deployment-cache when diagram-generation has failed the first time.
                if (deployment.isNew()) {
                    if (processEngineConfiguration.isCreateDiagramOnDeploy() && diagramResourceName == null && processDefinition.isGraphicalNotationDefined()) {
                        try {
                            byte[] diagramBytes = IoUtil.readInputStream(processEngineConfiguration.getProcessDiagramGenerator().generateDiagram(bpmnParse.getBpmnModel(), "png", processEngineConfiguration.getActivityFontName(), processEngineConfiguration.getLabelFontName(), processEngineConfiguration.getAnnotationFontName(), processEngineConfiguration.getClassLoader()), null);
                            diagramResourceName = getProcessImageResourceName(resourceName, processDefinition.getKey(), "png");
                            createResource(diagramResourceName, diagramBytes, deployment);
                        } catch (Throwable t) {
                            // if anything goes wrong, we don't store the image (the process will still be executable).
                            log.warn("Error while generating process diagram, image will not be stored in repository", t);
                        }
                    }
                }
                processDefinition.setDiagramResourceName(diagramResourceName);
                processDefinitions.add(processDefinition);
                bpmnModelMap.put(processDefinition.getKey(), bpmnParse.getBpmnModel());
            }
        }
    }
    // check if there are process definitions with the same process key to prevent database unique index violation
    List<String> keyList = new ArrayList<String>();
    for (ProcessDefinitionEntity processDefinition : processDefinitions) {
        if (keyList.contains(processDefinition.getKey())) {
            throw new ActivitiException("The deployment contains process definitions with the same key '" + processDefinition.getKey() + "' (process id atrribute), this is not allowed");
        }
        keyList.add(processDefinition.getKey());
    }
    CommandContext commandContext = Context.getCommandContext();
    ProcessDefinitionEntityManager processDefinitionManager = commandContext.getProcessDefinitionEntityManager();
    DbSqlSession dbSqlSession = commandContext.getSession(DbSqlSession.class);
    for (ProcessDefinitionEntity processDefinition : processDefinitions) {
        List<TimerEntity> timers = new ArrayList<TimerEntity>();
        if (deployment.isNew()) {
            int processDefinitionVersion;
            ProcessDefinitionEntity latestProcessDefinition = null;
            if (processDefinition.getTenantId() != null && !ProcessEngineConfiguration.NO_TENANT_ID.equals(processDefinition.getTenantId())) {
                latestProcessDefinition = processDefinitionManager.findLatestProcessDefinitionByKeyAndTenantId(processDefinition.getKey(), processDefinition.getTenantId());
            } else {
                latestProcessDefinition = processDefinitionManager.findLatestProcessDefinitionByKey(processDefinition.getKey());
            }
            if (latestProcessDefinition != null) {
                processDefinitionVersion = latestProcessDefinition.getVersion() + 1;
            } else {
                processDefinitionVersion = 1;
            }
            processDefinition.setVersion(processDefinitionVersion);
            processDefinition.setDeploymentId(deployment.getId());
            String nextId = idGenerator.getNextId();
            String processDefinitionId = processDefinition.getKey() + ":" + processDefinition.getVersion() + ":" + // ACT-505
            nextId;
            // ACT-115: maximum id length is 64 charcaters
            if (processDefinitionId.length() > 64) {
                processDefinitionId = nextId;
            }
            processDefinition.setId(processDefinitionId);
            if (commandContext.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
                commandContext.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.ENTITY_CREATED, processDefinition));
            }
            removeObsoleteTimers(processDefinition);
            addTimerDeclarations(processDefinition, timers);
            removeExistingMessageEventSubscriptions(processDefinition, latestProcessDefinition);
            addMessageEventSubscriptions(processDefinition);
            removeExistingSignalEventSubScription(processDefinition, latestProcessDefinition);
            addSignalEventSubscriptions(processDefinition);
            dbSqlSession.insert(processDefinition);
            addAuthorizations(processDefinition);
            if (commandContext.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
                commandContext.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.ENTITY_INITIALIZED, processDefinition));
            }
            scheduleTimers(timers);
        } else {
            String deploymentId = deployment.getId();
            processDefinition.setDeploymentId(deploymentId);
            ProcessDefinitionEntity persistedProcessDefinition = null;
            if (processDefinition.getTenantId() == null || ProcessEngineConfiguration.NO_TENANT_ID.equals(processDefinition.getTenantId())) {
                persistedProcessDefinition = processDefinitionManager.findProcessDefinitionByDeploymentAndKey(deploymentId, processDefinition.getKey());
            } else {
                persistedProcessDefinition = processDefinitionManager.findProcessDefinitionByDeploymentAndKeyAndTenantId(deploymentId, processDefinition.getKey(), processDefinition.getTenantId());
            }
            if (persistedProcessDefinition != null) {
                processDefinition.setId(persistedProcessDefinition.getId());
                processDefinition.setVersion(persistedProcessDefinition.getVersion());
                processDefinition.setSuspensionState(persistedProcessDefinition.getSuspensionState());
            }
        }
        // Add to cache
        DeploymentManager deploymentManager = processEngineConfiguration.getDeploymentManager();
        deploymentManager.getProcessDefinitionCache().add(processDefinition.getId(), processDefinition);
        addDefinitionInfoToCache(processDefinition, processEngineConfiguration, commandContext);
        // Add to deployment for further usage
        deployment.addDeployedArtifact(processDefinition);
        createLocalizationValues(processDefinition.getId(), bpmnModelMap.get(processDefinition.getKey()).getProcessById(processDefinition.getKey()));
    }
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) ProcessDefinitionEntityManager(org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntityManager) CommandContext(org.activiti.engine.impl.interceptor.CommandContext) HashMap(java.util.HashMap) TimerEntity(org.activiti.engine.impl.persistence.entity.TimerEntity) DeploymentManager(org.activiti.engine.impl.persistence.deploy.DeploymentManager) ArrayList(java.util.ArrayList) BpmnModel(org.activiti.bpmn.model.BpmnModel) DbSqlSession(org.activiti.engine.impl.db.DbSqlSession) ByteArrayInputStream(java.io.ByteArrayInputStream) ResourceEntity(org.activiti.engine.impl.persistence.entity.ResourceEntity) ProcessDefinitionEntity(org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity) ProcessEngineConfigurationImpl(org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl) BpmnParse(org.activiti.engine.impl.bpmn.parser.BpmnParse)

Example 5 with ResourceEntity

use of org.activiti.engine.impl.persistence.entity.ResourceEntity in project Activiti by Activiti.

the class DeploymentBuilderImpl method addInputStream.

public DeploymentBuilder addInputStream(String resourceName, InputStream inputStream) {
    if (inputStream == null) {
        throw new ActivitiIllegalArgumentException("inputStream for resource '" + resourceName + "' is null");
    }
    byte[] bytes = IoUtil.readInputStream(inputStream, resourceName);
    ResourceEntity resource = new ResourceEntity();
    resource.setName(resourceName);
    resource.setBytes(bytes);
    deployment.addResource(resource);
    return this;
}
Also used : ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) ResourceEntity(org.activiti.engine.impl.persistence.entity.ResourceEntity)

Aggregations

ResourceEntity (org.activiti.engine.impl.persistence.entity.ResourceEntity)10 ActivitiException (org.activiti.engine.ActivitiException)4 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ArrayList (java.util.ArrayList)2 BpmnModel (org.activiti.bpmn.model.BpmnModel)2 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)2 DeploymentManager (org.activiti.engine.impl.persistence.deploy.DeploymentManager)2 ProcessDefinitionEntity (org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ZipEntry (java.util.zip.ZipEntry)1 BpmnXMLConverter (org.activiti.bpmn.converter.BpmnXMLConverter)1 BpmnParse (org.activiti.engine.impl.bpmn.parser.BpmnParse)1 ProcessEngineConfigurationImpl (org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl)1 DbSqlSession (org.activiti.engine.impl.db.DbSqlSession)1 CommandContext (org.activiti.engine.impl.interceptor.CommandContext)1