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.jpdl.par;
023:
024: import java.io.ByteArrayInputStream;
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.util.ArrayList;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.List;
031: import java.util.Map;
032: import java.util.zip.ZipEntry;
033: import java.util.zip.ZipInputStream;
034:
035: import org.jbpm.JbpmConfiguration;
036: import org.jbpm.JbpmException;
037: import org.jbpm.graph.def.ProcessDefinition;
038: import org.jbpm.jpdl.JpdlException;
039: import org.jbpm.jpdl.xml.Problem;
040: import org.jbpm.jpdl.xml.ProblemListener;
041: import org.jbpm.util.ClassLoaderUtil;
042: import org.jbpm.util.IoUtil;
043: import org.jbpm.util.XmlUtil;
044: import org.w3c.dom.Document;
045: import org.w3c.dom.Element;
046:
047: public class ProcessArchive implements ProblemListener {
048:
049: private static final long serialVersionUID = 1L;
050:
051: static List processArchiveParsers = getProcessArchiveParsers();
052:
053: // fields ///////////////////////////////////////////////////////////////////
054:
055: String name = "";
056: // maps entry-names (String) to byte-arrays (byte[])
057: Map entries = new HashMap();
058: List problems = new ArrayList();
059:
060: // constructors /////////////////////////////////////////////////////////////
061:
062: public ProcessArchive(ZipInputStream zipInputStream)
063: throws IOException {
064: ZipEntry zipEntry = zipInputStream.getNextEntry();
065: while (zipEntry != null) {
066: String entryName = zipEntry.getName();
067: byte[] bytes = IoUtil.readBytes(zipInputStream);
068: if (bytes != null) {
069: entries.put(entryName, bytes);
070: }
071: zipEntry = zipInputStream.getNextEntry();
072: }
073: }
074:
075: // parse the process definition from the contents ///////////////////////////
076:
077: public ProcessDefinition parseProcessDefinition() {
078: ProcessDefinition processDefinition = ProcessDefinition
079: .createNewProcessDefinition();
080: Iterator iter = processArchiveParsers.iterator();
081: while (iter.hasNext()) {
082: ProcessArchiveParser processArchiveParser = (ProcessArchiveParser) iter
083: .next();
084: processDefinition = processArchiveParser.readFromArchive(
085: this , processDefinition);
086: }
087: if (Problem.containsProblemsOfLevel(problems,
088: Problem.LEVEL_ERROR)) {
089: throw new JpdlException(problems);
090: }
091: return processDefinition;
092: }
093:
094: // methods for the process archive parsers //////////////////////////////////
095:
096: public String toString() {
097: return "process-archive(" + name + ")";
098: }
099:
100: public Map getEntries() {
101: return entries;
102: }
103:
104: public byte[] getEntry(String entryName) {
105: return (byte[]) entries.get(entryName);
106: }
107:
108: public InputStream getEntryInputStream(String entryName) {
109: return new ByteArrayInputStream(getEntry(entryName));
110: }
111:
112: public byte[] removeEntry(String entryName) {
113: return (byte[]) entries.remove(entryName);
114: }
115:
116: public InputStream removeEntryInputStream(String entryName) {
117: return new ByteArrayInputStream(removeEntry(entryName));
118: }
119:
120: public void addProblem(Problem problem) {
121: problems.add(problem);
122: }
123:
124: public List getProblems() {
125: return problems;
126: }
127:
128: public void resetProblems() {
129: problems = new ArrayList();
130: }
131:
132: static List getProcessArchiveParsers() {
133: List processArchiveParsers = new ArrayList();
134: try {
135: String resource = JbpmConfiguration.Configs
136: .getString("resource.parsers");
137: InputStream parsersStream = ClassLoaderUtil
138: .getStream(resource);
139: Document document = XmlUtil
140: .parseXmlInputStream(parsersStream);
141: Iterator iter = XmlUtil.elementIterator(document
142: .getDocumentElement(), "parser");
143: while (iter.hasNext()) {
144: Element element = (Element) iter.next();
145: String className = element.getAttribute("class");
146: ProcessArchiveParser processArchiveParser = (ProcessArchiveParser) ClassLoaderUtil
147: .loadClass(className).newInstance();
148: if (processArchiveParser instanceof ConfigurableParser) {
149: ((ConfigurableParser) processArchiveParser)
150: .configure(element);
151: }
152: processArchiveParsers.add(processArchiveParser);
153: }
154: } catch (Exception e) {
155: throw new JbpmException(
156: "couldn't parse process archive parsers (jbpm.parsers.xml)",
157: e);
158: }
159: return processArchiveParsers;
160: }
161:
162: }
|