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

network.oxalis.ng.commons.filesystem.OxalisHomeDirectory Maven / Gradle / Ivy

Go to download

Holds the stuff which is common for both inbound and outbound messages and should be shared between them.

The newest version!
/*
 * Copyright 2010-2018 Norwegian Agency for Public Management and eGovernment (Difi)
 *
 * Licensed under the EUPL, Version 1.1 or – as soon they
 * will be approved by the European Commission - subsequent
 * versions of the EUPL (the "Licence");
 *
 * You may not use this work except in compliance with the Licence.
 *
 * You may obtain a copy of the Licence at:
 *
 * https://joinup.ec.europa.eu/community/eupl/og_page/eupl
 *
 * Unless required by applicable law or agreed to in
 * writing, software distributed under the Licence is
 * distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied.
 * See the Licence for the specific language governing
 * permissions and limitations under the Licence.
 */

package network.oxalis.ng.commons.filesystem;

import lombok.extern.slf4j.Slf4j;
import network.oxalis.ng.api.filesystem.HomeDetector;
import network.oxalis.ng.api.lang.OxalisLoadingException;
import network.oxalis.ng.commons.util.Sortables;

import java.io.File;
import java.util.List;
import java.util.Objects;
import java.util.ServiceLoader;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
 * Represents the Oxalis Home directory, which is located by inspecting various
 * places in the system in this order:
 * 

*

    *
  1. Directory referenced by Local JNDI Context java:comp/env/OXALIS_HOME
  2. *
  3. Directory referenced by Java System Property -D OXALIS_HOME
  4. *
  5. Directory referenced by Environment Variable OXALIS_HOME
  6. *
  7. Directory .oxalis inside the home directory of the user
  8. *
* * @author steinar * @author thore */ @Slf4j public class OxalisHomeDirectory { private List homeDetectors; public OxalisHomeDirectory(List homeDetectors) { this.homeDetectors = homeDetectors; } @SuppressWarnings("unused") public OxalisHomeDirectory() { this(StreamSupport.stream(ServiceLoader.load(HomeDetector.class).spliterator(), false) .sorted(Sortables.comparator()) .collect(Collectors.toList())); } public File detect() { File directory = homeDetectors.stream() .sequential() .map(HomeDetector::detect) .filter(Objects::nonNull) .findFirst() .orElseThrow(() -> new OxalisLoadingException( "Unable to detect Oxalis home folder.")); try { validateOxalisHomeDirectory(directory); } catch (OxalisLoadingException ex) { log.error(ex.getMessage()); throw ex; } return directory; } private static void validateOxalisHomeDirectory(File oxalisHomeDirectory) { if (!oxalisHomeDirectory.exists()) { throw new OxalisLoadingException(oxalisHomeDirectory + " does not exist!"); } else if (!oxalisHomeDirectory.isDirectory()) { throw new OxalisLoadingException(oxalisHomeDirectory + " is not a directory"); } else if (!oxalisHomeDirectory.canRead()) { throw new OxalisLoadingException(oxalisHomeDirectory + " exists, is a directory but can not be read"); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy