View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.cdi.scope;
20  
21  import java.lang.annotation.Annotation;
22  import java.util.Map;
23  import javax.enterprise.context.ContextNotActiveException;
24  import javax.enterprise.context.spi.Context;
25  import javax.enterprise.context.spi.Contextual;
26  import javax.enterprise.context.spi.CreationalContext;
27  import javax.enterprise.inject.Typed;
28  import javax.enterprise.inject.spi.BeanManager;
29  import javax.enterprise.inject.spi.PassivationCapable;
30  import javax.faces.context.FacesContext;
31  import org.apache.myfaces.cdi.util.ContextualInstanceInfo;
32  import org.apache.myfaces.cdi.util.ContextualStorage;
33  
34  /**
35   * Minimal implementation of FacesScope.
36   */
37  @Typed()
38  public class ViewTransientScopedContextImpl implements Context
39  {
40  
41      private BeanManager beanManager;
42      
43      public ViewTransientScopedContextImpl(BeanManager beanManager)
44      {
45          this.beanManager = beanManager;
46      }
47      
48      protected ViewTransientScopeBeanHolder getViewTransientScopeBeanHolder()
49      {
50          return getViewTransientScopeBeanHolder(FacesContext.getCurrentInstance());
51      }
52      
53      protected ViewTransientScopeBeanHolder getViewTransientScopeBeanHolder(FacesContext facesContext)
54      {
55          return new ViewTransientScopeBeanHolder();
56      }
57      
58      /**
59       * An implementation has to return the underlying storage which
60       * contains the items held in the Context.
61       * @param createIfNotExist whether a ContextualStorage shall get created if it doesn't yet exist.
62       * @return the underlying storage
63       */
64      protected ContextualStorage getContextualStorage(boolean createIfNotExist, FacesContext facesContext)
65      {
66          if (facesContext == null)
67          {
68              throw new ContextNotActiveException("FacesScopedContextImpl: no current active facesContext");
69          }
70  
71          if (createIfNotExist)
72          {
73              return getViewTransientScopeBeanHolder(facesContext).getContextualStorage(beanManager, facesContext);
74          }
75          else
76          {
77              return getViewTransientScopeBeanHolder(facesContext).getContextualStorageNoCreate(
78                      beanManager, facesContext);
79          }
80      }
81  
82      public Class<? extends Annotation> getScope()
83      {
84          return ViewTransientScoped.class;
85      }
86  
87      public boolean isActive()
88      {
89          return isActive(FacesContext.getCurrentInstance());
90      }
91  
92      public boolean isActive(FacesContext facesContext)
93      {
94          if (facesContext == null)
95          {
96              return false;
97          }
98          else if (facesContext.getViewRoot() == null)
99          {
100             return false;
101         }
102         return true;
103     }
104 
105     @Override
106     public <T> T get(Contextual<T> bean)
107     {
108         FacesContext facesContext = FacesContext.getCurrentInstance();
109 
110         checkActive(facesContext);
111 
112         if (facesContext != null)
113         {
114             ContextualStorage storage = getContextualStorage(false, facesContext);
115             if (storage != null)
116             {
117                 Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage();
118                 ContextualInstanceInfo<?> contextualInstanceInfo = contextMap.get(storage.getBeanKey(bean));
119 
120                 if (contextualInstanceInfo != null)
121                 {
122                     return (T) contextualInstanceInfo.getContextualInstance();
123                 }
124             }
125         }
126         else
127         {
128             throw new IllegalStateException("FacesContext cannot be found when resolving bean " +bean.toString());
129         }
130         return null;
131     }
132 
133     public <T> T get(Contextual<T> bean, CreationalContext<T> creationalContext)
134     {
135         FacesContext facesContext = FacesContext.getCurrentInstance();
136         
137         checkActive(facesContext);
138 
139         if (!(bean instanceof PassivationCapable))
140         {
141             throw new IllegalStateException(bean.toString() +
142                     " doesn't implement " + PassivationCapable.class.getName());
143         }
144 
145         ContextualStorage storage = getContextualStorage(true, facesContext);
146 
147         Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage();
148         ContextualInstanceInfo<?> contextualInstanceInfo = contextMap.get(storage.getBeanKey(bean));
149 
150         if (contextualInstanceInfo != null)
151         {
152             @SuppressWarnings("unchecked")
153             final T instance =  (T) contextualInstanceInfo.getContextualInstance();
154 
155             if (instance != null)
156             {
157                 return instance;
158             }
159         }
160 
161         return storage.createContextualInstance(bean, creationalContext);
162     }
163 
164     /**
165      * Destroy the Contextual Instance of the given Bean.
166      * @param bean dictates which bean shall get cleaned up
167      * @return <code>true</code> if the bean was destroyed, <code>false</code> if there was no such bean.
168      */
169     public boolean destroy(Contextual bean)
170     {
171         FacesContext facesContext = FacesContext.getCurrentInstance();
172         ContextualStorage storage = getContextualStorage(false, facesContext);
173         if (storage == null)
174         {
175             return false;
176         }
177         ContextualInstanceInfo<?> contextualInstanceInfo = storage.getStorage().get(storage.getBeanKey(bean));
178 
179         if (contextualInstanceInfo == null)
180         {
181             return false;
182         }
183 
184         bean.destroy(contextualInstanceInfo.getContextualInstance(), contextualInstanceInfo.getCreationalContext());
185         return true;
186     }
187 
188     /**
189      * Destroys all the Contextual Instances in the specified ContextualStorage.
190      * This is a static method to allow various holder objects to cleanup
191      * properly in &#064;PreDestroy.
192      */
193     public static void destroyAllActive(ContextualStorage storage)
194     {
195         Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage();
196         for (Map.Entry<Object, ContextualInstanceInfo<?>> entry : contextMap.entrySet())
197         {
198             if (!ViewTransientScopeBeanHolder.VIEW_TRANSIENT_SCOPE_MAP_INFO.equals(entry.getKey()))
199             {
200                 Contextual bean = storage.getBean(entry.getKey());
201 
202                 ContextualInstanceInfo<?> contextualInstanceInfo = entry.getValue();
203                 bean.destroy(contextualInstanceInfo.getContextualInstance(), 
204                     contextualInstanceInfo.getCreationalContext());
205             }
206         }
207     }
208 
209     /**
210      * Make sure that the Context is really active.
211      * @throws ContextNotActiveException if there is no active
212      *         Context for the current Thread.
213      */
214     protected void checkActive(FacesContext facesContext)
215     {
216         if (!isActive(facesContext))
217         {
218             throw new ContextNotActiveException("CDI context with scope annotation @"
219                 + getScope().getName() + " is not active with respect to the current thread");
220         }
221     }
222 
223 }