001/*
002 * #%L
003 * Netarchivesuite - deploy
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023package dk.netarkivet.deploy;
024
025import java.io.File;
026import java.io.FileWriter;
027import java.io.IOException;
028import java.nio.charset.Charset;
029
030import org.dom4j.Document;
031import org.dom4j.DocumentException;
032import org.dom4j.Element;
033import org.dom4j.io.SAXReader;
034
035import dk.netarkivet.common.exceptions.ArgumentNotValid;
036import dk.netarkivet.common.utils.FileUtils;
037
038/**
039 * Class for combining the different setting files into a complete settings file. The different settings are listed
040 * here: {@link Constants#BUILD_SETTING_FILES}
041 * <p>
042 * export NAS_SRC=$HOME/workspace/netarchivesuite cd $NAS_SRC ant jarfiles export
043 * CLASSPATH=$NAS_SRC/lib/dk.netarkivet.harvester.jar:$NAS_SRC/lib/dk.netarkivet.archive.jar:\
044 * $NAS_SRC/lib/dk.netarkivet.wayback.jar:$NAS_SRC/lib/dk.netarkivet.deploy.jar: cd src java
045 * dk.netarkivet.deploy.BuildCompleteSettings
046 */
047public final class BuildCompleteSettings {
048    /**
049     * Private constructor to disallow instantiation of this class.
050     */
051    private BuildCompleteSettings() {
052    }
053
054    /**
055     * Run the program. This loads and merges all the setting files into a single file.
056     *
057     * @param args Optional argument for name of complete settings file. E.g. /home/myUser/myDir/default_settings.xml
058     * @throws IOException For input/output errors.
059     */
060    public static void main(String[] args) {
061        if (args.length < 1) {
062            buildCompleteSettings(Constants.BUILD_COMPLETE_SETTINGS_FILE_PATH);
063        } else {
064            buildCompleteSettings(args[0]);
065        }
066    }
067
068    public static void buildCompleteSettings(String completeSettingsPath) {
069        ArgumentNotValid.checkNotNullOrEmpty(completeSettingsPath, "completeSettingsPath");
070        XmlStructure settings = null;
071        for (String path : Constants.BUILD_SETTING_FILES) {
072            File tmpFile = FileUtils.getResourceFileFromClassPath(path);
073            if (settings == null) {
074                settings = new XmlStructure(tmpFile, Charset.defaultCharset().name());
075            } else {
076                Element elem = retrieveXmlSettingsTree(tmpFile);
077                if (elem != null) {
078                    settings.overWrite(elem);
079                } else {
080                    throw new ArgumentNotValid("No settings found at: " + tmpFile.getAbsolutePath());
081                }
082            }
083        }
084
085        try {
086            FileWriter fw = new FileWriter(new File(completeSettingsPath));
087            fw.append(settings.getXML());
088            fw.append(Constants.NEWLINE);
089            fw.close();
090        } catch (IOException e) {
091            throw new RuntimeException("Failed to write new settings", e);
092        }
093    }
094
095    /**
096     * Retrieves the main element from the file.
097     *
098     * @param settingFile The file to load into an Element. This has to be a temporary file, since it is deleted
099     * afterwards.
100     * @return The root of the XML structure of the settings file. Returns null if problems occurred during reading.
101     */
102    private static Element retrieveXmlSettingsTree(File settingFile) {
103        try {
104            Document doc;
105            SAXReader reader = new SAXReader();
106            if (settingFile.canRead()) {
107                doc = reader.read(settingFile);
108                settingFile.deleteOnExit();
109                return doc.getRootElement();
110            } else {
111                System.out.println("Cannot read file: " + settingFile.getAbsolutePath());
112            }
113        } catch (DocumentException e) {
114            System.err.println("Problems with file: " + settingFile.getAbsolutePath() + " : " + e);
115
116        }
117        return null;
118    }
119}