mobile/android/base/sync/synchronizer/ServerLocalSynchronizerSession.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/sync/synchronizer/ServerLocalSynchronizerSession.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,76 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.sync.synchronizer;
     1.9 +
    1.10 +import org.mozilla.gecko.background.common.log.Logger;
    1.11 +import org.mozilla.gecko.sync.repositories.FetchFailedException;
    1.12 +import org.mozilla.gecko.sync.repositories.StoreFailedException;
    1.13 +
    1.14 +/**
    1.15 + * A <code>SynchronizerSession</code> designed to be used between a remote
    1.16 + * server and a local repository.
    1.17 + * <p>
    1.18 + * Handles failure cases as follows (in the order they will occur during a sync):
    1.19 + * <ul>
    1.20 + * <li>Remote fetch failures abort.</li>
    1.21 + * <li>Local store failures are ignored.</li>
    1.22 + * <li>Local fetch failures abort.</li>
    1.23 + * <li>Remote store failures abort.</li>
    1.24 + * </ul>
    1.25 + */
    1.26 +public class ServerLocalSynchronizerSession extends SynchronizerSession {
    1.27 +  protected static final String LOG_TAG = "ServLocSynchronizerSess";
    1.28 +
    1.29 +  public ServerLocalSynchronizerSession(Synchronizer synchronizer, SynchronizerSessionDelegate delegate) {
    1.30 +    super(synchronizer, delegate);
    1.31 +  }
    1.32 +
    1.33 +  public void onFirstFlowCompleted(RecordsChannel recordsChannel, long fetchEnd, long storeEnd) {
    1.34 +    // Fetch failures always abort.
    1.35 +    int numRemoteFetchFailed = recordsChannel.getFetchFailureCount();
    1.36 +    if (numRemoteFetchFailed > 0) {
    1.37 +      final String message = "Got " + numRemoteFetchFailed + " failures fetching remote records!";
    1.38 +      Logger.warn(LOG_TAG, message + " Aborting session.");
    1.39 +      delegate.onSynchronizeFailed(this, new FetchFailedException(), message);
    1.40 +      return;
    1.41 +    }
    1.42 +    Logger.trace(LOG_TAG, "No failures fetching remote records.");
    1.43 +
    1.44 +    // Local store failures are ignored.
    1.45 +    int numLocalStoreFailed = recordsChannel.getStoreFailureCount();
    1.46 +    if (numLocalStoreFailed > 0) {
    1.47 +      final String message = "Got " + numLocalStoreFailed + " failures storing local records!";
    1.48 +      Logger.warn(LOG_TAG, message + " Ignoring local store failures and continuing synchronizer session.");
    1.49 +    } else {
    1.50 +      Logger.trace(LOG_TAG, "No failures storing local records.");
    1.51 +    }
    1.52 +
    1.53 +    super.onFirstFlowCompleted(recordsChannel, fetchEnd, storeEnd);
    1.54 +  }
    1.55 +
    1.56 +  public void onSecondFlowCompleted(RecordsChannel recordsChannel, long fetchEnd, long storeEnd) {
    1.57 +    // Fetch failures always abort.
    1.58 +    int numLocalFetchFailed = recordsChannel.getFetchFailureCount();
    1.59 +    if (numLocalFetchFailed > 0) {
    1.60 +      final String message = "Got " + numLocalFetchFailed + " failures fetching local records!";
    1.61 +      Logger.warn(LOG_TAG, message + " Aborting session.");
    1.62 +      delegate.onSynchronizeFailed(this, new FetchFailedException(), message);
    1.63 +      return;
    1.64 +    }
    1.65 +    Logger.trace(LOG_TAG, "No failures fetching local records.");
    1.66 +
    1.67 +    // Remote store failures abort!
    1.68 +    int numRemoteStoreFailed = recordsChannel.getStoreFailureCount();
    1.69 +    if (numRemoteStoreFailed > 0) {
    1.70 +      final String message = "Got " + numRemoteStoreFailed + " failures storing remote records!";
    1.71 +      Logger.warn(LOG_TAG, message + " Aborting session.");
    1.72 +      delegate.onSynchronizeFailed(this, new StoreFailedException(), message);
    1.73 +      return;
    1.74 +    }
    1.75 +    Logger.trace(LOG_TAG, "No failures storing remote records.");
    1.76 +
    1.77 +    super.onSecondFlowCompleted(recordsChannel, fetchEnd, storeEnd);
    1.78 +  }
    1.79 +}

mercurial