Author: [email protected]
Date: Fri Dec 30 14:46:47 2011
New Revision: 1878

Log:
AMDATU-468 some Code cleanup / some doc / some refactorring

Modified:
   
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/MultiTenantBundleActivator.java
   
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/adaptor/TenantAdapter.java
   
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/adaptor/TenantAwareBundle.java
   
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/adaptor/TenantAwareBundleContext.java
   
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/osgi/Activator.java

Modified: 
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/MultiTenantBundleActivator.java
==============================================================================
--- 
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/MultiTenantBundleActivator.java
       (original)
+++ 
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/MultiTenantBundleActivator.java
       Fri Dec 30 14:46:47 2011
@@ -15,6 +15,8 @@
  */
 package org.amdatu.core.tenant;
 
+import java.util.Properties;
+
 import org.amdatu.core.tenant.adaptor.TenantAdapter;
 import org.amdatu.core.tenant.adaptor.TenantAwareBundleContext;
 import org.apache.felix.dm.DependencyActivatorBase;
@@ -66,8 +68,16 @@
                 }
                 m_tenantBundleActivator =
                     (BundleActivator) 
context.getBundle().loadClass(m_bundleActivatorClass).newInstance();
+
+                Properties props = new Properties();
+                props.put(Tenant.TENANT_ID_SERVICEPROPERTY, 
TenantConstants.MULTITENANT_SCOPE_PLATFORM_TENANTID);
+
                 m_tenantAwareBundleContext =
