Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1a1e289258a5d83d9e0e408666da100b51792022 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*******************************************************************************
 * Copyright (c) 2009 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.skynet.core.internal;

import com.google.common.io.InputSupplier;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import javax.ws.rs.core.Response;
import org.eclipse.osee.framework.core.enums.OseeCacheEnum;
import org.eclipse.osee.framework.core.model.TransactionRecordFactory;
import org.eclipse.osee.framework.core.model.cache.ArtifactTypeCache;
import org.eclipse.osee.framework.core.model.cache.AttributeTypeCache;
import org.eclipse.osee.framework.core.model.cache.BranchCache;
import org.eclipse.osee.framework.core.model.cache.IOseeCache;
import org.eclipse.osee.framework.core.model.cache.OseeEnumTypeCache;
import org.eclipse.osee.framework.core.model.cache.RelationTypeCache;
import org.eclipse.osee.framework.core.model.cache.TransactionCache;
import org.eclipse.osee.framework.core.services.IOseeCachingService;
import org.eclipse.osee.framework.jdk.core.type.OseeArgumentException;
import org.eclipse.osee.framework.jdk.core.util.Conditions;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.internal.accessors.DatabaseBranchAccessor;
import org.eclipse.osee.framework.skynet.core.internal.accessors.DatabaseTransactionRecordAccessor;
import org.eclipse.osee.jaxrs.client.JaxRsExceptions;
import org.eclipse.osee.jdbc.JdbcClient;
import org.eclipse.osee.jdbc.JdbcService;
import org.eclipse.osee.orcs.rest.client.OseeClient;
import org.eclipse.osee.orcs.rest.model.TypesEndpoint;

/**
 * @author Roberto E. Escobar
 */
public class ClientCachingServiceProxy implements IOseeCachingService {

   public static interface TypesLoader {
      void loadTypes(IOseeCachingService service, InputSupplier<? extends InputStream> supplier);
   }

   private JdbcService jdbcService;
   private OseeClient oseeClient;

   private BranchCache branchCache;
   private TransactionCache txCache;

   private OseeEnumTypeCache enumTypeCache;
   private AttributeTypeCache attributeTypeCache;
   private ArtifactTypeCache artifactTypeCache;
   private RelationTypeCache relationTypeCache;

   private List<IOseeCache<?>> caches;

   public void setJdbcService(JdbcService jdbcService) {
      this.jdbcService = jdbcService;
   }

   public void setOseeClient(OseeClient oseeClient) {
      this.oseeClient = oseeClient;
   }

   public void start() {
      JdbcClient jdbcClient = jdbcService.getClient();

      txCache = new TransactionCache();
      txCache.setAccessor(new DatabaseTransactionRecordAccessor(jdbcClient, txCache, new TransactionRecordFactory()));
      branchCache = new BranchCache(new DatabaseBranchAccessor(jdbcClient, txCache), txCache);

      artifactTypeCache = new ArtifactTypeCache();
      enumTypeCache = new OseeEnumTypeCache();
      attributeTypeCache = new AttributeTypeCache();
      relationTypeCache = new RelationTypeCache();

      caches = new ArrayList<>();
      caches.add(branchCache);
      caches.add(txCache);
      caches.add(artifactTypeCache);
      caches.add(attributeTypeCache);
      caches.add(relationTypeCache);
      caches.add(enumTypeCache);
   }

   public void stop() {
      caches.clear();

      enumTypeCache = null;
      attributeTypeCache = null;
      relationTypeCache = null;
      artifactTypeCache = null;

      branchCache = null;
      txCache = null;
   }

   @Override
   public BranchCache getBranchCache() {
      return branchCache;
   }

   @Override
   public TransactionCache getTransactionCache() {
      return txCache;
   }

   @Override
   public ArtifactTypeCache getArtifactTypeCache() {
      return artifactTypeCache;
   }

   @Override
   public AttributeTypeCache getAttributeTypeCache() {
      return attributeTypeCache;
   }

   @Override
   public RelationTypeCache getRelationTypeCache() {
      return relationTypeCache;
   }

   @Override
   public OseeEnumTypeCache getEnumTypeCache() {
      return enumTypeCache;
   }

   @Override
   public Collection<?> getCaches() {
      return caches;
   }

   @Override
   public IOseeCache<?> getCache(OseeCacheEnum cacheId) {
      Conditions.checkNotNull(cacheId, "cache id to find");
      for (IOseeCache<?> cache : caches) {
         if (cache.getCacheId().equals(cacheId)) {
            return cache;
         }
      }
      throw new OseeArgumentException("Unable to find cache for id [%s]", cacheId);
   }

   @Override
   public void reloadTypes() {
      DslToTypeLoader typesLoader = new DslToTypeLoader(branchCache);
      typesLoader.loadTypes(this, new InputSupplier<InputStream>() {
         @Override
         public InputStream getInput() {
            OseeLog.log(Activator.class, Level.INFO, "Loading All type caches <<<<<<<<<<<<<<<<<<<<<<");
            TypesEndpoint typesEndpoint = oseeClient.getTypesEndpoint();
            try {
               Response response = typesEndpoint.getTypes();
               return response.hasEntity() ? response.readEntity(InputStream.class) : new ByteArrayInputStream(
                  new byte[0]);
            } catch (Exception ex) {
               throw JaxRsExceptions.asOseeException(ex);
            }
         }
      });
   }

   @Override
   public void reloadAll() {
      getBranchCache().reloadCache();
      getTransactionCache().reloadCache();

      reloadTypes();
   }

   @Override
   public void clearAll() {
      getBranchCache().decacheAll();
      getTransactionCache().decacheAll();
      clearAllTypes();
   }

   private void clearAllTypes() {
      getEnumTypeCache().decacheAll();
      getAttributeTypeCache().decacheAll();
      getRelationTypeCache().decacheAll();
      getArtifactTypeCache().decacheAll();
   }

}

Back to the top