Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file 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.as.ejb3.cache.impl.backing;
 
 import java.util.Map;
 import java.util.Set;
 
A org.jboss.as.ejb3.cache.spi.BackingCacheEntryStore that stores in a simple Map and delegates to a provided org.jboss.as.ejb3.cache.spi.PersistentObjectStore for persistence.

Author(s):
Brian Stansberry
Paul Ferraro
 
 public class SimpleBackingCacheEntryStore<K extends Serializable, V extends Cacheable<K>, E extends BackingCacheEntry<K, V>> extends AbstractBackingCacheEntryStore<K, V, E> {
     private final IdentifierFactory<K> identifierFactory;
     private final PersistentObjectStore<K, E> store;
     private final Map<K, EntryHoldercache = new ConcurrentHashMap<K, EntryHolder>();

    
SORTED SETS COMPARE FOR EQUALITY USING Comparable not equals()/hashCode()

This means that when removing from this set you MUST use the timestamp that is stored in cache, otherwise nothing will be removed.

 
     private final SortedSet<CacheableTimestamp<K>> entries = new ConcurrentSkipListSet<CacheableTimestamp<K>>();
     private final ServerEnvironment environment;

    
Create a new SimpleIntegratedObjectStore.
 
     public SimpleBackingCacheEntryStore(IdentifierFactory<K> identifierFactoryPersistentObjectStore<K, E> storeServerEnvironment environmentStatefulTimeoutInfo timeoutBackingCacheEntryStoreConfig config) {
         super(timeoutconfig);
         this. = identifierFactory;
         this. = store;
         this. = environment;
     }
 
     @Override
     public K createIdentifier() {
         return this..createIdentifier();
     }
 
     @Override
     public Affinity getStrictAffinity() {
         return new NodeAffinity(this..getNodeName());
     }
 
     @Override
     public Affinity getWeakAffinity(K key) {
         return .;
     }
 
     @Override
     public boolean hasAffinity(K key) {
         return true;
     }
    @Override
    public boolean isClustered() {
        return false;
    }
    @Override
    public E get(K keyboolean lock) {
        EntryHolder holder = .get(key);
        if (holder == null) {
            E value = .load(key);
            if (value != null) {
                CacheableTimestamp<K> timestamp = new CacheableTimestamp<K>(value);
                .put(keynew EntryHolder(valuetimestamp));
                this..add(timestamp);
            }
            return value;
        }
        return holder.value;
    }
    @Override
    public Set<K> insert(E entry) {
        K key = entry.getId();
        if (.containsKey(key)) {
            throw ..duplicateCacheEntry(key);
        }
        CacheableTimestamp<K> timestamp = new CacheableTimestamp<K>(entry);
        .put(keynew EntryHolder(entrytimestamp));
        this..add(timestamp);
        final Set<K> toPassivate = new HashSet<K>();
        int maxSize = this.getConfig().getMaxSize();
        int thisSize = .size();
        if (thisSize > maxSize) {
            int remaining = thisSize - maxSize;
            final Iterator<CacheableTimestamp<K>> iterator = this..iterator();
            while (remaining > 0 && iterator.hasNext()) {
                // Passivate the oldest
                final EntryHolder holder = this..get(iterator.next().getId());
                if (holder != null && !holder.value.getId().equals(timestamp.getId()) && !holder.value.isInUse()) {
                    remaining--;
                    toPassivate.add(holder.value.getId());
                }
            }
        }
        return toPassivate;
    }
    @Override
    public void update(E entryboolean modified) {
        K key = entry.getId();
        if (!.containsKey(key)) {
            throw ..missingCacheEntry(key);
        }
        this.update(entry);
        // Otherwise we do nothing; we already have a ref to the entry
    }
    @Override
    public void passivate(E entry) {
        synchronized (entry) {
            K key = entry.getId();
            .store(entry);
            EntryHolder holder = .remove(key);
            if (holder != null) {
                this.remove(holder.timestamp);
            }
        }
    }
    @Override
    public E remove(K id) {
        E entry = get(idfalse);
        if (entry != null) {
            EntryHolder holder = .remove(id);
            if (holder != null) {
                this.remove(holder.timestamp);
            }
        }
        return entry;
    }
    private void remove(CacheableTimestamp<K> timestamp) {
        this..remove(timestamp);
    }
    private void update(E entry) {
        CacheableTimestamp<K> timestamp = new CacheableTimestamp<K>(entry);
        final EntryHolder holder = .get(entry.getId());
        if (holder != null) {
            this..remove(holder.timestamp);
        }
        .put(entry.getId(), new EntryHolder(entrytimestamp));
        this..add(timestamp);
    }
    @Override
    public void start() {
        .start();
    }
    @Override
    public void stop() {
        .stop();
    }
    @Override
    public boolean isCompatibleWith(GroupCompatibilityChecker other) {
        if (other instanceof BackingCacheEntryStore) {
            return ((BackingCacheEntryStore<?, ?, ?>) other).isClustered() == false;
        }
        return false;
    }
    private final class EntryHolder {
        private final CacheableTimestamp<K> timestamp;
        private final E value;
        private EntryHolder(final E valuefinal CacheableTimestamp<K> timestamp) {
            this. = value;
            this. = timestamp;
        }
    }
New to GrepCode? Check out our FAQ X