-                    new TenantAwareBundleContext(context, 
TenantConstants.MULTITENANT_SCOPE_PLATFORM_TENANTID);
+                    new TenantAwareBundleContext(context, 
TenantConstants.MULTITENANT_SCOPE_PLATFORM_TENANTID, props,
+                        "(|("
+                            + Tenant.TENANT_ID_SERVICEPROPERTY + "="
+                            + 
TenantConstants.MULTITENANT_SCOPE_PLATFORM_TENANTID + ")(!("
+                            + Tenant.TENANT_ID_SERVICEPROPERTY + "=*)))");
                 m_tenantBundleActivator.start(m_tenantAwareBundleContext);
             }
             catch (Exception e) {

Modified: 
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/adaptor/TenantAdapter.java
==============================================================================
--- 
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/adaptor/TenantAdapter.java
    (original)
+++ 
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/adaptor/TenantAdapter.java
    Fri Dec 30 14:46:47 2011
@@ -15,6 +15,8 @@
  */
 package org.amdatu.core.tenant.adaptor;
 
+import java.util.Properties;
+
 import org.amdatu.core.tenant.Tenant;
 import org.amdatu.core.tenant.TenantConstants;
 import org.osgi.framework.BundleActivator;
@@ -29,7 +31,7 @@
  * @author <a href="mailto:[email protected]";>Amdatu Project 
Team</a>
  */
 public class TenantAdapter {
-    
+
     private volatile BundleContext m_context;
     private volatile Tenant m_tenant;
     private volatile LogService m_log;
@@ -50,7 +52,17 @@
             }
             m_tenantBundleActivator =
                 (BundleActivator) 
m_context.getBundle().loadClass(m_bundleActivatorClass).newInstance();
-            m_tenantAwareBundleContext = new 
TenantAwareBundleContext(m_context, m_tenant.getId());
+
+            Properties props = new Properties();
+            props.put(Tenant.TENANT_ID_SERVICEPROPERTY, m_tenant.getId());
+
+            m_tenantAwareBundleContext =
+                new TenantAwareBundleContext(m_context, m_tenant.getId(), 
props,
+                    "(|(objectClass=org.amdatu.web.rest.jaxrs.JaxRsSpi)"
+                        + "(objectClass=org.osgi.service.log.LogService)("
+                        + Tenant.TENANT_ID_SERVICEPROPERTY + "="
+                        + m_tenant.getId() + "))");
+            
             m_tenantBundleActivator.start(m_tenantAwareBundleContext);
         }
         catch (Exception e) {

Modified: 
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/adaptor/TenantAwareBundle.java
==============================================================================
--- 
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/adaptor/TenantAwareBundle.java
        (original)
+++ 
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/adaptor/TenantAwareBundle.java
        Fri Dec 30 14:46:47 2011
@@ -34,119 +34,214 @@
  * @author <a href="mailto:[email protected]";>Amdatu Project 
Team</a>
  */
 public class TenantAwareBundle implements Bundle {
-       private final Bundle m_bundle;
-       private final String m_tenantId;
-       
-       public TenantAwareBundle(Bundle bundle, String tenantId) {
-               m_bundle = bundle;
-               m_tenantId = tenantId;
-       }
-
-       public int getState() {
-               return m_bundle.getState();
-       }
-
-       public void start(int options) throws BundleException {
-               m_bundle.start(options);
-       }
-
-       public void start() throws BundleException {
-               m_bundle.start();
-       }
-
-       public void stop(int options) throws BundleException {
-               m_bundle.stop(options);
-       }
-
-       public void stop() throws BundleException {
-               m_bundle.stop();
-       }
-
-       public void update(InputStream input) throws BundleException {
-               m_bundle.update(input);
-       }
-
-       public void update() throws BundleException {
-               m_bundle.update();
-       }
-
-       public void uninstall() throws BundleException {
-               m_bundle.uninstall();
-       }
-
-       public Dictionary getHeaders() {
-               return m_bundle.getHeaders();
-       }
-
-       public long getBundleId() {
-               return m_bundle.getBundleId();
-       }
-
-       public String getLocation() {
-               return m_bundle.getLocation();
-       }
-
-       public ServiceReference[] getRegisteredServices() {
-               return m_bundle.getRegisteredServices();
-       }
-
-       public ServiceReference[] getServicesInUse() {
-               return m_bundle.getServicesInUse();
-       }
-
-       public boolean hasPermission(Object permission) {
-               return m_bundle.hasPermission(permission);
-       }
-
-       public URL getResource(String name) {
-               return m_bundle.getResource(name);
-       }
-
-       public Dictionary getHeaders(String locale) {
-               return m_bundle.getHeaders(locale);
-       }
-
-       public String getSymbolicName() {
-               return m_bundle.getSymbolicName();
-       }
-
-       public Class loadClass(String name) throws ClassNotFoundException {
-               return m_bundle.loadClass(name);
-       }
-
-       public Enumeration getResources(String name) throws IOException {
-               return m_bundle.getResources(name);
-       }
-
-       public Enumeration getEntryPaths(String path) {
-               return m_bundle.getEntryPaths(path);
-       }
-
-       public URL getEntry(String path) {
-               return m_bundle.getEntry(path);
-       }
-
-       public long getLastModified() {
-               return m_bundle.getLastModified();
-       }
-
-       public Enumeration findEntries(String path, String filePattern, boolean 
recurse) {
-               return m_bundle.findEntries(path, filePattern, recurse);
-       }
-
-       public BundleContext getBundleContext() {
-               BundleContext bundleContext = m_bundle.getBundleContext();
-               if (bundleContext != null) {
-                       return new TenantAwareBundleContext(bundleContext, 
m_tenantId);
-               }
-               return null;
-       }
-
-       public Map getSignerCertificates(int signersType) {
-               return m_bundle.getSignerCertificates(signersType);
-       }
-
-       public Version getVersion() {
-               return m_bundle.getVersion();
-       }
+
+    private final TenantAwareBundleContext m_scopedBundleContext;
+    private final Bundle m_parentBundle;
+
+    public TenantAwareBundle(TenantAwareBundleContext context, Bundle 
parentBundle) {
+        m_scopedBundleContext = context;
+        m_parentBundle = parentBundle;
+    }
+
+    public TenantAwareBundle(TenantAwareBundleContext context) {
+        this(context, context.getParent().getBundle());
+    }
+
+    public TenantAwareBundle(Bundle bundle) {
+        this(null, bundle);
+    }
+
+    /*
+     * Public access
+     */
+
+    public Bundle getParent() {
+        return m_parentBundle;
+    }
+
+    /*
+     * Bundle interface
+     */
+
+    /**
+     * @see org.osgi.framework.Bundle#getState()
+     */
+    public int getState() {
+        return getParent().getState();
+    }
+
+    /**
+     * @see org.osgi.framework.Bundle#start(int)
+     */
+    public void start(int options) throws BundleException {
+        getParent().start(options);
+    }
+
+    /**
+     * @see org.osgi.framework.Bundle#start()
+     */
+    public void start() throws BundleException {
+        getParent().start();
+    }
+
+    /**
+     * @see org.osgi.framework.Bundle#stop(int)
+     */
+    public void stop(int options) throws BundleException {
+        getParent().stop(options);
+    }
+
+    /**
+     * @see org.osgi.framework.Bundle#stop()
+     */
+    public void stop() throws BundleException {
+        getParent().stop();
+    }
+
+    /**
+     * @see org.osgi.framework.Bundle#update(java.io.InputStream)
+     */
+    public void update(InputStream input) throws BundleException {
+        getParent().update(input);
+    }
+
+    /**
+     * @see org.osgi.framework.Bundle#update()
+     */
+    public void update() throws BundleException {
+        getParent().update();
+    }
+
+    /**
+     * @see org.osgi.framework.Bundle#uninstall()
+     */
+    public void uninstall() throws BundleException {
+        getParent().uninstall();
+    }
+
+    /**
+     * @see org.osgi.framework.Bundle#getHeaders()
+     */
+    public Dictionary getHeaders() {
+        return getParent().getHeaders();
+    }
+
+    /**
+     * @see org.osgi.framework.Bundle#getBundleId()
+     */
+    public long getBundleId() {
+        return getParent().getBundleId();
+    }
+
+    /**
+     * @see org.osgi.framework.Bundle#getLocation()
+     */
+    public String getLocation() {
+        return getParent().getLocation();
+    }
+
+    /**
+     * @see org.osgi.framework.Bundle#getRegisteredServices()
+     */
+    public ServiceReference[] getRegisteredServices() {
+        return getParent().getRegisteredServices();
+    }
+
+    /**
+     * @see org.osgi.framework.Bundle#getServicesInUse()
+     */
+    public ServiceReference[] getServicesInUse() {
+        return getParent().getServicesInUse();
+    }
+
+    /**
+     * @see org.osgi.framework.Bundle#hasPermission(java.lang.Object)
+     */
+    public boolean hasPermission(Object permission) {
+        return getParent().hasPermission(permission);
+    }
+
+    /**
+     * @see org.osgi.framework.Bundle#getResource(java.lang.String)
+     */
+    public URL getResource(String name) {
+        return getParent().getResource(name);
+    }
+
+    /**
+     * @see org.osgi.framework.Bundle#getHeaders(java.lang.String)
+     */
+    public Dictionary getHeaders(String locale) {
+        return getParent().getHeaders(locale);
+    }
+
+    /**
+     * @see org.osgi.framework.Bundle#getSymbolicName()
+     */
+    public String getSymbolicName() {
+        return getParent().getSymbolicName();
+    }
+
+    /**
+     * @see org.osgi.framework.Bundle#loadClass(java.lang.String)
+     */
+    public Class loadClass(String name) throws ClassNotFoundException {
+        return getParent().loadClass(name);
+    }
+
+    /**
+     * @see org.osgi.framework.Bundle#getResources(java.lang.String)
+     */
+    public Enumeration getResources(String name) throws IOException {
+        return getParent().getResources(name);
+    }
+
+    /**
+     * @see org.osgi.framework.Bundle#getEntryPaths(java.lang.String)
+     */
+    public Enumeration getEntryPaths(String path) {
+        return getParent().getEntryPaths(path);
+    }
+
+    /**
+     * @see org.osgi.framework.Bundle#getEntry(java.lang.String)
+     */
+    public URL getEntry(String path) {
+        return getParent().getEntry(path);
+    }
+
+    /**
+     * @see org.osgi.framework.Bundle#getLastModified()
+     */
+    public long getLastModified() {
+        return getParent().getLastModified();
+    }
+
+    /**
+     * @see org.osgi.framework.Bundle#findEntries(java.lang.String, 
java.lang.String, boolean)
+     */
+    public Enumeration findEntries(String path, String filePattern, boolean 
recurse) {
+        return getParent().findEntries(path, filePattern, recurse);
+    }
+
+    /**
+     * @see org.osgi.framework.Bundle#getBundleContext()
+     */
+    public BundleContext getBundleContext() {
+        return m_scopedBundleContext;
+    }
+
+    /**
+     * @see org.osgi.framework.Bundle#getSignerCertificates(int)
+     */
+    public Map getSignerCertificates(int signersType) {
+        return getParent().getSignerCertificates(signersType);
+    }
+
+    /**
+     * @see org.osgi.framework.Bundle#getVersion()
+     */
+    public Version getVersion() {
+        return getParent().getVersion();
+    }
 }

Modified: 
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/adaptor/TenantAwareBundleContext.java
==============================================================================
--- 
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/adaptor/TenantAwareBundleContext.java
 (original)
+++ 
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/adaptor/TenantAwareBundleContext.java
 Fri Dec 30 14:46:47 2011
@@ -21,11 +21,11 @@
 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.core.tenant.MultiTenantBundleInputStream;
-import org.amdatu.core.tenant.Tenant;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
@@ -45,24 +45,51 @@
  * @author <a href="mailto:[email protected]";>Amdatu Project 
Team</a>
  */
 public class TenantAwareBundleContext implements BundleContext {
-    private static final String TENANT_DATAFILE_PREFIX = "tenant-";
-    private final BundleContext m_bundleContext;
-    private final Bundle m_bundle;
-    private final String m_tenantId;
-    private File m_dataFile;
+
     private final ConcurrentHashMap<BundleListener, TenantAwareBundleListener> 
m_bundleListeners =
         new ConcurrentHashMap<BundleListener, TenantAwareBundleListener>();
+
     private final ConcurrentHashMap<FrameworkListener, 
TenantAwareFrameworkListener> m_frameworkListeners =
         new ConcurrentHashMap<FrameworkListener, 
TenantAwareFrameworkListener>();
+
     private final ConcurrentHashMap<ServiceRegistrationAdapter, 
ServiceRegistration> m_serviceRegistrations =
         new ConcurrentHashMap<ServiceRegistrationAdapter, 
ServiceRegistration>();
 
-    public TenantAwareBundleContext(BundleContext bc, String tenantId) {
-        m_bundleContext = bc;
-        m_tenantId = tenantId;
-        m_bundle = new TenantAwareBundle(bc.getBundle(), m_tenantId);
+    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) {
+
+        assert bundleContext != null;
+        m_bundleContext = bundleContext;
+
+        assert identifier != null;
+        assert "".equals(identifier);
+        m_identifier = identifier;
+
+        if (properties != null)
+            m_properties = properties;
+        else
+            m_properties = new Properties();
+
+        if (filter != null)
+            m_filter = filter;
+        else
+            m_filter = "";
     }
 
+    /*
+     * Public access
+     */
+
+    // FIXME protect?
+    public BundleContext getParent() {
+        return m_bundleContext;
+    }
+
+    // FIXME why do we need this?
     public void unregisterServices() {
         for (ServiceRegistration registration : 
m_serviceRegistrations.values()) {
             registration.unregister();
@@ -70,222 +97,269 @@
         m_serviceRegistrations.clear();
     }
 
+    /*
+     * BundleContext interface
+     */
+
+    /**
+     * @see org.osgi.framework.BundleContext#getProperty(java.lang.String)
+     */
     public String getProperty(String key) {
         return m_bundleContext.getProperty(key);
     }
 
+    /**
+     * @see org.osgi.framework.BundleContext#getBundle()
+     */
     public Bundle getBundle() {
-        return m_bundle;
+        return new TenantAwareBundle(this);
     }
 
-    public Bundle installBundle(String location, InputStream input)
-        throws BundleException {
-        return m_bundleContext.installBundle(location,
-            new MultiTenantBundleInputStream(input));
+    /**
+     * @see org.osgi.framework.BundleContext#installBundle(java.lang.String, 
java.io.InputStream)
+     */
+    public Bundle installBundle(String location, InputStream input) throws 
BundleException {
+        // FIXME This does not do what we want... do we even want to allow it?
+        return m_bundleContext.installBundle(location, new 
MultiTenantBundleInputStream(input));
     }
 
+    /**
+     * @see org.osgi.framework.BundleContext#installBundle(java.lang.String)
+     */
     public Bundle installBundle(String location) throws BundleException {
         try {
+            // FIXME This does not do what we want... do we even want to allow 
it?
             return m_bundleContext.installBundle(location,
-                new MultiTenantBundleInputStream(
-                    bundleLocationToInputStream(location)));
+                new MultiTenantBundleInputStream((new 
URL(location)).openStream()));
         }
         catch (IOException e) {
             throw new BundleException(
-                "Could not convert bundle location to an input stream, 
wrapping it failed.",
-                e);
+                "Could not convert bundle location to an input stream, 
wrapping it failed.", e);
         }
     }
 
-    private InputStream bundleLocationToInputStream(String location)
-        throws IOException {
-        return (new URL(location)).openStream();
-    }
-
+    /**
+     * @see org.osgi.framework.BundleContext#getBundle(long)
+     */
     public Bundle getBundle(long id) {
         Bundle bundle = m_bundleContext.getBundle(id);
-        return new TenantAwareBundle(bundle, m_tenantId);
+        if (bundle == null)
+            return null;
+        BundleContext bundleContext = bundle.getBundleContext();
+        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);
+            return scopedBundleContext.getBundle();
+        }
+        return new TenantAwareBundle(bundle);
     }
 
+    /**
+     * @see org.osgi.framework.BundleContext#getBundles()
+     */
     public Bundle[] getBundles() {
         Bundle[] bundles = m_bundleContext.getBundles();
+        if (bundles == null)
+            return new Bundle[] {};
         for (int i = 0; i < bundles.length; i++) {
-            Bundle bundle = bundles[i];
-            bundles[i] = new TenantAwareBundle(bundle, m_tenantId);
+            BundleContext bundleContext = bundles[i].getBundleContext();
+            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);
+                bundles[i] = scopedBundleContext.getBundle();
+            }
+            else {
+                bundles[i] = new TenantAwareBundle(bundles[i]);
+            }
         }
         return bundles;
     }
 
