BlobtoByteArrayUserType.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.pentaho.repository.usertypes |
![]() |
![]() |
Pentaho |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* Copyright 2006 Pentaho Corporation. All rights reserved.
* This software was developed by Pentaho Corporation and is provided under the terms
* of the Mozilla Public License, Version 1.1, or any later version. You may not use
* this file except in compliance with the license. If you need a copy of the license,
* please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
* BI Platform. The Initial Developer is Pentaho Corporation.
*
* Software distributed under the Mozilla Public License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
* the license for the specific language governing your rights and limitations.
*
* @created Oct 22, 2007
* @author Marc Batchelor
*
*/
/*
* Class for persisting lists and other collections. Using serialization to persist these items.
* I'm using this class because I have a requirement to have a map element that may be a map or
* some other collection of strings.
*/
package org.pentaho.repository.usertypes;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.sql.Blob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.type.SerializationException;
import org.hibernate.usertype.UserType;
import org.pentaho.messages.Messages;
public class BlobtoByteArrayUserType implements UserType {
private static Log log = LogFactory.getLog(BlobtoByteArrayUserType.class);
private static final int[] SQLTYPE = { Types.BLOB };
/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#sqlTypes()
*/
public int[] sqlTypes() {
return SQLTYPE;
}
/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#returnedClass()
*/
public Class returnedClass() {
return byte[].class;
}
/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#equals(java.lang.Object,
* java.lang.Object)
*/
public boolean equals(Object x, Object y) throws HibernateException {
return (x == y) || (x != null && y != null && java.util.Arrays.equals((byte[]) x, (byte[]) y));
}
/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
*/
public int hashCode(Object arg0) throws HibernateException {
return arg0.hashCode();
}
/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet,
* java.lang.String[], java.lang.Object)
*/
public Object nullSafeGet(ResultSet resultSet, String[] arg1, Object arg2) throws HibernateException, SQLException {
Blob blob = resultSet.getBlob(arg1[0]);
if (blob != null) {
byte[] bytes = blob.getBytes(1, (int) blob.length());
if (bytes != null) {
ByteArrayInputStream baos = new ByteArrayInputStream(bytes);
ObjectInputStream ois;
try {
ois = new ObjectInputStream(baos);
return ois.readObject();
} catch (IOException ioe) {
log.error(Messages.getErrorString("BLOBUTYPE.ERROR_0001_SETTING_BLOB"), ioe); //$NON-NLS-1$
throw new HibernateException(Messages.getErrorString("BLOBUTYPE.ERROR_0001_SETTING_BLOB"), ioe); //$NON-NLS-1$
} catch (ClassNotFoundException cnfe) {
log.error(Messages.getErrorString("BLOBUTYPE.ERROR_0001_SETTING_BLOB"), cnfe); //$NON-NLS-1$
throw new HibernateException(Messages.getErrorString("BLOBUTYPE.ERROR_0001_SETTING_BLOB"), cnfe); //$NON-NLS-1$
}
}
}
return null;
}
/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement,
* java.lang.Object, int)
*/
public void nullSafeSet(PreparedStatement prepSt, Object arg1, int arg2) throws HibernateException, SQLException {
if (arg1 != null) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(arg1);
oos.close();
prepSt.setBytes(arg2, baos.toByteArray());
} catch (SerializationException se) {
log.error(Messages.getErrorString("BLOBUTYPE.ERROR_0001_SETTING_BLOB"), se); //$NON-NLS-1$
throw new HibernateException(Messages.getErrorString("BLOBUTYPE.ERROR_0001_SETTING_BLOB"), se); //$NON-NLS-1$
} catch (IOException ioe) {
log.error(Messages.getErrorString("BLOBUTYPE.ERROR_0001_SETTING_BLOB"), ioe); //$NON-NLS-1$
throw new HibernateException(Messages.getErrorString("BLOBUTYPE.ERROR_0001_SETTING_BLOB"), ioe); //$NON-NLS-1$
}
} else {
prepSt.setNull(arg2, sqlTypes()[0]);
}
}
/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
*/
public Object deepCopy(Object value)
{
if (value == null){
return null;
}
byte[] bytes = null;
if (value instanceof String || value instanceof String[] || value instanceof Number || value instanceof Number[]) {
return value;
} else if (value instanceof byte[]){
return new String((byte[])value);
} else {
bytes = (byte[]) value;
}
byte[] result = new byte[bytes.length];
System.arraycopy(bytes, 0, result, 0, bytes.length);
return result;
}
/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#isMutable()
*/
public boolean isMutable() {
return true;
}
/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
*/
public Serializable disassemble(Object arg0) throws HibernateException {
return (Serializable)deepCopy(arg0);
}
/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#assemble(java.io.Serializable,
* java.lang.Object)
*/
public Object assemble(Serializable arg0, Object arg1) throws HibernateException {
return (deepCopy(arg0));
}
/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#replace(java.lang.Object,
* java.lang.Object, java.lang.Object)
*/
public Object replace(Object arg0, Object arg1, Object arg2) throws HibernateException {
return (deepCopy(arg0));
}
}
The table below shows all metrics for BlobtoByteArrayUserType.java.




