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.io.*;
023:
024: /**
025: * IO Manager implementation based on directories.
026: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
027: * @version 3.4 <7 March 05>
028: * @since 3.4
029: */
030:
031: public class DirectoryIOManager extends AbstractIOManager {
032: /**
033: * Write zkb data.
034: * @param target an object describing the target (e.g. a file)
035: * @param metaData the serialized meta data
036: * @param kbData the serialized knowledge base
037: * @param ops the object persistency service used
038: * @param resourceData the serialized resource data (objects)
039: */
040: public void write(Object target, byte[] metaData, byte[] kbData,
041: byte[] resourceData, ObjectPersistencyService ops)
042: throws Exception {
043: OutputStream out = null;
044: File dir = null;
045: if (target instanceof File) {
046: dir = (File) target;
047: if (!dir.isDirectory())
048: throw new IllegalArgumentException(
049: "target must be a directory");
050: } else
051: throw new IllegalArgumentException(
052: "This target type is not support: " + target);
053:
054: File f = this .getFile(dir, META);
055: out = new FileOutputStream(f);
056: out.write(metaData);
057: out.close();
058:
059: f = this .getFile(dir, RESOURCES + "." + ops.getExtension());
060: out = new FileOutputStream(f);
061: out.write(resourceData);
062: out.close();
063:
064: f = this .getFile(dir, KB);
065: out = new FileOutputStream(f);
066: out.write(kbData);
067: out.close();
068:
069: out.close();
070:
071: }
072:
073: /**
074: * Read zkb data.
075: * @param source the data source (e.g., a file)
076: * @return a data object
077: */
078: public Data read(Object source) throws Exception {
079: Data result = new Data();
080: File dir = null;
081:
082: if (source instanceof File) {
083: dir = (File) source;
084: if (!dir.isDirectory()) {
085: throw new IllegalArgumentException(
086: "target must be a directory");
087: }
088: } else
089: throw new IllegalArgumentException(
090: "This source type is not support: " + source);
091:
092: // meta data
093: byte[] metaData = readFromFile(dir, META);
094:
095: // read extension for resource file from meta data
096: result.metaData = new Properties();
097: result.metaData.load(new ByteArrayInputStream(metaData));
098: result.ops = getOPS(result.metaData);
099:
100: result.resourceData = readFromFile(dir, RESOURCES + "."
101: + result.ops.getExtension());
102: result.kbData = readFromFile(dir, KB);
103:
104: return result;
105: }
106:
107: /**
108: * Get the file.
109: * @param folder a folder
110: * @param fileName a file name
111: */
112: private File getFile(File folder, String fileName) {
113: return new File(folder, fileName);
114: }
115:
116: /**
117: * Read data from a file.
118: * @param dir the folder
119: * @param fileName the file name
120: */
121: private byte[] readFromFile(File dir, String fileName)
122: throws IOException {
123: File f = getFile(dir, fileName);
124: FileInputStream in = new FileInputStream(f);
125: byte[] data = this.readData(in);
126: in.close();
127: return data;
128: }
129:
130: }
|