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.apache.commons.lang.StringUtils;
022 import org.sonar.ide.api.Logs;
023 import org.sonar.ide.api.SonarIdeException;
024 import org.sonar.ide.client.SonarClient;
025 import org.sonar.wsclient.Host;
026 import org.sonar.wsclient.Sonar;
027
028 import java.io.*;
029 import java.util.ArrayList;
030 import java.util.List;
031
032 /**
033 * @author Jérémie Lagarde
034 */
035 public class DefaultServerManager {
036
037 private static final String SERVER_CACHE_NAME = ".serverlist"; //$NON-NLS-1$
038
039 protected final ArrayList<Host> serverList = new ArrayList<Host>();
040 protected String path;
041
042 public DefaultServerManager() {
043 this(null);
044 }
045
046 public DefaultServerManager(String path) {
047 this.path = path;
048 try {
049 load();
050 } catch (Exception e) {
051 Logs.INFO.warn("default server manager error!", e);
052 }
053 }
054
055 public void addServer(String location, String username, String password) throws Exception {
056 addServer(new Host(location, username, password));
057 }
058
059 public void addServer(Host server) {
060 if (findServer(server.getHost()) != null) {
061 throw new SonarIdeException("Duplicate server: " + server.getHost()); //$NON-NLS-1$
062 }
063 serverList.add(server);
064 commit();
065 notifyListeners(IServerSetListener.SERVER_ADDED);
066 }
067
068 public List<Host> getServers() {
069 return serverList;
070 }
071
072 public boolean removeServer(String host) {
073 Host server = findServer(host);
074 if (server == null) {
075 return false;
076 }
077 boolean result = false;
078 result = serverList.remove(server);
079 notifyListeners(IServerSetListener.SERVER_REMOVED);
080 commit();
081 return result;
082 }
083
084 public Host createServer(String url) {
085 if (StringUtils.isBlank(url)) {
086 return null;
087 }
088 Host host = findServer(url);
089 if (host == null) {
090 host = new Host(url);
091 addServer(host);
092 commit();
093 }
094 return host;
095 }
096
097 public Host findServer(String host) {
098 Host server = null;
099 for (Host element : serverList) {
100 if (element.getHost().equals(host)) {
101 server = element;
102 break;
103 }
104 }
105 return server;
106 }
107
108 public Sonar getSonar(String url) {
109 final Host server = createServer(url);
110 return new SonarClient(server.getHost(), server.getUsername(), server.getPassword());
111 }
112
113 public boolean testSonar(String url, String user, String password) throws Exception {
114 SonarClient sonar = new SonarClient(url, user, password);
115 return sonar.isAvailable();
116 }
117
118 protected File getServerListFile() {
119 if (StringUtils.isBlank(path)) {
120 path = System.getProperty("user.home");
121 }
122 return new File(path + File.separator + SERVER_CACHE_NAME);
123 }
124
125 private void commit() {
126 File serverListFile = getServerListFile();
127 FileOutputStream fos = null;
128 PrintWriter writer = null;
129 try {
130 fos = new FileOutputStream(serverListFile);
131 writer = new PrintWriter(fos);
132 for (Host server : serverList) {
133 writer.println(server.getHost() + "|" + server.getUsername() + "|" + server.getPassword());
134 }
135 writer.flush();
136 fos.flush();
137 } catch (Exception ex) {
138 throw new SonarIdeException("error in commit server manager", ex);
139 } finally {
140 if (writer != null) {
141 writer.close();
142 }
143 if (fos != null) {
144 try {
145 fos.close();
146 } catch (IOException e) {
147 throw new SonarIdeException("error in commit server manager", e);
148 }
149 }
150 }
151 }
152
153 private void load() throws Exception {
154 serverList.clear();
155 File serverListFile = getServerListFile();
156 if (!serverListFile.exists()) {
157 return;
158 }
159 FileInputStream fis = null;
160 BufferedReader reader = null;
161 try {
162 fis = new FileInputStream(serverListFile);
163 reader = new BufferedReader(new InputStreamReader(fis));
164 String line;
165 do {
166 line = reader.readLine();
167 if (StringUtils.isNotBlank(line)) {
168 String[] infos = StringUtils.split(line, "|");
169 if (infos.length == 1) {
170 serverList.add(new Host(infos[0]));
171 }
172 if (infos.length == 3) {
173 serverList.add(new Host(infos[0], infos[1], infos[2]));
174 }
175 }
176 } while (line != null);
177 } finally {
178 if (fis != null) {
179 fis.close();
180 }
181 if (reader != null) {
182 reader.close();
183 }
184 }
185 }
186
187 public interface IServerSetListener {
188 public static final int SERVER_ADDED = 0;
189 public static final int SERVER_EDIT = 1;
190 public static final int SERVER_REMOVED = 2;
191
192 public void serverSetChanged(int type, List<Host> serverList);
193 }
194
195 protected final List<IServerSetListener> serverSetListeners = new ArrayList<IServerSetListener>();
196
197 public boolean addServerSetListener(IServerSetListener listener) {
198 return serverSetListeners.add(listener);
199 }
200
201 public boolean removeServerSetListener(IServerSetListener listener) {
202 return serverSetListeners.remove(listener);
203 }
204
205 protected void notifyListeners(final int eventType) {
206 for (final IServerSetListener listener : serverSetListeners) {
207 listener.serverSetChanged(eventType, serverList);
208 }
209 }
210 }