use of ch.ethz.iks.slp.ServiceURL in project ecf by eclipse.
the class JSLPDiscoveryJob method run.
/*
* (non-Javadoc)
*
* @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
*/
protected IStatus run(final IProgressMonitor monitor) {
Assert.isNotNull(monitor);
try {
final Map availableServices = Activator.getDefault().getLocator().getServiceURLs();
final Map removedServices = new HashMap(services);
for (final Iterator itr = availableServices.entrySet().iterator(); itr.hasNext() && !monitor.isCanceled(); ) {
final Map.Entry entry = (Map.Entry) itr.next();
final ServiceURL url = (ServiceURL) entry.getKey();
// do we know the service already?
if (removedServices.containsKey(url)) {
removedServices.remove(url);
} else {
// we don't know the service, so we need to create the
final ServicePropertiesAdapter spa = new ServicePropertiesAdapter((List) entry.getValue());
final String serviceName = spa.getServiceName() == null ? url.toString() : spa.getServiceName();
final IServiceInfo serviceInfo = new JSLPServiceInfo(serviceName, new ServiceURLAdapter(url), spa.getPriority(), spa.getWeight(), spa);
services.put(url, serviceInfo);
discoveryContainer.fireServiceTypeDiscovered(serviceInfo.getServiceID().getServiceTypeID());
discoveryContainer.fireServiceDiscovered(serviceInfo);
}
monitor.worked(1);
}
// at this point removedServices only contains stale services
for (final Iterator itr = removedServices.entrySet().iterator(); itr.hasNext() && !monitor.isCanceled(); ) {
final Map.Entry entry = (Map.Entry) itr.next();
final Object key = entry.getKey();
final IServiceInfo value = (IServiceInfo) entry.getValue();
discoveryContainer.fireServiceUndiscovered(value);
services.remove(key);
monitor.worked(1);
}
} catch (final ServiceLocationException e) {
// TODO-mkuppe if the advertiser is gone, we run into this exception
// but we have to let the listeners know about the gone services
// too
// $NON-NLS-1$
Trace.catching(Activator.PLUGIN_ID, JSLPDebugOptions.EXCEPTIONS_CATCHING, this.getClass(), "run", e);
}
// check if the JSLPDiscoveryContainer has been disconnected or disposed
if (discoveryContainer.getConnectedID() != null) {
this.schedule(JSLPDiscoveryContainer.REDISCOVER);
}
return Status.OK_STATUS;
}
use of ch.ethz.iks.slp.ServiceURL in project ecf by eclipse.
the class JSLPDiscoveryContainer method convertToIServiceInfo.
private IServiceInfo[] convertToIServiceInfo(Map serviceURLs, String[] scopes) {
List tmp = new ArrayList();
for (Iterator itr = serviceURLs.entrySet().iterator(); itr.hasNext(); ) {
Map.Entry entry = (Entry) itr.next();
ServiceURL url = (ServiceURL) entry.getKey();
ServicePropertiesAdapter spa = new ServicePropertiesAdapter((List) entry.getValue());
String serviceName = spa.getServiceName() == null ? url.toString() : spa.getServiceName();
IServiceInfo serviceInfo = new JSLPServiceInfo(serviceName, new ServiceURLAdapter(url, scopes), spa.getPriority(), spa.getWeight(), spa);
tmp.add(serviceInfo);
}
return (IServiceInfo[]) tmp.toArray(new IServiceInfo[tmp.size()]);
}
use of ch.ethz.iks.slp.ServiceURL in project ecf by eclipse.
the class JSLPNamespace method createInstance.
/* (non-Javadoc)
* @see org.eclipse.ecf.core.identity.Namespace#createInstance(java.lang.Object[])
*/
public ID createInstance(Object[] parameters) {
// error case
if (parameters == null || parameters.length < 1 || parameters.length > 2) {
// $NON-NLS-1$
throw new IDCreateException("Parameters cannot be null and must be of length 1 or 2");
// error case
} else if (parameters[0] == null || parameters[0].equals("")) {
// $NON-NLS-1$
throw new IDCreateException("First parameter cannot be null or empty String");
// create by jSLP ServiceURL
} else if (parameters.length == 2 && parameters[0] instanceof ServiceURL) {
final ServiceURL anURL = (ServiceURL) parameters[0];
final String[] scopes = (String[]) parameters[1];
return new JSLPServiceTypeID(this, anURL, scopes);
// final String serviceName = (String) (parameters[1] != null ? parameters[1] : anURL.getHost());
// return null /*new JSLPServiceID(this, stid, serviceName)*/;
// conversion call where conversion isn't necessary
} else if (parameters.length == 1 && parameters[0] instanceof JSLPServiceID) {
return (ID) parameters[0];
// convert from IServiceID to IServiceTypeID, String
} else if (parameters.length == 1 && parameters[0] instanceof IServiceID) {
final IServiceID anId = (IServiceID) parameters[0];
final Object[] newParams = new Object[2];
newParams[0] = anId.getServiceTypeID();
newParams[1] = anId.getName();
return createInstance(newParams);
// create by ECF discovery generic IServiceTypeID (but not JSLPServiceID!!!)
} else if (parameters[0] instanceof IServiceTypeID) {
final IServiceTypeID stid = (IServiceTypeID) parameters[0];
parameters[0] = stid.getName();
return createInstance(parameters);
// create by jSLP ServiceType
} else if (parameters.length == 1 && parameters[0] instanceof ServiceType) {
return new JSLPServiceTypeID(this, (ServiceType) parameters[0]);
// return new JSLPServiceID(this, stid, (String) parameters[1]);
// create by jSLP ServiceType String representation (from external)
} else if (parameters[0] instanceof String && ((String) parameters[0]).startsWith("service:")) {
// $NON-NLS-1$
parameters[0] = new ServiceType((String) parameters[0]);
return createInstance(parameters);
// create IServiceID by ECF discovery generic String representation
} else if (parameters.length == 2 && parameters[0] instanceof String && ((String) parameters[0]).startsWith("_") && parameters[1] instanceof URI) {
// $NON-NLS-1$
final String type = (String) parameters[0];
final URI anURI = (URI) parameters[1];
final JSLPServiceTypeID serviceType = new JSLPServiceTypeID(this, new ServiceTypeID(this, type));
return new JSLPServiceID(this, serviceType, anURI);
// create IServiceTypeID by ECF discovery generic ServiceType
} else if (parameters.length == 1 && parameters[0] instanceof String && ((String) parameters[0]).startsWith("_")) {
// $NON-NLS-1$
final String type = (String) parameters[0];
return new JSLPServiceTypeID(this, new ServiceTypeID(this, type));
// error case second parameter not a String
} else if (parameters.length == 2 && parameters[1] != null && !(parameters[1] instanceof String)) {
// $NON-NLS-1$
throw new IDCreateException("Second parameter must be of type String");
// error case
} else {
// $NON-NLS-1$
throw new IDCreateException("Wrong JSLPServiceID creation parameters");
}
}
use of ch.ethz.iks.slp.ServiceURL in project ecf by eclipse.
the class JSLPServiceIDTest method testCreateByjSLPAndRemoveServicePrefix.
/* (non-Javadoc)
* @see org.eclipse.ecf.tests.discovery.identity.ServiceIDTest#testCreateServiceTypeIDFromInternalString()
*
* test from jSLP -> ECF discovery which needs to remove the first occurrence of "service:"
*/
public void testCreateByjSLPAndRemoveServicePrefix() throws ServiceLocationException {
final String internalRep = "service:foo.eclipse:bar";
final ServiceURL sUrl = new ServiceURL(internalRep + "://localhost:1234/a/path/to/something", ServiceURL.LIFETIME_PERMANENT);
final IServiceInfo serviceInfo = new JSLPServiceInfo(DiscoveryTestHelper.SERVICENAME, new ServiceURLAdapter(sUrl), DiscoveryTestHelper.PRIORITY, DiscoveryTestHelper.WEIGHT, new ServicePropertiesAdapter(new ArrayList()));
assertEquals(serviceInfo.getPriority(), DiscoveryTestHelper.PRIORITY);
assertEquals(serviceInfo.getWeight(), DiscoveryTestHelper.WEIGHT);
final IServiceID sid = serviceInfo.getServiceID();
assertEquals(serviceInfo.getServiceName(), DiscoveryTestHelper.SERVICENAME);
final IServiceTypeID stid = sid.getServiceTypeID();
String internal = stid.getInternal();
assertEquals(internalRep, internal);
assertEquals("_foo._bar._" + IServiceTypeID.DEFAULT_PROTO[0] + "." + IServiceTypeID.DEFAULT_SCOPE[0] + "._eclipse", stid.getName());
assertEquals("eclipse", stid.getNamingAuthority());
assertTrue(Arrays.equals(new String[] { "foo", "bar" }, stid.getServices()));
assertTrue(Arrays.equals(IServiceTypeID.DEFAULT_SCOPE, stid.getScopes()));
assertTrue(Arrays.equals(IServiceTypeID.DEFAULT_PROTO, stid.getProtocols()));
}
use of ch.ethz.iks.slp.ServiceURL in project ecf by eclipse.
the class ServiceURLTest method testServiceURLNamingAuthorityCustom.
public void testServiceURLNamingAuthorityCustom() throws Exception {
String urlString = "service:test.foo://http://localhost";
ServiceURL url = new ServiceURL(urlString, 0);
assertEquals(url.getServiceType().toString(), "service:test.foo");
assertEquals(url.getHost(), "localhost");
assertEquals(url.getPort(), 0);
assertEquals(url.getURLPath(), "");
assertEquals(url.getUserInfo(), "");
assertEquals(url.getProtocol(), "http");
assertEquals(url.toString(), urlString);
assertTrue("foo".equals(url.getServiceType().getNamingAuthority()));
}
Aggregations