SitePaths: Convert configuration paths to Path
Change-Id: Ie25659964fa2546e2b73370edaa87a86da1d6b70
This commit is contained in:
@@ -125,6 +125,7 @@ java_test(
|
||||
':init-api',
|
||||
':pgm',
|
||||
'//gerrit-server:server',
|
||||
'//lib:guava',
|
||||
'//lib:junit',
|
||||
'//lib/easymock:easymock',
|
||||
'//lib/guice:guice',
|
||||
|
@@ -47,7 +47,7 @@ import java.util.zip.ZipEntry;
|
||||
public class SwitchSecureStore extends SiteProgram {
|
||||
private static String getSecureStoreClassFromGerritConfig(SitePaths sitePaths) {
|
||||
FileBasedConfig cfg =
|
||||
new FileBasedConfig(sitePaths.gerrit_config, FS.DETECTED);
|
||||
new FileBasedConfig(sitePaths.gerrit_config.toFile(), FS.DETECTED);
|
||||
try {
|
||||
cfg.load();
|
||||
} catch (IOException | ConfigInvalidException e) {
|
||||
@@ -151,7 +151,7 @@ public class SwitchSecureStore extends SiteProgram {
|
||||
log.info("Set gerrit.secureStoreClass property of gerrit.config to {}",
|
||||
newSecureStore);
|
||||
FileBasedConfig config =
|
||||
new FileBasedConfig(sitePaths.gerrit_config, FS.DETECTED);
|
||||
new FileBasedConfig(sitePaths.gerrit_config.toFile(), FS.DETECTED);
|
||||
config.load();
|
||||
config.setString("gerrit", null, "secureStoreClass", newSecureStore);
|
||||
config.save();
|
||||
|
@@ -169,7 +169,7 @@ class InitHttpd implements InitStep {
|
||||
final String dname =
|
||||
"CN=" + hostname + ",OU=Gerrit Code Review,O=" + domainOf(hostname);
|
||||
|
||||
Path tmpdir = site.etc_dir.toPath().resolve("tmp.sslcertgen");
|
||||
Path tmpdir = site.etc_dir.resolve("tmp.sslcertgen");
|
||||
try {
|
||||
Files.createDirectory(tmpdir);
|
||||
} catch (IOException e) {
|
||||
|
@@ -25,6 +25,8 @@ import com.google.gerrit.server.mail.SmtpEmailSender.Encryption;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
import java.nio.file.Files;
|
||||
|
||||
/** Initialize the {@code sendemail} configuration section. */
|
||||
@Singleton
|
||||
class InitSendEmail implements InitStep {
|
||||
@@ -54,7 +56,7 @@ class InitSendEmail implements InitStep {
|
||||
true);
|
||||
|
||||
String username = null;
|
||||
if (site.gerrit_config.exists()) {
|
||||
if (Files.exists(site.gerrit_config)) {
|
||||
username = sendemail.get("smtpUser");
|
||||
} else if ((enc != null && enc != Encryption.NONE) || !isLocal(hostname)) {
|
||||
username = username();
|
||||
|
@@ -131,7 +131,7 @@ class InitSshd implements InitStep {
|
||||
// short period of time. We try to reduce that risk by creating
|
||||
// the key within a temporary directory.
|
||||
//
|
||||
Path tmpdir = site.etc_dir.toPath().resolve("tmp.sshkeygen");
|
||||
Path tmpdir = site.etc_dir.resolve("tmp.sshkeygen");
|
||||
try {
|
||||
Files.createDirectory(tmpdir);
|
||||
} catch (IOException e) {
|
||||
|
@@ -37,6 +37,8 @@ import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
@@ -65,7 +67,7 @@ class UpgradeFrom2_0_x implements InitStep {
|
||||
private final FileBasedConfig cfg;
|
||||
private final SecureStore sec;
|
||||
private final File site_path;
|
||||
private final File etc_dir;
|
||||
private final Path etc_dir;
|
||||
private final Section.Factory sections;
|
||||
|
||||
@Inject
|
||||
@@ -100,14 +102,16 @@ class UpgradeFrom2_0_x implements InitStep {
|
||||
}
|
||||
|
||||
for (String name : etcFiles) {
|
||||
final File src = new File(site_path, name);
|
||||
final File dst = new File(etc_dir, name);
|
||||
if (src.exists()) {
|
||||
if (dst.exists()) {
|
||||
Path src = site_path.toPath().resolve(name);
|
||||
Path dst = etc_dir.resolve(name);
|
||||
if (Files.exists(src)) {
|
||||
if (Files.exists(dst)) {
|
||||
throw die("File " + src + " would overwrite " + dst);
|
||||
}
|
||||
if (!src.renameTo(dst)) {
|
||||
throw die("Cannot rename " + src + " to " + dst);
|
||||
try {
|
||||
Files.move(src, dst);
|
||||
} catch (IOException e) {
|
||||
throw die("Cannot rename " + src + " to " + dst, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -51,7 +51,7 @@ public class InitFlags {
|
||||
ConfigInvalidException {
|
||||
sec = secureStore;
|
||||
this.installPlugins = installPlugins;
|
||||
cfg = new FileBasedConfig(site.gerrit_config, FS.DETECTED);
|
||||
cfg = new FileBasedConfig(site.gerrit_config.toFile(), FS.DETECTED);
|
||||
|
||||
cfg.load();
|
||||
}
|
||||
|
@@ -199,7 +199,8 @@ public abstract class SiteProgram extends AbstractProgram {
|
||||
};
|
||||
Injector i = Guice.createInjector(m);
|
||||
SitePaths site = i.getInstance(SitePaths.class);
|
||||
FileBasedConfig cfg = new FileBasedConfig(site.gerrit_config, FS.DETECTED);
|
||||
FileBasedConfig cfg =
|
||||
new FileBasedConfig(site.gerrit_config.toFile(), FS.DETECTED);
|
||||
if (!cfg.getFile().exists()) {
|
||||
return null;
|
||||
}
|
||||
|
@@ -26,6 +26,7 @@ import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.google.gerrit.pgm.init.api.ConsoleUI;
|
||||
import com.google.gerrit.pgm.init.api.InitFlags;
|
||||
import com.google.gerrit.pgm.init.api.Section;
|
||||
@@ -36,11 +37,10 @@ import org.eclipse.jgit.errors.ConfigInvalidException;
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
import org.eclipse.jgit.storage.file.FileBasedConfig;
|
||||
import org.eclipse.jgit.util.FS;
|
||||
import org.eclipse.jgit.util.IO;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Collections;
|
||||
@@ -55,7 +55,7 @@ public class UpgradeFrom2_0_xTest extends InitTestCase {
|
||||
final SitePaths site = new SitePaths(p);
|
||||
assertTrue(site.isNew);
|
||||
assertTrue(site.site_path.mkdir());
|
||||
assertTrue(site.etc_dir.mkdir());
|
||||
Files.createDirectory(site.etc_dir);
|
||||
|
||||
for (String n : UpgradeFrom2_0_x.etcFiles) {
|
||||
Files.write(p.resolve(n), ("# " + n + "\n").getBytes(UTF_8));
|
||||
@@ -98,11 +98,14 @@ public class UpgradeFrom2_0_xTest extends InitTestCase {
|
||||
for (String n : UpgradeFrom2_0_x.etcFiles) {
|
||||
if ("gerrit.config".equals(n)) continue;
|
||||
if ("secure.config".equals(n)) continue;
|
||||
assertEquals("# " + n + "\n",//
|
||||
new String(IO.readFully(new File(site.etc_dir, n)), "UTF-8"));
|
||||
try (InputStream in = Files.newInputStream(site.etc_dir.resolve(n))) {
|
||||
assertEquals("# " + n + "\n",
|
||||
new String(ByteStreams.toByteArray(in), UTF_8));
|
||||
}
|
||||
}
|
||||
|
||||
FileBasedConfig cfg = new FileBasedConfig(site.gerrit_config, FS.DETECTED);
|
||||
FileBasedConfig cfg =
|
||||
new FileBasedConfig(site.gerrit_config.toFile(), FS.DETECTED);
|
||||
cfg.load();
|
||||
|
||||
assertEquals("email.user", cfg.getString("sendemail", null, "smtpUser"));
|
||||
|
@@ -44,10 +44,11 @@ class GerritServerConfigProvider implements Provider<Config> {
|
||||
|
||||
@Override
|
||||
public Config get() {
|
||||
FileBasedConfig cfg = new FileBasedConfig(site.gerrit_config, FS.DETECTED);
|
||||
FileBasedConfig cfg =
|
||||
new FileBasedConfig(site.gerrit_config.toFile(), FS.DETECTED);
|
||||
|
||||
if (!cfg.getFile().exists()) {
|
||||
log.info("No " + site.gerrit_config.getAbsolutePath()
|
||||
log.info("No " + site.gerrit_config.toAbsolutePath()
|
||||
+ "; assuming defaults");
|
||||
return new GerritConfig(cfg, secureStore);
|
||||
}
|
||||
|
@@ -35,6 +35,7 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
|
||||
@Singleton
|
||||
@@ -61,7 +62,7 @@ public class PluginConfigFactory implements ReloadPluginListener {
|
||||
this.projectStateFactory = projectStateFactory;
|
||||
this.pluginConfigs = Maps.newHashMap();
|
||||
|
||||
this.cfgSnapshot = FileSnapshot.save(site.gerrit_config);
|
||||
this.cfgSnapshot = FileSnapshot.save(site.gerrit_config.toFile());
|
||||
this.cfg = cfgProvider.get();
|
||||
}
|
||||
|
||||
@@ -103,8 +104,9 @@ public class PluginConfigFactory implements ReloadPluginListener {
|
||||
* @return the plugin configuration from the 'gerrit.config' file
|
||||
*/
|
||||
public PluginConfig getFromGerritConfig(String pluginName, boolean refresh) {
|
||||
if (refresh && cfgSnapshot.isModified(site.gerrit_config)) {
|
||||
cfgSnapshot = FileSnapshot.save(site.gerrit_config);
|
||||
File configFile = site.gerrit_config.toFile();
|
||||
if (refresh && cfgSnapshot.isModified(configFile)) {
|
||||
cfgSnapshot = FileSnapshot.save(configFile);
|
||||
cfg = cfgProvider.get();
|
||||
}
|
||||
return new PluginConfig(pluginName, cfg);
|
||||
@@ -250,20 +252,21 @@ public class PluginConfigFactory implements ReloadPluginListener {
|
||||
return pluginConfigs.get(pluginName);
|
||||
}
|
||||
|
||||
File pluginConfigFile = new File(site.etc_dir, pluginName + ".config");
|
||||
FileBasedConfig cfg = new FileBasedConfig(pluginConfigFile, FS.DETECTED);
|
||||
Path pluginConfigFile = site.etc_dir.resolve(pluginName + ".config");
|
||||
FileBasedConfig cfg =
|
||||
new FileBasedConfig(pluginConfigFile.toFile(), FS.DETECTED);
|
||||
pluginConfigs.put(pluginName, cfg);
|
||||
if (!cfg.getFile().exists()) {
|
||||
log.info("No " + pluginConfigFile.getAbsolutePath() + "; assuming defaults");
|
||||
log.info("No " + pluginConfigFile.toAbsolutePath() + "; assuming defaults");
|
||||
return cfg;
|
||||
}
|
||||
|
||||
try {
|
||||
cfg.load();
|
||||
} catch (IOException e) {
|
||||
log.warn("Failed to load " + pluginConfigFile.getAbsolutePath(), e);
|
||||
log.warn("Failed to load " + pluginConfigFile.toAbsolutePath(), e);
|
||||
} catch (ConfigInvalidException e) {
|
||||
log.warn("Failed to load " + pluginConfigFile.getAbsolutePath(), e);
|
||||
log.warn("Failed to load " + pluginConfigFile.toAbsolutePath(), e);
|
||||
}
|
||||
|
||||
return cfg;
|
||||
|
@@ -31,7 +31,7 @@ public final class SitePaths {
|
||||
|
||||
public final File site_path;
|
||||
public final Path bin_dir;
|
||||
public final File etc_dir;
|
||||
public final Path etc_dir;
|
||||
public final File lib_dir;
|
||||
public final Path tmp_dir;
|
||||
public final File logs_dir;
|
||||
@@ -46,9 +46,9 @@ public final class SitePaths {
|
||||
public final Path gerrit_sh;
|
||||
public final Path gerrit_war;
|
||||
|
||||
public final File gerrit_config;
|
||||
public final File secure_config;
|
||||
public final File contact_information_pub;
|
||||
public final Path gerrit_config;
|
||||
public final Path secure_config;
|
||||
public final Path contact_information_pub;
|
||||
|
||||
public final Path ssl_keystore;
|
||||
public final Path ssh_key;
|
||||
@@ -71,13 +71,13 @@ public final class SitePaths {
|
||||
Path p = sitePath;
|
||||
|
||||
bin_dir = p.resolve("bin");
|
||||
etc_dir = new File(site_path, "etc");
|
||||
etc_dir = p.resolve("etc");
|
||||
lib_dir = new File(site_path, "lib");
|
||||
tmp_dir = p.resolve("tmp");
|
||||
plugins_dir = new File(site_path, "plugins");
|
||||
data_dir = new File(site_path, "data");
|
||||
logs_dir = new File(site_path, "logs");
|
||||
mail_dir = new File(etc_dir, "mail");
|
||||
mail_dir = etc_dir.resolve("mail").toFile();
|
||||
hooks_dir = new File(site_path, "hooks");
|
||||
static_dir = new File(site_path, "static");
|
||||
themes_dir = new File(site_path, "themes");
|
||||
@@ -86,21 +86,20 @@ public final class SitePaths {
|
||||
gerrit_sh = bin_dir.resolve("gerrit.sh");
|
||||
gerrit_war = bin_dir.resolve("gerrit.war");
|
||||
|
||||
gerrit_config = new File(etc_dir, "gerrit.config");
|
||||
secure_config = new File(etc_dir, "secure.config");
|
||||
contact_information_pub = new File(etc_dir, "contact_information.pub");
|
||||
gerrit_config = etc_dir.resolve("gerrit.config");
|
||||
secure_config = etc_dir.resolve("secure.config");
|
||||
contact_information_pub = etc_dir.resolve("contact_information.pub");
|
||||
|
||||
Path etcDirPath = etc_dir.toPath();
|
||||
ssl_keystore = etcDirPath.resolve("keystore");
|
||||
ssh_key = etcDirPath.resolve("ssh_host_key");
|
||||
ssh_rsa = etcDirPath.resolve("ssh_host_rsa_key");
|
||||
ssh_dsa = etcDirPath.resolve("ssh_host_dsa_key");
|
||||
peer_keys = etcDirPath.resolve("peer_keys");
|
||||
ssl_keystore = etc_dir.resolve("keystore");
|
||||
ssh_key = etc_dir.resolve("ssh_host_key");
|
||||
ssh_rsa = etc_dir.resolve("ssh_host_rsa_key");
|
||||
ssh_dsa = etc_dir.resolve("ssh_host_dsa_key");
|
||||
peer_keys = etc_dir.resolve("peer_keys");
|
||||
|
||||
site_css = etcDirPath.resolve(CSS_FILENAME);
|
||||
site_header = etcDirPath.resolve(HEADER_FILENAME);
|
||||
site_footer = etcDirPath.resolve(FOOTER_FILENAME);
|
||||
site_gitweb = etcDirPath.resolve("gitweb_config.perl");
|
||||
site_css = etc_dir.resolve(CSS_FILENAME);
|
||||
site_header = etc_dir.resolve(HEADER_FILENAME);
|
||||
site_footer = etc_dir.resolve(FOOTER_FILENAME);
|
||||
site_gitweb = etc_dir.resolve("gitweb_config.perl");
|
||||
|
||||
if (site_path.exists()) {
|
||||
final String[] contents = site_path.list();
|
||||
|
@@ -28,11 +28,12 @@ import org.bouncycastle.openpgp.PGPPublicKey;
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
import org.eclipse.jgit.util.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.security.Security;
|
||||
|
||||
/** Creates the {@link ContactStore} based on the configuration. */
|
||||
@@ -46,7 +47,7 @@ public class ContactStoreModule extends AbstractModule {
|
||||
public ContactStore provideContactStore(@GerritServerConfig final Config config,
|
||||
final SitePaths site, final SchemaFactory<ReviewDb> schema,
|
||||
final ContactStoreConnection.Factory connFactory) {
|
||||
final String url = config.getString("contactstore", null, "url");
|
||||
String url = config.getString("contactstore", null, "url");
|
||||
if (StringUtils.isEmptyOrNull(url)) {
|
||||
return new NoContactStore();
|
||||
}
|
||||
@@ -56,18 +57,18 @@ public class ContactStoreModule extends AbstractModule {
|
||||
+ " needed to encrypt contact information");
|
||||
}
|
||||
|
||||
final URL storeUrl;
|
||||
URL storeUrl;
|
||||
try {
|
||||
storeUrl = new URL(url);
|
||||
} catch (MalformedURLException e) {
|
||||
throw new ProvisionException("Invalid contactstore.url: " + url, e);
|
||||
}
|
||||
|
||||
final String storeAPPSEC = config.getString("contactstore", null, "appsec");
|
||||
final File pubkey = site.contact_information_pub;
|
||||
if (!pubkey.exists()) {
|
||||
String storeAPPSEC = config.getString("contactstore", null, "appsec");
|
||||
Path pubkey = site.contact_information_pub;
|
||||
if (!Files.exists(pubkey)) {
|
||||
throw new ProvisionException("PGP public key file \""
|
||||
+ pubkey.getAbsolutePath() + "\" not found");
|
||||
+ pubkey.toAbsolutePath() + "\" not found");
|
||||
}
|
||||
return new EncryptedContactStore(storeUrl, storeAPPSEC, pubkey, schema,
|
||||
connFactory);
|
||||
|
@@ -42,12 +42,12 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.NoSuchProviderException;
|
||||
import java.security.SecureRandom;
|
||||
@@ -72,7 +72,7 @@ class EncryptedContactStore implements ContactStore {
|
||||
private final ContactStoreConnection.Factory connFactory;
|
||||
|
||||
EncryptedContactStore(final URL storeUrl, final String storeAPPSEC,
|
||||
final File pubKey, final SchemaFactory<ReviewDb> schema,
|
||||
final Path pubKey, final SchemaFactory<ReviewDb> schema,
|
||||
final ContactStoreConnection.Factory connFactory) {
|
||||
this.storeUrl = storeUrl;
|
||||
this.storeAPPSEC = storeAPPSEC;
|
||||
@@ -104,8 +104,8 @@ class EncryptedContactStore implements ContactStore {
|
||||
return true;
|
||||
}
|
||||
|
||||
private static PGPPublicKeyRingCollection readPubRing(final File pub) {
|
||||
try (InputStream fin = new FileInputStream(pub);
|
||||
private static PGPPublicKeyRingCollection readPubRing(Path pub) {
|
||||
try (InputStream fin = Files.newInputStream(pub);
|
||||
InputStream in = PGPUtil.getDecoderStream(fin)) {
|
||||
return new PGPPublicKeyRingCollection(in);
|
||||
} catch (IOException | PGPException e) {
|
||||
|
@@ -27,6 +27,7 @@ import org.eclipse.jgit.util.FS;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
@Singleton
|
||||
@@ -35,8 +36,8 @@ public class DefaultSecureStore extends SecureStore {
|
||||
|
||||
@Inject
|
||||
DefaultSecureStore(SitePaths site) {
|
||||
File secureConfig = new File(site.etc_dir, "secure.config");
|
||||
sec = new FileBasedConfig(secureConfig, FS.DETECTED);
|
||||
Path secureConfig = site.etc_dir.resolve("secure.config");
|
||||
sec = new FileBasedConfig(secureConfig.toFile(), FS.DETECTED);
|
||||
try {
|
||||
sec.load();
|
||||
} catch (Exception e) {
|
||||
|
@@ -38,7 +38,7 @@ public class SitePathsTest {
|
||||
final SitePaths site = new SitePaths(root);
|
||||
assertTrue(site.isNew);
|
||||
assertEquals(root.toFile(), site.site_path);
|
||||
assertEquals(root.resolve("etc").toFile(), site.etc_dir);
|
||||
assertEquals(root.resolve("etc"), site.etc_dir);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Submodule plugins/replication updated: 3973b31f3c...2266d4e2ec
Reference in New Issue
Block a user