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.util.Map;
24  
25  import javax.faces.FacesException;
26  import javax.faces.context.ExternalContext;
27  import javax.faces.context.FacesContext;
28  
29  import org.apache.myfaces.spi.impl.SpiUtils;
30  
31  public abstract class InjectionProviderFactory
32  {
33      protected static final String FACTORY_DEFAULT = 
34          "org.apache.myfaces.spi.impl.DefaultInjectionProviderFactory";
35  
36      private static final String FACTORY_KEY = InjectionProviderFactory.class.getName();
37  
38      public static InjectionProviderFactory getInjectionProviderFactory()
39      {
40          // Since we always provide a StartupFacesContext on initialization time, this is safe:
41          return getInjectionProviderFactory(FacesContext.getCurrentInstance().getExternalContext());
42      }
43      
44      public static InjectionProviderFactory getInjectionProviderFactory(ExternalContext ctx)
45      {
46          Map<String, Object> applicationMap = ctx.getApplicationMap();
47          InjectionProviderFactory instance = 
48              (InjectionProviderFactory) applicationMap.get(FACTORY_KEY);
49          if (instance != null)
50          {
51              return instance;
52          }
53          InjectionProviderFactory lpf = null;
54          try
55          {
56  
57              if (System.getSecurityManager() != null)
58              {
59                  final ExternalContext ectx = ctx; 
60                  lpf = (InjectionProviderFactory)
61                          AccessController.doPrivileged(new java.security.PrivilegedExceptionAction<Object>()
62                          {
63                              public Object run() throws PrivilegedActionException
64                              {
65                                  return SpiUtils.build(ectx,
66                                          InjectionProviderFactory.class,
67                                          FACTORY_DEFAULT);
68                              }
69                          });
70              }
71              else
72              {
73                  lpf = (InjectionProviderFactory) SpiUtils.build(ctx, 
74                      InjectionProviderFactory.class, FACTORY_DEFAULT);
75              }
76          }
77          catch (PrivilegedActionException pae)
78          {
79              throw new FacesException(pae);
80          }
81          if (lpf != null)
82          {
83              applicationMap.put(FACTORY_KEY, lpf);
84          }
85          return lpf;
86      }
87  
88  
89      public static void setInjectionProviderFactory(InjectionProviderFactory instance)
90      {
91          FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().put(FACTORY_KEY, instance);
92      }
93  
94      public abstract InjectionProvider getInjectionProvider(ExternalContext externalContext);
95  
96      public abstract void release();
97  
98  }