geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject svn commit: r777244 - in /geronimo/sandbox/blueprint/blueprint-cm/src/main/java/org/apache/geronimo/blueprint/compendium/cm: CmManagedProperties.java ManagedServiceManager.java
Date Thu, 21 May 2009 20:33:57 GMT
Author: gawor
Date: Thu May 21 20:33:57 2009
New Revision: 777244

URL: http://svn.apache.org/viewvc?rev=777244&view=rev
Log:
only one ManagedService is allowed for persistance id

Added:
    geronimo/sandbox/blueprint/blueprint-cm/src/main/java/org/apache/geronimo/blueprint/compendium/cm/ManagedServiceManager.java
  (with props)
Modified:
    geronimo/sandbox/blueprint/blueprint-cm/src/main/java/org/apache/geronimo/blueprint/compendium/cm/CmManagedProperties.java

Modified: geronimo/sandbox/blueprint/blueprint-cm/src/main/java/org/apache/geronimo/blueprint/compendium/cm/CmManagedProperties.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-cm/src/main/java/org/apache/geronimo/blueprint/compendium/cm/CmManagedProperties.java?rev=777244&r1=777243&r2=777244&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-cm/src/main/java/org/apache/geronimo/blueprint/compendium/cm/CmManagedProperties.java
(original)
+++ geronimo/sandbox/blueprint/blueprint-cm/src/main/java/org/apache/geronimo/blueprint/compendium/cm/CmManagedProperties.java
Thu May 21 20:33:57 2009
@@ -38,10 +38,7 @@
 import org.apache.geronimo.blueprint.di.MissingAccessorException;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.Constants;
-import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.cm.ConfigurationAdmin;
-import org.osgi.service.cm.ConfigurationException;
-import org.osgi.service.cm.ManagedService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -64,7 +61,6 @@
 
     private final Object lock = new Object();
     private final Set<Object> beans = new HashSet<Object>();
-    private ServiceRegistration registration;
     private Dictionary<String,Object> properties;
 
     public ExtendedBlueprintContainer getBlueprintContainer() {
@@ -123,7 +119,7 @@
         props.put(Constants.BUNDLE_SYMBOLICNAME, bundle.getSymbolicName());
         props.put(Constants.BUNDLE_VERSION, bundle.getHeaders().get(Constants.BUNDLE_VERSION));
         synchronized (lock) {
-            registration = blueprintContainer.getBundleContext().registerService(ManagedService.class.getName(),
new ConfigurationWatcher(), props);
+            ManagedServiceManager.register(this, props);
             try {
                 properties = configAdmin.getConfiguration(persistentId).getProperties();
             } catch (Throwable t) {
@@ -133,9 +129,7 @@
     }
 
     public void destroy() {
-        if (registration != null) {
-            registration.unregister();
-        }
+        ManagedServiceManager.unregister(this);
     }
 
     public void updated(Dictionary<String,Object> props) {
@@ -264,11 +258,4 @@
         }
     }
 
-    private class ConfigurationWatcher implements ManagedService {
-
-        public void updated(Dictionary props) throws ConfigurationException {
-            CmManagedProperties.this.updated(props);
-        }
-    }
-
 }

Added: geronimo/sandbox/blueprint/blueprint-cm/src/main/java/org/apache/geronimo/blueprint/compendium/cm/ManagedServiceManager.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-cm/src/main/java/org/apache/geronimo/blueprint/compendium/cm/ManagedServiceManager.java?rev=777244&view=auto
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-cm/src/main/java/org/apache/geronimo/blueprint/compendium/cm/ManagedServiceManager.java
(added)
+++ geronimo/sandbox/blueprint/blueprint-cm/src/main/java/org/apache/geronimo/blueprint/compendium/cm/ManagedServiceManager.java
Thu May 21 20:33:57 2009
@@ -0,0 +1,151 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.geronimo.blueprint.compendium.cm;
+
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Properties;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.cm.ManagedService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Since persistence id can only be associated with one ManagedService in a bundle
+ * this class ensures only one ManagedService is registered per persistence id.
+ */
+public class ManagedServiceManager {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(ManagedServiceManager.class);
+
+    private static HashMap<Key, ConfigurationWatcher> map = new HashMap<Key, ConfigurationWatcher>();
+               
+    public static synchronized void register(CmManagedProperties cm, Properties props) {
+        Key key = new Key(cm);
+        ConfigurationWatcher reg = map.get(key);
+        if (reg == null) {
+            reg = new ConfigurationWatcher(); 
+            ServiceRegistration registration = cm.getBlueprintContainer().getBundleContext().registerService(ManagedService.class.getName(),
reg, props);
+            reg.setRegistration(registration);            
+            map.put(key, reg);
+        }
+        reg.add(cm);
+    }
+
+    public static synchronized void unregister(CmManagedProperties cm) {
+        Key key = new Key(cm);
+        ConfigurationWatcher reg = map.get(key);
+        if (reg != null) {
+            reg.remove(cm);
+            if (reg.isEmpty()) {
+                map.remove(key);
+                reg.getRegistration().unregister();
+            }
+        }
+    }
+            
+    private static class ConfigurationWatcher implements ManagedService {
+
+        private ServiceRegistration registration;
+        private List<CmManagedProperties> list = new ArrayList<CmManagedProperties>();
+        
+        public ConfigurationWatcher() {
+        }
+        
+        public void updated(Dictionary props) throws ConfigurationException {
+            for (CmManagedProperties cm : list) {
+                cm.updated(props);
+            }
+        }
+        
+        private void setRegistration(ServiceRegistration registration) {
+            this.registration = registration;
+        }
+        
+        private ServiceRegistration getRegistration() {
+            return registration;
+        }
+        
+        private void add(CmManagedProperties cm) {
+            list.add(cm);
+        }
+        
+        private void remove(CmManagedProperties cm) {
+            list.remove(cm);
+        }
+        
+        private boolean isEmpty() {
+            return list.isEmpty();
+        }
+    }
+    
+    private static class Key {
+        
+        private String persistanceId;
+        private Bundle bundle;
+        
+        public Key(CmManagedProperties cm) {
+            this.persistanceId = cm.getPersistentId();
+            this.bundle = cm.getBlueprintContainer().getBundleContext().getBundle();
+        }
+
+        public int hashCode() {
+            final int prime = 31;
+            int result = 1;
+            result = prime * result + ((bundle == null) ? 0 : bundle.hashCode());
+            result = prime * result + ((persistanceId == null) ? 0 : persistanceId.hashCode());
+            return result;
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj) {
+                return true;
+            }
+            if (obj == null) {
+                return false;
+            }
+            if (getClass() != obj.getClass()) {
+                return false;
+            }
+            Key other = (Key) obj;
+            if (bundle == null) {
+                if (other.bundle != null) {
+                    return false;
+                }
+            } else if (!bundle.equals(other.bundle)) {
+                return false;
+            }
+            if (persistanceId == null) {
+                if (other.persistanceId != null) {
+                    return false;
+                }
+            } else if (!persistanceId.equals(other.persistanceId)) {
+                return false;
+            }
+            return true;
+        }
+        
+    }
+}

Propchange: geronimo/sandbox/blueprint/blueprint-cm/src/main/java/org/apache/geronimo/blueprint/compendium/cm/ManagedServiceManager.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/blueprint/blueprint-cm/src/main/java/org/apache/geronimo/blueprint/compendium/cm/ManagedServiceManager.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/sandbox/blueprint/blueprint-cm/src/main/java/org/apache/geronimo/blueprint/compendium/cm/ManagedServiceManager.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message