[jboss-cvs] jboss-portal/jems/src/main/org/jboss/portal/jems/hibernate ...

Julien Viet julien at jboss.com
Mon Jul 31 17:43:04 EDT 2006


  User: julien  
  Date: 06/07/31 17:43:04

  Added:       jems/src/main/org/jboss/portal/jems/hibernate 
                        CacheableBlobUserType.java
  Log:
  - add CacheableBlobUserType
  - make the jackrabbit PM use CacheableUserType to avoid 10000 request per page view to the DB
  
  Revision  Changes    Path
  1.1      date: 2006/07/31 21:43:04;  author: julien;  state: Exp;jboss-portal/jems/src/main/org/jboss/portal/jems/hibernate/CacheableBlobUserType.java
  
  Index: CacheableBlobUserType.java
  ===================================================================
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  package org.jboss.portal.jems.hibernate;
  
  import org.hibernate.usertype.UserType;
  import org.hibernate.HibernateException;
  import org.jboss.portal.common.util.Tools;
  
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.PreparedStatement;
  import java.sql.Types;
  import java.sql.Blob;
  import java.io.Serializable;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.io.ByteArrayInputStream;
  import java.io.IOException;
  import java.io.ByteArrayOutputStream;
  
  /**
   * @author <a href="mailto:julien at jboss.org">Julien Viet</a>
   * @version $Revision: 1.1 $
   */
  public class CacheableBlobUserType implements UserType
  {
  
     /** . */
     private static final int[] SQL_TYPES = {Types.BLOB};
  
     public int[] sqlTypes()
     {
        return SQL_TYPES;
     }
  
     public Class returnedClass()
     {
       return Blob.class;
     }
  
     public boolean equals(Object x, Object y)
     {
        if (x == y)
        {
           return true;
        }
        if (x != null && y != null)
        {
           ByteArrayBlob _x = (ByteArrayBlob)x;
           ByteArrayBlob _y = (ByteArrayBlob)y;
           return java.util.Arrays.equals(_x._bytes, _y._bytes);
        }
        return false;
     }
  
     public int hashCode(Object object) throws HibernateException
     {
        return object.hashCode();
     }
  
     public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException
     {
        Blob blob = rs.getBlob(names[0]);
        return rs.wasNull() ? null : new ByteArrayBlob(blob);
     }
  
     public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException
     {
        if (value == null)
        {
           st.setNull(index, Types.BLOB);
        }
        else
        {
           st.setBlob(index, (Blob)value);
        }
  //      if (value==null) {
  //         st.setNull(index, Types.BLOB);
  //      }
  //      else {
  //
  //         if (value instanceof SerializableBlob) {
  //            value = ( (SerializableBlob) value ).getWrappedBlob();
  //         }
  //
  //         final boolean useInputStream = session.getFactory().getDialect().useInputStreamToInsertBlob() &&
  //            (value instanceof BlobImpl);
  //
  //         if ( useInputStream ) {
  //            BlobImpl blob = (BlobImpl) value;
  //            st.setBinaryStream( index, blob.getBinaryStream(), (int) blob.length() );
  //         }
  //         else {
  //            st.setBlob(index, (Blob) value);
  //         }
  //
  //      }
     }
  
     public Object deepCopy(Object value) throws HibernateException
     {
        if (value == null)
        {
           return null;
        }
        return new ByteArrayBlob(((ByteArrayBlob)value));
     }
  
     public boolean isMutable()
     {
        return true;
     }
  
     public Serializable disassemble(Object value) throws HibernateException
     {
        return ((ByteArrayBlob)value)._bytes;
     }
  
     public Object assemble(Serializable cached, Object owner) throws HibernateException
     {
        return new ByteArrayBlob((byte[])cached);
     }
  
     public Object replace(Object original, Object target, Object owner) throws HibernateException
     {
        return original;
     }
  
     public static final class ByteArrayBlob implements Blob
     {
  
        /** . */
        private byte[] _bytes;
  
        public ByteArrayBlob(InputStream in) throws IOException
        {
           ByteArrayOutputStream out = new ByteArrayOutputStream();
           Tools.copy(in, out);
           _bytes = out.toByteArray();
        }
  
        public ByteArrayBlob(ByteArrayBlob that)
        {
           _bytes = that.getBytes(0, that._bytes.length);
        }
  
        public ByteArrayBlob(byte[] _bytes)
        {
           this._bytes = _bytes;
        }
  
        public ByteArrayBlob(Blob blob) throws SQLException
        {
           _bytes = blob.getBytes(1, (int) blob.length());
        }
  
        public long length()
        {
           return _bytes.length;
        }
  
        public byte[] getBytes(long pos, int length)
        {
           byte[] bytes = new byte[length];
           System.arraycopy(_bytes, (int)pos, bytes, 0, length);
           return bytes;
        }
  
        public InputStream getBinaryStream()
        {
           return new ByteArrayInputStream(_bytes);
        }
  
        public int setBytes(long pos, byte[] bytes)
        {
           throw new UnsupportedOperationException();
        }
  
        public int setBytes(long pos, byte[] bytes, int offset, int len)
        {
           throw new UnsupportedOperationException();
        }
  
        public OutputStream setBinaryStream(long pos)
        {
           throw new UnsupportedOperationException();
        }
        public long position(byte pattern[], long start)
        {
           throw new UnsupportedOperationException();
        }
  
        public long position(Blob pattern, long start)
        {
           throw new UnsupportedOperationException();
        }
  
        public void truncate(long len)
        {
           throw new UnsupportedOperationException();
        }
     }
  }
  
  
  



More information about the jboss-cvs-commits mailing list