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 | |
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
37 files changed, 1397 insertions, 186 deletions
diff --git a/features/org.eclipse.osee.x.core.external.feature/feature.xml b/features/org.eclipse.osee.x.core.external.feature/feature.xml index 855349f397a..fc8953ea991 100644 --- a/features/org.eclipse.osee.x.core.external.feature/feature.xml +++ b/features/org.eclipse.osee.x.core.external.feature/feature.xml @@ -31,6 +31,7 @@ <import plugin="org.slf4j.api" version="1.6.1" match="equivalent"/> <import plugin="javax.servlet" version="2.5.0" match="compatible"/> <import plugin="org.apache.log4j" version="1.2.15" match="greaterOrEqual"/> + <import plugin="com.google.guava" version="12.0.0" match="greaterOrEqual"/> </requires> <plugin @@ -279,4 +280,11 @@ version="0.0.0" unpack="false"/> + <plugin + id="com.google.guava" + download-size="0" + install-size="0" + version="0.0.0" + unpack="false"/> + </feature> diff --git a/features/org.eclipse.osee.x.core.feature.source/feature.xml b/features/org.eclipse.osee.x.core.feature.source/feature.xml index c26062bcd20..237a7f5192f 100644 --- a/features/org.eclipse.osee.x.core.feature.source/feature.xml +++ b/features/org.eclipse.osee.x.core.feature.source/feature.xml @@ -108,4 +108,11 @@ version="0.0.0" unpack="false"/> + <plugin + id="org.eclipse.osee.cache.admin.source" + download-size="0" + install-size="0" + version="0.0.0" + unpack="false"/> + </feature> diff --git a/features/org.eclipse.osee.x.core.feature/feature.xml b/features/org.eclipse.osee.x.core.feature/feature.xml index 8f8c23c91c8..13b9f2fd09a 100644 --- a/features/org.eclipse.osee.x.core.feature/feature.xml +++ b/features/org.eclipse.osee.x.core.feature/feature.xml @@ -27,13 +27,16 @@ <import plugin="org.slf4j.api"/> <import plugin="ch.qos.logback.classic" version="0.9.27" match="greaterOrEqual"/> <import plugin="org.apache.commons.codec"/> - <import plugin="org.apache.commons.httpclient"/> <import plugin="org.apache.xerces"/> <import plugin="javax.mail.glassfish"/> <import plugin="com.vaadin" version="6.6.6" match="greaterOrEqual"/> - <import plugin="com.sun.jersey"/> - <import plugin="javax.ws.rs"/> <import plugin="javax.servlet" version="2.5.0" match="greaterOrEqual"/> + <import plugin="javax.ws.rs"/> + <import plugin="com.sun.jersey" version="1.8.0" match="greaterOrEqual"/> + <import plugin="org.junit"/> + <import plugin="com.google.guava" version="12.0.0" match="greaterOrEqual"/> + <import plugin="org.eclipse.core.runtime"/> + <import plugin="org.eclipse.osee.framework.core"/> </requires> <plugin @@ -143,4 +146,11 @@ version="0.0.0" unpack="false"/> + <plugin + id="org.eclipse.osee.cache.admin" + download-size="0" + install-size="0" + version="0.0.0" + unpack="false"/> + </feature> diff --git a/plugins/org.eclipse.osee.cache.admin.test/.classpath b/plugins/org.eclipse.osee.cache.admin.test/.classpath new file mode 100644 index 00000000000..ad32c83a788 --- /dev/null +++ b/plugins/org.eclipse.osee.cache.admin.test/.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.test/.project b/plugins/org.eclipse.osee.cache.admin.test/.project new file mode 100644 index 00000000000..924db007300 --- /dev/null +++ b/plugins/org.eclipse.osee.cache.admin.test/.project @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>org.eclipse.osee.cache.admin.test</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> + </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.test/META-INF/MANIFEST.MF b/plugins/org.eclipse.osee.cache.admin.test/META-INF/MANIFEST.MF new file mode 100644 index 00000000000..ac24658a8e9 --- /dev/null +++ b/plugins/org.eclipse.osee.cache.admin.test/META-INF/MANIFEST.MF @@ -0,0 +1,10 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Cache Admin Test (Incubation) +Bundle-SymbolicName: org.eclipse.osee.cache.admin.test +Bundle-Version: 0.11.0.qualifier +Bundle-RequiredExecutionEnvironment: JavaSE-1.6 +Bundle-Vendor: Eclipse Open System Engineering Environment +Fragment-Host: org.eclipse.osee.cache.admin +Require-Bundle: org.junit, + org.mockito;bundle-version="1.9.0" diff --git a/plugins/org.eclipse.osee.cache.admin.test/build.properties b/plugins/org.eclipse.osee.cache.admin.test/build.properties new file mode 100644 index 00000000000..34d2e4d2dad --- /dev/null +++ b/plugins/org.eclipse.osee.cache.admin.test/build.properties @@ -0,0 +1,4 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + . diff --git a/plugins/org.eclipse.osee.cache.admin.test/pom.xml b/plugins/org.eclipse.osee.cache.admin.test/pom.xml new file mode 100644 index 00000000000..5baa1f14387 --- /dev/null +++ b/plugins/org.eclipse.osee.cache.admin.test/pom.xml @@ -0,0 +1,38 @@ +<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.test</artifactId> + <packaging>eclipse-test-plugin</packaging> + <name>OSEE Cache Admin Test - (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> + <plugin> + <groupId>org.eclipse.tycho</groupId> + <artifactId>tycho-surefire-plugin</artifactId> + <version>${tycho-version}</version> + <configuration> + <testSuite>org.eclipse.osee.cache.admin.test</testSuite> + <testClass>org.eclipse.osee.cache.admin.CacheAdminTestSuite</testClass> + </configuration> + </plugin> + </plugins> + </build> + +</project>
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.cache.admin.test/src/org/eclipse/osee/cache/admin/CacheAdminTestSuite.java b/plugins/org.eclipse.osee.cache.admin.test/src/org/eclipse/osee/cache/admin/CacheAdminTestSuite.java new file mode 100644 index 00000000000..90c2f7796db --- /dev/null +++ b/plugins/org.eclipse.osee.cache.admin.test/src/org/eclipse/osee/cache/admin/CacheAdminTestSuite.java @@ -0,0 +1,25 @@ +/******************************************************************************* + * 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.cache.admin.internal.LoadingCacheTest; +import org.eclipse.osee.cache.admin.internal.NoneLoadingCacheTest; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +/** + * @author Roberto E. Escobar + */ +@RunWith(Suite.class) +@Suite.SuiteClasses({NoneLoadingCacheTest.class, LoadingCacheTest.class}) +public class CacheAdminTestSuite { + // Test Suite +} diff --git a/plugins/org.eclipse.osee.cache.admin.test/src/org/eclipse/osee/cache/admin/internal/LoadingCacheTest.java b/plugins/org.eclipse.osee.cache.admin.test/src/org/eclipse/osee/cache/admin/internal/LoadingCacheTest.java new file mode 100644 index 00000000000..f060bfc3864 --- /dev/null +++ b/plugins/org.eclipse.osee.cache.admin.test/src/org/eclipse/osee/cache/admin/internal/LoadingCacheTest.java @@ -0,0 +1,370 @@ +/******************************************************************************* + * 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 static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.concurrent.Callable; +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.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Matchers; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import com.google.common.collect.Iterables; + +/** + * Test Case for {@link CacheFactory, CacheProxy} + * + * @author Roberto E. Escobar + */ +public class LoadingCacheTest { + + private static final String KEY_1 = "key1"; + private static final String KEY_2 = "key2"; + private static final String KEY_3 = "key3"; + + private static final Object OBJECT_1 = new Object(); + private static final Object OBJECT_2 = new Object(); + private static final Object OBJECT_3 = new Object(); + private static final Object OBJECT_4 = new Object(); + + //@formatter:off + @Mock private CacheDataLoader<String, Object> dataLoader; + @Mock private CacheKeysLoader<String> keyLoader; + @Captor private ArgumentCaptor<Iterable<? extends String>> keysCaptor; + //@formatter:on + + private Cache<String, Object> cache; + private final CacheFactory factory = new CacheFactory(); + private final CacheConfiguration config = CacheConfiguration.newConfiguration(); + + @Before + public void setup() throws OseeCoreException { + MockitoAnnotations.initMocks(this); + + cache = factory.createLoadingCache(config, dataLoader, keyLoader); + } + + @Test + public void testGetIfPresent() throws OseeCoreException { + Object value = cache.getIfPresent(KEY_1); + assertNull(value); + + verify(dataLoader, never()).load(Matchers.<Iterable<? extends String>> any()); + verify(dataLoader, never()).load(anyString()); + + value = cache.get(KEY_1, createCallable(OBJECT_1)); + assertEquals(OBJECT_1, value); + + assertEquals(1, cache.size()); + + verify(dataLoader, never()).load(Matchers.<Iterable<? extends String>> any()); + verify(dataLoader, never()).load(anyString()); + + value = cache.getIfPresent(KEY_1); + assertEquals(OBJECT_1, value); + } + + @Test + public void testGetAllPresent() throws OseeCoreException { + Iterable<Object> values = cache.getAllPresent(); + assertEquals(false, values.iterator().hasNext()); + + verify(dataLoader, never()).load(Matchers.<Iterable<? extends String>> any()); + verify(dataLoader, never()).load(anyString()); + + Object value1 = cache.get(KEY_1, createCallable(OBJECT_1)); + Object value2 = cache.get(KEY_2, createCallable(OBJECT_2)); + Object value3 = cache.get(KEY_3, createCallable(OBJECT_3)); + + assertEquals(OBJECT_1, value1); + assertEquals(OBJECT_2, value2); + assertEquals(OBJECT_3, value3); + + assertEquals(3, cache.size()); + + verify(dataLoader, never()).load(Matchers.<Iterable<? extends String>> any()); + verify(dataLoader, never()).load(anyString()); + + values = cache.getAllPresent(); + + Assert.assertTrue(Iterables.contains(values, OBJECT_1)); + Assert.assertTrue(Iterables.contains(values, OBJECT_2)); + Assert.assertTrue(Iterables.contains(values, OBJECT_3)); + + verify(dataLoader, never()).load(Matchers.<Iterable<? extends String>> any()); + verify(dataLoader, never()).load(anyString()); + } + + @Test + public void testGet() throws OseeCoreException { + assertEquals(true, cache.isEmpty()); + + when(dataLoader.load(KEY_1)).thenReturn(OBJECT_1); + + Object value1 = cache.get(KEY_1); + + assertEquals(OBJECT_1, value1); + assertEquals(1, cache.size()); + assertEquals(false, cache.isEmpty()); + + verify(dataLoader, never()).load(Matchers.<Iterable<? extends String>> any()); + verify(dataLoader, times(1)).load(KEY_1); + } + + @Test + public void testGetAll() throws OseeCoreException { + Iterable<? extends String> keysInStore = Arrays.asList(KEY_1, KEY_2); + when(keyLoader.getAllKeys()).thenAnswer(createAnswer(keysInStore)); + + Map<String, Object> data = new LinkedHashMap<String, Object>(); + data.put(KEY_1, OBJECT_1); + data.put(KEY_2, OBJECT_2); + when(dataLoader.load(Matchers.<Iterable<? extends String>> any())).thenAnswer(createAnswer(data)); + + assertEquals(true, cache.isEmpty()); + + Iterable<Object> value = cache.getAll(); + + assertEquals(2, cache.size()); + assertEquals(false, cache.isEmpty()); + + assertTrue(Iterables.contains(value, OBJECT_1)); + assertTrue(Iterables.contains(value, OBJECT_2)); + + verify(dataLoader, times(1)).load(keysCaptor.capture()); + verify(dataLoader, never()).load(Matchers.anyString()); + + Iterable<? extends String> capturedIt = keysCaptor.getValue(); + assertTrue(Iterables.contains(capturedIt, KEY_1)); + assertTrue(Iterables.contains(capturedIt, KEY_2)); + + Object value3 = cache.get(KEY_3, createCallable(OBJECT_3)); + assertEquals(OBJECT_3, value3); + + verify(dataLoader, never()).load(Matchers.anyString()); + + Iterable<Object> value2 = cache.getAll(); + + assertEquals(3, cache.size()); + + assertTrue(Iterables.contains(value2, OBJECT_1)); + assertTrue(Iterables.contains(value2, OBJECT_2)); + assertTrue(Iterables.contains(value2, OBJECT_3)); + + // Load not called again + verify(dataLoader, times(1)).load(keysCaptor.capture()); + verify(dataLoader, times(0)).load(anyString()); + } + + @Test + public void testGetAllKeys() throws OseeCoreException { + Iterable<? extends String> keysInStore = Arrays.asList(KEY_1, KEY_2); + when(keyLoader.getAllKeys()).thenAnswer(createAnswer(keysInStore)); + + Map<String, Object> data = new LinkedHashMap<String, Object>(); + data.put(KEY_1, OBJECT_1); + data.put(KEY_2, OBJECT_2); + when(dataLoader.load(Matchers.<Iterable<? extends String>> any())).thenAnswer(createAnswer(data)); + + Iterable<? extends String> value = cache.getAllKeys(); + assertTrue(Iterables.contains(value, KEY_1)); + assertTrue(Iterables.contains(value, KEY_2)); + + Iterable<? extends String> presentValue = cache.getAllKeysPresent(); + assertFalse(Iterables.contains(presentValue, KEY_1)); + assertFalse(Iterables.contains(presentValue, KEY_2)); + + assertEquals(true, cache.isEmpty()); + + Object value3 = cache.get(KEY_3, createCallable(OBJECT_3)); + assertEquals(OBJECT_3, value3); + assertEquals(false, cache.isEmpty()); + + Iterable<? extends String> value1 = cache.getAllKeys(); + assertTrue(Iterables.contains(value1, KEY_1)); + assertTrue(Iterables.contains(value1, KEY_2)); + assertTrue(Iterables.contains(value1, KEY_3)); + + Iterable<? extends String> presentValue2 = cache.getAllKeysPresent(); + assertFalse(Iterables.contains(presentValue2, KEY_1)); + assertFalse(Iterables.contains(presentValue2, KEY_2)); + assertTrue(Iterables.contains(presentValue2, KEY_3)); + + Iterable<Object> allPresent = cache.getAllPresent(); + assertFalse(Iterables.contains(allPresent, OBJECT_1)); + assertFalse(Iterables.contains(allPresent, OBJECT_2)); + assertTrue(Iterables.contains(allPresent, OBJECT_3)); + + Iterable<? extends String> presentValue3 = cache.getAllKeysPresent(); + assertFalse(Iterables.contains(presentValue3, KEY_1)); + assertFalse(Iterables.contains(presentValue3, KEY_2)); + assertTrue(Iterables.contains(presentValue3, KEY_3)); + + Iterable<Object> all = cache.getAll(); + assertTrue(Iterables.contains(all, OBJECT_1)); + assertTrue(Iterables.contains(all, OBJECT_2)); + assertTrue(Iterables.contains(all, OBJECT_3)); + + Iterable<? extends String> presentValue4 = cache.getAllKeysPresent(); + assertTrue(Iterables.contains(presentValue4, KEY_1)); + assertTrue(Iterables.contains(presentValue4, KEY_2)); + assertTrue(Iterables.contains(presentValue4, KEY_3)); + } + + @Test + public void testInvalidateAll() throws OseeCoreException { + Iterable<? extends String> keysInStore = Arrays.asList(KEY_1, KEY_2, KEY_3); + when(keyLoader.getAllKeys()).thenAnswer(createAnswer(keysInStore)); + + Map<String, Object> data = new LinkedHashMap<String, Object>(); + data.put(KEY_1, OBJECT_1); + data.put(KEY_2, OBJECT_2); + data.put(KEY_3, OBJECT_3); + when(dataLoader.load(Matchers.<Iterable<? extends String>> any())).thenAnswer(createAnswer(data)); + + assertEquals(0, cache.size()); + + cache.getAll(); + + assertEquals(3, cache.size()); + + cache.invalidateAll(); + + assertEquals(0, cache.size()); + } + + @Test + public void testInvalidateKeys() throws OseeCoreException { + Iterable<? extends String> keysInStore = Arrays.asList(KEY_1, KEY_2, KEY_3); + when(keyLoader.getAllKeys()).thenAnswer(createAnswer(keysInStore)); + + Map<String, Object> data = new LinkedHashMap<String, Object>(); + data.put(KEY_1, OBJECT_1); + data.put(KEY_2, OBJECT_2); + data.put(KEY_3, OBJECT_3); + when(dataLoader.load(Matchers.<Iterable<? extends String>> any())).thenAnswer(createAnswer(data)); + + assertEquals(0, cache.size()); + + cache.getAll(); + + assertEquals(3, cache.size()); + + cache.invalidate(Arrays.asList(KEY_1, KEY_3)); + assertEquals(1, cache.size()); + + assertNull(cache.getIfPresent(KEY_1)); + assertEquals(OBJECT_2, cache.getIfPresent(KEY_2)); + assertNull(cache.getIfPresent(KEY_3)); + } + + @Test + public void testInvalidate() throws OseeCoreException { + Iterable<? extends String> keysInStore = Arrays.asList(KEY_1, KEY_2, KEY_3); + when(keyLoader.getAllKeys()).thenAnswer(createAnswer(keysInStore)); + + Map<String, Object> data = new LinkedHashMap<String, Object>(); + data.put(KEY_1, OBJECT_1); + data.put(KEY_2, OBJECT_2); + data.put(KEY_3, OBJECT_3); + when(dataLoader.load(Matchers.<Iterable<? extends String>> any())).thenAnswer(createAnswer(data)); + + assertEquals(0, cache.size()); + + cache.getAll(); + + assertEquals(3, cache.size()); + + cache.invalidate(KEY_2); + assertEquals(2, cache.size()); + + assertEquals(OBJECT_1, cache.getIfPresent(KEY_1)); + assertNull(cache.getIfPresent(KEY_2)); + assertEquals(OBJECT_3, cache.getIfPresent(KEY_3)); + } + + @Test + public void testRefresh() throws OseeCoreException { + Iterable<? extends String> keysInStore = Arrays.asList(KEY_1, KEY_2, KEY_3); + when(keyLoader.getAllKeys()).thenAnswer(createAnswer(keysInStore)); + + Map<String, Object> data = new LinkedHashMap<String, Object>(); + data.put(KEY_1, OBJECT_1); + data.put(KEY_2, OBJECT_2); + data.put(KEY_3, OBJECT_3); + when(dataLoader.load(Matchers.<Iterable<? extends String>> any())).thenAnswer(createAnswer(data)); + + when(dataLoader.reload(KEY_2, OBJECT_2)).thenReturn(OBJECT_4); + + assertEquals(0, cache.size()); + + cache.getAll(); + + assertEquals(3, cache.size()); + + verify(dataLoader, times(1)).load(Matchers.<Iterable<? extends String>> any()); + verify(dataLoader, never()).load(anyString()); + + cache.refresh(KEY_2); + + verify(dataLoader, times(1)).load(Matchers.<Iterable<? extends String>> any()); + verify(dataLoader, times(0)).load(anyString()); + verify(dataLoader, times(1)).reload(KEY_2, OBJECT_2); + + Assert.assertEquals(OBJECT_4, cache.get(KEY_2)); + + verify(dataLoader, times(1)).load(Matchers.<Iterable<? extends String>> any()); + verify(dataLoader, times(0)).load(anyString()); + verify(dataLoader, times(1)).reload(KEY_2, OBJECT_2); + } + + private static <K> Answer<K> createAnswer(final K object) { + return new Answer<K>() { + + @Override + public K answer(InvocationOnMock invocation) throws Throwable { + return object; + } + + }; + } + + private static Callable<Object> createCallable(final Object object) { + return new Callable<Object>() { + + @Override + public Object call() throws Exception { + return object; + } + }; + } +} diff --git a/plugins/org.eclipse.osee.cache.admin.test/src/org/eclipse/osee/cache/admin/internal/NoneLoadingCacheTest.java b/plugins/org.eclipse.osee.cache.admin.test/src/org/eclipse/osee/cache/admin/internal/NoneLoadingCacheTest.java new file mode 100644 index 00000000000..d3bb6ca02ee --- /dev/null +++ b/plugins/org.eclipse.osee.cache.admin.test/src/org/eclipse/osee/cache/admin/internal/NoneLoadingCacheTest.java @@ -0,0 +1,235 @@ +/******************************************************************************* + * 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 static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import java.util.Arrays; +import java.util.concurrent.Callable; +import org.eclipse.osee.cache.admin.Cache; +import org.eclipse.osee.cache.admin.CacheConfiguration; +import org.eclipse.osee.framework.core.exception.OseeCoreException; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.MockitoAnnotations; +import com.google.common.collect.Iterables; + +/** + * Test Case for {@link CacheFactory, CacheProxy} + * + * @author Roberto E. Escobar + */ +public class NoneLoadingCacheTest { + + private static final String KEY_1 = "key1"; + private static final String KEY_2 = "key2"; + private static final String KEY_3 = "key3"; + + private static final Object OBJECT_1 = new Object(); + private static final Object OBJECT_2 = new Object(); + private static final Object OBJECT_3 = new Object(); + + private Cache<String, Object> cache; + private final CacheFactory factory = new CacheFactory(); + private final CacheConfiguration config = CacheConfiguration.newConfiguration(); + + @Before + public void setup() throws OseeCoreException { + MockitoAnnotations.initMocks(this); + + cache = factory.createCache(config); + } + + @Test + public void testGetIfPresent() throws OseeCoreException { + Object value = cache.getIfPresent(KEY_1); + assertNull(value); + + value = cache.get(KEY_1, createCallable(OBJECT_1)); + assertEquals(OBJECT_1, value); + + assertEquals(1, cache.size()); + + value = cache.getIfPresent(KEY_1); + assertEquals(OBJECT_1, value); + } + + @Test + public void testGetAllPresent() throws OseeCoreException { + Iterable<Object> values = cache.getAllPresent(); + assertEquals(false, values.iterator().hasNext()); + + Object value1 = cache.get(KEY_1, createCallable(OBJECT_1)); + Object value2 = cache.get(KEY_2, createCallable(OBJECT_2)); + Object value3 = cache.get(KEY_3, createCallable(OBJECT_3)); + + assertEquals(OBJECT_1, value1); + assertEquals(OBJECT_2, value2); + assertEquals(OBJECT_3, value3); + + assertEquals(3, cache.size()); + + values = cache.getAllPresent(); + + Assert.assertTrue(Iterables.contains(values, OBJECT_1)); + Assert.assertTrue(Iterables.contains(values, OBJECT_2)); + Assert.assertTrue(Iterables.contains(values, OBJECT_3)); + } + + @Test + public void testGet() throws OseeCoreException { + assertEquals(true, cache.isEmpty()); + + Object value = cache.get(KEY_1); + assertNull(value); + + Object value1 = cache.get(KEY_1, createCallable(OBJECT_1)); + + assertEquals(OBJECT_1, value1); + assertEquals(1, cache.size()); + assertEquals(false, cache.isEmpty()); + + Object value2 = cache.get(KEY_1); + assertEquals(OBJECT_1, value2); + assertEquals(1, cache.size()); + assertEquals(false, cache.isEmpty()); + } + + @Test + public void testGetAll() throws OseeCoreException { + assertEquals(true, cache.isEmpty()); + + cache.get(KEY_1, createCallable(OBJECT_1)); + cache.get(KEY_2, createCallable(OBJECT_2)); + cache.get(KEY_3, createCallable(OBJECT_3)); + + Iterable<Object> value = cache.getAll(); + + assertEquals(3, cache.size()); + assertEquals(false, cache.isEmpty()); + + assertTrue(Iterables.contains(value, OBJECT_1)); + assertTrue(Iterables.contains(value, OBJECT_2)); + assertTrue(Iterables.contains(value, OBJECT_3)); + } + + @Test + public void testGetAllKeys() throws OseeCoreException { + assertEquals(true, cache.isEmpty()); + + cache.get(KEY_1, createCallable(OBJECT_1)); + cache.get(KEY_2, createCallable(OBJECT_2)); + cache.get(KEY_3, createCallable(OBJECT_3)); + + Iterable<? extends String> value = cache.getAllKeys(); + + assertEquals(3, cache.size()); + assertEquals(false, cache.isEmpty()); + + assertTrue(Iterables.contains(value, KEY_1)); + assertTrue(Iterables.contains(value, KEY_2)); + assertTrue(Iterables.contains(value, KEY_3)); + } + + @Test + public void testInvalidateAll() throws OseeCoreException { + assertEquals(true, cache.isEmpty()); + assertEquals(0, cache.size()); + + cache.get(KEY_1, createCallable(OBJECT_1)); + cache.get(KEY_2, createCallable(OBJECT_2)); + cache.get(KEY_3, createCallable(OBJECT_3)); + + assertEquals(3, cache.size()); + + cache.invalidateAll(); + + assertEquals(0, cache.size()); + } + + @Test + public void testInvalidateKeys() throws OseeCoreException { + assertEquals(true, cache.isEmpty()); + assertEquals(0, cache.size()); + + cache.get(KEY_1, createCallable(OBJECT_1)); + cache.get(KEY_2, createCallable(OBJECT_2)); + cache.get(KEY_3, createCallable(OBJECT_3)); + + assertEquals(3, cache.size()); + + cache.invalidate(Arrays.asList(KEY_1, KEY_3)); + assertEquals(1, cache.size()); + + assertNull(cache.getIfPresent(KEY_1)); + assertEquals(OBJECT_2, cache.getIfPresent(KEY_2)); + assertNull(cache.getIfPresent(KEY_3)); + } + + @Test + public void testInvalidate() throws OseeCoreException { + assertEquals(true, cache.isEmpty()); + assertEquals(0, cache.size()); + + cache.get(KEY_1, createCallable(OBJECT_1)); + cache.get(KEY_2, createCallable(OBJECT_2)); + cache.get(KEY_3, createCallable(OBJECT_3)); + + assertEquals(3, cache.size()); + + cache.invalidate(KEY_2); + assertEquals(2, cache.size()); + + assertEquals(OBJECT_1, cache.getIfPresent(KEY_1)); + assertNull(cache.getIfPresent(KEY_2)); + assertEquals(OBJECT_3, cache.getIfPresent(KEY_3)); + } + + @Test + public void testRefresh() throws Exception { + assertEquals(true, cache.isEmpty()); + assertEquals(0, cache.size()); + + @SuppressWarnings("unchecked") + Callable<Object> mockedCallable = mock(Callable.class); + + when(mockedCallable.call()).thenReturn(OBJECT_2); + + cache.get(KEY_1, createCallable(OBJECT_1)); + + cache.get(KEY_2, mockedCallable); + verify(mockedCallable, times(1)).call(); + assertEquals(2, cache.size()); + + cache.get(KEY_3, createCallable(OBJECT_3)); + assertEquals(3, cache.size()); + + cache.refresh(KEY_2); + + verify(mockedCallable, times(1)).call(); + } + + private static Callable<Object> createCallable(final Object object) { + return new Callable<Object>() { + + @Override + public Object call() throws Exception { + return object; + } + }; + } +} 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.framework.core.server/src/org/eclipse/osee/framework/core/server/internal/session/Cache.java b/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/Cache.java index 6f3f8872c2c..8f8a3112313 100644 --- a/plugins/org.eclipse.osee.framework.core.server/src/org/eclipse/osee/framework/core/server/internal/session/Cache.java +++ b/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/Cache.java @@ -8,8 +8,9 @@ * Contributors: * Boeing - initial API and implementation *******************************************************************************/ -package org.eclipse.osee.framework.core.server.internal.session; +package org.eclipse.osee.cache.admin; +import java.util.Map; import java.util.concurrent.Callable; import org.eclipse.osee.framework.core.exception.OseeCoreException; @@ -19,20 +20,34 @@ import org.eclipse.osee.framework.core.exception.OseeCoreException; */ 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; - V get(K key, Callable<? extends V> callable) 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 invalidateAll(Iterable<? extends K> keys); + 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.framework.core.server/src/org/eclipse/osee/framework/core/server/internal/session/ReadDataAccessor.java b/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/CacheDataLoader.java index 967203e6d8c..12daf4458bd 100644 --- a/plugins/org.eclipse.osee.framework.core.server/src/org/eclipse/osee/framework/core/server/internal/session/ReadDataAccessor.java +++ b/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/CacheDataLoader.java @@ -8,7 +8,7 @@ * Contributors: * Boeing - initial API and implementation *******************************************************************************/ -package org.eclipse.osee.framework.core.server.internal.session; +package org.eclipse.osee.cache.admin; import java.util.Map; import org.eclipse.osee.framework.core.exception.OseeCoreException; @@ -17,12 +17,11 @@ import org.eclipse.osee.framework.core.exception.OseeCoreException; * @author John Misinco * @author Roberto E. Escobar */ -public interface ReadDataAccessor<K, V> { +public interface CacheDataLoader<K, V> { Map<K, V> load(Iterable<? extends K> keys) throws OseeCoreException; V load(K key) throws OseeCoreException; - Iterable<? extends K> getAllKeys() 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.framework.core.server/src/org/eclipse/osee/framework/core/server/internal/session/CacheProxy.java b/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/internal/CacheProxy.java index 4ca35aab1cb..533277a9b4f 100644 --- a/plugins/org.eclipse.osee.framework.core.server/src/org/eclipse/osee/framework/core/server/internal/session/CacheProxy.java +++ b/plugins/org.eclipse.osee.cache.admin/src/org/eclipse/osee/cache/admin/internal/CacheProxy.java @@ -8,15 +8,17 @@ * Contributors: * Boeing - initial API and implementation *******************************************************************************/ -package org.eclipse.osee.framework.core.server.internal.session; +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.cache.LoadingCache; -import com.google.common.collect.ImmutableCollection; +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; @@ -26,22 +28,44 @@ import com.google.common.util.concurrent.UncheckedExecutionException; */ public class CacheProxy<K, V> implements Cache<K, V> { - private final LoadingCache<K, V> proxied; - private final ReadDataAccessor<K, V> accessor; + private final com.google.common.cache.Cache<K, V> proxied; - public CacheProxy(LoadingCache<K, V> proxied, ReadDataAccessor<K, V> accessor) { + public CacheProxy(com.google.common.cache.Cache<K, V> proxied) { this.proxied = proxied; - this.accessor = accessor; } @Override - public Iterable<V> getAll() throws OseeCoreException { - Iterable<? extends K> keys = accessor.getAllKeys(); - ImmutableCollection<V> toReturn = null; + 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 { - toReturn = proxied.getAll(keys).values(); - } catch (ExecutionException ex) { - OseeExceptions.wrapAndThrow(ex); + items = proxied.getAllPresent(keys); } catch (InvalidCacheLoadException ex) { OseeExceptions.wrapAndThrow(ex); } catch (UncheckedExecutionException ex) { @@ -49,16 +73,20 @@ public class CacheProxy<K, V> implements Cache<K, V> { } catch (ExecutionError ex) { OseeExceptions.wrapAndThrow(ex); } - return toReturn; + 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); + toReturn = proxied.getIfPresent(key); } catch (InvalidCacheLoadException ex) { OseeExceptions.wrapAndThrow(ex); } catch (UncheckedExecutionException ex) { @@ -71,7 +99,7 @@ public class CacheProxy<K, V> implements Cache<K, V> { @Override public void refresh(K key) { - proxied.refresh(key); + // Does nothing; } @Override @@ -80,7 +108,7 @@ public class CacheProxy<K, V> implements Cache<K, V> { } @Override - public void invalidateAll(Iterable<? extends K> keys) { + public void invalidate(Iterable<? extends K> keys) { proxied.invalidateAll(keys); } @@ -111,4 +139,9 @@ public class CacheProxy<K, V> implements Cache<K, V> { 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; + } + +} diff --git a/plugins/org.eclipse.osee.framework.core.server.test/src/org/eclipse/osee/framework/core/server/test/internal/session/SessionCacheTest.java b/plugins/org.eclipse.osee.framework.core.server.test/src/org/eclipse/osee/framework/core/server/test/internal/session/SessionCacheTest.java deleted file mode 100644 index 50caa8bcd09..00000000000 --- a/plugins/org.eclipse.osee.framework.core.server.test/src/org/eclipse/osee/framework/core/server/test/internal/session/SessionCacheTest.java +++ /dev/null @@ -1,102 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2004, 2007 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.core.server.test.internal.session; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Matchers.anyList; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import org.eclipse.osee.framework.core.exception.OseeCoreException; -import org.eclipse.osee.framework.core.server.internal.session.Cache; -import org.eclipse.osee.framework.core.server.internal.session.CacheFactory; -import org.eclipse.osee.framework.core.server.internal.session.ReadDataAccessor; -import org.eclipse.osee.framework.core.server.internal.session.Session; -import org.junit.Before; -import org.junit.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.Captor; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -/** - * Test Case for {@link SessionCacheTest} - * - * @author Roberto E. Escobar - */ -public class SessionCacheTest { - - private static Cache<String, Session> cache; - - @Mock - private ReadDataAccessor<String, Session> lda; - @Captor - private ArgumentCaptor<Iterable<? extends String>> idCaptor; - - @Before - public void initMocks() { - MockitoAnnotations.initMocks(this); - - CacheFactory factory = new CacheFactory(); - cache = factory.create(lda); - } - - @Test - public void testGet() throws OseeCoreException { - Session s = mock(Session.class); - when(s.getGuid()).thenReturn("id1"); - when(lda.load("id1")).thenReturn(s); - - Session toCheck = cache.get("id1"); - - assertEquals(s, toCheck); - verify(lda).load("id1"); - } - - @SuppressWarnings("unchecked") - @Test - public void testGetAll() throws OseeCoreException { - List<String> allIds = new LinkedList<String>(); - allIds.add("id1"); - doReturn(allIds).when(lda).getAllKeys(); - Map<String, Session> map = new HashMap<String, Session>(); - Session session = mock(Session.class); - map.put("id1", session); - when(lda.load(anyList())).thenReturn(map); - Iterable<Session> all = cache.getAll(); - verify(lda).getAllKeys(); - verify(lda).load(idCaptor.capture()); - assertEquals("id1", idCaptor.getValue().iterator().next()); - assertEquals(session, all.iterator().next()); - } - - @Test - public void testReloadCache() throws OseeCoreException { - Session s1 = mock(Session.class); - Session s2 = mock(Session.class); - when(s1.getGuid()).thenReturn("id1"); - when(s1.getGuid()).thenReturn("id1"); - - when(lda.load("id1")).thenReturn(s1, s2); - - Session toCheck = cache.get("id1"); - assertEquals(s1, toCheck); - - cache.invalidateAll(); - toCheck = cache.get("id1"); - assertEquals(s2, toCheck); - } -} diff --git a/plugins/org.eclipse.osee.framework.core.server.test/src/org/eclipse/osee/framework/core/server/test/internal/session/SessionTestSuite.java b/plugins/org.eclipse.osee.framework.core.server.test/src/org/eclipse/osee/framework/core/server/test/internal/session/SessionTestSuite.java index c4acdc51551..316a39f7e25 100644 --- a/plugins/org.eclipse.osee.framework.core.server.test/src/org/eclipse/osee/framework/core/server/test/internal/session/SessionTestSuite.java +++ b/plugins/org.eclipse.osee.framework.core.server.test/src/org/eclipse/osee/framework/core/server/test/internal/session/SessionTestSuite.java @@ -16,7 +16,6 @@ import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({ // SessionTest.class, // - SessionCacheTest.class, // SessionFactoryTest.class, // SessionManagerTest.class, // }) diff --git a/plugins/org.eclipse.osee.framework.core.server/META-INF/MANIFEST.MF b/plugins/org.eclipse.osee.framework.core.server/META-INF/MANIFEST.MF index fe85f36675c..fa7ef923473 100644 --- a/plugins/org.eclipse.osee.framework.core.server/META-INF/MANIFEST.MF +++ b/plugins/org.eclipse.osee.framework.core.server/META-INF/MANIFEST.MF @@ -8,6 +8,7 @@ Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Import-Package: javax.servlet;version="2.5.0", javax.servlet.http;version="2.5.0", org.apache.commons.codec.binary;version="1.3.0", + org.eclipse.osee.cache.admin, org.eclipse.osee.console.admin, org.eclipse.osee.framework.core.data, org.eclipse.osee.framework.core.enums, @@ -29,5 +30,4 @@ Import-Package: javax.servlet;version="2.5.0", Export-Package: org.eclipse.osee.framework.core.server Service-Component: OSGI-INF/*.xml Require-Bundle: org.eclipse.core.runtime, - javax.servlet;bundle-version="2.5.0", - com.google.guava;bundle-version="12.0.0" + javax.servlet;bundle-version="2.5.0" diff --git a/plugins/org.eclipse.osee.framework.core.server/OSGI-INF/session.manager.xml b/plugins/org.eclipse.osee.framework.core.server/OSGI-INF/session.manager.xml index 7cc510c7aec..478b8230bd7 100644 --- a/plugins/org.eclipse.osee.framework.core.server/OSGI-INF/session.manager.xml +++ b/plugins/org.eclipse.osee.framework.core.server/OSGI-INF/session.manager.xml @@ -8,4 +8,5 @@ <reference bind="setServerManager" cardinality="1..1" interface="org.eclipse.osee.framework.core.server.IApplicationServerManager" name="IApplicationServerManager" policy="static"/> <reference bind="setAuthenticationManager" cardinality="1..1" interface="org.eclipse.osee.framework.core.server.IAuthenticationManager" name="IAuthenticationManager" policy="static"/> <reference bind="setDbInfoRegistry" cardinality="1..1" interface="org.eclipse.osee.framework.database.DatabaseInfoRegistry" name="DatabaseInfoRegistry" policy="static"/> + <reference bind="setCacheAdmin" cardinality="1..1" interface="org.eclipse.osee.cache.admin.CacheAdmin" name="CacheAdmin" policy="static"/> </scr:component> diff --git a/plugins/org.eclipse.osee.framework.core.server/build.properties b/plugins/org.eclipse.osee.framework.core.server/build.properties index ddb8ca2417e..aabcc85ab19 100644 --- a/plugins/org.eclipse.osee.framework.core.server/build.properties +++ b/plugins/org.eclipse.osee.framework.core.server/build.properties @@ -8,5 +8,4 @@ bin.includes = META-INF/,\ source.. = src/ additional.bundles = org.eclipse.osee.logger,\ org.eclipse.osee.framework.jdk.core,\ - org.eclipse.osee.console.admin,\ - com.google.guava + org.eclipse.osee.console.admin diff --git a/plugins/org.eclipse.osee.framework.core.server/src/org/eclipse/osee/framework/core/server/internal/session/CacheFactory.java b/plugins/org.eclipse.osee.framework.core.server/src/org/eclipse/osee/framework/core/server/internal/session/CacheFactory.java deleted file mode 100644 index e7c77da1483..00000000000 --- a/plugins/org.eclipse.osee.framework.core.server/src/org/eclipse/osee/framework/core/server/internal/session/CacheFactory.java +++ /dev/null @@ -1,42 +0,0 @@ -/******************************************************************************* - * 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.framework.core.server.internal.session; - -import java.util.Map; -import org.eclipse.osee.framework.core.exception.OseeCoreException; -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.CacheLoader; -import com.google.common.cache.LoadingCache; - -/** - * @author John Misinco - * @author Roberto E. Escobar - */ -public class CacheFactory { - - public <K, V> Cache<K, V> create(final ReadDataAccessor<K, V> accessor) { - final LoadingCache<K, V> loadingCache = CacheBuilder.newBuilder().build(new CacheLoader<K, V>() { - - @Override - public Map<K, V> loadAll(Iterable<? extends K> keys) throws OseeCoreException { - return accessor.load(keys); - } - - @Override - public V load(K key) throws OseeCoreException { - return accessor.load(key); - } - }); - Cache<K, V> toReturn = new CacheProxy<K, V>(loadingCache, accessor); - return toReturn; - } - -} diff --git a/plugins/org.eclipse.osee.framework.core.server/src/org/eclipse/osee/framework/core/server/internal/session/DatabaseSessionAccessor.java b/plugins/org.eclipse.osee.framework.core.server/src/org/eclipse/osee/framework/core/server/internal/session/DatabaseSessionAccessor.java index 6d570b551fb..11678983e6b 100644 --- a/plugins/org.eclipse.osee.framework.core.server/src/org/eclipse/osee/framework/core/server/internal/session/DatabaseSessionAccessor.java +++ b/plugins/org.eclipse.osee.framework.core.server/src/org/eclipse/osee/framework/core/server/internal/session/DatabaseSessionAccessor.java @@ -18,6 +18,8 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; +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.database.IOseeDatabaseService; import org.eclipse.osee.framework.database.core.DatabaseTransactions; @@ -27,7 +29,7 @@ import org.eclipse.osee.framework.database.core.OseeConnection; /** * @author Roberto E. Escobar */ -public final class DatabaseSessionAccessor implements ReadDataAccessor<String, Session>, WriteDataAccessor<Session> { +public final class DatabaseSessionAccessor implements CacheDataLoader<String, Session>, CacheKeysLoader<String>, WriteDataAccessor<Session> { private static final String INSERT_SESSION = "INSERT INTO osee_session (managed_by_server_id, session_id, user_id, client_machine_name, client_address, client_port, client_version, created_on, last_interaction_date, last_interaction) VALUES (?,?,?,?,?,?,?,?,?,?)"; @@ -115,6 +117,11 @@ public final class DatabaseSessionAccessor implements ReadDataAccessor<String, S } @Override + public Session reload(String key, Session oldValue) throws OseeCoreException { + return load(key); + }; + + @Override public Iterable<? extends String> getAllKeys() throws OseeCoreException { final Set<String> ids = new LinkedHashSet<String>(); ISessionCollector idCollector = new ISessionCollector() { @@ -233,5 +240,6 @@ public final class DatabaseSessionAccessor implements ReadDataAccessor<String, S public void handleTxFinally() { // } - }; + } + } diff --git a/plugins/org.eclipse.osee.framework.core.server/src/org/eclipse/osee/framework/core/server/internal/session/SessionManagerImpl.java b/plugins/org.eclipse.osee.framework.core.server/src/org/eclipse/osee/framework/core/server/internal/session/SessionManagerImpl.java index a59b04d5178..796540483c2 100644 --- a/plugins/org.eclipse.osee.framework.core.server/src/org/eclipse/osee/framework/core/server/internal/session/SessionManagerImpl.java +++ b/plugins/org.eclipse.osee.framework.core.server/src/org/eclipse/osee/framework/core/server/internal/session/SessionManagerImpl.java @@ -17,6 +17,7 @@ import java.util.Date; import java.util.HashSet; import java.util.Set; import java.util.concurrent.Callable; +import org.eclipse.osee.cache.admin.Cache; import org.eclipse.osee.framework.core.data.IUserToken; import org.eclipse.osee.framework.core.data.OseeCredential; import org.eclipse.osee.framework.core.data.OseeSessionGrant; @@ -163,7 +164,7 @@ public final class SessionManagerImpl implements ISessionManager { if (!sessions.isEmpty()) { storeDataAccessor.delete(sessions); } - sessionCache.invalidateAll(Arrays.asList(sessionIds)); + sessionCache.invalidate(Arrays.asList(sessionIds)); } } diff --git a/plugins/org.eclipse.osee.framework.core.server/src/org/eclipse/osee/framework/core/server/internal/session/SessionManagerService.java b/plugins/org.eclipse.osee.framework.core.server/src/org/eclipse/osee/framework/core/server/internal/session/SessionManagerService.java index 78f3e084523..2777b3127d8 100644 --- a/plugins/org.eclipse.osee.framework.core.server/src/org/eclipse/osee/framework/core/server/internal/session/SessionManagerService.java +++ b/plugins/org.eclipse.osee.framework.core.server/src/org/eclipse/osee/framework/core/server/internal/session/SessionManagerService.java @@ -11,6 +11,9 @@ package org.eclipse.osee.framework.core.server.internal.session; import java.util.Collection; +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.framework.core.data.OseeCredential; import org.eclipse.osee.framework.core.data.OseeSessionGrant; import org.eclipse.osee.framework.core.exception.OseeCoreException; @@ -34,7 +37,7 @@ public final class SessionManagerService implements ISessionManager { private IOseeDatabaseService dbService; private IApplicationServerManager serverManager; private IAuthenticationManager authenticationManager; - + private CacheAdmin cacheAdmin; private ISessionManager proxiedSessionManager; public void setLogger(Log logger) { @@ -57,6 +60,10 @@ public final class SessionManagerService implements ISessionManager { this.authenticationManager = authenticationManager; } + public void setCacheAdmin(CacheAdmin cacheAdmin) { + this.cacheAdmin = cacheAdmin; + } + private IOseeDatabaseService getDbService() { return dbService; } @@ -69,7 +76,7 @@ public final class SessionManagerService implements ISessionManager { return authenticationManager; } - public void start() { + public void start() throws OseeCoreException { String serverId = getServerManager().getId(); BuildTypeIdentifier identifier = new BuildTypeIdentifier(new BuildTypeDataProvider()); @@ -80,8 +87,8 @@ public final class SessionManagerService implements ISessionManager { DatabaseSessionAccessor accessor = new DatabaseSessionAccessor(serverId, sessionFactory, sessionQuery, getDbService()); - CacheFactory cacheFactory = new CacheFactory(); - Cache<String, Session> sessionCache = cacheFactory.create(accessor); + CacheConfiguration config = CacheConfiguration.newConfiguration(); + Cache<String, Session> sessionCache = cacheAdmin.createLoadingCache(config, accessor, accessor); proxiedSessionManager = new SessionManagerImpl(serverId, sessionFactory, sessionQuery, sessionCache, getAuthenticationManager(), diff --git a/plugins/org.eclipse.osee.support.config/launchConfig/OSEE_Application_Server_[H2].launch b/plugins/org.eclipse.osee.support.config/launchConfig/OSEE_Application_Server_[H2].launch index fc1a50e2a71..bc3dec53f22 100644 --- a/plugins/org.eclipse.osee.support.config/launchConfig/OSEE_Application_Server_[H2].launch +++ b/plugins/org.eclipse.osee.support.config/launchConfig/OSEE_Application_Server_[H2].launch @@ -39,5 +39,5 @@ <booleanAttribute key="tracing" value="false"/> <booleanAttribute key="useCustomFeatures" value="false"/> <booleanAttribute key="useDefaultConfigArea" value="true"/> -<stringAttribute key="workspace_bundles" value="com.sun.jersey@default:default,com.vaadin@default:default,javax.ws.rs@default:default,org.eclipse.osee.ats.api@default:default,org.eclipse.osee.ats.presenter.test@default:false,org.eclipse.osee.ats.presenter@default:default,org.eclipse.osee.ats.ui.api@default:default,org.eclipse.osee.ats.view.web.test@default:false,org.eclipse.osee.ats.view.web@default:default,org.eclipse.osee.console.admin.test@default:false,org.eclipse.osee.console.admin@default:default,org.eclipse.osee.database.schema@default:default,org.eclipse.osee.database@default:default,org.eclipse.osee.display.api@default:default,org.eclipse.osee.display.mvp.test@default:false,org.eclipse.osee.display.mvp@default:default,org.eclipse.osee.display.presenter.mocks@default:default,org.eclipse.osee.display.presenter.test@default:false,org.eclipse.osee.display.presenter@default:default,org.eclipse.osee.display.view.web@default:default,org.eclipse.osee.event.osgi@default:default,org.eclipse.osee.event@default:default,org.eclipse.osee.executor.admin.test@default:false,org.eclipse.osee.executor.admin@default:default,org.eclipse.osee.framework.authentication.ldap.core@default:default,org.eclipse.osee.framework.core.dsl.integration.test@default:false,org.eclipse.osee.framework.core.dsl.integration@default:default,org.eclipse.osee.framework.core.dsl@default:default,org.eclipse.osee.framework.core.message.test@default:false,org.eclipse.osee.framework.core.message@default:default,org.eclipse.osee.framework.core.model.test@default:false,org.eclipse.osee.framework.core.model@default:default,org.eclipse.osee.framework.core.server.test@default:false,org.eclipse.osee.framework.core.server@default:default,org.eclipse.osee.framework.core.test@default:false,org.eclipse.osee.framework.core@default:default,org.eclipse.osee.framework.database.test@default:false,org.eclipse.osee.framework.database@default:default,org.eclipse.osee.framework.h2@default:default,org.eclipse.osee.framework.jdk.core.test@default:false,org.eclipse.osee.framework.jdk.core@default:default,org.eclipse.osee.framework.logging.test@default:false,org.eclipse.osee.framework.logging@default:default,org.eclipse.osee.framework.manager.servlet@default:default,org.eclipse.osee.framework.resource.management.test@default:false,org.eclipse.osee.framework.resource.management@default:default,org.eclipse.osee.logback.config@default:false,org.eclipse.osee.logger.slf4j@default:default,org.eclipse.osee.logger@default:default,org.eclipse.osee.mail.test@default:false,org.eclipse.osee.mail@default:default,org.eclipse.osee.orcs.core.test@default:false,org.eclipse.osee.orcs.core@default:default,org.eclipse.osee.orcs.db.mock@default:default,org.eclipse.osee.orcs.db.test@default:false,org.eclipse.osee.orcs.db@default:default,org.eclipse.osee.orcs.rest.model@default:default,org.eclipse.osee.orcs.rest.test@default:false,org.eclipse.osee.orcs.rest@default:default,org.eclipse.osee.orcs.test@default:false,org.eclipse.osee.orcs@default:default,org.eclipse.osee.rest.admin@default:default,org.eclipse.osee.vaadin.themes@default:false,org.eclipse.osee.vaadin.widgets@default:default,org.eclipse.osee.vaadin@default:default,org.jvnet.mimepull@default:default,org.mockito@default:default,org.objenesis@default:default"/> +<stringAttribute key="workspace_bundles" value="com.sun.jersey@default:default,com.vaadin@default:default,javax.ws.rs@default:default,org.eclipse.osee.ats.api@default:default,org.eclipse.osee.ats.presenter.test@default:false,org.eclipse.osee.ats.presenter@default:default,org.eclipse.osee.ats.ui.api@default:default,org.eclipse.osee.ats.view.web.test@default:false,org.eclipse.osee.ats.view.web@default:default,org.eclipse.osee.cache.admin.test@default:default,org.eclipse.osee.cache.admin@default:default,org.eclipse.osee.console.admin.test@default:false,org.eclipse.osee.console.admin@default:default,org.eclipse.osee.database.schema@default:default,org.eclipse.osee.database@default:default,org.eclipse.osee.display.api@default:default,org.eclipse.osee.display.mvp.test@default:false,org.eclipse.osee.display.mvp@default:default,org.eclipse.osee.display.presenter.mocks@default:default,org.eclipse.osee.display.presenter.test@default:false,org.eclipse.osee.display.presenter@default:default,org.eclipse.osee.display.view.web@default:default,org.eclipse.osee.event.osgi@default:default,org.eclipse.osee.event@default:default,org.eclipse.osee.executor.admin.test@default:false,org.eclipse.osee.executor.admin@default:default,org.eclipse.osee.framework.authentication.ldap.core@default:default,org.eclipse.osee.framework.core.dsl.integration.test@default:false,org.eclipse.osee.framework.core.dsl.integration@default:default,org.eclipse.osee.framework.core.dsl@default:default,org.eclipse.osee.framework.core.message.test@default:false,org.eclipse.osee.framework.core.message@default:default,org.eclipse.osee.framework.core.model.test@default:false,org.eclipse.osee.framework.core.model@default:default,org.eclipse.osee.framework.core.server.test@default:false,org.eclipse.osee.framework.core.server@default:default,org.eclipse.osee.framework.core.test@default:false,org.eclipse.osee.framework.core@default:default,org.eclipse.osee.framework.database.test@default:false,org.eclipse.osee.framework.database@default:default,org.eclipse.osee.framework.h2@default:default,org.eclipse.osee.framework.jdk.core.test@default:false,org.eclipse.osee.framework.jdk.core@default:default,org.eclipse.osee.framework.logging.test@default:false,org.eclipse.osee.framework.logging@default:default,org.eclipse.osee.framework.manager.servlet@default:default,org.eclipse.osee.framework.resource.management.test@default:false,org.eclipse.osee.framework.resource.management@default:default,org.eclipse.osee.logback.config@default:false,org.eclipse.osee.logger.slf4j@default:default,org.eclipse.osee.logger@default:default,org.eclipse.osee.mail.test@default:false,org.eclipse.osee.mail@default:default,org.eclipse.osee.orcs.core.test@default:false,org.eclipse.osee.orcs.core@default:default,org.eclipse.osee.orcs.db.mock@default:default,org.eclipse.osee.orcs.db.test@default:false,org.eclipse.osee.orcs.db@default:default,org.eclipse.osee.orcs.rest.model@default:default,org.eclipse.osee.orcs.rest.test@default:false,org.eclipse.osee.orcs.rest@default:default,org.eclipse.osee.orcs.test@default:false,org.eclipse.osee.orcs@default:default,org.eclipse.osee.rest.admin@default:default,org.eclipse.osee.vaadin.themes@default:false,org.eclipse.osee.vaadin.widgets@default:default,org.eclipse.osee.vaadin@default:default,org.jvnet.mimepull@default:default,org.mockito@default:default,org.objenesis@default:default"/> </launchConfiguration> diff --git a/plugins/org.eclipse.osee.x.core.parent/pom.xml b/plugins/org.eclipse.osee.x.core.parent/pom.xml index 6879dbd9097..dea18d3ee89 100644 --- a/plugins/org.eclipse.osee.x.core.parent/pom.xml +++ b/plugins/org.eclipse.osee.x.core.parent/pom.xml @@ -31,6 +31,9 @@ <module>../../plugins/org.eclipse.osee.console.admin</module> <module>../../plugins/org.eclipse.osee.console.admin.test</module> + <module>../../plugins/org.eclipse.osee.cache.admin</module> + <module>../../plugins/org.eclipse.osee.cache.admin.test</module> + <module>../../plugins/org.eclipse.osee.executor.admin</module> <module>../../plugins/org.eclipse.osee.executor.admin.test</module> |