Author: [email protected] Date: Mon Dec 12 16:23:28 2011 New Revision: 1836
Log: AMDATU-468 Code format / catch illegal state in unget due to race condition at framework stop Modified: trunk/amdatu-core/tenant/src/main/java/org/amdatu/core/tenant/adaptor/TenantAwareBundleContext.java 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 Mon Dec 12 16:23:28 2011 @@ -39,317 +39,331 @@ import org.osgi.framework.ServiceReference; import org.osgi.framework.ServiceRegistration; - /** * Wrapper class for the bundle context interface to make it tenant aware. * * @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); - } - - public void unregisterServices() { - for (ServiceRegistration registration : m_serviceRegistrations.values()) { - registration.unregister(); - } - m_serviceRegistrations.clear(); - } - - public String getProperty(String key) { - return m_bundleContext.getProperty(key); - } - - public Bundle getBundle() { - return m_bundle; - } - - public Bundle installBundle(String location, InputStream input) - throws BundleException { - return m_bundleContext.installBundle(location, - new MultiTenantBundleInputStream(input)); - } - - public Bundle installBundle(String location) throws BundleException { - try { - return m_bundleContext.installBundle(location, - new MultiTenantBundleInputStream( - bundleLocationToInputStream(location))); - } catch (IOException e) { - throw new BundleException( - "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(); - } - - public Bundle getBundle(long id) { - Bundle bundle = m_bundleContext.getBundle(id); - return new TenantAwareBundle(bundle, m_tenantId); - } - - public Bundle[] getBundles() { - Bundle[] bundles = m_bundleContext.getBundles(); - for (int i = 0; i < bundles.length; i++) { - Bundle bundle = bundles[i]; - bundles[i] = new TenantAwareBundle(bundle, m_tenantId); - } - return bundles; - } - - public void addServiceListener(ServiceListener listener, String filter) - throws InvalidSyntaxException { - m_bundleContext.addServiceListener(listener, getCompleteFilter(filter)); - } - - public void addServiceListener(ServiceListener listener) { - try { - m_bundleContext.addServiceListener(listener, - getCompleteFilter(null)); - } catch (InvalidSyntaxException e) { - e.printStackTrace(); - } - } - - public void removeServiceListener(ServiceListener listener) { - m_bundleContext.removeServiceListener(listener); - } - - public void addBundleListener(BundleListener listener) { - TenantAwareBundleListener tenantAwareBundleListener = new TenantAwareBundleListener( - listener); - if (m_bundleListeners.putIfAbsent(listener, tenantAwareBundleListener) == null) { - m_bundleContext.addBundleListener(tenantAwareBundleListener); - } - } - - public void removeBundleListener(BundleListener listener) { - TenantAwareBundleListener tenantAwareBundleListener = m_bundleListeners - .remove(listener); - if (tenantAwareBundleListener != null) { - m_bundleContext.removeBundleListener(tenantAwareBundleListener); - } - } - - public void addFrameworkListener(FrameworkListener listener) { - TenantAwareFrameworkListener tenantAwareFrameworkListener = new TenantAwareFrameworkListener( - listener); - if (m_frameworkListeners.putIfAbsent(listener, - tenantAwareFrameworkListener) == null) { - m_bundleContext.addFrameworkListener(tenantAwareFrameworkListener); - } - } - - public void removeFrameworkListener(FrameworkListener listener) { - TenantAwareFrameworkListener tenantAwareFrameworkListener = m_frameworkListeners - .remove(listener); - if (tenantAwareFrameworkListener != null) { - m_bundleContext - .removeFrameworkListener(tenantAwareFrameworkListener); - } - } - - 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); - 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); - 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; - } - - public ServiceReference getServiceReference(String clazz) { - try { - ServiceReference[] references = m_bundleContext - .getServiceReferences(clazz, "(|(" + Tenant.TENANT_ID_SERVICEPROPERTY + "=" - + m_tenantId + ")(!(" + Tenant.TENANT_ID_SERVICEPROPERTY + "=*)))"); - if (references != null && references.length > 0) { - if (references.length > 1) { - Arrays.sort(references); - } - return references[0]; - } - } catch (InvalidSyntaxException e) { - e.printStackTrace(); - } - return null; - } - - public Object getService(ServiceReference reference) { - return m_bundleContext.getService(reference); - } - - public boolean ungetService(ServiceReference reference) { - return m_bundleContext.ungetService(reference); - } - - 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; - } - - public Filter createFilter(String filter) throws InvalidSyntaxException { - return m_bundleContext.createFilter(filter); - } - - private class TenantAwareBundleListener implements BundleListener { - private final BundleListener m_listener; - - public TenantAwareBundleListener(BundleListener listener) { - m_listener = listener; - } - - public void bundleChanged(BundleEvent event) { - m_listener.bundleChanged(new TenantAwareBundleEvent(event)); - } - } - - private class TenantAwareFrameworkListener implements FrameworkListener { - private final FrameworkListener m_listener; - - public TenantAwareFrameworkListener(FrameworkListener listener) { - m_listener = listener; - - } - - public void frameworkEvent(FrameworkEvent event) { - m_listener.frameworkEvent(new TenantAwareFrameworkEvent(event)); - } - - } - - private class TenantAwareBundleEvent extends BundleEvent { - 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; - } - } - - private class TenantAwareFrameworkEvent extends FrameworkEvent { - private final FrameworkEvent m_event; - private final Bundle m_bundle; - - public TenantAwareFrameworkEvent(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; - } - } - - private class ServiceRegistrationAdapter implements ServiceRegistration { - private final ServiceRegistration m_registration; - - public ServiceRegistrationAdapter(ServiceRegistration registration) { - m_registration = registration; - - } - - public ServiceReference getReference() { - return m_registration.getReference(); - } - - public void setProperties(Dictionary properties) { - m_registration.setProperties(properties); - } - - public void unregister() { - m_serviceRegistrations.remove(this); - m_registration.unregister(); - } - } + 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); + } + + public void unregisterServices() { + for (ServiceRegistration registration : m_serviceRegistrations.values()) { + registration.unregister(); + } + m_serviceRegistrations.clear(); + } + + public String getProperty(String key) { + return m_bundleContext.getProperty(key); + } + + public Bundle getBundle() { + return m_bundle; + } + + public Bundle installBundle(String location, InputStream input) + throws BundleException { + return m_bundleContext.installBundle(location, + new MultiTenantBundleInputStream(input)); + } + + public Bundle installBundle(String location) throws BundleException { + try { + return m_bundleContext.installBundle(location, + new MultiTenantBundleInputStream( + bundleLocationToInputStream(location))); + } + catch (IOException e) { + throw new BundleException( + "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(); + } + + public Bundle getBundle(long id) { + Bundle bundle = m_bundleContext.getBundle(id); + return new TenantAwareBundle(bundle, m_tenantId); + } + + public Bundle[] getBundles() { + Bundle[] bundles = m_bundleContext.getBundles(); + for (int i = 0; i < bundles.length; i++) { + Bundle bundle = bundles[i]; + bundles[i] = new TenantAwareBundle(bundle, m_tenantId); + } + return bundles; + } + + public void addServiceListener(ServiceListener listener, String filter) + throws InvalidSyntaxException { + m_bundleContext.addServiceListener(listener, getCompleteFilter(filter)); + } + + public void addServiceListener(ServiceListener listener) { + try { + m_bundleContext.addServiceListener(listener, + getCompleteFilter(null)); + } + catch (InvalidSyntaxException e) { + e.printStackTrace(); + } + } + + public void removeServiceListener(ServiceListener listener) { + m_bundleContext.removeServiceListener(listener); + } + + public void addBundleListener(BundleListener listener) { + TenantAwareBundleListener tenantAwareBundleListener = new TenantAwareBundleListener( + listener); + if (m_bundleListeners.putIfAbsent(listener, tenantAwareBundleListener) == null) { + m_bundleContext.addBundleListener(tenantAwareBundleListener); + } + } + + public void removeBundleListener(BundleListener listener) { + TenantAwareBundleListener tenantAwareBundleListener = m_bundleListeners + .remove(listener); + if (tenantAwareBundleListener != null) { + m_bundleContext.removeBundleListener(tenantAwareBundleListener); + } + } + + public void addFrameworkListener(FrameworkListener listener) { + TenantAwareFrameworkListener tenantAwareFrameworkListener = new TenantAwareFrameworkListener( + listener); + if (m_frameworkListeners.putIfAbsent(listener, + tenantAwareFrameworkListener) == null) { + m_bundleContext.addFrameworkListener(tenantAwareFrameworkListener); + } + } + + public void removeFrameworkListener(FrameworkListener listener) { + TenantAwareFrameworkListener tenantAwareFrameworkListener = m_frameworkListeners + .remove(listener); + if (tenantAwareFrameworkListener != null) { + m_bundleContext + .removeFrameworkListener(tenantAwareFrameworkListener); + } + } + + 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); + 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); + 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; + } + + public ServiceReference getServiceReference(String clazz) { + try { + ServiceReference[] references = m_bundleContext + .getServiceReferences(clazz, "(|(" + Tenant.TENANT_ID_SERVICEPROPERTY + "=" + + m_tenantId + ")(!(" + Tenant.TENANT_ID_SERVICEPROPERTY + "=*)))"); + if (references != null && references.length > 0) { + if (references.length > 1) { + Arrays.sort(references); + } + return references[0]; + } + } + catch (InvalidSyntaxException e) { + e.printStackTrace(); + } + return null; + } + + public Object getService(ServiceReference reference) { + return m_bundleContext.getService(reference); + } + + public boolean ungetService(ServiceReference reference) { + try { + return m_bundleContext.ungetService(reference); + } + catch (Exception e) { + // race condition + } + return false; + } + + 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; + } + + public Filter createFilter(String filter) throws InvalidSyntaxException { + return m_bundleContext.createFilter(filter); + } + + private class TenantAwareBundleListener implements BundleListener { + private final BundleListener m_listener; + + public TenantAwareBundleListener(BundleListener listener) { + m_listener = listener; + } + + public void bundleChanged(BundleEvent event) { + m_listener.bundleChanged(new TenantAwareBundleEvent(event)); + } + } + + private class TenantAwareFrameworkListener implements FrameworkListener { + private final FrameworkListener m_listener; + + public TenantAwareFrameworkListener(FrameworkListener listener) { + m_listener = listener; + + } + + public void frameworkEvent(FrameworkEvent event) { + m_listener.frameworkEvent(new TenantAwareFrameworkEvent(event)); + } + + } + + private class TenantAwareBundleEvent extends BundleEvent { + 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; + } + } + + private class TenantAwareFrameworkEvent extends FrameworkEvent { + private final FrameworkEvent m_event; + private final Bundle m_bundle; + + public TenantAwareFrameworkEvent(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; + } + } + + private class ServiceRegistrationAdapter implements ServiceRegistration { + private final ServiceRegistration m_registration; + + public ServiceRegistrationAdapter(ServiceRegistration registration) { + m_registration = registration; + + } + + public ServiceReference getReference() { + return m_registration.getReference(); + } + + public void setProperties(Dictionary properties) { + m_registration.setProperties(properties); + } + + public void unregister() { + m_serviceRegistrations.remove(this); + m_registration.unregister(); + } + } } _______________________________________________ Amdatu-commits mailing list [email protected] http://lists.amdatu.org/mailman/listinfo/amdatu-commits
