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;
 
 
A member of a SerializationGroupImpl.

Author(s):
Brian Stansberry
Paul Ferraro
 
 public class SerializationGroupMemberImpl<K extends Serializable, V extends Cacheable<K>, G extends Serializableextends AbstractBackingCacheEntry<K, V> implements SerializationGroupMember<K, V, G> {
    
The serialVersionUID
 
     private static final long serialVersionUID = 7268142730501106252L;
 
     private static final Logger log = Logger.getLogger(SerializationGroupMemberImpl.class);

    
Identifier for our underlying object
 
     private K id;

    
The underlying object (e.g. bean context).
 
     private transient V value;

    
The group. Never serialize the group; only the groupCache does that
 
     private transient SerializationGroup<K, V, G> group;

    
Id for our group; serialize this so we can find our group again after deserialization on a remote node.
 
     private G groupId;
 
     private boolean clustered;
 
     private boolean preReplicated;
 
     private transient ReentrantLock lock = new ReentrantLock();
     private transient boolean groupLockHeld;

    
The cache that's handling us
 
     private transient GroupAwareBackingCache<K, V, G, SerializationGroupMember<K, V, G>> cache;
 
     public SerializationGroupMemberImpl(V objGroupAwareBackingCache<K, V, G, SerializationGroupMember<K, V, G>> cache) {
         this. = obj;
         this. = obj.getId();
         this. = cache;
         this. = cache.isClustered();
     }
 
     @Override
     public K getId() {
         return ;
     }
 
     @Override
     public boolean isModified() {
         boolean localModified = (this. != null && this..isModified());
         if (localModified &&  != null)
             .setGroupModified(true);
         return ( == null ? localModified : .isGroupModified());
     }

    
Gets whether this member supports clustering functionality.

Returns:
true if clustering is supported, false otherwise
    public boolean isClustered() {
        return ;
    }
    @Override
    public V getUnderlyingItem() {
        return this.;
    }

    
Sets the underlying org.jboss.as.ejb3.cache.Cacheable associated with this group member.

Parameters:
obj the cache item
    @Override
    public void setUnderlyingItem(V obj) {
        this. = obj;
    }

    
Gets the SerializationGroupImpl of which this object is a member.

Returns:
the group. May return null
    @Override
    public SerializationGroup<K, V, G> getGroup() {
        return ;
    }

    
Sets the SerializationGroupImpl of which this object is a member.

Parameters:
group May be null
    @Override
    public void setGroup(SerializationGroup<K, V, G> group) {
        if (this. != group) {
            // Remove any lock held on existing group
            if (group == null && ) {
                this..unlock();
                 = false;
            }
            this. = group;
            if (this. == null && group != null)
                this. = group.getId();
            if (group != null && .isHeldByCurrentThread()) {
                group.lock();
                 = true;
            }
        }
    }

    
Gets the id for the group

Returns:
    @Override
    public G getGroupId() {
        return ;
    }
    @Override
    public void prePassivate() {
        // By the time we get here this thread has already locked the
        // group. It's possible another thread has locked this member
        // but not yet the group. We're willing to wait 2ms for that
        // other thread to see it can't lock the group and release this
        // member's lock, which it will only do if it is another
        // passivation thread. A request thread will hold the lock and
        // we'll fail (as intended)
        if (tryLock(2)) {
            try {
                // make sure we don't passivate the group twice
                // use the setter to clear any group lock
                setGroup(null);
                .passivate(this.);
            } finally {
                unlock();
            }
        } else {
            throw ..cacheEntryInUse(this);
        }
    }
    @Override
    public void preReplicate() {
        // By the time we get here this thread has already locked the
        // group. It's possible another thread has locked this member
        // but not yet the group. We're willing to wait 2ms for that
        // other thread to see it can't lock the group and release this
        // member's lock, which it will only do if it is a passivation thread
        // A request thread will hold the lock and we'll fail (as intended)
        if (tryLock(2)) {
            try {
                // make sure we don't passivate the group twice
                // use the setter to clear any group lock
                setGroup(null);
                .notifyPreReplicate(this);
            } finally {
                unlock();
            }
        } else {
            throw ..cacheEntryInUse(this);
        }
    }

    
Notification that the group has been activated from a passivated state.
    public void postActivate() {
    }
    @Override
    public boolean isPreReplicated() {
        return ;
    }
    @Override
    public void setPreReplicated(boolean preReplicated) {
        this. = preReplicated;
    }

    
Notification that the previously replicated group has been retrieved from a clustered cache.
    public void postReplicate() {
    }
    @Override
    public synchronized void increaseUsageCount() {
        boolean inUse = isInUse();
        super.increaseUsageCount();
        if(!inUse) {
            setInUse(true);
        }
    }
    @Override
    public synchronized void decreaseUsageCount() {
        super.decreaseUsageCount();
        if(!isInUse()) {
            setInUse(false);
        }
    }
    private void setInUse(boolean inUse) {
        // Tell our group about it
        if ( != null) {
            boolean localGroupLock = false;
            if (!) {
                .lock();
                localGroupLock =  = true;
            }
            try {
                if (inUse)
                    .addInUse();
                else
                    .removeInUse();
            } finally {
                if (localGroupLock) {
                    .unlock();
                     = false;
                }
            }
        }
    }

    
Allows our controlling org.jboss.as.ejb3.cache.spi.PassivatingBackingCache to provide us a reference after deserialization.

Parameters:
delegate
    @Override
    public void setPassivatingCache(GroupAwareBackingCache<K, V, G, SerializationGroupMember<K, V, G>> delegate) {
        this. = delegate;
    }
    @Override
    public void lock() {
        try {
            .lockInterruptibly();
        } catch (InterruptedException e) {
            throw ..lockAcquisitionInterrupted(ethis);
        }
        if (! &&  != null) {
            try {
                .lock();
                 = true;
            } finally {
                if (!)
                    .unlock();
            }
        }
    }
    @Override
    public boolean tryLock() {
        boolean success = .tryLock();
        if (success) {
            success = ( ||  == null);
            if (!success) {
                try {
                    success =  = .tryLock();
                    if (!success) {
                        .tracef("Member %s cannot lock serialization group %s");
                    }
                } finally {
                    if (!success)
                        .unlock();
                }
            }
        }
        return success;
    }
    private boolean tryLock(long wait) {
        boolean success = false;
        try {
            success = .tryLock(wait.);
        } catch (InterruptedException ie) {
            // success remains false
        }
        if (success) {
            success = ( ||  == null);
            if (!success) {
                try {
                    success =  = .tryLock();
                    if (!success) {
                        .tracef("Member %s cannot lock serialization group %s");
                    }
                } finally {
                    if (!success)
                        .unlock();
                }
            }
        }
        return success;
    }
    @Override
    public void unlock() {
        if ( && .getHoldCount() == 1) {
            // time to release our group lock
            .unlock();
             = false;
        }
        .unlock();
    }
    @Override
    public String toString() {
        return this..toString();
    }
    @SuppressWarnings("unchecked")
    private void readObject(java.io.ObjectInputStream inthrows IOExceptionClassNotFoundException {
        in.defaultReadObject();
         = new ReentrantLock();
        if ( == null) {
            this. = (V) in.readObject();
        }
    }
    private void writeObject(java.io.ObjectOutputStream outthrows IOException {
        if ( != null) {
            setGroup(null);
        }
        out.defaultWriteObject();
        if ( == null) {
            out.writeObject();
        }
    }
New to GrepCode? Check out our FAQ X