Load gerrit.config through Guice injection
Rather than loading it in GerritServer we can load it directly through Guice, making it available as a singleton to anyone who wants access. Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
@@ -21,13 +21,13 @@ import com.google.gerrit.client.reviewdb.ReviewDb;
|
||||
import com.google.gerrit.client.reviewdb.SystemConfig;
|
||||
import com.google.gerrit.client.reviewdb.SystemConfig.LoginType;
|
||||
import com.google.gerrit.client.rpc.Common;
|
||||
import com.google.gerrit.server.config.GerritServerConfig;
|
||||
import com.google.gerrit.server.config.SitePath;
|
||||
import com.google.gerrit.server.mail.EmailException;
|
||||
import com.google.gerrit.server.patch.DiffCacheEntryFactory;
|
||||
import com.google.gerrit.server.ssh.SshKeyCacheEntryFactory;
|
||||
import com.google.gwtjsonrpc.server.SignedToken;
|
||||
import com.google.gwtjsonrpc.server.XsrfException;
|
||||
import com.google.gwtorm.client.OrmException;
|
||||
import com.google.gwtorm.jdbc.Database;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
@@ -46,20 +46,15 @@ import org.apache.commons.net.smtp.SMTPClient;
|
||||
import org.apache.commons.net.smtp.SMTPReply;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.spearce.jgit.errors.ConfigInvalidException;
|
||||
import org.spearce.jgit.errors.RepositoryNotFoundException;
|
||||
import org.spearce.jgit.lib.Config;
|
||||
import org.spearce.jgit.lib.FileBasedConfig;
|
||||
import org.spearce.jgit.lib.PersonIdent;
|
||||
import org.spearce.jgit.lib.Repository;
|
||||
import org.spearce.jgit.lib.RepositoryCache;
|
||||
import org.spearce.jgit.lib.UserConfig;
|
||||
import org.spearce.jgit.lib.WindowCache;
|
||||
import org.spearce.jgit.lib.WindowCacheConfig;
|
||||
import org.spearce.jgit.lib.RepositoryCache.FileKey;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
@@ -103,7 +98,7 @@ public class GerritServer {
|
||||
|
||||
private final Database<ReviewDb> db;
|
||||
private final File sitePath;
|
||||
private final FileBasedConfig gerritConfigFile;
|
||||
private final Config gerritConfigFile;
|
||||
private final int sessionAge;
|
||||
private final SignedToken xsrf;
|
||||
private final SignedToken account;
|
||||
@@ -114,23 +109,12 @@ public class GerritServer {
|
||||
|
||||
@Inject
|
||||
GerritServer(final Database<ReviewDb> database, final SystemConfig sConfig,
|
||||
@SitePath final File path) throws OrmException, XsrfException {
|
||||
@SitePath final File path, @GerritServerConfig final Config cfg)
|
||||
throws XsrfException {
|
||||
db = database;
|
||||
sitePath = path;
|
||||
|
||||
final File cfgLoc = new File(sitePath, "gerrit.config");
|
||||
gerritConfigFile = new FileBasedConfig(cfgLoc);
|
||||
try {
|
||||
gerritConfigFile.load();
|
||||
} catch (FileNotFoundException e) {
|
||||
log.info("No " + cfgLoc.getAbsolutePath() + "; assuming defaults");
|
||||
} catch (ConfigInvalidException e) {
|
||||
throw new OrmException("Cannot read " + cfgLoc.getAbsolutePath(), e);
|
||||
} catch (IOException e) {
|
||||
throw new OrmException("Cannot read " + cfgLoc.getAbsolutePath(), e);
|
||||
}
|
||||
reconfigureWindowCache();
|
||||
sessionAge = gerritConfigFile.getInt("auth", "maxsessionage", 12 * 60) * 60;
|
||||
gerritConfigFile = cfg;
|
||||
sessionAge = cfg.getInt("auth", "maxsessionage", 12 * 60) * 60;
|
||||
|
||||
xsrf = new SignedToken(getSessionAge(), sConfig.xsrfPrivateKey);
|
||||
|
||||
@@ -147,8 +131,7 @@ public class GerritServer {
|
||||
account = new SignedToken(accountCookieAge, sConfig.accountPrivateKey);
|
||||
emailReg = new SignedToken(5 * 24 * 60 * 60, sConfig.accountPrivateKey);
|
||||
|
||||
final String basePath =
|
||||
getGerritConfig().getString("gerrit", null, "basepath");
|
||||
final String basePath = cfg.getString("gerrit", null, "basepath");
|
||||
if (basePath != null) {
|
||||
File root = new File(basePath);
|
||||
if (!root.isAbsolute()) {
|
||||
@@ -341,12 +324,6 @@ public class GerritServer {
|
||||
return client;
|
||||
}
|
||||
|
||||
private void reconfigureWindowCache() {
|
||||
final WindowCacheConfig c = new WindowCacheConfig();
|
||||
c.fromConfig(gerritConfigFile);
|
||||
WindowCache.reconfigure(c);
|
||||
}
|
||||
|
||||
/** Time (in seconds) that user sessions stay "signed in". */
|
||||
public int getSessionAge() {
|
||||
return sessionAge;
|
||||
|
||||
@@ -23,6 +23,8 @@ import com.google.gerrit.git.ChangeMergeQueue;
|
||||
import com.google.gerrit.git.MergeQueue;
|
||||
import com.google.gerrit.git.PushReplication;
|
||||
import com.google.gerrit.git.ReplicationQueue;
|
||||
import com.google.gerrit.server.config.GerritServerConfig;
|
||||
import com.google.gerrit.server.config.GerritServerConfigProvider;
|
||||
import com.google.gerrit.server.config.SitePath;
|
||||
import com.google.gerrit.server.config.SitePathProvider;
|
||||
import com.google.gwtorm.client.SchemaFactory;
|
||||
@@ -32,6 +34,8 @@ import com.google.inject.Key;
|
||||
import com.google.inject.TypeLiteral;
|
||||
import com.google.inject.name.Names;
|
||||
|
||||
import org.spearce.jgit.lib.Config;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
@@ -53,6 +57,8 @@ public class GerritServerModule extends AbstractModule {
|
||||
SINGLETON);
|
||||
bind(File.class).annotatedWith(SitePath.class).toProvider(
|
||||
SitePathProvider.class);
|
||||
bind(Config.class).annotatedWith(GerritServerConfig.class).toProvider(
|
||||
GerritServerConfigProvider.class).in(SINGLETON);
|
||||
|
||||
bind(GerritServer.class);
|
||||
bind(ContactStore.class).toProvider(EncryptedContactStoreProvider.class);
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
// Copyright (C) 2009 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.server.config;
|
||||
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
import com.google.inject.BindingAnnotation;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
|
||||
/**
|
||||
* Marker on {@link org.spearce.jgit.lib.Config} holding {@code gerrit.config} .
|
||||
* <p>
|
||||
* The {@code gerrit.config} file contains almost all site-wide configuration
|
||||
* settings for the Gerrit Code Review server.
|
||||
*/
|
||||
@Retention(RUNTIME)
|
||||
@BindingAnnotation
|
||||
public @interface GerritServerConfig {
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
// Copyright (C) 2009 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.server.config;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
import com.google.inject.ProvisionException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.spearce.jgit.errors.ConfigInvalidException;
|
||||
import org.spearce.jgit.lib.Config;
|
||||
import org.spearce.jgit.lib.FileBasedConfig;
|
||||
import org.spearce.jgit.lib.WindowCache;
|
||||
import org.spearce.jgit.lib.WindowCacheConfig;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/** Provides {@link Config} annotated with {@link GerritServerConfig}. */
|
||||
public class GerritServerConfigProvider implements Provider<Config> {
|
||||
private static final Logger log =
|
||||
LoggerFactory.getLogger(GerritServerConfigProvider.class);
|
||||
|
||||
private final File sitePath;
|
||||
|
||||
@Inject
|
||||
GerritServerConfigProvider(@SitePath final File path) {
|
||||
sitePath = path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Config get() {
|
||||
final File cfgPath = new File(sitePath, "gerrit.config");
|
||||
final FileBasedConfig cfg = new FileBasedConfig(sitePath);
|
||||
|
||||
if (!cfg.getFile().exists()) {
|
||||
log.info("No " + cfgPath.getAbsolutePath() + "; assuming defaults");
|
||||
return cfg;
|
||||
}
|
||||
|
||||
try {
|
||||
cfg.load();
|
||||
} catch (IOException e) {
|
||||
throw new ProvisionException(e.getMessage(), e);
|
||||
} catch (ConfigInvalidException e) {
|
||||
throw new ProvisionException(e.getMessage(), e);
|
||||
}
|
||||
|
||||
final WindowCacheConfig c = new WindowCacheConfig();
|
||||
c.fromConfig(cfg);
|
||||
WindowCache.reconfigure(c);
|
||||
|
||||
return cfg;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user