You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jcs-dev@jakarta.apache.org by hc...@apache.org on 2005/01/19 11:58:13 UTC

cvs commit: jakarta-turbine-jcs/auxiliary-builds/jdk15/yajcache/cache/src/net/sf/yajcache/util BeanUtils.java

hchar       2005/01/19 02:58:13

  Added:       auxiliary-builds/jdk15/yajcache/cache/src/net/sf/yajcache/util
                        BeanUtils.java
  Log:
  no message
  
  Revision  Changes    Path
  1.1                  jakarta-turbine-jcs/auxiliary-builds/jdk15/yajcache/cache/src/net/sf/yajcache/util/BeanUtils.java
  
  Index: BeanUtils.java
  ===================================================================
  /*
   * BeanUtils.java
   *
   * Created on 18 January 2005, 23:02
   */
  
  package net.sf.yajcache.util;
  
  import java.beans.XMLDecoder;
  import java.beans.XMLEncoder;
  import java.io.ByteArrayInputStream;
  import java.io.ByteArrayOutputStream;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  /**
   *
   * @author Hanson Char
   */
  public enum BeanUtils {
      inst;
      private static final boolean debug = false;
      private Log log = debug ? LogFactory.getLog(this.getClass()) : null;
      
      public <B> B cloneDeep(B bean) {
          if (bean == null
          ||  ClassUtils.inst.isImmutable(bean))
              return bean;
          return (B)this.fromXmlByteArray(this.toXmlByteArray(bean));
      }
      public <B> B cloneShallow(B bean) {
          if (bean == null
          ||  ClassUtils.inst.isImmutable(bean))
              return bean;
          try {
              return (B)org.apache.commons.beanutils.BeanUtils.cloneBean(bean);
          } catch(Exception ex) {
              LogFactory.getLog(this.getClass()).error("", ex);
              throw new RuntimeException(ex);
          }
      }
      public byte[] toXmlByteArray(Object bean) {
          ByteArrayOutputStream bos = new ByteArrayOutputStream();
          XMLEncoder out = new XMLEncoder(bos);
          out.writeObject(bean);
          out.close();
          return bos.toByteArray();
      }
      public Object fromXmlByteArray(byte[] bytes) {
          if (debug)
              log.debug(new String(bytes));
          ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
          XMLDecoder in = new XMLDecoder(bis);
          Object toBean = in.readObject();
          in.close();
          return toBean;
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-jcs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-jcs-dev-help@jakarta.apache.org