001/* 002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Julien Carsique 016 * 017 */ 018 019package org.nuxeo.connect.update.task.standalone.commands; 020 021import java.util.Map; 022 023import org.nuxeo.connect.update.PackageException; 024import org.nuxeo.connect.update.ValidationStatus; 025import org.nuxeo.connect.update.task.Command; 026import org.nuxeo.connect.update.task.Task; 027import org.nuxeo.connect.update.xml.XmlWriter; 028import org.nuxeo.launcher.config.ConfigurationException; 029import org.nuxeo.launcher.config.ConfigurationGenerator; 030import org.w3c.dom.Element; 031 032/** 033 * Command for managing the configuration. It allows to set a property, add or remove a template. 034 * 035 * @since 5.5 036 */ 037public class Config extends AbstractCommand { 038 039 public static final String ID = "config"; 040 041 private String addtemplate; 042 043 private String rmtemplate; 044 045 private String set; 046 047 public Config() { 048 this(ID); 049 } 050 051 protected Config(String id) { 052 super(id); 053 } 054 055 @Override 056 public void writeTo(XmlWriter writer) { 057 writer.start(ID); 058 if (addtemplate != null) { 059 writer.attr("addtemplate", addtemplate); 060 } 061 if (rmtemplate != null) { 062 writer.attr("rmtemplate", rmtemplate); 063 } 064 if (set != null) { 065 writer.attr("set", set); 066 } 067 writer.end(); 068 } 069 070 @Override 071 protected Command doRun(Task task, Map<String, String> prefs) throws PackageException { 072 Config rollback = new Config(); 073 ConfigurationGenerator cg = new ConfigurationGenerator(); 074 cg.init(); 075 try { 076 if (addtemplate != null) { 077 cg.addTemplate(addtemplate); 078 rollback.rmtemplate = addtemplate; 079 } 080 if (rmtemplate != null) { 081 cg.rmTemplate(rmtemplate); 082 rollback.addtemplate = rmtemplate; 083 } 084 if (set != null) { 085 String[] newValue = set.split("=", 2); 086 String previousValue = cg.setProperty(newValue[0], (newValue[1].length() > 0 ? newValue[1] : null)); 087 if (previousValue == null) { 088 previousValue = ""; 089 } 090 rollback.set = newValue[0] + "=" + previousValue; 091 } 092 } catch (ConfigurationException e) { 093 throw new PackageException(e); 094 } 095 return rollback; 096 } 097 098 @Override 099 protected void doValidate(Task task, ValidationStatus status) throws PackageException { 100 if (addtemplate == null && rmtemplate == null && set == null) { 101 status.addError("Cannot execute command in installer." 102 + " Invalid config syntax: neither addtemplate, rmtemplate " + "or set was specified."); 103 } 104 if (set != null && !set.contains("=")) { 105 status.addError("Invalid config syntax: badly-formed property " + set); 106 } 107 } 108 109 @Override 110 public void readFrom(Element element) throws PackageException { 111 String v = element.getAttribute("addtemplate"); 112 if (v.length() > 0) { 113 addtemplate = v; 114 } 115 v = element.getAttribute("rmtemplate"); 116 if (v.length() > 0) { 117 rmtemplate = v; 118 } 119 v = element.getAttribute("set"); 120 if (v.length() > 0) { 121 set = v; 122 } 123 } 124 125}