Use listeners to manage server startup/shutdown
Instead of enumerating the startup/shutdown sequence inside of the
WebAppInitializer we now use listeners which are bound and managed
by Guice. The listeners are fired in the order they are registered
within the injector modules.
By using Guice we are more easily able to track the need to start
(or gracefully stop) a component alongside its explicit binding
in the injector. We can also conditionally include start or stop
rules by controlling which modules are included in the injection.
Change-Id: I93590c666d46e13fdce9aa05100489f9f6d94615
Signed-off-by: Shawn O. Pearce <sop@google.com>
diff --git a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/Daemon.java b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/Daemon.java
index 558a1b1..ebf582a 100644
--- a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/Daemon.java
+++ b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/Daemon.java
@@ -14,9 +14,9 @@
package com.google.gerrit.pgm;
-import com.google.gerrit.server.cache.CachePool;
+import com.google.gerrit.lifecycle.LifecycleManager;
import com.google.gerrit.server.config.GerritGlobalModule;
-import com.google.gerrit.sshd.SshDaemon;
+import com.google.gerrit.server.config.MasterNodeStartup;
import com.google.gerrit.sshd.SshModule;
import com.google.gerrit.sshd.commands.MasterCommandModule;
import com.google.gerrit.sshd.commands.SlaveCommandModule;
@@ -38,8 +38,10 @@
public int run() throws Exception {
Injector sysInjector = GerritGlobalModule.createInjector();
Injector sshInjector = createSshInjector(sysInjector);
- sysInjector.getInstance(CachePool.class).start();
- sshInjector.getInstance(SshDaemon.class).start();
+
+ final LifecycleManager mgr = new LifecycleManager();
+ mgr.add(sysInjector, sshInjector);
+ mgr.start();
return never();
}
@@ -50,6 +52,7 @@
modules.add(new SlaveCommandModule());
} else {
modules.add(new MasterCommandModule());
+ modules.add(new MasterNodeStartup());
}
return sysInjector.createChildInjector(modules);
}
diff --git a/gerrit-server/src/main/java/com/google/gerrit/lifecycle/LifecycleListener.java b/gerrit-server/src/main/java/com/google/gerrit/lifecycle/LifecycleListener.java
new file mode 100644
index 0000000..e6b06ef
--- /dev/null
+++ b/gerrit-server/src/main/java/com/google/gerrit/lifecycle/LifecycleListener.java
@@ -0,0 +1,26 @@
+// 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.lifecycle;
+
+import java.util.EventListener;
+
+/** Listener interested in server startup and shutdown events. */
+public interface LifecycleListener extends EventListener {
+ /** Invoke when the server is starting. */
+ public void start();
+
+ /** Invoked when the server is stopping. */
+ public void stop();
+}
diff --git a/gerrit-server/src/main/java/com/google/gerrit/lifecycle/LifecycleManager.java b/gerrit-server/src/main/java/com/google/gerrit/lifecycle/LifecycleManager.java
new file mode 100644
index 0000000..6c56978
--- /dev/null
+++ b/gerrit-server/src/main/java/com/google/gerrit/lifecycle/LifecycleManager.java
@@ -0,0 +1,93 @@
+// 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.lifecycle;
+
+import com.google.inject.Binding;
+import com.google.inject.Injector;
+import com.google.inject.TypeLiteral;
+
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+/** Tracks and executes registered {@link LifecycleListener}s. */
+public class LifecycleManager {
+ private final List<Injector> injectors = new ArrayList<Injector>();
+ private LifecycleListener[] listeners;
+
+ /** Add all {@link LifecycleListener}s registered in the Injector. */
+ public void add(final Injector injector) {
+ if (listeners != null) {
+ throw new IllegalStateException("Already started");
+ }
+ injectors.add(injector);
+ }
+
+ /** Add all {@link LifecycleListener}s registered in the Injectors. */
+ public void add(final Injector... injectors) {
+ for (final Injector i : injectors) {
+ add(i);
+ }
+ }
+
+ /** Start all listeners, in the order they were registered. */
+ public void start() {
+ if (listeners == null) {
+ listeners = all();
+
+ for (LifecycleListener obj : listeners) {
+ obj.start();
+ }
+ }
+ }
+
+ /** Stop all listeners, in the reverse order they were registered. */
+ public void stop() {
+ if (listeners != null) {
+ for (int i = listeners.length - 1; 0 <= i; i--) {
+ final LifecycleListener obj = listeners[i];
+ try {
+ obj.stop();
+ } catch (Throwable err) {
+ LoggerFactory.getLogger(obj.getClass()).warn("Failed to stop", err);
+ }
+ }
+
+ listeners = null;
+ }
+ }
+
+ private LifecycleListener[] all() {
+ final Map<LifecycleListener, Boolean> found =
+ new LinkedHashMap<LifecycleListener, Boolean>();
+
+ for (final Injector injector : injectors) {
+ for (final Binding<LifecycleListener> binding : get(injector)) {
+ found.put(binding.getProvider().get(), true);
+ }
+ }
+
+ final LifecycleListener[] a = new LifecycleListener[found.size()];
+ found.keySet().toArray(a);
+ return a;
+ }
+
+ private static List<Binding<LifecycleListener>> get(Injector i) {
+ return i.findBindingsByType(new TypeLiteral<LifecycleListener>() {});
+ }
+}
diff --git a/gerrit-server/src/main/java/com/google/gerrit/lifecycle/LifecycleModule.java b/gerrit-server/src/main/java/com/google/gerrit/lifecycle/LifecycleModule.java
new file mode 100644
index 0000000..dcc8a35
--- /dev/null
+++ b/gerrit-server/src/main/java/com/google/gerrit/lifecycle/LifecycleModule.java
@@ -0,0 +1,28 @@
+package com.google.gerrit.lifecycle;
+
+import com.google.inject.AbstractModule;
+import com.google.inject.Singleton;
+import com.google.inject.binder.LinkedBindingBuilder;
+import com.google.inject.internal.UniqueAnnotations;
+
+import java.lang.annotation.Annotation;
+
+/** Module to support registering a unique LifecyleListener. */
+public abstract class LifecycleModule extends AbstractModule {
+ /**
+ * Create a unique listener binding.
+ * <p>
+ * To create a listener binding use:
+ *
+ * <pre>
+ * listener().to(MyListener.class);
+ * </pre>
+ *
+ * where {@code MyListener} is a {@link Singleton} implementing the
+ * {@link LifecycleListener} interface.
+ */
+ protected LinkedBindingBuilder<LifecycleListener> listener() {
+ final Annotation id = UniqueAnnotations.create();
+ return bind(LifecycleListener.class).annotatedWith(id);
+ }
+}
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/cache/CachePool.java b/gerrit-server/src/main/java/com/google/gerrit/server/cache/CachePool.java
index cee1028..4d438e1 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/cache/CachePool.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/cache/CachePool.java
@@ -17,6 +17,7 @@
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;
+import com.google.gerrit.lifecycle.LifecycleListener;
import com.google.gerrit.server.config.ConfigUtil;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.config.SitePath;
@@ -43,6 +44,25 @@
public class CachePool {
private static final Logger log = LoggerFactory.getLogger(CachePool.class);
+ public static class Lifecycle implements LifecycleListener {
+ private final CachePool cachePool;
+
+ @Inject
+ Lifecycle(final CachePool cachePool) {
+ this.cachePool = cachePool;
+ }
+
+ @Override
+ public void start() {
+ cachePool.start();
+ }
+
+ @Override
+ public void stop() {
+ cachePool.stop();
+ }
+ }
+
private final Config config;
private final File sitePath;
@@ -57,8 +77,7 @@
this.caches = new HashMap<String, CacheProvider<?, ?>>();
}
- /** Start the cache pool. The pool must be started before any access occurs. */
- public void start() {
+ private void start() {
synchronized (lock) {
if (manager != null) {
throw new IllegalStateException("Cache pool has already been started");
@@ -71,8 +90,7 @@
}
}
- /** Stop the cache pool. The pool should be stopped before terminating. */
- public void stop() {
+ private void stop() {
synchronized (lock) {
if (manager != null) {
manager.shutdown();
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/config/GerritGlobalModule.java b/gerrit-server/src/main/java/com/google/gerrit/server/config/GerritGlobalModule.java
index 406a772..89432b3 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/config/GerritGlobalModule.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/config/GerritGlobalModule.java
@@ -18,6 +18,7 @@
import static com.google.inject.Stage.PRODUCTION;
import com.google.gerrit.common.data.ApprovalTypes;
+import com.google.gerrit.lifecycle.LifecycleModule;
import com.google.gerrit.reviewdb.AuthType;
import com.google.gerrit.server.AnonymousUser;
import com.google.gerrit.server.FileTypeRegistry;
@@ -159,5 +160,14 @@
factory(MergeFailSender.Factory.class);
factory(RegisterNewEmailSender.Factory.class);
factory(ReplicationUser.Factory.class);
+
+ install(new LifecycleModule() {
+ @Override
+ protected void configure() {
+ listener().to(GitRepositoryManager.Lifecycle.class);
+ listener().to(CachePool.Lifecycle.class);
+ listener().to(WorkQueue.Lifecycle.class);
+ }
+ });
}
}
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/config/GerritServerConfigProvider.java b/gerrit-server/src/main/java/com/google/gerrit/server/config/GerritServerConfigProvider.java
index a7d134d..8854a60 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/config/GerritServerConfigProvider.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/config/GerritServerConfigProvider.java
@@ -18,13 +18,11 @@
import com.google.inject.Provider;
import com.google.inject.ProvisionException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.FileBasedConfig;
-import org.eclipse.jgit.lib.WindowCache;
-import org.eclipse.jgit.lib.WindowCacheConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
@@ -59,10 +57,6 @@
throw new ProvisionException(e.getMessage(), e);
}
- final WindowCacheConfig c = new WindowCacheConfig();
- c.fromConfig(cfg);
- WindowCache.reconfigure(c);
-
return cfg;
}
}
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/config/MasterNodeStartup.java b/gerrit-server/src/main/java/com/google/gerrit/server/config/MasterNodeStartup.java
new file mode 100644
index 0000000..d2c5005
--- /dev/null
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/config/MasterNodeStartup.java
@@ -0,0 +1,53 @@
+// 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.gerrit.lifecycle.LifecycleListener;
+import com.google.gerrit.lifecycle.LifecycleModule;
+import com.google.gerrit.server.git.PushAllProjectsOp;
+import com.google.gerrit.server.git.ReloadSubmitQueueOp;
+import com.google.inject.Inject;
+
+import java.util.concurrent.TimeUnit;
+
+/** Configuration for a master node in a cluster of servers. */
+public class MasterNodeStartup extends LifecycleModule {
+ @Override
+ public void configure() {
+ listener().to(OnStart.class);
+ }
+
+ static class OnStart implements LifecycleListener {
+ private final PushAllProjectsOp.Factory pushAll;
+ private final ReloadSubmitQueueOp.Factory submit;
+
+ @Inject
+ OnStart(final PushAllProjectsOp.Factory pushAll,
+ final ReloadSubmitQueueOp.Factory submit) {
+ this.pushAll = pushAll;
+ this.submit = submit;
+ }
+
+ @Override
+ public void start() {
+ pushAll.create(null).start(30, TimeUnit.SECONDS);
+ submit.create().start(15, TimeUnit.SECONDS);
+ }
+
+ @Override
+ public void stop() {
+ }
+ }
+}
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/config/ReviewDbDataSourceProvider.java b/gerrit-server/src/main/java/com/google/gerrit/server/config/ReviewDbDataSourceProvider.java
index eed571a..a8c5d3e 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/config/ReviewDbDataSourceProvider.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/config/ReviewDbDataSourceProvider.java
@@ -14,9 +14,11 @@
package com.google.gerrit.server.config;
+import com.google.gerrit.lifecycle.LifecycleListener;
import com.google.gwtorm.jdbc.SimpleDataSource;
import com.google.inject.Provider;
import com.google.inject.ProvisionException;
+import com.google.inject.Singleton;
import java.io.FileInputStream;
import java.io.IOException;
@@ -30,9 +32,31 @@
import javax.sql.DataSource;
/** Provides access to the {@code ReviewDb} DataSource. */
-final class ReviewDbDataSourceProvider implements Provider<DataSource> {
+@Singleton
+final class ReviewDbDataSourceProvider implements Provider<DataSource>,
+ LifecycleListener {
+ private DataSource ds;
+
@Override
- public DataSource get() {
+ public synchronized DataSource get() {
+ if (ds == null) {
+ ds = open();
+ }
+ return ds;
+ }
+
+ @Override
+ public void start() {
+ }
+
+ @Override
+ public synchronized void stop() {
+ if (ds != null) {
+ closeDataSource(ds);
+ }
+ }
+
+ private DataSource open() {
final String dsName = "java:comp/env/jdbc/ReviewDb";
try {
return (DataSource) new InitialContext().lookup(dsName);
@@ -79,4 +103,26 @@
}
return dbprop;
}
+
+ private void closeDataSource(final DataSource ds) {
+ try {
+ Class<?> type = Class.forName("org.apache.commons.dbcp.BasicDataSource");
+ if (type.isInstance(ds)) {
+ type.getMethod("close").invoke(ds);
+ return;
+ }
+ } catch (Throwable bad) {
+ // Oh well, its not a Commons DBCP pooled connection.
+ }
+
+ try {
+ Class<?> type = Class.forName("com.mchange.v2.c3p0.DataSources");
+ if (type.isInstance(ds)) {
+ type.getMethod("destroy", DataSource.class).invoke(null, ds);
+ return;
+ }
+ } catch (Throwable bad) {
+ // Oh well, its not a c3p0 pooled connection.
+ }
+ }
}
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/git/GitRepositoryManager.java b/gerrit-server/src/main/java/com/google/gerrit/server/git/GitRepositoryManager.java
index 8adad05..3168d10 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/git/GitRepositoryManager.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/git/GitRepositoryManager.java
@@ -14,6 +14,7 @@
package com.google.gerrit.server.git;
+import com.google.gerrit.lifecycle.LifecycleListener;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.config.SitePath;
import com.google.inject.Inject;
@@ -27,6 +28,8 @@
import org.eclipse.jgit.lib.LockFile;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.RepositoryCache;
+import org.eclipse.jgit.lib.WindowCache;
+import org.eclipse.jgit.lib.WindowCacheConfig;
import org.eclipse.jgit.lib.RepositoryCache.FileKey;
import java.io.File;
@@ -36,6 +39,27 @@
@Singleton
public class GitRepositoryManager {
private static final Logger log = LoggerFactory.getLogger(GitRepositoryManager.class);
+
+ public static class Lifecycle implements LifecycleListener {
+ private final Config cfg;
+
+ @Inject
+ Lifecycle(@GerritServerConfig final Config cfg) {
+ this.cfg = cfg;
+ }
+
+ @Override
+ public void start() {
+ final WindowCacheConfig c = new WindowCacheConfig();
+ c.fromConfig(cfg);
+ WindowCache.reconfigure(c);
+ }
+
+ @Override
+ public void stop() {
+ }
+ }
+
private final File sitePath;
private final File basepath;
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/git/WorkQueue.java b/gerrit-server/src/main/java/com/google/gerrit/server/git/WorkQueue.java
index 51f5c99..84b1c01 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/git/WorkQueue.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/git/WorkQueue.java
@@ -14,6 +14,8 @@
package com.google.gerrit.server.git;
+import com.google.gerrit.lifecycle.LifecycleListener;
+import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.util.ArrayList;
@@ -35,6 +37,24 @@
/** Delayed execution of tasks using a background thread pool. */
@Singleton
public class WorkQueue {
+ public static class Lifecycle implements LifecycleListener {
+ private final WorkQueue workQueue;
+
+ @Inject
+ Lifecycle(final WorkQueue workQeueue) {
+ this.workQueue = workQeueue;
+ }
+
+ @Override
+ public void start() {
+ }
+
+ @Override
+ public void stop() {
+ workQueue.stop();
+ }
+ }
+
private Executor defaultQueue;
private final CopyOnWriteArrayList<Executor> queues =
new CopyOnWriteArrayList<Executor>();
@@ -65,8 +85,7 @@
return r;
}
- /** Shutdown all queues, aborting any pending tasks that haven't started. */
- public void shutdown() {
+ private void stop() {
for (final Executor p : queues) {
p.shutdown();
boolean isTerminated;
diff --git a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/SshDaemon.java b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/SshDaemon.java
index 3477b53..64a84aa 100644
--- a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/SshDaemon.java
+++ b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/SshDaemon.java
@@ -14,6 +14,7 @@
package com.google.gerrit.sshd;
+import com.google.gerrit.lifecycle.LifecycleListener;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.ssh.SshInfo;
import com.google.inject.Inject;
@@ -103,7 +104,7 @@
* </pre>
*/
@Singleton
-public class SshDaemon extends SshServer implements SshInfo {
+public class SshDaemon extends SshServer implements SshInfo, LifecycleListener {
private static final int IANA_SSH_PORT = 22;
private static final int DEFAULT_PORT = 29418;
@@ -192,7 +193,7 @@
}
@Override
- public synchronized void start() throws IOException {
+ public synchronized void start() {
if (acceptor == null) {
checkConfig();
@@ -201,7 +202,11 @@
handler.setServer(this);
ain.setHandler(handler);
ain.setReuseAddress(reuseAddress);
- ain.bind(listen);
+ try {
+ ain.bind(listen);
+ } catch (IOException e) {
+ throw new IllegalStateException("Cannot bind to " + addressList(), e);
+ }
acceptor = ain;
log.info("Started Gerrit SSHD on " + addressList());
diff --git a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/SshModule.java b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/SshModule.java
index 59d0c32..e3755c0 100644
--- a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/SshModule.java
+++ b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/SshModule.java
@@ -16,6 +16,7 @@
import static com.google.inject.Scopes.SINGLETON;
+import com.google.gerrit.lifecycle.LifecycleModule;
import com.google.gerrit.reviewdb.Account;
import com.google.gerrit.reviewdb.AccountGroup;
import com.google.gerrit.reviewdb.PatchSet;
@@ -76,6 +77,13 @@
bind(KeyPairProvider.class).toProvider(HostKeyProvider.class).in(SINGLETON);
install(new DefaultCommandModule());
+
+ install(new LifecycleModule() {
+ @Override
+ protected void configure() {
+ listener().to(SshDaemon.class);
+ }
+ });
}
private void configureSessionScope() {
diff --git a/gerrit-war/src/main/java/com/google/gerrit/httpd/WebAppInitializer.java b/gerrit-war/src/main/java/com/google/gerrit/httpd/WebAppInitializer.java
index aa4763d..03f4237 100644
--- a/gerrit-war/src/main/java/com/google/gerrit/httpd/WebAppInitializer.java
+++ b/gerrit-war/src/main/java/com/google/gerrit/httpd/WebAppInitializer.java
@@ -16,38 +16,30 @@
import static com.google.inject.Stage.PRODUCTION;
-import com.google.gerrit.server.cache.CachePool;
+import com.google.gerrit.lifecycle.LifecycleManager;
import com.google.gerrit.server.config.CanonicalWebUrlModule;
import com.google.gerrit.server.config.DatabaseModule;
import com.google.gerrit.server.config.GerritGlobalModule;
-import com.google.gerrit.server.git.PushAllProjectsOp;
-import com.google.gerrit.server.git.ReloadSubmitQueueOp;
-import com.google.gerrit.server.git.WorkQueue;
-import com.google.gerrit.sshd.SshDaemon;
+import com.google.gerrit.server.config.MasterNodeStartup;
import com.google.gerrit.sshd.SshModule;
import com.google.gerrit.sshd.commands.MasterCommandModule;
-import com.google.inject.ConfigurationException;
import com.google.inject.CreationException;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Provider;
-import com.google.inject.ProvisionException;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.spi.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
-import java.util.concurrent.TimeUnit;
import javax.servlet.ServletContextEvent;
import javax.servlet.http.HttpServletRequest;
-import javax.sql.DataSource;
/** Configures the web application environment for Gerrit Code Review. */
public class WebAppInitializer extends GuiceServletContextListener {
@@ -58,9 +50,10 @@
private Injector sysInjector;
private Injector webInjector;
private Injector sshInjector;
+ private LifecycleManager manager;
private synchronized void init() {
- if (sysInjector == null) {
+ if (manager == null) {
try {
dbInjector = Guice.createInjector(PRODUCTION, new DatabaseModule());
} catch (CreationException ce) {
@@ -105,12 +98,21 @@
sysInjector.getInstance(HttpCanonicalWebUrlProvider.class)
.setHttpServletRequest(
webInjector.getProvider(HttpServletRequest.class));
+
+ manager = new LifecycleManager();
+ manager.add(dbInjector);
+ manager.add(sysInjector);
+ manager.add(sshInjector);
+ manager.add(webInjector);
}
}
private Injector createSshInjector() {
- return sysInjector.createChildInjector(new SshModule(),
- new MasterCommandModule());
+ final List<Module> modules = new ArrayList<Module>();
+ modules.add(new SshModule());
+ modules.add(new MasterCommandModule());
+ modules.add(new MasterNodeStartup());
+ return sysInjector.createChildInjector(modules);
}
private Injector createWebInjector() {
@@ -129,100 +131,15 @@
public void contextInitialized(final ServletContextEvent event) {
super.contextInitialized(event);
init();
-
- try {
- sysInjector.getInstance(CachePool.class).start();
- } catch (ConfigurationException e) {
- log.error("Unable to start CachePool", e);
- } catch (ProvisionException e) {
- log.error("Unable to start CachePool", e);
- }
-
- try {
- sysInjector.getInstance(PushAllProjectsOp.Factory.class).create(null)
- .start(30, TimeUnit.SECONDS);
- } catch (ConfigurationException e) {
- log.error("Unable to restart replication queue", e);
- } catch (ProvisionException e) {
- log.error("Unable to restart replication queue", e);
- }
-
- try {
- sysInjector.getInstance(ReloadSubmitQueueOp.Factory.class).create()
- .start(15, TimeUnit.SECONDS);
- } catch (ConfigurationException e) {
- log.error("Unable to restart merge queue", e);
- } catch (ProvisionException e) {
- log.error("Unable to restart merge queue", e);
- }
-
- try {
- sshInjector.getInstance(SshDaemon.class).start();
- } catch (ConfigurationException e) {
- log.error("Unable to start SSHD", e);
- } catch (ProvisionException e) {
- log.error("Unable to start SSHD", e);
- } catch (IOException e) {
- log.error("Unable to start SSHD", e);
- }
+ manager.start();
}
@Override
public void contextDestroyed(final ServletContextEvent event) {
- try {
- if (sshInjector != null) {
- sshInjector.getInstance(SshDaemon.class).stop();
- }
- } catch (ConfigurationException e) {
- } catch (ProvisionException e) {
+ if (manager != null) {
+ manager.stop();
+ manager = null;
}
-
- try {
- if (sysInjector != null) {
- sysInjector.getInstance(WorkQueue.class).shutdown();
- }
- } catch (ConfigurationException e) {
- } catch (ProvisionException e) {
- }
-
- try {
- if (sysInjector != null) {
- sysInjector.getInstance(CachePool.class).stop();
- }
- } catch (ConfigurationException e) {
- } catch (ProvisionException e) {
- }
-
- try {
- if (dbInjector != null) {
- closeDataSource(dbInjector.getInstance(DatabaseModule.DS));
- }
- } catch (ConfigurationException ce) {
- } catch (ProvisionException ce) {
- }
-
super.contextDestroyed(event);
}
-
- private void closeDataSource(final DataSource ds) {
- try {
- Class<?> type = Class.forName("org.apache.commons.dbcp.BasicDataSource");
- if (type.isInstance(ds)) {
- type.getMethod("close").invoke(ds);
- return;
- }
- } catch (Throwable bad) {
- // Oh well, its not a Commons DBCP pooled connection.
- }
-
- try {
- Class<?> type = Class.forName("com.mchange.v2.c3p0.DataSources");
- if (type.isInstance(ds)) {
- type.getMethod("destroy", DataSource.class).invoke(null, ds);
- return;
- }
- } catch (Throwable bad) {
- // Oh well, its not a c3p0 pooled connection.
- }
- }
}