001: /*
002: * $Id: LOBType.java,v 1.17 2005/05/02 22:29:40 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2005 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.types;
042:
043: import java.io.DataInput;
044: import java.io.DataOutput;
045: import java.io.File;
046: import java.io.IOException;
047: import java.io.OutputStream;
048: import java.io.Reader;
049: import java.io.Writer;
050: import java.sql.Blob;
051: import java.sql.Clob;
052: import java.sql.DatabaseMetaData;
053: import java.sql.SQLException;
054:
055: import org.axiondb.AxionException;
056: import org.axiondb.DataType;
057: import org.axiondb.io.BufferedDataInputStream;
058: import org.axiondb.util.ExceptionConverter;
059:
060: /**
061: * A {@link DataType}representing a Large Object (LOB), for example a {@link Clob}or
062: * {@link Blob}.
063: *
064: * @version $Revision: 1.17 $ $Date: 2005/05/02 22:29:40 $
065: * @author James Burke
066: * @author Rodney Waldhoff
067: * @author Chuck Burdick
068: * @author Ahimanikya Satapathy
069: */
070: public class LOBType extends BaseDataType {
071:
072: public LOBType() {
073: }
074:
075: public int getJdbcType() {
076: return java.sql.Types.BLOB;
077: }
078:
079: public boolean isCaseSensitive() {
080: return true;
081: }
082:
083: public boolean accepts(Object value) {
084: return (null == value || value instanceof String
085: || value instanceof byte[] || value instanceof Clob
086: || value instanceof Blob || value instanceof LobLocator);
087: }
088:
089: public int getColumnDisplaySize() {
090: return 0;
091: }
092:
093: public Object convert(Object value) throws IllegalArgumentException {
094: if ("newlob()".equals(value)) {
095: LobLocator loc = _locatorFactory
096: .makeLobLocator(getLobDir());
097: return loc;
098: } else if (null == value) {
099: return null;
100: } else if (value instanceof LobLocator) {
101: return value;
102: } else if (value instanceof String) {
103: return new StringClob((String) value);
104: } else if (value instanceof byte[]) {
105: return new ByteArrayBlob((byte[]) value);
106: } else if (value instanceof Clob) {
107: return value;
108: } else if (value instanceof Blob) {
109: return value;
110: } else {
111: throw new IllegalArgumentException("Can't convert "
112: + value.getClass().getName() + " " + value + ".");
113: }
114: }
115:
116: public Blob toBlob(Object value) throws AxionException {
117: Object obj = convert(value);
118: if (obj instanceof Blob) {
119: return (Blob) obj;
120: } else if (obj instanceof LobLocator) {
121: return makeBlobSource((LobLocator) obj);
122: } else if (null == obj) {
123: return null;
124: } else {
125: throw new AxionException(
126: "Expected Blob or LobLocator, found " + obj);
127: }
128: }
129:
130: public Clob toClob(Object value) throws AxionException {
131: Object obj = convert(value);
132: if (obj instanceof Clob) {
133: return (Clob) obj;
134: } else if (obj instanceof LobLocator) {
135: return makeClobSource((LobLocator) obj);
136: } else if (null == obj) {
137: return null;
138: } else {
139: throw new AxionException(
140: "Expected Clob or LobLocator, found " + obj);
141: }
142: }
143:
144: public String toString(Object value) throws AxionException {
145: Clob clob = toClob(value);
146: if (null == clob) {
147: return null;
148: }
149: StringBuffer buf = new StringBuffer(2048);
150: Reader in = null;
151: try {
152: in = clob.getCharacterStream();
153: for (int c = in.read(); c != -1; c = in.read()) {
154: buf.append((char) c);
155: }
156: return buf.toString();
157: } catch (IOException e) {
158: throw new AxionException(e);
159: } catch (SQLException e) {
160: throw new AxionException(e);
161: } finally {
162: try {
163: in.close();
164: } catch (Exception e) {
165: }
166: }
167: }
168:
169: public String toString() {
170: return "LOB";
171: }
172:
173: public DataType makeNewInstance() {
174: return new LOBType();
175: }
176:
177: public Object read(DataInput in) throws IOException {
178: if (in.readBoolean()) {
179: return _locatorFactory.read(in);
180: }
181: return null;
182: }
183:
184: // @TODO: clean up the exception handling/conversion here
185: public void write(Object value, DataOutput out) throws IOException {
186: if (null == value) {
187: out.writeBoolean(false);
188: } else if (value instanceof LobLocator) {
189: out.writeBoolean(true);
190: _locatorFactory.write(((LobLocator) value), out);
191: } else if (value instanceof StringClob) {
192: LobLocator loc = writeStringClob((StringClob) value);
193: out.writeBoolean(true);
194: _locatorFactory.write(loc, out);
195: } else if (value instanceof ByteArrayBlob) {
196: LobLocator loc = writeByteArrayBlob((ByteArrayBlob) value);
197: out.writeBoolean(true);
198: _locatorFactory.write(loc, out);
199: } else if (value instanceof StringClob) {
200: LobLocator loc = writeStringClob((StringClob) value);
201: out.writeBoolean(true);
202: _locatorFactory.write(loc, out);
203: } else {
204: throw new IllegalArgumentException(value.getClass()
205: .getName()
206: + ":" + value);
207: }
208: }
209:
210: public File getLobDir() {
211: return _lobDir;
212: }
213:
214: public void setLobDir(File lobDir) throws AxionException {
215: FileLobSource.FS.closeInputStream(_in);
216: _lobDir = lobDir;
217: if (!_lobDir.exists() || _lobDir.isDirectory()) {
218: _locatorFactory = new FileLobLocatorFactory();
219: } else {
220: _in = FileLobSource.FS.openBufferedDIS(getLobDir());
221: _locatorFactory = new FileOffsetLobLocatorFactory();
222: }
223: }
224:
225: public short getSearchableCode() {
226: return DatabaseMetaData.typePredNone;
227: }
228:
229: protected BufferedDataInputStream getLobFile() {
230: return _in;
231: }
232:
233: protected LobLocator writeStringClob(StringClob value)
234: throws IOException {
235: LobLocator loc = _locatorFactory.makeLobLocator(getLobDir());
236: Writer clobout = null;
237: try {
238: ClobSource clob = makeClobSource(loc);
239: clobout = clob.setCharacterStream(0);
240: clobout.write(value.getString());
241: } catch (AxionException e) {
242: throw ExceptionConverter.convertToIOException(e);
243: } catch (SQLException e) {
244: throw ExceptionConverter.convertToIOException(e);
245: } finally {
246: try {
247: clobout.close();
248: } catch (Exception e) {
249: }
250: }
251: return loc;
252: }
253:
254: protected LobLocator writeByteArrayBlob(ByteArrayBlob value)
255: throws IOException {
256: LobLocator loc = _locatorFactory.makeLobLocator(getLobDir());
257: OutputStream blobout = null;
258: try {
259: BlobSource blob = makeBlobSource(loc);
260: blobout = blob.setBinaryStream(0);
261: blobout.write(value.getBytes());
262: } catch (AxionException e) {
263: throw ExceptionConverter.convertToIOException(e);
264: } catch (SQLException e) {
265: throw ExceptionConverter.convertToIOException(e);
266: } finally {
267: try {
268: blobout.close();
269: } catch (Exception e) {
270: }
271: }
272: return loc;
273: }
274:
275: // TODO: this may support transactional updates of the "select for update" style of
276: // clob writing
277: //private StringClob readStringClob(LobLocator loc) throws IOException,
278: // AxionException, AxionException {
279: // ClobSource src = makeClobSource(loc);
280: // StringBuffer buf = new StringBuffer();
281: // Reader in = src.getCharacterStream();
282: // for(int c = in.read(); c != -1; c = in.read()) {
283: // buf.append((char)c);
284: // }
285: // in.close();
286: // return new StringClob(buf.toString());
287: //}
288:
289: protected BlobSource makeBlobSource(LobLocator loc)
290: throws AxionException {
291: return new BlobSource(loc.getLobSource(getLobDir(),
292: getLobFile()));
293: }
294:
295: protected ClobSource makeClobSource(LobLocator loc)
296: throws AxionException {
297: return new ClobSource(loc.getLobSource(getLobDir(),
298: getLobFile()));
299: }
300:
301: private transient File _lobDir = null;
302: private BufferedDataInputStream _in = null;
303: private LobLocatorFactory _locatorFactory = new FileLobLocatorFactory();
304: }
|