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 038/** 039 * Metadata of all the properties declared by plugins 040 * 041 * @since 2.12 042 */ 043@ScannerSide 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 addComponents(Arrays.asList(components)); 057 } 058 059 public PropertyDefinitions(Collection<PropertyDefinition> components) { 060 addComponents(components); 061 } 062 063 public PropertyDefinitions addComponents(Collection components) { 064 return addComponents(components, ""); 065 } 066 067 public PropertyDefinitions addComponents(Collection components, String defaultCategory) { 068 for (Object component : components) { 069 addComponent(component, defaultCategory); 070 } 071 return this; 072 } 073 074 public PropertyDefinitions addComponent(Object object) { 075 return addComponent(object, ""); 076 } 077 078 public PropertyDefinitions addComponent(Object component, String defaultCategory) { 079 addComponentFromAnnotationProperty(component, defaultCategory); 080 if (component instanceof PropertyDefinition) { 081 PropertyDefinition propertyDefinition = (PropertyDefinition) component; 082 add(propertyDefinition, defaultCategory); 083 } 084 return this; 085 } 086 087 private PropertyDefinitions addComponentFromAnnotationProperty(Object component, String defaultCategory) { 088 Properties annotations = AnnotationUtils.getAnnotation(component, Properties.class); 089 if (annotations != null) { 090 for (Property property : annotations.value()) { 091 addProperty(property, defaultCategory); 092 } 093 } 094 Property annotation = AnnotationUtils.getAnnotation(component, Property.class); 095 if (annotation != null) { 096 addProperty(annotation, defaultCategory); 097 } 098 return this; 099 } 100 101 private PropertyDefinitions addProperty(Property property, String defaultCategory) { 102 PropertyDefinition definition = PropertyDefinition.create(property); 103 return add(definition, defaultCategory); 104 } 105 106 private PropertyDefinitions add(PropertyDefinition definition, String defaultCategory) { 107 if (!definitions.containsKey(definition.key())) { 108 definitions.put(definition.key(), definition); 109 String category = StringUtils.defaultIfBlank(definition.category(), defaultCategory); 110 categories.put(definition.key(), new Category(category)); 111 String subcategory = StringUtils.defaultIfBlank(definition.subCategory(), category); 112 subcategories.put(definition.key(), new SubCategory(subcategory)); 113 if (!StringUtils.isEmpty(definition.deprecatedKey()) && !definition.deprecatedKey().equals(definition.key())) { 114 deprecatedKeys.put(definition.deprecatedKey(), definition.key()); 115 } 116 } 117 return this; 118 } 119 120 @CheckForNull 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<>(); 138 if (qualifier == null) { 139 // Special categories on global page 140 Map<SubCategory, Collection<PropertyDefinition>> emailSubCategories = new HashMap<>(); 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<>(); 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<>(); 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 @CheckForNull 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}