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.6
135 * @deprecated since 3.7 use {@link #propertiesByCategory(String)}
136 */
137 @Deprecated
138 public Map<String, Map<String, Collection<PropertyDefinition>>> getPropertiesByCategory(@Nullable String qualifier) {
139 Map<String, Map<String, Collection<PropertyDefinition>>> byCategory = new HashMap<String, Map<String, Collection<PropertyDefinition>>>();
140
141 for (PropertyDefinition definition : getAll()) {
142 if (qualifier == null ? definition.global() : definition.qualifiers().contains(qualifier)) {
143 String category = getCategory(definition.key());
144 if (!byCategory.containsKey(category)) {
145 byCategory.put(category, new HashMap<String, Collection<PropertyDefinition>>());
146 }
147 String subCategory = getSubCategory(definition.key());
148 if (!byCategory.get(category).containsKey(subCategory)) {
149 byCategory.get(category).put(subCategory, new ArrayList<PropertyDefinition>());
150 }
151 byCategory.get(category).get(subCategory).add(definition);
152 }
153 }
154
155 return byCategory;
156 }
157
158 /**
159 * @since 3.6
160 * @deprecated since 3.7 use {@link #propertiesByCategory(String)}
161 */
162 @Deprecated
163 public Map<String, Map<String, Collection<PropertyDefinition>>> getPropertiesByCategory() {
164 return getPropertiesByCategory(null);
165 }
166
167 /**
168 * @since 3.7
169 */
170 public Map<Category, Map<SubCategory, Collection<PropertyDefinition>>> propertiesByCategory(@Nullable String qualifier) {
171 Map<Category, Map<SubCategory, Collection<PropertyDefinition>>> byCategory = new HashMap<Category, Map<SubCategory, Collection<PropertyDefinition>>>();
172 if (qualifier == null) {
173 // Special categories on global page
174 Map<SubCategory, Collection<PropertyDefinition>> emailSubCategories = new HashMap<SubCategory, Collection<PropertyDefinition>>();
175 emailSubCategories.put(new SubCategory("email", true), new ArrayList<PropertyDefinition>());
176 byCategory.put(new Category(CoreProperties.CATEGORY_GENERAL, false), emailSubCategories);
177
178 HashMap<SubCategory, Collection<PropertyDefinition>> licenseSubCategories = new HashMap<SubCategory, Collection<PropertyDefinition>>();
179 licenseSubCategories.put(new SubCategory("server_id", true), new ArrayList<PropertyDefinition>());
180 byCategory.put(new Category(CoreProperties.CATEGORY_LICENSES, false), licenseSubCategories);
181
182 HashMap<SubCategory, Collection<PropertyDefinition>> encryptionSubCategories = new HashMap<SubCategory, Collection<PropertyDefinition>>();
183 encryptionSubCategories.put(new SubCategory("encryption", true), new ArrayList<PropertyDefinition>());
184 byCategory.put(new Category(CoreProperties.CATEGORY_SECURITY, false), encryptionSubCategories);
185 }
186 for (PropertyDefinition definition : getAll()) {
187 if (qualifier == null ? definition.global() : definition.qualifiers().contains(qualifier)) {
188 Category category = categories.get(definition.key());
189 if (!byCategory.containsKey(category)) {
190 byCategory.put(category, new HashMap<SubCategory, Collection<PropertyDefinition>>());
191 }
192 SubCategory subCategory = subcategories.get(definition.key());
193 if (!byCategory.get(category).containsKey(subCategory)) {
194 byCategory.get(category).put(subCategory, new ArrayList<PropertyDefinition>());
195 }
196 byCategory.get(category).get(subCategory).add(definition);
197 }
198 }
199 return byCategory;
200 }
201
202 public String getDefaultValue(String key) {
203 PropertyDefinition def = get(key);
204 if (def == null) {
205 return null;
206 }
207 return StringUtils.defaultIfEmpty(def.defaultValue(), null);
208 }
209
210 public String getCategory(String key) {
211 return categories.get(validKey(key)).toString();
212 }
213
214 public String getSubCategory(String key) {
215 return subcategories.get(validKey(key)).toString();
216 }
217
218 public String getCategory(Property prop) {
219 return getCategory(prop.key());
220 }
221
222 public String getNewKey(String deprecatedKey) {
223 return deprecatedKeys.get(deprecatedKey);
224 }
225
226 public String getDeprecatedKey(String key) {
227 PropertyDefinition def = get(key);
228 if (def == null) {
229 return null;
230 }
231 return StringUtils.defaultIfEmpty(def.deprecatedKey(), null);
232 }
233 }