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 com.google.common.base.Strings; 023import com.google.common.collect.Maps; 024import org.apache.commons.lang.StringUtils; 025import org.sonar.api.BatchComponent; 026import org.sonar.api.Properties; 027import org.sonar.api.Property; 028import org.sonar.api.ServerComponent; 029import org.sonar.api.utils.AnnotationUtils; 030 031import javax.annotation.Nullable; 032 033import java.util.ArrayList; 034import java.util.Arrays; 035import java.util.Collection; 036import java.util.HashMap; 037import java.util.Map; 038 039/** 040 * Metadata of all the properties declared by plugins 041 * 042 * @since 2.12 043 */ 044public final class PropertyDefinitions implements BatchComponent, ServerComponent { 045 046 private final Map<String, PropertyDefinition> definitions = Maps.newHashMap(); 047 private final Map<String, String> categories = Maps.newHashMap(); 048 private final Map<String, String> subcategories = Maps.newHashMap(); 049 050 // deprecated key -> new key 051 private final Map<String, String> deprecatedKeys = Maps.newHashMap(); 052 053 public PropertyDefinitions(Object... components) { 054 if (components != null) { 055 addComponents(Arrays.asList(components)); 056 } 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(), category); 111 subcategories.put(definition.key(), StringUtils.defaultIfBlank(definition.subCategory(), category)); 112 if (!Strings.isNullOrEmpty(definition.deprecatedKey()) && !definition.deprecatedKey().equals(definition.key())) { 113 deprecatedKeys.put(definition.deprecatedKey(), definition.key()); 114 } 115 } 116 return this; 117 } 118 119 public PropertyDefinition get(String key) { 120 return definitions.get(validKey(key)); 121 } 122 123 public Collection<PropertyDefinition> getAll() { 124 return definitions.values(); 125 } 126 127 public String validKey(String key) { 128 return StringUtils.defaultString(deprecatedKeys.get(key), key); 129 } 130 131 /** 132 * @since 3.6 133 */ 134 public Map<String, Map<String, Collection<PropertyDefinition>>> getPropertiesByCategory(@Nullable String qualifier) { 135 Map<String, Map<String, Collection<PropertyDefinition>>> byCategory = new HashMap<String, Map<String, Collection<PropertyDefinition>>>(); 136 137 for (PropertyDefinition definition : getAll()) { 138 if (qualifier == null ? definition.global() : definition.qualifiers().contains(qualifier)) { 139 String category = getCategory(definition.key()); 140 if (!byCategory.containsKey(category)) { 141 byCategory.put(category, new HashMap<String, Collection<PropertyDefinition>>()); 142 } 143 String subCategory = getSubCategory(definition.key()); 144 if (!byCategory.get(category).containsKey(subCategory)) { 145 byCategory.get(category).put(subCategory, new ArrayList<PropertyDefinition>()); 146 } 147 byCategory.get(category).get(subCategory).add(definition); 148 } 149 } 150 151 return byCategory; 152 } 153 154 public Map<String, Map<String, Collection<PropertyDefinition>>> getPropertiesByCategory() { 155 return getPropertiesByCategory(null); 156 } 157 158 public String getDefaultValue(String key) { 159 PropertyDefinition def = get(key); 160 if (def == null) { 161 return null; 162 } 163 return StringUtils.defaultIfEmpty(def.defaultValue(), null); 164 } 165 166 public String getCategory(String key) { 167 return categories.get(validKey(key)); 168 } 169 170 public String getSubCategory(String key) { 171 return subcategories.get(validKey(key)); 172 } 173 174 public String getCategory(Property prop) { 175 return getCategory(prop.key()); 176 } 177 178 public String getNewKey(String deprecatedKey) { 179 return deprecatedKeys.get(deprecatedKey); 180 } 181 182 public String getDeprecatedKey(String key) { 183 PropertyDefinition def = get(key); 184 if (def == null) { 185 return null; 186 } 187 return StringUtils.defaultIfEmpty(def.deprecatedKey(), null); 188 } 189}