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