001/*
002 * (C) Copyright 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 *      Andre Justo
018 *      Anahide Tchertchian
019 */
020package org.nuxeo.runtime.services.config;
021
022import org.apache.commons.lang3.StringUtils;
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.common.xmap.annotation.XNode;
026import org.nuxeo.common.xmap.annotation.XObject;
027import org.nuxeo.runtime.model.Descriptor;
028
029/**
030 * Descriptor for JSF configuration contributions.
031 *
032 * @since 7.4
033 */
034@XObject("property")
035public class ConfigurationPropertyDescriptor implements Descriptor {
036
037    protected static final Log log = LogFactory.getLog(ConfigurationPropertyDescriptor.class);
038
039    @XNode("@name")
040    protected String name;
041
042    @XNode("@list")
043    public boolean list;
044
045    @XNode("@override")
046    public boolean override;
047
048    @XNode
049    protected String value;
050
051    @Override
052    public ConfigurationPropertyDescriptor clone() {
053        ConfigurationPropertyDescriptor clone = new ConfigurationPropertyDescriptor();
054        clone.name = name;
055        clone.value = value;
056        clone.list = list;
057        clone.override = override;
058        return clone;
059    }
060
061    /**
062     * @since 10.3
063     */
064    @Override
065    public String getId() {
066        return getName();
067    }
068
069    public String getName() {
070        return name;
071    }
072
073    public String getValue() {
074        return value;
075    }
076
077    /**
078     * @since 10.3
079     */
080    public boolean isList() {
081        return list;
082    }
083
084    /**
085     * @since 10.3
086     */
087    public boolean isOverride() {
088        return override;
089    }
090
091    /**
092     * @since 10.3
093     */
094    @Override
095    public Descriptor merge(Descriptor o) {
096        ConfigurationPropertyDescriptor other = (ConfigurationPropertyDescriptor) o;
097        if (this.list) {
098            ConfigurationPropertyDescriptor merged = new ConfigurationPropertyDescriptor();
099            merged.list = this.list;
100            merged.name = this.name;
101            if (StringUtils.isNotEmpty(value) && !other.override) {
102                merged.value = value + ConfigurationService.LIST_SEPARATOR + other.value;
103            } else {
104                merged.value = other.value;
105            }
106            if (log.isDebugEnabled()) {
107                log.debug(String.format("Merging property %s with old %s resulting in %s", other, this, merged));
108            }
109            return merged;
110        } else {
111            if (log.isDebugEnabled()) {
112                log.debug(String.format("Overriding existing property %s with %s", this, other));
113            }
114            if (other.list) {
115                other.list = false;
116                log.warn(String.format(
117                        "Property %s cannot be marked as list because it is already defined as not list. Overriding existing property.",
118                        other.getName()));
119            }
120            return other;
121        }
122    }
123
124    /**
125     * @since 10.3
126     */
127    @Override
128    public String toString() {
129        return String.format("name=%s, value=%s, list=%s, replace=%s", name, value, list, override);
130    }
131
132}