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 org.apache.myfaces.spi.impl.SpiUtils;
22  
23  import javax.faces.FacesException;
24  import javax.faces.context.ExternalContext;
25  import java.security.AccessController;
26  import java.security.PrivilegedActionException;
27  
28  /**
29   * SPI to provide a FacesFlowProviderFactory implementation and thus
30   * a custom FacesFlowProvider instance.
31   *
32   * @author Leonardo Uribe
33   * @since 2.2
34   */
35  public abstract class FacesFlowProviderFactory
36  {
37  
38      protected static final String FACTORY_DEFAULT = 
39          "org.apache.myfaces.spi.impl.DefaultFacesFlowProviderFactory";
40  
41      private static final String FACTORY_KEY = FacesFlowProviderFactory.class.getName();
42  
43      public static FacesFlowProviderFactory getFacesFlowProviderFactory(ExternalContext ctx)
44      {
45          FacesFlowProviderFactory factory
46                  = (FacesFlowProviderFactory) ctx.getApplicationMap().get(FACTORY_KEY);
47          
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 = (FacesFlowProviderFactory) AccessController.doPrivileged(
62                          new java.security.PrivilegedExceptionAction<Object>()
63                          {
64                              public Object run() throws PrivilegedActionException
65                              {
66                                  return SpiUtils.build(ectx,
67                                          FacesFlowProviderFactory.class,
68                                          FACTORY_DEFAULT);
69                              }
70                          });
71              }
72              else
73              {
74                  factory = (FacesFlowProviderFactory) SpiUtils
75                          .build(ctx, FacesFlowProviderFactory.class, FACTORY_DEFAULT);
76              }
77          }
78          catch (PrivilegedActionException pae)
79          {
80              throw new FacesException(pae);
81          }
82  
83          if (factory != null)
84          {
85              // cache instance on ApplicationMap
86              setFacesFlowProviderFactory(ctx, factory);
87          }
88  
89          return factory;
90      }
91  
92      public static void setFacesFlowProviderFactory(ExternalContext ctx,
93                                                            FacesFlowProviderFactory factory)
94      {
95          ctx.getApplicationMap().put(FACTORY_KEY, factory);
96      }
97  
98      public abstract FacesFlowProvider getFacesFlowProvider(ExternalContext externalContext);
99  
100 }