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