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

org.btrplace.btrpsl.includes.PathBasedIncludes Maven / Gradle / Ivy

/*
 * Copyright (c) 2014 University Nice Sophia Antipolis
 *
 * This file is part of btrplace.
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 */

package org.btrplace.btrpsl.includes;

import org.btrplace.btrpsl.Script;
import org.btrplace.btrpsl.ScriptBuilder;
import org.btrplace.btrpsl.ScriptBuilderException;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * An implementation that loop for the searched script among several folders.
 * Similar to a shell path.
 *
 * @author Fabien Hermenier
 */
public class PathBasedIncludes implements Includes {

    /**
     * The folders to browse.
     */
    private List paths;

    /**
     * The builder to create the scripts.
     */
    private ScriptBuilder builder;

    /**
     * Make a new instance that will browse a first folder.
     *
     * @param vBuilder the builder to parse the scripts
     * @param path     the first folder to look into
     */
    public PathBasedIncludes(ScriptBuilder vBuilder, File path) {
        if (!path.isDirectory()) {
            throw new IllegalArgumentException(path + " must be an existing directory");
        }
        this.paths = new LinkedList<>();
        this.paths.add(path);
        this.builder = vBuilder;
    }

    /**
     * Make a new instance that will browse the current working directory.
     *
     * @param vBuilder the builder to parse the scripts
     */
    public PathBasedIncludes(ScriptBuilder vBuilder) {
        this(vBuilder, new File(System.getProperty("user.dir")));
    }

    /**
     * Get the script associated to a given identifier by browsing the given paths.
     * The first script having a matching identifier is selected, whatever the parsing process result will be
     *
     * @param name the identifier of the script
     * @return the script if found
     * @throws org.btrplace.btrpsl.ScriptBuilderException if the builder was not able to parse the looked script
     */
    @Override
    public List