/*
 *    Copyright 2009-2011 The MyBatis Team
 *
 *    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.ibatis.cache.decorators;

import org.apache.ibatis.cache.Cache;
import org.apache.ibatis.cache.CacheException;

import java.io.*;
import java.util.concurrent.locks.ReadWriteLock;

public class SerializedCache implements Cache {

  private Cache delegate;

  public SerializedCache(Cache delegate) {
    this.delegate = delegate;
  }

  public String getId() {
    return delegate.getId();
  }

  public int getSize() {
    return delegate.getSize();
  }

  public void putObject(Object key, Object object) {
    if (object == null || object instanceof Serializable) {
      delegate.putObject(key, serialize((Serializable) object));
    } else {
      throw new CacheException("SharedCache failed to make a copy of a non-serializable object: " + object);
    }
  }

  public Object getObject(Object key) {
    Object object = delegate.getObject(key);
    return object == null ? null : deserialize((byte[]) object);
  }

  public Object removeObject(Object key) {
    return delegate.removeObject(key);
  }

  public void clear() {
    delegate.clear();
  }

  public ReadWriteLock getReadWriteLock() {
    return delegate.getReadWriteLock();
  }

  public int hashCode() {
    return delegate.hashCode();
  }

  public boolean equals(Object obj) {
    return delegate.equals(obj);
  }

  private byte[] serialize(Serializable value) {
    try {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      ObjectOutputStream oos = new ObjectOutputStream(bos);
      oos.writeObject(value);
      oos.flush();
      oos.close();
      return bos.toByteArray();
    } catch (Exception e) {
      throw new CacheException("Error serializing object.  Cause: " + e, e);
    }
  }

  private Serializable deserialize(byte[] value) {
    Serializable result;
    try {
      ByteArrayInputStream bis = new ByteArrayInputStream((byte[]) value);
      ObjectInputStream ois = new ObjectInputStream(bis);
      result = (Serializable) ois.readObject();
      ois.close();
    } catch (Exception e) {
      throw new CacheException("Error deserializing object.  Cause: " + e, e);
    }
    return result;
  }


}

@@ -21,17 +21,17 @@
 2009-201
-1
+2
  The MyB
@@ -685,24 +685,347 @@
 orators;%0D%0A%0D%0A
+import java.io.ByteArrayInputStream;%0D%0Aimport java.io.ByteArrayOutputStream;%0D%0Aimport java.io.IOException;%0D%0Aimport java.io.InputStream;%0D%0Aimport java.io.ObjectInputStream;%0D%0Aimport java.io.ObjectOutputStream;%0D%0Aimport java.io.ObjectStreamClass;%0D%0Aimport java.io.Serializable;%0D%0Aimport java.util.concurrent.locks.ReadWriteLock;%0D%0A%0D%0A
 import org.a
@@ -1103,27 +1103,66 @@
 n;%0D%0A
-%0D%0A
 import 
-java.io.*;
+org.apache.ibatis.submitted.permissions.Resource;%0D%0A
 %0D%0Aim
@@ -1170,48 +1170,45 @@
 ort 
-java.util.concurrent.locks.ReadWriteLock
+com.ibatis.common.resources.Resources
 ;%0D%0A%0D
@@ -2970,24 +2970,37 @@
 m ois = new 
+ThreadContext
 ObjectInputS
@@ -3224,13 +3224,397 @@
 %0A  %7D%0D%0A%0D%0A
+  public static class ThreadContextObjectInputStream extends ObjectInputStream %7B%0D%0A%0D%0A    public ThreadContextObjectInputStream(InputStream in) throws IOException %7B%0D%0A      super(in);%0D%0A    %7D%0D%0A%0D%0A    @Override%0D%0A    protected Class%3C?%3E resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException %7B%0D%0A      return Resources.classForName(desc.getName());%0D%0A    %7D%0D%0A    %0D%0A  %7D%0D%0A
 %0D%0A%7D%0D%0A

fix for issue #530. Fix cache classloading issue with PlayFramework