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