001/*
002 * (C) Copyright 2006-2015 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 *     Nuxeo - initial API and implementation
018 *
019 */
020
021package org.nuxeo.ecm.platform.commandline.executor.service;
022
023import java.io.Serializable;
024import java.util.HashMap;
025import java.util.Map;
026
027import org.nuxeo.common.Environment;
028import org.nuxeo.common.xmap.annotation.XNode;
029import org.nuxeo.common.xmap.annotation.XNodeMap;
030import org.nuxeo.common.xmap.annotation.XObject;
031
032@XObject(value = "environment")
033public class EnvironmentDescriptor implements Serializable {
034
035    private static final long serialVersionUID = 1L;
036
037    /**
038     * If {@code name} is null, then the environment is global.<br>
039     * Else the environment can be associated with a command ("command name") or with a tool ("command line").
040     *
041     * @since 7.4
042     */
043    @XNode("@name")
044    protected String name;
045
046    @XNode("workingDirectory")
047    protected String workingDirectory;
048
049    @XNodeMap(value = "parameters/parameter", key = "@name", type = HashMap.class, componentType = String.class)
050    private Map<String, String> parameters = new HashMap<>();
051
052    public String getWorkingDirectory() {
053        if (workingDirectory == null) {
054            workingDirectory = Environment.getDefault().getTemp().getPath();
055        }
056        if (!workingDirectory.endsWith("/")) {
057            workingDirectory += "/";
058        }
059        return workingDirectory;
060    }
061
062    public EnvironmentDescriptor merge(EnvironmentDescriptor other) {
063        if (other != null) {
064            if (other.workingDirectory != null) {
065                workingDirectory = other.workingDirectory;
066            }
067            getParameters().putAll(other.getParameters());
068        }
069        return this;
070    }
071
072    /**
073     * @since 7.4
074     */
075    public Map<String, String> getParameters() {
076        return parameters;
077    }
078
079    /**
080     * @since 7.4
081     */
082    public String getName() {
083        return name;
084    }
085
086}