Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.sync.middleware; |
michael@0 | 6 | |
michael@0 | 7 | import java.util.concurrent.ExecutorService; |
michael@0 | 8 | |
michael@0 | 9 | import org.mozilla.gecko.background.common.log.Logger; |
michael@0 | 10 | import org.mozilla.gecko.sync.repositories.InactiveSessionException; |
michael@0 | 11 | import org.mozilla.gecko.sync.repositories.InvalidSessionTransitionException; |
michael@0 | 12 | import org.mozilla.gecko.sync.repositories.RepositorySession; |
michael@0 | 13 | import org.mozilla.gecko.sync.repositories.RepositorySessionBundle; |
michael@0 | 14 | import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionBeginDelegate; |
michael@0 | 15 | import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionFinishDelegate; |
michael@0 | 16 | import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionGuidsSinceDelegate; |
michael@0 | 17 | import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionWipeDelegate; |
michael@0 | 18 | |
michael@0 | 19 | public abstract class MiddlewareRepositorySession extends RepositorySession { |
michael@0 | 20 | private static final String LOG_TAG = "MiddlewareSession"; |
michael@0 | 21 | protected final RepositorySession inner; |
michael@0 | 22 | |
michael@0 | 23 | public MiddlewareRepositorySession(RepositorySession innerSession, MiddlewareRepository repository) { |
michael@0 | 24 | super(repository); |
michael@0 | 25 | this.inner = innerSession; |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | @Override |
michael@0 | 29 | public void wipe(RepositorySessionWipeDelegate delegate) { |
michael@0 | 30 | inner.wipe(delegate); |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | public class MiddlewareRepositorySessionBeginDelegate implements RepositorySessionBeginDelegate { |
michael@0 | 34 | |
michael@0 | 35 | private MiddlewareRepositorySession outerSession; |
michael@0 | 36 | private RepositorySessionBeginDelegate next; |
michael@0 | 37 | |
michael@0 | 38 | public MiddlewareRepositorySessionBeginDelegate(MiddlewareRepositorySession outerSession, RepositorySessionBeginDelegate next) { |
michael@0 | 39 | this.outerSession = outerSession; |
michael@0 | 40 | this.next = next; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | @Override |
michael@0 | 44 | public void onBeginFailed(Exception ex) { |
michael@0 | 45 | next.onBeginFailed(ex); |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | @Override |
michael@0 | 49 | public void onBeginSucceeded(RepositorySession session) { |
michael@0 | 50 | next.onBeginSucceeded(outerSession); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | @Override |
michael@0 | 54 | public RepositorySessionBeginDelegate deferredBeginDelegate(ExecutorService executor) { |
michael@0 | 55 | final RepositorySessionBeginDelegate deferred = next.deferredBeginDelegate(executor); |
michael@0 | 56 | return new RepositorySessionBeginDelegate() { |
michael@0 | 57 | @Override |
michael@0 | 58 | public void onBeginSucceeded(RepositorySession session) { |
michael@0 | 59 | if (inner != session) { |
michael@0 | 60 | Logger.warn(LOG_TAG, "Got onBeginSucceeded for session " + session + ", not our inner session!"); |
michael@0 | 61 | } |
michael@0 | 62 | deferred.onBeginSucceeded(outerSession); |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | @Override |
michael@0 | 66 | public void onBeginFailed(Exception ex) { |
michael@0 | 67 | deferred.onBeginFailed(ex); |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | @Override |
michael@0 | 71 | public RepositorySessionBeginDelegate deferredBeginDelegate(ExecutorService executor) { |
michael@0 | 72 | return this; |
michael@0 | 73 | } |
michael@0 | 74 | }; |
michael@0 | 75 | } |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | public void begin(RepositorySessionBeginDelegate delegate) throws InvalidSessionTransitionException { |
michael@0 | 79 | inner.begin(new MiddlewareRepositorySessionBeginDelegate(this, delegate)); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | public class MiddlewareRepositorySessionFinishDelegate implements RepositorySessionFinishDelegate { |
michael@0 | 83 | private final MiddlewareRepositorySession outerSession; |
michael@0 | 84 | private final RepositorySessionFinishDelegate next; |
michael@0 | 85 | |
michael@0 | 86 | public MiddlewareRepositorySessionFinishDelegate(MiddlewareRepositorySession outerSession, RepositorySessionFinishDelegate next) { |
michael@0 | 87 | this.outerSession = outerSession; |
michael@0 | 88 | this.next = next; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | @Override |
michael@0 | 92 | public void onFinishFailed(Exception ex) { |
michael@0 | 93 | next.onFinishFailed(ex); |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | @Override |
michael@0 | 97 | public void onFinishSucceeded(RepositorySession session, RepositorySessionBundle bundle) { |
michael@0 | 98 | next.onFinishSucceeded(outerSession, bundle); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | @Override |
michael@0 | 102 | public RepositorySessionFinishDelegate deferredFinishDelegate(ExecutorService executor) { |
michael@0 | 103 | return this; |
michael@0 | 104 | } |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | @Override |
michael@0 | 108 | public void finish(RepositorySessionFinishDelegate delegate) throws InactiveSessionException { |
michael@0 | 109 | inner.finish(new MiddlewareRepositorySessionFinishDelegate(this, delegate)); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | |
michael@0 | 113 | @Override |
michael@0 | 114 | public synchronized void ensureActive() throws InactiveSessionException { |
michael@0 | 115 | inner.ensureActive(); |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | @Override |
michael@0 | 119 | public synchronized boolean isActive() { |
michael@0 | 120 | return inner.isActive(); |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | @Override |
michael@0 | 124 | public synchronized SessionStatus getStatus() { |
michael@0 | 125 | return inner.getStatus(); |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | @Override |
michael@0 | 129 | public synchronized void setStatus(SessionStatus status) { |
michael@0 | 130 | inner.setStatus(status); |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | @Override |
michael@0 | 134 | public synchronized void transitionFrom(SessionStatus from, SessionStatus to) |
michael@0 | 135 | throws InvalidSessionTransitionException { |
michael@0 | 136 | inner.transitionFrom(from, to); |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | @Override |
michael@0 | 140 | public void abort() { |
michael@0 | 141 | inner.abort(); |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | @Override |
michael@0 | 145 | public void abort(RepositorySessionFinishDelegate delegate) { |
michael@0 | 146 | inner.abort(new MiddlewareRepositorySessionFinishDelegate(this, delegate)); |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | @Override |
michael@0 | 150 | public void guidsSince(long timestamp, RepositorySessionGuidsSinceDelegate delegate) { |
michael@0 | 151 | // TODO: need to do anything here? |
michael@0 | 152 | inner.guidsSince(timestamp, delegate); |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | @Override |
michael@0 | 156 | public void storeDone() { |
michael@0 | 157 | inner.storeDone(); |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | @Override |
michael@0 | 161 | public void storeDone(long storeEnd) { |
michael@0 | 162 | inner.storeDone(storeEnd); |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | @Override |
michael@0 | 166 | public boolean shouldSkip() { |
michael@0 | 167 | return inner.shouldSkip(); |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | @Override |
michael@0 | 171 | public boolean dataAvailable() { |
michael@0 | 172 | return inner.dataAvailable(); |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | @Override |
michael@0 | 176 | public void unbundle(RepositorySessionBundle bundle) { |
michael@0 | 177 | inner.unbundle(bundle); |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | @Override |
michael@0 | 181 | public long getLastSyncTimestamp() { |
michael@0 | 182 | return inner.getLastSyncTimestamp(); |
michael@0 | 183 | } |
michael@0 | 184 | } |