001/* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2012 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar 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 * Sonar 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 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020package org.sonar.api.config; 021 022import com.google.common.base.Strings; 023import com.google.common.collect.ArrayListMultimap; 024import com.google.common.collect.Maps; 025import com.google.common.collect.Multimap; 026import org.apache.commons.lang.StringUtils; 027import org.sonar.api.BatchComponent; 028import org.sonar.api.Properties; 029import org.sonar.api.Property; 030import org.sonar.api.ServerComponent; 031import org.sonar.api.utils.AnnotationUtils; 032 033import java.util.Arrays; 034import java.util.Collection; 035import java.util.Map; 036 037/** 038 * Metadata of all the properties declared by plugins 039 * 040 * @since 2.12 041 */ 042public final class PropertyDefinitions implements BatchComponent, ServerComponent { 043 044 private final Map<String, PropertyDefinition> definitions = Maps.newHashMap(); 045 private final Map<String, String> categories = Maps.newHashMap(); 046 047 // deprecated key -> new key 048 private final Map<String, String> deprecatedKeys = Maps.newHashMap(); 049 050 public PropertyDefinitions(Object... components) { 051 if (components != null) { 052 addComponents(Arrays.asList(components)); 053 } 054 } 055 056 public PropertyDefinitions addComponents(Collection components) { 057 return addComponents(components, ""); 058 } 059 060 public PropertyDefinitions addComponents(Collection components, String defaultCategory) { 061 for (Object component : components) { 062 addComponent(component, defaultCategory); 063 } 064 return this; 065 } 066 067 public PropertyDefinitions addComponent(Object object) { 068 return addComponent(object, ""); 069 } 070 071 public PropertyDefinitions addComponent(Object component, String defaultCategory) { 072 Properties annotations = AnnotationUtils.getAnnotation(component, Properties.class); 073 if (annotations != null) { 074 for (Property property : annotations.value()) { 075 addProperty(property, defaultCategory); 076 } 077 } 078 Property annotation = AnnotationUtils.getAnnotation(component, Property.class); 079 if (annotation != null) { 080 addProperty(annotation, defaultCategory); 081 } 082 return this; 083 } 084 085 private PropertyDefinitions addProperty(Property property, String defaultCategory) { 086 PropertyDefinition definition = PropertyDefinition.create(property); 087 return add(definition, defaultCategory); 088 } 089 090 private PropertyDefinitions add(PropertyDefinition definition, String defaultCategory) { 091 if (!definitions.containsKey(definition.getKey())) { 092 definitions.put(definition.getKey(), definition); 093 categories.put(definition.getKey(), StringUtils.defaultIfBlank(definition.getCategory(), defaultCategory)); 094 if (!Strings.isNullOrEmpty(definition.getDeprecatedKey()) && !definition.getDeprecatedKey().equals(definition.getKey())) { 095 deprecatedKeys.put(definition.getDeprecatedKey(), definition.getKey()); 096 } 097 } 098 return this; 099 } 100 101 public PropertyDefinition get(String key) { 102 return definitions.get(validKey(key)); 103 } 104 105 public Collection<PropertyDefinition> getAll() { 106 return definitions.values(); 107 } 108 109 public String validKey(String key) { 110 return StringUtils.defaultString(deprecatedKeys.get(key), key); 111 } 112 113 static enum PropertyDefinitionFilter { 114 GLOBAL { 115 @Override 116 boolean accept(PropertyDefinition propertyDefinition) { 117 return propertyDefinition.isGlobal(); 118 } 119 }, 120 PROJECT { 121 @Override 122 boolean accept(PropertyDefinition propertyDefinition) { 123 return propertyDefinition.isOnProject(); 124 } 125 }, 126 MODULE { 127 @Override 128 boolean accept(PropertyDefinition propertyDefinition) { 129 return propertyDefinition.isOnModule(); 130 } 131 }; 132 133 abstract boolean accept(PropertyDefinition propertyDefinition); 134 } 135 136 private Map<String, Collection<PropertyDefinition>> getPropertiesByCategory(PropertyDefinitionFilter filter) { 137 Multimap<String, PropertyDefinition> byCategory = ArrayListMultimap.create(); 138 139 for (PropertyDefinition definition : getAll()) { 140 if (filter.accept(definition)) { 141 byCategory.put(getCategory(definition.getKey()), definition); 142 } 143 } 144 145 return byCategory.asMap(); 146 } 147 148 /** 149 * since 3.3 150 */ 151 public Map<String, Collection<PropertyDefinition>> getGlobalPropertiesByCategory() { 152 return getPropertiesByCategory(PropertyDefinitionFilter.GLOBAL); 153 } 154 155 /** 156 * since 3.3 157 */ 158 public Map<String, Collection<PropertyDefinition>> getProjectPropertiesByCategory() { 159 return getPropertiesByCategory(PropertyDefinitionFilter.PROJECT); 160 } 161 162 /** 163 * since 3.3 164 */ 165 public Map<String, Collection<PropertyDefinition>> getModulePropertiesByCategory() { 166 return getPropertiesByCategory(PropertyDefinitionFilter.MODULE); 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.getDefaultValue(), null); 175 } 176 177 public String getCategory(String key) { 178 return categories.get(validKey(key)); 179 } 180 181 public String getCategory(Property prop) { 182 return getCategory(prop.key()); 183 } 184 185 public String getNewKey(String deprecatedKey) { 186 return deprecatedKeys.get(deprecatedKey); 187 } 188 189 public String getDeprecatedKey(String key) { 190 PropertyDefinition def = get(key); 191 if (def == null) { 192 return null; 193 } 194 return StringUtils.defaultIfEmpty(def.getDeprecatedKey(), null); 195 } 196}