-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
ServerImpl.java
192 lines (161 loc) · 5.3 KB
/
ServerImpl.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
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.base.Objects;
import com.google.common.io.Resources;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import javax.annotation.CheckForNull;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.picocontainer.Startable;
import org.sonar.api.CoreProperties;
import org.sonar.api.config.Settings;
import org.sonar.api.platform.Server;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.process.ProcessProperties;
import org.sonar.server.app.TomcatContexts;
import static org.sonar.api.CoreProperties.SERVER_BASE_URL;
import static org.sonar.api.CoreProperties.SERVER_BASE_URL_DEFAULT_VALUE;
public final class ServerImpl extends Server implements Startable {
private static final Logger LOG = Loggers.get(ServerImpl.class);
private final Settings settings;
private final Date startedAt;
private final String buildProperties;
private final String versionPath;
private String id;
private String version;
private String implementationBuild;
private File sonarHome;
private File deployDir;
public ServerImpl(Settings settings) {
this(settings, "/build.properties", "/sq-version.txt");
}
@VisibleForTesting
ServerImpl(Settings settings, String buildProperties, String versionPath) {
this.settings = settings;
this.startedAt = new Date();
this.buildProperties = buildProperties;
this.versionPath = versionPath;
}
@Override
public void start() {
try {
id = new SimpleDateFormat("yyyyMMddHHmmss").format(startedAt);
version = readVersion(versionPath);
implementationBuild = read(buildProperties).getProperty("Implementation-Build");
sonarHome = new File(settings.getString(ProcessProperties.PATH_HOME));
if (!sonarHome.isDirectory()) {
throw new IllegalStateException("SonarQube home directory is not valid");
}
deployDir = new File(settings.getString(ProcessProperties.PATH_DATA), TomcatContexts.WEB_DEPLOY_PATH_RELATIVE_TO_DATA_DIR);
LOG.info("SonarQube {}", Joiner.on(" / ").skipNulls().join("Server", version, implementationBuild));
} catch (IOException e) {
throw new IllegalStateException("Can not load metadata", e);
}
}
@Override
public void stop() {
// do nothing
}
@Override
public String getPermanentServerId() {
return settings.getString(CoreProperties.PERMANENT_SERVER_ID);
}
@Override
public String getId() {
return id;
}
@Override
public String getVersion() {
return version;
}
public String getImplementationBuild() {
return implementationBuild;
}
@Override
public Date getStartedAt() {
return startedAt;
}
@Override
public File getRootDir() {
return sonarHome;
}
@Override
@CheckForNull
public File getDeployDir() {
return deployDir;
}
@Override
public String getContextPath() {
return "";
}
@Override
public String getPublicRootUrl() {
return get(SERVER_BASE_URL, SERVER_BASE_URL_DEFAULT_VALUE);
}
@Override
public boolean isDev() {
return settings.getBoolean("sonar.web.dev");
}
@Override
public boolean isSecured() {
return get(SERVER_BASE_URL, SERVER_BASE_URL_DEFAULT_VALUE).startsWith("https://");
}
private static String readVersion(String filename) throws IOException {
URL url = ServerImpl.class.getResource(filename);
if (url != null) {
String version = Resources.toString(url, StandardCharsets.UTF_8);
if (!StringUtils.isBlank(version)) {
return StringUtils.deleteWhitespace(version);
}
}
throw new IllegalStateException("Unknown SonarQube version");
}
private static Properties read(String filename) throws IOException {
Properties properties = new Properties();
InputStream stream = null;
try {
stream = ServerImpl.class.getResourceAsStream(filename);
if (stream != null) {
properties.load(stream);
}
} finally {
IOUtils.closeQuietly(stream);
}
return properties;
}
@Override
public String getURL() {
return null;
}
private String get(String key, String defaultValue) {
return Objects.firstNonNull(settings.getString(key), defaultValue);
}
}