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