001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info 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 java.util.ArrayList;
023import java.util.Arrays;
024import java.util.Collection;
025import java.util.HashMap;
026import java.util.Map;
027import javax.annotation.CheckForNull;
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.ScannerSide;
034import org.sonar.api.ce.ComputeEngineSide;
035import org.sonar.api.server.ServerSide;
036import org.sonar.api.utils.AnnotationUtils;
037
038import static java.util.Objects.requireNonNull;
039
040/**
041 * Metadata of all the properties declared by plugins
042 *
043 * @since 2.12
044 */
045@ScannerSide
046@ServerSide
047@ComputeEngineSide
048public final class PropertyDefinitions {
049
050  private final Map<String, PropertyDefinition> definitions = new HashMap<>();
051  private final Map<String, Category> categories = new HashMap<>();
052  private final Map<String, SubCategory> subcategories = new HashMap<>();
053
054  // deprecated key -> new key
055  private final Map<String, String> deprecatedKeys = new HashMap<>();
056
057  public PropertyDefinitions(Object... components) {
058    addComponents(Arrays.asList(components));
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 (!StringUtils.isEmpty(definition.deprecatedKey()) && !definition.deprecatedKey().equals(definition.key())) {
116        deprecatedKeys.put(definition.deprecatedKey(), definition.key());
117      }
118    }
119    return this;
120  }
121
122  @CheckForNull
123  public PropertyDefinition get(String key) {
124    return definitions.get(validKey(key));
125  }
126
127  public Collection<PropertyDefinition> getAll() {
128    return definitions.values();
129  }
130
131  public String validKey(String key) {
132    requireNonNull(key, "key can't be null");
133    String trimmedKey = key.trim();
134    return StringUtils.defaultString(deprecatedKeys.get(trimmedKey), trimmedKey);
135  }
136
137  /**
138   * @since 3.7
139   */
140  public Map<Category, Map<SubCategory, Collection<PropertyDefinition>>> propertiesByCategory(@Nullable String qualifier) {
141    Map<Category, Map<SubCategory, Collection<PropertyDefinition>>> byCategory = new HashMap<>();
142    if (qualifier == null) {
143      // Special categories on global page
144      Map<SubCategory, Collection<PropertyDefinition>> emailSubCategories = new HashMap<>();
145      emailSubCategories.put(new SubCategory("email", true), new ArrayList<PropertyDefinition>());
146      byCategory.put(new Category(CoreProperties.CATEGORY_GENERAL, false), emailSubCategories);
147
148      HashMap<SubCategory, Collection<PropertyDefinition>> licenseSubCategories = new HashMap<>();
149      licenseSubCategories.put(new SubCategory("server_id", true), new ArrayList<PropertyDefinition>());
150      byCategory.put(new Category(CoreProperties.CATEGORY_LICENSES, false), licenseSubCategories);
151
152      HashMap<SubCategory, Collection<PropertyDefinition>> encryptionSubCategories = new HashMap<>();
153      encryptionSubCategories.put(new SubCategory("encryption", true), new ArrayList<PropertyDefinition>());
154      byCategory.put(new Category(CoreProperties.CATEGORY_SECURITY, false), encryptionSubCategories);
155    }
156    for (PropertyDefinition definition : getAll()) {
157      if (qualifier == null ? definition.global() : definition.qualifiers().contains(qualifier)) {
158        Category category = categories.get(definition.key());
159        if (!byCategory.containsKey(category)) {
160          byCategory.put(category, new HashMap<SubCategory, Collection<PropertyDefinition>>());
161        }
162        SubCategory subCategory = subcategories.get(definition.key());
163        if (!byCategory.get(category).containsKey(subCategory)) {
164          byCategory.get(category).put(subCategory, new ArrayList<PropertyDefinition>());
165        }
166        byCategory.get(category).get(subCategory).add(definition);
167      }
168    }
169    return byCategory;
170  }
171
172  @CheckForNull
173  public String getDefaultValue(String key) {
174    PropertyDefinition def = get(key);
175    if (def == null) {
176      return null;
177    }
178    return StringUtils.defaultIfEmpty(def.defaultValue(), null);
179  }
180
181  public String getCategory(String key) {
182    return categories.get(validKey(key)).toString();
183  }
184
185  public String getSubCategory(String key) {
186    return subcategories.get(validKey(key)).toString();
187  }
188
189  public String getCategory(Property prop) {
190    return getCategory(prop.key());
191  }
192
193  public String getNewKey(String deprecatedKey) {
194    return deprecatedKeys.get(deprecatedKey);
195  }
196
197  public String getDeprecatedKey(String key) {
198    PropertyDefinition def = get(key);
199    if (def == null) {
200      return null;
201    }
202    return StringUtils.defaultIfEmpty(def.deprecatedKey(), null);
203  }
204}