forked from jenkinsci/gerrit-trigger-plugin
/
GerritProjectListUpdater.java
214 lines (196 loc) · 7.26 KB
/
GerritProjectListUpdater.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* The MIT License
*
* Copyright 2010 Sony Ericsson Mobile Communications.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.sonyericsson.hudson.plugins.gerrit.trigger;
import com.sonymobile.tools.gerrit.gerritevents.ConnectionListener;
import com.sonymobile.tools.gerrit.gerritevents.ssh.SshConnection;
import com.sonymobile.tools.gerrit.gerritevents.ssh.SshConnectionFactory;
import com.sonymobile.tools.gerrit.gerritevents.ssh.SshException;
import com.sonyericsson.hudson.plugins.gerrit.trigger.config.IGerritHudsonTriggerConfig;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Class responsible for providing the Config object with a list of all
* available Gerrit projects. Executed periodically on a timely basis
* and every time the connection to the Gerrit server has been restored.
*
* @author Gustaf Lundh <Gustaf.Lundh@sonyericsson.com>
*/
public class GerritProjectListUpdater extends Thread implements ConnectionListener {
/**
* The command for fetching projects.
*/
public static final String GERRIT_LS_PROJECTS = "gerrit ls-projects";
/**
* Time to wait between refresh attempts
*/
private static final int UPDATE_DELAY = 3600 * 1000;
private boolean connected = false;
private boolean shutdown = false;
private static final Logger logger = LoggerFactory.getLogger(GerritProjectListUpdater.class);
private List<String> gerritProjects;
private String serverName;
/**
* Standard constructor.
*
* @param serverName the name of the Gerrit server.
*/
public GerritProjectListUpdater(String serverName) {
this.setName(this.getClass().getName() + " for " + serverName + " Thread");
this.setDaemon(true);
this.serverName = serverName;
addThisAsListener();
}
/**
* Add the current list updater as a listener to the GerritServer object.
*/
private void addThisAsListener() {
GerritServer server = PluginImpl.getInstance().getServer(serverName);
if (server != null) {
server.addListener(this);
connected = server.isConnected();
} else {
logger.error("Could not find the server {}", serverName);
}
}
@Override
public synchronized void connectionEstablished() {
setConnected(true);
notify();
}
@Override
public synchronized void connectionDown() {
setConnected(false);
}
/**
* Shutdown the thread.
*/
public synchronized void shutdown() {
shutdown = true;
notify();
}
@Override
public void run() {
while (!shutdown) {
try {
if (isConnected()) {
IGerritHudsonTriggerConfig activeConfig = getConfig();
SshConnection sshConnection = SshConnectionFactory.getConnection(
activeConfig.getGerritHostName(),
activeConfig.getGerritSshPort(),
activeConfig.getGerritProxy(),
activeConfig.getGerritAuthentication()
);
setGerritProjects(readProjects(sshConnection.executeCommandReader(GERRIT_LS_PROJECTS)));
sshConnection.disconnect();
}
} catch (SshException ex) {
logger.warn("Could not connect to Gerrit server when updating Gerrit project list: ", ex);
} catch (IOException ex) {
logger.error("Could not read stream with Gerrit projects: ", ex);
}
try {
synchronized (this) {
wait(UPDATE_DELAY);
}
} catch (InterruptedException ex) {
logger.warn("InterruptedException: ", ex);
break;
}
}
GerritServer server = PluginImpl.getInstance().getServer(serverName);
if (server != null) {
server.removeListener(this);
} else {
logger.error("Could not find server {}", serverName);
}
}
/**
* Get the the server config.
* @return the server config or null if config not found.
*/
private IGerritHudsonTriggerConfig getConfig() {
GerritServer server = PluginImpl.getInstance().getServer(serverName);
if (server != null) {
IGerritHudsonTriggerConfig config = server.getConfig();
if (config != null) {
return config;
} else {
logger.error("Could not find the server config");
}
} else {
logger.error("Could not find server {}", serverName);
}
return null;
}
/**
* Reads a list of Gerrit projects from a Reader, one project per line. As from command: gerrit ls-projects.
* @param commandReader the Reader.
* @return a list of projects
* @throws IOException if something unfortunate happens.
*/
public static List<String> readProjects(Reader commandReader) throws IOException {
List<String> projects = new ArrayList<String>();
BufferedReader br = new BufferedReader(commandReader);
String line = br.readLine();
while (line != null) {
projects.add(line);
line = br.readLine();
}
return projects;
}
/**
* @return if connected to Gerrit.
*/
public synchronized boolean isConnected() {
return connected;
}
/**
* @param connected the connected to set.
*/
public synchronized void setConnected(boolean connected) {
this.connected = connected;
}
/**
* Sets the internal Gerrit project list.
* @param projects The list of projects
*/
public synchronized void setGerritProjects(List<String> projects) {
gerritProjects = projects;
}
/**
* Returns a string list of Gerrit projects.
* @return list of gerrit projects
*/
public synchronized List<String> getGerritProjects() {
if (gerritProjects == null) {
gerritProjects = new ArrayList<String>();
}
return gerritProjects;
}
}