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.security.PrivilegedExceptionAction;
25  import java.util.List;
26  import java.util.logging.Level;
27  import java.util.logging.Logger;
28  import javax.faces.FacesException;
29  import javax.faces.context.ExternalContext;
30  import org.apache.myfaces.application.viewstate.StateCacheFactoryImpl;
31  import org.apache.myfaces.shared.util.ClassUtils;
32  import org.apache.myfaces.spi.ServiceProviderFinderFactory;
33  import org.apache.myfaces.spi.StateCacheProvider;
34  import org.apache.myfaces.spi.StateCacheProviderFactory;
35  
36  /**
37   *
38   */
39  public class DefaultStateCacheProviderFactory extends StateCacheProviderFactory
40  {
41      
42      public static final String STATE_CACHE_PROVIDER = StateCacheProvider.class.getName();
43      
44      public static final String STATE_CACHE_PROVIDER_LIST = StateCacheProvider.class.getName()+".LIST";
45      
46      public static final String STATE_CACHE_PROVIDER_INSTANCE = StateCacheProvider.class.getName()+".INSTANCE";
47      
48      private Logger getLogger()
49      {
50          return Logger.getLogger(DefaultStateCacheProviderFactory.class.getName());
51      }
52      
53      @Override
54      public StateCacheProvider getStateCacheProvider(ExternalContext externalContext)
55      {
56          StateCacheProvider stateCacheProvider
57                  = (StateCacheProvider) externalContext.getApplicationMap().get(STATE_CACHE_PROVIDER_INSTANCE);
58          if (stateCacheProvider == null)
59          {
60              stateCacheProvider = createStateCacheProvider(externalContext);
61              externalContext.getApplicationMap().put(STATE_CACHE_PROVIDER_INSTANCE, stateCacheProvider);
62          }
63          return stateCacheProvider;
64      }
65      
66      @Override
67      public StateCacheProvider createStateCacheProvider(
68              ExternalContext externalContext)
69      {
70          StateCacheProvider returnValue = null;
71          final ExternalContext extContext = externalContext;
72          try
73          {
74              if (System.getSecurityManager() != null)
75              {
76                  returnValue = AccessController.doPrivileged(new PrivilegedExceptionAction<StateCacheProvider>()
77                          {
78                              public StateCacheProvider run() throws ClassNotFoundException,
79                                      NoClassDefFoundError,
80                                      InstantiationException,
81                                      IllegalAccessException,
82                                      InvocationTargetException,
83                                      PrivilegedActionException
84                              {
85                                  return resolveStateCacheProviderFromService(extContext);
86                              }
87                          });
88              }
89              else
90              {
91                  returnValue = resolveStateCacheProviderFromService(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 StateCacheProvider resolveStateCacheProviderFromService(
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(STATE_CACHE_PROVIDER_LIST);
130         if (classList == null)
131         {
132             classList = ServiceProviderFinderFactory.getServiceProviderFinder(externalContext).
133                     getServiceProviderList(STATE_CACHE_PROVIDER);
134             externalContext.getApplicationMap().put(STATE_CACHE_PROVIDER_LIST, classList);
135         }
136         return ClassUtils.buildApplicationObject(StateCacheProvider.class, classList, new StateCacheFactoryImpl());
137     }    
138     
139 }