View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.common.events;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.net.URL;
24  import java.security.CodeSource;
25  import java.security.ProtectionDomain;
26  import java.util.Enumeration;
27  import java.util.Set;
28  
29  import org.apache.logging.log4j.LogManager;
30  import org.apache.logging.log4j.Logger;
31  import org.mycore.common.MCRClassTools;
32  
33  import jakarta.servlet.ServletContainerInitializer;
34  import jakarta.servlet.ServletContext;
35  import jakarta.servlet.ServletException;
36  import se.jiderhamn.classloader.leak.prevention.ClassLoaderLeakPreventor;
37  import se.jiderhamn.classloader.leak.prevention.ClassLoaderLeakPreventorFactory;
38  
39  /**
40   * @author Thomas Scheffler (yagee)
41   */
42  public class MCRServletContainerInitializer implements ServletContainerInitializer {
43  
44      /* (non-Javadoc)
45       * @see jakarta.servlet.ServletContainerInitializer#onStartup(java.util.Set, jakarta.servlet.ServletContext)
46       */
47      @Override
48      public void onStartup(final Set<Class<?>> c, final ServletContext ctx) throws ServletException {
49          final boolean runClassLoaderLeakPreventor = runClassLoaderLeakPreventor();
50          ClassLoaderLeakPreventor leakPreventor = null;
51          if (runClassLoaderLeakPreventor) {
52              final ClassLoaderLeakPreventorFactory leakPreventorFactory = new ClassLoaderLeakPreventorFactory();
53              ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
54              leakPreventor = leakPreventorFactory.newLeakPreventor(classLoader);
55              leakPreventor.runPreClassLoaderInitiators();
56          }
57          MCRShutdownHandler shutdownHandler = MCRShutdownHandler.getInstance();
58          shutdownHandler.isWebAppRunning = true;
59          shutdownHandler.leakPreventor = leakPreventor;
60          MCRStartupHandler.startUp(ctx);
61          //Make sure logging is configured
62          final Logger logger = LogManager.getLogger();
63          if (logger.isDebugEnabled()) {
64              try {
65                  Enumeration<URL> resources = MCRClassTools.getClassLoader().getResources("META-INF/web-fragment.xml");
66                  while (resources.hasMoreElements()) {
67                      logger.debug("Found: {}", resources.nextElement());
68                  }
69              } catch (IOException e) {
70                  // TODO Auto-generated catch block
71                  e.printStackTrace();
72              }
73              logger.debug("This class is here: {}", getSource(this.getClass()));
74          }
75      }
76  
77      private static String getSource(final Class<? extends MCRServletContainerInitializer> clazz) {
78          if (clazz == null) {
79              return null;
80          }
81          ProtectionDomain protectionDomain = clazz.getProtectionDomain();
82          CodeSource codeSource = protectionDomain.getCodeSource();
83          if (codeSource == null) {
84              LogManager.getLogger().warn("Cannot get CodeSource.");
85              return null;
86          }
87          URL location = codeSource.getLocation();
88          String fileName = location.getFile();
89          File sourceFile = new File(fileName);
90          return sourceFile.getName();
91      }
92  
93      private boolean runClassLoaderLeakPreventor() {
94          //do not run ClassLoaderLeakPreventor by default on JRE 17
95          String defaultValue = (Runtime.version().feature() > 11) ? Boolean.FALSE.toString() : Boolean.TRUE.toString();
96          final String propValue = System.getProperty("MCR.ClassLoaderLeakPreventor", defaultValue);
97          return Boolean.parseBoolean(propValue);
98      }
99  
100 }