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