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 java.lang.reflect.InvocationTargetException;
22  import java.security.AccessController;
23  import java.security.PrivilegedActionException;
24  import java.security.PrivilegedExceptionAction;
25  import java.util.List;
26  import java.util.logging.Level;
27  import java.util.logging.Logger;
28  
29  import javax.faces.FacesException;
30  import javax.faces.context.ExternalContext;
31  
32  import org.apache.myfaces.shared.util.ClassUtils;
33  import org.apache.myfaces.spi.ServiceProviderFinderFactory;
34  import org.apache.myfaces.spi.WebConfigProvider;
35  import org.apache.myfaces.spi.WebConfigProviderFactory;
36  
37  /**
38   * The default implementation of WebXmlProviderFactory. Looks for META-INF/service
39   * entries of org.apache.myfaces.shared.spi.WebXmlProvider.
40   *
41   * Returns a new DefaultWebXmlProvider if no custom impl can be found.
42   *
43   * @author Jakob Korherr
44   * @since 2.0.3
45   */
46  public class DefaultWebConfigProviderFactory extends WebConfigProviderFactory
47  {
48  
49      public static final String WEB_CONFIG_PROVIDER = WebConfigProvider.class.getName();
50      
51      public static final String WEB_CONFIG_PROVIDER_LIST = WebConfigProvider.class.getName()+".LIST";
52  
53      private Logger getLogger()
54      {
55          return Logger.getLogger(DefaultWebConfigProviderFactory.class.getName());
56      }
57  
58      @Override
59      public WebConfigProvider getWebConfigProvider(ExternalContext externalContext)
60      {
61          WebConfigProvider returnValue = null;
62          final ExternalContext extContext = externalContext;
63          try
64          {
65              if (System.getSecurityManager() != null)
66              {
67                  returnValue = AccessController.doPrivileged(new PrivilegedExceptionAction<WebConfigProvider>()
68                          {
69                              public WebConfigProvider run() throws ClassNotFoundException,
70                                      NoClassDefFoundError,
71                                      InstantiationException,
72                                      IllegalAccessException,
73                                      InvocationTargetException,
74                                      PrivilegedActionException
75                              {
76                                  return resolveWebXmlProviderFromService(extContext);
77                              }
78                          });
79              }
80              else
81              {
82                  returnValue = resolveWebXmlProviderFromService(extContext);
83              }
84          }
85          catch (ClassNotFoundException e)
86          {
87              // ignore
88          }
89          catch (NoClassDefFoundError e)
90          {
91              // ignore
92          }
93          catch (InstantiationException e)
94          {
95              getLogger().log(Level.SEVERE, "", e);
96          }
97          catch (IllegalAccessException e)
98          {
99              getLogger().log(Level.SEVERE, "", e);
100         }
101         catch (InvocationTargetException e)
102         {
103             getLogger().log(Level.SEVERE, "", e);
104         }
105         catch (PrivilegedActionException e)
106         {
107             throw new FacesException(e);
108         }
109 
110         return returnValue;
111     }
112 
113     private WebConfigProvider resolveWebXmlProviderFromService(
114             ExternalContext externalContext) throws ClassNotFoundException,
115             NoClassDefFoundError,
116             InstantiationException,
117             IllegalAccessException,
118             InvocationTargetException,
119             PrivilegedActionException
120     {
121         List<String> classList = (List<String>) externalContext.getApplicationMap().get(WEB_CONFIG_PROVIDER_LIST);
122         if (classList == null)
123         {
124             classList = ServiceProviderFinderFactory.getServiceProviderFinder(externalContext).
125                     getServiceProviderList(WEB_CONFIG_PROVIDER);
126             externalContext.getApplicationMap().put(WEB_CONFIG_PROVIDER_LIST, classList);
127         }
128 
129         return ClassUtils.buildApplicationObject(WebConfigProvider.class, classList, new DefaultWebConfigProvider());
130     }
131 
132 }