Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2007, 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;
 
 
Simple org.jboss.as.ejb3.cache.spi.BackingCache that doesn't handle passivation (although it does handle expiration). Pure in-VM memory cache. Not group-aware, as there is no point in managing groups if there is no serialization.

Author(s):
Carlo de Wolf
Brian Stansberry
Paul Ferraro
 
 public class NonPassivatingBackingCacheImpl<K extends Serializable, V extends Cacheable<K>> extends AbstractBackingCache<K, V, NonPassivatingBackingCacheEntry<K, V>> implements BackingCacheEntryFactory<K, V, NonPassivatingBackingCacheEntry<K, V>> {
     private final StatefulObjectFactory<V> factory;
     private final Map<K, NonPassivatingBackingCacheEntry<K, V>> cache = new ConcurrentHashMap<K, NonPassivatingBackingCacheEntry<K, V>>();
     private final StatefulTimeoutInfo timeout;
     private volatile ScheduledExecutorService executor;
     private final ThreadFactory threadFactory;
     private final Map<K, Future<?>> expirationFutures = new ConcurrentHashMap<K, Future<?>>();
     private final ServerEnvironment environment;
     private final IdentifierFactory<K> identifierFactory;
 
     public NonPassivatingBackingCacheImpl(IdentifierFactory<K> identifierFactoryStatefulObjectFactory<V> factoryThreadFactory threadFactoryStatefulTimeoutInfo timeoutServerEnvironment environment) {
         this. = identifierFactory;
         this. = factory;
         this. = timeout;
         this. = threadFactory;
         this. = environment;
     }
 
     public NonPassivatingBackingCacheImpl(IdentifierFactory<K> identifierFactoryStatefulObjectFactory<V> factoryScheduledExecutorService executorStatefulTimeoutInfo timeoutServerEnvironment environment) {
         this. = identifierFactory;
         this. = factory;
         this. = timeout;
         this. = executor;
         this. = null;
         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 NonPassivatingBackingCacheEntry<K, V> create() {
        NonPassivatingBackingCacheEntry<K, V> entry = this.createEntry(this..createInstance());
        .put(entry.getUnderlyingItem().getId(), entry);
        return entry;
    }
    @Override
    public NonPassivatingBackingCacheEntry<K, V> createEntry(V item) {
        return new NonPassivatingBackingCacheEntry<K, V>(item);
    }
    @Override
    public void destroyEntry(NonPassivatingBackingCacheEntry<K, V> entry) {
        this..destroyInstance(entry.getUnderlyingItem());
    }
    @Override
    public NonPassivatingBackingCacheEntry<K, V> get(K keythrows NoSuchEJBException {
        NonPassivatingBackingCacheEntry<K, V> entry = .get(key);
        if (entry == nullreturn null;
        entry.increaseUsageCount();
        this.scheduleExpiration(keytrue);
        return entry;
    }
    @Override
    public NonPassivatingBackingCacheEntry<K, V> peek(K keythrows NoSuchEJBException {
        return .get(key);
    }
    @Override
    public NonPassivatingBackingCacheEntry<K, V> release(K key) {
        NonPassivatingBackingCacheEntry<K, V> entry = .get(key);
        if (entry == null) {
            ..cacheEntryNotFound(key);
            return null;
        }
        if (!entry.isInUse()) {
            throw ..cacheEntryNotInUse(key);
        }
        entry.decreaseUsageCount();
        if(!entry.isInUse()) {
            this.scheduleExpiration(keyfalse);
        }
        return entry;
    }
    @Override
    public void discard(K key) {
        .remove(key);
    }
    @Override
    public void remove(K key) {
        this.scheduleExpiration(keytrue);
        NonPassivatingBackingCacheEntry<K, V> entry = .remove(key);
        if (entry != null && entry.isInUse()) {
            entry.decreaseUsageCount();
        }
        if (entry != null) {
            .destroyInstance(entry.getUnderlyingItem());
        }
    }
    @Override
    public boolean isClustered() {
        return false;
    }
    @Override
    public void start() {
        try {
            if (this. != null) {
                this. = Executors.newScheduledThreadPool(1, this.);
            }
        } catch (RuntimeException e) {
            throw e;
        }
    }
    @Override
    public void stop() {
        try {
            if (this. != null) {
                this..shutdownNow();
            } else {
                // This is a shared executor, so just cancel our tasks
                for (Future<?> futurethis..values()) {
                    future.cancel(false);
                }
            }
            this..clear();
            this..clear();
        } catch (RuntimeException e) {
            throw e;
        }
    }
    private void scheduleExpiration(K idboolean cancel) {
        if (this. != null && .getValue() != -1) {
            Future<?> future = cancel ? this..remove(id) : this..put(idthis..schedule(new RemoveTask<K>(thisid), this..getValue(), this..getTimeUnit()));
            if (future != null) {
                future.cancel(false);
            }
        }
    }
New to GrepCode? Check out our FAQ X