Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.context.creational » [javadoc | source]

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    * 
    9    *       http://www.apache.org/licenses/LICENSE-2.0
   10    * 
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   package org.apache.webbeans.context.creational;
   18   
   19   import java.util.Collection;
   20   import java.util.Iterator;
   21   import java.util.Map;
   22   import java.util.WeakHashMap;
   23   import java.util.concurrent.ConcurrentHashMap;
   24   
   25   import javax.enterprise.context.spi.Contextual;
   26   import javax.enterprise.context.spi.CreationalContext;
   27   
   28   import org.apache.webbeans.util.Asserts;
   29   
   30   /** {@inheritDoc} */
   31   public class CreationalContextImpl<T> implements CreationalContext<T>
   32   {
   33       /**Map of bean with its incomplete instance*/
   34       private Map<Contextual<?>,Object> incompleteInstancesMap = new ConcurrentHashMap<Contextual<?>, Object>();
   35   
   36       /**Contextual bean*/
   37       private Contextual<T> incompleteBean = null;
   38       
   39       /**Contextual bean dependent instances*/
   40       private Map<Object, Contextual<?>> dependentObjects = new WeakHashMap<Object, Contextual<?>>();
   41       
   42       /**
   43        * Package private
   44        */
   45       CreationalContextImpl()
   46       {
   47           //Empty
   48       }
   49       
   50       /**
   51        * Returns new creational context.
   52        * 
   53        * @param incompleteBean incomplete instance owner
   54        * @return new creational context
   55        */
   56       protected CreationalContextImpl<T> getCreationalContextImpl(Contextual<T> incompleteBean)
   57       {
   58           CreationalContextImpl<T> impl = new CreationalContextImpl<T>();        
   59           
   60           impl.incompleteBean = incompleteBean;
   61           impl.incompleteInstancesMap = this.incompleteInstancesMap;
   62           
   63           return impl;
   64           
   65       }
   66       
   67       /**
   68        * Save this incomplete instance.
   69        * 
   70        * @param incompleteInstance incomplete bean instance
   71        */
   72       public void push(T incompleteInstance)
   73       {
   74           this.incompleteInstancesMap.put(this.incompleteBean, incompleteInstance);
   75           
   76       }
   77       
   78       
   79       /**
   80        * Adds given dependent instance to the map.
   81        * 
   82        * @param dependent dependent contextual
   83        * @param instance dependent instance
   84        */
   85       public <K> void addDependent(Contextual<K> dependent, Object instance)
   86       {
   87           Asserts.assertNotNull(dependent,"dependent parameter cannot be null");
   88           
   89           if(instance != null)
   90           {
   91               synchronized (this.dependentObjects)
   92               {
   93                   this.dependentObjects.put(instance, dependent);   
   94               }            
   95           }
   96       }
   97       
   98       /**
   99        * Returns incomplete instance.
  100        * 
  101        * @param incompleteBean instance owner
  102        * @return incomplete instance
  103        */
  104       public Object get(Contextual<?> incompleteBean)
  105       {
  106           return incompleteInstancesMap.get(incompleteBean);
  107       }
  108       
  109       
  110       /**
  111        * Removes from creational context.
  112        * 
  113        * @param bean owner bean
  114        */
  115       public void  remove()
  116       {
  117           if(this.incompleteInstancesMap.containsKey(this.incompleteBean))
  118           {
  119               this.incompleteInstancesMap.remove(this.incompleteBean);
  120           }        
  121       }
  122       
  123       /**
  124        * Removes dependent objects.
  125        */
  126       @SuppressWarnings("unchecked")
  127       private void  removeDependents()
  128       {
  129           //Clear its dependence objects
  130           synchronized (this.dependentObjects)
  131           {
  132               Collection<?> values = this.dependentObjects.keySet();
  133               Iterator<?> iterator = values.iterator();
  134               
  135               while(iterator.hasNext())
  136               {
  137                   T instance = (T)iterator.next();
  138                   Contextual<T> dependent = (Contextual<T>)this.dependentObjects.get(instance);
  139                   dependent.destroy(instance, (CreationalContext<T>)this);                
  140               }
  141               
  142               this.dependentObjects.clear();
  143           }
  144           
  145       }
  146       
  147       /**
  148        * Clear registry.
  149        */
  150       public void clear()
  151       {
  152           this.incompleteInstancesMap.clear();
  153       }
  154   
  155       /**
  156        * {@inheritDoc}
  157        */
  158       @Override
  159       public void release()
  160       {
  161           removeDependents();        
  162           
  163       }
  164   
  165   }

Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.context.creational » [javadoc | source]