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.util.HashMap; 022import java.util.Map; 023 024import org.w3c.dom.Element; 025 026import org.nuxeo.connect.update.PackageException; 027import org.nuxeo.connect.update.PackageUpdateService; 028import org.nuxeo.connect.update.ValidationStatus; 029import org.nuxeo.connect.update.task.Command; 030import org.nuxeo.connect.update.task.Task; 031import org.nuxeo.connect.update.task.guards.Guard; 032 033/** 034 * All commands have 2 attributes: fail and ignore which are EL expressions. 035 * <p> 036 * If ignore is defined and evaluated to true then the command will be ignored (null is returned as the inverse command) 037 * If fail is defined and evaluated to true then the validation fails. 038 * <p> 039 * Commands extending this class must implement the {@link #doRun} and {@link #doValidate} methods instead of the one in 040 * the interface. These methods are first testing for ignore and fail guards and then if needed delegated to the doXXX 041 * method versions. 042 * 043 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 044 */ 045public abstract class AbstractCommand implements Command { 046 047 /** 048 * List of files which must never be deleted at runtime. 049 * 050 * @since 5.5 051 */ 052 protected final String id; 053 054 protected final Map<String, Object> guardVars; 055 056 protected String fail; 057 058 protected String ignore; 059 060 protected AbstractCommand(String id) { 061 this.id = id; 062 guardVars = new HashMap<>(); 063 } 064 065 public AbstractCommand(AbstractCommand command) { 066 this.id = command.id; 067 guardVars = command.guardVars; 068 } 069 070 @Override 071 public void setPackageUpdateService(PackageUpdateService packageUpdateService) { 072 guardVars.put("packageUpdateService", packageUpdateService); 073 } 074 075 @Override 076 public boolean isPostInstall() { 077 return false; 078 } 079 080 /** 081 * Override to implement command actions 082 * 083 * @return Rollback command 084 */ 085 protected abstract Command doRun(Task task, Map<String, String> prefs) throws PackageException; 086 087 /** 088 * Override to implement validation. 089 * 090 * @param task The task being validated 091 * @param status Use {@link ValidationStatus#addError(String)} or {@link ValidationStatus#addWarning(String)} to 092 * provide validation error/warning messages 093 */ 094 protected abstract void doValidate(Task task, ValidationStatus status) throws PackageException; 095 096 @Override 097 public void validate(Task task, ValidationStatus status) throws PackageException { 098 if (fail != null) { 099 if (new Guard(fail).evaluate(guardVars)) { 100 status.addError("Guard failed for command " + getId() + ": " + fail); 101 } 102 } 103 doValidate(task, status); 104 } 105 106 @Override 107 public Command run(Task task, Map<String, String> prefs) throws PackageException { 108 if (ignore()) { 109 return null; 110 } 111 return doRun(task, prefs); 112 } 113 114 @Override 115 public String getId() { 116 return id; 117 } 118 119 public void setFail(String fail) { 120 this.fail = fail; 121 } 122 123 public void setIgnore(String ignore) { 124 this.ignore = ignore; 125 } 126 127 public boolean ignore() throws PackageException { 128 if (ignore != null) { 129 return new Guard(ignore).evaluate(guardVars); 130 } 131 return false; 132 } 133 134 @Override 135 public void initialize(Element element) throws PackageException { 136 String v = element.getAttribute("fail"); 137 if (v.length() > 0) { 138 fail = v; 139 } 140 v = element.getAttribute("ignore"); 141 if (v.length() > 0) { 142 ignore = v; 143 } 144 v = element.getAttribute("if"); 145 if (v.length() > 0) { 146 ignore = String.format("!(%s)", v); 147 } 148 readFrom(element); 149 } 150 151 /** 152 * Must be implemented to initialize the command arguments from an XML fragment. 153 */ 154 public abstract void readFrom(Element element) throws PackageException; 155 156}