001 /*
002 * Copyright (C) 2010 Evgeny Mandrikov
003 *
004 * Sonar-IDE is free software; you can redistribute it and/or
005 * modify it under the terms of the GNU Lesser General Public
006 * License as published by the Free Software Foundation; either
007 * version 3 of the License, or (at your option) any later version.
008 *
009 * Sonar-IDE is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with Sonar-IDE; if not, write to the Free Software
016 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
017 */
018
019 package org.sonar.ide.shared;
020
021 import org.slf4j.Logger;
022 import org.slf4j.LoggerFactory;
023 import org.sonar.wsclient.Host;
024
025 import java.io.*;
026 import java.util.Properties;
027
028 /**
029 * @author Evgeny Mandrikov
030 * @deprecated since 0.2
031 */
032 @Deprecated
033 public final class SonarProperties {
034 public static final Logger LOG = LoggerFactory.getLogger(SonarProperties.class);
035
036 public static final String FILENAME = ".sonar-ide.properties";
037
038 public static final String HOST_DEFAULT = "http://localhost:9000";
039
040 public static final String HOST_PROPERTY = "host";
041 public static final String USERNAME_PROPERTY = "username";
042 public static final String PASSWORD_PROPERTY = "password";
043
044 private static SonarProperties instance;
045
046 private String path;
047 private Host server;
048
049 public SonarProperties(String path) {
050 this.path = path;
051 this.server = new Host(HOST_DEFAULT);
052 reload();
053 }
054
055 public Host getServer() {
056 return server;
057 }
058
059 public void reload() {
060 Properties properties = load(path);
061 server.setHost(properties.getProperty(HOST_PROPERTY, HOST_DEFAULT));
062 server.setPassword(properties.getProperty(PASSWORD_PROPERTY));
063 server.setUsername(properties.getProperty(USERNAME_PROPERTY));
064 }
065
066 private static void addProperty(Properties properties, String key, String value) {
067 if (value != null) {
068 properties.setProperty(key, value);
069 }
070 }
071
072 public void save() {
073 Properties properties = new Properties();
074 addProperty(properties, HOST_PROPERTY, server.getHost());
075 addProperty(properties, USERNAME_PROPERTY, server.getUsername());
076 addProperty(properties, PASSWORD_PROPERTY, server.getPassword());
077 save(properties, path);
078 }
079
080 public static Properties load(String filename) {
081 LOG.info("Loading settings from '{}'", filename);
082 Properties properties = new Properties();
083 if (filename == null) {
084 return properties;
085 }
086 FileInputStream inputStream = null;
087 File file = new File(filename);
088 if (file.exists()) {
089 try {
090 inputStream = new FileInputStream(filename);
091 properties.load(inputStream);
092 } catch (IOException e) {
093 LOG.error(e.getMessage(), e);
094 } finally {
095 closeStream(inputStream);
096 }
097 } else {
098 LOG.warn("File doesn't exists");
099 }
100 return properties;
101 }
102
103 public static void save(Properties properties, String filename) {
104 LOG.info("Saving settings to '{}'", filename);
105 FileOutputStream outputStream = null;
106 try {
107 outputStream = new FileOutputStream(filename);
108 properties.store(outputStream, "Sonar Settings");
109 } catch (FileNotFoundException e) {
110 LOG.error(e.getMessage(), e);
111 } catch (IOException e) {
112 LOG.error(e.getMessage(), e);
113 } finally {
114 closeStream(outputStream);
115 }
116 }
117
118 public static String getDefaultPath() {
119 return System.getProperty("user.home") + File.separator + FILENAME;
120 }
121
122 public static void closeStream(Closeable stream) {
123 if (stream != null) {
124 try {
125 stream.close();
126 } catch (IOException e) {
127 // ignore
128 }
129 }
130 }
131 }