Guest User

Activiti CRC Workflow Deployer

a guest
May 17th, 2011
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.util.List;
  6.  
  7. import org.activiti.engine.ActivitiException;
  8. import org.activiti.engine.RepositoryService;
  9. import org.activiti.engine.repository.Deployment;
  10. import org.apache.commons.io.FileUtils;
  11. import org.apache.commons.io.IOUtils;
  12. import org.springframework.beans.BeansException;
  13. import org.springframework.beans.FatalBeanException;
  14. import org.springframework.beans.factory.InitializingBean;
  15. import org.springframework.context.ApplicationContext;
  16. import org.springframework.context.ApplicationContextAware;
  17. import org.springframework.core.io.Resource;
  18.  
  19. public class WorkflowDeployer implements InitializingBean, ApplicationContextAware {
  20.  
  21.     static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(WorkflowDeployer.class);
  22.  
  23.     private Resource[] deploymentResources;
  24.  
  25.     public void setDeploymentResources(Resource[] resources) {
  26.         this.deploymentResources = resources;
  27.     }
  28.  
  29.     private String category;
  30.  
  31.     public void setCategory(String category) {
  32.         this.category = category;
  33.     }
  34.  
  35.     ApplicationContext appCtx;
  36.  
  37.     @Override
  38.     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  39.         this.appCtx = applicationContext;
  40.  
  41.     }
  42.  
  43.     @Override
  44.     public void afterPropertiesSet() throws Exception {
  45.         if (category == null)
  46.             throw new FatalBeanException("Missing property: category");
  47.  
  48.         if (deploymentResources != null) {
  49.             RepositoryService repositoryService = appCtx.getBean(RepositoryService.class);
  50.             for (Resource r : deploymentResources) {
  51.  
  52.                 String deploymentName = category + "_" + r.getFilename();
  53.                 String resourceName = r.getFilename();
  54.  
  55.                 boolean doDeploy = true;
  56.                 List<Deployment> deployments = repositoryService.createDeploymentQuery().deploymentName(deploymentName).orderByDeploymenTime().desc().list();
  57.                 if (!deployments.isEmpty()) {
  58.                     Deployment existing = deployments.get(0);
  59.                     try {
  60.                         InputStream in = repositoryService.getResourceAsStream(existing.getId(), resourceName);
  61.                         if (in != null) {
  62.                            
  63.                             File f = File.createTempFile("deployment","xml", new File(System.getProperty("java.io.tmpdir"), "deployment-temp"));
  64.                             f.deleteOnExit();
  65.                             OutputStream out=new FileOutputStream(f);
  66.                             IOUtils.copy(in,out);
  67.                             in.close();
  68.                             out.close();
  69.                            
  70.                             doDeploy =  (FileUtils.checksumCRC32(f) != FileUtils.checksumCRC32(r.getFile()));
  71.                            
  72.                         }
  73.                         else
  74.                             throw new ActivitiException("Unable to read resource " + resourceName + ", input stream is null");
  75.                     } catch (ActivitiException ex) {
  76.                         //do nothing, simply re-deploy
  77.                         log.error("Unable to read " + resourceName + " of deployment " + existing.getName() + ", id: " + existing.getId() + ", will re-deploy");
  78.                     }
  79.  
  80.                 }
  81.  
  82.                 if (doDeploy) {
  83.                     repositoryService.createDeployment()
  84.                             .name(deploymentName)
  85.                             .addInputStream(resourceName, r.getInputStream())
  86.                             .deploy();
  87.                     log.warn("Deployed BPMN20 resource " + r.getFilename());
  88.                 }
  89.  
  90.             }
  91.         }
  92.     }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment