001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2011 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, Property> properties = 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 PropertyDefinitions addProperty(Property property) {
080 return addProperty(property, "");
081 }
082
083 PropertyDefinitions addProperty(Property property, String defaultCategory) {
084 if (!properties.containsKey(property.key())) {
085 properties.put(property.key(), property);
086 categories.put(property.key(), StringUtils.defaultIfBlank(property.category(), defaultCategory));
087 }
088 return this;
089 }
090
091 public Property getProperty(String key) {
092 return properties.get(key);
093 }
094
095 public Collection<Property> getProperties() {
096 return properties.values();
097 }
098
099 public String getDefaultValue(String key) {
100 Property prop = getProperty(key);
101 if (prop != null) {
102 return StringUtils.defaultIfEmpty(prop.defaultValue(), null);
103 }
104 return null;
105 }
106
107 public String getCategory(String key) {
108 return categories.get(key);
109 }
110
111 public String getCategory(Property prop) {
112 return getCategory(prop.key());
113 }
114 }