KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > repository > usertypes > BlobUserType


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Jun 26, 2005
14  * @author Marc Batchelor
15  *
16  */

17
18 /*
19  * Class for persisting lists and other collections. Using serialization to persist these items.
20  * I'm using this class because I have a requirement to have a map element that may be a map or
21  * some other collection of strings.
22  */

23 package org.pentaho.repository.usertypes;
24
25 import java.io.InputStream JavaDoc;
26 import java.io.Serializable JavaDoc;
27 import java.sql.PreparedStatement JavaDoc;
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.sql.Types JavaDoc;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.hibernate.HibernateException;
34 import org.hibernate.type.SerializationException;
35 import org.hibernate.usertype.UserType;
36 import org.hibernate.util.EqualsHelper;
37 import org.hibernate.util.SerializationHelper;
38 import org.pentaho.core.system.PentahoSystem;
39 import org.pentaho.messages.Messages;
40
41 public class BlobUserType implements UserType {
42     private static Log log = LogFactory.getLog(BlobUserType.class);
43
44     private final static boolean debug = PentahoSystem.debug;
45
46     private static final int[] SQLTYPE = { Types.BLOB };
47
48     /*
49      * (non-Javadoc)
50      *
51      * @see org.hibernate.usertype.UserType#sqlTypes()
52      */

53     public int[] sqlTypes() {
54         return SQLTYPE;
55     }
56
57     /*
58      * (non-Javadoc)
59      *
60      * @see org.hibernate.usertype.UserType#returnedClass()
61      */

62     public Class JavaDoc returnedClass() {
63         return Serializable JavaDoc.class;
64     }
65
66     /*
67      * (non-Javadoc)
68      *
69      * @see org.hibernate.usertype.UserType#equals(java.lang.Object,
70      * java.lang.Object)
71      */

72     public boolean equals(Object JavaDoc arg0, Object JavaDoc arg1) throws HibernateException {
73         return EqualsHelper.equals(arg0, arg1);
74     }
75
76     /*
77      * (non-Javadoc)
78      *
79      * @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
80      */

81     public int hashCode(Object JavaDoc arg0) throws HibernateException {
82         return arg0.hashCode();
83     }
84
85     /*
86      * (non-Javadoc)
87      *
88      * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet,
89      * java.lang.String[], java.lang.Object)
90      */

91     public Object JavaDoc nullSafeGet(ResultSet JavaDoc arg0, String JavaDoc[] arg1, Object JavaDoc arg2) throws HibernateException, SQLException JavaDoc {
92         if (debug)
93             log.debug(Messages.getString("BLOBUTYPE.DEBUG_NULL_SAFE_GET")); //$NON-NLS-1$
94
InputStream JavaDoc is = arg0.getBinaryStream(arg1[0]);
95         if (is != null) {
96             return SerializationHelper.deserialize(is);
97         }
98         return null;
99     }
100
101     /*
102      * (non-Javadoc)
103      *
104      * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement,
105      * java.lang.Object, int)
106      */

107     public void nullSafeSet(PreparedStatement JavaDoc arg0, Object JavaDoc arg1, int arg2) throws HibernateException, SQLException JavaDoc {
108         if (debug)
109             log.debug(Messages.getString("BLOBUTYPE.DEBUG_NULL_SAFE_SET")); //$NON-NLS-1$
110
if (arg1 != null) {
111             try {
112                 arg0.setBytes(arg2, SerializationHelper.serialize((Serializable JavaDoc) arg1));
113             } catch (SerializationException ex) {
114                 log.error(Messages.getErrorString("BLOBUTYPE.ERROR_0001_SETTING_BLOB"), ex); //$NON-NLS-1$
115
throw new HibernateException(Messages.getErrorString("BLOBUTYPE.ERROR_0001_SETTING_BLOB"), ex); //$NON-NLS-1$
116
}
117         } else {
118             arg0.setNull(arg2, Types.BLOB);
119         }
120     }
121
122     /*
123      * (non-Javadoc)
124      *
125      * @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
126      */

127     public Object JavaDoc deepCopy(Object JavaDoc arg0) throws HibernateException {
128         return SerializationHelper.clone((Serializable JavaDoc) arg0);
129     }
130
131     /*
132      * (non-Javadoc)
133      *
134      * @see org.hibernate.usertype.UserType#isMutable()
135      */

136     public boolean isMutable() {
137         return true;
138     }
139
140     /*
141      * (non-Javadoc)
142      *
143      * @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
144      */

145     public Serializable JavaDoc disassemble(Object JavaDoc arg0) throws HibernateException {
146         return (Serializable JavaDoc) SerializationHelper.clone((Serializable JavaDoc) arg0);
147     }
148
149     /*
150      * (non-Javadoc)
151      *
152      * @see org.hibernate.usertype.UserType#assemble(java.io.Serializable,
153      * java.lang.Object)
154      */

155     public Object JavaDoc assemble(Serializable JavaDoc arg0, Object JavaDoc arg1) throws HibernateException {
156         return SerializationHelper.clone(arg0);
157     }
158
159     /*
160      * (non-Javadoc)
161      *
162      * @see org.hibernate.usertype.UserType#replace(java.lang.Object,
163      * java.lang.Object, java.lang.Object)
164      */

165     public Object JavaDoc replace(Object JavaDoc arg0, Object JavaDoc arg1, Object JavaDoc arg2) throws HibernateException {
166         return SerializationHelper.clone((Serializable JavaDoc) arg0);
167     }
168
169 }
170
Popular Tags