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  import java.security.PrivilegedExceptionAction;
24  
25  import javax.faces.FacesException;
26  import javax.faces.context.ExternalContext;
27  
28  import org.apache.myfaces.spi.impl.DefaultFacesConfigResourceProviderFactory;
29  import org.apache.myfaces.spi.impl.SpiUtils;
30  
31  /**
32   * Factory that provide FacesConfigResourceProvider instances
33   * 
34   * @since 2.0.2
35   * @author Leonardo Uribe
36   */
37  public abstract class FacesConfigResourceProviderFactory
38  {
39      protected static final String FACTORY_DEFAULT = DefaultFacesConfigResourceProviderFactory.class.getName();
40  
41      private static final String FACTORY_KEY = FacesConfigResourceProviderFactory.class.getName();
42  
43      public static FacesConfigResourceProviderFactory getFacesConfigResourceProviderFactory(ExternalContext ctx)
44      {
45          FacesConfigResourceProviderFactory instance
46                  = (FacesConfigResourceProviderFactory) ctx.getApplicationMap().get(FACTORY_KEY);
47          if (instance != null)
48          {
49              return instance;
50          }
51          FacesConfigResourceProviderFactory lpf = null;
52          try
53          {
54  
55              if (System.getSecurityManager() != null)
56              {
57                  final ExternalContext ectx = ctx;
58                  lpf = (FacesConfigResourceProviderFactory)
59                          AccessController.doPrivileged(new PrivilegedExceptionAction<Object>()
60                          {
61                              public Object run() throws PrivilegedActionException
62                              {
63                                  return SpiUtils.build(ectx,
64                                          FacesConfigResourceProviderFactory.class,
65                                          FACTORY_DEFAULT);
66                              }
67                          });
68              }
69              else
70              {
71                  lpf = (FacesConfigResourceProviderFactory)
72                          SpiUtils.build(ctx, FacesConfigResourceProviderFactory.class, FACTORY_DEFAULT);
73              }
74          }
75          catch (PrivilegedActionException pae)
76          {
77              throw new FacesException(pae);
78          }
79          if (lpf != null)
80          {
81              setFacesConfigResourceProviderFactory(ctx, lpf);
82          }
83          return lpf;
84      }
85  
86      public static void setFacesConfigResourceProviderFactory(ExternalContext ctx,
87                                                               FacesConfigResourceProviderFactory instance)
88      {
89          ctx.getApplicationMap().put(FACTORY_KEY, instance);
90      }
91  
92      public abstract FacesConfigResourceProvider createFacesConfigResourceProvider(ExternalContext externalContext);
93  }