001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.orm.ibatis.support;
018:
019: import java.io.ByteArrayOutputStream;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.io.ObjectInputStream;
023: import java.io.ObjectOutputStream;
024: import java.sql.PreparedStatement;
025: import java.sql.ResultSet;
026: import java.sql.SQLException;
027:
028: import org.springframework.jdbc.support.lob.LobCreator;
029: import org.springframework.jdbc.support.lob.LobHandler;
030:
031: /**
032: * iBATIS TypeHandler implementation for arbitrary objects that get serialized to BLOBs.
033: * Retrieves the LobHandler to use from SqlMapClientFactoryBean at config time.
034: *
035: * <p>Can also be defined in generic iBATIS mappings, as DefaultLobCreator will
036: * work with most JDBC-compliant database drivers. In this case, the field type
037: * does not have to be BLOB: For databases like MySQL and MS SQL Server, any
038: * large enough binary type will work.
039: *
040: * @author Juergen Hoeller
041: * @since 1.1.5
042: * @see org.springframework.orm.ibatis.SqlMapClientFactoryBean#setLobHandler
043: */
044: public class BlobSerializableTypeHandler extends AbstractLobTypeHandler {
045:
046: /**
047: * Constructor used by iBATIS: fetches config-time LobHandler from
048: * SqlMapClientFactoryBean.
049: * @see org.springframework.orm.ibatis.SqlMapClientFactoryBean#getConfigTimeLobHandler
050: */
051: public BlobSerializableTypeHandler() {
052: super ();
053: }
054:
055: /**
056: * Constructor used for testing: takes an explicit LobHandler.
057: */
058: protected BlobSerializableTypeHandler(LobHandler lobHandler) {
059: super (lobHandler);
060: }
061:
062: protected void setParameterInternal(PreparedStatement ps,
063: int index, Object value, String jdbcType,
064: LobCreator lobCreator) throws SQLException, IOException {
065:
066: if (value != null) {
067: ByteArrayOutputStream baos = new ByteArrayOutputStream();
068: ObjectOutputStream oos = new ObjectOutputStream(baos);
069: try {
070: oos.writeObject(value);
071: oos.flush();
072: lobCreator
073: .setBlobAsBytes(ps, index, baos.toByteArray());
074: } finally {
075: oos.close();
076: }
077: } else {
078: lobCreator.setBlobAsBytes(ps, index, null);
079: }
080: }
081:
082: protected Object getResultInternal(ResultSet rs, int index,
083: LobHandler lobHandler) throws SQLException, IOException {
084:
085: InputStream is = lobHandler.getBlobAsBinaryStream(rs, index);
086: if (is != null) {
087: ObjectInputStream ois = new ObjectInputStream(is);
088: try {
089: return ois.readObject();
090: } catch (ClassNotFoundException ex) {
091: throw new SQLException(
092: "Could not deserialize BLOB contents: "
093: + ex.getMessage());
094: } finally {
095: ois.close();
096: }
097: } else {
098: return null;
099: }
100: }
101:
102: public Object valueOf(String s) {
103: return s;
104: }
105:
106: }
|