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