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