Author: [email protected]
Date: Tue Jan 31 16:11:07 2012
New Revision: 2042
Log:
AMDATU-532: don't propagate any tenant-properties, apart from the tenant.pid.
If a tenant-aware service wants to get any of those properties, it should
retrieve the tenant-service instead.
Modified:
trunk/amdatu-core/tenant-adapter/src/main/java/org/amdatu/tenant/adapter/MultiTenantBundleActivator.java
trunk/amdatu-core/tenant-adapter/src/main/java/org/amdatu/tenant/adapter/TenantAdapter.java
trunk/amdatu-core/tenant-adapter/src/main/java/org/amdatu/tenant/adapter/TenantAwareBundleContext.java
Modified:
trunk/amdatu-core/tenant-adapter/src/main/java/org/amdatu/tenant/adapter/MultiTenantBundleActivator.java
==============================================================================
---
trunk/amdatu-core/tenant-adapter/src/main/java/org/amdatu/tenant/adapter/MultiTenantBundleActivator.java
(original)
+++
trunk/amdatu-core/tenant-adapter/src/main/java/org/amdatu/tenant/adapter/MultiTenantBundleActivator.java
Tue Jan 31 16:11:07 2012
@@ -17,18 +17,16 @@
import static org.amdatu.tenant.TenantConstants.PID_KEY;
import static org.amdatu.tenant.TenantConstants.PID_VALUE_PLATFORM;
-import static org.amdatu.tenant.adapter.TenantAdapterConstants.*;
-
-import java.util.Properties;
+import static
org.amdatu.tenant.adapter.TenantAdapterConstants.MULTITENANT_SCOPE_KEY;
+import static
org.amdatu.tenant.adapter.TenantAdapterConstants.MULTITENANT_SCOPE_VALUE_BOTH;
+import static
org.amdatu.tenant.adapter.TenantAdapterConstants.MULTITENANT_SCOPE_VALUE_PLATFORM;
+import static
org.amdatu.tenant.adapter.TenantAdapterConstants.MULTITENANT_SCOPE_VALUE_TENANTS;
import org.amdatu.tenant.Tenant;
import org.amdatu.tenant.TenantConstants;
import org.apache.felix.dm.DependencyActivatorBase;
import org.apache.felix.dm.DependencyManager;
-import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
-import org.osgi.framework.Constants;
-import org.osgi.framework.ServiceReference;
import org.osgi.service.log.LogService;
/**
@@ -41,45 +39,36 @@
*/
public class MultiTenantBundleActivator extends DependencyActivatorBase {
- private BundleContext m_context;
- private String m_bundleActivatorClass;
- private BundleActivator m_tenantBundleActivator;
- private TenantAwareBundleContext m_tenantAwareBundleContext;
-
+ /**
+ * @see
org.apache.felix.dm.DependencyActivatorBase#init(org.osgi.framework.BundleContext,
org.apache.felix.dm.DependencyManager)
+ */
@Override
public void init(BundleContext context, DependencyManager manager) throws
Exception {
- m_context = context;
-
- String scope = determineScope();
+ String scope = determineScope(context);
if (createPlatformInstance(scope)) {
- log(LogService.LOG_DEBUG, "[" + PID_VALUE_PLATFORM + "] Starting "
- + getBundleHeader(Constants.BUNDLE_NAME), null);
-
- m_bundleActivatorClass =
getBundleHeader(MULTITENANT_BUNDLE_ACTIVATOR_KEY);
- if (m_bundleActivatorClass == null) {
- log(LogService.LOG_ERROR, "Missing manifest header " +
MULTITENANT_BUNDLE_ACTIVATOR_KEY, null);
- return;
- }
-
- try {
- m_tenantBundleActivator =
- (BundleActivator)
context.getBundle().loadClass(m_bundleActivatorClass).newInstance();
-
- Properties props = new Properties();
- props.put(PID_KEY, PID_VALUE_PLATFORM);
-
- m_tenantAwareBundleContext =
- new TenantAwareBundleContext(context, PID_VALUE_PLATFORM,
props, getPlatformTenantFilter());
-
- m_tenantBundleActivator.start(m_tenantAwareBundleContext);
- }
- catch (Exception e) {
- log(LogService.LOG_ERROR, "Could not start activator for
platform scope", e);
- }
+ // For the _PLATFORM instance, we need to provide a custom filter
& PID...
+ manager.add(createComponent()
+ .setImplementation(new TenantAdapter() {
+ @Override
+ protected String getTenantFilter() {
+ return getPlatformTenantFilter();
+ }
+
+ @Override
+ protected String getTenantID() {
+ return PID_VALUE_PLATFORM;
+ }
+ })
+ .add(createServiceDependency()
+ .setService(LogService.class)
+ .setRequired(false)
+ )
+ );
}
- if (createTenantAdapter(scope)) {
+ if (createTenantInstances(scope)) {
+ // For the individual tenants, we create an adapter that gets
called for each individually registered tenant...
manager.add(createAdapterService(Tenant.class, null)
.setImplementation(TenantAdapter.class)
.add(createServiceDependency()
@@ -90,62 +79,54 @@
}
}
+ /**
+ * @see
org.apache.felix.dm.DependencyActivatorBase#destroy(org.osgi.framework.BundleContext,
org.apache.felix.dm.DependencyManager)
+ */
@Override
public void destroy(BundleContext context, DependencyManager manager)
throws Exception {
}
/**
- * @return
+ * Returns the filter condition used to scope the platform-tenant.
+ *
+ * @return a filter condition, never <code>null</code>.
*/
- private String getPlatformTenantFilter() {
+ final String getPlatformTenantFilter() {
return String.format("(|(%1$s=%2$s)(!(%1$s=*)))", PID_KEY,
PID_VALUE_PLATFORM);
}
/**
- * @param scope
- * @return
+ * Returns whether or not a platform instance should be created for this
tenant-aware service.
+ *
+ * @param scope the scope, may be <code>null</code>.
+ * @return <code>true</code> if a platform-tenant should be created,
<code>false</code> otherwise.
*/
private boolean createPlatformInstance(String scope) {
return !MULTITENANT_SCOPE_VALUE_TENANTS.equals(scope);
}
/**
- * @param scope
- * @return
+ * Returns whether or not a normal tenant instances should be created for
this tenant-aware service.
+ *
+ * @param scope the scope, may be <code>null</code>.
+ * @return <code>true</code> if a individual tenants should be created,
<code>false</code> otherwise.
*/
- private boolean createTenantAdapter(String scope) {
+ private boolean createTenantInstances(String scope) {
return !MULTITENANT_SCOPE_VALUE_PLATFORM.equals(scope);
}
/**
- * @return
+ * Determines the scope in which tenants should be created.
+ *
+ * @return a scope, never <code>null</code>.
*/
- private String determineScope() {
- String scope = getBundleHeader(MULTITENANT_SCOPE_KEY);
+ private String determineScope(BundleContext context) {
+ String scope = (String)
context.getBundle().getHeaders().get(MULTITENANT_SCOPE_KEY);
if (scope == null
- || (createTenantAdapter(scope) &&
!MULTITENANT_SCOPE_VALUE_BOTH.equals(scope))) {
+ || (!MULTITENANT_SCOPE_VALUE_PLATFORM.equals(scope) &&
!MULTITENANT_SCOPE_VALUE_BOTH.equals(scope))) {
// default to tenants
scope = MULTITENANT_SCOPE_VALUE_TENANTS;
}
return scope;
}
-
- /**
- * @param m_context
- * @return
- */
- private String getBundleHeader(String key) {
- return (String) m_context.getBundle().getHeaders().get(key);
- }
-
- private void log(int level, String message, Throwable exception) {
- final ServiceReference ref =
m_context.getServiceReference(LogService.class.getName());
- if (ref != null) {
- final LogService svc = (LogService) m_context.getService(ref);
- if (svc != null) {
- svc.log(level, message, exception);
- m_context.ungetService(ref);
- }
- }
- }
}
Modified:
trunk/amdatu-core/tenant-adapter/src/main/java/org/amdatu/tenant/adapter/TenantAdapter.java
==============================================================================
---
trunk/amdatu-core/tenant-adapter/src/main/java/org/amdatu/tenant/adapter/TenantAdapter.java
(original)
+++
trunk/amdatu-core/tenant-adapter/src/main/java/org/amdatu/tenant/adapter/TenantAdapter.java
Tue Jan 31 16:11:07 2012
@@ -15,12 +15,9 @@
*/
package org.amdatu.tenant.adapter;
-import static org.amdatu.tenant.TenantConstants.NAME_KEY;
import static org.amdatu.tenant.TenantConstants.PID_KEY;
import static
org.amdatu.tenant.adapter.TenantAdapterConstants.MULTITENANT_BUNDLE_ACTIVATOR_KEY;
-import java.util.Properties;
-
import org.amdatu.tenant.Tenant;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
@@ -47,8 +44,7 @@
* Called by Felix dependency manager when this service adapter is started.
*/
public void start() {
- m_log
- .log(LogService.LOG_DEBUG, "[" + m_tenant.getId() + "] Starting "
+ getBundleHeader(Constants.BUNDLE_NAME));
+ m_log.log(LogService.LOG_DEBUG, "[" + getTenantID() + "] Starting " +
getBundleHeader(Constants.BUNDLE_NAME));
m_bundleActivatorClass =
getBundleHeader(MULTITENANT_BUNDLE_ACTIVATOR_KEY);
if (m_bundleActivatorClass == null) {
@@ -60,17 +56,13 @@
m_tenantBundleActivator =
(BundleActivator)
m_context.getBundle().loadClass(m_bundleActivatorClass).newInstance();
- Properties props = new Properties();
- props.put(PID_KEY, m_tenant.getId());
- props.put(NAME_KEY, m_tenant.getName());
-
m_tenantAwareBundleContext =
- new TenantAwareBundleContext(m_context, m_tenant.getId(),
props, getTenantFilter());
+ new TenantAwareBundleContext(m_context, getTenantID(),
getTenantFilter());
m_tenantBundleActivator.start(m_tenantAwareBundleContext);
}
catch (Exception e) {
- m_log.log(LogService.LOG_ERROR, "Could not start activator for
tenant " + m_tenant.getId(), e);
+ m_log.log(LogService.LOG_ERROR, "Could not start activator for
tenant " + getTenantID(), e);
}
}
@@ -79,7 +71,7 @@
*/
public void stop() {
m_log
- .log(LogService.LOG_DEBUG, "[" + m_tenant.getId() + "] Stopping "
+ getBundleHeader(Constants.BUNDLE_NAME));
+ .log(LogService.LOG_DEBUG, "[" + getTenantID() + "] Stopping " +
getBundleHeader(Constants.BUNDLE_NAME));
try {
if (m_tenantBundleActivator != null) {
@@ -87,7 +79,7 @@
}
}
catch (Exception e) {
- m_log.log(LogService.LOG_ERROR, "Could not stop activator for
tenant " + m_tenant.getId(), e);
+ m_log.log(LogService.LOG_ERROR, "Could not stop activator for
tenant " + getTenantID(), e);
}
finally {
m_tenantAwareBundleContext.unregisterServices();
@@ -97,11 +89,17 @@
/**
* @return
*/
- private String getTenantFilter() {
+ protected String getTenantFilter() {
return "(|(objectClass=org.amdatu.web.rest.jaxrs.JaxRsSpi)"
+ "(objectClass=org.osgi.service.log.LogService)("
- + PID_KEY + "="
- + m_tenant.getId() + "))";
+ + PID_KEY + "=" + getTenantID() + "))";
+ }
+
+ /**
+ * @return
+ */
+ protected String getTenantID() {
+ return m_tenant.getId();
}
/**
@@ -111,5 +109,4 @@
private String getBundleHeader(String key) {
return (String) m_context.getBundle().getHeaders().get(key);
}
-
}
Modified:
trunk/amdatu-core/tenant-adapter/src/main/java/org/amdatu/tenant/adapter/TenantAwareBundleContext.java
==============================================================================
---
trunk/amdatu-core/tenant-adapter/src/main/java/org/amdatu/tenant/adapter/TenantAwareBundleContext.java
(original)
+++
trunk/amdatu-core/tenant-adapter/src/main/java/org/amdatu/tenant/adapter/TenantAwareBundleContext.java
Tue Jan 31 16:11:07 2012
@@ -21,10 +21,10 @@
import java.net.URL;
import java.util.Arrays;
import java.util.Dictionary;
-import java.util.Enumeration;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
+import org.amdatu.tenant.TenantConstants;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
@@ -56,10 +56,9 @@
private final BundleContext m_bundleContext;
private final String m_identifier;
- private final Properties m_properties;
private final String m_filter;
- public TenantAwareBundleContext(BundleContext bundleContext, String
identifier, Properties properties, String filter) {
+ public TenantAwareBundleContext(BundleContext bundleContext, String
identifier, String filter) {
assert bundleContext != null;
m_bundleContext = bundleContext;
@@ -68,11 +67,6 @@
assert "".equals(identifier);
m_identifier = identifier;
- if (properties != null)
- m_properties = properties;
- else
- m_properties = new Properties();
-
if (filter != null)
m_filter = filter;
else
@@ -148,7 +142,7 @@
if (bundleContext != null) {
// FIXME Do we even want to (always) provide access to foreign
BundleContext?
TenantAwareBundleContext scopedBundleContext =
- new TenantAwareBundleContext(bundleContext, m_identifier,
m_properties, m_filter);
+ new TenantAwareBundleContext(bundleContext, m_identifier,
m_filter);
return scopedBundleContext.getBundle();
}
return new TenantAwareBundle(bundle);
@@ -166,7 +160,7 @@
if (bundleContext != null) {
// FIXME Do we even want to (always) provide access to foreign
BundleContext?
TenantAwareBundleContext scopedBundleContext =
- new TenantAwareBundleContext(bundleContext, m_identifier,
m_properties, m_filter);
+ new TenantAwareBundleContext(bundleContext, m_identifier,
m_filter);
bundles[i] = scopedBundleContext.getBundle();
}
else {
@@ -242,10 +236,9 @@
* @see
org.osgi.framework.BundleContext#registerService(java.lang.String[],
java.lang.Object, java.util.Dictionary)
*/
public ServiceRegistration registerService(String[] clazzes, Object
service, Dictionary properties) {
- properties = getScopedProperties(properties);
- ServiceRegistration registration =
m_bundleContext.registerService(clazzes, service, properties);
+ ServiceRegistration registration =
m_bundleContext.registerService(clazzes, service,
getScopedProperties(properties));
ServiceRegistrationAdapter adapter = new
ServiceRegistrationAdapter(registration);
- m_serviceRegistrations.put(adapter, registration);
+ m_serviceRegistrations.putIfAbsent(adapter, registration);
return adapter;
}
@@ -253,10 +246,9 @@
* @see org.osgi.framework.BundleContext#registerService(java.lang.String,
java.lang.Object, java.util.Dictionary)
*/
public ServiceRegistration registerService(String clazz, Object service,
Dictionary properties) {
- properties = getScopedProperties(properties);
- ServiceRegistration registration =
m_bundleContext.registerService(clazz, service, properties);
+ ServiceRegistration registration =
m_bundleContext.registerService(clazz, service,
getScopedProperties(properties));
ServiceRegistrationAdapter adapter = new
ServiceRegistrationAdapter(registration);
- m_serviceRegistrations.put(adapter, registration);
+ m_serviceRegistrations.putIfAbsent(adapter, registration);
return adapter;
}
@@ -338,14 +330,19 @@
return "(&" + filter + m_filter + ")";
}
+ /**
+ * Adapts (or creates) the given dictionary in such way that it always
contains the tenant.pid property.
+ *
+ * @param properties the original dictionary, may be null, in which case a
new dictionary will be created.
+ * @return the scoped dictionary, never <code>null</code>.
+ */
protected Dictionary getScopedProperties(Dictionary properties) {
- if (properties == null)
+ if (properties == null) {
properties = new Properties();
- Enumeration<Object> keys = m_properties.keys();
- while (keys.hasMoreElements()) {
- Object key = keys.nextElement();
- properties.put(key, m_properties.get(key));
}
+ // Make sure the identifier of this tenant is registered with *all*
+ // services the tenant-aware service registers itself...
+ properties.put(TenantConstants.PID_KEY, m_identifier);
return properties;
}
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits