1   /*
2    * Copyright 2019 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package com.linecorp.centraldogma.client;
18  
19  import static com.linecorp.centraldogma.internal.PathPatternUtil.toPathPattern;
20  import static java.util.Objects.requireNonNull;
21  
22  import java.util.List;
23  import java.util.concurrent.CompletableFuture;
24  import java.util.concurrent.Executor;
25  import java.util.concurrent.ScheduledExecutorService;
26  import java.util.function.Function;
27  
28  import javax.annotation.Nullable;
29  
30  import com.linecorp.centraldogma.common.Author;
31  import com.linecorp.centraldogma.common.Change;
32  import com.linecorp.centraldogma.common.Commit;
33  import com.linecorp.centraldogma.common.Entry;
34  import com.linecorp.centraldogma.common.Markup;
35  import com.linecorp.centraldogma.common.MergeSource;
36  import com.linecorp.centraldogma.common.MergedEntry;
37  import com.linecorp.centraldogma.common.PushResult;
38  import com.linecorp.centraldogma.common.Query;
39  import com.linecorp.centraldogma.common.Revision;
40  
41  import io.micrometer.core.instrument.MeterRegistry;
42  
43  /**
44   * A skeletal {@link CentralDogma} implementation.
45   */
46  public abstract class AbstractCentralDogma implements CentralDogma {
47  
48      private final ScheduledExecutorService blockingTaskExecutor;
49      @Nullable
50      private final MeterRegistry meterRegistry;
51  
52      /**
53       * Creates a new instance.
54       *
55       * @param blockingTaskExecutor the {@link ScheduledExecutorService} which will be used for scheduling the
56       *                             tasks related with automatic retries and invoking the callbacks for
57       *                             watched changes.
58       */
59      protected AbstractCentralDogma(ScheduledExecutorService blockingTaskExecutor,
60                                     @Nullable MeterRegistry meterRegistry) {
61          this.blockingTaskExecutor = requireNonNull(blockingTaskExecutor, "blockingTaskExecutor");
62          this.meterRegistry = meterRegistry;
63      }
64  
65      /**
66       * Returns the {@link ScheduledExecutorService} which is used for scheduling the tasks related with
67       * automatic retries and invoking the callbacks for watched changes.
68       */
69      protected final ScheduledExecutorService executor() {
70          return blockingTaskExecutor;
71      }
72  
73      @Override
74      public CentralDogmaRepository forRepo(String projectName, String repositoryName) {
75          requireNonNull(projectName, "projectName");
76          requireNonNull(repositoryName, "repositoryName");
77          return new CentralDogmaRepository(this, projectName, repositoryName, blockingTaskExecutor,
78                                            meterRegistry);
79      }
80  
81      @Override
82      public final CompletableFuture<Entry<?>> getFile(
83              String projectName, String repositoryName, Revision revision, String path) {
84          return CentralDogma.super.getFile(projectName, repositoryName, revision, path);
85      }
86  
87      @Override
88      public final CompletableFuture<MergedEntry<?>> mergeFiles(
89              String projectName, String repositoryName, Revision revision, MergeSource... mergeSources) {
90          return CentralDogma.super.mergeFiles(projectName, repositoryName, revision, mergeSources);
91      }
92  
93      @Override
94      public final CompletableFuture<MergedEntry<?>> mergeFiles(
95              String projectName, String repositoryName, Revision revision, Iterable<MergeSource> mergeSources) {
96          return CentralDogma.super.mergeFiles(projectName, repositoryName, revision, mergeSources);
97      }
98  
99      @Override
100     public final CompletableFuture<List<Commit>> getHistory(
101             String projectName, String repositoryName, Revision from, Revision to) {
102         return CentralDogma.super.getHistory(projectName, repositoryName, from, to);
103     }
104 
105     @Override
106     public final CompletableFuture<Change<?>> getDiff(
107             String projectName, String repositoryName, Revision from, Revision to, String path) {
108         return CentralDogma.super.getDiff(projectName, repositoryName, from, to, path);
109     }
110 
111     @Override
112     public final CompletableFuture<List<Change<?>>> getPreviewDiffs(
113             String projectName, String repositoryName, Revision baseRevision, Change<?>... changes) {
114         return CentralDogma.super.getPreviewDiffs(projectName, repositoryName, baseRevision, changes);
115     }
116 
117     @Override
118     public final CompletableFuture<PushResult> push(
119             String projectName, String repositoryName, Revision baseRevision,
120             String summary, Change<?>... changes) {
121         return CentralDogma.super.push(projectName, repositoryName, baseRevision, summary, changes);
122     }
123 
124     @Override
125     public final CompletableFuture<PushResult> push(
126             String projectName, String repositoryName, Revision baseRevision,
127             String summary, Iterable<? extends Change<?>> changes) {
128         return CentralDogma.super.push(projectName, repositoryName, baseRevision, summary, changes);
129     }
130 
131     @Override
132     public final CompletableFuture<PushResult> push(
133             String projectName, String repositoryName, Revision baseRevision,
134             String summary, String detail, Markup markup, Change<?>... changes) {
135         return CentralDogma.super.push(projectName, repositoryName, baseRevision,
136                                        summary, detail, markup, changes);
137     }
138 
139     @Override
140     public final CompletableFuture<PushResult> push(
141             String projectName, String repositoryName, Revision baseRevision,
142             Author author, String summary, Change<?>... changes) {
143         return CentralDogma.super.push(projectName, repositoryName, baseRevision, author, summary, changes);
144     }
145 
146     @Override
147     public final CompletableFuture<PushResult> push(
148             String projectName, String repositoryName, Revision baseRevision,
149             Author author, String summary, Iterable<? extends Change<?>> changes) {
150         return CentralDogma.super.push(projectName, repositoryName, baseRevision, author, summary, changes);
151     }
152 
153     @Override
154     public final CompletableFuture<PushResult> push(
155             String projectName, String repositoryName, Revision baseRevision,
156             Author author, String summary, String detail, Markup markup, Change<?>... changes) {
157         return CentralDogma.super.push(projectName, repositoryName, baseRevision,
158                                        author, summary, detail, markup, changes);
159     }
160 
161     @Override
162     public <T, U> Watcher<U> fileWatcher(
163             String projectName, String repositoryName, Query<T> query,
164             Function<? super T, ? extends U> function) {
165         return fileWatcher(projectName, repositoryName, query, function, blockingTaskExecutor);
166     }
167 
168     @Override
169     public <T, U> Watcher<U> fileWatcher(String projectName, String repositoryName, Query<T> query,
170                                          Function<? super T, ? extends U> function, Executor executor) {
171         //noinspection unchecked
172         return (Watcher<U>) forRepo(projectName, repositoryName).watcher(query)
173                                                                 .map(function)
174                                                                 .mapperExecutor(executor)
175                                                                 .start();
176     }
177 
178     @Override
179     public <T> Watcher<T> repositoryWatcher(
180             String projectName, String repositoryName, String pathPattern,
181             Function<Revision, ? extends T> function) {
182         return repositoryWatcher(projectName, repositoryName, pathPattern, function, blockingTaskExecutor);
183     }
184 
185     @Override
186     public <T> Watcher<T> repositoryWatcher(String projectName, String repositoryName, String pathPattern,
187                                             Function<Revision, ? extends T> function, Executor executor) {
188         //noinspection unchecked
189         return (Watcher<T>) forRepo(projectName, repositoryName).watcher(toPathPattern(pathPattern))
190                                                                 .map(function)
191                                                                 .mapperExecutor(executor)
192                                                                 .start();
193     }
194 
195     /**
196      * Normalizes the specified {@link Revision} only if it is a relative revision.
197      *
198      * @return the absolute {@link Revision}
199      */
200     protected final CompletableFuture<Revision> maybeNormalizeRevision(
201             String projectName, String repositoryName, Revision revision) {
202 
203         if (revision.isRelative()) {
204             return normalizeRevision(projectName, repositoryName, revision);
205         } else {
206             return CompletableFuture.completedFuture(revision);
207         }
208     }
209 }