Created
November 5, 2013 16:16
-
-
Save vongohren/7321523 to your computer and use it in GitHub Desktop.
ManagedServiceFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ntnu.opcua.client.service.impl; | |
import java.util.Dictionary; | |
import java.util.Enumeration; | |
import java.util.HashMap; | |
import java.util.Map; | |
import ntnu.opcua.client.api.OPCUAClientAPI; | |
import org.osgi.framework.BundleContext; | |
import org.osgi.framework.ServiceReference; | |
import org.osgi.framework.ServiceRegistration; | |
import org.osgi.service.cm.ConfigurationException; | |
import org.osgi.service.cm.ManagedServiceFactory; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
public class OPCUAClientManagedServiceFactory implements ManagedServiceFactory { | |
Logger logger = LoggerFactory.getLogger("console"); | |
private int usageCounter; | |
Map<String, ServiceRegistration> existingServices = new HashMap<String, ServiceRegistration>(); | |
private String name; | |
private BundleContext context; | |
public OPCUAClientManagedServiceFactory(BundleContext context) { | |
this.context = context; | |
this.name = "test"; | |
} | |
@Override | |
public void updated(String pid, Dictionary properties) throws ConfigurationException { | |
logger.debug("UPDATEING "+pid); | |
if (existingServices.containsKey(pid)) { | |
logger.debug("ALREADY CONTAINS THIS PID "+pid+" Does Nothing"); | |
} | |
else { | |
logger.debug("NOT CONTAINING PID, ADDING "+pid); | |
usageCounter++; | |
Enumeration<String> test = properties.keys(); | |
logger.debug("PRINTING THE PROPERTY KEYS"); | |
while(test.hasMoreElements()) { | |
String key = test.nextElement(); | |
logger.debug(key); | |
logger.debug(properties.get(key).toString()); | |
} | |
logger.debug("PROPERTY HARDWARE "+ properties.get("hardware")); | |
OPCUAClient client = new OPCUAClient(properties.get("hardware").toString()); | |
existingServices.put(pid, registerService(client, properties)); | |
} | |
} | |
public ServiceRegistration registerService(OPCUAClient client, Dictionary props) { | |
ServiceRegistration reg = context.registerService(OPCUAClientAPI.class.getName(), client, props); | |
return reg; | |
} | |
@Override | |
public void deleted(String pid) { | |
logger.debug("REMOVING EXISTING PID "+pid); | |
existingServices.get(pid).unregister(); | |
existingServices.remove(pid); | |
} | |
@Override | |
public String getName() { | |
return this.getClass().getName(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment