KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > efs > openreports > util > EncryptedStringUserType


1 /*
2  * Copyright (C) 2005 Erik Swenson - erik@oreports.com
3  *
4  * This program is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */

19
20 package org.efs.openreports.util;
21
22 import java.io.Serializable JavaDoc;
23 import java.sql.*;
24
25 import org.apache.commons.codec.DecoderException;
26 import org.apache.commons.codec.EncoderException;
27 import org.apache.commons.codec.net.BCodec;
28 import org.apache.log4j.Logger;
29
30 import org.hibernate.Hibernate;
31 import org.hibernate.HibernateException;
32 import org.hibernate.usertype.*;
33
34 public class EncryptedStringUserType implements UserType
35 {
36     protected static Logger log = Logger.getLogger(EncryptedStringUserType.class);
37     
38     private static final int[] TYPES = new int[]{Types.VARCHAR};
39     
40     private BCodec bCodec = new BCodec();
41
42     public int[] sqlTypes()
43     {
44         return TYPES;
45     }
46
47     public Class JavaDoc returnedClass()
48     {
49         return String JavaDoc.class;
50     }
51
52     public boolean equals(Object JavaDoc x, Object JavaDoc y) throws HibernateException
53     {
54         if (x == y)
55         {
56             return true;
57         }
58
59         if (x == null)
60         {
61             return false;
62         }
63
64         return x.equals(y);
65     }
66
67     public Object JavaDoc nullSafeGet(ResultSet rs, String JavaDoc[] names, Object JavaDoc owner)
68             throws HibernateException, SQLException
69     {
70         String JavaDoc encryptedValue = (String JavaDoc) Hibernate.STRING.nullSafeGet(rs, names[0]);
71
72         if (encryptedValue != null)
73         {
74             return decrypt(encryptedValue);
75         }
76         else
77         {
78             return null;
79         }
80     }
81
82     public void nullSafeSet(PreparedStatement pStmt, Object JavaDoc value, int index)
83             throws HibernateException, SQLException
84     {
85         if (value != null)
86         {
87             String JavaDoc encryptedValue = encrypt((String JavaDoc) value);
88             Hibernate.STRING.nullSafeSet(pStmt, encryptedValue, index);
89         }
90         else
91         {
92             Hibernate.STRING.nullSafeSet(pStmt, value, index);
93         }
94     }
95
96     public Object JavaDoc deepCopy(Object JavaDoc value) throws HibernateException
97     {
98         if (value == null)
99         {
100             return null;
101         }
102         else
103         {
104             return new String JavaDoc((String JavaDoc) value);
105         }
106     }
107
108     public boolean isMutable()
109     {
110         return false;
111     }
112     
113     protected String JavaDoc encrypt(String JavaDoc value)
114     {
115         try
116         {
117             return bCodec.encode(value);
118         }
119         catch(EncoderException ee)
120         {
121             log.warn("ENCRYPT - " + value + " : " + ee.getMessage());
122             return value;
123         }
124     }
125     
126     protected String JavaDoc decrypt(String JavaDoc value)
127     {
128         try
129         {
130             return bCodec.decode(value);
131         }
132         catch(DecoderException de)
133         {
134             log.warn("DECRYPT - " + value + " : " + de.getMessage());
135             return value;
136         }
137     }
138     
139     public int hashCode(Object JavaDoc arg0) throws HibernateException
140     {
141         return arg0.hashCode();
142     }
143     
144    
145     public Object JavaDoc replace(Object JavaDoc arg0, Object JavaDoc arg1, Object JavaDoc arg2)
146             throws HibernateException
147     {
148         return deepCopy(arg0);
149     }
150
151     public Object JavaDoc assemble(Serializable JavaDoc arg0, Object JavaDoc arg1) throws HibernateException
152     {
153         return deepCopy(arg0);
154     }
155
156     public Serializable JavaDoc disassemble(Object JavaDoc value)
157     {
158         return (Serializable JavaDoc) deepCopy(value);
159     }
160             
161 }
Popular Tags