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.util.HashMap;
024import java.util.Map;
025
026import org.nuxeo.common.Environment;
027import org.nuxeo.common.xmap.annotation.XNode;
028import org.nuxeo.common.xmap.annotation.XNodeMap;
029import org.nuxeo.common.xmap.annotation.XObject;
030
031@XObject(value = "environment")
032public class EnvironmentDescriptor {
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 = Environment.getDefault().getTemp().getPath();
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}