FELIX-598
---------
BundlesProbes, ObrProbe and OsgiProbes are now well implemented. They now use service notification. The agent listens to service activity and interfaces such as XXXMBean

ie :
 org.apache.felix.mosgi.managedelements.obrprobe.ObrProbeMBea
 org.apache.felix.mosgi.managedelements.bundlesprobes.BundlesProbesMBean
 org.apache.felix.mosgi.managedelements.osgiprobes.OsgiProbesMBean



git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@663620 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/mosgi/managedelements.bundlesprobes/pom.xml b/mosgi/managedelements.bundlesprobes/pom.xml
index 1a490af..57f8f0d 100644
--- a/mosgi/managedelements.bundlesprobes/pom.xml
+++ b/mosgi/managedelements.bundlesprobes/pom.xml
@@ -56,13 +56,18 @@
       <version>1.0.0</version>
       <scope>provided</scope>
     </dependency>
-     <dependency>
-       <groupId>mx4j</groupId>
-       <artifactId>mx4j-jmx</artifactId>
-       <version>3.0.1</version>
-       <scope>provided</scope>
+    <dependency>
+      <groupId>${pom.groupId}</groupId>
+      <artifactId>org.apache.felix.mosgi.jmx.agent</artifactId>
+      <scope>provided</scope>
+      <version>0.9.0-SNAPSHOT</version>
     </dependency>
-   
+    <dependency>
+      <groupId>mx4j</groupId>
+      <artifactId>mx4j-jmx</artifactId>
+      <version>3.0.1</version>
+      <scope>provided</scope>
+    </dependency>
   </dependencies>
 
   <build>
diff --git a/mosgi/managedelements.bundlesprobes/src/main/java/org/apache/felix/mosgi/managedelements/bundlesprobes/BundlesProbes.java b/mosgi/managedelements.bundlesprobes/src/main/java/org/apache/felix/mosgi/managedelements/bundlesprobes/BundlesProbes.java
index 7a21e6f..4bb5cf2 100644
--- a/mosgi/managedelements.bundlesprobes/src/main/java/org/apache/felix/mosgi/managedelements/bundlesprobes/BundlesProbes.java
+++ b/mosgi/managedelements.bundlesprobes/src/main/java/org/apache/felix/mosgi/managedelements/bundlesprobes/BundlesProbes.java
@@ -45,19 +45,19 @@
 import org.osgi.framework.ServiceEvent;
 import org.osgi.framework.ServiceListener;
 import org.osgi.framework.ServiceReference;
-
+import org.osgi.framework.ServiceRegistration;
 
 import org.osgi.service.log.LogService;
 
 public class BundlesProbes extends NotificationBroadcasterSupport implements BundleActivator, BundlesProbesMBean, ServiceListener, BundleListener {
 
   private String version = null;
-  private static String tabNameString = "TabUI:name=BundlesProbes";
+  private static String TAB_NAME_STRING="TabUI:name=BundlesProbes";
 
   private ObjectName tabName = null;
   private MBeanServer server = null;
   private BundleContext bc = null;
-  private ServiceReference sr = null;
+  private ServiceRegistration sr = null;
 
 
   ////////////////////////////////////////////////////////
@@ -73,24 +73,18 @@
   ////////////////////////////////////////////////////////
   public void start(BundleContext context) throws Exception  {
     this.bc=context;
-    this.version=(String)bc.getBundle().getHeaders().get(Constants.BUNDLE_VERSION);
     this.log(LogService.LOG_INFO, "Starting BundlesProbe MBean " + this.version,null);
-    this.tabName=new ObjectName(tabNameString);
-    this.sr = context.getServiceReference(MBeanServer.class.getName());
-    if (sr!=null){
-      this.connectToAgent(sr);
-    }
-		this.log(LogService.LOG_INFO, "BundlesProbes MBean "+this.version+" started", null);
+    java.util.Properties p = new java.util.Properties();
+    p.put(org.apache.felix.mosgi.jmx.agent.Constants.OBJECTNAME, TAB_NAME_STRING);
+    this.sr = this.bc.registerService(BundlesProbesMBean.class.getName(), this, p);
+    this.log(LogService.LOG_INFO, "BundlesProbes MBean "+this.version+" started", null);
   }
 
-
   public void stop(BundleContext context) {
-		this.log(LogService.LOG_INFO, "Stopping BundlesProbes MBean "+this.version, null);
-    if (this.server!=null){
-      this.disconnectFromAgent();
-    }
+    this.log(LogService.LOG_INFO, "Stopping BundlesProbes MBean "+this.version, null);
+    this.sr.unregister();
     this.sr=null;
-		this.log(LogService.LOG_INFO, "BundlesProbes MBean "+this.version+" stopped", null);
+    this.log(LogService.LOG_INFO, "BundlesProbes MBean "+this.version+" stopped", null);
     this.bc=null;
   }
 
@@ -173,16 +167,16 @@
   //       ServiceListener                              //
   ////////////////////////////////////////////////////////
   public void serviceChanged(ServiceEvent event) {
-    ServiceReference sr=event.getServiceReference();
-    Object service=bc.getService(sr);
+    ServiceReference sref=event.getServiceReference();
+    Object service=bc.getService(sref);
     if (this.server==null && event.getType()==ServiceEvent.REGISTERED && service instanceof MBeanServer){
-      this.connectToAgent(sr);
+      //this.connectToAgent(sref);
     }
     if (this.server!=null){
       if(event.getType()==ServiceEvent.UNREGISTERING && service instanceof MBeanServer){
-        this.disconnectFromAgent();
+        //this.disconnectFromAgent();
       }else{
-        this.sendRemoteNotification(ServiceEvent.class.getName(),sr.getBundle().getBundleId(), event.getType(), (String)sr.getBundle().getHeaders().get(Constants.BUNDLE_NAME));
+        this.sendRemoteNotification(ServiceEvent.class.getName(),sref.getBundle().getBundleId(), event.getType(), (String)sref.getBundle().getHeaders().get(Constants.BUNDLE_NAME));
       }
     }
   }
@@ -210,33 +204,6 @@
     super.sendNotification(new AttributeChangeNotification(this.tabName, 0, 0,str.toString(),null, "Bundle", null, null));
   }
 
