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/22 22:34:43 UTC

cvs commit: jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/util BeanUtils.java ClassUtils.java SerializeUtils.java

hchar       2005/01/22 13:34:43

  Added:       sandbox/yajcache/src/org/apache/jcs/yajcache/util
                        BeanUtils.java ClassUtils.java SerializeUtils.java
  Log:
  move yajcache to sandbox
  
  Revision  Changes    Path
  1.1                  jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/util/BeanUtils.java
  
  Index: BeanUtils.java
  ===================================================================
  
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License")
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.jcs.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;
  import org.apache.jcs.yajcache.annotate.*;
  
  /**
   *
   * @author Hanson Char
   */
  @CopyRightApache
  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;
      }
  }
  
  
  
  1.1                  jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/util/ClassUtils.java
  
  Index: ClassUtils.java
  ===================================================================
  
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License")
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.jcs.yajcache.util;
  
  import java.math.BigDecimal;
  import java.math.BigInteger;
  
  import org.apache.jcs.yajcache.annotate.*;
  /**
   *
   * @author Hanson Char
   */
  @CopyRightApache
  public enum ClassUtils {
      inst;
      /** 
       * Returns true if instances of the given class is known to be immutable; 
       * false if we don't know.
       */
      public boolean isImmutable(Class t) {
          return t == String.class
          ||  t.isPrimitive()
          ||  t == Boolean.class
          ||  t == Byte.class
          ||  t == Character.class
          ||  t == Short.class
          ||  t == Integer.class
          ||  t == Long.class
          ||  t == Float.class
          ||  t == Double.class
          ||  t == BigInteger.class
          ||  t == BigDecimal.class
          ||  t.isEnum()
          ;
      }
      public boolean isImmutable(Object obj) {
          return this.isImmutable(obj.getClass());
      }
  }
  
  
  
  1.1                  jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/util/SerializeUtils.java
  
  Index: SerializeUtils.java
  ===================================================================
  
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License")
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.jcs.yajcache.util;
  
  import java.io.Serializable;
  import java.lang.reflect.Array;
  import org.apache.commons.lang.SerializationUtils;
  import org.apache.jcs.yajcache.annotate.*;
  
  /**
   * @author Hanson Char
   */
  @CopyRightApache
  public enum SerializeUtils {
      inst;
      /**
       * Duplicates the given object.
       *
       * @return a duplicate of the given Serializable object,
       * short-cutting the deep clone process if possible.
       */
      public <V extends Serializable> V dup(V obj) {
          Class k = null;
          
          if (obj == null 
          ||  ClassUtils.inst.isImmutable(k=obj.getClass()))
              return obj;
          Class t = k.getComponentType();
          
          if (t != null) {
              // an array.
              if (ClassUtils.inst.isImmutable(t))
              {
                  // array elements are immutable.
                  // short cut via shallow clone.
                  return this.cloneArray(obj);
              }
          }
          // deep clone.
          return (V)SerializationUtils.clone(obj);
      }
      private <A> A cloneArray(A a) {
          int len = Array.getLength(a);
  	Object result = Array.newInstance(a.getClass().getComponentType(), len);
          System.arraycopy(a, 0, result, 0, len);
          return (A)result;
      }
  //    public Class<?> getLeaveComponentType(Class<?> k) {
  //        if (k == null)
  //            return k;
  //        if (k.isArray()) {
  //            return this.getLeaveComponentType(k.getComponentType());
  //        }
  //        return k.getClass();
  //    }
  }
  
  
  

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