001/* 002 * (C) Copyright 2006-2014 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 018 */ 019package org.nuxeo.connect.update.task.standalone.commands; 020 021import java.io.File; 022import java.io.IOException; 023import java.util.Map; 024 025import org.apache.commons.logging.Log; 026import org.apache.commons.logging.LogFactory; 027import org.w3c.dom.Element; 028import org.nuxeo.common.utils.FileRef; 029import org.nuxeo.connect.update.PackageException; 030import org.nuxeo.connect.update.ValidationStatus; 031import org.nuxeo.connect.update.task.Command; 032import org.nuxeo.connect.update.task.Task; 033import org.nuxeo.connect.update.util.IOUtils; 034import org.nuxeo.connect.update.xml.XmlWriter; 035 036/** 037 * The delete command. This command takes 2 arguments: the file path to delete and an optional md5. If md5 is set then 038 * the command fails if the target file has not the same md5. 039 * <p> 040 * The inverse of the delete command is a copy command. 041 * 042 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 043 */ 044public class Delete extends AbstractCommand { 045 046 protected static final Log log = LogFactory.getLog(Delete.class); 047 048 public static final String ID = "delete"; 049 050 protected File file; // the file to restore 051 052 protected String md5; 053 054 protected boolean onExit; 055 056 public Delete() { 057 super(ID); 058 } 059 060 public Delete(File file, String md5) { 061 this(file, md5, false); 062 } 063 064 public Delete(File file, String md5, boolean onExit) { 065 super(ID); 066 this.file = file; 067 this.md5 = md5; 068 this.onExit = onExit; 069 } 070 071 @Override 072 protected void doValidate(Task task, ValidationStatus status) { 073 if (file == null) { 074 status.addError("Invalid delete syntax: No file specified"); 075 } 076 if (file.isDirectory()) { 077 status.addError("Cannot delete directories: " + file.getName()); 078 } 079 } 080 081 @Override 082 protected Command doRun(Task task, Map<String, String> prefs) throws PackageException { 083 try { 084 if (file.isFile()) { 085 if (md5 != null && !md5.equals(IOUtils.createMd5(file))) { 086 // ignore the command since the md5 doesn't match 087 return null; 088 } 089 File bak = IOUtils.backup(task.getPackage(), file); 090 if (onExit) { 091 file.deleteOnExit(); 092 } else { 093 if (!file.delete()) { 094 throw new PackageException("Cannot delete " + file.getName()); 095 } 096 } 097 return new Copy(bak, file, md5, false, onExit); 098 } else { 099 return null; 100 } 101 } catch (IOException e) { 102 throw new PackageException("Failed to create backup when deleting: " + file.getName(), e); 103 } 104 } 105 106 @Override 107 public void readFrom(Element element) throws PackageException { 108 String v = element.getAttribute("file"); 109 if (v.length() > 0) { 110 FileRef ref = FileRef.newFileRef(v); 111 ref.fillPatternVariables(guardVars); 112 file = ref.getFile(); 113 guardVars.put("file", file); 114 } 115 v = element.getAttribute("md5"); 116 if (v.length() > 0) { 117 md5 = v; 118 } 119 v = element.getAttribute("onExit"); 120 if (v.length() > 0) { 121 onExit = Boolean.parseBoolean(v); 122 } 123 } 124 125 @Override 126 public void writeTo(XmlWriter writer) { 127 writer.start(ID); 128 if (file != null) { 129 writer.attr("file", file.getAbsolutePath()); 130 } 131 if (md5 != null) { 132 writer.attr("md5", md5); 133 } 134 if (onExit) { 135 writer.attr("onExit", "true"); 136 } 137 writer.end(); 138 } 139}