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

docet.SimplePackageLocator Maven / Gradle / Ivy

/*
 * Licensed to Diennea S.r.l. under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Diennea S.r.l. licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package docet;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import docet.engine.DocetConfiguration;
import docet.error.DocetPackageNotFoundException;

public class SimplePackageLocator implements DocetPackageLocator {

    private static final Logger LOGGER = Logger.getLogger(SimplePackageLocator.class.getName());
    private final Map installedPackages;
    private final DocetConfiguration docetConf;

    public SimplePackageLocator(final DocetConfiguration docetConf) {
        this.docetConf = docetConf;
        this.installedPackages = new HashMap<>();
        this.initializeInstalledPackages();
    }

    private void initializeInstalledPackages() {
        //only in case we are in developer mode then load packages at startup
        final Set availablePackages = this.docetConf.getInstalledPackages();
        if (!availablePackages.isEmpty()) {
            for (final String packageId : availablePackages) {
                Path directory = Paths.get(this.docetConf.getPathToDocPackage(packageId)).toAbsolutePath();
                LOGGER.log(Level.SEVERE, "initialize package {0} in {1}", new Object[]{packageId, directory});
                boolean initializationError = false;
                if (!Files.isDirectory(directory)) {
                    LOGGER.log(Level.SEVERE, "Cannot find package {0} directory {1}",
                        new Object[]{packageId, directory});
                    initializationError = true;
                }
                final Path docsBasePath = Paths.get(this.docetConf.getDocetPackageDocsFolderPath());
                final Path docsDirectory = directory.resolve(docsBasePath);
                if (!Files.isDirectory(docsDirectory)) {
                    LOGGER.log(Level.SEVERE, "Cannot find package {0} docs folder {1}",
                        new Object[]{packageId, docsDirectory});
                    initializationError = true;
                }
                final Path searchBasePath = Paths.get(this.docetConf.getDocetPackageSearchIndexFolderPath());
                final Path searchDirectory = directory.resolve(searchBasePath);
                if (!Files.isDirectory(searchDirectory)) {
                    LOGGER.log(Level.SEVERE, "Cannot find package {0} search index folder {1}",
                        new Object[]{packageId, searchDirectory});
                    initializationError = true;
                }
                if (!initializationError) {
                    final DocetPackageLocation packageBasePath = new DocetPackageLocation(packageId, directory);
                    this.installedPackages.put(packageId, packageBasePath);
                    LOGGER.log(Level.SEVERE, "initialize package {0} in {1} success", new Object[]{packageId, directory});
                } else {
                    LOGGER.log(Level.SEVERE, "initialize package {0} in {1} failure", new Object[]{packageId, directory});
                }
            }
        }
    }

    public List getInstalledPackages() {
        final List res = new ArrayList<>();
        res.addAll(this.installedPackages.values());
        return res;
    }

    @Override
    public DocetPackageLocation getPackageLocation(String packageId) throws DocetPackageNotFoundException {
        final DocetPackageLocation res = this.installedPackages.get(packageId);
        if (res == null) {
            throw new DocetPackageNotFoundException("Package '" + packageId + "' not available");
        }
        return res;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy