Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.kie.remote.services.rest;
  
  import static org.kie.remote.services.rest.async.cmd.DeploymentCmd.DEPLOYMENT_UNIT;
  import static org.kie.remote.services.rest.async.cmd.DeploymentCmd.JOB_ID;
  import static org.kie.remote.services.rest.async.cmd.DeploymentCmd.JOB_TYPE;
  
  import java.util.List;
 import java.util.Map;
 
 import  javax.inject.Inject;
 import  javax.inject.Singleton;
 
 import  org.jbpm.kie.services.impl.KModuleDeploymentService;
 import  org.jbpm.kie.services.impl.KModuleDeploymentUnit;
 import  org.jbpm.runtime.manager.impl.deploy.DeploymentDescriptorImpl;
 import  org.jbpm.services.api.DefinitionService;
 import  org.jbpm.services.api.DeploymentService;
 import  org.jbpm.services.api.RuntimeDataService;
 import  org.jbpm.services.api.model.DeployedUnit;
 import  org.jbpm.services.api.model.ProcessDefinition;
 import  org.jbpm.services.cdi.Kjar;
 import  org.kie.internal.executor.api.CommandContext;
 import  org.kie.internal.executor.api.ExecutorService;
 import  org.kie.internal.query.QueryContext;
 import  org.kie.internal.runtime.conf.DeploymentDescriptor;
 import  org.kie.internal.runtime.conf.MergeMode;
 import  org.kie.internal.runtime.conf.RuntimeStrategy;
 import  org.kie.services.client.serialization.jaxb.impl.deploy.JaxbDeploymentDescriptor;
 import  org.kie.services.client.serialization.jaxb.impl.deploy.JaxbDeploymentJobResult;
 import  org.kie.services.client.serialization.jaxb.impl.deploy.JaxbDeploymentUnit;
 import  org.kie.services.client.serialization.jaxb.impl.deploy.JaxbDeploymentUnit.JaxbDeploymentStatus;
 import  org.kie.services.client.serialization.jaxb.impl.deploy.JaxbDeploymentUnitList;
 import  org.kie.services.client.serialization.jaxb.impl.process.JaxbProcessDefinition;
 import  org.kie.services.client.serialization.jaxb.impl.process.JaxbProcessDefinitionList;
 import  org.slf4j.Logger;
 import  org.slf4j.LoggerFactory;
 
 @Singleton
 public class DeployResourceBase extends ResourceBase {
 
     private static final Logger logger = LoggerFactory.getLogger(DeployResourceBase.class);
     
     /* KIE information and processing */
    
     @Inject
     @Kjar
     private KModuleDeploymentService deploymentService;
 
     @Inject
    
     @Inject
     private RuntimeDataService runtimeDataService;
  
     @Inject
     private DefinitionService bpmn2DataService;
     
     /* Async */
     
     @Inject
     private ExecutorService jobExecutor;
     
     private final AtomicLong jobIdGen = new AtomicLong(0);
     
     @Inject
     private JobResultManager jobResultMgr;
     
     // Helper methods ------------------------------------------------------------------------------------------------------------
     
    
Convert the KModuleDeploymentUnit instance from the DeploymentService to a JaxbDeploymentUnit usable by the REST service.

Parameters:
kDepUnit The KModuleDeploymentUnit instance
Returns:
A JaxbDeploymentUnit instance
 
     static JaxbDeploymentUnit convertKModuleDepUnitToJaxbDepUnit(KModuleDeploymentUnit kDepUnit ) { 
         JaxbDeploymentUnit jDepUnit = new JaxbDeploymentUnit(
                 kDepUnit.getGroupId(),
                 kDepUnit.getArtifactId(),
                 kDepUnit.getVersion(),
                 kDepUnit.getKbaseName(),
                 kDepUnit.getKsessionName());
         jDepUnit.setStrategy(kDepUnit.getStrategy());
         return jDepUnit;
     }
   
    
Create a KModuleDeploymentUnit instance using the given information

Parameters:
deploymentId The deployment id
descriptor The optional JaxbDeploymentDescriptor instance with additional information
Returns:
The KModuleDeploymentUnit instance
    // pkg scope for tests
    static KModuleDeploymentUnit createDeploymentUnit(String deploymentId, JaxbDeploymentDescriptor descriptor) {
        String [] gavKK = deploymentId.split(":");
        KModuleDeploymentUnit deployUnit = new KModuleDeploymentUnit(gavKK[0], gavKK[1], gavKK[2]);
        ifgavKK.length > 3 ) { 
            deployUnit.setKbaseName(gavKK[3]);
        }
        ifgavKK.length > 4 ) { 
            deployUnit.setKsessionName(gavKK[4]);
        }
        if (descriptor != null) {
            DeploymentDescriptor realDepDesc = convertToDeploymentDescriptor(descriptor);
            deployUnit.setDeploymentDescriptor(realDepDesc);
        }
        return deployUnit;
    }

    
Convert the received JaxbDeploymentDescriptor instance to a DeploymentDescriptor instance that the DeploymentService can process.

Parameters:
jaxbDepDesc The received JaxbDeploymentDescriptor instance
Returns:
A DeploymentDescriptor instance
    private static DeploymentDescriptor convertToDeploymentDescriptor( JaxbDeploymentDescriptor jaxbDepDesc ) { 
        DeploymentDescriptorImpl depDescImpl = new DeploymentDescriptorImpl(jaxbDepDesc.getPersistenceUnit());
       
        depDescImpl.setAuditPersistenceUnit(jaxbDepDesc.getAuditPersistenceUnit());
        depDescImpl.setAuditMode(jaxbDepDesc.getAuditMode());
        depDescImpl.setPersistenceMode(jaxbDepDesc.getPersistenceMode());
        depDescImpl.setRuntimeStrategy(jaxbDepDesc.getRuntimeStrategy());
        depDescImpl.setMarshallingStrategies(jaxbDepDesc.getMarshallingStrategies());
        depDescImpl.setEventListeners(jaxbDepDesc.getEventListeners());
        depDescImpl.setTaskEventListeners(jaxbDepDesc.getTaskEventListeners());
        depDescImpl.setGlobals(jaxbDepDesc.getGlobals());
        depDescImpl.setWorkItemHandlers(jaxbDepDesc.getWorkItemHandlers());
        depDescImpl.setEnvironmentEntries(jaxbDepDesc.getEnvironmentEntries()); 
        depDescImpl.setConfiguration(jaxbDepDesc.getConfiguration()); 
        depDescImpl.setRequiredRoles(jaxbDepDesc.getRequiredRoles());
        depDescImpl.setClasses(jaxbDepDesc.getClasses());
        
        return depDescImpl;
    }
  
    // Deployment methods ---------------------------------------------------------------------------------------------------------
    
    
Determines the status of a deployment

Parameters:
checkDeploymentService Whether or not to use the DeploymentService when checking the status
Returns:
A JaxbDeploymentUnit representing the status
    public JaxbDeploymentUnit determineStatus(String deploymentIdboolean checkDeploymentService) { 
        
        JaxbDeploymentUnit jaxbDepUnit;
        ifcheckDeploymentService ) { 
            DeployedUnit deployedUnit = .getDeployedUnit(deploymentId);
            // Deployed
            ifdeployedUnit != null ) {
                KModuleDeploymentUnit depUnit = (KModuleDeploymentUnit) deployedUnit.getDeploymentUnit();
                jaxbDepUnit = convertKModuleDepUnitToJaxbDepUnit(depUnit);
                jaxbDepUnit.setStatus(JaxbDeploymentStatus.DEPLOYED);
                return jaxbDepUnit;
            } 
        }
        
        // Most recent job? 
        JaxbDeploymentJobResult jobResult = .getMostRecentJob(deploymentId);
        ifjobResult != null ) { 
            jaxbDepUnit = jobResult.getDeploymentUnit();
            return jaxbDepUnit;
        }
        
        // Nonexistent? 
        String [] gavKK = deploymentId.split(":");
        switchgavKK.length ) { 
        case 3:
            jaxbDepUnit = new JaxbDeploymentUnit(gavKK[0], gavKK[1], gavKK[2]);
            break;
        case 4:
            jaxbDepUnit = new JaxbDeploymentUnit(gavKK[0], gavKK[1], gavKK[2], gavKK[3], null);
            break;
        case 5:
            jaxbDepUnit = new JaxbDeploymentUnit(gavKK[0], gavKK[1], gavKK[2], gavKK[3], gavKK[4]);
            break;
        default:
            throw KieRemoteRestOperationException.notFound("Invalid deployment id: " + deploymentId);
        }
        jaxbDepUnit.setStatus(JaxbDeploymentStatus.NONEXISTENT);
        return jaxbDepUnit;
    }
    public JaxbDeploymentJobResult submitDeployJob(String deploymentIdString strategyString mergeMode, JaxbDeploymentDescriptor deployDescriptor ) { 
        JaxbDeploymentJobResult jobResult;
        DeployedUnit deployedUnit = .getDeployedUnit(deploymentId);
        ifdeployedUnit != null ) { 
            // If the deployment unit already exists, request can not be completed..
            KModuleDeploymentUnit kDepUnit = (KModuleDeploymentUnit) deployedUnit.getDeploymentUnit();
            JaxbDeploymentUnit jaxbDepUnit = convertKModuleDepUnitToJaxbDepUnit(kDepUnit);
            jobResult = new JaxbDeploymentJobResult(
                    null
                    "The deployment already exists and must be first undeployed!"
                   jaxbDepUnit
                   ..toString());
            jobResult.setSuccess(false);
        } else { 
            KModuleDeploymentUnit deploymentUnit = createDeploymentUnit(deploymentIddeployDescriptor);
            ifstrategy != null ) { 
                strategy = strategy.toUpperCase();
                RuntimeStrategy runtimeStrategy;
                try { 
                    runtimeStrategy = RuntimeStrategy.valueOf(strategy);
                } catchIllegalArgumentException iae ) { 
                    throw KieRemoteRestOperationException.badRequest("Runtime strategy '" + strategy + "' does not exist.");
                }
                deploymentUnit.setStrategy(runtimeStrategy);
            }
            if (mergeMode != null) {
                mergeMode = mergeMode.toUpperCase();
                MergeMode mode;
                try {
                    mode = MergeMode.valueOf(mergeMode);
                }  catchIllegalArgumentException iae ) {
                    throw KieRemoteRestOperationException.badRequest("Merge mode '" + mergeMode + "' does not exist.");
                }
                deploymentUnit.setMergeMode(mode);
            }
            jobResult = scheduleDeploymentJobRequest(deploymentId.deploymentUnit);
        }
        return jobResult;
    }
    
    
