001/*
002 * SonarQube
003 * Copyright (C) 2009-2016 SonarSource SA
004 * mailto:contact AT sonarsource DOT com
005 *
006 * This program is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.api.config;
021
022import com.google.common.base.Strings;
023import java.util.ArrayList;
024import java.util.Arrays;
025import java.util.Collection;
026import java.util.HashMap;
027import java.util.Map;
028import javax.annotation.Nullable;
029import org.apache.commons.lang.StringUtils;
030import org.sonar.api.CoreProperties;
031import org.sonar.api.Properties;
032import org.sonar.api.Property;
033import org.sonar.api.batch.BatchSide;
034import org.sonar.api.ce.ComputeEngineSide;
035import org.sonar.api.server.ServerSide;
036import org.sonar.api.utils.AnnotationUtils;
037
038/**
039 * Metadata of all the properties declared by plugins
040 *
041 * @since 2.12
042 */
043@BatchSide
044@ServerSide
045@ComputeEngineSide
046public final class PropertyDefinitions {
047
048  private final Map<String, PropertyDefinition> definitions = new HashMap<>();
049  private final Map<String, Category> categories = new HashMap<>();
050  private final Map<String, SubCategory> subcategories = new HashMap<>();
051
052  // deprecated key -> new key
053  private final Map<String, String> deprecatedKeys = new HashMap<>();
054
055  public PropertyDefinitions(Object... components) {
056    if (components != null) {
057      addComponents(Arrays.asList(components));
058    }
059  }
060
061  public PropertyDefinitions(Collection<PropertyDefinition> components) {
062    addComponents(components);
063  }
064
065  public PropertyDefinitions addComponents(Collection components) {
066    return addComponents(components, "");
067  }
068
069  public PropertyDefinitions addComponents(Collection components, String defaultCategory) {
070    for (Object component : components) {
071      addComponent(component, defaultCategory);
072    }
073    return this;
074  }
075
076  public PropertyDefinitions addComponent(Object object) {
077    return addComponent(object, "");
078  }
079
080  public PropertyDefinitions addComponent(Object component, String defaultCategory) {
081    addComponentFromAnnotationProperty(component, defaultCategory);
082    if (component instanceof PropertyDefinition) {
083      PropertyDefinition propertyDefinition = (PropertyDefinition) component;
084      add(propertyDefinition, defaultCategory);
085    }
086    return this;
087  }
088
089  private PropertyDefinitions addComponentFromAnnotationProperty(Object component, String defaultCategory) {
090    Properties annotations = AnnotationUtils.getAnnotation(component, Properties.class);
091    if (annotations != null) {
092      for (Property property : annotations.value()) {
093        addProperty(property, defaultCategory);
094      }
095    }
096    Property annotation = AnnotationUtils.getAnnotation(component, Property.class);
097    if (annotation != null) {
098      addProperty(annotation, defaultCategory);
099    }
100    return this;
101  }
102
103  private PropertyDefinitions addProperty(Property property, String defaultCategory) {
104    PropertyDefinition definition = PropertyDefinition.create(property);
105    return add(definition, defaultCategory);
106  }
107
108  private PropertyDefinitions add(PropertyDefinition definition, String defaultCategory) {
109    if (!definitions.containsKey(definition.key())) {
110      definitions.put(definition.key(), definition);
111      String category = StringUtils.defaultIfBlank(definition.category(), defaultCategory);
112      categories.put(definition.key(), new Category(category));
113      String subcategory = StringUtils.defaultIfBlank(definition.subCategory(), category);
114      subcategories.put(definition.key(), new SubCategory(subcategory));
115      if (!Strings.isNullOrEmpty(definition.deprecatedKey()) && !definition.deprecatedKey().equals(definition.key())) {
116        deprecatedKeys.put(definition.deprecatedKey(), definition.key());
117      }
118    }
119    return this;
120  }
121
122  public PropertyDefinition get(String key) {
123    return definitions.get(validKey(key));
124  }
125
126  public Collection<PropertyDefinition> getAll() {
127    return definitions.values();
128  }
129
130  public String validKey(String key) {
131    return StringUtils.defaultString(deprecatedKeys.get(key), key);
132  }
133
134  /**
135   * @since 3.7
136   */
137  public Map<Category, Map<SubCategory, Collection<PropertyDefinition>>> propertiesByCategory(@Nullable String qualifier) {
138    Map<Category, Map<SubCategory, Collection<PropertyDefinition>>> byCategory = new HashMap<>();
139    if (qualifier == null) {
140      // Special categories on global page
141      Map<SubCategory, Collection<PropertyDefinition>> emailSubCategories = new HashMap<>();
142      emailSubCategories.put(new SubCategory("email", true), new ArrayList<PropertyDefinition>());
143      byCategory.put(new Category(CoreProperties.CATEGORY_GENERAL, false), emailSubCategories);
144
145      HashMap<SubCategory, Collection<PropertyDefinition>> licenseSubCategories = new HashMap<>();
146      licenseSubCategories.put(new SubCategory("server_id", true), new ArrayList<PropertyDefinition>());
147      byCategory.put(new Category(CoreProperties.CATEGORY_LICENSES, false), licenseSubCategories);
148
149      HashMap<SubCategory, Collection<PropertyDefinition>> encryptionSubCategories = new HashMap<>();
150      encryptionSubCategories.put(new SubCategory("encryption", true), new ArrayList<PropertyDefinition>());
151      byCategory.put(new Category(CoreProperties.CATEGORY_SECURITY, false), encryptionSubCategories);
152    }
153    for (PropertyDefinition definition : getAll()) {
154      if (qualifier == null ? definition.global() : definition.qualifiers().contains(qualifier)) {
155        Category category = categories.get(definition.key());
156        if (!byCategory.containsKey(category)) {
157          byCategory.put(category, new HashMap<SubCategory, Collection<PropertyDefinition>>());
158        }
159        SubCategory subCategory = subcategories.get(definition.key());
160        if (!byCategory.get(category).containsKey(subCategory)) {
161          byCategory.get(category).put(subCategory, new ArrayList<PropertyDefinition>());
162        }
163        byCategory.get(category).get(subCategory).add(definition);
164      }
165    }
166    return byCategory;
167  }
168
169  public String getDefaultValue(String key) {
170    PropertyDefinition def = get(key);
171    if (def == null) {
172      return null;
173    }
174    return StringUtils.defaultIfEmpty(def.defaultValue(), null);
175  }
176
177  public String getCategory(String key) {
178    return categories.get(validKey(key)).toString();
179  }
180
181  public String getSubCategory(String key) {
182    return subcategories.get(validKey(key)).toString();
183  }
184
185  public String getCategory(Property prop) {
186    return getCategory(prop.key());
187  }
188
189  public String getNewKey(String deprecatedKey) {
190    return deprecatedKeys.get(deprecatedKey);
191  }
192
193  public String getDeprecatedKey(String key) {
194    PropertyDefinition def = get(key);
195    if (def == null) {
196      return null;
197    }
198    return StringUtils.defaultIfEmpty(def.deprecatedKey(), null);
199  }
200}