001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2013 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube 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 * SonarQube 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 org.sonar.api.config.internal.SubCategory;
023
024import org.sonar.api.config.internal.Category;
025import com.google.common.base.Strings;
026import com.google.common.collect.Maps;
027import org.apache.commons.lang.StringUtils;
028import org.sonar.api.BatchComponent;
029import org.sonar.api.CoreProperties;
030import org.sonar.api.Properties;
031import org.sonar.api.Property;
032import org.sonar.api.ServerComponent;
033import org.sonar.api.utils.AnnotationUtils;
034
035import javax.annotation.Nullable;
036
037import java.util.ArrayList;
038import java.util.Arrays;
039import java.util.Collection;
040import java.util.HashMap;
041import java.util.Map;
042
043/**
044 * Metadata of all the properties declared by plugins
045 *
046 * @since 2.12
047 */
048public final class PropertyDefinitions implements BatchComponent, ServerComponent {
049
050  private final Map<String, PropertyDefinition> definitions = Maps.newHashMap();
051  private final Map<String, Category> categories = Maps.newHashMap();
052  private final Map<String, SubCategory> subcategories = Maps.newHashMap();
053
054  // deprecated key -> new key
055  private final Map<String, String> deprecatedKeys = Maps.newHashMap();
056
057  public PropertyDefinitions(Object... components) {
058    if (components != null) {
059      addComponents(Arrays.asList(components));
060    }
061  }
062
063  public PropertyDefinitions(Collection<PropertyDefinition> components) {
064    addComponents(components);
065  }
066
067  public PropertyDefinitions addComponents(Collection components) {
068    return addComponents(components, "");
069  }
070
071  public PropertyDefinitions addComponents(Collection components, String defaultCategory) {
072    for (Object component : components) {
073      addComponent(component, defaultCategory);
074    }
075    return this;
076  }
077
078  public PropertyDefinitions addComponent(Object object) {
079    return addComponent(object, "");
080  }
081
082  public PropertyDefinitions addComponent(Object component, String defaultCategory) {
083    addComponentFromAnnotationProperty(component, defaultCategory);
084    if (component instanceof PropertyDefinition) {
085      PropertyDefinition propertyDefinition = (PropertyDefinition) component;
086      add(propertyDefinition, defaultCategory);
087    }
088    return this;
089  }
090
091  private PropertyDefinitions addComponentFromAnnotationProperty(Object component, String defaultCategory) {
092    Properties annotations = AnnotationUtils.getAnnotation(component, Properties.class);
093    if (annotations != null) {
094      for (Property property : annotations.value()) {
095        addProperty(property, defaultCategory);
096      }
097    }
098    Property annotation = AnnotationUtils.getAnnotation(component, Property.class);
099    if (annotation != null) {
100      addProperty(annotation, defaultCategory);
101    }
102    return this;
103  }
104
105  private PropertyDefinitions addProperty(Property property, String defaultCategory) {
106    PropertyDefinition definition = PropertyDefinition.create(property);
107    return add(definition, defaultCategory);
108  }
109
110  private PropertyDefinitions add(PropertyDefinition definition, String defaultCategory) {
111    if (!definitions.containsKey(definition.key())) {
112      definitions.put(definition.key(), definition);
113      String category = StringUtils.defaultIfBlank(definition.category(), defaultCategory);
114      categories.put(definition.key(), new Category(category));
115      String subcategory = StringUtils.defaultIfBlank(definition.subCategory(), category);
116      subcategories.put(definition.key(), new SubCategory(subcategory));
117      if (!Strings.isNullOrEmpty(definition.deprecatedKey()) && !definition.deprecatedKey().equals(definition.key())) {
118        deprecatedKeys.put(definition.deprecatedKey(), definition.key());
119      }
120    }
121    return this;
122  }
123
124  public PropertyDefinition get(String key) {
125    return definitions.get(validKey(key));
126  }
127
128  public Collection<PropertyDefinition> getAll() {
129    return definitions.values();
130  }
131
132  public String validKey(String key) {
133    return StringUtils.defaultString(deprecatedKeys.get(key), key);
134  }
135
136  /**
137   * @since 3.6
138   * @deprecated since 3.7 use {@link #propertiesByCategory(String)}
139   */
140  @Deprecated
141  public Map<String, Map<String, Collection<PropertyDefinition>>> getPropertiesByCategory(@Nullable String qualifier) {
142    Map<String, Map<String, Collection<PropertyDefinition>>> byCategory = new HashMap<String, Map<String, Collection<PropertyDefinition>>>();
143
144    for (PropertyDefinition definition : getAll()) {
145      if (qualifier == null ? definition.global() : definition.qualifiers().contains(qualifier)) {
146        String category = getCategory(definition.key());
147        if (!byCategory.containsKey(category)) {
148          byCategory.put(category, new HashMap<String, Collection<PropertyDefinition>>());
149        }
150        String subCategory = getSubCategory(definition.key());
151        if (!byCategory.get(category).containsKey(subCategory)) {
152          byCategory.get(category).put(subCategory, new ArrayList<PropertyDefinition>());
153        }
154        byCategory.get(category).get(subCategory).add(definition);
155      }
156    }
157
158    return byCategory;
159  }
160
161  /**
162   * @since 3.6
163   * @deprecated since 3.7 use {@link #propertiesByCategory(String)}
164   */
165  @Deprecated
166  public Map<String, Map<String, Collection<PropertyDefinition>>> getPropertiesByCategory() {
167    return getPropertiesByCategory(null);
168  }
169
170  /**
171   * @since 3.7
172   */
173  public Map<Category, Map<SubCategory, Collection<PropertyDefinition>>> propertiesByCategory(@Nullable String qualifier) {
174    Map<Category, Map<SubCategory, Collection<PropertyDefinition>>> byCategory = new HashMap<Category, Map<SubCategory, Collection<PropertyDefinition>>>();
175    if (qualifier == null) {
176      // Special categories on global page
177      byCategory.put(new Category("email", true), new HashMap<SubCategory, Collection<PropertyDefinition>>());
178      byCategory.put(new Category("encryption", true), new HashMap<SubCategory, Collection<PropertyDefinition>>());
179      HashMap<SubCategory, Collection<PropertyDefinition>> licenseSubCategories = new HashMap<SubCategory, Collection<PropertyDefinition>>();
180      licenseSubCategories.put(new SubCategory("server_id", true), new ArrayList<PropertyDefinition>());
181      byCategory.put(new Category(CoreProperties.CATEGORY_LICENSES, false), licenseSubCategories);
182    }
183    for (PropertyDefinition definition : getAll()) {
184      if (qualifier == null ? definition.global() : definition.qualifiers().contains(qualifier)) {
185        Category category = categories.get(definition.key());
186        if (!byCategory.containsKey(category)) {
187          byCategory.put(category, new HashMap<SubCategory, Collection<PropertyDefinition>>());
188        }
189        SubCategory subCategory = subcategories.get(definition.key());
190        if (!byCategory.get(category).containsKey(subCategory)) {
191          byCategory.get(category).put(subCategory, new ArrayList<PropertyDefinition>());
192        }
193        byCategory.get(category).get(subCategory).add(definition);
194      }
195    }
196
197    return byCategory;
198  }
199
200  public String getDefaultValue(String key) {
201    PropertyDefinition def = get(key);
202    if (def == null) {
203      return null;
204    }
205    return StringUtils.defaultIfEmpty(def.defaultValue(), null);
206  }
207
208  public String getCategory(String key) {
209    return categories.get(validKey(key)).toString();
210  }
211
212  public String getSubCategory(String key) {
213    return subcategories.get(validKey(key)).toString();
214  }
215
216  public String getCategory(Property prop) {
217    return getCategory(prop.key());
218  }
219
220  public String getNewKey(String deprecatedKey) {
221    return deprecatedKeys.get(deprecatedKey);
222  }
223
224  public String getDeprecatedKey(String key) {
225    PropertyDefinition def = get(key);
226    if (def == null) {
227      return null;
228    }
229    return StringUtils.defaultIfEmpty(def.deprecatedKey(), null);
230  }
231}