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.application;
20  
21  import java.util.logging.Level;
22  import java.util.logging.Logger;
23  
24  import javax.faces.application.Application;
25  import javax.faces.application.ApplicationFactory;
26  import javax.faces.context.ExternalContext;
27  import javax.faces.context.FacesContext;
28  
29  /**
30   * @author Manfred Geiler (latest modification by $Author$)
31   * @author Thomas Spiegl
32   * @version $Revision$ $Date$
33   */
34  public class ApplicationFactoryImpl extends ApplicationFactory
35  {
36      //private static final Log log = LogFactory.getLog(ApplicationFactoryImpl.class);
37      private static final Logger log = Logger.getLogger(ApplicationFactoryImpl.class.getName());
38  
39      /**
40       * Application is thread-safe (see Application javadoc)
41       * "Application represents a per-web-application singleton object..." FactoryFinder has a ClassLoader-Factory Map.
42       * Since each webapp has it's own ClassLoader, each webapp will have it's own private factory instances.
43       */
44      private Application _application;
45      
46      private boolean _myfacesInstanceAddedToApplicationMap = false;
47  
48      public ApplicationFactoryImpl()
49      {
50          createAndLogNewApplication();
51      }
52  
53      private void createAndLogNewApplication()
54      {
55          _application = new ApplicationImpl();
56          putApplicationOnMap();
57          if (log.isLoggable(Level.FINEST))
58          {
59              log.finest("New ApplicationFactory instance created");
60          }
61      }
62  
63      public void purgeApplication()
64      {
65          createAndLogNewApplication();
66      }
67  
68      @Override
69      public Application getApplication()
70      {
71          //Put it on ApplicationMap, so javax.faces.application.Application
72          //class can find it. This allows wrapped jsf 1.1 application instances
73          //to work correctly in jsf 1.2 as ri does.
74          if (_application != null && !_myfacesInstanceAddedToApplicationMap)
75          {
76              putApplicationOnMap();
77          }
78  
79          return _application;
80      }
81  
82      @Override
83      public void setApplication(Application application)
84      {
85          if (application == null)
86          {
87              throw new NullPointerException("Cannot set a null application in the ApplicationFactory");
88          }
89          _application = application;
90          putApplicationOnMap();
91      }
92      
93      private void putApplicationOnMap()
94      {
95          FacesContext facesContext = FacesContext.getCurrentInstance();
96          if (facesContext != null)
97          {
98              ExternalContext externalContext = facesContext.getExternalContext();
99              if (externalContext != null)
100             {
101                 externalContext.
102                     getApplicationMap().put("org.apache.myfaces.application.ApplicationImpl", _application);
103                 _myfacesInstanceAddedToApplicationMap = true;
104             }
105         }        
106     }
107 
108 }