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:[email protected]">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 return; 076 } 077 if (file.isDirectory()) { 078 status.addError("Cannot delete directories: " + file.getName()); 079 } 080 } 081 082 @Override 083 protected Command doRun(Task task, Map<String, String> prefs) throws PackageException { 084 try { 085 if (file.isFile()) { 086 if (md5 != null && !md5.equals(IOUtils.createMd5(file))) { 087 // ignore the command since the md5 doesn't match 088 return null; 089 } 090 File bak = IOUtils.backup(task.getPackage(), file); 091 if (onExit) { 092 file.deleteOnExit(); 093 } else { 094 if (!file.delete()) { 095 throw new PackageException("Cannot delete " + file.getName()); 096 } 097 } 098 return new Copy(bak, file, md5, false, onExit); 099 } else { 100 return null; 101 } 102 } catch (IOException e) { 103 throw new PackageException("Failed to create backup when deleting: " + file.getName(), e); 104 } 105 } 106 107 @Override 108 public void readFrom(Element element) throws PackageException { 109 String v = element.getAttribute("file"); 110 if (v.length() > 0) { 111 FileRef ref = FileRef.newFileRef(v); 112 ref.fillPatternVariables(guardVars); 113 file = ref.getFile(); 114 guardVars.put("file", file); 115 } 116 v = element.getAttribute("md5"); 117 if (v.length() > 0) { 118 md5 = v; 119 } 120 v = element.getAttribute("onExit"); 121 if (v.length() > 0) { 122 onExit = Boolean.parseBoolean(v); 123 } 124 } 125 126 @Override 127 public void writeTo(XmlWriter writer) { 128 writer.start(ID); 129 if (file != null) { 130 writer.attr("file", file.getAbsolutePath()); 131 } 132 if (md5 != null) { 133 writer.attr("md5", md5); 134 } 135 if (onExit) { 136 writer.attr("onExit", "true"); 137 } 138 writer.end(); 139 } 140}