All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mycore.datamodel.niofs.MCRFileSystemPromoter Maven / Gradle / Ivy

There is a newer version: 2024.05
Show newest version
/*
 * This file is part of ***  M y C o R e  ***
 * See http://www.mycore.de/ for details.
 *
 * MyCoRe is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MyCoRe is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MyCoRe.  If not, see .
 */

package org.mycore.datamodel.niofs;

import java.nio.file.FileSystem;
import java.nio.file.spi.FileSystemProvider;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.ServiceLoader;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import javax.servlet.ServletContext;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.mycore.common.events.MCRStartupHandler.AutoExecutable;

/**
 * This {@link AutoExecutable} checks if the {@link FileSystem} implementations are available.
 * 
 * There is a documented "feature" in OpenJDK 8 that only {@link FileSystemProvider}
 * available to the system {@link ClassLoader} are available.
 * We try to fix (a.k.a. hack) it right.
 * @author Thomas Scheffler (yagee)
 *
 */
public class MCRFileSystemPromoter implements AutoExecutable {

    private static Logger LOGGER = LogManager.getLogger(MCRFileSystemPromoter.class);

    /**
     * 
     */
    public MCRFileSystemPromoter() {
    }

    /* (non-Javadoc)
     * @see org.mycore.common.events.MCRStartupHandler.AutoExecutable#getName()
     */
    @Override
    public String getName() {
        return getClass().getSimpleName();
    }

    /* (non-Javadoc)
     * @see org.mycore.common.events.MCRStartupHandler.AutoExecutable#getPriority()
     */
    @Override
    public int getPriority() {
        return Integer.MAX_VALUE;
    }

    /* (non-Javadoc)
     * @see org.mycore.common.events.MCRStartupHandler.AutoExecutable#startUp(javax.servlet.ServletContext)
     */
    @Override
    public void startUp(ServletContext servletContext) {
        if (servletContext != null) {
            HashSet installedSchemes = FileSystemProvider.installedProviders()
                .stream()
                .map(FileSystemProvider::getScheme)
                .collect(Collectors.toCollection(HashSet::new));
            ServiceLoader sl = ServiceLoader.load(FileSystemProvider.class, getClass()
                .getClassLoader());
            promoteFileSystemProvider(StreamSupport.stream(sl.spliterator(), false)
                .filter(p -> !installedSchemes.contains(p.getScheme()))
                .collect(Collectors.toCollection(LinkedList::new)));
        }
    }

    private void promoteFileSystemProvider(List detectedProviders) {
        if (detectedProviders.isEmpty()) {
            return;
        }
        for (FileSystemProvider provider : detectedProviders) {
            LOGGER.info("Promoting filesystem {}: {}", provider.getScheme(), provider.getClass().getCanonicalName());
            MCRPaths.addFileSystemProvider(provider);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy