Author: bdekruijff at gmail.com
Date: Mon Jan 17 09:58:36 2011
New Revision: 662

Log:
AMDATU-263 refactorred consumer storage to make use of fsstorage base

Added:
   
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/main/java/org/amdatu/authentication/oauth/consumerregistry/fs/internal/FSConsumerEntity.java
      - copied, changed from r628, 
/trunk/amdatu-authentication/oauth-consumerregistry-fs/src/main/java/org/amdatu/authentication/oauth/consumerregistry/fs/internal/FSOAuthServiceConsumer.java
   
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/main/java/org/amdatu/authentication/oauth/consumerregistry/fs/internal/FSConsumerStorage.java
   trunk/amdatu-authentication/oauth-consumerregistry-fs/src/test/
   trunk/amdatu-authentication/oauth-consumerregistry-fs/src/test/java/
   trunk/amdatu-authentication/oauth-consumerregistry-fs/src/test/java/org/
   
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/test/java/org/amdatu/
   
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/test/java/org/amdatu/authentication/
   
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/test/java/org/amdatu/authentication/oauth/
   
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/test/java/org/amdatu/authentication/oauth/consumerregistry/
   
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/test/java/org/amdatu/authentication/oauth/consumerregistry/fs/
   
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/test/java/org/amdatu/authentication/oauth/consumerregistry/fs/service/
   
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/test/java/org/amdatu/authentication/oauth/consumerregistry/fs/service/FSConsumerRegistryImplTest.java
Removed:
   
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/main/java/org/amdatu/authentication/oauth/consumerregistry/fs/internal/ConsumerKeyList.java
   
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/main/java/org/amdatu/authentication/oauth/consumerregistry/fs/internal/FSOAuthServiceConsumer.java
Modified:
   trunk/amdatu-authentication/oauth-consumerregistry-fs/pom.xml
   
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/main/java/org/amdatu/authentication/oauth/consumerregistry/fs/internal/FSConsumerStore.java
   
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/main/java/org/amdatu/authentication/oauth/consumerregistry/fs/service/FSConsumerRegistryImpl.java

Modified: trunk/amdatu-authentication/oauth-consumerregistry-fs/pom.xml
==============================================================================
--- trunk/amdatu-authentication/oauth-consumerregistry-fs/pom.xml       
(original)
+++ trunk/amdatu-authentication/oauth-consumerregistry-fs/pom.xml       Mon Jan 
17 09:58:36 2011
@@ -23,10 +23,10 @@
     </dependency> 
     <dependency>
       <groupId>org.amdatu.libraries</groupId>
-      <artifactId>utilities</artifactId>
+      <artifactId>fsstorage</artifactId>
       <version>${platform.version}</version>
       <scope>compile</scope>
-    </dependency>
+    </dependency>    
   </dependencies>
   
   <build>

Copied: 
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/main/java/org/amdatu/authentication/oauth/consumerregistry/fs/internal/FSConsumerEntity.java
 (from r628, 
/trunk/amdatu-authentication/oauth-consumerregistry-fs/src/main/java/org/amdatu/authentication/oauth/consumerregistry/fs/internal/FSOAuthServiceConsumer.java)
==============================================================================
--- 
/trunk/amdatu-authentication/oauth-consumerregistry-fs/src/main/java/org/amdatu/authentication/oauth/consumerregistry/fs/internal/FSOAuthServiceConsumer.java
       (original)
+++ 
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/main/java/org/amdatu/authentication/oauth/consumerregistry/fs/internal/FSConsumerEntity.java
      Mon Jan 17 09:58:36 2011
@@ -21,14 +21,14 @@
 import java.util.Map;
 
 import org.amdatu.authentication.oauth.api.OAuthServiceConsumer;
+import org.amdatu.libraries.fsstorage.FSStoreEntity;
 
 /**
  * Internal class to store OAuth service consumer properties, including a list 
of userids that granted access
  * to this service consumer to invoke the provider on its behalf.
- * 
- * @author ivol
  */
