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.notifications;
021
022import com.google.common.collect.Maps;
023import org.sonar.api.ServerExtension;
024
025import java.util.Map;
026
027/**
028 * <p>
029 * Notification dispatchers (see {@link NotificationDispatcher}) can define their own metadata class in order
030 * to tell more about them.
031 * <p/>
032 * Instances of these classes must be declared in {@link org.sonar.api.SonarPlugin#getExtensions()}.
033 *
034 * @since 3.5
035 */
036public final class NotificationDispatcherMetadata implements ServerExtension {
037
038  public static final String GLOBAL_NOTIFICATION = "globalNotification";
039  public static final String PER_PROJECT_NOTIFICATION = "perProjectNotification";
040
041  private String dispatcherKey;
042  private Map<String, String> properties;
043
044  private NotificationDispatcherMetadata(String dispatcherKey) {
045    this.dispatcherKey = dispatcherKey;
046    this.properties = Maps.newHashMap();
047  }
048
049  /**
050   * Creates a new metadata instance for the given dispatcher.
051   * <p/>
052   * By default the key is the class name without package. It can be changed by overriding
053   * {@link org.sonar.api.notifications.NotificationDispatcher#getKey()}.
054   */
055  public static NotificationDispatcherMetadata create(String dispatcherKey) {
056    return new NotificationDispatcherMetadata(dispatcherKey);
057  }
058
059  /**
060   * Sets a property on this metadata object.
061   */
062  public NotificationDispatcherMetadata setProperty(String key, String value) {
063    properties.put(key, value);
064    return this;
065  }
066
067  /**
068   * Gives the property for the given key.
069   */
070  public String getProperty(String key) {
071    return properties.get(key);
072  }
073
074  /**
075   * Returns the unique key of the dispatcher.
076   */
077  public String getDispatcherKey() {
078    return dispatcherKey;
079  }
080
081  @Override
082  public String toString() {
083    return dispatcherKey;
084  }
085
086  @Override
087  public boolean equals(Object o) {
088    if (this == o) {
089      return true;
090    }
091    if (o == null || getClass() != o.getClass()) {
092      return false;
093    }
094    NotificationDispatcherMetadata that = (NotificationDispatcherMetadata) o;
095    return dispatcherKey.equals(that.dispatcherKey);
096  }
097
098  @Override
099  public int hashCode() {
100    return dispatcherKey.hashCode();
101  }
102}