001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Julien Carsique
018 *
019 */
020
021package org.nuxeo.connect.update.task.standalone.commands;
022
023import java.util.Map;
024
025import org.nuxeo.connect.update.PackageException;
026import org.nuxeo.connect.update.ValidationStatus;
027import org.nuxeo.connect.update.task.Command;
028import org.nuxeo.connect.update.task.Task;
029import org.nuxeo.connect.update.xml.XmlWriter;
030import org.nuxeo.launcher.config.ConfigurationException;
031import org.nuxeo.launcher.config.ConfigurationGenerator;
032import org.w3c.dom.Element;
033
034/**
035 * Command for managing the configuration. It allows to set a property, add or remove a template.
036 *
037 * @since 5.5
038 */
039public class Config extends AbstractCommand {
040
041    public static final String ID = "config";
042
043    private String addtemplate;
044
045    private String rmtemplate;
046
047    private String set;
048
049    public Config() {
050        this(ID);
051    }
052
053    protected Config(String id) {
054        super(id);
055    }
056
057    @Override
058    public void writeTo(XmlWriter writer) {
059        writer.start(ID);
060        if (addtemplate != null) {
061            writer.attr("addtemplate", addtemplate);
062        }
063        if (rmtemplate != null) {
064            writer.attr("rmtemplate", rmtemplate);
065        }
066        if (set != null) {
067            writer.attr("set", set);
068        }
069        writer.end();
070    }
071
072    @Override
073    protected Command doRun(Task task, Map<String, String> prefs) throws PackageException {
074        Config rollback = new Config();
075        ConfigurationGenerator cg = new ConfigurationGenerator();
076        cg.init();
077        try {
078            if (addtemplate != null) {
079                cg.addTemplate(addtemplate);
080                rollback.rmtemplate = addtemplate;
081            }
082            if (rmtemplate != null) {
083                cg.rmTemplate(rmtemplate);
084                rollback.addtemplate = rmtemplate;
085            }
086            if (set != null) {
087                String[] newValue = set.split("=", 2);
088                String previousValue = cg.setProperty(newValue[0], (newValue[1].length() > 0 ? newValue[1] : null));
089                if (previousValue == null) {
090                    previousValue = "";
091                }
092                rollback.set = newValue[0] + "=" + previousValue;
093            }
094        } catch (ConfigurationException e) {
095            throw new PackageException(e);
096        }
097        return rollback;
098    }
099
100    @Override
101    protected void doValidate(Task task, ValidationStatus status) throws PackageException {
102        if (addtemplate == null && rmtemplate == null && set == null) {
103            status.addError("Cannot execute command in installer."
104                    + " Invalid config syntax: neither addtemplate, rmtemplate " + "or set was specified.");
105        }
106        if (set != null && !set.contains("=")) {
107            status.addError("Invalid config syntax: badly-formed property " + set);
108        }
109    }
110
111    @Override
112    public void readFrom(Element element) throws PackageException {
113        String v = element.getAttribute("addtemplate");
114        if (v.length() > 0) {
115            addtemplate = v;
116        }
117        v = element.getAttribute("rmtemplate");
118        if (v.length() > 0) {
119            rmtemplate = v;
120        }
121        v = element.getAttribute("set");
122        if (v.length() > 0) {
123            set = v;
124        }
125    }
126
127}