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 com.google.common.base.Strings; 023import java.util.ArrayList; 024import java.util.Arrays; 025import java.util.Collection; 026import java.util.HashMap; 027import java.util.Map; 028import javax.annotation.CheckForNull; 029import javax.annotation.Nullable; 030import org.apache.commons.lang.StringUtils; 031import org.sonar.api.CoreProperties; 032import org.sonar.api.Properties; 033import org.sonar.api.Property; 034import org.sonar.api.batch.ScannerSide; 035import org.sonar.api.ce.ComputeEngineSide; 036import org.sonar.api.server.ServerSide; 037import org.sonar.api.utils.AnnotationUtils; 038 039/** 040 * Metadata of all the properties declared by plugins 041 * 042 * @since 2.12 043 */ 044@ScannerSide 045@ServerSide 046@ComputeEngineSide 047public final class PropertyDefinitions { 048 049 private final Map<String, PropertyDefinition> definitions = new HashMap<>(); 050 private final Map<String, Category> categories = new HashMap<>(); 051 private final Map<String, SubCategory> subcategories = new HashMap<>(); 052 053 // deprecated key -> new key 054 private final Map<String, String> deprecatedKeys = new HashMap<>(); 055 056 public PropertyDefinitions(Object... components) { 057 addComponents(Arrays.asList(components)); 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 @CheckForNull 122 public PropertyDefinition get(String key) { 123 return definitions.get(validKey(key)); 124 } 125 126 public Collection<PropertyDefinition> getAll() { 127 return definitions.values(); 128 } 129 130 public String validKey(String key) { 131 return StringUtils.defaultString(deprecatedKeys.get(key), key); 132 } 133 134 /** 135 * @since 3.7 136 */ 137 public Map<Category, Map<SubCategory, Collection<PropertyDefinition>>> propertiesByCategory(@Nullable String qualifier) { 138 Map<Category, Map<SubCategory, Collection<PropertyDefinition>>> byCategory = new HashMap<>(); 139 if (qualifier == null) { 140 // Special categories on global page 141 Map<SubCategory, Collection<PropertyDefinition>> emailSubCategories = new HashMap<>(); 142 emailSubCategories.put(new SubCategory("email", true), new ArrayList<PropertyDefinition>()); 143 byCategory.put(new Category(CoreProperties.CATEGORY_GENERAL, false), emailSubCategories); 144 145 HashMap<SubCategory, Collection<PropertyDefinition>> licenseSubCategories = new HashMap<>(); 146 licenseSubCategories.put(new SubCategory("server_id", true), new ArrayList<PropertyDefinition>()); 147 byCategory.put(new Category(CoreProperties.CATEGORY_LICENSES, false), licenseSubCategories); 148 149 HashMap<SubCategory, Collection<PropertyDefinition>> encryptionSubCategories = new HashMap<>(); 150 encryptionSubCategories.put(new SubCategory("encryption", true), new ArrayList<PropertyDefinition>()); 151 byCategory.put(new Category(CoreProperties.CATEGORY_SECURITY, false), encryptionSubCategories); 152 } 153 for (PropertyDefinition definition : getAll()) { 154 if (qualifier == null ? definition.global() : definition.qualifiers().contains(qualifier)) { 155 Category category = categories.get(definition.key()); 156 if (!byCategory.containsKey(category)) { 157 byCategory.put(category, new HashMap<SubCategory, Collection<PropertyDefinition>>()); 158 } 159 SubCategory subCategory = subcategories.get(definition.key()); 160 if (!byCategory.get(category).containsKey(subCategory)) { 161 byCategory.get(category).put(subCategory, new ArrayList<PropertyDefinition>()); 162 } 163 byCategory.get(category).get(subCategory).add(definition); 164 } 165 } 166 return byCategory; 167 } 168 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}