-public class FSOAuthServiceConsumer implements OAuthServiceConsumer {
+public class FSConsumerEntity implements OAuthServiceConsumer, FSStoreEntity {
+
     private String m_consumerKey;
     private String m_consumerSecret;
     private String m_name;
@@ -36,10 +36,10 @@
     private List<String> m_allowedUserIds = new ArrayList<String>();
     private Map<String, String> m_properties;
 
-    public FSOAuthServiceConsumer() {
+    public FSConsumerEntity() {
     }
 
-    public FSOAuthServiceConsumer(OAuthServiceConsumer consumer) {
+    public FSConsumerEntity(OAuthServiceConsumer consumer) {
         setName(consumer.getName());
         setConsumerKey(consumer.getConsumerKey());
         setConsumerSecret(consumer.getConsumerSecret());
@@ -47,6 +47,10 @@
         setProperties(consumer.getProperties());
     }
 
+    public String getId() {
+        return getConsumerKey();
+    }
+
     public String getConsumerKey() {
         return m_consumerKey;
     }

Added: 
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/main/java/org/amdatu/authentication/oauth/consumerregistry/fs/internal/FSConsumerStorage.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/main/java/org/amdatu/authentication/oauth/consumerregistry/fs/internal/FSConsumerStorage.java
     Mon Jan 17 09:58:36 2011
@@ -0,0 +1,38 @@
+/*
+    Copyright (C) 2010 Amdatu.org
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.authentication.oauth.consumerregistry.fs.internal;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.amdatu.libraries.fsstorage.FSStorageBase;
+
+public class FSConsumerStorage extends FSStorageBase<FSConsumerStore, 
FSConsumerEntity> {
+
+    private static final String ENTITYLIST_FILENAME = "consumerKeyList.ser";
+    private static final String STORAGEFILE_PREFIX = "consumerkey_";
+    private static final String STORAGEFILE_POSTFIX = ".ser";
+
+    public FSConsumerStorage(File dataDirectory) throws IOException {
+        super(dataDirectory, ENTITYLIST_FILENAME, STORAGEFILE_PREFIX, 
STORAGEFILE_POSTFIX);
+    }
+
+    @Override
+    protected FSConsumerStore newFSStore(File file) throws IOException {
+        return new FSConsumerStore(file);
+    }
+}
\ No newline at end of file

Modified: 
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/main/java/org/amdatu/authentication/oauth/consumerregistry/fs/internal/FSConsumerStore.java
==============================================================================
--- 
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/main/java/org/amdatu/authentication/oauth/consumerregistry/fs/internal/FSConsumerStore.java
       (original)
+++ 
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/main/java/org/amdatu/authentication/oauth/consumerregistry/fs/internal/FSConsumerStore.java
       Mon Jan 17 09:58:36 2011
@@ -17,85 +17,32 @@
 package org.amdatu.authentication.oauth.consumerregistry.fs.internal;
 
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.UnsupportedEncodingException;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import org.amdatu.authentication.oauth.api.ConsumerRegistryStorageException;
-import org.amdatu.authentication.oauth.api.OAuthServiceConsumer;
-import org.amdatu.libraries.utilities.FSUtil;
-
-public class FSConsumerStore {
-    private final File m_file;
-    private final Map<String, FSOAuthServiceConsumer> m_consumers;
-
-    public FSConsumerStore(File file) throws ConsumerRegistryStorageException {
-        m_file = file;
-        m_consumers = new HashMap<String, FSOAuthServiceConsumer>();
-        try {
-            readConsumers();
-        }
-        catch (IOException e) {
-            throw new ConsumerRegistryStorageException(e);
-        }
-    }
-
-    public OAuthServiceConsumer getConsumer(final String consumerKey) {
-        return m_consumers.get(consumerKey);
-    }
-
-    public OAuthServiceConsumer addConsumer(final OAuthServiceConsumer 
consumer) {
-        return m_consumers.put(consumer.getConsumerKey(), new 
FSOAuthServiceConsumer(consumer));
-    }
+import org.amdatu.libraries.fsstorage.FSStorageUtil;
+import org.amdatu.libraries.fsstorage.FSStoreBase;
 
-    public OAuthServiceConsumer removeConsumer(final String consumerId) {
-        return m_consumers.remove(consumerId);
-    }
-
-    public void save() throws ConsumerRegistryStorageException {
-        try {
-            writeConsumers();
-        }
-        catch (IOException e) {
-            throw new ConsumerRegistryStorageException(e);
-        }
-    }
+public class FSConsumerStore extends FSStoreBase<FSConsumerEntity> {
 
-    private void readConsumers() throws IOException {
-        if (!m_file.exists()) {
-            return;
-        }
-        m_consumers.clear();
-        FileInputStream fis = null;
-        ObjectInputStream ois = null;
-        try {
-            fis = new FileInputStream(m_file);
-            ois = new ObjectInputStream(fis);
-            final int numberOfConsumers = ois.readInt();
-            for (int i = 0; i < numberOfConsumers; i++) {
-                final FSOAuthServiceConsumer consumer = readConsumer(ois);
-                m_consumers.put(consumer.getConsumerKey(), consumer);
-            }
-        }
-        finally {
-            FSUtil.closeInputStreamsSafely(ois, fis);
-        }
+    public FSConsumerStore(File file) throws IOException {
+        super(file);
     }
 
-    private FSOAuthServiceConsumer readConsumer(ObjectInputStream ois) throws 
IOException, UnsupportedEncodingException {
-        final String name = FSUtil.readString(ois);
-        final String key = FSUtil.readString(ois);
-        final String secret = FSUtil.readString(ois);
-        final String callbackUrl = FSUtil.readString(ois);
-        final List<String> allowedUserids = FSUtil.readList(ois);
-        final Map<String, String> properties = FSUtil.readProperties(ois);
-        FSOAuthServiceConsumer consumer = new FSOAuthServiceConsumer();
+    @Override
+    protected FSConsumerEntity readEntity(ObjectInputStream ois) throws 
IOException,
+        UnsupportedEncodingException {
+        final String name = FSStorageUtil.readString(ois);
+        final String key = FSStorageUtil.readString(ois);
+        final String secret = FSStorageUtil.readString(ois);
+        final String callbackUrl = FSStorageUtil.readString(ois);
+        final List<String> allowedUserids = FSStorageUtil.readList(ois);
+        final Map<String, String> properties = 
FSStorageUtil.readProperties(ois);
+        FSConsumerEntity consumer = new FSConsumerEntity();
         consumer.setName(name);
         consumer.setConsumerKey(key);
         consumer.setConsumerSecret(secret);
@@ -105,34 +52,14 @@
         return consumer;
     }
 
-    private void writeConsumers() throws IOException {
-        if ((m_consumers == null || m_consumers.size() == 0) && 
m_file.exists()) {
-            m_file.delete();
-            return;
-        }
-        FileOutputStream fos = null;
-        ObjectOutputStream oos = null;
-        try {
-            fos = new FileOutputStream(m_file);
-            oos = new ObjectOutputStream(fos);
-            oos.writeInt(m_consumers.size());
-            for (FSOAuthServiceConsumer consumer : m_consumers.values()) {
-                writeConsumer(oos, consumer);
-            }
-            oos.flush();
-        }
-        finally {
-            FSUtil.closeOutputStreamsSafely(oos, fos);
-        }
-    }
-
-    private void writeConsumer(ObjectOutputStream oos, FSOAuthServiceConsumer 
consumer)
+    @Override
+    protected void writeEntity(ObjectOutputStream oos, FSConsumerEntity 
consumer)
         throws UnsupportedEncodingException, IOException {
-        FSUtil.writeString(oos, consumer.getName());
-        FSUtil.writeString(oos, consumer.getConsumerKey());
-        FSUtil.writeString(oos, consumer.getConsumerSecret());
-        FSUtil.writeString(oos, consumer.getCallbackUrl());
-        FSUtil.writeList(oos, consumer.getAllowedUserIds());
-        FSUtil.writeProperties(oos, consumer.getProperties());
+        FSStorageUtil.writeString(oos, consumer.getName());
+        FSStorageUtil.writeString(oos, consumer.getConsumerKey());
+        FSStorageUtil.writeString(oos, consumer.getConsumerSecret());
+        FSStorageUtil.writeString(oos, consumer.getCallbackUrl());
+        FSStorageUtil.writeList(oos, consumer.getAllowedUserIds());
+        FSStorageUtil.writeProperties(oos, consumer.getProperties());
     }
 }

Modified: 
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/main/java/org/amdatu/authentication/oauth/consumerregistry/fs/service/FSConsumerRegistryImpl.java
==============================================================================
--- 
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/main/java/org/amdatu/authentication/oauth/consumerregistry/fs/service/FSConsumerRegistryImpl.java
 (original)
+++ 
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/main/java/org/amdatu/authentication/oauth/consumerregistry/fs/service/FSConsumerRegistryImpl.java
 Mon Jan 17 09:58:36 2011
@@ -17,6 +17,7 @@
 package org.amdatu.authentication.oauth.consumerregistry.fs.service;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Dictionary;
 import java.util.List;
@@ -26,66 +27,43 @@
 import org.amdatu.authentication.oauth.api.ConsumerRegistryStorageException;
 import org.amdatu.authentication.oauth.api.OAuthServiceConsumer;
 import org.amdatu.authentication.oauth.api.OAuthServiceConsumerRegistry;
-import 
org.amdatu.authentication.oauth.consumerregistry.fs.internal.ConsumerKeyList;
-import 
org.amdatu.authentication.oauth.consumerregistry.fs.internal.FSConsumerStore;
-import 
org.amdatu.authentication.oauth.consumerregistry.fs.internal.FSOAuthServiceConsumer;
+import 
org.amdatu.authentication.oauth.consumerregistry.fs.internal.FSConsumerStorage;
+import 
org.amdatu.authentication.oauth.consumerregistry.fs.internal.FSConsumerEntity;
 import org.osgi.service.cm.ConfigurationException;
-import org.osgi.service.cm.ManagedService;
 import org.osgi.service.log.LogService;
 
-public class FSConsumerRegistryImpl implements OAuthServiceConsumerRegistry, 
ManagedService {
+public class FSConsumerRegistryImpl implements OAuthServiceConsumerRegistry {
+
     // Configuration ids
     public static final String CONFIGURATION_PID = 
"org.amdatu.authentication.oauth.consumerregistry.fs";
     public final static String DATA_DIRECTORY = "datadir";
 
-    // File naming constants
-    private static final String ENTITYLIST_FILENAME = "consumerKeyList.ser";
-    private static final String STORAGEFILE_PREFIX = "consumerkey_";
-    private static final String STORAGEFILE_POSTFIX = ".ser";
-
     // Injected service dependencies
     private volatile LogService m_logService;
-
-    // Storage directory
-    private String m_dataDirectoryName;
-    private File m_dataDirectory;
-
-    // Collaborator
-    private ConsumerKeyList m_consumerKeyList = null;
+    private FSConsumerStorage m_storage;
 
     /**
      * Invoked since this is a managed service.
+     * 
      * @param dictionary Dictionary holding the configuration properties
      * @throws ConfigurationException In case some Config Admin exception 
occurred
      */
-    @SuppressWarnings("unchecked")
-    public synchronized void updated(Dictionary dictionary) throws 
ConfigurationException {
+    public synchronized void updated(Dictionary<String, Object> dictionary) 
throws ConfigurationException {
         if (dictionary != null) {
-            m_dataDirectoryName = (String) dictionary.get(DATA_DIRECTORY);
-            if (m_dataDirectoryName == null || "".equals(m_dataDirectoryName)) 
{
+            String dataDirectoryName = (String) dictionary.get(DATA_DIRECTORY);
+            if (dataDirectoryName == null || "".equals(dataDirectoryName)) {
                 throw new ConfigurationException(DATA_DIRECTORY, "Missing 
mandatory data directory configuration");
-            }           
-        }
-    }
-    
-    public void init() {
-        File dataDirectory = new File(m_dataDirectoryName);
-        try {
-            setDataDirectory(dataDirectory);
-        }
-        catch (ConsumerRegistryStorageException e) {
-            m_logService.log(LogService.LOG_ERROR, "Could not prepare data 
directory '" + m_dataDirectoryName + "' for storage");
+            }
+            File dataDirectory = new File(dataDirectoryName);
+            try {
+                setDataDirectory(dataDirectory);
+            }
+            catch (ConsumerRegistryStorageException e) {
+                throw new ConfigurationException(DATA_DIRECTORY, 
e.getMessage());
+            }
         }
     }
 
-    public synchronized void stop() {
-        m_consumerKeyList = null;
-    }
-
-    public synchronized File getDataDirectory() {
-        return m_dataDirectory;
-    }
-
     public synchronized void setDataDirectory(File dataDirectory) throws 
ConsumerRegistryStorageException {
         if (!dataDirectory.isAbsolute()) {
             File userDirectory = new File(System.getProperty("user.dir"));
@@ -95,93 +73,127 @@
             throw new ConsumerRegistryStorageException("Unable to access data 
directory: "
                 + dataDirectory.getAbsolutePath());
         }
-        m_dataDirectory = dataDirectory;
-        m_consumerKeyList = new ConsumerKeyList(new File(m_dataDirectory, 
ENTITYLIST_FILENAME));
-        m_logService.log(LogService.LOG_DEBUG, "Datadirectory set to: " + 
m_dataDirectory.getAbsolutePath());
+        try {
+            m_storage = new FSConsumerStorage(dataDirectory);
+            if (m_logService != null)
+                m_logService.log(LogService.LOG_DEBUG, "Datadirectory set to: "
+                    + m_storage.getDataDirectory().getAbsolutePath());
+        }
+        catch (IOException e) {
+            throw new ConsumerRegistryStorageException(e);
+        }
+
     }
 
     public synchronized OAuthServiceConsumer getConsumer(String consumerKey) 
throws ConsumerRegistryStorageException {
-        FSConsumerStore consumerStorageFile = getStorageFile(consumerKey);
-        return consumerStorageFile.getConsumer(consumerKey);
+        try {
+            return m_storage.getEntity(consumerKey);
+        }
+        catch (IOException e) {
+            throw new ConsumerRegistryStorageException(e);
+        }
+    }
+
+    public synchronized void addConsumer(OAuthServiceConsumer consumer) throws 
ConsumerAlreadyExistsException,
+        ConsumerRegistryStorageException {
+        try {
+            if (m_storage.getEntity(consumer.getConsumerKey()) != null) {
+                throw new ConsumerAlreadyExistsException("Consumer with key '" 
+ consumer.getConsumerKey()
+                    + "' already exists");
+            }
+            m_storage.addEntity(new FSConsumerEntity(consumer));
+            if (m_logService != null)
+                m_logService.log(LogService.LOG_DEBUG, "Added consumer with 
key '" + consumer.getConsumerKey()
+                    + "' to the oAuth service consumer registry");
+        }
+        catch (IOException e) {
+            throw new ConsumerRegistryStorageException(e);
+        }
+    }
+
+    public synchronized void updateConsumer(OAuthServiceConsumer consumer) 
throws ConsumerNotFoundException,
+        ConsumerRegistryStorageException {
+        try {
+            if (m_storage.getEntity(consumer.getConsumerKey()) == null) {
+                throw new ConsumerNotFoundException("Consumer with key '" + 
consumer.getConsumerKey()
+                    + "' doesn't exist");
+            }
+            m_storage.addEntity(new FSConsumerEntity(consumer));
+            if (m_logService != null)
+                m_logService.log(LogService.LOG_DEBUG, "Updated consumer with 
key '" + consumer.getConsumerKey()
+                    + "' in the oAuth service consumer registry");
+        }
+        catch (IOException e) {
+            throw new ConsumerRegistryStorageException(e);
+        }
     }
 
-    public synchronized void addConsumer(OAuthServiceConsumer consumer) throws 
ConsumerAlreadyExistsException, ConsumerRegistryStorageException {
-        FSConsumerStore consumerStorageFile = 
getStorageFile(consumer.getConsumerKey());
-        if (consumerStorageFile.getConsumer(consumer.getConsumerKey()) != 
null) {
-            throw new ConsumerAlreadyExistsException("Consumer with key '" + 
consumer.getConsumerKey() + "' already exists");
-        }
-        if (consumerStorageFile.addConsumer(consumer) != null) {
-            // This shouldn't happen
-            throw new ConsumerRegistryStorageException("Internal storage error 
while adding consumer '" + consumer.getConsumerKey() + "'");
-        }
-        m_consumerKeyList.addConsumerKey(consumer.getConsumerKey());
-        consumerStorageFile.save();
-        m_logService.log(LogService.LOG_DEBUG, "Added consumer with key '" + 
consumer.getConsumerKey() + "' to the oAuth service consumer registry");
-    }
-
-    public synchronized void updateConsumer(OAuthServiceConsumer consumer) 
throws ConsumerNotFoundException, ConsumerRegistryStorageException {
-        FSConsumerStore consumerStorageFile = 
getStorageFile(consumer.getConsumerKey());
-        if (consumerStorageFile.getConsumer(consumer.getConsumerKey()) == 
null) {
-            throw new ConsumerNotFoundException("Consumer with key '" + 
consumer.getConsumerKey() + "' doesn't exist");
-        }
-        OAuthServiceConsumer storedEntity = 
consumerStorageFile.addConsumer(consumer);
-        if (storedEntity == null) {
-            // This shouldn't happen
-            throw new ConsumerRegistryStorageException("Internal storage error 
while updating consumer '" + consumer.getConsumerKey() + "'");
-        }
-        consumerStorageFile.save();
-        m_logService.log(LogService.LOG_DEBUG, "Updated consumer with key '" + 
consumer.getConsumerKey() + "' in the oAuth service consumer registry");
-    }
-
-    public synchronized void removeConsumer(OAuthServiceConsumer consumer) 
throws ConsumerNotFoundException, ConsumerRegistryStorageException {
-        FSConsumerStore consumerStorageFile = 
getStorageFile(consumer.getConsumerKey());
-        if (consumerStorageFile.getConsumer(consumer.getConsumerKey()) == 
null) {
-            throw new ConsumerNotFoundException("Consumer with key '" + 
consumer.getConsumerKey() + "' doesn't exist");
-        }
-        OAuthServiceConsumer storedEntity = 
consumerStorageFile.removeConsumer(consumer.getConsumerKey());
-        if (storedEntity == null) {
-            // This shouldn't happen
-            throw new ConsumerRegistryStorageException("Internal storage error 
while deleting consumer '" + consumer.getConsumerKey() + "'");
-        }
-        m_consumerKeyList.removeConsumerKey(consumer.getConsumerKey());
-        consumerStorageFile.save();
-        m_logService.log(LogService.LOG_DEBUG, "Removed consumer with key '" + 
consumer.getConsumerKey() + "' from the oAuth service consumer registry");
-    }
-    
-    public void grantResourceAccess(OAuthServiceConsumer consumer, String 
userId) throws ConsumerRegistryStorageException {
-        FSConsumerStore consumerStorageFile = 
getStorageFile(consumer.getConsumerKey());
-        FSOAuthServiceConsumer fsConsumer = (FSOAuthServiceConsumer) 
consumerStorageFile.getConsumer(consumer.getConsumerKey());
-        List<String> allowedUserIds = fsConsumer.getAllowedUserIds();
-        if (allowedUserIds == null) {
-            allowedUserIds = new ArrayList<String>();
-        }
-        allowedUserIds.add(userId);
-        consumerStorageFile.save();
-    }
-    
-    public void withdrawResourceAccess(OAuthServiceConsumer consumer, String 
userId) throws ConsumerRegistryStorageException {
-        FSConsumerStore consumerStorageFile = 
getStorageFile(consumer.getConsumerKey());
-        FSOAuthServiceConsumer fsConsumer = (FSOAuthServiceConsumer) 
consumerStorageFile.getConsumer(consumer.getConsumerKey());
-        List<String> allowedUserIds = fsConsumer.getAllowedUserIds();
-        if (allowedUserIds == null) {
-            allowedUserIds = new ArrayList<String>();
-        }
-        allowedUserIds.remove(userId);
-        consumerStorageFile.save();
-    }
-    
-    public boolean hasResourceAccess(OAuthServiceConsumer consumer, String 
userId) throws ConsumerRegistryStorageException {
-        FSOAuthServiceConsumer fsConsumer = (FSOAuthServiceConsumer) 
getConsumer(consumer.getConsumerKey());
-        return fsConsumer.getAllowedUserIds().contains(userId);
-    }
-
-    private FSConsumerStore getStorageFile(final String entityId) throws 
ConsumerRegistryStorageException {
-        final int hash = Math.abs(entityId.hashCode());
-        final String subdirName = "" + (hash % 50);
-        final File subdirFile = new File(m_dataDirectory, subdirName);
-        if (!subdirFile.exists()) {
-            subdirFile.mkdir();
+    public synchronized void removeConsumer(OAuthServiceConsumer consumer) 
throws ConsumerNotFoundException,
+        ConsumerRegistryStorageException {
+        try {
+            if (m_storage.getEntity(consumer.getConsumerKey()) == null) {
+                throw new ConsumerNotFoundException("Consumer with key '" + 
consumer.getConsumerKey()
+                    + "' doesn't exist");
+            }
+            m_storage.removeEntity(consumer.getConsumerKey());
+            if (m_logService != null)
+                m_logService.log(LogService.LOG_DEBUG, "Removed consumer with 
key '" + consumer.getConsumerKey()
+                    + "' from the oAuth service consumer registry");
+        }
+        catch (IOException e) {
+            throw new ConsumerRegistryStorageException(e);
+        }
+    }
+
+    public synchronized void grantResourceAccess(OAuthServiceConsumer 
consumer, String userId)
+        throws ConsumerRegistryStorageException {
+        try {
+            FSConsumerEntity fsConsumer = 
m_storage.getEntity(consumer.getConsumerKey());
+            List<String> allowedUserIds = fsConsumer.getAllowedUserIds();
+            if (allowedUserIds == null) {
+                allowedUserIds = new ArrayList<String>();
+            }
+            if (!allowedUserIds.contains(userId)) {
+                allowedUserIds.add(userId);
+                m_storage.addEntity(fsConsumer);
+            }
+        }
+        catch (IOException e) {
+            throw new ConsumerRegistryStorageException(e);
+        }
+    }
+
+    public synchronized void withdrawResourceAccess(OAuthServiceConsumer 
consumer, String userId)
+        throws ConsumerRegistryStorageException {
+        try {
+            FSConsumerEntity entity = 
m_storage.getEntity(consumer.getConsumerKey());
+            if (entity == null) {
+                // FIXME for API consistency throw exception?
+                return;
+            }
+            List<String> allowedUserIds = entity.getAllowedUserIds();
+            if (allowedUserIds != null && allowedUserIds.contains(userId)) {
+                allowedUserIds.remove(userId);
+                m_storage.addEntity(entity);
+            }
+        }
+        catch (IOException e) {
+            throw new ConsumerRegistryStorageException(e);
+        }
+    }
+
+    public boolean hasResourceAccess(OAuthServiceConsumer consumer, String 
userId)
+        throws ConsumerRegistryStorageException {
+        try {
+            FSConsumerEntity entity = 
m_storage.getEntity(consumer.getConsumerKey());
+            if (entity == null) {
+                // FIXME for API consistency throw exception?
+                return false;
+            }
+            return entity.getAllowedUserIds() != null && 
entity.getAllowedUserIds().contains(userId);
+        }
+        catch (IOException e) {
+            throw new ConsumerRegistryStorageException(e);
         }
-        return new FSConsumerStore(new File(subdirFile, STORAGEFILE_PREFIX + 
hash + STORAGEFILE_POSTFIX));
     }
 }

Added: 
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/test/java/org/amdatu/authentication/oauth/consumerregistry/fs/service/FSConsumerRegistryImplTest.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-authentication/oauth-consumerregistry-fs/src/test/java/org/amdatu/authentication/oauth/consumerregistry/fs/service/FSConsumerRegistryImplTest.java
     Mon Jan 17 09:58:36 2011
@@ -0,0 +1,174 @@
+/*
+    Copyright (C) 2010 Amdatu.org
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.authentication.oauth.consumerregistry.fs.service;
+
+import java.io.File;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+
+import org.amdatu.authentication.oauth.api.ConsumerAlreadyExistsException;
+import org.amdatu.authentication.oauth.api.ConsumerNotFoundException;
+import org.amdatu.authentication.oauth.api.ConsumerRegistryStorageException;
+import org.amdatu.authentication.oauth.api.OAuthServiceConsumer;
+import 
org.amdatu.authentication.oauth.consumerregistry.fs.internal.FSConsumerEntity;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TestName;
+import org.osgi.service.cm.ConfigurationException;
+
+public class FSConsumerRegistryImplTest {
+    private final static String JAVA_IO_TMPDIR = 
System.getProperty("java.io.tmpdir");
+
+    private static File m_absoluteTestRootDirectory;
+    private static String m_relativeTestRootDirectory;
+
+    @Rule
+    public TestName m_testName = new TestName();
+
+    private File m_testDirectory;
+    private FSConsumerRegistryImpl m_oauthConsumerRegistry;
+
+    @BeforeClass
+    public static void setUpOnce() throws ConsumerRegistryStorageException {
+        Random rand = new Random();
+        int randomInt = 1 + Math.abs(rand.nextInt());
+        m_relativeTestRootDirectory = 
FSConsumerRegistryImplTest.class.getSimpleName() + "_" + randomInt;
+        m_absoluteTestRootDirectory =
+            new File(JAVA_IO_TMPDIR + File.separator + 
m_relativeTestRootDirectory);
+        m_absoluteTestRootDirectory.mkdirs();
+    }
+
+    @Before
+    public void setUp() throws ConsumerRegistryStorageException, 
ConfigurationException {
+        m_testDirectory = new File(m_absoluteTestRootDirectory, 
m_testName.getMethodName());
+        m_testDirectory.mkdir();
+        m_oauthConsumerRegistry = new FSConsumerRegistryImpl();
+        m_oauthConsumerRegistry.setDataDirectory(m_testDirectory);
+    }
+
+    
+    @Test
+    public void testRobustness(){
+        
+    }
+    @Test
+    public void testSimpleCrud() throws ConsumerAlreadyExistsException, 
ConsumerRegistryStorageException,
+        ConsumerNotFoundException {
+
+        OAuthServiceConsumer c = new FSConsumerEntity();
+        ((FSConsumerEntity) c).setName("John Doe");
+        ((FSConsumerEntity) c).setConsumerKey("key1");
+        ((FSConsumerEntity) c).setConsumerSecret("42");
+        ((FSConsumerEntity) c).setCallbackUrl("http://call.me.back/";);
+        Map<String, String> properties = new HashMap<String, String>();
+        properties.put("key1", "value1");
+        properties.put("key2", "value2");
+        ((FSConsumerEntity) c).setProperties(properties);
+        m_oauthConsumerRegistry.addConsumer(c);
+
+        c = m_oauthConsumerRegistry.getConsumer("key1");
+        Assert.assertNotNull(c);
+        Assert.assertEquals("John Doe", c.getName());
+        Assert.assertEquals("key1", c.getConsumerKey());
+        Assert.assertEquals("42", c.getConsumerSecret());
+        Assert.assertEquals("http://call.me.back/";, c.getCallbackUrl());
+        Assert.assertEquals("value1", c.getProperties().get("key1"));
+        Assert.assertEquals("value2", c.getProperties().get("key2"));
+
+        c = new FSConsumerEntity();
+        ((FSConsumerEntity) c).setName("John Doe2");
+        ((FSConsumerEntity) c).setConsumerKey("key1");
+        ((FSConsumerEntity) c).setConsumerSecret("44");
+        ((FSConsumerEntity) c).setCallbackUrl("http://call.me.back/?1";);
+        Map<String, String> properties2 = new HashMap<String, String>();
+        properties2.put("key1", "value1");
+        ((FSConsumerEntity) c).setProperties(properties2);
+        m_oauthConsumerRegistry.updateConsumer(c);
+
+        c = m_oauthConsumerRegistry.getConsumer("key1");
+        Assert.assertNotNull(c);
+        Assert.assertEquals("John Doe2", c.getName());
+        Assert.assertEquals("key1", c.getConsumerKey());
+        Assert.assertEquals("44", c.getConsumerSecret());
+        Assert.assertEquals("http://call.me.back/?1";, c.getCallbackUrl());
+        Assert.assertEquals("value1", c.getProperties().get("key1"));
+        Assert.assertNull(c.getProperties().get("key2"));
+
+        m_oauthConsumerRegistry.removeConsumer(c);
+        c = m_oauthConsumerRegistry.getConsumer("key1");
+        Assert.assertNull(c);
+    }
+
+    @Test(expected = ConsumerAlreadyExistsException.class)
+    public void testDuplicateAdd() throws ConsumerAlreadyExistsException, 
ConsumerRegistryStorageException {
+        OAuthServiceConsumer c = new FSConsumerEntity();
+        ((FSConsumerEntity) c).setConsumerKey("key1");
+        m_oauthConsumerRegistry.addConsumer(c);
+        m_oauthConsumerRegistry.addConsumer(c);
+    }
+
+    @Test(expected = ConsumerNotFoundException.class)
+    public void testFaultyUpdate() throws ConsumerAlreadyExistsException, 
ConsumerRegistryStorageException,
+        ConsumerNotFoundException {
+        OAuthServiceConsumer c1 = new FSConsumerEntity();
+        ((FSConsumerEntity) c1).setConsumerKey("key1");
+        m_oauthConsumerRegistry.addConsumer(c1);
+        OAuthServiceConsumer c2 = new FSConsumerEntity();
+        ((FSConsumerEntity) c2).setConsumerKey("key2");
+        m_oauthConsumerRegistry.updateConsumer(c2);
+    }
+
+    @Test
+    public void testFaultyConsumer() throws ConsumerAlreadyExistsException, 
ConsumerRegistryStorageException,
+        ConsumerNotFoundException {
+        OAuthServiceConsumer c = new FSConsumerEntity();
+        ((FSConsumerEntity) c).setConsumerKey("key1");
+        m_oauthConsumerRegistry.addConsumer(c);
+
+        Assert.assertFalse(m_oauthConsumerRegistry.hasResourceAccess(c, 
"john"));
+        Assert.assertFalse(m_oauthConsumerRegistry.hasResourceAccess(c, 
"eric"));
+
+        m_oauthConsumerRegistry.grantResourceAccess(c, "john");
+        c = m_oauthConsumerRegistry.getConsumer("key1");
+        List<String> userIds = ((FSConsumerEntity) c).getAllowedUserIds();
+        Assert.assertArrayEquals(new String[] { "john" }, userIds.toArray(new 
String[1]));
+
+        Assert.assertTrue(m_oauthConsumerRegistry.hasResourceAccess(c, 
"john"));
+        Assert.assertFalse(m_oauthConsumerRegistry.hasResourceAccess(c, 
"eric"));
+
+        m_oauthConsumerRegistry.grantResourceAccess(c, "eric");
+        c = m_oauthConsumerRegistry.getConsumer("key1");
+        userIds = ((FSConsumerEntity) c).getAllowedUserIds();
+        Assert.assertArrayEquals(new String[] { "john", "eric" }, 
userIds.toArray(new String[2]));
+
+        Assert.assertTrue(m_oauthConsumerRegistry.hasResourceAccess(c, 
"john"));
+        Assert.assertTrue(m_oauthConsumerRegistry.hasResourceAccess(c, 
"eric"));
+
+        m_oauthConsumerRegistry.withdrawResourceAccess(c, "john");
+        c = m_oauthConsumerRegistry.getConsumer("key1");
+        userIds = ((FSConsumerEntity) c).getAllowedUserIds();
+        Assert.assertArrayEquals(new String[] { "eric" }, userIds.toArray(new 
String[1]));
+
+        Assert.assertFalse(m_oauthConsumerRegistry.hasResourceAccess(c, 
"john"));
+        Assert.assertTrue(m_oauthConsumerRegistry.hasResourceAccess(c, 
"eric"));
+    }
+}

Reply via email to