1   /*
2    * Copyright 2017 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.server.internal.storage.project;
18  
19  import static java.util.Objects.requireNonNull;
20  
21  import java.io.File;
22  import java.util.concurrent.Executor;
23  import java.util.function.Supplier;
24  
25  import javax.annotation.Nullable;
26  
27  import com.linecorp.centraldogma.common.Author;
28  import com.linecorp.centraldogma.common.CentralDogmaException;
29  import com.linecorp.centraldogma.common.ProjectExistsException;
30  import com.linecorp.centraldogma.common.ProjectNotFoundException;
31  import com.linecorp.centraldogma.server.internal.storage.DirectoryBasedStorageManager;
32  import com.linecorp.centraldogma.server.internal.storage.repository.RepositoryCache;
33  import com.linecorp.centraldogma.server.storage.project.InternalProjectInitializer;
34  import com.linecorp.centraldogma.server.storage.project.Project;
35  import com.linecorp.centraldogma.server.storage.project.ProjectManager;
36  
37  import io.micrometer.core.instrument.MeterRegistry;
38  
39  public class DefaultProjectManager extends DirectoryBasedStorageManager<Project> implements ProjectManager {
40  
41      private final Executor repositoryWorker;
42      @Nullable
43      private final RepositoryCache cache;
44  
45      public DefaultProjectManager(File rootDir, Executor repositoryWorker, Executor purgeWorker,
46                                   MeterRegistry meterRegistry, @Nullable String cacheSpec) {
47          super(rootDir, Project.class, purgeWorker);
48  
49          requireNonNull(meterRegistry, "meterRegistry");
50          requireNonNull(repositoryWorker, "repositoryWorker");
51  
52          this.repositoryWorker = repositoryWorker;
53          cache = cacheSpec != null ? new RepositoryCache(cacheSpec, meterRegistry) : null;
54  
55          init();
56      }
57  
58      @Override
59      public void close(Supplier<CentralDogmaException> failureCauseSupplier) {
60          super.close(failureCauseSupplier);
61          if (cache != null) {
62              cache.clear();
63          }
64      }
65  
66      @Override
67      protected Project openChild(File childDir) throws Exception {
68          return new DefaultProject(childDir, repositoryWorker, purgeWorker(), cache);
69      }
70  
71      @Override
72      protected Project createChild(File childDir, Author author, long creationTimeMillis) throws Exception {
73          final Project dogmaProject;
74          if (exists(InternalProjectInitializer.INTERNAL_PROJECT_DOGMA)) {
75              dogmaProject = get(InternalProjectInitializer.INTERNAL_PROJECT_DOGMA);
76          } else {
77              dogmaProject = null;
78          }
79          return new DefaultProject(dogmaProject, childDir, repositoryWorker, purgeWorker(),
80                                    creationTimeMillis, author, cache);
81      }
82  
83      @Override
84      protected void closeChild(File childDir, Project child,
85                                Supplier<CentralDogmaException> failureCauseSupplier) {
86          final DefaultProject c = (DefaultProject) child;
87          c.repos.close(failureCauseSupplier);
88      }
89  
90      @Override
91      protected CentralDogmaException newStorageExistsException(String name) {
92          return new ProjectExistsException(name);
93      }
94  
95      @Override
96      protected CentralDogmaException newStorageNotFoundException(String name) {
97          return new ProjectNotFoundException(name);
98      }
99  }