/** * Synchronously wipe the server. * * <p>Logs and re-throws an exception on failure. */ public void wipeServer() throws Exception { final WipeWaiter monitor = new WipeWaiter(); final Runnable doWipe = new Runnable() { @Override public void run() { wipeServer( session, new WipeServerDelegate() { @Override public void onWiped(long timestamp) { synchronized (monitor) { monitor.notify(); } } @Override public void onWipeFailed(Exception e) { synchronized (monitor) { monitor.notify(e, false); } } }); } }; final Thread wiping = new Thread(doWipe); synchronized (monitor) { wiping.start(); try { monitor.wait(); } catch (InterruptedException e) { Logger.error(LOG_TAG, "Server wipe interrupted."); } } if (!monitor.wipeSucceeded) { Logger.error(LOG_TAG, "Failed to wipe server."); throw monitor.error; } Logger.info(LOG_TAG, "Wiping server complete."); }
/** * Synchronously wipe this stage by instantiating a local repository session and wiping that. * * <p>Logs and re-throws an exception on failure. */ @Override public void wipeLocal() throws Exception { // Reset, then clear data. this.resetLocal(); final WipeWaiter monitor = new WipeWaiter(); final Context context = session.getContext(); final Repository r = this.getLocalRepository(); final Runnable doWipe = new Runnable() { @Override public void run() { r.createSession( new RepositorySessionCreationDelegate() { @Override public void onSessionCreated(final RepositorySession session) { try { session.begin( new RepositorySessionBeginDelegate() { @Override public void onBeginSucceeded(final RepositorySession session) { session.wipe( new RepositorySessionWipeDelegate() { @Override public void onWipeSucceeded() { try { session.finish( new RepositorySessionFinishDelegate() { @Override public void onFinishSucceeded( RepositorySession session, RepositorySessionBundle bundle) { // Hurrah. synchronized (monitor) { monitor.notify(); } } @Override public void onFinishFailed(Exception ex) { // Assume that no finish => no wipe. synchronized (monitor) { monitor.notify(ex, true); } } @Override public RepositorySessionFinishDelegate deferredFinishDelegate(ExecutorService executor) { return this; } }); } catch (InactiveSessionException e) { // Cannot happen. Call for safety. synchronized (monitor) { monitor.notify(e, true); } } } @Override public void onWipeFailed(Exception ex) { session.abort(); synchronized (monitor) { monitor.notify(ex, true); } } @Override public RepositorySessionWipeDelegate deferredWipeDelegate( ExecutorService executor) { return this; } }); } @Override public void onBeginFailed(Exception ex) { session.abort(); synchronized (monitor) { monitor.notify(ex, true); } } @Override public RepositorySessionBeginDelegate deferredBeginDelegate( ExecutorService executor) { return this; } }); } catch (InvalidSessionTransitionException e) { session.abort(); synchronized (monitor) { monitor.notify(e, true); } } } @Override public void onSessionCreateFailed(Exception ex) { synchronized (monitor) { monitor.notify(ex, false); } } @Override public RepositorySessionCreationDelegate deferredCreationDelegate() { return this; } }, context); } }; final Thread wiping = new Thread(doWipe); synchronized (monitor) { wiping.start(); try { monitor.wait(); } catch (InterruptedException e) { Logger.error(LOG_TAG, "Wipe interrupted."); } } if (!monitor.sessionSucceeded) { Logger.error(LOG_TAG, "Failed to create session for wipe."); throw monitor.error; } if (!monitor.wipeSucceeded) { Logger.error(LOG_TAG, "Failed to wipe session."); throw monitor.error; } Logger.info(LOG_TAG, "Wiping stage complete."); }