From 3dbe346da6324f662408bc32234f05283d78c79a Mon Sep 17 00:00:00 2001 From: Scott Lewis Date: Fri, 16 Aug 2024 12:57:15 -0700 Subject: [PATCH] Update to 1.3.1 --- org.eclipse.ecf.provider.etcd3/bnd.bnd | 3 +- .../eclipse/ecf/provider/etcd3/Activator.java | 60 ++++++++++++++++++- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/org.eclipse.ecf.provider.etcd3/bnd.bnd b/org.eclipse.ecf.provider.etcd3/bnd.bnd index a8d509f..d1fc306 100644 --- a/org.eclipse.ecf.provider.etcd3/bnd.bnd +++ b/org.eclipse.ecf.provider.etcd3/bnd.bnd @@ -28,7 +28,8 @@ osgi.core,\ org.osgi.service.component.annotations,\ json,\ - wrapped.com.google.protobuf.protobuf-java + wrapped.com.google.protobuf.protobuf-java,\ + org.apache.felix.framework javac.source=17 javac.target=17 diff --git a/org.eclipse.ecf.provider.etcd3/src/main/java/org/eclipse/ecf/provider/etcd3/Activator.java b/org.eclipse.ecf.provider.etcd3/src/main/java/org/eclipse/ecf/provider/etcd3/Activator.java index 4e9046d..de0263f 100644 --- a/org.eclipse.ecf.provider.etcd3/src/main/java/org/eclipse/ecf/provider/etcd3/Activator.java +++ b/org.eclipse.ecf.provider.etcd3/src/main/java/org/eclipse/ecf/provider/etcd3/Activator.java @@ -8,19 +8,28 @@ ******************************************************************************/ package org.eclipse.ecf.provider.etcd3; +import java.util.Hashtable; + +import org.eclipse.ecf.core.ContainerConnectException; +import org.eclipse.ecf.core.ContainerCreateException; import org.eclipse.ecf.core.ContainerTypeDescription; import org.eclipse.ecf.core.IContainerFactory; import org.eclipse.ecf.core.identity.IDFactory; import org.eclipse.ecf.core.identity.Namespace; +import org.eclipse.ecf.discovery.IDiscoveryAdvertiser; +import org.eclipse.ecf.discovery.IDiscoveryLocator; import org.eclipse.ecf.provider.etcd3.container.Etcd3DiscoveryContainer; import org.eclipse.ecf.provider.etcd3.container.Etcd3DiscoveryContainerConfig; import org.eclipse.ecf.provider.etcd3.container.Etcd3DiscoveryContainerInstantiator; import org.eclipse.ecf.provider.etcd3.identity.Etcd3Namespace; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceFactory; +import org.osgi.framework.ServiceRegistration; import org.osgi.util.tracker.ServiceTracker; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.osgi.framework.Bundle; public class Activator implements BundleActivator { @@ -40,6 +49,7 @@ public static BundleContext getContext() { return context; } + private Etcd3DiscoveryContainer container; private ServiceTracker cfTracker; @SuppressWarnings("unchecked") @@ -58,7 +68,45 @@ public void start(BundleContext ctxt) throws Exception { null); // Only create/setup if not explicitly disabled if (!Boolean.parseBoolean(System.getProperty(Etcd3DiscoveryContainerConfig.ETCD_DISABLED_PROP, "false"))) { - context.registerService(new String[] { Etcd3DiscoveryContainerConfig.class.getName()},Etcd3DiscoveryContainerConfig.newBuilder().build(),null); + logger.debug("starting Etcd3 discovery provider"); + @SuppressWarnings("rawtypes") + final Hashtable props = new Hashtable(); + props.put(IDiscoveryLocator.CONTAINER_NAME, Etcd3DiscoveryContainerInstantiator.NAME); + context.registerService( + new String[] { IDiscoveryAdvertiser.class.getName(), IDiscoveryLocator.class.getName() }, + new ServiceFactory() { + public Etcd3DiscoveryContainer getService(Bundle bundle, + ServiceRegistration registration) { + if (container == null) { + try { + container = (Etcd3DiscoveryContainer) getContainerFactory() + .createContainer(Etcd3DiscoveryContainerInstantiator.NAME); + logger.debug("Etcd3 discovery container created with name="+ Etcd3DiscoveryContainerInstantiator.NAME); + } catch (ContainerCreateException e) { + logger.error("Could not create Etcd3 discovery="+ Etcd3DiscoveryContainerInstantiator.NAME, e); + container = null; + } + } + // Connect + try { + container.connect(null, null); + logger.debug("Etcd3 discovery container connected with name="+ Etcd3DiscoveryContainerInstantiator.NAME); + } catch (ContainerConnectException e) { + logger.error("Could not connect Etcd3 discovery="+ Etcd3DiscoveryContainerInstantiator.NAME, e); + container = null; + } + return container; + } + + public void ungetService(Bundle bundle, + ServiceRegistration registration, + Etcd3DiscoveryContainer service) { + if (container != null) { + container.disconnect(); + container = null; + } + } + }, props); } else { logger.debug("Etcd3 discovery provider DISABLED"); } @@ -73,4 +121,14 @@ public void stop(BundleContext context) throws Exception { plugin = null; } + @SuppressWarnings("unchecked") + IContainerFactory getContainerFactory() { + if (cfTracker == null) { + cfTracker = new ServiceTracker(context, IContainerFactory.class, + null); + cfTracker.open(); + } + return (IContainerFactory) cfTracker.getService(); + } + }