001 package org.apache.ojb.broker.accesslayer.conversions;
002
003 /* Copyright 2002-2005 The Apache Software Foundation
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 import java.io.ByteArrayInputStream;
019 import java.io.ByteArrayOutputStream;
020 import java.io.ObjectInputStream;
021 import java.io.ObjectOutputStream;
022 import java.util.zip.GZIPInputStream;
023 import java.util.zip.GZIPOutputStream;
024
025 import org.apache.ojb.broker.util.Base64;
026
027 /**
028 * this implementation of the FieldConversion interface converts
029 * between java.lang.Objects values and char[] values in the rdbms.
030 * This conversion is useful to store serialized objects in database
031 * columns. For an example have a look at the mapping of
032 * org.apache.ojb.odmg.collections.DlistEntry.
033 *
034 * @author Thomas Mahler, Scott C. Gray
035 * @version $Id: Object2Base64StringFieldConversion.java,v 1.1 2007-08-24 22:17:31 ewestfal Exp $
036 */
037 public class Object2Base64StringFieldConversion implements FieldConversion
038 {
039 private ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
040 private Base64.OutputStream uuOut =
041 new Base64.OutputStream(byteOut, Base64.ENCODE, false);
042 private GZIPOutputStream gzipOut;
043 private ObjectOutputStream objOut;
044
045 /*
046 ** @see FieldConversion#javaToSql(Object)
047 */
048 public Object javaToSql(Object source)
049 {
050 synchronized (byteOut)
051 {
052 try
053 {
054 if (gzipOut == null)
055 {
056 gzipOut = new GZIPOutputStream(uuOut);
057 objOut = new ObjectOutputStream(gzipOut);
058 }
059
060 /*
061 ** Clear out the byte array
062 */
063 byteOut.reset();
064
065 objOut.writeObject(source);
066 objOut.flush();
067 gzipOut.finish();
068 gzipOut.flush();
069
070 return (byteOut.toString());
071 }
072 catch (Throwable t)
073 {
074 throw new ConversionException(t);
075 }
076 }
077 }
078
079 /*
080 ** @see FieldConversion#sqlToJava(Object)
081 */
082 public Object sqlToJava(Object source)
083 {
084 try
085 {
086 ByteArrayInputStream stringIn =
087 new ByteArrayInputStream(((String) source).getBytes());
088 Base64.InputStream uuIn =
089 new Base64.InputStream(stringIn, Base64.DECODE, false);
090 GZIPInputStream gzipIn = new GZIPInputStream(uuIn);
091 ObjectInputStream objIn = new ObjectInputStream(gzipIn);
092 Object result = objIn.readObject();
093
094 objIn.close();
095 return result;
096 }
097 catch (Throwable t)
098 {
099 throw new ConversionException(t);
100 }
101 }
102
103 }