diff options
author | Roberto E. Escobar | 2013-04-25 01:42:36 +0000 |
---|---|---|
committer | Gerrit Code Review @ Eclipse.org | 2013-05-01 22:13:13 +0000 |
commit | 4e209919113d47b70880b53c90fda98507c6aba7 (patch) | |
tree | 284df78dafbb5d21e870d032a2b3cd71bff4c629 /plugins/org.eclipse.osee.cache.admin | |
parent | 910bff76856a2456569da5925f4f9504b72a23ed (diff) | |
download | org.eclipse.osee-4e209919113d47b70880b53c90fda98507c6aba7.tar.gz org.eclipse.osee-4e209919113d47b70880b53c90fda98507c6aba7.tar.xz org.eclipse.osee-4e209919113d47b70880b53c90fda98507c6aba7.zip |
feature: Create cache admin bundle and tests
Create a wrapper over google cache to keep track
of application caches. This the initial check-in
of the CacheAdmin service. This service will be
responsible for collecting and managing application
caches.
Change-Id: I678d6c43d633add1ed7890ae9207418adab655ef
Diffstat (limited to 'plugins/org.eclipse.osee.cache.admin')
15 files changed, 775 insertions, 0 deletions
diff --git a/plugins/org.eclipse.osee.cache.admin/.classpath b/plugins/org.eclipse.osee.cache.admin/.classpath new file mode 100644 index 00000000000..ad32c83a788 --- /dev/null +++ b/plugins/org.eclipse.osee.cache.admin/.classpath @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> + <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> + <classpathentry kind="src" path="src"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/plugins/org.eclipse.osee.cache.admin/.project b/plugins/org.eclipse.osee.cache.admin/.project new file mode 100644 index 00000000000..6a7cb2961e5 --- /dev/null +++ b/plugins/org.eclipse.osee.cache.admin/.project @@ -0,0 +1,33 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>org.eclipse.osee.cache.admin</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.ManifestBuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.SchemaBuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.ds.core.builder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.pde.PluginNature</nature> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> diff --git a/plugins/org.eclipse.osee.cache.admin/META-INF/MANIFEST.MF b/plugins/org.eclipse.osee.cache.admin/META-INF/MANIFEST.MF new file mode 100644 index 00000000000..26e525f2ede --- /dev/null +++ b/plugins/org.eclipse.osee.cache.admin/META-INF/MANIFEST.MF @@ -0,0 +1,15 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Cache Admin (Incubation) +Bundle-SymbolicName: org.eclipse.osee.cache.admin +Bundle-Version: 0.11.0.qualifier +Bundle-Vendor: Eclipse Open System Engineering Environment +Bundle-RequiredExecutionEnvironment: JavaSE-1.6 +Require-Bundle: com.google.guava;bundle-version="12.0.0" +Import-Package: org.eclipse.core.runtime, + org.eclipse.osee.framework.core.exception, + org.eclipse.osee.framework.core.util, + org.eclipse.osee.framework.jdk.core.type +Service-Component: OSGI-INF/*.xml +Export-Package: org.eclipse.osee.cache.admin +Bundle-ActivationPolicy: lazy diff --git a/plugins/org.eclipse.osee.cache.admin/OSGI-INF/cache.admin.xml b/plugins/org.eclipse.osee.cache.admin/OSGI-INF/cache.admin.xml new file mode 100644 index 00000000000..716272a66b8 --- /dev/null +++ b/plugins/org.eclipse.osee.cache.admin/OSGI-INF/cache.admin.xml @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="start" deactivate="stop" name="org.eclipse.osee.cache.admin.internal.CacheAdminImpl"> + <implementation class="org.eclipse.osee.cache.admin.internal.CacheAdminImpl"/> + <service> + <provide interface="org.eclipse.osee.cache.admin.CacheAdmin"/> + </service> +</scr:component> diff --git a/plugins/org.eclipse.osee.cache.admin/build.properties b/plugins/org.eclipse.osee.cache.admin/build.properties new file mode 100644 index 00000000000..6210e849b59 --- /dev/null +++ b/plugins/org.eclipse.osee.cache.admin/build.properties @@ -0,0 +1,5 @@ +output.. = bin/ +bin.includes = META-INF/,\ + .,\ + OSGI-INF/ +source.. = src/ diff --git a/plugins/org.eclipse.osee.cache.admin/pom.xml b/plugins/org.eclipse.osee.cache.admin/pom.xml new file mode 100644 index 00000000000..b9b7f8854d8 --- /dev/null +++ b/plugins/org.eclipse.osee.cache.admin/pom.xml @@ -0,0 +1,34 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.eclipse.osee</groupId> + <artifactId>org.eclipse.osee.x.core.parent</artifactId> + <version>0.11.0-SNAPSHOT</version> + <relativePath>../../plugins/org.eclipse.osee.x.core.parent</relativePath> + </parent> + + <artifactId>org.eclipse.osee.cache.admin</artifactId> + <packaging>eclipse-plugin</packaging> + <name>OSEE Cache Admin - (Incubation)</name> + + <build> + <!-- workaround for https://issues.sonatype.org/browse/TYCHO-168 --> + <resources> + <resource> + <directory>src</directory> + <excludes> + <exclude>**/*.java</exclude> + </excludes> + </resource> + </resources> + <plugins> + <plugin> + <groupId>org.eclipse.tycho</groupId> + <artifactId>tycho-source-plugin</artifactId> + </plugin> + </plugins> + </build> +</project>
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/Cache.java b/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/Cache.java new file mode 100644 index 00000000000..8f8a3112313 --- /dev/null +++ b/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/Cache.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * Copyright (c) 2013 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.cache.admin; + +import java.util.Map; +import java.util.concurrent.Callable; +import org.eclipse.osee.framework.core.exception.OseeCoreException; + +/** + * @author John Misinco + * @author Roberto E. Escobar + */ +public interface Cache<K, V> { + + V getIfPresent(K key); + + Map<K, V> getIfPresent(Iterable<? extends K> keys); + + V get(K key, Callable<? extends V> loader) throws OseeCoreException; + + V get(K key) throws OseeCoreException; + + Map<K, V> get(Iterable<? extends K> keys) throws OseeCoreException; + + Iterable<V> getAllPresent(); + + Iterable<V> getAll() throws OseeCoreException; + + Iterable<? extends K> getAllKeysPresent(); + + Iterable<? extends K> getAllKeys() throws OseeCoreException; + + void refresh(K key); + + void invalidateAll(); + + void invalidate(Iterable<? extends K> keys); + + void invalidate(K key); + + long size(); + + boolean isEmpty(); + +}
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/CacheAdmin.java b/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/CacheAdmin.java new file mode 100644 index 00000000000..b3a1c6b8d56 --- /dev/null +++ b/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/CacheAdmin.java @@ -0,0 +1,27 @@ +/******************************************************************************* + * Copyright (c) 2013 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.cache.admin; + +import org.eclipse.osee.framework.core.exception.OseeCoreException; + +/** + * Service in-charge of creating application caches. + * + * @author John Misinco + * @author Roberto E. Escobar + */ +public interface CacheAdmin { + + <K, V> Cache<K, V> createCache(CacheConfiguration configuration) throws OseeCoreException; + + <K, V> Cache<K, V> createLoadingCache(CacheConfiguration configuration, CacheDataLoader<K, V> dataLoader, CacheKeysLoader<K> keyLoader) throws OseeCoreException; + +}
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/CacheConfiguration.java b/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/CacheConfiguration.java new file mode 100644 index 00000000000..881cd78dc45 --- /dev/null +++ b/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/CacheConfiguration.java @@ -0,0 +1,100 @@ +/******************************************************************************* + * Copyright (c) 2013 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.cache.admin; + +import java.util.concurrent.TimeUnit; +import org.eclipse.osee.framework.jdk.core.type.Pair; + +/** + * @author John Misinco + * @author Roberto E. Escobar + */ +public class CacheConfiguration { + + private static final int DEFAULT_UNSET = -1; + private static final Pair<Long, TimeUnit> UNSET_EXPIRATION = new Pair<Long, TimeUnit>(-1L, TimeUnit.NANOSECONDS); + + private int initialCapacity = DEFAULT_UNSET; + private long maxSize = DEFAULT_UNSET; + + private Pair<Long, TimeUnit> expireAfterAccess = UNSET_EXPIRATION; + private Pair<Long, TimeUnit> expireAfterWrite = UNSET_EXPIRATION; + private Pair<Long, TimeUnit> refreshAfterWrite = UNSET_EXPIRATION; + + private CacheConfiguration() { + // + } + + public static CacheConfiguration newConfiguration() { + return new CacheConfiguration(); + } + + public boolean hasInitialCapacity() { + return initialCapacity >= 0; + } + + public void setInitialCapacity(int initialCapacity) { + this.initialCapacity = initialCapacity; + } + + public boolean hasMaximumSize() { + return maxSize >= 0; + } + + public void setMaximumSize(long maxSize) { + this.maxSize = maxSize; + } + + public boolean isExpireAfterAccess() { + return !UNSET_EXPIRATION.equals(expireAfterAccess); + } + + public void setExpireAfterAccess(long duration, TimeUnit timeUnit) { + this.expireAfterAccess = new Pair<Long, TimeUnit>(duration, timeUnit); + } + + public boolean isExpireAfterWrite() { + return !UNSET_EXPIRATION.equals(expireAfterWrite); + } + + public void setExpireAfterWrite(long duration, TimeUnit timeUnit) { + this.expireAfterWrite = new Pair<Long, TimeUnit>(duration, timeUnit); + } + + public boolean isRefreshAfterWrite() { + return !UNSET_EXPIRATION.equals(refreshAfterWrite); + } + + public void setRefreshAfterWrite(long duration, TimeUnit timeUnit) { + this.refreshAfterWrite = new Pair<Long, TimeUnit>(duration, timeUnit); + } + + public int getInitialCapacity() { + return initialCapacity; + } + + public long getMaximumSize() { + return maxSize; + } + + public Pair<Long, TimeUnit> getExpireAfterAccess() { + return expireAfterAccess; + } + + public Pair<Long, TimeUnit> getExpireAfterWrite() { + return expireAfterWrite; + } + + public Pair<Long, TimeUnit> getRefreshAfterWrite() { + return refreshAfterWrite; + } + +}
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/CacheDataLoader.java b/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/CacheDataLoader.java new file mode 100644 index 00000000000..12daf4458bd --- /dev/null +++ b/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/CacheDataLoader.java @@ -0,0 +1,27 @@ +/******************************************************************************* + * Copyright (c) 2013 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.cache.admin; + +import java.util.Map; +import org.eclipse.osee.framework.core.exception.OseeCoreException; + +/** + * @author John Misinco + * @author Roberto E. Escobar + */ +public interface CacheDataLoader<K, V> { + + Map<K, V> load(Iterable<? extends K> keys) throws OseeCoreException; + + V load(K key) throws OseeCoreException; + + V reload(K key, V oldValue) throws OseeCoreException; +}
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/CacheKeysLoader.java b/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/CacheKeysLoader.java new file mode 100644 index 00000000000..e423ad883f2 --- /dev/null +++ b/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/CacheKeysLoader.java @@ -0,0 +1,23 @@ +/******************************************************************************* + * Copyright (c) 2013 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.cache.admin; + +import org.eclipse.osee.framework.core.exception.OseeCoreException; + +/** + * @author John Misinco + * @author Roberto E. Escobar + */ +public interface CacheKeysLoader<K> { + + Iterable<? extends K> getAllKeys() throws OseeCoreException; + +} diff --git a/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/internal/CacheAdminImpl.java b/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/internal/CacheAdminImpl.java new file mode 100644 index 00000000000..232886edad1 --- /dev/null +++ b/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/internal/CacheAdminImpl.java @@ -0,0 +1,48 @@ +/******************************************************************************* + * Copyright (c) 2013 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.cache.admin.internal; + +import org.eclipse.osee.cache.admin.Cache; +import org.eclipse.osee.cache.admin.CacheAdmin; +import org.eclipse.osee.cache.admin.CacheConfiguration; +import org.eclipse.osee.cache.admin.CacheDataLoader; +import org.eclipse.osee.cache.admin.CacheKeysLoader; +import org.eclipse.osee.framework.core.exception.OseeCoreException; + +/** + * Creating and keeping track of application caches. + * + * @author John Misinco + * @author Roberto E. Escobar + */ +public class CacheAdminImpl implements CacheAdmin { + + private final CacheFactory cacheFactory = new CacheFactory(); + + public void start() { + // + } + + public void stop() { + // + } + + @Override + public <K, V> Cache<K, V> createCache(CacheConfiguration configuration) throws OseeCoreException { + return cacheFactory.createCache(configuration); + } + + @Override + public <K, V> Cache<K, V> createLoadingCache(CacheConfiguration configuration, CacheDataLoader<K, V> accessor, CacheKeysLoader<K> keyLoader) throws OseeCoreException { + return cacheFactory.createLoadingCache(configuration, accessor, keyLoader); + } + +}
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/internal/CacheFactory.java b/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/internal/CacheFactory.java new file mode 100644 index 00000000000..e12f8f54a96 --- /dev/null +++ b/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/internal/CacheFactory.java @@ -0,0 +1,90 @@ +/******************************************************************************* + * Copyright (c) 2013 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.cache.admin.internal; + +import java.util.Map; +import java.util.concurrent.TimeUnit; +import org.eclipse.osee.cache.admin.Cache; +import org.eclipse.osee.cache.admin.CacheConfiguration; +import org.eclipse.osee.cache.admin.CacheDataLoader; +import org.eclipse.osee.cache.admin.CacheKeysLoader; +import org.eclipse.osee.framework.core.exception.OseeCoreException; +import org.eclipse.osee.framework.core.util.Conditions; +import org.eclipse.osee.framework.jdk.core.type.Pair; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; + +/** + * @author John Misinco + * @author Roberto E. Escobar + */ +public class CacheFactory { + + public <K, V> Cache<K, V> createCache(final CacheConfiguration config) throws OseeCoreException { + Conditions.checkNotNull(config, "cacheConfiguration"); + com.google.common.cache.Cache<K, V> cache = createCacheBuilder(config).build(); + Cache<K, V> toReturn = new CacheProxy<K, V>(cache); + return toReturn; + } + + public <K, V> Cache<K, V> createLoadingCache(final CacheConfiguration config, final CacheDataLoader<K, V> dataLoader, final CacheKeysLoader<K> keyLoader) throws OseeCoreException { + Conditions.checkNotNull(config, "cacheConfiguration"); + Conditions.checkNotNull(dataLoader, "cacheDataLoader"); + Conditions.checkNotNull(keyLoader, "cacheKeysLoader"); + + final LoadingCache<K, V> loadingCache = createCacheBuilder(config).build(new CacheLoader<K, V>() { + + @Override + public Map<K, V> loadAll(Iterable<? extends K> keys) throws Exception { + return dataLoader.load(keys); + } + + @Override + public V load(K key) throws Exception { + return dataLoader.load(key); + } + + @Override + public ListenableFuture<V> reload(K key, V oldValue) throws Exception { + V newValue = dataLoader.reload(key, oldValue); + return Futures.immediateFuture(newValue); + } + }); + Cache<K, V> toReturn = new LoadingCacheProxy<K, V>(loadingCache, keyLoader); + return toReturn; + } + + private CacheBuilder<Object, Object> createCacheBuilder(CacheConfiguration config) { + CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder(); + if (config.hasInitialCapacity()) { + builder = builder.initialCapacity(config.getInitialCapacity()); + } + if (config.hasMaximumSize()) { + builder = builder.maximumSize(config.getMaximumSize()); + } + if (config.isExpireAfterAccess()) { + Pair<Long, TimeUnit> data = config.getExpireAfterAccess(); + builder = builder.expireAfterAccess(data.getFirst(), data.getSecond()); + } + if (config.isExpireAfterWrite()) { + Pair<Long, TimeUnit> data = config.getExpireAfterWrite(); + builder = builder.expireAfterWrite(data.getFirst(), data.getSecond()); + } + if (config.isRefreshAfterWrite()) { + Pair<Long, TimeUnit> data = config.getRefreshAfterWrite(); + builder = builder.refreshAfterWrite(data.getFirst(), data.getSecond()); + } + return builder; + } +} diff --git a/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/internal/CacheProxy.java b/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/internal/CacheProxy.java new file mode 100644 index 00000000000..533277a9b4f --- /dev/null +++ b/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/internal/CacheProxy.java @@ -0,0 +1,147 @@ +/******************************************************************************* + * Copyright (c) 2013 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.cache.admin.internal; + +import java.util.Map; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import org.eclipse.osee.cache.admin.Cache; +import org.eclipse.osee.framework.core.exception.OseeCoreException; +import org.eclipse.osee.framework.core.exception.OseeExceptions; +import com.google.common.cache.CacheLoader.InvalidCacheLoadException; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Iterables; +import com.google.common.util.concurrent.ExecutionError; +import com.google.common.util.concurrent.UncheckedExecutionException; + +/** + * @author John Misinco + * @author Roberto E. Escobar + */ +public class CacheProxy<K, V> implements Cache<K, V> { + + private final com.google.common.cache.Cache<K, V> proxied; + + public CacheProxy(com.google.common.cache.Cache<K, V> proxied) { + this.proxied = proxied; + } + + @Override + public V getIfPresent(K key) { + return proxied.getIfPresent(key); + } + + @Override + public Iterable<V> getAllPresent() { + Iterable<? extends K> keys = getAllKeysPresent(); + return getIfPresent(keys).values(); + } + + @Override + public Iterable<? extends K> getAllKeysPresent() { + return Iterables.unmodifiableIterable(proxied.asMap().keySet()); + } + + @Override + public Iterable<? extends K> getAllKeys() { + return getAllKeysPresent(); + + } + + @Override + public Map<K, V> getIfPresent(Iterable<? extends K> keys) { + return proxied.getAllPresent(keys); + } + + @Override + public Map<K, V> get(Iterable<? extends K> keys) throws OseeCoreException { + ImmutableMap<K, V> items = null; + try { + items = proxied.getAllPresent(keys); + } catch (InvalidCacheLoadException ex) { + OseeExceptions.wrapAndThrow(ex); + } catch (UncheckedExecutionException ex) { + OseeExceptions.wrapAndThrow(ex); + } catch (ExecutionError ex) { + OseeExceptions.wrapAndThrow(ex); + } + return items; + } + + @Override + public Iterable<V> getAll() throws OseeCoreException { + Iterable<? extends K> allKeys = getAllKeys(); + return get(allKeys).values(); + } + + @Override + public V get(K key) throws OseeCoreException { + V toReturn = null; + try { + toReturn = proxied.getIfPresent(key); + } catch (InvalidCacheLoadException ex) { + OseeExceptions.wrapAndThrow(ex); + } catch (UncheckedExecutionException ex) { + OseeExceptions.wrapAndThrow(ex); + } catch (ExecutionError ex) { + OseeExceptions.wrapAndThrow(ex); + } + return toReturn; + } + + @Override + public void refresh(K key) { + // Does nothing; + } + + @Override + public void invalidateAll() { + proxied.invalidateAll(); + } + + @Override + public void invalidate(Iterable<? extends K> keys) { + proxied.invalidateAll(keys); + } + + @Override + public void invalidate(K key) { + proxied.invalidate(key); + } + + @Override + public long size() { + return proxied.size(); + } + + @Override + public V get(K key, Callable<? extends V> callable) throws OseeCoreException { + V toReturn = null; + try { + toReturn = proxied.get(key, callable); + } catch (ExecutionException ex) { + OseeExceptions.wrapAndThrow(ex); + } catch (InvalidCacheLoadException ex) { + OseeExceptions.wrapAndThrow(ex); + } catch (UncheckedExecutionException ex) { + OseeExceptions.wrapAndThrow(ex); + } catch (ExecutionError ex) { + OseeExceptions.wrapAndThrow(ex); + } + return toReturn; + } + + @Override + public boolean isEmpty() { + return proxied.size() == 0; + } + +} diff --git a/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/internal/LoadingCacheProxy.java b/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/internal/LoadingCacheProxy.java new file mode 100644 index 00000000000..6492b368f7c --- /dev/null +++ b/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/internal/LoadingCacheProxy.java @@ -0,0 +1,159 @@ +/******************************************************************************* + * Copyright (c) 2013 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.cache.admin.internal; + +import java.util.Map; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import org.eclipse.osee.cache.admin.Cache; +import org.eclipse.osee.cache.admin.CacheKeysLoader; +import org.eclipse.osee.framework.core.exception.OseeCoreException; +import org.eclipse.osee.framework.core.exception.OseeExceptions; +import com.google.common.cache.CacheLoader.InvalidCacheLoadException; +import com.google.common.cache.LoadingCache; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Iterables; +import com.google.common.collect.Sets; +import com.google.common.util.concurrent.ExecutionError; +import com.google.common.util.concurrent.UncheckedExecutionException; + +/** + * @author John Misinco + * @author Roberto E. Escobar + */ +public class LoadingCacheProxy<K, V> implements Cache<K, V> { + + private final LoadingCache<K, V> proxied; + private final CacheKeysLoader<K> keyProvider; + + public LoadingCacheProxy(LoadingCache<K, V> proxied, CacheKeysLoader<K> keyProvider) { + this.proxied = proxied; + this.keyProvider = keyProvider; + } + + @Override + public V getIfPresent(K key) { + return proxied.getIfPresent(key); + } + + @Override + public Iterable<V> getAllPresent() { + Iterable<? extends K> keys = getAllKeysPresent(); + return getIfPresent(keys).values(); + } + + @Override + public Iterable<? extends K> getAllKeysPresent() { + return Iterables.unmodifiableIterable(proxied.asMap().keySet()); + } + + @Override + public Iterable<? extends K> getAllKeys() throws OseeCoreException { + Iterable<? extends K> iterator1 = getAllKeysPresent(); + Iterable<? extends K> iterator2 = keyProvider.getAllKeys(); + Iterable<? extends K> joined = Iterables.concat(iterator1, iterator2); + return Sets.newHashSet(joined); + + } + + @Override + public Map<K, V> getIfPresent(Iterable<? extends K> keys) { + return proxied.getAllPresent(keys); + } + + @Override + public Map<K, V> get(Iterable<? extends K> keys) throws OseeCoreException { + ImmutableMap<K, V> items = null; + try { + items = proxied.getAll(keys); + } catch (ExecutionException ex) { + OseeExceptions.wrapAndThrow(ex); + } catch (InvalidCacheLoadException ex) { + OseeExceptions.wrapAndThrow(ex); + } catch (UncheckedExecutionException ex) { + OseeExceptions.wrapAndThrow(ex); + } catch (ExecutionError ex) { + OseeExceptions.wrapAndThrow(ex); + } + return items; + } + + @Override + public Iterable<V> getAll() throws OseeCoreException { + Iterable<? extends K> allKeys = getAllKeys(); + return get(allKeys).values(); + } + + @Override + public V get(K key) throws OseeCoreException { + V toReturn = null; + try { + toReturn = proxied.get(key); + } catch (ExecutionException ex) { + OseeExceptions.wrapAndThrow(ex); + } catch (InvalidCacheLoadException ex) { + OseeExceptions.wrapAndThrow(ex); + } catch (UncheckedExecutionException ex) { + OseeExceptions.wrapAndThrow(ex); + } catch (ExecutionError ex) { + OseeExceptions.wrapAndThrow(ex); + } + return toReturn; + } + + @Override + public void refresh(K key) { + proxied.refresh(key); + } + + @Override + public void invalidateAll() { + proxied.invalidateAll(); + } + + @Override + public void invalidate(Iterable<? extends K> keys) { + proxied.invalidateAll(keys); + } + + @Override + public void invalidate(K key) { + proxied.invalidate(key); + } + + @Override + public long size() { + return proxied.size(); + } + + @Override + public V get(K key, Callable<? extends V> callable) throws OseeCoreException { + V toReturn = null; + try { + toReturn = proxied.get(key, callable); + } catch (ExecutionException ex) { + OseeExceptions.wrapAndThrow(ex); + } catch (InvalidCacheLoadException ex) { + OseeExceptions.wrapAndThrow(ex); + } catch (UncheckedExecutionException ex) { + OseeExceptions.wrapAndThrow(ex); + } catch (ExecutionError ex) { + OseeExceptions.wrapAndThrow(ex); + } + return toReturn; + } + + @Override + public boolean isEmpty() { + return proxied.size() == 0; + } + +} |