-    public void addServiceListener(ServiceListener listener, String filter)
-        throws InvalidSyntaxException {
-        m_bundleContext.addServiceListener(listener, 
getCompleteFilter(filter));
+    /**
+     * @see 
org.osgi.framework.BundleContext#addServiceListener(org.osgi.framework.ServiceListener,
 java.lang.String)
+     */
+    public void addServiceListener(ServiceListener listener, String filter) 
throws InvalidSyntaxException {
+        m_bundleContext.addServiceListener(listener, getScopedFilter(filter));
     }
 
+    /**
+     * @see 
org.osgi.framework.BundleContext#addServiceListener(org.osgi.framework.ServiceListener)
+     */
     public void addServiceListener(ServiceListener listener) {
         try {
-            m_bundleContext.addServiceListener(listener,
-                getCompleteFilter(null));
+            m_bundleContext.addServiceListener(listener, 
getScopedFilter(null));
         }
         catch (InvalidSyntaxException e) {
             e.printStackTrace();
         }
     }
 
+    /**
+     * @see 
org.osgi.framework.BundleContext#removeServiceListener(org.osgi.framework.ServiceListener)
+     */
     public void removeServiceListener(ServiceListener listener) {
         m_bundleContext.removeServiceListener(listener);
     }
 
+    /**
+     * @see 
org.osgi.framework.BundleContext#addBundleListener(org.osgi.framework.BundleListener)
+     */
     public void addBundleListener(BundleListener listener) {
-        TenantAwareBundleListener tenantAwareBundleListener = new 
TenantAwareBundleListener(
-            listener);
-        if (m_bundleListeners.putIfAbsent(listener, tenantAwareBundleListener) 
== null) {
-            m_bundleContext.addBundleListener(tenantAwareBundleListener);
-        }
+        TenantAwareBundleListener scopedBundleListener = new 
TenantAwareBundleListener(listener);
+        if (m_bundleListeners.putIfAbsent(listener, scopedBundleListener) == 
null)
+            m_bundleContext.addBundleListener(scopedBundleListener);
     }
 
+    /**
+     * @see 
org.osgi.framework.BundleContext#removeBundleListener(org.osgi.framework.BundleListener)
+     */
     public void removeBundleListener(BundleListener listener) {
-        TenantAwareBundleListener tenantAwareBundleListener = m_bundleListeners
-            .remove(listener);
-        if (tenantAwareBundleListener != null) {
-            m_bundleContext.removeBundleListener(tenantAwareBundleListener);
-        }
+        TenantAwareBundleListener scopedBundleListener = 
m_bundleListeners.remove(listener);
+        if (scopedBundleListener != null)
+            m_bundleContext.removeBundleListener(scopedBundleListener);
     }
 
+    /**
+     * @see 
org.osgi.framework.BundleContext#addFrameworkListener(org.osgi.framework.FrameworkListener)
+     */
     public void addFrameworkListener(FrameworkListener listener) {
-        TenantAwareFrameworkListener tenantAwareFrameworkListener = new 
TenantAwareFrameworkListener(
-            listener);
-        if (m_frameworkListeners.putIfAbsent(listener,
-            tenantAwareFrameworkListener) == null) {
-            m_bundleContext.addFrameworkListener(tenantAwareFrameworkListener);
-        }
+        TenantAwareFrameworkListener scopedFrameworkListener = new 
TenantAwareFrameworkListener(listener);
+        if (m_frameworkListeners.putIfAbsent(listener, 
scopedFrameworkListener) == null)
+            m_bundleContext.addFrameworkListener(scopedFrameworkListener);
     }
 
+    /**
+     * @see 
org.osgi.framework.BundleContext#removeFrameworkListener(org.osgi.framework.FrameworkListener)
+     */
     public void removeFrameworkListener(FrameworkListener listener) {
-        TenantAwareFrameworkListener tenantAwareFrameworkListener = 
m_frameworkListeners
-            .remove(listener);
-        if (tenantAwareFrameworkListener != null) {
-            m_bundleContext
-                .removeFrameworkListener(tenantAwareFrameworkListener);
-        }
+        TenantAwareFrameworkListener scopedFrameworkListener = 
m_frameworkListeners.remove(listener);
+        if (scopedFrameworkListener != null)
+            m_bundleContext.removeFrameworkListener(scopedFrameworkListener);
     }
 
-    public ServiceRegistration registerService(String[] clazzes,
-        Object service, Dictionary properties) {
-        if (properties == null) {
-            properties = new Properties();
-        }
-        properties.put(Tenant.TENANT_ID_SERVICEPROPERTY, m_tenantId);
-        ServiceRegistration registration = m_bundleContext.registerService(
-            clazzes, service, properties);
-        ServiceRegistrationAdapter adapter = new ServiceRegistrationAdapter(
-            registration);
+    /**
+     * @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);
+        ServiceRegistrationAdapter adapter = new 
ServiceRegistrationAdapter(registration);
         m_serviceRegistrations.put(adapter, registration);
         return adapter;
     }
 
-    public ServiceRegistration registerService(String clazz, Object service,
-        Dictionary properties) {
-        if (properties == null) {
-            properties = new Properties();
-        }
-        properties.put(Tenant.TENANT_ID_SERVICEPROPERTY, m_tenantId);
-        ServiceRegistration registration = m_bundleContext.registerService(
-            clazz, service, properties);
-        ServiceRegistrationAdapter adapter = new ServiceRegistrationAdapter(
-            registration);
+    /**
+     * @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);
+        ServiceRegistrationAdapter adapter = new 
ServiceRegistrationAdapter(registration);
         m_serviceRegistrations.put(adapter, registration);
         return adapter;
     }
 
-    public ServiceReference[] getServiceReferences(String clazz, String filter)
-        throws InvalidSyntaxException {
-        try {
-            return m_bundleContext.getServiceReferences(clazz,
-                getCompleteFilter(filter));
-        }
-        catch (InvalidSyntaxException e) {
-            e.printStackTrace();
-        }
-        return null;
-    }
-
-    public String getCompleteFilter(String filter) {
-        String result = (filter == null) ? "(|(" + 
Tenant.TENANT_ID_SERVICEPROPERTY + "="
-            + m_tenantId + ")(!(" + Tenant.TENANT_ID_SERVICEPROPERTY + "=*)))" 
: "(&(|("
-            + Tenant.TENANT_ID_SERVICEPROPERTY + "=" + m_tenantId + ")(!(" + 
Tenant.TENANT_ID_SERVICEPROPERTY
-            + "=*)))" + filter + ")";
-        return result;
-    }
-
-    public ServiceReference[] getAllServiceReferences(String clazz,
-        String filter) throws InvalidSyntaxException {
-        try {
-            return m_bundleContext.getAllServiceReferences(clazz,
-                getCompleteFilter(filter));
-        }
-        catch (InvalidSyntaxException e) {
-            e.printStackTrace();
-        }
-        return null;
-    }
-
+    /**
+     * @see 
org.osgi.framework.BundleContext#getServiceReferences(java.lang.String, 
java.lang.String)
+     */
+    public ServiceReference[] getServiceReferences(String clazz, String 
filter) throws InvalidSyntaxException {
+        return m_bundleContext.getServiceReferences(clazz, 
getScopedFilter(filter));
+    }
+
+    /**
+     * @see 
org.osgi.framework.BundleContext#getAllServiceReferences(java.lang.String, 
java.lang.String)
+     */
+    public ServiceReference[] getAllServiceReferences(String clazz, String 
filter) throws InvalidSyntaxException {
+        return m_bundleContext.getAllServiceReferences(clazz, 
getScopedFilter(filter));
+    }
+
+    /**
+     * @see 
org.osgi.framework.BundleContext#getServiceReference(java.lang.String)
+     */
     public ServiceReference getServiceReference(String clazz) {
         try {
-            ServiceReference[] references = m_bundleContext
-                .getServiceReferences(clazz, "(|(" + 
Tenant.TENANT_ID_SERVICEPROPERTY + "="
-                    + m_tenantId + ")(!(" + Tenant.TENANT_ID_SERVICEPROPERTY + 
"=*)))");
+            ServiceReference[] references = 
m_bundleContext.getServiceReferences(clazz, getScopedFilter(null));
             if (references != null && references.length > 0) {
-                if (references.length > 1) {
-                    Arrays.sort(references);
-                }
+                Arrays.sort(references);
                 return references[0];
             }
         }
-        catch (InvalidSyntaxException e) {
-            e.printStackTrace();
-        }
+        catch (Exception e) {}
         return null;
     }
 
+    /**
+     * @see 
org.osgi.framework.BundleContext#getService(org.osgi.framework.ServiceReference)
+     */
     public Object getService(ServiceReference reference) {
         return m_bundleContext.getService(reference);
     }
 
+    /**
+     * @see 
org.osgi.framework.BundleContext#ungetService(org.osgi.framework.ServiceReference)
+     */
     public boolean ungetService(ServiceReference reference) {
         try {
             return m_bundleContext.ungetService(reference);
         }
-        catch (Exception e) {
-            // race condition
-        }
+        catch (Exception e) {}
         return false;
     }
 
+    /**
+     * @see org.osgi.framework.BundleContext#getDataFile(java.lang.String)
+     */
     public File getDataFile(String filename) {
-        if (m_dataFile == null) {
-            m_dataFile = m_bundleContext.getDataFile(createTenantDataFile());
-            if (m_dataFile == null) {
-                return null;
-            }
-            m_dataFile.mkdir();
-        }
-        if (filename == null) {
-            return m_dataFile;
-        }
-        else {
-            return new File(m_dataFile, filename);
-        }
-    }
-
-    private String createTenantDataFile() {
-        return TENANT_DATAFILE_PREFIX + m_tenantId;
+        File dataFile = m_bundleContext.getDataFile(m_identifier);
+        if (dataFile == null)
+            return null;
+        if (!dataFile.exists())
+            dataFile.mkdir();
+        if (filename == null || "".equals(filename))
+            return dataFile;
+        return new File(dataFile, filename);
     }
 
+    /**
+     * @see org.osgi.framework.BundleContext#createFilter(java.lang.String)
+     */
     public Filter createFilter(String filter) throws InvalidSyntaxException {
         return m_bundleContext.createFilter(filter);
     }
 
-    private class TenantAwareBundleListener implements BundleListener {
+    /**
+     * @param filter
+     * @return
+     */
+    protected String getScopedFilter(String filter) {
+        if (filter == null || "".equals(filter))
+            return m_filter;
+        return "(&" + filter + m_filter + ")";
+    }
+
+    protected Dictionary getScopedProperties(Dictionary properties) {
+        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));
+        }
+        return properties;
+    }
+
+    protected File getScopedDataFile(String scope) {
+        return m_bundleContext.getDataFile(scope);
+    }
+
+    /*
+     * Private stuff
+     */
+
+    private static class TenantAwareBundleListener implements BundleListener {
+
         private final BundleListener m_listener;
 
         public TenantAwareBundleListener(BundleListener listener) {
@@ -297,7 +371,8 @@
         }
     }
 
