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;
20  
21  import java.security.AccessController;
22  import java.security.PrivilegedActionException;
23  import java.security.PrivilegedExceptionAction;
24  
25  import javax.faces.FacesException;
26  import javax.faces.context.ExternalContext;
27  
28  import org.apache.myfaces.spi.impl.DefaultAnnotationProviderFactory;
29  import org.apache.myfaces.spi.impl.SpiUtils;
30  
31  /**
32   * Factory that provide AnnotationProvider instances
33   * 
34   * @since 2.0.2
35   * @author Leonardo Uribe
36   */
37  public abstract class AnnotationProviderFactory
38  {
39      protected static final String FACTORY_DEFAULT = DefaultAnnotationProviderFactory.class.getName();
40  
41      private static final String FACTORY_KEY = AnnotationProviderFactory.class.getName();
42  
43      public static AnnotationProviderFactory getAnnotationProviderFactory(ExternalContext ctx)
44      {
45          AnnotationProviderFactory instance = (AnnotationProviderFactory) ctx.getApplicationMap().get(FACTORY_KEY);
46          if (instance != null)
47          {
48              return instance;
49          }
50          AnnotationProviderFactory lpf = null;
51          try
52          {
53  
54              if (System.getSecurityManager() != null)
55              {
56                  final ExternalContext ectx = ctx;
57                  lpf = (AnnotationProviderFactory) AccessController.doPrivileged(new PrivilegedExceptionAction<Object>()
58                          {
59                              public Object run() throws PrivilegedActionException
60                              {
61                                  return SpiUtils.build(ectx, 
62                                          AnnotationProviderFactory.class,
63                                          FACTORY_DEFAULT);
64                              }
65                          });
66              }
67              else
68              {
69                  lpf = (AnnotationProviderFactory) SpiUtils.build(ctx, AnnotationProviderFactory.class, FACTORY_DEFAULT);
70              }
71          }
72          catch (PrivilegedActionException pae)
73          {
74              throw new FacesException(pae);
75          }
76          if (lpf != null)
77          {
78              setAnnotationProviderFactory(ctx, lpf);
79          }
80          return lpf;
81      }
82  
83      public static void setAnnotationProviderFactory(ExternalContext ctx, AnnotationProviderFactory instance)
84      {
85          ctx.getApplicationMap().put(FACTORY_KEY, instance);
86      }
87  
88      public abstract AnnotationProvider createAnnotationProvider(ExternalContext externalContext);
89      
90      public AnnotationProvider getAnnotationProvider(ExternalContext externalContext)
91      {
92          return createAnnotationProvider(externalContext);
93      }
94  }