Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / cacheinvalidation / src / java / com / google / ipc / invalidation / ticl / CheckingInvalidationListener.java
1 /*
2  * Copyright 2011 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.ipc.invalidation.ticl;
18
19 import static com.google.ipc.invalidation.external.client.SystemResources.Scheduler.NO_DELAY;
20
21 import com.google.ipc.invalidation.external.client.InvalidationClient;
22 import com.google.ipc.invalidation.external.client.InvalidationListener;
23 import com.google.ipc.invalidation.external.client.SystemResources.Logger;
24 import com.google.ipc.invalidation.external.client.SystemResources.Scheduler;
25 import com.google.ipc.invalidation.external.client.types.AckHandle;
26 import com.google.ipc.invalidation.external.client.types.ErrorInfo;
27 import com.google.ipc.invalidation.external.client.types.Invalidation;
28 import com.google.ipc.invalidation.external.client.types.ObjectId;
29 import com.google.ipc.invalidation.ticl.Statistics.ListenerEventType;
30 import com.google.ipc.invalidation.util.NamedRunnable;
31 import com.google.ipc.invalidation.util.Preconditions;
32
33
34 /**
35  * {@link InvalidationListener} wrapper that ensures that a delegate listener is called on the
36  * proper thread and calls the listener method on the listener thread.
37  *
38  */
39 class CheckingInvalidationListener implements InvalidationListener {
40
41   /** The actual listener to which this listener delegates. */
42   private final InvalidationListener delegate;
43
44   /** The scheduler for scheduling internal events in the library. */
45   private final Scheduler internalScheduler;
46
47   /** The scheduler for scheduling events for the delegate. */
48   private final Scheduler listenerScheduler;
49
50   /** Statistics objects to track number of sent messages, etc. */
51   private Statistics statistics;
52
53   private final Logger logger;
54
55   CheckingInvalidationListener(InvalidationListener delegate, Scheduler internalScheduler,
56       Scheduler listenerScheduler, Logger logger) {
57     this.delegate = Preconditions.checkNotNull(delegate, "Delegate cannot be null");
58     this.internalScheduler = Preconditions.checkNotNull(internalScheduler,
59         "Internal scheduler cannot be null");
60     this.listenerScheduler = Preconditions.checkNotNull(listenerScheduler,
61         "Listener scheduler cannot be null");
62     this.logger = Preconditions.checkNotNull(logger, "Logger cannot be null");
63   }
64
65   void setStatistics(Statistics statistics) {
66     this.statistics = Preconditions.checkNotNull(statistics, "Statistics cannot be null");
67   }
68
69   @Override
70   public void invalidate(final InvalidationClient client, final Invalidation invalidation,
71       final AckHandle ackHandle) {
72     Preconditions.checkState(internalScheduler.isRunningOnThread(), "Not on internal thread");
73     Preconditions.checkNotNull(ackHandle);
74     listenerScheduler.schedule(NO_DELAY, new NamedRunnable("CheckingInvalListener.invalidate") {
75       @Override
76       public void run() {
77         statistics.recordListenerEvent(ListenerEventType.INVALIDATE);
78         delegate.invalidate(client, invalidation, ackHandle);
79       }
80     });
81   }
82
83   @Override
84   public void invalidateUnknownVersion(final InvalidationClient client, final ObjectId objectId,
85       final AckHandle ackHandle) {
86     Preconditions.checkState(internalScheduler.isRunningOnThread(), "Not on internal thread");
87     Preconditions.checkNotNull(ackHandle);
88     listenerScheduler.schedule(NO_DELAY,
89         new NamedRunnable("CheckingInvalListener.invalidateUnknownVersion") {
90       @Override
91       public void run() {
92         statistics.recordListenerEvent(ListenerEventType.INVALIDATE_UNKNOWN);
93         delegate.invalidateUnknownVersion(client, objectId, ackHandle);
94       }
95     });
96   }
97
98   @Override
99   public void invalidateAll(final InvalidationClient client, final AckHandle ackHandle) {
100     Preconditions.checkState(internalScheduler.isRunningOnThread(), "Not on internal thread");
101     Preconditions.checkNotNull(ackHandle);
102     listenerScheduler.schedule(NO_DELAY, new NamedRunnable("CheckingInvalListener.invalidateAll") {
103       @Override
104       public void run() {
105         statistics.recordListenerEvent(ListenerEventType.INVALIDATE_ALL);
106         delegate.invalidateAll(client, ackHandle);
107       }
108     });
109   }
110
111   @Override
112   public void informRegistrationFailure(final InvalidationClient client, final ObjectId objectId,
113       final boolean isTransient, final String errorMessage) {
114     Preconditions.checkState(internalScheduler.isRunningOnThread(), "Not on internal thread");
115     listenerScheduler.schedule(NO_DELAY, new NamedRunnable("CheckingInvalListener.regFailure") {
116       @Override
117       public void run() {
118         statistics.recordListenerEvent(ListenerEventType.INFORM_REGISTRATION_FAILURE);
119         delegate.informRegistrationFailure(client, objectId, isTransient, errorMessage);
120       }
121     });
122   }
123
124   @Override
125   public void informRegistrationStatus(final InvalidationClient client, final ObjectId objectId,
126       final RegistrationState regState) {
127     Preconditions.checkState(internalScheduler.isRunningOnThread(), "Not on internal thread");
128     listenerScheduler.schedule(NO_DELAY, new NamedRunnable("CheckingInvalListener.regStatus") {
129       @Override
130       public void run() {
131         statistics.recordListenerEvent(ListenerEventType.INFORM_REGISTRATION_STATUS);
132         delegate.informRegistrationStatus(client, objectId, regState);
133       }
134     });
135   }
136
137   @Override
138   public void reissueRegistrations(final InvalidationClient client, final byte[] prefix,
139       final int prefixLen) {
140     Preconditions.checkState(internalScheduler.isRunningOnThread(), "Not on internal thread");
141     listenerScheduler.schedule(NO_DELAY, new NamedRunnable("CheckingInvalListener.reissueRegs") {
142       @Override
143       public void run() {
144         statistics.recordListenerEvent(ListenerEventType.REISSUE_REGISTRATIONS);
145         delegate.reissueRegistrations(client, prefix, prefixLen);
146       }
147     });
148   }
149
150   @Override
151   public void informError(final InvalidationClient client, final ErrorInfo errorInfo) {
152     Preconditions.checkState(internalScheduler.isRunningOnThread(), "Not on internal thread");
153     listenerScheduler.schedule(NO_DELAY, new NamedRunnable("CheckingInvalListener.informError") {
154       @Override
155       public void run() {
156         statistics.recordListenerEvent(ListenerEventType.INFORM_ERROR);
157         delegate.informError(client, errorInfo);
158       }
159     });
160   }
161
162   /** Returns the delegate {@link InvalidationListener}. */
163   InvalidationListener getDelegate() {
164     return delegate;
165   }
166
167   @Override
168   public void ready(final InvalidationClient client) {
169     Preconditions.checkState(internalScheduler.isRunningOnThread(), "Not on internal thread");
170     listenerScheduler.schedule(NO_DELAY, new NamedRunnable("CheckingInvalListener.ready") {
171       @Override
172       public void run() {
173         logger.info("Informing app that ticl is ready");
174         delegate.ready(client);
175       }
176     });
177   }
178 }