Changed the root package from net.sf.briar to org.briarproject.

This commit is contained in:
akwizgran
2014-01-08 16:18:30 +00:00
parent dce70f487c
commit 832476412c
427 changed files with 2507 additions and 2507 deletions

View File

@@ -0,0 +1,111 @@
package org.briarproject.lifecycle;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import java.io.IOException;
import java.util.Collection;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.logging.Logger;
import javax.inject.Inject;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.lifecycle.Service;
class LifecycleManagerImpl implements LifecycleManager {
private static final Logger LOG =
Logger.getLogger(LifecycleManagerImpl.class.getName());
private final DatabaseComponent db;
private final Collection<Service> services;
private final Collection<ExecutorService> executors;
private final CountDownLatch dbLatch = new CountDownLatch(1);
private final CountDownLatch startupLatch = new CountDownLatch(1);
private final CountDownLatch shutdownLatch = new CountDownLatch(1);
@Inject
LifecycleManagerImpl(DatabaseComponent db) {
this.db = db;
services = new CopyOnWriteArrayList<Service>();
executors = new CopyOnWriteArrayList<ExecutorService>();
}
public void register(Service s) {
if(LOG.isLoggable(INFO))
LOG.info("Registering service " + s.getClass().getName());
services.add(s);
}
public void registerForShutdown(ExecutorService e) {
if(LOG.isLoggable(INFO))
LOG.info("Registering executor " + e.getClass().getName());
executors.add(e);
}
public void startServices() {
try {
if(LOG.isLoggable(INFO)) LOG.info("Starting");
boolean reopened = db.open();
if(LOG.isLoggable(INFO)) {
if(reopened) LOG.info("Database reopened");
else LOG.info("Database created");
}
dbLatch.countDown();
for(Service s : services) {
boolean started = s.start();
if(LOG.isLoggable(INFO)) {
String name = s.getClass().getName();
if(started) LOG.info("Service started: " + name);
else LOG.info("Service failed to start: " + name);
}
}
startupLatch.countDown();
} catch(DbException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
public void stopServices() {
try {
if(LOG.isLoggable(INFO)) LOG.info("Shutting down");
for(Service s : services) {
boolean stopped = s.stop();
if(LOG.isLoggable(INFO)) {
String name = s.getClass().getName();
if(stopped) LOG.info("Service stopped: " + name);
else LOG.warning("Service failed to stop: " + name);
}
}
for(ExecutorService e : executors) e.shutdownNow();
if(LOG.isLoggable(INFO))
LOG.info(executors.size() + " executors shut down");
db.close();
if(LOG.isLoggable(INFO)) LOG.info("Database closed");
shutdownLatch.countDown();
} catch(DbException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
public void waitForDatabase() throws InterruptedException {
dbLatch.await();
}
public void waitForStartup() throws InterruptedException {
startupLatch.await();
}
public void waitForShutdown() throws InterruptedException {
shutdownLatch.await();
}
}

View File

@@ -0,0 +1,18 @@
package org.briarproject.lifecycle;
import javax.inject.Singleton;
import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.lifecycle.ShutdownManager;
import com.google.inject.AbstractModule;
public class LifecycleModule extends AbstractModule {
protected void configure() {
bind(LifecycleManager.class).to(
LifecycleManagerImpl.class).in(Singleton.class);
bind(ShutdownManager.class).to(
ShutdownManagerImpl.class).in(Singleton.class);
}
}

View File

@@ -0,0 +1,35 @@
package org.briarproject.lifecycle;
import java.util.HashMap;
import java.util.Map;
import org.briarproject.api.lifecycle.ShutdownManager;
class ShutdownManagerImpl implements ShutdownManager {
protected final Map<Integer, Thread> hooks; // Locking: this
private int nextHandle = 0; // Locking: this
ShutdownManagerImpl() {
hooks = new HashMap<Integer, Thread>();
}
public synchronized int addShutdownHook(Runnable r) {
int handle = nextHandle++;
Thread hook = createThread(r);
hooks.put(handle, hook);
Runtime.getRuntime().addShutdownHook(hook);
return handle;
}
protected Thread createThread(Runnable r) {
return new Thread(r, "ShutdownManager");
}
public synchronized boolean removeShutdownHook(int handle) {
Thread hook = hooks.remove(handle);
if(hook == null) return false;
else return Runtime.getRuntime().removeShutdownHook(hook);
}
}