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 * Deploy an OSGi bundle into the running platform. The bundle is specified using the absolute bundle file path. The
033 * inverse of this command is the Undeploy command.
034 *
035 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
036 */
037public class InstallPlaceholder extends AbstractCommand {
038
039    public static final String ID = "install";
040
041    protected File file;
042
043    public InstallPlaceholder() {
044        super(ID);
045    }
046
047    public InstallPlaceholder(File file) {
048        super(ID);
049        this.file = file;
050    }
051
052    @Override
053    protected void doValidate(Task task, ValidationStatus status) throws PackageException {
054        if (file == null) {
055            status.addError("Invalid install syntax: No file specified");
056        }
057    }
058
059    @Override
060    protected Command doRun(Task task, Map<String, String> prefs) throws PackageException {
061        // standalone mode: nothing to do
062        return new UninstallPlaceholder(file);
063    }
064
065    public void readFrom(Element element) throws PackageException {
066        String v = element.getAttribute("file");
067        if (v.length() > 0) {
068            file = new File(v);
069            guardVars.put("file", file);
070        }
071    }
072
073    public void writeTo(XmlWriter writer) {
074        writer.start(ID);
075        if (file != null) {
076            writer.attr("file", file.getAbsolutePath());
077        }
078        writer.end();
079    }
080}