001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2005, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jbpm.ant;
023:
024: import java.io.File;
025: import java.io.FileInputStream;
026: import java.io.FileNotFoundException;
027: import java.io.IOException;
028: import java.util.ArrayList;
029: import java.util.Iterator;
030: import java.util.List;
031: import java.util.zip.ZipInputStream;
032:
033: import org.apache.tools.ant.BuildException;
034: import org.apache.tools.ant.DirectoryScanner;
035: import org.apache.tools.ant.taskdefs.MatchingTask;
036: import org.apache.tools.ant.types.FileSet;
037: import org.jbpm.JbpmConfiguration;
038: import org.jbpm.JbpmContext;
039: import org.jbpm.graph.def.ProcessDefinition;
040:
041: /**
042: * ant task for deploying process archives.
043: */
044: public class DeployProcessTask extends MatchingTask {
045:
046: String jbpmCfg = null;
047: File process = null;
048: List fileSets = new ArrayList();
049:
050: public void execute() throws BuildException {
051: try {
052: // get the JbpmConfiguration
053: JbpmConfiguration jbpmConfiguration = AntHelper
054: .getJbpmConfiguration(jbpmCfg);
055:
056: // if attribute process is set, deploy that process file
057: if (process != null) {
058: log("deploying par " + process.getAbsolutePath()
059: + " ...");
060: deploy(process, jbpmConfiguration);
061: }
062:
063: // loop over all files that are specified in the filesets
064: Iterator iter = fileSets.iterator();
065: while (iter.hasNext()) {
066: FileSet fileSet = (FileSet) iter.next();
067: DirectoryScanner dirScanner = fileSet
068: .getDirectoryScanner(getProject());
069: String[] fileSetFiles = dirScanner.getIncludedFiles();
070:
071: for (int i = 0; i < fileSetFiles.length; i++) {
072: String fileName = fileSetFiles[i];
073: File file = new File(fileName);
074: if (!file.isFile()) {
075: file = new File(dirScanner.getBasedir(),
076: fileName);
077: }
078:
079: // deploy the file, specified in a fileset element
080: log("deploying process archive " + file + " ...");
081: deploy(file, jbpmConfiguration);
082: }
083: }
084:
085: } catch (Exception e) {
086: e.printStackTrace();
087: throw new BuildException(
088: "couldn't deploy process archives : "
089: + e.getMessage());
090: }
091: }
092:
093: void deploy(File file, JbpmConfiguration jbpmConfiguration)
094: throws IOException, FileNotFoundException {
095:
096: JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
097: try {
098: ZipInputStream zipInputStream = new ZipInputStream(
099: new FileInputStream(file));
100: ProcessDefinition processDefinition = ProcessDefinition
101: .parseParZipInputStream(zipInputStream);
102: jbpmContext.deployProcessDefinition(processDefinition);
103:
104: } finally {
105: jbpmContext.close();
106: }
107: }
108:
109: public void addFileset(FileSet fileSet) {
110: this .fileSets.add(fileSet);
111: }
112:
113: public void setJbpmCfg(String jbpmCfg) {
114: this .jbpmCfg = jbpmCfg;
115: }
116:
117: public void setProcess(File process) {
118: this.process = process;
119: }
120: }
|