Author: [email protected]
Date: Fri Sep 23 11:14:15 2011
New Revision: 1410

Log:
[AMDATUAUTH-92] Refactored tenant awareness in token provider, as it was not 
implemented properly. Now not only OAuthTokenProvider is full tenant aware, but 
also the implementations of TokenStorageProvider, OAuthNonceStorageProvider and 
OAuthServiceConsumerRegistry

Modified:
   
trunk/amdatu-auth/oauth-consumerregistry/src/main/java/org/amdatu/auth/oauth/consumerregistry/service/OAuthServiceConsumerRegistryRESTImpl.java
   
trunk/amdatu-auth/oauth-server/src/main/java/org/amdatu/auth/oauth/server/osgi/Activator.java
   
trunk/amdatu-auth/oauth-server/src/main/java/org/amdatu/auth/oauth/server/service/OAuthTokenProviderImpl.java
   
trunk/amdatu-auth/oauth-server/src/main/java/org/amdatu/auth/oauth/server/servlet/OAuthAccessTokenServletImpl.java
   
trunk/amdatu-auth/oauth-server/src/main/java/org/amdatu/auth/oauth/server/servlet/OAuthAuthorizeTokenServletImpl.java
   
trunk/amdatu-auth/oauth-server/src/main/java/org/amdatu/auth/oauth/server/servlet/OAuthRequestTokenServletImpl.java
   
trunk/amdatu-auth/oauth-stores/consumer-store-fs/src/main/java/org/amdatu/auth/oauth/consumerregistry/fs/service/FSConsumerRegistryImpl.java
   trunk/amdatu-auth/oauth-stores/nonce-store-mem/pom.xml
   
trunk/amdatu-auth/oauth-stores/nonce-store-mem/src/main/java/org/amdatu/auth/oauth/store/nonce/mem/osgi/Activator.java
   
trunk/amdatu-auth/oauth-stores/nonce-store-mem/src/main/java/org/amdatu/auth/oauth/store/nonce/mem/service/InMemNonceStorageProviderImpl.java
   
trunk/amdatu-auth/tokenprovider/src/main/java/org/amdatu/auth/tokenprovider/service/TokenProviderImpl.java
   trunk/amdatu-auth/tokenstore-mem/pom.xml
   
trunk/amdatu-auth/tokenstore-mem/src/main/java/org/amdatu/auth/tokenstore/mem/osgi/Activator.java
   
trunk/amdatu-auth/tokenstore-mem/src/main/java/org/amdatu/auth/tokenstore/mem/service/InMemTokenStorageProviderImpl.java

Modified: 
trunk/amdatu-auth/oauth-consumerregistry/src/main/java/org/amdatu/auth/oauth/consumerregistry/service/OAuthServiceConsumerRegistryRESTImpl.java
==============================================================================
--- 
trunk/amdatu-auth/oauth-consumerregistry/src/main/java/org/amdatu/auth/oauth/consumerregistry/service/OAuthServiceConsumerRegistryRESTImpl.java
     (original)
+++ 
trunk/amdatu-auth/oauth-consumerregistry/src/main/java/org/amdatu/auth/oauth/consumerregistry/service/OAuthServiceConsumerRegistryRESTImpl.java
     Fri Sep 23 11:14:15 2011
