001: /*
002: * Copyright (C) 1999-2005 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package org.mandarax.zkb;
020:
021: import java.util.Properties;
022: import java.util.zip.*;
023: import java.util.jar.*;
024: import java.io.*;
025: import java.net.URL;
026:
027: import org.mandarax.zkb.IOManager.Data;
028:
029: /**
030: * IO Manager implementation based on zip / jar files.
031: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
032: * @version 3.4 <7 March 05>
033: * @since 3.4
034: */
035:
036: public class ZIPIOManager extends AbstractIOManager {
037: /**
038: * Write zkb data.
039: * @param target an object describing the target (e.g. a file)
040: * @param metaData the serialized meta data
041: * @param kbData the serialized knowledge base
042: * @param ops the object persistency service used
043: * @param resourceData the serialized resource data (objects)
044: */
045: public void write(Object target, byte[] metaData, byte[] kbData,
046: byte[] resourceData, ObjectPersistencyService ops)
047: throws Exception {
048: OutputStream out = null;
049: if (target instanceof File) {
050: File f = (File) target;
051: if (f.isDirectory())
052: throw new IllegalArgumentException(
053: "target must not be a directory");
054: else
055: out = new FileOutputStream(f);
056: } else if (target instanceof OutputStream)
057: out = (OutputStream) target;
058: else
059: throw new IllegalArgumentException(
060: "This target type is not support: " + target);
061:
062: JarOutputStream jarOut = new JarOutputStream(out);
063:
064: JarEntry entry = new JarEntry(META);
065: jarOut.putNextEntry(entry);
066: jarOut.write(metaData);
067: jarOut.closeEntry();
068:
069: entry = new JarEntry(RESOURCES + "." + ops.getExtension());
070: jarOut.putNextEntry(entry);
071: jarOut.write(resourceData);
072: jarOut.closeEntry();
073:
074: entry = new JarEntry(KB);
075: jarOut.putNextEntry(entry);
076: jarOut.write(kbData);
077: jarOut.closeEntry();
078:
079: jarOut.close();
080: out.close();
081:
082: }
083:
084: /**
085: * Read zkb data.
086: * @param source the data source (e.g., a file)
087: * @return a data object
088: */
089: public Data read(Object source) throws Exception {
090: Data result = new Data();
091: File f = null;
092: InputStream in = null;
093:
094: if (source instanceof File) {
095: f = (File) source;
096: if (f.isDirectory()) {
097: throw new IllegalArgumentException(
098: "target must not be a directory");
099: } else
100: in = new FileInputStream(f);
101: } else if (source instanceof InputStream)
102: in = (InputStream) source;
103: else if (source instanceof URL)
104: in = ((URL) source).openStream();
105: else
106: throw new IllegalArgumentException(
107: "This source type is not support: " + source);
108:
109: JarInputStream jarIn = new JarInputStream(in);
110:
111: // meta data
112: byte[] metaData = this .readFromZip(jarIn, META);
113:
114: // read extension for resource file from meta data
115: result.metaData = new Properties();
116: result.metaData.load(new ByteArrayInputStream(metaData));
117: result.ops = getOPS(result.metaData);
118:
119: result.resourceData = this .readFromZip(jarIn, RESOURCES + "."
120: + result.ops.getExtension());
121: result.kbData = this .readFromZip(jarIn, KB);
122:
123: jarIn.close();
124: in.close();
125:
126: return result;
127: }
128:
129: /**
130: * Read the next piece of data from a zip file.
131: * @param jarIn the jar input stream
132: * @param expectedName the expected name of the next zip entry
133: * @return an array of bytes
134: */
135: private byte[] readFromZip(JarInputStream jarIn, String expectedName)
136: throws ZKBException {
137: // kb
138: try {
139: JarEntry jarEntry = jarIn.getNextJarEntry();
140: if (!jarEntry.getName().equals(expectedName)) {
141: error("Zip entry " + expectedName + " expected but "
142: + jarEntry.getName() + " found");
143: }
144: byte[] data = readData(jarIn);
145: jarIn.closeEntry();
146: return data;
147: } catch (IOException x) {
148: error("Cannot read zkb kb from zip entry", x);
149: }
150: return new byte[0];
151: }
152:
153: }
|