001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 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     */
020    package org.sonar.process;
021    
022    import org.apache.commons.lang.StringUtils;
023    
024    import javax.annotation.CheckForNull;
025    import javax.annotation.Nullable;
026    
027    import java.io.File;
028    import java.util.Properties;
029    
030    public class Props {
031    
032      private final Properties properties;
033      private final Encryption encryption;
034    
035      public Props(Properties props) {
036        this.properties = props;
037        this.encryption = new Encryption(props.getProperty(AesCipher.ENCRYPTION_SECRET_KEY_PATH));
038      }
039    
040      public boolean contains(String key) {
041        return properties.containsKey(key);
042      }
043    
044      @CheckForNull
045      public String value(String key) {
046        String value = properties.getProperty(key);
047        if (value != null && encryption.isEncrypted(value)) {
048          value = encryption.decrypt(value);
049        }
050        return value;
051      }
052    
053      public String nonNullValue(String key) {
054        String value = value(key);
055        if (value == null) {
056          throw new IllegalArgumentException("Missing property: " + key);
057        }
058        return value;
059      }
060    
061      @CheckForNull
062      public String value(String key, @Nullable String defaultValue) {
063        String s = value(key);
064        return s == null ? defaultValue : s;
065      }
066    
067      public boolean valueAsBoolean(String key) {
068        String s = value(key);
069        return s != null && Boolean.parseBoolean(s);
070      }
071    
072      public boolean valueAsBoolean(String key, boolean defaultValue) {
073        String s = value(key);
074        return s != null ? Boolean.parseBoolean(s) : defaultValue;
075      }
076    
077      public File nonNullValueAsFile(String key) {
078        String s = value(key);
079        if (s == null) {
080          throw new IllegalArgumentException("Property " + key + " is not set");
081        }
082        return new File(s);
083      }
084    
085      @CheckForNull
086      public Integer valueAsInt(String key) {
087        String s = value(key);
088        if (s != null && !"".equals(s)) {
089          try {
090            return Integer.parseInt(s);
091          } catch (NumberFormatException e) {
092            throw new IllegalStateException("Value of property " + key + " is not an integer: " + s, e);
093          }
094        }
095        return null;
096      }
097    
098      public int valueAsInt(String key, int defaultValue) {
099        Integer i = valueAsInt(key);
100        return i == null ? defaultValue : i;
101      }
102    
103      public Properties rawProperties() {
104        return properties;
105      }
106    
107      public Props set(String key, @Nullable String value) {
108        if (value != null) {
109          properties.setProperty(key, value);
110        }
111        return this;
112      }
113    
114      public void setDefault(String key, String value) {
115        String s = properties.getProperty(key);
116        if (StringUtils.isBlank(s)) {
117          properties.setProperty(key, value);
118        }
119      }
120    }