01: /*
02: * Copyright (C) 1999-2005 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */
18:
19: package org.mandarax.zkb;
20:
21: import java.util.Properties;
22: import java.io.*;
23:
24: /**
25: * Abstract superclass for IOManager implementations.
26: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
27: * @version 3.4 <7 March 05>
28: * @since 3.4
29: */
30:
31: public abstract class AbstractIOManager implements IOManager {
32: /**
33: * Read data into a byte buffer.
34: * @param in an input stream
35: * @return an array of bytes
36: */
37: protected byte[] readData(InputStream in) throws IOException {
38: int c = -1;
39: ByteArrayOutputStream bout = new ByteArrayOutputStream();
40: while ((c = in.read()) != -1)
41: bout.write(c); // TODO improve efficiency
42: return bout.toByteArray();
43: }
44:
45: /**
46: * Log and (re-)throw an exception.
47: * @param msg the exception message
48: * @param x the cause
49: */
50: protected void error(String msg, Exception x) throws ZKBException {
51: LOG_ZKB.error(msg);
52: throw new ZKBException(msg, x);
53: }
54:
55: /**
56: * Log and (re-)throw an exception.
57: * @param msg the exception message
58: */
59: protected void error(String msg) throws ZKBException {
60: error(msg, null);
61: }
62:
63: /**
64: * Get the ops.
65: * @param metaData meta data
66: * @return teh OPS used
67: */
68: protected ObjectPersistencyService getOPS(Properties metaData)
69: throws ZKBException {
70: String opsClassName = metaData.getProperty(ZKBManager.OPS);
71: ObjectPersistencyService ops = null;
72: try {
73: LOG_ZKB.debug("Using OPS : " + opsClassName);
74: ops = (ObjectPersistencyService) Class
75: .forName(opsClassName).newInstance();
76: return ops;
77: } catch (ClassNotFoundException x) {
78: error("Cannot find class " + opsClassName, x);
79: } catch (InstantiationException x) {
80: error("Cannot instanciate " + opsClassName, x);
81: } catch (IllegalAccessException x) {
82: error("Cannot access class " + opsClassName, x);
83: }
84: return null;
85: }
86: }
|