Schedules a deploy or undeploy job with the jbpm-executor for execution.

Parameters:
jobType The type of job: deploy or undeploy
deploymentUnit The deployment unit that should be acted upon
Returns:
The initial status of the job in a JaxbDeploymentJobResult instance
    private JaxbDeploymentJobResult scheduleDeploymentJobRequest(String deploymentIdJobType jobType, KModuleDeploymentUnit deploymentUnit) { 
        CommandContext ctx = new CommandContext();
        ctx.setData(,  deploymentUnit);
        ctx.setData(jobType);
        ctx.setData("businessKey"deploymentId);
        ctx.setData("retries", 0);
	ctx.setData("owner", ExecutorService.EXECUTOR_ID);
       
        String jobTypeLower = jobType.toString().toLowerCase();
       
        String jobId = "" + System.currentTimeMillis() + "-" + .incrementAndGet();
        ctx.setData(jobId);
        JaxbDeploymentJobResult jobResult = new JaxbDeploymentJobResult(
                jobId,
                jobTypeLower + " job accepted."
                convertKModuleDepUnitToJaxbDepUnit(deploymentUnit), jobType.toString());
        jobResult.getDeploymentUnit().setStatus(JaxbDeploymentStatus.ACCEPTED);
        .debug( "{} job [{}] for deployment '{}' created."jobType.toString(), jobIddeploymentUnit.getIdentifier());
        .putJob(jobResult.getJobId(), jobResultjobType);
        Long executorJobId;
        try { 
            executorJobId = .scheduleRequest(DeploymentCmd.class.getName(), ctx);
            jobResult.setIdentifier(executorJobId);
            jobResult.setSuccess(true);
        } catchException e ) { 
            String msg = "Unable to " + jobType.toString().toLowerCase() 
                    + " deployment '" + deploymentId + "': "
                    + e.getClass().getSimpleName() + " thrown [" + e.getMessage() + "]";
            .error( msge );
            jobResult.setExplanation(msg);
            jobResult.setSuccess(false);
        } 
                
        return jobResult;
    }
   
    public JaxbDeploymentJobResult submitUndeployJob(String deploymentId) { 
        DeployedUnit deployedUnit = .getDeployedUnit(deploymentId);
        JaxbDeploymentJobResult jobResult
        ifdeployedUnit != null ) { 
            KModuleDeploymentUnit deploymentUnit = (KModuleDeploymentUnit) deployedUnit.getDeploymentUnit();
            jobResult = scheduleDeploymentJobRequest(deploymentId.deploymentUnit);
        } else { 
            JaxbDeploymentUnit depUnit = determineStatus(deploymentIdfalse);
           
            String explanation;
            switchdepUnit.getStatus()) { 
            case ACCEPTED: // deployment service (above) has not found it, so it must be still deploying
            case DEPLOYED: // minor race condition between the deployment service and this code
            case DEPLOYING: // obvious.. 
                explanation = "The deployment can not be undeployed because the initial deployment has not yet fully completed.";
                break;
            case DEPLOY_FAILED: 
                explanation = "The deployment can not be undeployed because the initial deployment failed.";
                break;
            case NONEXISTENT:
            case UNDEPLOYED:
            case UNDEPLOYING:
                explanation = "The deployment can not be undeployed because it has already been undeployed (or is currently being undeployed)";
                break;
            case UNDEPLOY_FAILED: // from the last request
                explanation = "The last undeployment failed, but the deployment unit is no longer present (and can not be undeployed, thus). "
                        + "There is probably a very high load on this server. Turning on debugging may provide insight.";
                       .debug("Stack trace:"new Throwable()); 
                break;
            default
                throw new IllegalStateException("Unknown deployment unit status: " + depUnit.getStatus());
            }
            jobResult = new JaxbDeploymentJobResult(nullexplanationdepUnit..toString() );
            jobResult.setSuccess(false);
        }
        return jobResult;
    }
    public JaxbDeploymentUnitList getDeploymentList(int [] pageInfoint maxNumResults) { 
        List<StringdeploymentIds = new ArrayList<String>(.getDeploymentIds());
        Collections.sort(deploymentIds);
        JaxbDeploymentUnitList jaxbDepUnitList = new JaxbDeploymentUnitList();
        List<JaxbDeploymentUnit> depUnitList = jaxbDepUnitList.getDeploymentUnitList();
        forString deploymentId : deploymentIds ) { 
            DeployedUnit deployedUnit = .getDeployedUnit(deploymentId);
            ifdeployedUnit != null ) { 
                JaxbDeploymentUnit jaxbDepUnit = convertKModuleDepUnitToJaxbDepUnit((KModuleDeploymentUnit) deployedUnit.getDeploymentUnit());
                jaxbDepUnit.setStatus(JaxbDeploymentStatus.DEPLOYED);
                depUnitList.add(jaxbDepUnit);
                ifdepUnitList.size() >= maxNumResults) { 
                    // pagination parameters indicate that no more than current list is needed
                    break;
                }
            }
        }
        JaxbDeploymentUnitList resultList = paginateAndCreateResult(pageInfodepUnitListnew JaxbDeploymentUnitList());
        return resultList;
    }
    
    public JaxbProcessDefinitionList getProcessDefinitionList(int [] pageInfoint maxNumResults) { 
        List<StringdeploymentIds = new ArrayList<String>(.getDeploymentIds());
        Collections.sort(deploymentIds);
        
        JaxbProcessDefinitionList jaxbProcDefList = new JaxbProcessDefinitionList();
        List<JaxbProcessDefinition> procDefList = jaxbProcDefList.getProcessDefinitionList();
        forString deploymentId : deploymentIds ) {
            fillProcessDefinitionList(deploymentIdpageInfomaxNumResultsprocDefList);
                
            ifprocDefList.size() == maxNumResults) { 
                // pagination parameters indicate that no more than current list is needed
                break;
            }
        }
       
        JaxbProcessDefinitionList resultList = paginateAndCreateResult(pageInfoprocDefListnew JaxbProcessDefinitionList());
        return resultList;
    }
   
    public void fillProcessDefinitionList(String deploymentIdint [] pageInfoint maxNumResultsList<JaxbProcessDefinition> procDefList) { 
        List<StringprocessIdList = .;
        try { 
            processIdList = new ArrayList<String>(.getProcessIds(deploymentIdnew QueryContext(pageInfo[0], pageInfo[1])));
            Collections.sort(processIdList);
        } catchException e) { 
            // possibly because the deployment is being modified and not fully un/deployed.. (un/deploy*ing*) 
            .debug( "Unable to retrieve process ids for deployment '{}': {}"deploymentIde.getMessage(), e);
        }
        forString processId : processIdList ) { 
            ProcessDefinition processAssetDesc;
            try { 
                processAssetDesc = .getProcessesByDeploymentIdProcessId(deploymentIdprocessId); 
                ifprocessAssetDesc == null ) { 
                    .error( "No process definition information available for process definition '{}' in deployment '{}'!"
                            processIddeploymentId); 
                    continue;
                }
            } catchException e ) {
                // possibly because the deployment is being modified and not fully un/deployed.. (un/deploy*ing*) 
                .debug( "Unable to retrieve process definition for process '{}' in deployment '{}': {}"
                        processIddeploymentIde.getMessage(), e);
                continue
            }
            JaxbProcessDefinition jaxbProcDef = convertProcAssetDescToJaxbProcDef(processAssetDesc);
            Map<StringStringvariables
            try { 
                variables = .getProcessVariables(deploymentIdprocessId);
            } catchException e) { 
                // possibly because the deployment is being modified and not fully un/deployed.. (un/deploy*ing*) 
                .debug( "Unable to retrieve process definition data for process '{}' in deployment '{}': {}"
                        processIddeploymentIde.getMessage(), e);
                continue
            }
            jaxbProcDef.setVariables(variables);
            procDefList.add(jaxbProcDef);
            ifprocDefList.size() == maxNumResults) { 
                // pagination parameters indicate that no more than current list is needed
                break;
            }
        }
    }
New to GrepCode? Check out our FAQ X