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