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.impl;
20  
21  import org.apache.myfaces.config.DefaultFacesConfigurationProvider;
22  import org.apache.myfaces.shared.util.ClassUtils;
23  import org.apache.myfaces.spi.FacesConfigurationProvider;
24  import org.apache.myfaces.spi.FacesConfigurationProviderFactory;
25  import org.apache.myfaces.spi.ServiceProviderFinderFactory;
26  
27  import javax.faces.FacesException;
28  import javax.faces.context.ExternalContext;
29  import java.lang.reflect.InvocationTargetException;
30  import java.security.AccessController;
31  import java.security.PrivilegedActionException;
32  import java.security.PrivilegedExceptionAction;
33  import java.util.List;
34  import java.util.logging.Level;
35  import java.util.logging.Logger;
36  
37  /**
38   * 
39   * @author Leonardo Uribe
40   * @since 2.0.3
41   */
42  public class DefaultFacesConfigurationProviderFactory extends FacesConfigurationProviderFactory
43  {
44  
45      public static final String FACES_CONFIGURATION_PROVIDER = FacesConfigurationProvider.class.getName();
46      
47      public static final String FACES_CONFIGURATION_PROVIDER_LIST = FacesConfigurationProvider.class.getName()+".LIST";
48      
49      public static final String FACES_CONFIGURATION_PROVIDER_INSTANCE_KEY
50              = FacesConfigurationProvider.class.getName() + ".INSTANCE";
51  
52      private Logger getLogger()
53      {
54          return Logger.getLogger(DefaultFacesConfigurationProviderFactory.class.getName());
55      }
56  
57      @Override
58      public FacesConfigurationProvider getFacesConfigurationProvider(
59              ExternalContext externalContext)
60      {
61          FacesConfigurationProvider returnValue = (FacesConfigurationProvider)
62                  externalContext.getApplicationMap().get(FACES_CONFIGURATION_PROVIDER_INSTANCE_KEY);
63          if (returnValue == null)
64          {
65              final ExternalContext extContext = externalContext;
66              try
67              {
68                  if (System.getSecurityManager() != null)
69                  {
70                      returnValue
71                              = AccessController.doPrivileged(new PrivilegedExceptionAction<FacesConfigurationProvider>()
72                              {
73                                  public FacesConfigurationProvider run() throws ClassNotFoundException,
74                                          NoClassDefFoundError,
75                                          InstantiationException,
76                                          IllegalAccessException,
77                                          InvocationTargetException,
78                                          PrivilegedActionException
79                                  {
80                                      return resolveFacesConfigurationProviderFromService(extContext);
81                                  }
82                              });
83                  }
84                  else
85                  {
86                      returnValue = resolveFacesConfigurationProviderFromService(extContext);
87                  }
88                  externalContext.getApplicationMap().put(FACES_CONFIGURATION_PROVIDER_INSTANCE_KEY, returnValue);
89              }
90              catch (ClassNotFoundException e)
91              {
92                  // ignore
93              }
94              catch (NoClassDefFoundError e)
95              {
96                  // ignore
97              }
98              catch (InstantiationException e)
99              {
100                 getLogger().log(Level.SEVERE, "", e);
101             }
102             catch (IllegalAccessException e)
103             {
104                 getLogger().log(Level.SEVERE, "", e);
105             }
106             catch (InvocationTargetException e)
107             {
108                 getLogger().log(Level.SEVERE, "", e);
109             }
110             catch (PrivilegedActionException e)
111             {
112                 throw new FacesException(e);
113             }
114         }
115 
116 
117         return returnValue;
118     }
119 
120     private FacesConfigurationProvider resolveFacesConfigurationProviderFromService(
121             ExternalContext externalContext) throws ClassNotFoundException,
122             NoClassDefFoundError,
123             InstantiationException,
124             IllegalAccessException,
125             InvocationTargetException,
126             PrivilegedActionException
127     {
128         List<String> classList = (List<String>)
129                 externalContext.getApplicationMap().get(FACES_CONFIGURATION_PROVIDER_LIST);
130         if (classList == null)
131         {
132             classList = ServiceProviderFinderFactory.getServiceProviderFinder(externalContext).
133                     getServiceProviderList(FACES_CONFIGURATION_PROVIDER);
134             externalContext.getApplicationMap().put(FACES_CONFIGURATION_PROVIDER_LIST, classList);
135         }
136 
137         return ClassUtils.buildApplicationObject(FacesConfigurationProvider.class, classList,
138                                                  new DefaultFacesConfigurationProvider());
139     }
140 }