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;
 
 import java.util.List;
 import java.util.Map;
 
 
 
 import  edu.umd.cs.findbugs.annotations.SuppressWarnings;

Author(s):
Pete Muir
Ales Justin
Jozef Hartinger
 
 public class CreationalContextImpl<T> implements CreationalContext<T>, WeldCreationalContext<T>, Serializable {
 
     private static final long serialVersionUID = 7375854583908262422L;
 
     private static final SerializationProxy SERIALIZATION_PROXY = new SerializationProxy();
 
     @SuppressWarnings(value = "SE_TRANSIENT_FIELD_NOT_RESTORED", justification = "Not needed after initial creation")
     private transient Map<Contextual<?>, ObjectincompleteInstances;
     @SuppressWarnings(value = "SE_TRANSIENT_FIELD_NOT_RESTORED", justification = "Not needed after initial creation")
     private final transient Contextual<T> contextual;
 
     private final List<ContextualInstance<?>> dependentInstances;
 
     private final List<ContextualInstance<?>> parentDependentInstances;
 
     private final CreationalContextImpl<?> parentCreationalContext;
 
     private transient List<ResourceReference<?>> resourceReferences;
 
     private transient boolean constructorInterceptionSuppressed;
 
     private transient List<AroundConstructCallback<T>> aroundConstructCallbacks;
 
     public CreationalContextImpl(Contextual<T> contextual) {
         this(contextualnull, Collections.synchronizedList(new ArrayList<ContextualInstance<?>>()), null);
     }
 
     private CreationalContextImpl(Contextual<T> contextualMap<Contextual<?>, ObjectincompleteInstances,
             List<ContextualInstance<?>> parentDependentInstancesStoreCreationalContextImpl<?> parentCreationalContext) {
         this. = incompleteInstances;
         this. = contextual;
         // this is direct ref by intention - to track dependencies hierarchy
         this. = Collections.synchronizedList(new ArrayList<ContextualInstance<?>>());
         this. = parentDependentInstancesStore;
         this. = parentCreationalContext;
         this. = false;
     }
 
     private CreationalContextImpl() {
         this. = null;
         this. = null;
         // We can't use immutable empty lists because of some rare scenarios with Instance.get()
         this. = Collections.synchronizedList(new ArrayList<ContextualInstance<?>>());
         this. = Collections.synchronizedList(new ArrayList<ContextualInstance<?>>());
     }
 
     public void push(T incompleteInstance) {
         if ( == null) {
              = new HashMap<Contextual<?>, Object>();
         }
         .put(incompleteInstance);
     }
 
     public <S> CreationalContextImpl<S> getCreationalContext(Contextual<S> contextual) {
         return new CreationalContextImpl<S>(contextualthis);
     }
        return new CreationalContextImpl<S>(contextual != null ? new HashMap<Contextual<?>, Object>() : null, Collections.synchronizedList(new ArrayList<ContextualInstance<?>>()), null);
    }
    public <S> S getIncompleteInstance(Contextual<S> bean) {
        return  == null ? null : Reflections.<S> cast(.get(bean));
    }
    public boolean containsIncompleteInstance(Contextual<?> bean) {
        return  != null && .containsKey(bean);
    }
    public void addDependentInstance(ContextualInstance<?> contextualInstance) {
        .add(contextualInstance);
    }
    public void release() {
        release(nullnull);
    }
    // should not be public
    public void release(Contextual<T> contextual, T instance) {
        synchronized () {
            for (ContextualInstance<?> dependentInstance : ) {
                // do not destroy contextual again, since it's just being destroyed
                if (contextual == null || !(dependentInstance.getContextual().equals(contextual))) {
                    destroy(dependentInstance);
                }
            }
        }
        if ( != null) {
            for (ResourceReference<?> reference : ) {
                reference.release();
            }
        }
    }
    private static <T> void destroy(ContextualInstance<T> beanInstance) {
        beanInstance.getContextual().destroy(beanInstance.getInstance(), beanInstance.getCreationalContext());
    }

    

Returns:
the parent CreationalContext or null if there isn't any parent.
        return ;
    }

    
Returns an unmodifiable list of dependent instances.
        return Collections.unmodifiableList();
    }
    // Serialization
    protected Object writeReplace() throws ObjectStreamException {
        synchronized () {
            for (Iterator<ContextualInstance<?>> iterator = .iterator(); iterator.hasNext();) {
                ContextualInstance<?> instance = iterator.next();
                if (!(instance.getInstance() instanceof Serializable)) {
                    /*
                     * This non-serializable instance is a dependency of a passivation capable enclosing bean. This means that:
                     *
                     * 1) The dependency was injected into a transient field, constructor or initializer injection point of the enclosing bean instance
                     * (otherwise it would not pass deployment validation) and is no longer retained by the enclosing bean instance. In that case we can safely
                     * destroy the dependent instance now.
                     *
                     * 2) Same as above but the enclosing bean instance retained a reference in a field that Weld has no control of. If that is the case and the
                     * bean class does not implement serialization properly, serialization of the bean instance is going to fail anyway so it is safe to destroy
                     * the dependent instance now.
                     *
                     * 3) Same as above but the bean class implements serialization properly (writeObject) so that it is able to reconstruct the state of the
                     * injected dependency on activation. If that's the case we would probably won't be able to destroy the dependency later on anyway since the
                     * identity of the dependent instance would change. Destroying it now may be risky in certain circumstances.
                     *
                     * @see https://issues.jboss.org/browse/WELD-1076
                     */
                    destroy(instance);
                    iterator.remove();
                }
            }
        }
        // Return a serialization proxy for an "empty" instance
        if ( == null && .isEmpty() && ( == null || .isEmpty())) {
            return ;
        }
        return this;
    }

    
Register a ResourceReference as a dependency. ResourceReference.release() will be called on every ResourceReference once this CreationalContext instance is released.
    public void addDependentResourceReference(ResourceReference<?> resourceReference) {
        if ( == null) {
            this. = new ArrayList<ResourceReference<?>>();
        }
        this..add(resourceReference);
    }

    
Destroys dependent instance

Parameters:
instance
Returns:
true if the instance was destroyed, false otherwise
    public boolean destroyDependentInstance(T instance) {
        synchronized () {
            for (Iterator<ContextualInstance<?>> iterator = .iterator(); iterator.hasNext();) {
                ContextualInstance<?> contextualInstance = iterator.next();
                if (contextualInstance.getInstance().equals(instance)) {
                    iterator.remove();
                    destroy(contextualInstance);
                    return true;
                }
            }
        }
        return false;
    }

    

Returns:
the Contextual for which this CreationalContext is created.
    public Contextual<T> getContextual() {
        return ;
    }
        if ( == null) {
            return Collections.emptyList();
        }
        return ;
    }
    @Override
    public void setConstructorInterceptionSuppressed(boolean value) {
        this. = value;
    }
    @Override
    public boolean isConstructorInterceptionSuppressed() {
        return this.;
    }
    @Override
    public void registerAroundConstructCallback(AroundConstructCallback<T> callback) {
        if ( == null) {
            this. = new LinkedList<AroundConstructCallback<T>>();
        }
        this..add(callback);
    }

    

Author(s):
Martin Kouba
    private static class SerializationProxy implements Serializable {
        private static final long serialVersionUID = 5261112077771498097L;
        @java.lang.SuppressWarnings("rawtypes")
        private Object readResolve() throws ObjectStreamException {
            return new CreationalContextImpl();
        }
    }
New to GrepCode? Check out our FAQ X