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  
20  package org.apache.myfaces.tobago.webapp;
21  
22  import org.apache.myfaces.tobago.config.TobagoConfig;
23  import org.apache.myfaces.tobago.internal.config.ContentSecurityPolicy;
24  import org.apache.myfaces.tobago.internal.config.TobagoConfigBuilder;
25  import org.apache.myfaces.tobago.internal.context.ResourceManagerFactory;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import javax.servlet.ServletContext;
30  import javax.servlet.ServletContextEvent;
31  import javax.servlet.ServletContextListener;
32  
33  public class TobagoServletContextListener implements ServletContextListener {
34  
35    private static final Logger LOG = LoggerFactory.getLogger(TobagoServletContextListener.class);
36  
37    public void contextInitialized(ServletContextEvent event) {
38  
39      if (LOG.isInfoEnabled()) {
40        LOG.info("*** contextInitialized ***");
41      }
42  
43      ServletContext servletContext = event.getServletContext();
44  
45      if (servletContext.getAttribute(TobagoConfig.TOBAGO_CONFIG) != null) {
46        LOG.warn("Tobago has been already initialized. Do nothing.");
47        return;
48      }
49  
50      TobagoConfigBuilder.init(servletContext);
51      if (LOG.isInfoEnabled()) {
52        final TobagoConfig tobagoConfig = TobagoConfig.getInstance(servletContext);
53        LOG.info("TobagoConfig: " + tobagoConfig);
54        final ContentSecurityPolicy.Mode mode = tobagoConfig.getContentSecurityPolicy().getMode();
55        StringBuilder builder = new StringBuilder();
56        builder.append("\n*************************************************************************************");
57        builder.append("\nNote: CSP is ");
58        builder.append(mode);
59        if (mode == ContentSecurityPolicy.Mode.ON) {
60          builder.append("\nYou may need to check application specific JavaScript code.");
61          builder.append("\nOtherwise the application will not run in modern browsers, that are supporting CSP.");
62          builder.append("\nFor more information see http://myfaces.apache.org/tobago/migration-2.0.html");
63        }
64        builder.append("\n*************************************************************************************");
65        final String note = builder.toString();
66        LOG.info(note);
67        LOG.info("ResourcesManager: " + ResourceManagerFactory.getResourceManager(servletContext));
68      }
69    }
70  
71    public void contextDestroyed(ServletContextEvent event) {
72      if (LOG.isInfoEnabled()) {
73        LOG.info("*** contextDestroyed ***\n--- snip ---------"
74            + "--------------------------------------------------------------");
75      }
76  
77      ServletContext servletContext = event.getServletContext();
78  
79      servletContext.removeAttribute(TobagoConfig.TOBAGO_CONFIG);
80  
81      ResourceManagerFactory.release(servletContext);
82  
83      //LogFactory.releaseAll();
84  //    LogManager.shutdown();
85    }
86  
87  }