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 */ 020 package org.sonar.api.config; 021 022 import com.google.common.collect.Maps; 023 import org.apache.commons.lang.StringUtils; 024 import org.sonar.api.BatchComponent; 025 import org.sonar.api.Properties; 026 import org.sonar.api.Property; 027 import org.sonar.api.ServerComponent; 028 import org.sonar.api.utils.AnnotationUtils; 029 030 import java.util.Arrays; 031 import java.util.Collection; 032 import java.util.Map; 033 034 /** 035 * Metadata of all the properties declared by plugins 036 * 037 * @since 2.12 038 */ 039 public final class PropertyDefinitions implements BatchComponent, ServerComponent { 040 041 private Map<String, PropertyDefinition> definitions = Maps.newHashMap(); 042 private Map<String, String> categories = Maps.newHashMap(); 043 044 public PropertyDefinitions(Object... components) { 045 if (components != null) { 046 addComponents(Arrays.asList(components)); 047 } 048 } 049 050 public PropertyDefinitions addComponents(Collection components) { 051 return addComponents(components, ""); 052 } 053 054 public PropertyDefinitions addComponents(Collection components, String defaultCategory) { 055 for (Object component : components) { 056 addComponent(component, defaultCategory); 057 } 058 return this; 059 } 060 061 public PropertyDefinitions addComponent(Object object) { 062 return addComponent(object, ""); 063 } 064 065 public PropertyDefinitions addComponent(Object component, String defaultCategory) { 066 Properties annotations = AnnotationUtils.getClassAnnotation(component, Properties.class); 067 if (annotations != null) { 068 for (Property property : annotations.value()) { 069 addProperty(property, defaultCategory); 070 } 071 } 072 Property annotation = AnnotationUtils.getClassAnnotation(component, Property.class); 073 if (annotation != null) { 074 addProperty(annotation, defaultCategory); 075 } 076 return this; 077 } 078 079 private PropertyDefinitions addProperty(Property property, String defaultCategory) { 080 PropertyDefinition definition = PropertyDefinition.create(property); 081 return add(definition, defaultCategory); 082 } 083 084 private PropertyDefinitions add(PropertyDefinition definition, String defaultCategory) { 085 if (!definitions.containsKey(definition.getKey())) { 086 definitions.put(definition.getKey(), definition); 087 categories.put(definition.getKey(), StringUtils.defaultIfBlank(definition.getCategory(), defaultCategory)); 088 } 089 return this; 090 } 091 092 public PropertyDefinition get(String key) { 093 return definitions.get(key); 094 } 095 096 public Collection<PropertyDefinition> getAll() { 097 return definitions.values(); 098 } 099 100 public String getDefaultValue(String key) { 101 PropertyDefinition def = get(key); 102 if (def != null) { 103 return StringUtils.defaultIfEmpty(def.getDefaultValue(), null); 104 } 105 return null; 106 } 107 108 public String getCategory(String key) { 109 return categories.get(key); 110 } 111 112 public String getCategory(Property prop) { 113 return getCategory(prop.key()); 114 } 115 }