-  private void connectToAgent(ServiceReference sr){
-    this.log(LogService.LOG_INFO, "Registering to agent", null);
-    try{
-      this.server=(MBeanServer)this.bc.getService(sr);
-      this.server.registerMBean(this, tabName);
-      this.bc.addServiceListener(this);
-      this.bc.addBundleListener(this);
-    }catch (Exception e){
-      e.printStackTrace();
-    }
-    this.log(LogService.LOG_INFO, "Registered to agent", null);
-  }
-  
-  private void disconnectFromAgent(){
-    this.log(LogService.LOG_INFO, "Unregistering from agent", null);
-    this.bc.removeServiceListener(this);
-    this.bc.removeBundleListener(this);
-    try {
-      server.unregisterMBean(tabName);
-    } catch (Exception e) {
-      e.printStackTrace();
-    }
-    this.server=null;
-    this.bc.ungetService(this.sr);
-    this.log(LogService.LOG_INFO, "Unregistered from agent", null);
-  }
-
   private void log(int prio, String message, Throwable t){
     if (this.bc!=null){
       ServiceReference logSR=this.bc.getServiceReference(LogService.class.getName());
diff --git a/mosgi/managedelements.obrprobe/pom.xml b/mosgi/managedelements.obrprobe/pom.xml
index e99f732..37e2d0d 100644
--- a/mosgi/managedelements.obrprobe/pom.xml
+++ b/mosgi/managedelements.obrprobe/pom.xml
@@ -68,8 +68,12 @@
        <version>3.0.1</version>
        <scope>provided</scope>
     </dependency>
-
-    
+    <dependency>
+      <groupId>${pom.groupId}</groupId>
+      <artifactId>org.apache.felix.mosgi.jmx.agent</artifactId>
+      <scope>provided</scope>
+      <version>0.9.0-SNAPSHOT</version>
+    </dependency>
   </dependencies>
 
   <build>
diff --git a/mosgi/managedelements.obrprobe/src/main/java/org/apache/felix/mosgi/managedelements/obrprobe/ObrProbe.java b/mosgi/managedelements.obrprobe/src/main/java/org/apache/felix/mosgi/managedelements/obrprobe/ObrProbe.java
index 9499915..aed0e8a 100644
--- a/mosgi/managedelements.obrprobe/src/main/java/org/apache/felix/mosgi/managedelements/obrprobe/ObrProbe.java
+++ b/mosgi/managedelements.obrprobe/src/main/java/org/apache/felix/mosgi/managedelements/obrprobe/ObrProbe.java
@@ -43,6 +43,7 @@
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceEvent;
 import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
 
 //import org.apache.felix.bundlerepository.BundleRepository;
 import org.osgi.service.obr.RepositoryAdmin;
@@ -55,12 +56,11 @@
 public class ObrProbe implements BundleActivator, ObrProbeMBean {
 
   private String version = null;
-  private static String tabNameString = "TabUI:name=ObrProbe";
+  private static final String TAB_NAME_STRING = "TabUI:name=ObrProbe";
 
-  private ObjectName tabName = null;
   private MBeanServer server = null;
   private BundleContext bc = null;
-  private ServiceReference sr = null;
+  private ServiceRegistration sr = null;
 
 
   ////////////////////////////////////////////////////////
@@ -78,20 +78,15 @@
     this.bc=context;
     this.version=(String)bc.getBundle().getHeaders().get(Constants.BUNDLE_VERSION);
     this.log(LogService.LOG_INFO, "Starting obrProbe MBean " + this.version,null);
-    this.tabName=new ObjectName(tabNameString);
-    this.sr = context.getServiceReference(MBeanServer.class.getName());
-    if (sr!=null){
-      this.connectToAgent(sr);
-    }
+    java.util.Properties p = new java.util.Properties();
+    p.put(org.apache.felix.mosgi.jmx.agent.Constants.OBJECTNAME, TAB_NAME_STRING);
+    this.sr = this.bc.registerService(ObrProbeMBean.class.getName(), this, p);
     this.log(LogService.LOG_INFO, "ObrProbe MBean "+this.version+" started", null);
   }
 
-
   public void stop(BundleContext context) {
     this.log(LogService.LOG_INFO, "Stopping obrprobe MBean "+this.version, null);
-    if (this.server!=null){
-      this.disconnectFromAgent();
-    }
+    this.sr.unregister();
     this.sr=null;
     this.log(LogService.LOG_INFO, "obrProbe MBean "+this.version+" stopped", null);
     this.bc=null;
@@ -101,11 +96,11 @@
   //       ObrProbeMBean                              //
   ////////////////////////////////////////////////////////
   public void deploy(String location,String version){
-    ServiceReference sr=this.bc.getServiceReference(RepositoryAdmin.class.getName());
+    ServiceReference sref=this.bc.getServiceReference(RepositoryAdmin.class.getName());
 
     System.out.println("Starting "+location+" "+version);
-    if (sr!=null){
-      RepositoryAdmin brs=(RepositoryAdmin)this.bc.getService(sr);
+    if (sref!=null){
+      RepositoryAdmin brs=(RepositoryAdmin)this.bc.getService(sref);
       Resolver resolver=brs.resolver();
       Resource ressource = selectNewestVersion(searchRepository(brs, location, version));
       if (ressource!=null){
@@ -126,45 +121,6 @@
     }
   }
 
-  ////////////////////////////////////////////////////////
-  //       ServiceListener                              //
-  ////////////////////////////////////////////////////////
-  public void serviceChanged(ServiceEvent event) {
-    ServiceReference sr=event.getServiceReference();
-    Object service=bc.getService(sr);
-    if (this.server==null && event.getType()==ServiceEvent.REGISTERED && service instanceof MBeanServer){
-      this.connectToAgent(sr);
-    }
-    if (this.server!=null){
-      if(event.getType()==ServiceEvent.UNREGISTERING && service instanceof MBeanServer){
-        this.disconnectFromAgent();
-      }
-    }
-  }
-
-  private void connectToAgent(ServiceReference sr){
-    this.log(LogService.LOG_INFO, "Registering to agent", null);
-    try{
-      this.server=(MBeanServer)this.bc.getService(sr);
-      this.server.registerMBean(this, tabName);
-    }catch (Exception e){
-      e.printStackTrace();
-    }
-    this.log(LogService.LOG_INFO, "Registered to agent", null);
-  }
-  
-  private void disconnectFromAgent(){
-    this.log(LogService.LOG_INFO, "Unregistering from agent", null);
-    try {
-      server.unregisterMBean(tabName);
-    } catch (Exception e) {
-      e.printStackTrace();
-    }
-    this.server=null;
-    this.bc.ungetService(this.sr);
-    this.log(LogService.LOG_INFO, "Unregistered from agent", null);
-  }
-
   private void log(int prio, String message, Throwable t){
     if (this.bc!=null){
       ServiceReference logSR=this.bc.getServiceReference(LogService.class.getName());
diff --git a/mosgi/managedelements.osgiprobes/pom.xml b/mosgi/managedelements.osgiprobes/pom.xml
index 8cae93e..03d2369 100644
--- a/mosgi/managedelements.osgiprobes/pom.xml
+++ b/mosgi/managedelements.osgiprobes/pom.xml
@@ -56,6 +56,12 @@
       <version>1.0.0</version>
       <scope>provided</scope>
     </dependency>
+    <dependency>
+      <groupId>${pom.groupId}</groupId>
+      <artifactId>org.apache.felix.mosgi.jmx.agent</artifactId>
+      <scope>provided</scope>
+      <version>0.9.0-SNAPSHOT</version>
+    </dependency>
      <dependency>
        <groupId>mx4j</groupId>
        <artifactId>mx4j-jmx</artifactId>
diff --git a/mosgi/managedelements.osgiprobes/src/main/java/org/apache/felix/mosgi/managedelements/osgiprobes/OsgiProbes.java b/mosgi/managedelements.osgiprobes/src/main/java/org/apache/felix/mosgi/managedelements/osgiprobes/OsgiProbes.java
index 409124e..2383e28 100644
--- a/mosgi/managedelements.osgiprobes/src/main/java/org/apache/felix/mosgi/managedelements/osgiprobes/OsgiProbes.java
+++ b/mosgi/managedelements.osgiprobes/src/main/java/org/apache/felix/mosgi/managedelements/osgiprobes/OsgiProbes.java
@@ -42,21 +42,21 @@
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceEvent;
 import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
 import org.osgi.framework.ServiceListener;
 
 import org.apache.felix.framework.cache.BundleCache;
 
 import org.osgi.service.log.LogService;
 
-public class OsgiProbes extends NotificationBroadcasterSupport implements BundleActivator, OsgiProbesMBean, ServiceListener {
+public class OsgiProbes extends NotificationBroadcasterSupport implements BundleActivator, OsgiProbesMBean {
 
   private String version = null;
-  private static String tabNameString = "TabUI:name=OsgiProbes";
+  private static final String TAB_NAME_STRING = "TabUI:name=OsgiProbes";
 
-  private ObjectName tabName = null;
   private MBeanServer server = null;
   private BundleContext bc = null;
-  private ServiceReference sr = null;
+  private ServiceRegistration sr = null;
 
 
   ////////////////////////////////////////////////////////
@@ -72,22 +72,17 @@
   ////////////////////////////////////////////////////////
   public void start(BundleContext context) throws Exception  {
     this.bc=context;
-    this.version=(String)bc.getBundle().getHeaders().get(Constants.BUNDLE_VERSION);
     this.log(LogService.LOG_INFO, "Starting OsgiProbe MBean " + this.version,null);
-    this.tabName=new ObjectName(tabNameString);
-    this.sr = context.getServiceReference(MBeanServer.class.getName());
-    if (sr!=null){
-      this.connectToAgent(sr);
-    }
+    java.util.Properties p = new java.util.Properties();
+    p.put(org.apache.felix.mosgi.jmx.agent.Constants.OBJECTNAME, TAB_NAME_STRING);
+    this.sr = this.bc.registerService(OsgiProbesMBean.class.getName(), this, p);
+    this.version=(String)bc.getBundle().getHeaders().get(Constants.BUNDLE_VERSION);
     this.log(LogService.LOG_INFO, "OsgiProbes MBean "+this.version+" started", null);
   }
 
-
   public void stop(BundleContext context) {
     this.log(LogService.LOG_INFO, "Stopping OsgiProbes MBean "+this.version, null);
-    if (this.server!=null){
-      this.disconnectFromAgent();
-    }
+    this.sr.unregister();
     this.sr=null;
     this.log(LogService.LOG_INFO, "OsgiProbes MBean "+this.version+" stopped", null);
     this.bc=null;
@@ -129,48 +124,6 @@
     return this.bc.getProperty(BundleCache.CACHE_PROFILE_PROP);
   }
 
-  ////////////////////////////////////////////////////////
-  //       ServiceListener                              //
-  ////////////////////////////////////////////////////////
-  public void serviceChanged(ServiceEvent event) {
-    ServiceReference sr=event.getServiceReference();
-    Object service=bc.getService(sr);
-    if (this.server==null && event.getType()==ServiceEvent.REGISTERED && service instanceof MBeanServer){
-      this.connectToAgent(sr);
-    }
-    if (this.server!=null){
-      if(event.getType()==ServiceEvent.UNREGISTERING && service instanceof MBeanServer){
-        this.disconnectFromAgent();
-      }
-    }
-  }
-
-
-  private void connectToAgent(ServiceReference sr){
-    this.log(LogService.LOG_INFO, "Registering to agent", null);
-    try{
-      this.server=(MBeanServer)this.bc.getService(sr);
-      this.server.registerMBean(this, tabName);
-      this.bc.addServiceListener(this);
-    }catch (Exception e){
-      e.printStackTrace();
-    }
-    this.log(LogService.LOG_INFO, "Registered to agent", null);
-  }
-  
-  private void disconnectFromAgent(){
-    this.log(LogService.LOG_INFO, "Unregistering from agent", null);
-    this.bc.removeServiceListener(this);
-    try {
-      server.unregisterMBean(tabName);
-    } catch (Exception e) {
-      e.printStackTrace();
-    }
-    this.server=null;
-    this.bc.ungetService(this.sr);
-    this.log(LogService.LOG_INFO, "Unregistered from agent", null);
-  }
-
   private void log(int prio, String message, Throwable t){
     if (this.bc!=null){
       ServiceReference logSR=this.bc.getServiceReference(LogService.class.getName());