Author: ivol37 at gmail.com
Date: Tue Feb 1 11:14:07 2011
New Revision: 739
Log:
[AMDATU-84] Fixed tenant awareness of shindig AppData service
Added:
branches/amdatu-dispatcher/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/service/TenantDispatchExtender.java
Modified:
branches/amdatu-dispatcher/amdatu-authorization/login-service/src/main/java/org/amdatu/authorization/login/service/LoginService.java
branches/amdatu-dispatcher/amdatu-authorization/login-service/src/main/java/org/amdatu/authorization/login/service/service/LoginServiceImpl.java
branches/amdatu-dispatcher/amdatu-opensocial/gadgetmanagement/pom.xml
branches/amdatu-dispatcher/amdatu-opensocial/gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/osgi/Activator.java
branches/amdatu-dispatcher/amdatu-opensocial/shindig/pom.xml
branches/amdatu-dispatcher/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/osgi/Activator.java
branches/amdatu-dispatcher/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/persistence/CassandraAppDataServiceStore.java
Modified:
branches/amdatu-dispatcher/amdatu-authorization/login-service/src/main/java/org/amdatu/authorization/login/service/LoginService.java
==============================================================================
---
branches/amdatu-dispatcher/amdatu-authorization/login-service/src/main/java/org/amdatu/authorization/login/service/LoginService.java
(original)
+++
branches/amdatu-dispatcher/amdatu-authorization/login-service/src/main/java/org/amdatu/authorization/login/service/LoginService.java
Tue Feb 1 11:14:07 2011
@@ -36,6 +36,11 @@
String PASSWORD_CREDENTIAL_KEY = "password";
/**
+ * Key under which the tenant id is stored in the credentials of the User.
+ */
+ String TENANT_CREDENTIAL_KEY = "tenantid";
+
+ /**
* NB: Do NOT use this method since it will be removed from future
versions. For more information,
* see http://jira.amdatu.org/jira/browse/AMDATU-166.
*
Modified:
branches/amdatu-dispatcher/amdatu-authorization/login-service/src/main/java/org/amdatu/authorization/login/service/service/LoginServiceImpl.java
==============================================================================
---
branches/amdatu-dispatcher/amdatu-authorization/login-service/src/main/java/org/amdatu/authorization/login/service/service/LoginServiceImpl.java
(original)
+++
branches/amdatu-dispatcher/amdatu-authorization/login-service/src/main/java/org/amdatu/authorization/login/service/service/LoginServiceImpl.java
Tue Feb 1 11:14:07 2011
@@ -110,6 +110,7 @@
User adminUser = (User)
m_userAdmin.createRole(DEFAULT_USERNAME, Role.USER);
if (adminUser != null) {
adminUser.getCredentials().put(PASSWORD_CREDENTIAL_KEY, DEFAULT_PASSWORD);
+
adminUser.getCredentials().put(TENANT_CREDENTIAL_KEY, m_tenant.getId());
adminUser.getProperties().put(USER_NAME_CREDENTIAL_KEY, DEFAULT_USERNAME);
Group adminGroup = (Group)
m_userAdmin.createRole(DEFAULT_ADMIN_GROUP, Role.GROUP);
adminGroup.addMember(adminUser);
Modified: branches/amdatu-dispatcher/amdatu-opensocial/gadgetmanagement/pom.xml
==============================================================================
--- branches/amdatu-dispatcher/amdatu-opensocial/gadgetmanagement/pom.xml
(original)
+++ branches/amdatu-dispatcher/amdatu-opensocial/gadgetmanagement/pom.xml
Tue Feb 1 11:14:07 2011
@@ -12,8 +12,8 @@
<packaging>bundle</packaging>
<name>Amdatu Open Social - Gadget Management</name>
<description>Provides a service to manage gadgets stored in the gadget
store</description>
-
- <dependencies>
+
+ <dependencies>
<dependency>
<groupId>org.amdatu.web</groupId>
<artifactId>httpcontext</artifactId>
@@ -32,7 +32,7 @@
<version>${platform.version}</version>
<scope>provided</scope>
<type>bundle</type>
- </dependency>
+ </dependency>
<dependency>
<groupId>org.amdatu.cassandra</groupId>
<artifactId>listener</artifactId>
@@ -52,7 +52,7 @@
<artifactId>shindig</artifactId>
<scope>provided</scope>
<type>bundle</type>
- </dependency>
+ </dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
@@ -71,8 +71,15 @@
<scope>provided</scope>
<type>bundle</type>
</dependency>
+ <dependency>
+ <groupId>org.amdatu.core</groupId>
+ <artifactId>tenant</artifactId>
+ <version>${platform.version}</version>
+ <scope>provided</scope>
+ <type>bundle</type>
+ </dependency>
</dependencies>
-
+
<build>
<plugins>
<plugin>
@@ -81,12 +88,12 @@
<configuration>
<instructions>
<Bundle-Activator>org.amdatu.opensocial.gadgetmanagement.osgi.Activator</Bundle-Activator>
- <Bundle-SymbolicName>
org.amdatu.opensocial.gadgetmanagement</Bundle-SymbolicName>
+ <Bundle-SymbolicName>
org.amdatu.opensocial.gadgetmanagement</Bundle-SymbolicName>
<Embed-Dependency>*;scope=compile</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>
</instructions>
</configuration>
- </plugin>
- </plugins>
- </build>
+ </plugin>
+ </plugins>
+ </build>
</project>
Modified:
branches/amdatu-dispatcher/amdatu-opensocial/gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/osgi/Activator.java
==============================================================================
---
branches/amdatu-dispatcher/amdatu-opensocial/gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/osgi/Activator.java
(original)
+++
branches/amdatu-dispatcher/amdatu-opensocial/gadgetmanagement/src/main/java/org/amdatu/opensocial/gadgetmanagement/osgi/Activator.java
Tue Feb 1 11:14:07 2011
@@ -19,6 +19,7 @@
import org.amdatu.authentication.tokenprovider.TokenProvider;
import org.amdatu.cassandra.listener.ColumnFamilyAvailable;
import org.amdatu.cassandra.persistencemanager.CassandraPersistenceManager;
+import org.amdatu.core.tenant.Tenant;
import org.amdatu.libraries.utilities.osgi.ServiceDependentActivator;
import org.amdatu.opensocial.gadgetmanagement.GadgetManagement;
import
org.amdatu.opensocial.gadgetmanagement.service.GadgetManagementServiceImpl;
@@ -28,6 +29,7 @@
import org.amdatu.web.rest.jaxrs.JaxRsSpi;
import org.apache.felix.dm.DependencyManager;
import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
import org.osgi.service.log.LogService;
import org.osgi.service.useradmin.UserAdmin;
@@ -47,6 +49,11 @@
@Override
public void initWithDependencies(BundleContext context, DependencyManager
manager) throws Exception {
+ // This REST service is tenant aware. This filter ensures that these
tenant aware services
+ // only adapt UserAdmin services which are also tenant aware.
+ String userAdminfilter = "(&(" + Tenant.SERVICE_PREFIX + "id=*)(" +
Constants.OBJECTCLASS
+ + "=" + UserAdmin.class.getName() + "))";
+
// Create and register the gadget management service.
String gadgetFilter = "(" + ColumnFamilyAvailable.FILTER_NAME + "=" +
GadgetStore.CF_GADGET + ")";
String keyspaceFilter =
@@ -54,11 +61,10 @@
+ CassandraPersistenceManager.DEFAULT_KEYSPACE + ")";
gadgetFilter = "(&" + gadgetFilter + keyspaceFilter + ")";
manager.add(
- createComponent()
+ createAdapterService(UserAdmin.class, userAdminfilter)
.setInterface(new String[]{GadgetManagement.class.getName(),
ResourceProvider.class.getName()}, null)
.setImplementation(GadgetManagementServiceImpl.class)
.add(createServiceDependency().setService(LogService.class).setRequired(true))
-
.add(createServiceDependency().setService(UserAdmin.class).setRequired(true))
.add(createServiceDependency().setService(HttpContextServiceFactory.class).setRequired(true))
.add(createServiceDependency().setService(ColumnFamilyAvailable.class,
gadgetFilter).setRequired(true))
.add(createServiceDependency().setService(GadgetStore.class).setRequired(true))
Modified: branches/amdatu-dispatcher/amdatu-opensocial/shindig/pom.xml
==============================================================================
--- branches/amdatu-dispatcher/amdatu-opensocial/shindig/pom.xml
(original)
+++ branches/amdatu-dispatcher/amdatu-opensocial/shindig/pom.xml Tue Feb
1 11:14:07 2011
@@ -139,6 +139,13 @@
<version>${org.apache.felix.http.version}</version>
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>org.amdatu.core</groupId>
+ <artifactId>tenant</artifactId>
+ <version>${platform.version}</version>
+ <scope>provided</scope>
+ <type>bundle</type>
+ </dependency>
</dependencies>
<build>
Modified:
branches/amdatu-dispatcher/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/osgi/Activator.java
==============================================================================
---
branches/amdatu-dispatcher/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/osgi/Activator.java
(original)
+++
branches/amdatu-dispatcher/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/osgi/Activator.java
Tue Feb 1 11:14:07 2011
@@ -16,9 +16,15 @@
*/
package org.amdatu.opensocial.shindig.osgi;
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import javax.servlet.Filter;
+
import org.amdatu.cassandra.listener.ColumnFamilyAvailable;
import org.amdatu.cassandra.listener.ColumnFamilyProvider;
import org.amdatu.cassandra.persistencemanager.CassandraPersistenceManager;
+import org.amdatu.core.tenant.TenantManagementService;
import org.amdatu.opensocial.shindig.GadgetStore;
import org.amdatu.opensocial.shindig.OAuthModule;
import org.amdatu.opensocial.shindig.ShindigService;
@@ -33,6 +39,7 @@
import org.amdatu.opensocial.shindig.service.GuiceInjectorService;
import org.amdatu.opensocial.shindig.service.GuiceInjectorServiceImpl;
import org.amdatu.opensocial.shindig.service.ShindigRegistrationServiceImpl;
+import org.amdatu.opensocial.shindig.service.TenantDispatchExtender;
import org.amdatu.web.httpcontext.HttpContextServiceFactory;
import org.amdatu.web.httpcontext.ResourceProvider;
import org.apache.felix.dm.DependencyActivatorBase;
@@ -43,7 +50,6 @@
import org.osgi.framework.Constants;
import org.osgi.service.http.HttpService;
import org.osgi.service.log.LogService;
-import org.osgi.service.useradmin.UserAdmin;
/**
* This is the OSGi activator for this Cassandra application bundle.
@@ -66,8 +72,7 @@
createComponent()
.setImplementation(CassandraAppDataServiceStore.class)
.setInterface(AppDataService.class.getName(), null)
-
.add(createServiceDependency().setService(LogService.class).setRequired(true))
-
.add(createServiceDependency().setService(UserAdmin.class).setRequired(true)));
+
.add(createServiceDependency().setService(LogService.class).setRequired(true)));
// Create and register the Gadget store
String gadgetKeyspaceFilter = "(" +
CassandraPersistenceManager.KEYSPACE_AWARE_KEY + "=" +
CassandraPersistenceManager.DEFAULT_KEYSPACE + ")";
@@ -138,6 +143,17 @@
.setRequired(true))
.add(createServiceDependency().setService(HttpContextServiceFactory.class).setRequired(true))
.add(createServiceDependency().setService(HttpService.class).setRequired(true)));
+
+ // Create the configuration module
+ Dictionary<String, Object> properties = new Hashtable<String,
Object>();
+ properties.put("pattern", ".*");
+ properties.put(Constants.SERVICE_RANKING, 0);
+ properties.put("contextId", RESOURCE_ID);
+ manager.add(
+ createComponent()
+ .setInterface(Filter.class.getName(), properties)
+ .setImplementation(TenantDispatchExtender.class)
+
.add(createServiceDependency().setService(TenantManagementService.class).setRequired(true)));
}
@Override
Modified:
branches/amdatu-dispatcher/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/persistence/CassandraAppDataServiceStore.java
==============================================================================
---
branches/amdatu-dispatcher/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/persistence/CassandraAppDataServiceStore.java
(original)
+++
branches/amdatu-dispatcher/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/persistence/CassandraAppDataServiceStore.java
Tue Feb 1 11:14:07 2011
@@ -26,6 +26,7 @@
import org.amdatu.libraries.utilities.ConversionUtil;
import org.amdatu.opensocial.shindig.OpenSocialConstants;
+import org.amdatu.opensocial.shindig.service.TenantDispatchExtender;
import org.apache.shindig.auth.SecurityToken;
import org.apache.shindig.common.util.ImmediateFuture;
import org.apache.shindig.protocol.DataCollection;
@@ -33,107 +34,121 @@
import org.apache.shindig.social.opensocial.spi.AppDataService;
import org.apache.shindig.social.opensocial.spi.GroupId;
import org.apache.shindig.social.opensocial.spi.UserId;
+import org.osgi.framework.InvalidSyntaxException;
import org.osgi.service.log.LogService;
import org.osgi.service.useradmin.Role;
import org.osgi.service.useradmin.User;
-import org.osgi.service.useradmin.UserAdmin;
import com.google.common.collect.Maps;
+
/**
* This class is responsible for storage of the OpenSocial content called
"AppData".
* @author ivol
*/
public class CassandraAppDataServiceStore implements AppDataService {
- // Services injected by the dependency manager
- private volatile UserAdmin m_userAdmin;
- private volatile LogService m_logService;
-
- public void start() {
- m_logService.log(LogService.LOG_INFO, getClass().getName() + " service
started");
- }
-
- @SuppressWarnings("unchecked")
- public Future<Void> updatePersonData(UserId userId, GroupId groupId,
String appId, Set<String> fields,
- Map<String, String> values, SecurityToken token) throws
ProtocolException {
- User user = getUser(userId, token);
- if (user != null) {
- // Convert the String Map to a byte array and write it to UserAdmin
- try {
- byte[] bytesValue = ConversionUtil.objectToByteArray(values);
- user.getProperties().put(OpenSocialConstants.APP_DATA,
bytesValue);
- return ImmediateFuture.newInstance(null);
- } catch (IOException e) {
- throw new
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
- "Could not update AppData for user with id '" +
userId.getUserId() + "'.", e);
- }
- }
-
- // If no user is known, ignore this update person data request
- return ImmediateFuture.newInstance(null);
- }
-
- @SuppressWarnings("unchecked")
- public Future<DataCollection> getPersonData(Set<UserId> userIds, GroupId
groupId, String appId, Set<String> fields,
- SecurityToken token) throws ProtocolException {
- Map<String, Map<String, String>> idToData = Maps.newHashMap();
- for (UserId userId : userIds) {
- User user = getUser(userId, token);
- if (user != null) {
- byte[] bytesValue = (byte[])
user.getProperties().get(OpenSocialConstants.APP_DATA);
- if (bytesValue != null) {
- try {
- Map<String, String> values = (Map<String, String>)
ConversionUtil.byteArrayToObject(bytesValue);
- idToData.put(userId.getUserId(token), values);
- } catch (ClassNotFoundException e) {
- throw new
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
- "Could not retrieve AppData for user with id '" +
userId.getUserId() + "'.", e);
- } catch (IOException e) {
- throw new
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
- "Could not retrieve AppData for user with id '" +
userId.getUserId() + "'.", e);
- }
- } else {
- idToData.put(userId.getUserId(token), new HashMap<String,
String>());
- }
- }
- }
- return ImmediateFuture.newInstance(new DataCollection(idToData));
- }
-
- @SuppressWarnings("unchecked")
- public Future<Void> deletePersonData(UserId userId, GroupId groupId,
String appId, Set<String> fields,
- SecurityToken token) throws ProtocolException {
- User user = getUser(userId, token);
- if (user != null) {
- // Convert the String Map to a byte array and write it to UserAdmin
- try {
- byte[] bytesValue = (byte[])
user.getProperties().get(OpenSocialConstants.APP_DATA);
- Map<String, String> values = (Map<String, String>)
ConversionUtil.byteArrayToObject(bytesValue);
- for (String field: fields) {
- values.remove(field);
- }
- user.getProperties().put(OpenSocialConstants.APP_DATA,
ConversionUtil.objectToByteArray(values));
- return ImmediateFuture.newInstance(null);
- } catch (IOException e) {
- throw new
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
- "Could not delete AppData for user with id '" +
userId.getUserId() + "'.", e);
- } catch (ClassNotFoundException e) {
- throw new
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
- "Could not delete AppData for user with id '" +
userId.getUserId() + "'.", e);
- }
- }
-
- throw new
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "User with id
'" + userId.getUserId()
- + "' not found");
- }
-
- private User getUser(UserId userId, SecurityToken token) {
- String id = userId.getUserId(token);
- if (!"".equals(userId.getUserId(token))) {
- Role user = m_userAdmin.getRole(id);
- if (user != null) {
- return (User) user;
- }
- }
- return null;
- }
+ // Services injected by the dependency manager
+
+ private volatile LogService m_logService;
+
+ public void start() {
+ m_logService.log(LogService.LOG_INFO, getClass().getName() + "
service started");
+ }
+
+ @SuppressWarnings("unchecked")
+ public Future<Void> updatePersonData(UserId userId, GroupId groupId,
String appId, Set<String> fields,
+ Map<String, String> values, SecurityToken token) throws
ProtocolException {
+ try {
+ User user = getUser(userId, token);
+ if (user != null) {
+ // Convert the String Map to a byte array and
write it to UserAdmin
+ try {
+ byte[] bytesValue =
ConversionUtil.objectToByteArray(values);
+
user.getProperties().put(OpenSocialConstants.APP_DATA, bytesValue);
+ return
ImmediateFuture.newInstance(null);
+ } catch (IOException e) {
+ throw new
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
+ "Could not update
AppData for user with id '" + userId.getUserId() + "'.", e);
+ }
+ }
+ } catch (InvalidSyntaxException e) {
+ throw new
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not
update person data", e);
+ }
+
+ // If no user is known, ignore this update person data request
+ return ImmediateFuture.newInstance(null);
+ }
+
+ @SuppressWarnings("unchecked")
+ public Future<DataCollection> getPersonData(Set<UserId> userIds,
GroupId groupId, String appId, Set<String> fields,
+ SecurityToken token) throws ProtocolException {
+ Map<String, Map<String, String>> idToData = Maps.newHashMap();
+ try {
+ for (UserId userId : userIds) {
+ User user = getUser(userId, token);
+ if (user != null) {
+ byte[] bytesValue = (byte[])
user.getProperties().get(OpenSocialConstants.APP_DATA);
+ if (bytesValue != null) {
+ try {
+ Map<String, String>
values = (Map<String, String>) ConversionUtil.byteArrayToObject(bytesValue);
+
idToData.put(userId.getUserId(token), values);
+ } catch (ClassNotFoundException
e) {
+ throw new
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
+ "Could
not retrieve AppData for user with id '" + userId.getUserId() + "'.", e);
+ } catch (IOException e) {
+ throw new
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
+ "Could
not retrieve AppData for user with id '" + userId.getUserId() + "'.", e);
+ }
+ } else {
+
idToData.put(userId.getUserId(token), new HashMap<String, String>());
+ }
+ }
+ }
+ } catch (InvalidSyntaxException e) {
+ throw new
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not get
person data", e);
+ }
+ return ImmediateFuture.newInstance(new
DataCollection(idToData));
+ }
+
+ @SuppressWarnings("unchecked")
+ public Future<Void> deletePersonData(UserId userId, GroupId groupId,
String appId, Set<String> fields,
+ SecurityToken token) throws ProtocolException {
+ try {
+ User user = getUser(userId, token);
+ if (user != null) {
+ // Convert the String Map to a byte array and
write it to UserAdmin
+ try {
+ byte[] bytesValue = (byte[])
user.getProperties().get(OpenSocialConstants.APP_DATA);
+ Map<String, String> values =
(Map<String, String>) ConversionUtil.byteArrayToObject(bytesValue);
+ for (String field: fields) {
+ values.remove(field);
+ }
+
user.getProperties().put(OpenSocialConstants.APP_DATA,
ConversionUtil.objectToByteArray(values));
+ return
ImmediateFuture.newInstance(null);
+ } catch (IOException e) {
+ throw new
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
+ "Could not delete
AppData for user with id '" + userId.getUserId() + "'.", e);
+ } catch (ClassNotFoundException e) {
+ throw new
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
+ "Could not delete
AppData for user with id '" + userId.getUserId() + "'.", e);
+ }
+ }
+ } catch (InvalidSyntaxException e) {
+ throw new
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "User with id
'" + userId.getUserId()
+ + "' not found", e);
+ }
+
+ throw new
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "User with id
'" + userId.getUserId()
+ + "' not found");
+ }
+
+ private User getUser(UserId userId, SecurityToken token) throws
InvalidSyntaxException {
+ String id = userId.getUserId(token);
+ if (!"".equals(userId.getUserId(token))) {
+ Role user =
TenantDispatchExtender.getUserAdmin().getRole(id);
+ if (user != null) {
+ return (User) user;
+ }
+ }
+ return null;
+ }
}
Added:
branches/amdatu-dispatcher/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/service/TenantDispatchExtender.java
==============================================================================
--- (empty file)
+++
branches/amdatu-dispatcher/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/service/TenantDispatchExtender.java
Tue Feb 1 11:14:07 2011
@@ -0,0 +1,72 @@
+package org.amdatu.opensocial.shindig.service;
+
+import java.io.IOException;
+import java.util.List;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+
+import org.amdatu.core.tenant.Tenant;
+import org.amdatu.core.tenant.TenantEntity;
+import org.amdatu.core.tenant.TenantException;
+import org.amdatu.core.tenant.TenantManagementService;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.useradmin.UserAdmin;
+
+public class TenantDispatchExtender implements Filter {
+ private volatile TenantManagementService m_tenantManagementService;
+ private static volatile BundleContext m_bundleContext;
+
+ // Tenant context information is bound to a ThreadLocal variable.
+ private static ThreadLocal<Tenant> m_properties = new
ThreadLocal<Tenant>();
+
+ public void destroy() {
+ }
+
+ public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
+ throws IOException, ServletException {
+
+ HttpServletRequest httpServletRequest = (HttpServletRequest) request;
+ String serverName = httpServletRequest.getServerName();
+ if (serverName != null) {
+ try {
+ List<TenantEntity> tenants =
m_tenantManagementService.getTenants();
+ for (TenantEntity tenant : tenants) {
+ if
(serverName.equals(tenant.getProperties().get("hostname"))) {
+ m_properties.set(tenant);
+ }
+ }
+ }
+ catch (TenantException e) {}
+ }
+
+ chain.doFilter(request, response);
+
+ m_properties.set(null);
+ }
+
+ public void init(FilterConfig filterConfig) throws ServletException {
+ }
+
+ public static Tenant getTenant() {
+ return m_properties.get();
+ }
+
+ public static UserAdmin getUserAdmin() throws InvalidSyntaxException {
+ Tenant tenant = getTenant();
+ ServiceReference[] refs =
m_bundleContext.getAllServiceReferences(UserAdmin.class.getName(), null);
+ for (ServiceReference ref : refs) {
+ if
(tenant.getId().equals(ref.getProperty("tenant_id"))) {
+ return (UserAdmin)
m_bundleContext.getService(ref);
+ }
+ }
+ return null;
+ }
+}