usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From snoopd...@apache.org
Subject [07/24] usergrid git commit: Plug our Cassandra backed auth-info cache into Shiro, some tests are failing with the new cache.
Date Wed, 21 Oct 2015 20:36:24 GMT
Plug our Cassandra backed auth-info cache into Shiro, some tests are failing with the new cache.


Project: http://git-wip-us.apache.org/repos/asf/usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/usergrid/commit/dc7681f4
Tree: http://git-wip-us.apache.org/repos/asf/usergrid/tree/dc7681f4
Diff: http://git-wip-us.apache.org/repos/asf/usergrid/diff/dc7681f4

Branch: refs/heads/master
Commit: dc7681f471aee41148c1931cf55f976a29d1a99e
Parents: 08934c2
Author: Dave Johnson <snoopdave@apache.org>
Authored: Tue Sep 22 11:15:26 2015 -0400
Committer: Dave Johnson <snoopdave@apache.org>
Committed: Tue Sep 22 11:15:26 2015 -0400

----------------------------------------------------------------------
 stack/core/pom.xml                              |  9 ++-
 .../usergrid/corepersistence/CoreModule.java    |  2 +
 .../usergrid/persistence/cache/ScopedCache.java | 10 ++-
 .../persistence/cache/guice/CacheModule.java    |  7 +-
 .../persistence/cache/impl/ScopedCacheImpl.java | 13 +++-
 .../cache/impl/ScopedCacheSerialization.java    |  7 +-
 .../impl/ScopedCacheSerializationImpl.java      | 50 ++++++++++--
 .../persistence/cache/CacheTestModule.java      | 16 +++-
 .../persistence/cache/ScopedCacheTest.java      | 44 ++++++++---
 .../rest/security/shiro/ShiroCache.java         | 80 ++++++++++++++++----
 .../rest/security/shiro/ShiroCacheManager.java  | 17 ++++-
 .../collection/users/PermissionsResourceIT.java |  6 +-
 stack/rest/src/test/resources/log4j.properties  |  1 +
 .../services/guice/ServiceModuleImpl.java       | 45 ++++++-----
 14 files changed, 236 insertions(+), 71 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/usergrid/blob/dc7681f4/stack/core/pom.xml
----------------------------------------------------------------------
diff --git a/stack/core/pom.xml b/stack/core/pom.xml
index be2d28c..7944d96 100644
--- a/stack/core/pom.xml
+++ b/stack/core/pom.xml
@@ -423,6 +423,7 @@
            <scope>test</scope>
        </dependency>
 
+
     <!-- Core Persistence deps -->
     <dependency>
 	    <groupId>org.apache.usergrid</groupId>
@@ -463,7 +464,13 @@
       <type>jar</type>
     </dependency>
 
-    <dependency>
+      <dependency>
+          <groupId>org.apache.usergrid</groupId>
+          <artifactId>cache</artifactId>
+          <version>2.1.0-SNAPSHOT</version>
+      </dependency>
+
+      <dependency>
       <groupId>org.apache.usergrid</groupId>
       <artifactId>queue</artifactId>
       <version>2.1.0-SNAPSHOT</version>

http://git-wip-us.apache.org/repos/asf/usergrid/blob/dc7681f4/stack/core/src/main/java/org/apache/usergrid/corepersistence/CoreModule.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CoreModule.java
b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CoreModule.java
index 959edec..ae69b6f 100644
--- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CoreModule.java
+++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CoreModule.java
@@ -16,6 +16,7 @@
 package org.apache.usergrid.corepersistence;
 
 
+import org.apache.usergrid.persistence.cache.guice.CacheModule;
 import org.safehaus.guicyfig.GuicyFigModule;
 
 import org.apache.usergrid.corepersistence.asyncevents.AsyncEventService;
