Search in sources :

Example 1 with ClassLoaderLeakPreventor

use of se.jiderhamn.classloader.leak.prevention.ClassLoaderLeakPreventor in project mycore by MyCoRe-Org.

the class MCRServletContainerInitializer method onStartup.

/* (non-Javadoc)
     * @see javax.servlet.ServletContainerInitializer#onStartup(java.util.Set, javax.servlet.ServletContext)
     */
@Override
public void onStartup(final Set<Class<?>> c, final ServletContext ctx) throws ServletException {
    ClassLoaderLeakPreventor leakPreventor = new MCRClassLoaderLeakPreventor();
    leakPreventor.contextInitialized(new ServletContextEvent(ctx));
    MCRShutdownHandler shutdownHandler = MCRShutdownHandler.getInstance();
    shutdownHandler.isWebAppRunning = true;
    shutdownHandler.leakPreventor = leakPreventor;
    MCRStartupHandler.startUp(ctx);
    // Make sure logging is configured
    final Logger LOGGER = LogManager.getLogger();
    if (LOGGER.isDebugEnabled()) {
        try {
            Enumeration<URL> resources = this.getClass().getClassLoader().getResources("META-INF/web-fragment.xml");
            while (resources.hasMoreElements()) {
                LOGGER.debug("Found: {}", resources.nextElement());
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        LOGGER.debug("This class is here: {}", getSource(this.getClass()));
    }
}
Also used : IOException(java.io.IOException) Logger(org.apache.logging.log4j.Logger) ClassLoaderLeakPreventor(se.jiderhamn.classloader.leak.prevention.ClassLoaderLeakPreventor) ServletContextEvent(javax.servlet.ServletContextEvent) URL(java.net.URL)

Example 2 with ClassLoaderLeakPreventor

use of se.jiderhamn.classloader.leak.prevention.ClassLoaderLeakPreventor in project mycore by MyCoRe-Org.

the class MCRShutdownHandler method shutDown.

void shutDown() {
    Logger logger = LogManager.getLogger(MCRShutdownHandler.class);
    String cfgSystemName = "MyCoRe:";
    try {
        cfgSystemName = MCRConfiguration.instance().getString(PROPERTY_SYSTEM_NAME) + ":";
    } catch (MCRConfigurationException e) {
        // may occur early if there is an error starting mycore up or in JUnit tests
        logger.warn("Error getting '" + PROPERTY_SYSTEM_NAME + "': {}", e.getMessage());
    }
    final String system = cfgSystemName;
    System.out.println(system + " Shutting down system, please wait...\n");
    logger.debug(() -> "requests: " + requests);
    Closeable[] closeables = requests.stream().toArray(Closeable[]::new);
    Stream.of(closeables).peek(c -> logger.debug("Prepare Closing (1): {}", c)).forEach(Closeable::prepareClose);
    shutdownLock.writeLock().lock();
    try {
        shuttingDown = true;
        // during shut down more request may come in MCR-1726
        requests.stream().filter(c -> !Arrays.asList(closeables).contains(c)).peek(c -> logger.debug("Prepare Closing (2): {}", c)).forEach(Closeable::prepareClose);
        requests.stream().peek(c -> logger.debug("Closing: {}", c)).forEach(Closeable::close);
        System.out.println(system + " closing any remaining MCRSession instances, please wait...\n");
        MCRSessionMgr.close();
        System.out.println(system + " Goodbye, and remember: \"Alles wird gut.\"\n");
        LogManager.shutdown();
        SINGLETON = null;
    } finally {
        shutdownLock.writeLock().unlock();
    }
    // may be needed in webapp to release file handles correctly.
    if (leakPreventor != null) {
        ClassLoaderLeakPreventor myLeakPreventor = leakPreventor;
        leakPreventor = null;
        myLeakPreventor.contextDestroyed(null);
    }
}
Also used : Arrays(java.util.Arrays) ClassLoaderLeakPreventor(se.jiderhamn.classloader.leak.prevention.ClassLoaderLeakPreventor) MCRConfiguration(org.mycore.common.config.MCRConfiguration) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) MCRException(org.mycore.common.MCRException) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) MCRSessionMgr(org.mycore.common.MCRSessionMgr) LogManager(org.apache.logging.log4j.LogManager) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException) Logger(org.apache.logging.log4j.Logger) ClassLoaderLeakPreventor(se.jiderhamn.classloader.leak.prevention.ClassLoaderLeakPreventor)

Aggregations

Logger (org.apache.logging.log4j.Logger)2 ClassLoaderLeakPreventor (se.jiderhamn.classloader.leak.prevention.ClassLoaderLeakPreventor)2 IOException (java.io.IOException)1 URL (java.net.URL)1 Arrays (java.util.Arrays)1 Objects (java.util.Objects)1 ConcurrentSkipListSet (java.util.concurrent.ConcurrentSkipListSet)1 TimeUnit (java.util.concurrent.TimeUnit)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 Stream (java.util.stream.Stream)1 ServletContextEvent (javax.servlet.ServletContextEvent)1 LogManager (org.apache.logging.log4j.LogManager)1 MCRException (org.mycore.common.MCRException)1 MCRSessionMgr (org.mycore.common.MCRSessionMgr)1 MCRConfiguration (org.mycore.common.config.MCRConfiguration)1 MCRConfigurationException (org.mycore.common.config.MCRConfigurationException)1