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.factory;
 
 import java.util.UUID;
 
org.jboss.as.ejb3.cache.CacheFactory implementation that can return a group-aware cache. How the cache functions depends on the behavior of the org.jboss.as.ejb3.cache.spi.BackingCacheEntryStore implementations returned by the injected org.jboss.as.ejb3.cache.spi.BackingCacheEntryStoreSource.

Author(s):
Brian Stansberry
Paul Ferraro
 
 public class GroupAwareCacheFactory<K extends Serializable, V extends Cacheable<K>> implements CacheFactory<K, V>, BackingCacheLifecycleListenerIdentifierFactory<UUID> {
 
     private final AtomicInteger memberCounter = new AtomicInteger();
     private final BackingCacheEntryStoreSource<K, V, UUIDstoreSource;
 
     public GroupAwareCacheFactory(BackingCacheEntryStoreSource<K, V, UUIDstoreSource) {
         this. = storeSource;
     }
 
     @Override
     public Cache<K, V> createCache(String beanNameIdentifierFactory<K> identifierFactoryStatefulObjectFactory<V> factoryPassivationManager<K, V> passivationManagerStatefulTimeoutInfo timeout) {
 
         // Create/find the cache for SerializationGroup that the container
         // may be associated with
         SerializationGroupContainer<K, V> groupContainer = this..get();
         if (groupContainer == null) {
             groupContainer = this.createGroupContainer(passivationManagertimeout);
             if (!this..compareAndSet(nullgroupContainer)) {
                 groupContainer = this..get();
             }
         }
         groupContainer.addMemberPassivationManager(passivationManager);
         PassivatingBackingCache<UUIDCacheable<UUID>, SerializationGroup<K, V, UUID>> groupCache = groupContainer.getGroupCache();
 
         SerializationGroupMemberContainer<K, V, UUIDcontainer = new SerializationGroupMemberContainer<K, V, UUID>(passivationManagergroupCachethis.);
 
         // Create the store for SerializationGroupMembers from the container
         BackingCacheEntryStore<K, V, SerializationGroupMember<K, V, UUID>> store = .createIntegratedObjectStore(beanNameidentifierFactorycontainertimeout);
         container.setBackingCacheEntryStore(store);
 
         // Set up the backing cache with the store and group cache
         GroupAwareBackingCache<K, V, UUIDSerializationGroupMember<K, V, UUID>> backingCache = new GroupAwareBackingCacheImpl<K, V, UUID>(factorycontainergroupCache, Executors.defaultThreadFactory());
 
         // Listen for backing cache lifecycle changes so we know when to start/stop groupCache
         backingCache.addLifecycleListener(this);
 
         // Finally, the front-end cache
         return new GroupAwareCache<K, V, UUIDSerializationGroupMember<K, V, UUID>>(backingCachetrue);
     }
 
     private SerializationGroupContainer<K, V> createGroupContainer(PassivationManager<K, V> passivationManagerStatefulTimeoutInfo timeout) {
        SerializationGroupContainer<K, V> container = new SerializationGroupContainer<K, V>(passivationManager);
        BackingCacheEntryStore<UUIDCacheable<UUID>, SerializationGroup<K, V, UUID>> store = .createGroupIntegratedObjectStore(thiscontainertimeout);
        PassivatingBackingCache<UUIDCacheable<UUID>, SerializationGroup<K, V, UUID>> groupCache = new PassivatingBackingCacheImpl<UUIDCacheable<UUID>, SerializationGroup<K, V, UUID>>(containercontainercontainerstore);
        container.setGroupCache(groupCache);
        return container;
    }
    @Override
    public UUID createIdentifier() {
        return UUID.randomUUID();
    }
    // TODO Make the group cache a service
    @Override
    public void lifecycleChange(LifecycleState newState) {
        switch (newState) {
            case : {
                if (this..incrementAndGet() == 1) {
                    PassivatingBackingCache<UUIDCacheable<UUID>, SerializationGroup<K, V, UUID>> groupCache = this..get().getGroupCache();
                    synchronized (groupCache) {
                        groupCache.start();
                    }
                }
                break;
            }
            case : {
                if (this..decrementAndGet() == 0) {
                    PassivatingBackingCache<UUIDCacheable<UUID>, SerializationGroup<K, V, UUID>> groupCache = this..get().getGroupCache();
                    synchronized (groupCache) {
                        groupCache.stop();
                    }
                }
                break;
            }
        }
    }
New to GrepCode? Check out our FAQ X