@@ -118,7 +118,8 @@
     }
 
     public void start() {
-        m_logService.log(LogService.LOG_DEBUG, "OAuth Service Consumer 
registry REST service started.");
+        m_logService.log(LogService.LOG_DEBUG,
+            "Service '" + getClass().getName() + "' started for tenant '" + 
m_tenant.getId() + "'");
     }
 
     /**

Modified: 
trunk/amdatu-auth/oauth-server/src/main/java/org/amdatu/auth/oauth/server/osgi/Activator.java
==============================================================================
--- 
trunk/amdatu-auth/oauth-server/src/main/java/org/amdatu/auth/oauth/server/osgi/Activator.java
       (original)
+++ 
trunk/amdatu-auth/oauth-server/src/main/java/org/amdatu/auth/oauth/server/osgi/Activator.java
       Fri Sep 23 11:14:15 2011
@@ -21,10 +21,8 @@
 import javax.servlet.Servlet;
 
 import org.amdatu.auth.oauth.api.OAuthServiceProvider;
-import org.amdatu.auth.oauth.consumerregistry.OAuthServiceConsumerRegistry;
 import org.amdatu.auth.oauth.server.OAuthAccessTokenServlet;
 import org.amdatu.auth.oauth.server.OAuthAuthorizeTokenServlet;
-import org.amdatu.auth.oauth.server.OAuthNonceStorageProvider;
 import org.amdatu.auth.oauth.server.OAuthRequestTokenServlet;
 import org.amdatu.auth.oauth.server.OAuthServerConfig;
 import org.amdatu.auth.oauth.server.OAuthTokenProvider;
@@ -36,7 +34,6 @@
 import org.amdatu.auth.oauth.server.servlet.OAuthAccessTokenServletImpl;
 import org.amdatu.auth.oauth.server.servlet.OAuthAuthorizeTokenServletImpl;
 import org.amdatu.auth.oauth.server.servlet.OAuthRequestTokenServletImpl;
-import org.amdatu.auth.tokenprovider.TokenStorageProvider;
 import org.amdatu.core.tenant.Tenant;
 import org.amdatu.libraries.utilities.osgi.ServiceDependentActivator;
 import org.amdatu.web.dispatcher.DispatcherService;
@@ -83,11 +80,8 @@
                 .setInterface(OAuthTokenProvider.class.getName(), null)
                 .setImplementation(OAuthTokenProviderImpl.class)
                 
.add(createServiceDependency().setService(LogService.class).setRequired(true))
-                
.add(createServiceDependency().setService(TokenStorageProvider.class).setRequired(true))
                 
.add(createServiceDependency().setService(OAuthServiceProvider.class).setRequired(true))
-                
.add(createServiceDependency().setService(OAuthServerConfiguration.class).setRequired(true))
-                
.add(createServiceDependency().setService(OAuthNonceStorageProvider.class).setRequired(true))
-                
.add(createServiceDependency().setService(OAuthServiceConsumerRegistry.class).setRequired(true)));
+                
.add(createServiceDependency().setService(OAuthServerConfiguration.class).setRequired(true)));
 
         // Create and register the resource provider
         Dictionary<String, Object> properties = new Hashtable<String, 
Object>();

Modified: 
trunk/amdatu-auth/oauth-server/src/main/java/org/amdatu/auth/oauth/server/service/OAuthTokenProviderImpl.java
==============================================================================
--- 
trunk/amdatu-auth/oauth-server/src/main/java/org/amdatu/auth/oauth/server/service/OAuthTokenProviderImpl.java
       (original)
+++ 
trunk/amdatu-auth/oauth-server/src/main/java/org/amdatu/auth/oauth/server/service/OAuthTokenProviderImpl.java
       Fri Sep 23 11:14:15 2011
@@ -37,28 +37,37 @@
 import org.amdatu.auth.tokenprovider.TokenStorageProvider;
 import org.amdatu.core.tenant.Tenant;
 import org.apache.felix.dm.Component;
+import org.apache.felix.dm.DependencyManager;
+import org.apache.felix.dm.ServiceDependency;
+import org.osgi.framework.Constants;
 import org.osgi.service.log.LogService;
 
 public class OAuthTokenProviderImpl implements OAuthTokenProvider {
     // Default byte size of a random generated nonce
     private static final int NONCE_BYTESIZE = 32;
 
-    // Service dependencies injected by the dependency manager
+    // Tenant unaware service dependencies
     private volatile LogService m_logService;
     private volatile OAuthServiceProvider m_serviceProvider;
-    private volatile OAuthServiceConsumerRegistry m_consumerRegistry;
-    private volatile TokenStorageProvider m_tokenStore;
     private volatile OAuthServerConfiguration m_config;
+    private volatile Tenant m_tenant;
+    private volatile Component m_component;
+    private volatile DependencyManager m_dependencyManager;
+    
+    // Tenant aware service dependencies
+    private volatile TokenStorageProvider m_tokenStore;
     private volatile OAuthNonceStorageProvider m_nonceStore;
-
+    private volatile OAuthServiceConsumerRegistry m_consumerRegistry;
+    
+    
     // The simple oAuth validator.
     private PluggableOAuthValidator m_oAuthValidator;
 
-    // Other private members
-    private String m_tenantId;
-
     public void init(final Component component) {
-        m_tenantId = 
component.getServiceProperties().get(Tenant.TENANT_ID_SERVICEPROPERTY).toString();
+        // Define tenant aware service dependencies
+        
m_component.add(getTenantServiceDependency(TokenStorageProvider.class));
+        
m_component.add(getTenantServiceDependency(OAuthNonceStorageProvider.class));
+        
m_component.add(getTenantServiceDependency(OAuthServiceConsumerRegistry.class));
     }
 
     public void start() {
@@ -71,9 +80,18 @@
         }
         m_oAuthValidator = new PluggableOAuthValidator(requestMaxAge, 
Double.parseDouble(OAuth.VERSION_1_0), m_nonceStore);
 
-        m_logService.log(LogService.LOG_DEBUG, "OAuthTokenProvider service 
started for tenant '" + m_tenantId + "'");
+        m_logService.log(LogService.LOG_DEBUG, "Service '" + 
getClass().getName() + "' started for tenant '" + m_tenant.getId() + "'");
     }
 
+    private ServiceDependency getTenantServiceDependency(Class<?> clazz) {
+        String tenantFilter =  "(&(" + Tenant.TENANT_ID_SERVICEPROPERTY + "=" 
+ m_tenant.getId() 
+            + ")(" + Constants.OBJECTCLASS + "=" + clazz.getName() + "))";
+        return m_dependencyManager.createServiceDependency()
+            .setService(clazz, tenantFilter)
+            .setRequired(true)
+            .setInstanceBound(true);
+    }
+    
     public synchronized OAuthConsumer getConsumer(final OAuthMessage 
requestMessage) throws OAuthException {
         try {
             // try to load from local cache if not throw exception

Modified: 
trunk/amdatu-auth/oauth-server/src/main/java/org/amdatu/auth/oauth/server/servlet/OAuthAccessTokenServletImpl.java
==============================================================================
--- 
trunk/amdatu-auth/oauth-server/src/main/java/org/amdatu/auth/oauth/server/servlet/OAuthAccessTokenServletImpl.java
  (original)
+++ 
trunk/amdatu-auth/oauth-server/src/main/java/org/amdatu/auth/oauth/server/servlet/OAuthAccessTokenServletImpl.java
  Fri Sep 23 11:14:15 2011
@@ -50,7 +50,8 @@
     }
 
     public void start() {
-        m_logService.log(LogService.LOG_DEBUG, "OAuth Access Token servlet 
started for tenant '" + m_tenantId + "'");
+        m_logService.log(LogService.LOG_DEBUG,
+            "Service '" + getClass().getName() + "' started for tenant '" + 
m_tenantId + "'");
     }
 
     @Override

Modified: 
trunk/amdatu-auth/oauth-server/src/main/java/org/amdatu/auth/oauth/server/servlet/OAuthAuthorizeTokenServletImpl.java
==============================================================================
--- 
trunk/amdatu-auth/oauth-server/src/main/java/org/amdatu/auth/oauth/server/servlet/OAuthAuthorizeTokenServletImpl.java
       (original)
+++ 
trunk/amdatu-auth/oauth-server/src/main/java/org/amdatu/auth/oauth/server/servlet/OAuthAuthorizeTokenServletImpl.java
       Fri Sep 23 11:14:15 2011
@@ -69,7 +69,8 @@
     }
 
     public void start() {
-        m_logService.log(LogService.LOG_DEBUG, "OAuth Authorize Token servlet 
started for tenant '" + m_tenantId + "'");
+        m_logService.log(LogService.LOG_DEBUG,
+            "Service '" + getClass().getName() + "' started for tenant '" + 
m_tenantId + "'");
     }
 
     @Override

Modified: 
trunk/amdatu-auth/oauth-server/src/main/java/org/amdatu/auth/oauth/server/servlet/OAuthRequestTokenServletImpl.java
==============================================================================
--- 
trunk/amdatu-auth/oauth-server/src/main/java/org/amdatu/auth/oauth/server/servlet/OAuthRequestTokenServletImpl.java
 (original)
+++ 
trunk/amdatu-auth/oauth-server/src/main/java/org/amdatu/auth/oauth/server/servlet/OAuthRequestTokenServletImpl.java
 Fri Sep 23 11:14:15 2011
@@ -58,7 +58,8 @@
     }
 
     public void start() {
-        m_logService.log(LogService.LOG_DEBUG, "OAuth Request Token servlet 
started for tenant '" + m_tenantId + "'");
+        m_logService.log(LogService.LOG_DEBUG,
+            "Service '" + getClass().getName() + "' started for tenant '" + 
m_tenantId + "'");
     }
 
     @Override

Modified: 
trunk/amdatu-auth/oauth-stores/consumer-store-fs/src/main/java/org/amdatu/auth/oauth/consumerregistry/fs/service/FSConsumerRegistryImpl.java
==============================================================================
--- 
trunk/amdatu-auth/oauth-stores/consumer-store-fs/src/main/java/org/amdatu/auth/oauth/consumerregistry/fs/service/FSConsumerRegistryImpl.java
        (original)
+++ 
trunk/amdatu-auth/oauth-stores/consumer-store-fs/src/main/java/org/amdatu/auth/oauth/consumerregistry/fs/service/FSConsumerRegistryImpl.java
        Fri Sep 23 11:14:15 2011
@@ -13,8 +13,8 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.amdatu.auth.oauth.consumerregistry.fs.service;
-
+package org.amdatu.auth.oauth.consumerregistry.fs.service;
+
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
@@ -31,85 +31,94 @@
 import org.amdatu.auth.oauth.consumerregistry.OAuthServiceConsumerRegistry;
 import org.amdatu.auth.oauth.consumerregistry.fs.internal.FSConsumerEntity;
 import org.amdatu.auth.oauth.consumerregistry.fs.internal.FSConsumerStorage;
+import org.amdatu.core.tenant.Tenant;
+import org.apache.felix.dm.Component;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Filter;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.service.cm.ConfigurationException;
 import org.osgi.service.cm.ManagedService;
 import org.osgi.service.log.LogService;
-
-public class FSConsumerRegistryImpl implements ManagedService, 
OAuthServiceConsumerRegistry {
-    /**
-     * Configuration PID.
-     */
-    public static final String CONFIGURATION_PID = 
"org.amdatu.auth.oauth.consumerregistry.fs";
-
-    /**
-     * Configuration property name holding the data directory.
-     */
-    public static final String DATA_DIRECTORY = "datadir";
-
-    // Injected service dependencies
+
+public class FSConsumerRegistryImpl implements ManagedService, 
OAuthServiceConsumerRegistry {
+    /**
+     * Configuration PID.
+     */
+    public static final String CONFIGURATION_PID = 
"org.amdatu.auth.oauth.consumerregistry.fs";
+
+    /**
+     * Configuration property name holding the data directory.
+     */
+    public static final String DATA_DIRECTORY = "datadir";
+
+    // Injected service dependencies
     private volatile LogService m_logService;
-    private volatile BundleContext m_context;
+    private volatile BundleContext m_context;
+    private volatile Component m_component;
+    private volatile Tenant m_tenant;
+
     private FSConsumerStorage m_storage;
 
     public void start() {
-        m_logService.log(LogService.LOG_DEBUG, "FSConsumerRegistryImpl service 
started");
-    }
-
-    /**
-     * Invoked since this is a managed service.
-     *
-     * @param dictionary
-     *        Dictionary holding the configuration properties
-     * @throws ConfigurationException
-     *         In case some Config Admin exception occurred
-     */
-    public synchronized void updated(final Dictionary dictionary) throws 
ConfigurationException {
-        if (dictionary != null) {
-            String dataDirectoryName = (String) dictionary.get(DATA_DIRECTORY);
-            if (dataDirectoryName == null || "".equals(dataDirectoryName)) {
-                throw new ConfigurationException(DATA_DIRECTORY, "Missing 
mandatory data directory configuration");
-            }
-            File dataDirectory = new File(dataDirectoryName);
-            try {
-                setDataDirectory(dataDirectory);
-            }
-            catch (ConsumerRegistryStorageException e) {
-                throw new ConfigurationException(DATA_DIRECTORY, 
e.getMessage());
-            }
-        }
-    }
-
-    public synchronized void setDataDirectory(final File dataDirectory) throws 
ConsumerRegistryStorageException {
-        File newDataDirectory = dataDirectory;
-        if (!newDataDirectory.isAbsolute()) {
-            // If amdatu.dir is provided as System property, use this as base 
directory instead of the current directory
-            File baseDirectory;
-            if (System.getProperty("amdatu.dir") != null) {
-                baseDirectory = new File(System.getProperty("amdatu.dir"));
-            }
-            else {
-                baseDirectory = new File(System.getProperty("user.dir"));
-            }
-            newDataDirectory = new File(baseDirectory, 
newDataDirectory.getPath());
-        }
-        if (!((newDataDirectory.exists() && newDataDirectory.canRead() && 
newDataDirectory.canWrite())
-                        || newDataDirectory.mkdirs())) {
-            throw new ConsumerRegistryStorageException("Unable to access data 
directory: "
-                + newDataDirectory.getAbsolutePath());
-        }
-        try {
-            m_storage = new FSConsumerStorage(newDataDirectory);
-            if (m_logService != null) {
-                m_logService.log(LogService.LOG_DEBUG, "Datadirectory set to: "
-                    + m_storage.getDataDirectory().getAbsolutePath());
-            }
-        }
-        catch (IOException e) {
-            throw new ConsumerRegistryStorageException(e);
-        }
+        m_logService.log(LogService.LOG_DEBUG,
+            "Service '" + getClass().getName() + "' started for tenant '" + 
m_tenant.getId() + "'");
+    }
+
+    /**
+     * Invoked since this is a managed service.
+     * 
+     * @param dictionary
+     *        Dictionary holding the configuration properties
+     * @throws ConfigurationException
+     *         In case some Config Admin exception occurred
+     */
+    public synchronized void updated(final Dictionary dictionary) throws 
ConfigurationException {
+        if (dictionary != null) {
+            // NB: we must read the tenant id from the service properties 
instead of injected Tenant, ass the tenant
+            // is not yet injected by the dependency manager at this time
+            String tenantId = (String) 
m_component.getServiceProperties().get(Tenant.TENANT_ID_SERVICEPROPERTY);
+            String dataDirectoryName = (String) dictionary.get(DATA_DIRECTORY);
+            if (dataDirectoryName == null || "".equals(dataDirectoryName)) {
+                throw new ConfigurationException(DATA_DIRECTORY, "Missing 
mandatory data directory configuration");
+            }
+            File dataDirectory = new File(dataDirectoryName + 
File.pathSeparator + tenantId);
+            try {
+                setDataDirectory(dataDirectory);
+            }
+            catch (ConsumerRegistryStorageException e) {
+                throw new ConfigurationException(DATA_DIRECTORY, 
e.getMessage());
+            }
+        }
+    }
+
+    public synchronized void setDataDirectory(final File dataDirectory) throws 
ConsumerRegistryStorageException {
+        File newDataDirectory = dataDirectory;
+        if (!newDataDirectory.isAbsolute()) {
+            // If amdatu.dir is provided as System property, use this as base 
directory instead of the current directory
+            File baseDirectory;
+            if (System.getProperty("amdatu.dir") != null) {
+                baseDirectory = new File(System.getProperty("amdatu.dir"));
+            }
+            else {
+                baseDirectory = new File(System.getProperty("user.dir"));
+            }
+            newDataDirectory = new File(baseDirectory, 
newDataDirectory.getPath());
+        }
+        if (!((newDataDirectory.exists() && newDataDirectory.canRead() && 
newDataDirectory.canWrite()) || newDataDirectory
+            .mkdirs())) {
+            throw new ConsumerRegistryStorageException("Unable to access data 
directory: "
+                + newDataDirectory.getAbsolutePath());
+        }
+        try {
+            m_storage = new FSConsumerStorage(newDataDirectory);
+            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 Iterator<OAuthServiceConsumer> getConsumers(String filterString) 
throws ConsumerRegistryStorageException {
@@ -132,14 +141,15 @@
                             add = true;
                         }
                     }
-                } else {
+                }
+                else {
                     add = true;
                 }
                 if (add) {
                     consumers.add(getConsumer(entity.getConsumerKey()));
                 }
             }
-            return consumers.listIterator() ;
+            return consumers.listIterator();
         }
         catch (IOException e) {
             throw new ConsumerRegistryStorageException(e);
@@ -147,121 +157,121 @@
         catch (InvalidSyntaxException e) {
             throw new ConsumerRegistryStorageException(e);
         }
-    }
-
-    public synchronized OAuthServiceConsumer getConsumer(final String 
consumerKey)
-    throws ConsumerRegistryStorageException {
-        try {
-            return m_storage.getEntity(consumerKey);
-        }
-        catch (IOException e) {
-            throw new ConsumerRegistryStorageException(e);
-        }
-    }
-
-    public synchronized void addConsumer(final 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(final 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 removeConsumer(final 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(final OAuthServiceConsumer 
consumer, final 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(final OAuthServiceConsumer 
consumer, final String userId)
-    throws ConsumerNotFoundException, ConsumerRegistryStorageException {
-        try {
-            FSConsumerEntity entity = 
m_storage.getEntity(consumer.getConsumerKey());
-            if (entity == null) {
-                throw new ConsumerNotFoundException("Consumer with key '" + 
consumer.getConsumerKey()
-                    + "' doesn't exist");
-            }
-            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(final OAuthServiceConsumer consumer, 
final String userId)
-    throws ConsumerNotFoundException, ConsumerRegistryStorageException {
-        try {
-            FSConsumerEntity entity = 
m_storage.getEntity(consumer.getConsumerKey());
-            if (entity == null) {
-                throw new ConsumerNotFoundException("Consumer with key '" + 
consumer.getConsumerKey()
-                    + "' doesn't exist");
-            }
-            return entity.getAllowedUserIds() != null && 
entity.getAllowedUserIds().contains(userId);
-        }
-        catch (IOException e) {
-            throw new ConsumerRegistryStorageException(e);
-        }
-    }
-}
+    }
+
+    public synchronized OAuthServiceConsumer getConsumer(final String 
consumerKey)
+        throws ConsumerRegistryStorageException {
+        try {
+            return m_storage.getEntity(consumerKey);
+        }
+        catch (IOException e) {
+            throw new ConsumerRegistryStorageException(e);
+        }
+    }
+
+    public synchronized void addConsumer(final 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(final 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 removeConsumer(final 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(final OAuthServiceConsumer 
consumer, final 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(final OAuthServiceConsumer 
consumer, final String userId)
+        throws ConsumerNotFoundException, ConsumerRegistryStorageException {
+        try {
+            FSConsumerEntity entity = 
m_storage.getEntity(consumer.getConsumerKey());
+            if (entity == null) {
+                throw new ConsumerNotFoundException("Consumer with key '" + 
consumer.getConsumerKey()
+                    + "' doesn't exist");
+            }
+            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(final OAuthServiceConsumer consumer, 
final String userId)
+        throws ConsumerNotFoundException, ConsumerRegistryStorageException {
+        try {
+            FSConsumerEntity entity = 
m_storage.getEntity(consumer.getConsumerKey());
+            if (entity == null) {
+                throw new ConsumerNotFoundException("Consumer with key '" + 
consumer.getConsumerKey()
+                    + "' doesn't exist");
+            }
+            return entity.getAllowedUserIds() != null && 
entity.getAllowedUserIds().contains(userId);
+        }
+        catch (IOException e) {
+            throw new ConsumerRegistryStorageException(e);
+        }
+    }
+}

Modified: trunk/amdatu-auth/oauth-stores/nonce-store-mem/pom.xml
==============================================================================
--- trunk/amdatu-auth/oauth-stores/nonce-store-mem/pom.xml      (original)
+++ trunk/amdatu-auth/oauth-stores/nonce-store-mem/pom.xml      Fri Sep 23 
11:14:15 2011
@@ -37,6 +37,11 @@
       <groupId>org.amdatu.libraries</groupId>
       <artifactId>org.amdatu.libraries.fsstorage</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.amdatu.core</groupId>
+      <artifactId>org.amdatu.core.tenant</artifactId>
+      <type>bundle</type>
+    </dependency>
   </dependencies>
 
   <build>

Modified: 
trunk/amdatu-auth/oauth-stores/nonce-store-mem/src/main/java/org/amdatu/auth/oauth/store/nonce/mem/osgi/Activator.java
==============================================================================
--- 
trunk/amdatu-auth/oauth-stores/nonce-store-mem/src/main/java/org/amdatu/auth/oauth/store/nonce/mem/osgi/Activator.java
      (original)
+++ 
trunk/amdatu-auth/oauth-stores/nonce-store-mem/src/main/java/org/amdatu/auth/oauth/store/nonce/mem/osgi/Activator.java
      Fri Sep 23 11:14:15 2011
@@ -17,6 +17,7 @@
 
 import org.amdatu.auth.oauth.server.OAuthNonceStorageProvider;
 import 
org.amdatu.auth.oauth.store.nonce.mem.service.InMemNonceStorageProviderImpl;
+import org.amdatu.core.tenant.Tenant;
 import org.apache.felix.dm.DependencyActivatorBase;
 import org.apache.felix.dm.DependencyManager;
 import org.osgi.framework.BundleContext;
@@ -31,7 +32,7 @@
     public void init(final BundleContext context, final DependencyManager 
manager) throws Exception {
 
         manager.add(
-                createComponent()
+                createAdapterService(Tenant.class, null)
                     .setImplementation(InMemNonceStorageProviderImpl.class)
                     .setInterface(OAuthNonceStorageProvider.class.getName(), 
null)
                     
.add(createServiceDependency().setService(LogService.class).setRequired(true)));

Modified: 
trunk/amdatu-auth/oauth-stores/nonce-store-mem/src/main/java/org/amdatu/auth/oauth/store/nonce/mem/service/InMemNonceStorageProviderImpl.java
==============================================================================
--- 
trunk/amdatu-auth/oauth-stores/nonce-store-mem/src/main/java/org/amdatu/auth/oauth/store/nonce/mem/service/InMemNonceStorageProviderImpl.java
       (original)
+++ 
trunk/amdatu-auth/oauth-stores/nonce-store-mem/src/main/java/org/amdatu/auth/oauth/store/nonce/mem/service/InMemNonceStorageProviderImpl.java
       Fri Sep 23 11:14:15 2011
@@ -22,8 +22,19 @@
 import java.util.TreeSet;
 
 import org.amdatu.auth.oauth.server.OAuthNonceStorageProvider;
+import org.amdatu.core.tenant.Tenant;
+import org.osgi.service.log.LogService;
 
 public class InMemNonceStorageProviderImpl implements 
OAuthNonceStorageProvider {
+    // Tenant unaware service dependencies, injected by the dependency manager
+    private volatile Tenant m_tenant;
+    private volatile LogService m_logService;
+
+    public void start() {
+        m_logService.log(LogService.LOG_DEBUG,
+            "Service '" + getClass().getName() + "' started for tenant '" + 
m_tenant.getId() + "'");
+    }
+
     private final Set<UsedNonce> usedNonces = new TreeSet<UsedNonce>();
 
     public boolean addNonce(long timestamp, String nonce, String[] 
requestParams) {

Modified: 
trunk/amdatu-auth/tokenprovider/src/main/java/org/amdatu/auth/tokenprovider/service/TokenProviderImpl.java
==============================================================================
--- 
trunk/amdatu-auth/tokenprovider/src/main/java/org/amdatu/auth/tokenprovider/service/TokenProviderImpl.java
  (original)
+++ 
trunk/amdatu-auth/tokenprovider/src/main/java/org/amdatu/auth/tokenprovider/service/TokenProviderImpl.java
  Fri Sep 23 11:14:15 2011
@@ -96,7 +96,8 @@
     }
 
     public void start() {
-        m_logService.log(LogService.LOG_INFO, "Token provider for tenant '" + 
m_tenant.getId() + "' started.");
+        m_logService.log(LogService.LOG_DEBUG,
+            "Service '" + getClass().getName() + "' started for tenant '" + 
m_tenant.getId() + "'");
     }
     
     private String generateSignature(final SortedMap<String, String> 
attributes) throws TokenProviderException {

Modified: trunk/amdatu-auth/tokenstore-mem/pom.xml
==============================================================================
--- trunk/amdatu-auth/tokenstore-mem/pom.xml    (original)
+++ trunk/amdatu-auth/tokenstore-mem/pom.xml    Fri Sep 23 11:14:15 2011
@@ -33,6 +33,11 @@
       <artifactId>org.amdatu.auth.tokenprovider</artifactId>
       <type>bundle</type>
     </dependency>
+    <dependency>
+      <groupId>org.amdatu.core</groupId>
+      <artifactId>org.amdatu.core.tenant</artifactId>
+      <type>bundle</type>
+    </dependency>
   </dependencies>
 
   <build>

Modified: 
trunk/amdatu-auth/tokenstore-mem/src/main/java/org/amdatu/auth/tokenstore/mem/osgi/Activator.java
==============================================================================
--- 
trunk/amdatu-auth/tokenstore-mem/src/main/java/org/amdatu/auth/tokenstore/mem/osgi/Activator.java
   (original)
+++ 
trunk/amdatu-auth/tokenstore-mem/src/main/java/org/amdatu/auth/tokenstore/mem/osgi/Activator.java
   Fri Sep 23 11:14:15 2011
@@ -17,6 +17,7 @@
 
 import org.amdatu.auth.tokenprovider.TokenStorageProvider;
 import org.amdatu.auth.tokenstore.mem.service.InMemTokenStorageProviderImpl;
+import org.amdatu.core.tenant.Tenant;
 import org.apache.felix.dm.DependencyActivatorBase;
 import org.apache.felix.dm.DependencyManager;
 import org.osgi.framework.BundleContext;
@@ -33,7 +34,7 @@
     public void init(final BundleContext context, final DependencyManager 
manager) throws Exception {
         // Create and register the OAuth token provider service component.
         manager.add(
-            createComponent()
+            createAdapterService(Tenant.class, null)
                 .setInterface(TokenStorageProvider.class.getName(), null)
                 .setImplementation(InMemTokenStorageProviderImpl.class)
                 
.add(createServiceDependency().setService(LogService.class).setRequired(true)));

Modified: 
trunk/amdatu-auth/tokenstore-mem/src/main/java/org/amdatu/auth/tokenstore/mem/service/InMemTokenStorageProviderImpl.java
==============================================================================
--- 
trunk/amdatu-auth/tokenstore-mem/src/main/java/org/amdatu/auth/tokenstore/mem/service/InMemTokenStorageProviderImpl.java
    (original)
+++ 
trunk/amdatu-auth/tokenstore-mem/src/main/java/org/amdatu/auth/tokenstore/mem/service/InMemTokenStorageProviderImpl.java
    Fri Sep 23 11:14:15 2011
@@ -13,39 +13,50 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.amdatu.auth.tokenstore.mem.service;
-
+package org.amdatu.auth.tokenstore.mem.service;
+
 import java.util.HashMap;
 import java.util.Map;
 
 import org.amdatu.auth.tokenprovider.Token;
 import org.amdatu.auth.tokenprovider.TokenStorageProvider;
-
-/**
- * This class implements a very simple in-memory token store.
- * 
- * @author ivol
- */
-public class InMemTokenStorageProviderImpl implements TokenStorageProvider {
+import org.amdatu.core.tenant.Tenant;
+import org.osgi.service.log.LogService;
+
+/**
+ * This class implements a very simple in-memory token store.
+ * 
+ * @author ivol
+ */
+public class InMemTokenStorageProviderImpl implements TokenStorageProvider {
+    // Service dependencies, injected by the dependency manager
+    private volatile Tenant m_tenant;
+    private volatile LogService m_logService;
+
     // The in-memory token store
-    private Map<String, Token> m_tokens = new HashMap<String, Token>();
-
+    private Map<String, Token> m_tokens = new HashMap<String, Token>();
+
+    public void start() {
+        m_logService.log(LogService.LOG_DEBUG, "Service '" + 
getClass().getName() + "' started for tenant '"
+                + m_tenant.getId() + "'");
+    }
+
     public void addToken(Token token) {
-        m_tokens.put(token.getToken(), token);
+        m_tokens.put(token.getToken(), token);
     }
-    
+
     public Token getToken(String token) {
         if (m_tokens.containsKey(token)) {
             return m_tokens.get(token);
         }
         return null;
-    }
-
-    public boolean hasToken(final String token) {
-        return m_tokens.containsKey(token);
-    }
-
-    public void removeToken(final Token token) {
-        m_tokens.remove(token.getToken());
-    }
-}
+    }
+
+    public boolean hasToken(final String token) {
+        return m_tokens.containsKey(token);
+    }
+
+    public void removeToken(final Token token) {
+        m_tokens.remove(token.getToken());
+    }
+}
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to