001/*
002 * (C) Copyright 2006-2010 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.util.Map;
023
024import org.nuxeo.connect.update.PackageException;
025import org.nuxeo.connect.update.ValidationStatus;
026import org.nuxeo.connect.update.task.Command;
027import org.nuxeo.connect.update.task.Task;
028import org.nuxeo.connect.update.xml.XmlWriter;
029import org.w3c.dom.Element;
030
031/**
032 * Install bundle, flush any application cache and perform Nuxeo preprocessing on the bundle. The inverse of this
033 * command is Undeploy.
034 *
035 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
036 */
037public class UndeployPlaceholder extends AbstractCommand {
038
039    public static final String ID = "undeploy";
040
041    protected File file;
042
043    public UndeployPlaceholder() {
044        super(ID);
045    }
046
047    public UndeployPlaceholder(File file) {
048        super(ID);
049        this.file = file;
050    }
051
052    @Override
053    protected void doValidate(Task task, ValidationStatus status) throws PackageException {
054        // nothing to do
055    }
056
057    @Override
058    protected Command doRun(Task task, Map<String, String> prefs) throws PackageException {
059        if (!file.isFile()) {
060            // avoid throwing errors - this may happen at uninstall for
061            // broken packages
062            return null;
063        }
064        new UninstallPlaceholder(file).doRun(task, prefs);
065        // standalone mode: nothing to do
066        return new DeployPlaceholder(file);
067    }
068
069    @Override
070    public void readFrom(Element element) throws PackageException {
071        String v = element.getAttribute("file");
072        if (v.length() > 0) {
073            file = new File(v);
074            guardVars.put("file", file);
075        }
076    }
077
078    @Override
079    public void writeTo(XmlWriter writer) {
080        writer.start(ID);
081        if (file != null) {
082            writer.attr("file", file.getAbsolutePath());
083        }
084        writer.end();
085    }
086}