|
| 1 | +/* |
| 2 | +Copyright 2017 The Kubernetes Authors. |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | +package io.kubernetes.client; |
| 14 | + |
| 15 | +import com.google.gson.annotations.SerializedName; |
| 16 | +import com.squareup.okhttp.ResponseBody; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.lang.reflect.Type; |
| 20 | +import java.util.Iterator; |
| 21 | + |
| 22 | +/** |
| 23 | + * Watch class implements watch mechansim of kubernetes. For every list API |
| 24 | + * call with a watch parameter you should be able to pass its call to this class |
| 25 | + * and watch changes to that list. For example CoreV1Api.listNamespace has watch |
| 26 | + * parameter, so you can create a call using CoreV1Api.listNamespaceCall and |
| 27 | + * set watch to True and watch the changes to namespaces. |
| 28 | + */ |
| 29 | +public class Watch<T> implements Iterable<Watch.Response<T>>, |
| 30 | + Iterator<Watch.Response<T>> { |
| 31 | + |
| 32 | + /** |
| 33 | + * Response class holds a watch response that has a `type` that can be |
| 34 | + * ADDED, MODIFIED, DELETED and ERROR. It also hold the actual target |
| 35 | + * object. |
| 36 | + */ |
| 37 | + public static class Response<T> { |
| 38 | + @SerializedName("type") |
| 39 | + public String type; |
| 40 | + |
| 41 | + @SerializedName("object") |
| 42 | + public T object; |
| 43 | + |
| 44 | + Response(String type, T object) { |
| 45 | + this.type = type; |
| 46 | + this.object = object; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + Type watchType; |
| 51 | + ResponseBody response; |
| 52 | + JSON json; |
| 53 | + |
| 54 | + public Watch(JSON json, ResponseBody body, Type watchType) { |
| 55 | + this.response = body; |
| 56 | + this.watchType = watchType; |
| 57 | + this.json = json; |
| 58 | + } |
| 59 | + |
| 60 | + public Response<T> next() { |
| 61 | + try { |
| 62 | + String line = response.source().readUtf8Line(); |
| 63 | + if (line == null) { |
| 64 | + throw new RuntimeException("Null response from the server."); |
| 65 | + } |
| 66 | + return json.deserialize(line, watchType); |
| 67 | + } catch (IOException e) { |
| 68 | + throw new RuntimeException("IO Exception during next method.", e); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public boolean hasNext() { |
| 73 | + try { |
| 74 | + return !response.source().exhausted(); |
| 75 | + } catch (IOException e) { |
| 76 | + throw new RuntimeException("IO Exception during hasNext method.", |
| 77 | + e); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public Iterator<Response<T>> iterator() { |
| 82 | + return this; |
| 83 | + } |
| 84 | + |
| 85 | + public void remove() { |
| 86 | + throw new UnsupportedOperationException("remove"); |
| 87 | + } |
| 88 | +} |
0 commit comments