-    private class TenantAwareFrameworkListener implements FrameworkListener {
+    private static class TenantAwareFrameworkListener implements 
FrameworkListener {
+
         private final FrameworkListener m_listener;
 
         public TenantAwareFrameworkListener(FrameworkListener listener) {
@@ -306,46 +381,50 @@
         }
 
         public void frameworkEvent(FrameworkEvent event) {
-            m_listener.frameworkEvent(new TenantAwareFrameworkEvent(event));
+            m_listener.frameworkEvent(new ScopedFrameworkEvent(event));
         }
 
     }
 
-    private class TenantAwareBundleEvent extends BundleEvent {
+    private static class TenantAwareBundleEvent extends BundleEvent {
+
+        private static final long serialVersionUID = 1L;
+
         private final BundleEvent m_event;
-        private final Bundle m_bundle;
 
         public TenantAwareBundleEvent(BundleEvent event) {
             super(event.getType(), event.getBundle());
             m_event = event;
-            Bundle eventBundle = m_event.getBundle();
-            BundleContext eventBundleContext = eventBundle.getBundleContext();
-            m_bundle = new TenantAwareBundle(eventBundle, m_tenantId);
         }
 
         public Bundle getBundle() {
-            return m_bundle;
+            // TODO this effectively hides the original BundleContext. Maybe
+            // we should be nice and make it available within scope?
+            return new TenantAwareBundle(m_event.getBundle());
         }
     }
 
-    private class TenantAwareFrameworkEvent extends FrameworkEvent {
+    private static class ScopedFrameworkEvent extends FrameworkEvent {
+
+        private static final long serialVersionUID = 1L;
+
         private final FrameworkEvent m_event;
-        private final Bundle m_bundle;
 
-        public TenantAwareFrameworkEvent(FrameworkEvent event) {
+        public ScopedFrameworkEvent(FrameworkEvent event) {
             super(event.getType(), event.getBundle(), event.getThrowable());
             m_event = event;
-            Bundle eventBundle = m_event.getBundle();
-            BundleContext eventBundleContext = eventBundle.getBundleContext();
-            m_bundle = new TenantAwareBundle(eventBundle, m_tenantId);
         }
 
         public Bundle getBundle() {
-            return m_bundle;
+            // TODO this effectively hides the original BundleContext. Maybe
+            // we should be nice and make it available within scope?
+            return new TenantAwareBundle(m_event.getBundle());
         }
     }
 
+    // FIXME I'd like this to be static
     private class ServiceRegistrationAdapter implements ServiceRegistration {
+
         private final ServiceRegistration m_registration;
 
         public ServiceRegistrationAdapter(ServiceRegistration registration) {

Modified: 
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/osgi/Activator.java
==============================================================================
--- 
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/osgi/Activator.java
   (original)
+++ 
trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/osgi/Activator.java
   Fri Dec 30 14:46:47 2011
@@ -29,7 +29,7 @@
 /**
  * This class represents the OSGi activator for the tenant factory
  * 
- * @author ivol
+ * @author <a href="mailto:[email protected]";>Amdatu Project 
Team</a>
  * 
  */
 public class Activator extends DependencyActivatorBase {
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to