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.gwt;
021
022import com.google.gwt.i18n.client.Dictionary;
023
024import java.util.Set;
025
026public final class Configuration {
027
028  private Configuration() {
029    // only static methods
030  }
031
032  public static String getSonarVersion() {
033    return getParameter("version");
034  }
035
036  /**
037   * Get the id of the selected resource. Can be null if none resource is selected.
038   */
039  public static String getResourceId() {
040    return getParameter("resource_key");
041  }
042
043  public static String getParameter(String key) {
044    return getParameter(key, null);
045  }
046
047  public static String getParameter(String key, String defaultValue) {
048    String result = getDictionaryEntry("config", key);
049    if (result == null) {
050      result = defaultValue;
051    }
052    return result;
053  }
054
055  public static native void setParameter(String key, String val)  /*-{
056    $wnd.config[key] = val;
057  }-*/;
058
059  public static String getRequestParameter(String key) {
060    return getDictionaryEntry("rp", key);
061  }
062
063  public static String getRequestParameter(String key, String defaultValue) {
064    String value = getRequestParameter(key);
065    return (value!=null ? value : defaultValue);
066  }
067
068  public static Set<String> getParameterKeys() {
069    return getDictionaryKeys("config");
070  }
071
072  public static Set<String> getRequestParameterKeys() {
073    return getDictionaryKeys("rp");
074  }
075
076  private static String getDictionaryEntry(String dictionaryName, String key) {
077    try {
078      Dictionary dic = Dictionary.getDictionary(dictionaryName);
079      if (dic != null) {
080        return dic.get(key);
081      }
082      return null;
083
084    } catch (Exception e) {
085      return null;
086    }
087  }
088
089  private static Set<String> getDictionaryKeys(String dictionaryName) {
090    Dictionary dic = Dictionary.getDictionary(dictionaryName);
091    if (dic != null) {
092      return dic.keySet();
093    }
094    return null;
095  }
096
097}