001/* 002 * (C) Copyright 2006-2012 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 * bstefanescu, jcarsique 018 */ 019package org.nuxeo.connect.update.task.update; 020 021import java.io.File; 022import java.util.Map; 023 024import org.apache.commons.logging.Log; 025import org.apache.commons.logging.LogFactory; 026import org.nuxeo.connect.update.PackageException; 027import org.nuxeo.connect.update.ValidationStatus; 028import org.nuxeo.connect.update.task.Command; 029import org.nuxeo.connect.update.task.Task; 030import org.nuxeo.connect.update.task.standalone.AbstractTask; 031import org.nuxeo.connect.update.task.standalone.commands.AbstractCommand; 032import org.nuxeo.connect.update.task.standalone.commands.UndeployPlaceholder; 033import org.nuxeo.connect.update.xml.XmlWriter; 034import org.w3c.dom.Element; 035 036/** 037 * @since 5.5 038 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 039 */ 040public class Rollback extends AbstractCommand { 041 042 protected static final Log log = LogFactory.getLog(Rollback.class); 043 044 public static final String ID = "rollback"; 045 046 protected String pkgId; 047 048 protected String key; 049 050 protected String version; 051 052 protected boolean deleteOnExit; 053 054 public Rollback() { 055 super(ID); 056 } 057 058 public Rollback(RollbackOptions opt) { 059 super(ID); 060 this.pkgId = opt.pkgId; 061 this.key = opt.key; 062 this.version = opt.version; 063 this.deleteOnExit = opt.deleteOnExit; 064 } 065 066 @Override 067 public void writeTo(XmlWriter writer) { 068 writer.start(ID); 069 if (key != null) { 070 writer.attr("key", key); 071 } 072 if (pkgId != null) { 073 writer.attr("pkgId", pkgId); 074 } 075 if (version != null) { 076 writer.attr("version", version); 077 } 078 if (deleteOnExit) { 079 writer.attr("deleteOnExit", "true"); 080 } 081 writer.end(); 082 } 083 084 @Override 085 public void readFrom(Element element) throws PackageException { 086 String v = element.getAttribute("version"); 087 if (v.length() > 0) { 088 version = v; 089 } 090 v = element.getAttribute("pkgId"); 091 if (v.length() > 0) { 092 pkgId = v; 093 } 094 v = element.getAttribute("key"); 095 if (v.length() > 0) { 096 key = v; 097 } 098 v = element.getAttribute("deleteOnExit"); 099 if (v.length() > 0) { 100 deleteOnExit = Boolean.parseBoolean(v); 101 } 102 } 103 104 @Override 105 protected void doValidate(Task task, ValidationStatus status) throws PackageException { 106 // allow null version for Studio snapshot jar 107 if (key == null) { 108 status.addError("Cannot execute command in installer." + " Invalid rollback syntax: key was not specified."); 109 } 110 } 111 112 @Override 113 protected Command doRun(Task task, Map<String, String> prefs) throws PackageException { 114 UpdateManager mgr = ((AbstractTask) task).getUpdateManager(); 115 RollbackOptions opt = new RollbackOptions(task.getPackage().getId(), key, version); 116 File rollbackTarget = mgr.getRollbackTarget(opt); 117 if (rollbackTarget != null) { 118 Command undeploy = getUndeployCommand(rollbackTarget); 119 if (undeploy != null) { 120 undeploy.run(task, prefs); 121 } 122 } 123 opt.setDeleteOnExit(deleteOnExit); 124 mgr.rollback(opt); 125 return null; 126 } 127 128 public RollbackOptions getRollbackOptions() { 129 return new RollbackOptions(pkgId, key, version); 130 } 131 132 /** 133 * Method to be overridden by subclasses to provide a undeploy command for hot reload 134 * 135 * @since 5.6 136 */ 137 protected Command getUndeployCommand(File targetFile) { 138 return new UndeployPlaceholder(targetFile); 139 } 140 141}