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.spi;
20  
21  import java.security.AccessController;
22  import java.security.PrivilegedActionException;
23  
24  import javax.faces.FacesException;
25  import javax.faces.context.ExternalContext;
26  
27  import org.apache.myfaces.spi.impl.DefaultFacesConfigurationProviderFactory;
28  import org.apache.myfaces.spi.impl.SpiUtils;
29  
30  /**
31   * SPI to provide a FacesConfigurationProviderFactory implementation and thus
32   * a custom FacesConfigurationProvider instance.
33   *
34   * @author Leonardo Uribe
35   * @since 2.0.3
36   */
37  public abstract class FacesConfigurationProviderFactory
38  {
39  
40      protected static final String FACTORY_DEFAULT = DefaultFacesConfigurationProviderFactory.class.getName();
41  
42      private static final String FACTORY_KEY = FacesConfigurationProviderFactory.class.getName();
43  
44      public static FacesConfigurationProviderFactory getFacesConfigurationProviderFactory(ExternalContext ctx)
45      {
46          FacesConfigurationProviderFactory factory
47                  = (FacesConfigurationProviderFactory) ctx.getApplicationMap().get(FACTORY_KEY);
48          if (factory != null)
49          {
50              // use cached instance
51              return factory;
52          }
53  
54          // create new instance from service entry
55          try
56          {
57  
58              if (System.getSecurityManager() != null)
59              {
60                  final ExternalContext ectx = ctx;
61                  factory = (FacesConfigurationProviderFactory) AccessController.doPrivileged(
62                          new java.security.PrivilegedExceptionAction<Object>()
63                          {
64                              public Object run() throws PrivilegedActionException
65                              {
66                                  //return DiscoverSingleton.find(
67                                  return SpiUtils.build(ectx,
68                                          FacesConfigurationProviderFactory.class,
69                                          FACTORY_DEFAULT);
70                              }
71                          });
72              }
73              else
74              {
75                  factory = (FacesConfigurationProviderFactory)
76                          SpiUtils.build(ctx, FacesConfigurationProviderFactory.class, FACTORY_DEFAULT);
77              }
78          }
79          catch (PrivilegedActionException pae)
80          {
81              throw new FacesException(pae);
82          }
83  
84          if (factory != null)
85          {
86              // cache instance on ApplicationMap
87              setFacesConfigurationProviderFactory(ctx, factory);
88          }
89  
90          return factory;
91      }
92  
93      public static void setFacesConfigurationProviderFactory(ExternalContext ctx,
94                                                              FacesConfigurationProviderFactory factory)
95      {
96          ctx.getApplicationMap().put(FACTORY_KEY, factory);
97      }
98  
99      public abstract FacesConfigurationProvider getFacesConfigurationProvider(ExternalContext externalContext);
100 
101 }