001/*
002 * (C) Copyright 2006-2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 */
018
019package org.nuxeo.ecm.platform.commandline.executor.service;
020
021import java.io.Serializable;
022import java.util.HashMap;
023import java.util.Map;
024
025import org.nuxeo.common.xmap.annotation.XNode;
026import org.nuxeo.common.xmap.annotation.XNodeMap;
027import org.nuxeo.common.xmap.annotation.XObject;
028
029@XObject(value = "environment")
030public class EnvironmentDescriptor implements Serializable {
031
032    private static final long serialVersionUID = 1L;
033
034    /**
035     * If {@code name} is null, then the environment is global.<br>
036     * Else the environment can be associated with a command ("command name") or with a tool ("command line").
037     *
038     * @since 7.4
039     */
040    @XNode("@name")
041    protected String name;
042
043    @XNode("workingDirectory")
044    protected String workingDirectory;
045
046    @XNodeMap(value = "parameters/parameter", key = "@name", type = HashMap.class, componentType = String.class)
047    private Map<String, String> parameters = new HashMap<>();
048
049    public String getWorkingDirectory() {
050        if (workingDirectory == null) {
051            workingDirectory = System.getProperty("java.io.tmpdir");
052        }
053        if (!workingDirectory.endsWith("/")) {
054            workingDirectory += "/";
055        }
056        return workingDirectory;
057    }
058
059    public EnvironmentDescriptor merge(EnvironmentDescriptor other) {
060        if (other != null) {
061            if (other.workingDirectory != null) {
062                workingDirectory = other.workingDirectory;
063            }
064            getParameters().putAll(other.getParameters());
065        }
066        return this;
067    }
068
069    /**
070     * @since 7.4
071     */
072    public Map<String, String> getParameters() {
073        return parameters;
074    }
075
076    /**
077     * @since 7.4
078     */
079    public String getName() {
080        return name;
081    }
082
083}