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 FacesScopedContextImpl implements Context
39  {
40  
41      private BeanManager beanManager;
42      
43      public FacesScopedContextImpl(BeanManager beanManager)
44      {
45          this.beanManager = beanManager;
46      }
47      
48      protected FacesScopeBeanHolder getFacesScopeBeanHolder()
49      {
50          return getFacesScopeBeanHolder(FacesContext.getCurrentInstance());
51      }
52      
53      protected FacesScopeBeanHolder getFacesScopeBeanHolder(FacesContext facesContext)
54      {
55          return new FacesScopeBeanHolder();
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 getFacesScopeBeanHolder(facesContext).getContextualStorage(beanManager, facesContext);
74          }
75          else
76          {
77              return getFacesScopeBeanHolder(facesContext).getContextualStorageNoCreate(beanManager, facesContext);
78          }
79      }
80  
81      @Override
82      public Class<? extends Annotation> getScope()
83      {
84          return FacesScoped.class;
85      }
86  
87      @Override
88      public boolean isActive()
89      {
90          return isActive(FacesContext.getCurrentInstance());
91      }
92  
93      public boolean isActive(FacesContext facesContext)
94      {
95          if (facesContext == null)
96          {
97              return false;
98          }
99          return true;
100     }
101 
102     @Override
103     public <T> T get(Contextual<T> bean)
104     {
105         FacesContext facesContext = FacesContext.getCurrentInstance();
106 
107         checkActive(facesContext);
108 
109         if (facesContext != null)
110         {
111             ContextualStorage storage = getContextualStorage(false, facesContext);
112             if (storage != null)
113             {
114                 Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage();
115                 ContextualInstanceInfo<?> contextualInstanceInfo = contextMap.get(storage.getBeanKey(bean));
116 
117                 if (contextualInstanceInfo != null)
118                 {
119                     return (T) contextualInstanceInfo.getContextualInstance();
120                 }
121             }
122         }
123         else
124         {
125             throw new IllegalStateException("FacesContext cannot be found when resolving bean " +bean.toString());
126         }
127         return null;
128     }
129 
130     @Override
131     public <T> T get(Contextual<T> bean, CreationalContext<T> creationalContext)
132     {
133         FacesContext facesContext = FacesContext.getCurrentInstance();
134         
135         checkActive(facesContext);
136 
137         if (!(bean instanceof PassivationCapable))
138         {
139             throw new IllegalStateException(bean.toString() +
140                     " doesn't implement " + PassivationCapable.class.getName());
141         }
142 
143         ContextualStorage storage = getContextualStorage(true, facesContext);
144 
145         Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage();
146         ContextualInstanceInfo<?> contextualInstanceInfo = contextMap.get(storage.getBeanKey(bean));
147 
148         if (contextualInstanceInfo != null)
149         {
150             @SuppressWarnings("unchecked")
151             final T instance =  (T) contextualInstanceInfo.getContextualInstance();
152 
153             if (instance != null)
154             {
155                 return instance;
156             }
157         }
158 
159         return storage.createContextualInstance(bean, creationalContext);
160     }
161 
162     /**
163      * Destroy the Contextual Instance of the given Bean.
164      * @param bean dictates which bean shall get cleaned up
165      * @return <code>true</code> if the bean was destroyed, <code>false</code> if there was no such bean.
166      */
167     public boolean destroy(Contextual bean)
168     {
169         FacesContext facesContext = FacesContext.getCurrentInstance();
170         ContextualStorage storage = getContextualStorage(false, facesContext);
171         if (storage == null)
172         {
173             return false;
174         }
175         ContextualInstanceInfo<?> contextualInstanceInfo = storage.getStorage().get(storage.getBeanKey(bean));
176 
177         if (contextualInstanceInfo == null)
178         {
179             return false;
180         }
181 
182         bean.destroy(contextualInstanceInfo.getContextualInstance(), contextualInstanceInfo.getCreationalContext());
183         return true;
184     }
185 
186     /**
187      * Destroys all the Contextual Instances in the specified ContextualStorage.
188      * This is a static method to allow various holder objects to cleanup
189      * properly in &#064;PreDestroy.
190      */
191     public static void destroyAllActive(ContextualStorage storage)
192     {
193         Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage();
194         for (Map.Entry<Object, ContextualInstanceInfo<?>> entry : contextMap.entrySet())
195         {
196             if (!FacesScopeBeanHolder.FACES_SCOPE_MAP_INFO.equals(entry.getKey()))
197             {
198                 Contextual bean = storage.getBean(entry.getKey());
199 
200                 ContextualInstanceInfo<?> contextualInstanceInfo = entry.getValue();
201                 bean.destroy(contextualInstanceInfo.getContextualInstance(), 
202                     contextualInstanceInfo.getCreationalContext());
203             }
204         }
205     }
206 
207     /**
208      * Make sure that the Context is really active.
209      * @throws ContextNotActiveException if there is no active
210      *         Context for the current Thread.
211      */
212     protected void checkActive(FacesContext facesContext)
213     {
214         if (!isActive(facesContext))
215         {
216             throw new ContextNotActiveException("CDI context with scope annotation @"
217                 + getScope().getName() + " is not active with respect to the current thread");
218         }
219     }
220 
221 }