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.collect.ArrayListMultimap; 023import com.google.common.collect.Maps; 024import com.google.common.collect.Multimap; 025import org.apache.commons.lang.StringUtils; 026import org.sonar.api.BatchComponent; 027import org.sonar.api.Properties; 028import org.sonar.api.Property; 029import org.sonar.api.ServerComponent; 030import org.sonar.api.utils.AnnotationUtils; 031 032import java.util.Arrays; 033import java.util.Collection; 034import java.util.Map; 035 036/** 037 * Metadata of all the properties declared by plugins 038 * 039 * @since 2.12 040 */ 041public final class PropertyDefinitions implements BatchComponent, ServerComponent { 042 043 private final Map<String, PropertyDefinition> definitions = Maps.newHashMap(); 044 private final Map<String, String> categories = Maps.newHashMap(); 045 046 public PropertyDefinitions(Object... components) { 047 if (components != null) { 048 addComponents(Arrays.asList(components)); 049 } 050 } 051 052 public PropertyDefinitions addComponents(Collection components) { 053 return addComponents(components, ""); 054 } 055 056 public PropertyDefinitions addComponents(Collection components, String defaultCategory) { 057 for (Object component : components) { 058 addComponent(component, defaultCategory); 059 } 060 return this; 061 } 062 063 public PropertyDefinitions addComponent(Object object) { 064 return addComponent(object, ""); 065 } 066 067 public PropertyDefinitions addComponent(Object component, String defaultCategory) { 068 Properties annotations = AnnotationUtils.getAnnotation(component, Properties.class); 069 if (annotations != null) { 070 for (Property property : annotations.value()) { 071 addProperty(property, defaultCategory); 072 } 073 } 074 Property annotation = AnnotationUtils.getAnnotation(component, Property.class); 075 if (annotation != null) { 076 addProperty(annotation, defaultCategory); 077 } 078 return this; 079 } 080 081 private PropertyDefinitions addProperty(Property property, String defaultCategory) { 082 PropertyDefinition definition = PropertyDefinition.create(property); 083 return add(definition, defaultCategory); 084 } 085 086 private PropertyDefinitions add(PropertyDefinition definition, String defaultCategory) { 087 if (!definitions.containsKey(definition.getKey())) { 088 definitions.put(definition.getKey(), definition); 089 categories.put(definition.getKey(), StringUtils.defaultIfBlank(definition.getCategory(), defaultCategory)); 090 } 091 return this; 092 } 093 094 public PropertyDefinition get(String key) { 095 return definitions.get(key); 096 } 097 098 public Collection<PropertyDefinition> getAll() { 099 return definitions.values(); 100 } 101 102 static enum PropertyDefinitionFilter { 103 GLOBAL { 104 @Override 105 boolean accept(PropertyDefinition propertyDefinition) { 106 return propertyDefinition.isGlobal(); 107 } 108 }, 109 PROJECT { 110 @Override 111 boolean accept(PropertyDefinition propertyDefinition) { 112 return propertyDefinition.isOnProject(); 113 } 114 }, 115 MODULE { 116 @Override 117 boolean accept(PropertyDefinition propertyDefinition) { 118 return propertyDefinition.isOnModule(); 119 } 120 }; 121 122 abstract boolean accept(PropertyDefinition propertyDefinition); 123 } 124 125 private Map<String, Collection<PropertyDefinition>> getPropertiesByCategory(PropertyDefinitionFilter filter) { 126 Multimap<String, PropertyDefinition> byCategory = ArrayListMultimap.create(); 127 128 for (PropertyDefinition definition : getAll()) { 129 if (filter.accept(definition)) { 130 byCategory.put(getCategory(definition.getKey()), definition); 131 } 132 } 133 134 return byCategory.asMap(); 135 } 136 137 /** 138 * since 3.3 139 */ 140 public Map<String, Collection<PropertyDefinition>> getGlobalPropertiesByCategory() { 141 return getPropertiesByCategory(PropertyDefinitionFilter.GLOBAL); 142 } 143 144 /** 145 * since 3.3 146 */ 147 public Map<String, Collection<PropertyDefinition>> getProjectPropertiesByCategory() { 148 return getPropertiesByCategory(PropertyDefinitionFilter.PROJECT); 149 } 150 151 /** 152 * since 3.3 153 */ 154 public Map<String, Collection<PropertyDefinition>> getModulePropertiesByCategory() { 155 return getPropertiesByCategory(PropertyDefinitionFilter.MODULE); 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.getDefaultValue(), null); 164 } 165 166 public String getCategory(String key) { 167 return categories.get(key); 168 } 169 170 public String getCategory(Property prop) { 171 return getCategory(prop.key()); 172 } 173}