[274982] [plug-in registry] Investigate possibility of reuse ECF in remote monitoring
diff --git a/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/.classpath b/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/.classpath
new file mode 100644
index 0000000..304e861
--- /dev/null
+++ b/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/.classpath
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
+	<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/.project b/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/.project
new file mode 100644
index 0000000..ad55583
--- /dev/null
+++ b/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/.project
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>org.eclipse.pde.runtime.rosgi.rs</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.pde.ManifestBuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.pde.SchemaBuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.pde.PluginNature</nature>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>
diff --git a/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/.settings/org.eclipse.jdt.core.prefs b/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..679427e
--- /dev/null
+++ b/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,8 @@
+#Mon Jul 06 15:54:15 CEST 2009
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
+org.eclipse.jdt.core.compiler.compliance=1.5
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.5
diff --git a/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/META-INF/MANIFEST.MF b/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..9060aee
--- /dev/null
+++ b/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/META-INF/MANIFEST.MF
@@ -0,0 +1,18 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: Rs
+Bundle-SymbolicName: org.eclipse.pde.runtime.rosgi.rs;singleton:=true
+Bundle-Version: 1.0.0.qualifier
+Bundle-Activator: org.eclipse.pde.runtime.rosgi.rs.Activator
+Require-Bundle: org.eclipse.core.runtime,
+ org.eclipse.equinox.common,
+ org.eclipse.pde.runtime.core;bundle-version="1.0.0"
+Eclipse-LazyStart: true
+Bundle-RequiredExecutionEnvironment: J2SE-1.5
+Export-Package: org.eclipse.pde.internal.runtime.registry.rosgi;x-internal:=true
+Import-Package: org.eclipse.ecf.core,
+ org.eclipse.ecf.core.identity;version="3.0.0",
+ org.eclipse.ecf.remoteservice,
+ org.eclipse.ecf.remoteservice.events,
+ org.osgi.framework;version="1.5.0",
+ org.osgi.util.tracker
diff --git a/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/build.properties b/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/build.properties
new file mode 100644
index 0000000..e9863e2
--- /dev/null
+++ b/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/build.properties
@@ -0,0 +1,5 @@
+source.. = src/
+output.. = bin/
+bin.includes = META-INF/,\
+               .,\
+               plugin.xml
diff --git a/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/plugin.xml b/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/plugin.xml
new file mode 100644
index 0000000..96c7fac
--- /dev/null
+++ b/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/plugin.xml
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<?eclipse version="3.4"?>
+<plugin>
+   <extension
+         point="org.eclipse.pde.runtime.backends">
+      <backend
+            class="org.eclipse.pde.internal.runtime.registry.rosgi.RosgiRegistryBackend"
+            scheme="r-osgi">
+      </backend>
+   </extension>
+
+</plugin>
diff --git a/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/src/org/eclipse/pde/internal/runtime/registry/rosgi/IRosgiRegistryHost.java b/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/src/org/eclipse/pde/internal/runtime/registry/rosgi/IRosgiRegistryHost.java
new file mode 100644
index 0000000..a8966d7
--- /dev/null
+++ b/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/src/org/eclipse/pde/internal/runtime/registry/rosgi/IRosgiRegistryHost.java
@@ -0,0 +1,22 @@
+package org.eclipse.pde.internal.runtime.registry.rosgi;
+
+public interface IRosgiRegistryHost {
+	
+	public boolean connectRemoteBackendChangeListener();
+	
+	public void setEnabled(long id, boolean enabled);
+
+	public void start(long id);
+
+	public void stop(long id);
+
+	public String[] diagnose(long id);
+
+	public void initializeBundles();
+
+	public void initializeExtensionPoints();
+
+	public void initializeServices();
+	
+	public void disconnect();
+}
diff --git a/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/src/org/eclipse/pde/internal/runtime/registry/rosgi/RosgiRegistryBackend.java b/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/src/org/eclipse/pde/internal/runtime/registry/rosgi/RosgiRegistryBackend.java
new file mode 100644
index 0000000..718b275
--- /dev/null
+++ b/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/src/org/eclipse/pde/internal/runtime/registry/rosgi/RosgiRegistryBackend.java
@@ -0,0 +1,135 @@
+package org.eclipse.pde.internal.runtime.registry.rosgi;
+
+import org.eclipse.core.runtime.Assert;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.ecf.core.IContainer;
+import org.eclipse.ecf.core.IContainerManager;
+import org.eclipse.ecf.core.identity.IDFactory;
+import org.eclipse.ecf.remoteservice.IRemoteService;
+import org.eclipse.ecf.remoteservice.IRemoteServiceContainerAdapter;
+import org.eclipse.ecf.remoteservice.IRemoteServiceReference;
+import org.eclipse.ecf.remoteservice.IRemoteServiceRegistration;
+import org.eclipse.pde.runtime.core.model.BackendChangeListener;
+import org.eclipse.pde.runtime.core.model.RegistryBackend;
+import org.eclipse.pde.runtime.rosgi.rs.Activator;
+import org.osgi.framework.BundleContext;
+import org.osgi.util.tracker.ServiceTracker;
+
+public class RosgiRegistryBackend implements RegistryBackend {
+
+	private static final String DEFAUT_SERVER_HOST = "localhost:9278";
+	private static final String DEFAULT_PROTOCOL = "r-osgi://";
+	private static final String DEFAULT_CONTAINER_TYPE = "ecf.r_osgi.peer";
+	
+	private IRemoteServiceRegistration remoteBackendChangeListenerService;
+	private IRosgiRegistryHost remoteRosgiHost;
+	private BackendChangeListener backendChangeListener;
+	private ServiceTracker containerManagerServiceTracker;
+	private IContainer container;
+	
+	public boolean connect(IProgressMonitor monitor) {
+		try {
+			IContainerManager containerManager = getContainerManagerService(); 
+			container = containerManager.getContainerFactory().createContainer(DEFAULT_CONTAINER_TYPE);
+			IRemoteServiceContainerAdapter containerAdapter = 
+				(IRemoteServiceContainerAdapter) container.getAdapter(IRemoteServiceContainerAdapter.class); 
+
+			String target = DEFAULT_PROTOCOL;
+			if (System.getProperty("server.host") != null)
+				target += System.getProperty("server.host");
+			else 
+				target += DEFAUT_SERVER_HOST;
+			
+			IRemoteServiceReference[] helloReferences = 
+				containerAdapter.getRemoteServiceReferences(
+						IDFactory.getDefault().createID(Activator.getDefault().getContainer().getConnectNamespace(), target), 
+						IRosgiRegistryHost.class.getName(), null);
+			
+			Assert.isNotNull(helloReferences);
+			Assert.isTrue(helloReferences.length > 0);
+
+			IRemoteService remoteService = containerAdapter.getRemoteService(helloReferences[0]);
+			remoteRosgiHost = (IRosgiRegistryHost) remoteService.getProxy(); 
+			registerBackendListener();
+			remoteRosgiHost.connectRemoteBackendChangeListener();
+			
+			return true;
+
+		} catch (Exception e) {
+			disconnect();
+			return false;
+		}
+	}
+	
+	public void initializeBundles(IProgressMonitor monitor) {
+		remoteRosgiHost.initializeBundles();
+	}
+
+	public void initializeExtensionPoints(IProgressMonitor monitor) {
+		remoteRosgiHost.initializeExtensionPoints();
+	}
+
+	public void initializeServices(IProgressMonitor monitor) {
+		remoteRosgiHost.initializeServices();
+	}
+
+	public void setEnabled(long id, boolean enabled) {
+		remoteRosgiHost.setEnabled(id, enabled);
+	}
+
+	public void setRegistryListener(BackendChangeListener listener) {
+		this.backendChangeListener = listener;
+	}
+
+	public void start(long id) {
+		remoteRosgiHost.start(id);
+	}
+
+	public void stop(long id) {
+		remoteRosgiHost.stop(id);
+	}
+
+	public String[] diagnose(long id) {
+		return remoteRosgiHost.diagnose(id);
+	}
+
+	public void disconnect() {
+		if (remoteRosgiHost != null) {
+			remoteRosgiHost.disconnect();
+			remoteRosgiHost = null;
+		}
+		if (remoteBackendChangeListenerService != null) {
+			remoteBackendChangeListenerService.unregister();
+			remoteBackendChangeListenerService = null;
+		}
+		if (containerManagerServiceTracker != null) {
+			containerManagerServiceTracker.close();
+			containerManagerServiceTracker = null;
+		}
+		if (container != null) {
+			container.disconnect();
+			container = null;
+		}
+	}
+
+	private boolean registerBackendListener() {
+		IRemoteServiceContainerAdapter containerAdapter = 
+			(IRemoteServiceContainerAdapter) container.getAdapter(IRemoteServiceContainerAdapter.class); 
+		
+		remoteBackendChangeListenerService = containerAdapter.registerRemoteService(
+				new String[] { BackendChangeListener.class.getName() }, backendChangeListener, null);
+		
+		System.out.println("BackendChangeListener RemoteService registered");
+		
+		return true;
+	}
+	
+	private IContainerManager getContainerManagerService() {
+		if (containerManagerServiceTracker == null) {
+			BundleContext context = Activator.getDefault().getBundleContext();
+			containerManagerServiceTracker = new ServiceTracker(context, IContainerManager.class.getName(), null);
+			containerManagerServiceTracker.open();
+		}
+		return (IContainerManager) containerManagerServiceTracker.getService();
+	}
+}
diff --git a/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/src/org/eclipse/pde/internal/runtime/registry/rosgi/RosgiRegistryHost.java b/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/src/org/eclipse/pde/internal/runtime/registry/rosgi/RosgiRegistryHost.java
new file mode 100644
index 0000000..29474f2
--- /dev/null
+++ b/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/src/org/eclipse/pde/internal/runtime/registry/rosgi/RosgiRegistryHost.java
@@ -0,0 +1,120 @@
+package org.eclipse.pde.internal.runtime.registry.rosgi;
+
+import org.eclipse.core.runtime.Assert;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.ecf.core.IContainer;
+import org.eclipse.ecf.core.IContainerManager;
+import org.eclipse.ecf.core.identity.IDFactory;
+import org.eclipse.ecf.remoteservice.IRemoteService;
+import org.eclipse.ecf.remoteservice.IRemoteServiceContainerAdapter;
+import org.eclipse.ecf.remoteservice.IRemoteServiceReference;
+import org.eclipse.pde.runtime.core.model.BackendChangeListener;
+import org.eclipse.pde.runtime.core.model.LocalRegistryBackend;
+import org.eclipse.pde.runtime.rosgi.rs.Activator;
+import org.osgi.framework.BundleContext;
+import org.osgi.util.tracker.ServiceTracker;
+
+
+
+public class RosgiRegistryHost implements IRosgiRegistryHost {
+
+	private static final String DEFAUT_CLIENT_HOST = "localhost:9279";
+	private static final String DEFAULT_PROTOCOL = "r-osgi://";
+	private static final String DEFAULT_CONTAINER_TYPE = "ecf.r_osgi.peer";
+	
+	private LocalRegistryBackend backend;
+	private IContainer container;
+	private ServiceTracker containerManagerServiceTracker;
+	
+	public RosgiRegistryHost() {
+		backend = new LocalRegistryBackend();
+		backend.connect(new NullProgressMonitor());
+	}
+
+	public String[] diagnose(long id) {
+			return backend.diagnose(id);
+	}
+
+	public void initializeBundles() {
+			backend.initializeBundles(new NullProgressMonitor());
+	}
+
+	public void initializeExtensionPoints() {
+			backend.initializeExtensionPoints(new NullProgressMonitor());
+	}
+
+	public void initializeServices() {
+			backend.initializeServices(new NullProgressMonitor());
+	}
+
+	public void setEnabled(long id, boolean enabled) {
+			backend.setEnabled(id, enabled);
+	}
+
+	public void start(long id) {
+			backend.start(id);
+	}
+
+	public void stop(long id) {
+			backend.stop(id);
+	}
+
+	public void setBackendChangeListner(BackendChangeListener listener) {
+			backend.setRegistryListener(listener);
+	}
+
+	public boolean connectRemoteBackendChangeListener() {
+		try {
+			IContainerManager containerManager = getContainerManagerService(); 
+			container = containerManager.getContainerFactory().createContainer(DEFAULT_CONTAINER_TYPE);
+			
+			IRemoteServiceContainerAdapter containerAdapter = 
+				(IRemoteServiceContainerAdapter) container.getAdapter(IRemoteServiceContainerAdapter.class);
+			
+			String target = DEFAULT_PROTOCOL;
+			if (System.getProperty("client.host") != null)
+				target += System.getProperty("client.host");
+			else 
+				target += DEFAUT_CLIENT_HOST;
+			
+			IRemoteServiceReference[] listenerReferences = 
+				containerAdapter.getRemoteServiceReferences(
+						IDFactory.getDefault().createID(container.getConnectNamespace(), target), 
+						BackendChangeListener.class.getName(), null);
+			
+			Assert.isNotNull(listenerReferences);
+			Assert.isTrue(listenerReferences.length > 0);
+		
+			IRemoteService remoteService = containerAdapter.getRemoteService(listenerReferences[0]);
+			BackendChangeListener listener = (BackendChangeListener) remoteService.getProxy(); 
+			setBackendChangeListner(listener);
+
+			return true;
+
+		} catch (Exception e) {
+			disconnect();
+		}
+
+		return false;
+	}
+
+	public void disconnect() {
+		if (container != null) {
+			container.disconnect();
+			container = null;
+		}
+		if (containerManagerServiceTracker != null) {
+			containerManagerServiceTracker.close();
+			containerManagerServiceTracker = null;
+		}
+	}
+	
+	private IContainerManager getContainerManagerService() {
+		if (containerManagerServiceTracker == null) {
+			BundleContext context = Activator.getDefault().getBundleContext();
+			containerManagerServiceTracker = new ServiceTracker(context, IContainerManager.class.getName(), null);
+			containerManagerServiceTracker.open();
+		}
+		return (IContainerManager) containerManagerServiceTracker.getService();
+	}
+}
diff --git a/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/src/org/eclipse/pde/runtime/rosgi/rs/Activator.java b/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/src/org/eclipse/pde/runtime/rosgi/rs/Activator.java
new file mode 100644
index 0000000..1390805
--- /dev/null
+++ b/osgimonitoring/plugins/org.eclipse.pde.runtime.rosgi.rs/src/org/eclipse/pde/runtime/rosgi/rs/Activator.java
@@ -0,0 +1,91 @@
+package org.eclipse.pde.runtime.rosgi.rs;
+
+import org.eclipse.core.runtime.Plugin;
+import org.eclipse.ecf.core.ContainerCreateException;
+import org.eclipse.ecf.core.IContainer;
+import org.eclipse.ecf.core.IContainerManager;
+import org.eclipse.ecf.remoteservice.IRemoteServiceContainerAdapter;
+import org.eclipse.ecf.remoteservice.IRemoteServiceRegistration;
+import org.eclipse.pde.internal.runtime.registry.rosgi.IRosgiRegistryHost;
+import org.eclipse.pde.internal.runtime.registry.rosgi.RosgiRegistryHost;
+import org.osgi.framework.BundleContext;
+import org.osgi.util.tracker.ServiceTracker;
+
+public class Activator extends Plugin {
+
+	private RosgiRegistryHost host;
+	private IContainer container;
+	private IRemoteServiceRegistration serviceRegistration;
+	private ServiceTracker containerManagerServiceTracker;
+	private static Activator plugin;
+	private BundleContext context;
+	
+	public static final String PLUGIN_ID = "org.eclipse.pde.runtime.rosgi.rs";
+	
+	public Activator() {
+	}
+
+	public void start(BundleContext context) throws Exception {
+		super.start(context);
+		plugin = this;
+		this.context = context;
+		initializeHost();
+	}
+	
+	public boolean initializeHost() {
+		host = new RosgiRegistryHost(); 
+		IContainerManager containerManager = getContainerManagerService(); 
+		try {
+			container = containerManager.getContainerFactory().createContainer("ecf.r_osgi.peer");
+		} catch (ContainerCreateException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+		IRemoteServiceContainerAdapter containerAdapter = (IRemoteServiceContainerAdapter) container
+				.getAdapter(IRemoteServiceContainerAdapter.class); 
+		serviceRegistration = containerAdapter.registerRemoteService(
+				new String[] { IRosgiRegistryHost.class.getName() }, host, null); 
+		
+		System.out.println("IRosgiRegistryHost RemoteService registered");
+		return true;
+	}
+	
+	private IContainerManager getContainerManagerService() {
+		if (containerManagerServiceTracker == null) {
+			containerManagerServiceTracker = new ServiceTracker(context,
+					IContainerManager.class.getName(), null);
+			containerManagerServiceTracker.open();
+		}
+		return (IContainerManager) containerManagerServiceTracker.getService();
+	}
+	
+	public void stop(BundleContext context) throws Exception {
+		plugin = null;
+		if (serviceRegistration != null) {
+			serviceRegistration.unregister();
+			serviceRegistration = null;
+		}
+		if (container != null) {
+			container.disconnect();
+			container = null;
+		}
+		if (containerManagerServiceTracker != null) {
+			containerManagerServiceTracker.close();
+			containerManagerServiceTracker = null;
+		}
+		super.stop(context);
+	}
+	
+	public BundleContext getBundleContext() {
+		return context;
+	}
+
+	public static Activator getDefault() {
+		return plugin;
+	}
+	
+	public IContainer getContainer() {
+		return container;
+	}
+
+}