@@ -82,6 +83,7 @@ public class CoreModule  extends AbstractModule {
     protected void configure() {
 
         install( new CommonModule());
+        install( new CacheModule());
         install( new CollectionModule() {
             /**
              * configure our migration data provider for all entities in the system

http://git-wip-us.apache.org/repos/asf/usergrid/blob/dc7681f4/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/ScopedCache.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/ScopedCache.java
b/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/ScopedCache.java
index 29c08c8..23377d0 100644
--- a/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/ScopedCache.java
+++ b/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/ScopedCache.java
@@ -17,17 +17,23 @@
 package org.apache.usergrid.persistence.cache;
 
 
+import com.fasterxml.jackson.core.type.TypeReference;
+
 /**
  * Cache divided into scopes which can be individually invalidated.
  */
 public interface ScopedCache<K,V> {
 
     /** Put value into scope with ttl (null for no ttl) */
-    void put( K key, V value, Integer ttl );
+    V put( K key, V value, Integer ttl );
 
     /** Get value from scope */
-    V get( K key );
+    V get( K key, TypeReference typeRef );
+
+    /** Delete a specific cached item */
+    void remove( K key );
 
     /** Delete all cache data of all types in this scope */
     void invalidate();
+
 }

http://git-wip-us.apache.org/repos/asf/usergrid/blob/dc7681f4/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/guice/CacheModule.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/guice/CacheModule.java
b/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/guice/CacheModule.java
index d744fcf..981cea7 100644
--- a/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/guice/CacheModule.java
+++ b/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/guice/CacheModule.java
@@ -39,13 +39,8 @@ public class CacheModule extends AbstractModule {
     protected void configure() {
 
         bind( CacheFactory.class ).to( CacheFactoryImpl.class );
-        bind( ScopedCacheSerialization.class ).to( ScopedCacheSerializationImpl.class );
-
-        bind( new TypeLiteral<CacheFactory<String, Map<String, Object>>>()
{} )
-            .to(new TypeLiteral<CacheFactoryImpl<String, Map<String, Object>>>()
{});
 
-        bind( new TypeLiteral<ScopedCacheSerialization<String, Map<String, Object>>>()
{} )
-            .to(new TypeLiteral<ScopedCacheSerializationImpl<String, Map<String,
Object>>>() {});
+        bind( ScopedCacheSerialization.class ).to( ScopedCacheSerializationImpl.class );
 
         Multibinder<Migration> migrationBinding = Multibinder.newSetBinder( binder(),
Migration.class );
         migrationBinding.addBinding().to(Key.get(ScopedCacheSerialization.class));

http://git-wip-us.apache.org/repos/asf/usergrid/blob/dc7681f4/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheImpl.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheImpl.java
b/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheImpl.java
index 7e3bbf7..23a6573 100644
--- a/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheImpl.java
+++ b/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheImpl.java
@@ -16,6 +16,7 @@
  */
 package org.apache.usergrid.persistence.cache.impl;
 
+import com.fasterxml.jackson.core.type.TypeReference;
 import com.google.inject.Inject;
 import org.apache.usergrid.persistence.cache.CacheScope;
 import org.apache.usergrid.persistence.cache.ScopedCache;
@@ -36,13 +37,17 @@ public class ScopedCacheImpl<K,V> implements ScopedCache<K,V>
{
     }
 
     @Override
-    public void put(K key, V value, Integer ttl) {
-        serializer.writeValue( scope, key, value, ttl );
+    public V put(K key, V value, Integer ttl) {
+        return serializer.writeValue( scope, key, value, ttl );
     }
 
     @Override
-    public V get(K key) {
-        return serializer.readValue( scope, key );
+    public V get(K key, TypeReference typeRef ) {
+        return serializer.readValue( scope, key, typeRef );
+    }
+
+    public void remove( K key ) {
+        serializer.removeValue( scope, key );
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/usergrid/blob/dc7681f4/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheSerialization.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheSerialization.java
b/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheSerialization.java
index 7ef5882..0ff690d 100644
--- a/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheSerialization.java
+++ b/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheSerialization.java
@@ -17,6 +17,7 @@
 package org.apache.usergrid.persistence.cache.impl;
 
 
+import com.fasterxml.jackson.core.type.TypeReference;
 import org.apache.usergrid.persistence.cache.CacheScope;
 import org.apache.usergrid.persistence.core.migration.schema.Migration;
 
@@ -26,9 +27,11 @@ import org.apache.usergrid.persistence.core.migration.schema.Migration;
  */
 public interface ScopedCacheSerialization<K,V> extends Migration {
 
-    V readValue( CacheScope scope, K key );
+    V readValue( CacheScope scope, K key, TypeReference typeRef );
 
-    void writeValue( CacheScope scope, K key, V value, Integer ttl );
+    V writeValue( CacheScope scope, K key, V value, Integer ttl );
+
+    void removeValue( CacheScope scope, K key );
 
     void invalidate( CacheScope scope );
 }

http://git-wip-us.apache.org/repos/asf/usergrid/blob/dc7681f4/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheSerializationImpl.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheSerializationImpl.java
b/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheSerializationImpl.java
index 899d0bf..3fc7332 100644
--- a/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheSerializationImpl.java
+++ b/stack/corepersistence/cache/src/main/java/org/apache/usergrid/persistence/cache/impl/ScopedCacheSerializationImpl.java
@@ -20,10 +20,12 @@ import com.fasterxml.jackson.core.JsonFactory;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.dataformat.smile.SmileFactory;
 import com.google.common.base.Preconditions;
 import com.google.common.hash.Funnel;
 import com.google.common.hash.PrimitiveSink;
 import com.google.inject.Inject;
+import com.netflix.astyanax.ColumnListMutation;
 import com.netflix.astyanax.Keyspace;
 import com.netflix.astyanax.MutationBatch;
 import com.netflix.astyanax.Serializer;
@@ -97,9 +99,9 @@ public class ScopedCacheSerializationImpl<K,V> implements ScopedCacheSerializati
 
     private final Keyspace keyspace;
 
-    private final JsonFactory JSON_FACTORY = new JsonFactory();
+    private final SmileFactory SMILE_FACTORY = new SmileFactory();
 
-    private final ObjectMapper MAPPER = new ObjectMapper( JSON_FACTORY );
+    private final ObjectMapper MAPPER = new ObjectMapper( SMILE_FACTORY );
 
 
     //------------------------------------------------------------------------------------------
@@ -111,7 +113,7 @@ public class ScopedCacheSerializationImpl<K,V> implements ScopedCacheSerializati
 
 
     @Override
-    public V readValue(CacheScope scope, K key) {
+    public V readValue(CacheScope scope, K key, TypeReference typeRef ) {
 
         Preconditions.checkNotNull(scope, "scope is required");
         Preconditions.checkNotNull(key, "key is required");
@@ -131,7 +133,12 @@ public class ScopedCacheSerializationImpl<K,V> implements ScopedCacheSerializati
                     .getKey(keyRowKey).getColumn( columnName ).execute().getResult();
 
                 result.getByteBufferValue();
-                V value = MAPPER.readValue(result.getByteArrayValue(), new TypeReference<V>()
{});
+                //V value = MAPPER.readValue(result.getByteArrayValue(), new TypeReference<V>()
{});
+                V value = MAPPER.readValue(result.getByteArrayValue(), typeRef);
+
+                logger.info("Read cache item\n   key/value types {}/{}\n   key:value: {}:{}",
+                    new Object[] { key.getClass().getSimpleName(), value.getClass().getSimpleName(),
key, value });
+
                 return value;
 
             } catch (NotFoundException nfe) {
@@ -145,12 +152,14 @@ public class ScopedCacheSerializationImpl<K,V> implements ScopedCacheSerializati
             throw new RuntimeException("Unable to connect to cassandra", e);
         }
 
+        logger.info("Cache value not found for key {}", key );
+
         return null;
     }
 
 
     @Override
-    public void writeValue(CacheScope scope, K key, V value, Integer ttl) {
+    public V writeValue(CacheScope scope, K key, V value, Integer ttl) {
 
         Preconditions.checkNotNull( scope, "scope is required");
         Preconditions.checkNotNull( key, "key is required" );
@@ -163,7 +172,7 @@ public class ScopedCacheSerializationImpl<K,V> implements ScopedCacheSerializati
         final int bucket = BUCKET_LOCATOR.getCurrentBucket(rowKeyString);
 
         final BucketScopedRowKey<String> keyRowKey =
-            BucketScopedRowKey.fromKey( scope.getApplication(), rowKeyString, bucket);
+            BucketScopedRowKey.fromKey(scope.getApplication(), rowKeyString, bucket);
 
         // determine column name based on K key to string
         String columnName = key.toString();
@@ -181,6 +190,35 @@ public class ScopedCacheSerializationImpl<K,V> implements ScopedCacheSerializati
         batch.withRow(SCOPED_CACHE, keyRowKey).putColumn(columnName, cacheBytes, ttl);
 
         executeBatch(batch);
+
+        logger.info("Wrote cache item\n   key/value types {}/{}\n   key:value: {}:{}",
+            new Object[]{key.getClass().getSimpleName(), value.getClass().getSimpleName(),
key, value});
+
+        return value;
+    }
+
+
+    @Override
+    public void removeValue(CacheScope scope, K key) {
+
+        Preconditions.checkNotNull( scope, "scope is required");
+        Preconditions.checkNotNull( key, "key is required" );
+
+        // determine bucketed row-key based application UUID
+
+        String rowKeyString = scope.getApplication().getUuid().toString();
+        final int bucket = BUCKET_LOCATOR.getCurrentBucket(rowKeyString);
+
+        final BucketScopedRowKey<String> keyRowKey =
+            BucketScopedRowKey.fromKey(scope.getApplication(), rowKeyString, bucket);
+
+        // determine column name based on K key to string
+        String columnName = key.toString();
+
+        final MutationBatch batch = keyspace.prepareMutationBatch();
+        batch.withRow(SCOPED_CACHE, keyRowKey).deleteColumn(columnName);
+
+        executeBatch(batch);
     }
 
 

http://git-wip-us.apache.org/repos/asf/usergrid/blob/dc7681f4/stack/corepersistence/cache/src/test/java/org/apache/usergrid/persistence/cache/CacheTestModule.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/cache/src/test/java/org/apache/usergrid/persistence/cache/CacheTestModule.java
b/stack/corepersistence/cache/src/test/java/org/apache/usergrid/persistence/cache/CacheTestModule.java
index 47f6ccf..cb4caba 100644
--- a/stack/corepersistence/cache/src/test/java/org/apache/usergrid/persistence/cache/CacheTestModule.java
+++ b/stack/corepersistence/cache/src/test/java/org/apache/usergrid/persistence/cache/CacheTestModule.java
@@ -20,15 +20,29 @@
 package org.apache.usergrid.persistence.cache;
 
 
+import com.google.inject.TypeLiteral;
 import org.apache.usergrid.persistence.cache.guice.CacheModule;
+import org.apache.usergrid.persistence.cache.impl.CacheFactoryImpl;
+import org.apache.usergrid.persistence.cache.impl.ScopedCacheSerialization;
+import org.apache.usergrid.persistence.cache.impl.ScopedCacheSerializationImpl;
 import org.apache.usergrid.persistence.core.guice.CommonModule;
 
+import java.util.Map;
+
 
 public class CacheTestModule extends org.apache.usergrid.persistence.core.guice.TestModule
{
 
     @Override
     protected void configure() {
-        install( new CommonModule());
+
+        install( new CommonModule() );
         install( new CacheModule() );
+
+        bind( new TypeLiteral<CacheFactory<String, Map<String, Object>>>()
{} )
+            .to(new TypeLiteral<CacheFactoryImpl<String, Map<String, Object>>>()
{ });
+
+        bind( new TypeLiteral<ScopedCacheSerialization<String, Map<String, Object>>>()
{} )
+            .to(new TypeLiteral<ScopedCacheSerializationImpl<String, Map<String,
Object>>>() { });
+
     }
 }

http://git-wip-us.apache.org/repos/asf/usergrid/blob/dc7681f4/stack/corepersistence/cache/src/test/java/org/apache/usergrid/persistence/cache/ScopedCacheTest.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/cache/src/test/java/org/apache/usergrid/persistence/cache/ScopedCacheTest.java
b/stack/corepersistence/cache/src/test/java/org/apache/usergrid/persistence/cache/ScopedCacheTest.java
index c37bd8b..c497141 100644
--- a/stack/corepersistence/cache/src/test/java/org/apache/usergrid/persistence/cache/ScopedCacheTest.java
+++ b/stack/corepersistence/cache/src/test/java/org/apache/usergrid/persistence/cache/ScopedCacheTest.java
@@ -20,12 +20,12 @@
 package org.apache.usergrid.persistence.cache;
 
 
+import com.fasterxml.jackson.core.type.TypeReference;
 import com.google.inject.Inject;
 import org.apache.usergrid.persistence.core.guice.MigrationManagerRule;
 import org.apache.usergrid.persistence.core.test.ITRunner;
 import org.apache.usergrid.persistence.core.test.UseModules;
 import org.apache.usergrid.persistence.model.entity.SimpleId;
-import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -35,7 +35,6 @@ import java.util.Map;
 
 import static junit.framework.TestCase.assertNotNull;
 import static junit.framework.TestCase.assertNull;
-import static junit.framework.TestCase.fail;
 import static org.junit.Assert.assertEquals;
 
 
@@ -48,6 +47,8 @@ public class ScopedCacheTest {
 
     @Inject protected CacheFactory<String, Map<String, Object>> cf;
 
+    TypeReference typeRef = new TypeReference<Map<String, Object>>() {};
+
 
     @Test
     public void testBasicOperation() {
@@ -61,7 +62,7 @@ public class ScopedCacheTest {
         }};
         cache.put("item", item, 60);
 
-        Map<String, Object> retrievedItem = cache.get("item");
+        Map<String, Object> retrievedItem = cache.get("item", typeRef);
         assertNotNull( "should get back item", retrievedItem );
         assertEquals("value1", retrievedItem.get("field1"));
     }
@@ -90,11 +91,11 @@ public class ScopedCacheTest {
         }};
         cache2.put("item", item, 60);
 
-        Map<String, Object> fetched1 = cache1.get("item");
+        Map<String, Object> fetched1 = cache1.get("item", typeRef);
         assertNotNull( "should get back item", fetched1 );
         assertEquals("a_value", fetched1.get("field"));
 
-        Map<String, Object> fetched2 = cache2.get("item");
+        Map<String, Object> fetched2 = cache2.get("item", typeRef);
         assertNotNull( "should get back item", fetched2 );
         assertEquals("another_value", fetched2.get("field"));
     }
@@ -114,13 +115,13 @@ public class ScopedCacheTest {
         }};
         cache.put("item", item, 60);
 
-        Map<String, Object> retrievedItem = cache.get("item");
+        Map<String, Object> retrievedItem = cache.get("item", typeRef);
         assertNotNull( "should get back item", retrievedItem );
         assertEquals("value1", retrievedItem.get("field1"));
 
         cache.invalidate();
 
-        assertNull(cache.get("item"));
+        assertNull(cache.get("item", typeRef));
     }
 
 
@@ -138,12 +139,35 @@ public class ScopedCacheTest {
         }};
         cache.put("item", item, 1);
 
-        Map<String, Object> retrievedItem = cache.get("item");
-        assertNotNull( "should get back item", retrievedItem );
+        Map<String, Object> retrievedItem = cache.get("item", typeRef);
+        assertNotNull("should get back item", retrievedItem);
         assertEquals("value1", retrievedItem.get("field1"));
 
         try { Thread.sleep(1000); } catch (InterruptedException ignored) {}
 
-        assertNull( cache.get("item"));
+        assertNull( cache.get("item", typeRef));
+    }
+
+    @Test
+    public void testRemove() {
+
+        CacheScope scope = new CacheScope( new SimpleId( "application" ) );
+        ScopedCache<String, Map<String, Object>> cache = cf.getScopedCache(scope);
+        assertNotNull("should get a cache", cache);
+
+        // cache item for 1 second
+
+        Map<String, Object> item = new HashMap<String, Object>() {{
+            put("field1", "value1");
+        }};
+        cache.put("item", item, 1);
+
+        Map<String, Object> retrievedItem = cache.get("item", typeRef);
+        assertNotNull( "should get back item", retrievedItem );
+        assertEquals("value1", retrievedItem.get("field1"));
+
+        cache.remove("item");
+
+        assertNull( cache.get("item", typeRef));
     }
 }

http://git-wip-us.apache.org/repos/asf/usergrid/blob/dc7681f4/stack/rest/src/main/java/org/apache/usergrid/rest/security/shiro/ShiroCache.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/main/java/org/apache/usergrid/rest/security/shiro/ShiroCache.java
b/stack/rest/src/main/java/org/apache/usergrid/rest/security/shiro/ShiroCache.java
index bdc0e2f..03152b0 100644
--- a/stack/rest/src/main/java/org/apache/usergrid/rest/security/shiro/ShiroCache.java
+++ b/stack/rest/src/main/java/org/apache/usergrid/rest/security/shiro/ShiroCache.java
@@ -16,12 +16,21 @@
  */
 package org.apache.usergrid.rest.security.shiro;
 
+import com.fasterxml.jackson.core.type.TypeReference;
+import org.apache.shiro.authz.SimpleAuthorizationInfo;
 import org.apache.shiro.cache.Cache;
 import org.apache.shiro.cache.CacheException;
+import org.apache.shiro.subject.SimplePrincipalCollection;
+import org.apache.usergrid.persistence.cache.CacheFactory;
+import org.apache.usergrid.persistence.cache.CacheScope;
+import org.apache.usergrid.persistence.cache.ScopedCache;
+import org.apache.usergrid.persistence.model.entity.SimpleId;
+import org.apache.usergrid.security.shiro.principals.AdminUserPrincipal;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Set;
 
 
@@ -31,48 +40,91 @@ import java.util.Set;
 public class ShiroCache<K, V> implements Cache<K,V> {
 
     private static final Logger logger = LoggerFactory.getLogger( ShiroCacheManager.class
);
-    private Cache<K,V> wrappedCache;
 
-    public ShiroCache(Cache<K,V> ehc) {
-        this.wrappedCache = ehc;
+    CacheFactory<String, V> cacheFactory;
+
+    TypeReference typeRef = new TypeReference<SimpleAuthorizationInfo>() {};
+
+
+    public ShiroCache( CacheFactory<String, V> cacheFactory ) {
+        this.cacheFactory = cacheFactory;
     }
 
     @Override
     public V get(K key) throws CacheException {
-        V value = wrappedCache.get(key);
-        //logger.debug("SnoopCache: Returning key = {} value = {}", key, value);
-        return value;
+        ScopedCache<String, V> scopedCache = getCacheScope(key);
+        if ( scopedCache != null ) {
+            return scopedCache.get( getKeyString(key), typeRef);
+        }
+        return null;
     }
 
     @Override
     public V put(K key, V value) throws CacheException {
-        logger.debug( "SnoopCache: Key class={} value value={}",
-            key.getClass().getSimpleName(), value.getClass().getSimpleName() );
-        return wrappedCache.put(key, value);
+        ScopedCache<String, V> scopedCache = getCacheScope(key);
+        if ( scopedCache != null ) {
+            return scopedCache.put( getKeyString(key) , value, 5000);
+        }
+        return null;
     }
 
     @Override
     public V remove(K key) throws CacheException {
-        return remove(key);
+        ScopedCache<String, V> scopedCache = getCacheScope(key);
+        if ( scopedCache != null ) {
+            scopedCache.remove( getKeyString(key) );
+        }
+        return null;
     }
 
     @Override
     public void clear() throws CacheException {
-        wrappedCache.clear();
     }
 
     @Override
     public int size() {
-        return wrappedCache.size();
+        return 0;
     }
 
     @Override
     public Set<K> keys() {
-        return wrappedCache.keys();
+        return Collections.EMPTY_SET;
     }
 
     @Override
     public Collection<V> values() {
-        return wrappedCache.values();
+        return Collections.EMPTY_LIST;
+    }
+
+
+    /** get cache for application scope */
+    private ScopedCache<String, V> getCacheScope( K key ) {
+
+        if ( key instanceof SimplePrincipalCollection) {
+            SimplePrincipalCollection spc = (SimplePrincipalCollection)key;
+
+            if ( spc.getPrimaryPrincipal() instanceof AdminUserPrincipal ) {
+                AdminUserPrincipal p = (AdminUserPrincipal) spc.getPrimaryPrincipal();
+                CacheScope scope = new CacheScope(new SimpleId(p.getApplicationId(), "application"));
+                ScopedCache<String, V> scopedCache = cacheFactory.getScopedCache(scope);
+                return scopedCache;
+            }
+        }
+        return null;
+    }
+
+
+    /** key is the application UUID in string form */
+    private String getKeyString( K key ) {
+
+        if ( key instanceof SimplePrincipalCollection) {
+            SimplePrincipalCollection spc = (SimplePrincipalCollection)key;
+
+            if ( spc.getPrimaryPrincipal() instanceof AdminUserPrincipal ) {
+                AdminUserPrincipal p = (AdminUserPrincipal) spc.getPrimaryPrincipal();
+                return p.getApplicationId().toString();
+            }
+        }
+        return null;
     }
 }

http://git-wip-us.apache.org/repos/asf/usergrid/blob/dc7681f4/stack/rest/src/main/java/org/apache/usergrid/rest/security/shiro/ShiroCacheManager.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/main/java/org/apache/usergrid/rest/security/shiro/ShiroCacheManager.java
b/stack/rest/src/main/java/org/apache/usergrid/rest/security/shiro/ShiroCacheManager.java
index 5aaef9f..8ad9bd5 100644
--- a/stack/rest/src/main/java/org/apache/usergrid/rest/security/shiro/ShiroCacheManager.java
+++ b/stack/rest/src/main/java/org/apache/usergrid/rest/security/shiro/ShiroCacheManager.java
@@ -16,11 +16,21 @@
  */
 package org.apache.usergrid.rest.security.shiro;
 
+import com.google.inject.Injector;
+import com.google.inject.Key;
+import com.google.inject.TypeLiteral;
+import org.apache.shiro.authz.SimpleAuthorizationInfo;
 import org.apache.shiro.cache.Cache;
 import org.apache.shiro.cache.CacheException;
 import org.apache.shiro.cache.CacheManager;
+import org.apache.shiro.subject.SimplePrincipalCollection;
+import org.apache.usergrid.corepersistence.GuiceFactory;
+import org.apache.usergrid.persistence.cache.CacheFactory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import java.util.Map;
 
 
 /**
@@ -30,11 +40,16 @@ public class ShiroCacheManager implements CacheManager {
 
     private static final Logger logger = LoggerFactory.getLogger( ShiroCacheManager.class
);
 
+    @Autowired
+    protected Injector injector;
+
     public ShiroCacheManager() {
     }
 
     @Override
     public <K, V> Cache<K, V> getCache(String name) throws CacheException {
-        return new ShiroCache( null ); // TODO: plugin our cache here
+        return new ShiroCache( injector.getInstance(
+            Key.get(new TypeLiteral<CacheFactory<String, SimpleAuthorizationInfo>>()
{}))
+        );
     }
 }

http://git-wip-us.apache.org/repos/asf/usergrid/blob/dc7681f4/stack/rest/src/test/java/org/apache/usergrid/rest/applications/collection/users/PermissionsResourceIT.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/collection/users/PermissionsResourceIT.java
b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/collection/users/PermissionsResourceIT.java
index 49d7c00..f5cb60c 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/collection/users/PermissionsResourceIT.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/collection/users/PermissionsResourceIT.java
@@ -36,10 +36,8 @@ import static org.junit.Assert.*;
 
 
 /**
- * Tests permissions of adding and removing users from roles as well as groups
- *
+ * Tests permissions of adding and removing users from roles as well as groups.
  */
-
 public class PermissionsResourceIT extends AbstractRestIT {
 
     private static final String ROLE = "permtestrole";
@@ -53,7 +51,7 @@ public class PermissionsResourceIT extends AbstractRestIT {
     }
 
 
-    /**
+    /*hi
      * Creates a user in the default org/app combo for use by all of the tests
      */
     @Before

http://git-wip-us.apache.org/repos/asf/usergrid/blob/dc7681f4/stack/rest/src/test/resources/log4j.properties
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/resources/log4j.properties b/stack/rest/src/test/resources/log4j.properties
index 0288842..41d2a94 100644
--- a/stack/rest/src/test/resources/log4j.properties
+++ b/stack/rest/src/test/resources/log4j.properties
@@ -29,6 +29,7 @@ log4j.appender.stdout.layout.ConversionPattern=%d %p (%t) %c{1} - %m%n
 log4j.logger.org.glassfish=DEBUG
 log4j.logger.org.apache.shiro=DEBUG
 log4j.logger.org.apache.usergrid=INFO
+log4j.logger.org.apache.usergrid.persistence.cache=DEBUG
 
 #log4j.logger.org.apache.usergrid.cassandra=DEBUG
 #log4j.logger.org.apache.usergrid.persistence.cassandra=DEBUG

http://git-wip-us.apache.org/repos/asf/usergrid/blob/dc7681f4/stack/services/src/main/java/org/apache/usergrid/services/guice/ServiceModuleImpl.java
----------------------------------------------------------------------
diff --git a/stack/services/src/main/java/org/apache/usergrid/services/guice/ServiceModuleImpl.java
b/stack/services/src/main/java/org/apache/usergrid/services/guice/ServiceModuleImpl.java
index 1bcc881..ef680f0 100644
--- a/stack/services/src/main/java/org/apache/usergrid/services/guice/ServiceModuleImpl.java
+++ b/stack/services/src/main/java/org/apache/usergrid/services/guice/ServiceModuleImpl.java
@@ -19,36 +19,35 @@
 package org.apache.usergrid.services.guice;
 
 
-import org.apache.usergrid.corepersistence.CoreModule;
+import com.google.inject.AbstractModule;
+import com.google.inject.TypeLiteral;
+import com.google.inject.multibindings.Multibinder;
+import org.apache.shiro.authz.SimpleAuthorizationInfo;
+import org.apache.shiro.subject.SimplePrincipalCollection;
 import org.apache.usergrid.corepersistence.ServiceModule;
 import org.apache.usergrid.management.AppInfoMigrationPlugin;
-import org.apache.usergrid.persistence.EntityManagerFactory;
+import org.apache.usergrid.persistence.cache.CacheFactory;
+import org.apache.usergrid.persistence.cache.impl.CacheFactoryImpl;
+import org.apache.usergrid.persistence.cache.impl.ScopedCacheSerialization;
+import org.apache.usergrid.persistence.cache.impl.ScopedCacheSerializationImpl;
 import org.apache.usergrid.persistence.core.migration.data.MigrationPlugin;
-import org.apache.usergrid.services.ServiceManager;
-import org.apache.usergrid.services.ServiceManagerFactory;
-import org.apache.usergrid.services.queues.ImportQueueListener;
 
-import com.google.inject.AbstractModule;
-import com.google.inject.assistedinject.FactoryModuleBuilder;
-import com.google.inject.multibindings.Multibinder;
+import java.util.Map;
 
 
-/**
- * Module that handles all of the guice injects for services.
- */
-
+// <bean id="notificationsQueueListener" class="org.apache.usergrid.services.notifications.QueueListener"
+// scope="singleton">
+// <constructor-arg name="emf" ref="entityManagerFactory" />
+// <constructor-arg name="metricsService" ref="metricsFactory" />
+// <constructor-arg name="props" ref="properties" />
+// <constructor-arg name="smf" ref="serviceManagerFactory" />
+// </bean>
 
 /**
- *   <bean id="notificationsQueueListener" class="org.apache.usergrid.services.notifications.QueueListener"
- scope="singleton">
- <constructor-arg name="emf" ref="entityManagerFactory" />
- <constructor-arg name="metricsService" ref="metricsFactory" />
- <constructor-arg name="props" ref="properties" />
- <constructor-arg name="smf" ref="serviceManagerFactory" />
- </bean>
+ * Module that handles all of the guice injects for services.
  */
-
 public class ServiceModuleImpl extends AbstractModule implements ServiceModule {
+
     @Override
     protected void configure() {
 
@@ -56,5 +55,11 @@ public class ServiceModuleImpl extends AbstractModule implements ServiceModule
{
         final Multibinder<MigrationPlugin> plugins = Multibinder.newSetBinder( binder(),
MigrationPlugin.class );
         plugins.addBinding().to(AppInfoMigrationPlugin.class);
 
+        bind(    new TypeLiteral<CacheFactory<String, SimpleAuthorizationInfo>>()
{} )
+            .to( new TypeLiteral<CacheFactoryImpl<String, SimpleAuthorizationInfo>>()
{});
+
+        bind(    new TypeLiteral<ScopedCacheSerialization<String, SimpleAuthorizationInfo>>()
{})
+            .to( new TypeLiteral<ScopedCacheSerializationImpl<String, SimpleAuthorizationInfo>>()
{});
+
     }
 }


Mime
View raw message