Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2008, Red Hat, Inc., and individual contributors
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * 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 org.jboss.weld.context.beanstore;
 
 

A bound bean store backed by attributes. This bean store is "write-through" - if attached it will write any modifications to the backing store immediately. If detached modifications will not be written through. If the bean store is reattached, then any local modifications will be written to the underlying store.

This construct is not thread safe.

Author(s):
Pete Muir
Nicklas Karlsson
David Allen
 
 public abstract class AttributeBeanStore implements BoundBeanStore {
 
     private final HashMapBeanStore beanStore;
     private final NamingScheme namingScheme;
 
     private boolean attached;
 
     public AttributeBeanStore(NamingScheme namingScheme) {
         this. = namingScheme;
         this. = new HashMapBeanStore();
     }

    
Detach the bean store, causing updates to longer be written through to the underlying store.
 
     public boolean detach() {
         if () {
              = false;
             ..beanStoreDetached(this);
             return true;
         } else {
             return false;
         }
     }

    

Attach the bean store, any updates from now on will be written through to the underlying store.

When the bean store is attached, the detached state is assumed to be authoritative if there are any conflicts.

 
     public boolean attach() {
         if (!) {
              = true;
             // beanStore is authoritative, so copy everything to the backing store
             for (BeanIdentifier id : ) {
                 ContextualInstance<?> instance = .get(id);
                 String prefixedId = getNamingScheme().prefix(id);
                 ..updatingStoreWithContextualUnderId(instanceid);
                 setAttribute(prefixedIdinstance);
             }
 
             /*
             * Additionally copy anything not in the bean store but in the session
             * into the bean store
             */
             for (String prefixedId : getPrefixedAttributeNames()) {
                 BeanIdentifier id = getNamingScheme().deprefix(prefixedId);
                 if (!.contains(id)) {
                     ContextualInstance<?> instance = (ContextualInstance<?>) getAttribute(prefixedId);
                     .put(idinstance);
                    ..addingDetachedContextualUnderId(instanceid);
                }
            }
            return true;
        } else {
            return false;
        }
    }
    public boolean isAttached() {
        return ;
    }
    @Override
    public <T> ContextualInstance<T> get(BeanIdentifier id) {
        ContextualInstance<T> instance = .get(id);
        ..contextualInstanceFound(idinstancethis);
        return instance;
    }
    @Override
    public <T> void put(BeanIdentifier idContextualInstance<T> instance) {
        .put(idinstance); // moved due to WELD-892
        if (isAttached()) {
            String prefixedId = .prefix(id);
            setAttribute(prefixedIdinstance);
        }
        ..contextualInstanceAdded(instance.getContextual(), idthis);
    }
    @Override
    public <T> ContextualInstance<T> remove(BeanIdentifier id) {
        ContextualInstance<T> instance = .remove(id);
        if (instance != null) {
            if (isAttached()) {
                removeAttribute(.prefix(id));
            }
            ..contextualInstanceRemoved(idthis);
        }
        return instance;
    }
    public void clear() {
        Iterator<BeanIdentifierit = iterator();
        while (it.hasNext()) {
            BeanIdentifier id = it.next();
            if (isAttached()) {
                String prefixedId = .prefix(id);
                removeAttribute(prefixedId);
            }
            it.remove();
            ..contextualInstanceRemoved(idthis);
        }
        ..contextCleared(this);
    }
    public boolean contains(BeanIdentifier id) {
        return get(id) != null;
    }
    protected NamingScheme getNamingScheme() {
        return ;
    }
    public Iterator<BeanIdentifieriterator() {
        return .iterator();
    }

    
Gets an attribute from the underlying storage

Parameters:
prefixedId The (prefixed) id of the attribute
Returns:
The data
    protected abstract Object getAttribute(String prefixedId);

    
Removes an attribute from the underlying storage

Parameters:
prefixedId The (prefixed) id of the attribute to remove
    protected abstract void removeAttribute(String prefixedId);

    
Gets an enumeration of the attribute names present in the underlying storage

Returns:
The attribute names
    protected abstract Collection<StringgetAttributeNames();

    
Gets an enumeration of the attribute names present in the underlying storage

Returns:
The attribute names
        return getNamingScheme().filterIds(getAttributeNames());
    }

    
Sets an instance under a key in the underlying storage

Parameters:
prefixedId The (prefixed) id of the attribute to set
instance The instance
    protected abstract void setAttribute(String prefixedIdObject instance);
    public LockedBean lock(final BeanIdentifier id) {
        LockStore lockStore = getLockStore();
        if(lockStore == null) {
            //if the lockstore is null then no locking is necessary, as the underlying
            //context is single threaded
            return null;
        }
        return lockStore.lock(id);
    }
    protected abstract LockStore getLockStore();
New to GrepCode? Check out our FAQ X