/*******************************************************************************
 * * Copyright 2012 Impetus Infotech.
 *  *
 *  * 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 com.impetus.kundera.cache.ehcache;

import net.sf.ehcache.Element;
import net.sf.ehcache.Status;

import org.apache.commons.lang.NotImplementedException;

import com.impetus.kundera.cache.Cache;

/**
 * Cache implementation using Ehcache.
 * 
 * @author animesh.kumar
 */
public class EhCacheWrapper implements Cache
{

    /** The ehcache. */
    private net.sf.ehcache.Cache ehcache;

    /**
     * Instantiates a new eh cache wrapper.
     * 
     * @param ehcache
     *            the ehcache
     */
    public EhCacheWrapper(net.sf.ehcache.Cache ehcache)
    {
        this.ehcache = ehcache;
    }

    /* @see com.impetus.kundera.cache.Cache#get(java.lang.Object) */
    /*
     * (non-Javadoc)
     * 
     * @see com.impetus.kundera.cache.Cache#get(java.lang.Object)
     */
    @Override
    public Object get(Object key)
    {
        Element element = ehcache.get(key);
        return element == null ? null : element.getObjectValue();
    }

    /*
     * @see com.impetus.kundera.cache.Cache#put(java.lang.Object,
     * java.lang.Object)
     */
    /*
     * (non-Javadoc)
     * 
     * @see com.impetus.kundera.cache.Cache#put(java.lang.Object,
     * java.lang.Object)
     */
    @Override
    public void put(Object key, Object value)
    {
        ehcache.put(new Element(key, value));
    }

    /* @see com.impetus.kundera.cache.Cache#size() */
    /*
     * (non-Javadoc)
     * 
     * @see com.impetus.kundera.cache.Cache#size()
     */
    @Override
    public int size()
    {
        return ehcache.getSize();
    }

    /*
     * (non-Javadoc)
     * 
     * @see javax.persistence.Cache#contains(java.lang.Class, java.lang.Object)
     */
    @Override
    public boolean contains(Class arg0, Object arg1)
    {
        return (ehcache.get(arg1) != null);
    }

    /*
     * (non-Javadoc)
     * 
     * @see javax.persistence.Cache#evict(java.lang.Class)
     */
    @Override
    public void evict(Class arg0)
    {
        // TODO Can we use Class with ehcache
        throw new NotImplementedException("TODO");
    }

    /*
     * (non-Javadoc)
     * 
     * @see javax.persistence.Cache#evict(java.lang.Class, java.lang.Object)
     */
    @Override
    public void evict(Class arg0, Object arg1)
    {
        // TODO Can we use Class with ehcache
        ehcache.remove(arg1);
    }

    /*
     * (non-Javadoc)
     * 
     * @see javax.persistence.Cache#evictAll()
     */
    @Override
    public void evictAll()
    {
        if(ehcache.getStatus().equals(Status.STATUS_ALIVE))
        {
            ehcache.removeAll();
        }
    }

}

@@ -1717,16 +1717,79 @@
 lement =
+ null;%0D%0A        if(isAlive())%0D%0A        %7B%0D%0A            element =
  ehcache
@@ -1796,24 +1796,35 @@
 .get(key);%0D%0A
+        %7D%0D%0A
         retu
@@ -2199,32 +2199,55 @@
  value)%0D%0A    %7B%0D%0A
+        if(isAlive())%0D%0A
         ehcache.
@@ -2498,32 +2498,100 @@
     
-return ehcache.getSize()
+if(isAlive())%0D%0A        %7B%0D%0A            return ehcache.getSize();%0D%0A        %7D%0D%0A        return 0
 ;%0D%0A 
@@ -2815,42 +2815,124 @@
     
-return (ehcache.get(arg1) != null)
+if(isAlive())%0D%0A        %7B%0D%0A            return (ehcache.get(arg1) != null);%0D%0A        %7D%0D%0A        %0D%0A        return false
 ;%0D%0A 
@@ -3447,32 +3447,55 @@
 s with ehcache%0D%0A
+        if(isAlive())%0D%0A
         ehcache.
@@ -3679,54 +3679,16 @@
  if(
-ehcache.getStatus().equals(Status.STATUS_ALIVE
+isAlive(
 ))%0D%0A
@@ -3752,11 +3752,123 @@
    %7D%0D%0A%0D%0A
+    private boolean isAlive()%0D%0A    %7B%0D%0A        return ehcache.getStatus().equals(Status.STATUS_ALIVE);%0D%0A    %7D%0D%0A%0D%0A
 %7D%0D%0A

Fix for Issue #495