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.config.annotation;
20  
21  import java.security.AccessController;
22  import java.security.PrivilegedActionException;
23  import java.util.Map;
24  
25  import javax.faces.FacesException;
26  import javax.faces.context.ExternalContext;
27  import javax.faces.context.FacesContext;
28  
29  import org.apache.myfaces.spi.impl.SpiUtils;
30  
31  
32  
33  public abstract class LifecycleProviderFactory
34  {
35      protected static final String FACTORY_DEFAULT = DefaultLifecycleProviderFactory.class.getName();
36  
37      private static final String FACTORY_KEY = LifecycleProviderFactory.class.getName();
38  
39      public static LifecycleProviderFactory getLifecycleProviderFactory()
40      {
41          // Since we always provide a StartupFacesContext on initialization time, this is safe:
42          return getLifecycleProviderFactory(FacesContext.getCurrentInstance().getExternalContext());
43      }
44      
45      public static LifecycleProviderFactory getLifecycleProviderFactory(ExternalContext ctx)
46      {
47          Map<String, Object> applicationMap = ctx.getApplicationMap();
48          LifecycleProviderFactory instance = (LifecycleProviderFactory) applicationMap.get(FACTORY_KEY);
49          if (instance != null)
50          {
51              return instance;
52          }
53          LifecycleProviderFactory lpf = null;
54          try
55          {
56  
57              if (System.getSecurityManager() != null)
58              {
59                  final ExternalContext ectx = ctx; 
60                  lpf = (LifecycleProviderFactory)
61                          AccessController.doPrivileged(new java.security.PrivilegedExceptionAction<Object>()
62                          {
63                              public Object run() throws PrivilegedActionException
64                              {
65                                  return SpiUtils.build(ectx,
66                                          LifecycleProviderFactory.class,
67                                          FACTORY_DEFAULT);
68                              }
69                          });
70              }
71              else
72              {
73                  lpf = (LifecycleProviderFactory) SpiUtils.build(ctx, LifecycleProviderFactory.class, FACTORY_DEFAULT);
74              }
75          }
76          catch (PrivilegedActionException pae)
77          {
78              throw new FacesException(pae);
79          }
80          if (lpf != null)
81          {
82              applicationMap.put(FACTORY_KEY, lpf);
83          }
84          return lpf;
85      }
86  
87  
88      public static void setLifecycleProviderFactory(LifecycleProviderFactory instance)
89      {
90          FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().put(FACTORY_KEY, instance);
91      }
92  
93      public abstract LifecycleProvider getLifecycleProvider(ExternalContext externalContext);
94  
95      public abstract void release();
96  
97  }