diff options
5 files changed, 368 insertions, 7 deletions
diff --git a/providers/bundles/org.eclipse.ecf.provider.r_osgi/src/org/eclipse/ecf/internal/provider/r_osgi/R_OSGiContainerInstantiator.java b/providers/bundles/org.eclipse.ecf.provider.r_osgi/src/org/eclipse/ecf/internal/provider/r_osgi/R_OSGiContainerInstantiator.java index 9fbc047d9..89e9e7090 100644 --- a/providers/bundles/org.eclipse.ecf.provider.r_osgi/src/org/eclipse/ecf/internal/provider/r_osgi/R_OSGiContainerInstantiator.java +++ b/providers/bundles/org.eclipse.ecf.provider.r_osgi/src/org/eclipse/ecf/internal/provider/r_osgi/R_OSGiContainerInstantiator.java @@ -37,6 +37,13 @@ public final class R_OSGiContainerInstantiator implements IContainerInstantiator return (R_OSGiID) IDFactory.getDefault().createID(namespace, uriStr); } + public static final String ID_PROP = "id"; //$NON-NLS-1$ + + private static final String WS_PROTOCOL = "http"; //$NON-NLS-1$ + private static final String WSS_PROTOCOL = "https"; //$NON-NLS-1$ + private static final int WS_DEFAULT_PORT = 80; + private static final int WSS_DEFAULT_PORT = 443; + /** * creates a new container instance. * @@ -59,15 +66,28 @@ public final class R_OSGiContainerInstantiator implements IContainerInstantiator if (parameters == null) { //TODO factor localHost and protocol out? final String localHost = InetAddress.getLocalHost().getCanonicalHostName(); - final String protocol = ns.getScheme(); - - final int port = remoteOSGiService.getListeningPort(protocol); - containerID = createR_OSGiID(ns, new String(protocol + "://" + localHost + ":" + port)); //$NON-NLS-1$ //$NON-NLS-2$ + final String nsScheme = ns.getScheme(); + final String wsProtocol = (wss ? WSS_PROTOCOL : (ws ? WS_PROTOCOL : null)); + int listeningPort = remoteOSGiService.getListeningPort((wsProtocol != null) ? wsProtocol : nsScheme); + int idPort = -1; + if (WSS_PROTOCOL.equals(wsProtocol) && listeningPort != WSS_DEFAULT_PORT) + idPort = listeningPort; + else if (WS_PROTOCOL.equals(wsProtocol) && listeningPort != WS_DEFAULT_PORT) + idPort = listeningPort; + String portStr = (idPort > 0 ? (":" + idPort) : ""); //$NON-NLS-1$ //$NON-NLS-2$ + containerID = createR_OSGiID(ns, new String(nsScheme + "://" + localHost + portStr)); //$NON-NLS-1$ } else if (parameters.length > 0) { if (parameters[0] instanceof ID) containerID = (ID) parameters[0]; else if (parameters[0] instanceof String) containerID = createR_OSGiID(ns, (String) parameters[0]); + else if (parameters[0] instanceof Map) { + Map params = (Map) parameters[0]; + String idStr = (String) params.get(ID_PROP); + if (idStr == null) + throw new NullPointerException("No ID prop found in parameters map"); //$NON-NLS-1$ + containerID = createR_OSGiID(ns, idStr); + } } if (containerID == null) throw new ContainerCreateException("Unsupported arguments " //$NON-NLS-1$ @@ -123,10 +143,8 @@ public final class R_OSGiContainerInstantiator implements IContainerInstantiator public static final String NAME_HTTP = ROSGI_WEBSOCKETS_CONFIG; public static final String NAME_HTTPS = ROSGI_WEBSOCKETSS_CONFIG; - private static final String[] ROSGI_CONFIGS = new String[] {ROSGI_CONFIG, ROSGI_WEBSOCKETS_CONFIG, ROSGI_WEBSOCKETSS_CONFIG}; - public String[] getSupportedConfigs(ContainerTypeDescription description) { - return ROSGI_CONFIGS; + return new String[] {description.getName()}; } public String[] getImportedConfigs(ContainerTypeDescription description, String[] exporterSupportedConfigs) { diff --git a/providers/bundles/org.eclipse.ecf.provider.r_osgi/src/org/eclipse/ecf/internal/provider/r_osgi/R_OSGiWSRemoteServiceContainer.java b/providers/bundles/org.eclipse.ecf.provider.r_osgi/src/org/eclipse/ecf/internal/provider/r_osgi/R_OSGiWSRemoteServiceContainer.java new file mode 100644 index 000000000..a0b94c785 --- /dev/null +++ b/providers/bundles/org.eclipse.ecf.provider.r_osgi/src/org/eclipse/ecf/internal/provider/r_osgi/R_OSGiWSRemoteServiceContainer.java @@ -0,0 +1,28 @@ +/******************************************************************************* + * Copyright (c) 2015 Composent, Inc. All rights reserved. This + * program and the accompanying materials are made available under the terms of + * the Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: Scott Lewis - initial API and implementation + ******************************************************************************/ +package org.eclipse.ecf.internal.provider.r_osgi; + +import ch.ethz.iks.r_osgi.RemoteOSGiService; +import org.eclipse.ecf.core.identity.*; +import org.eclipse.ecf.provider.r_osgi.identity.R_OSGiWSNamespace; +import org.eclipse.ecf.provider.r_osgi.identity.R_OSGiWSSNamespace; + +class R_OSGiWSRemoteServiceContainer extends R_OSGiRemoteServiceContainer { + + private final boolean secure; + + public R_OSGiWSRemoteServiceContainer(RemoteOSGiService service, ID containerID, boolean secure) throws IDCreateException { + super(service, containerID); + this.secure = secure; + } + + public Namespace getConnectNamespace() { + return IDFactory.getDefault().getNamespaceByName(secure ? R_OSGiWSSNamespace.NAME_WSS : R_OSGiWSNamespace.NAME_WS); + } +} diff --git a/providers/bundles/org.eclipse.ecf.provider.r_osgi/src/org/eclipse/ecf/provider/r_osgi/identity/R_OSGiWSID.java b/providers/bundles/org.eclipse.ecf.provider.r_osgi/src/org/eclipse/ecf/provider/r_osgi/identity/R_OSGiWSID.java new file mode 100644 index 000000000..241b0c689 --- /dev/null +++ b/providers/bundles/org.eclipse.ecf.provider.r_osgi/src/org/eclipse/ecf/provider/r_osgi/identity/R_OSGiWSID.java @@ -0,0 +1,119 @@ +/******************************************************************************* + * Copyright (c) 2015 Composent, Inc. All rights reserved. This + * program and the accompanying materials are made available under the terms of + * the Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: Scott Lewis - initial API and implementation + ******************************************************************************/ +package org.eclipse.ecf.provider.r_osgi.identity; + +import ch.ethz.iks.r_osgi.URI; +import org.eclipse.ecf.core.identity.BaseID; + +/** + * @since 3.5 + */ +public class R_OSGiWSID extends R_OSGiID { + + public static final int HTTPS_PORT = 443; + public static final int HTTP_PORT = 80; + + private static final long serialVersionUID = -2801506059914687609L; + + private boolean secure; + private String hostname; + private int port; + private String name; + + public R_OSGiWSID(boolean secure, String hostname, int port) { + super(secure ? R_OSGiWSSNamespace.getDefault() : R_OSGiWSNamespace.getDefault()); + this.secure = secure; + this.hostname = hostname; + this.port = port; + this.name = getNamespace().getScheme() + "://" + this.hostname + getPortAsString(); //$NON-NLS-1$ + } + + private String getPortAsString() { + return isDefaultPort() ? "" : ":" + String.valueOf(this.port); //$NON-NLS-1$ //$NON-NLS-2$ + } + + private boolean isDefaultPort() { + if (this.port < 0) + return true; + return secure ? this.port == HTTPS_PORT : this.port == HTTP_PORT; + } + + /** + * compare in the context of the namespace. + * + * @param id + * another <code>BaseID</code> to compare to. + * @return -1 if smaller, 1 if larger, and 0 for equality. + * @see org.eclipse.ecf.core.identity.BaseID#namespaceCompareTo(org.eclipse.ecf.core.identity.BaseID) + */ + protected int namespaceCompareTo(final BaseID id) { + return getName().compareTo(id.getName()); + } + + /** + * check for equality in the context of the namespace. + * + * @param id + * another <code>BaseID</code> to check with. + * @return <code>true</code> iff the two IDs are equal within the given + * namespace. + * @see org.eclipse.ecf.core.identity.BaseID#namespaceEquals(org.eclipse.ecf.core.identity.BaseID) + */ + protected boolean namespaceEquals(final BaseID id) { + if (id instanceof R_OSGiWSID) { + final R_OSGiWSID other = (R_OSGiWSID) id; + return name.equals(other.name); + } + return false; + } + + /** + * get the internal URI. + * + * @return the internal R-OSGi URI. + */ + public URI getURI() { + return URI.create((this.secure ? "https" : "http") + "://" + this.hostname + getPortAsString()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + } + + /** + * get the name. + * + * @return the name as a String. + */ + protected String namespaceGetName() { + return name; + } + + /** + * get the hash code. + * + * @return the hash code. + */ + protected int namespaceHashCode() { + return getName().hashCode(); + } + + /** + * get a string representation. + * + * @see java.lang.Object#toString() + */ + public String toString() { + return getName(); + } + + /** + * @see org.eclipse.ecf.core.identity.ID#toExternalForm() + * @since 3.0 + */ + public String toExternalForm() { + return getName(); + } +} diff --git a/providers/bundles/org.eclipse.ecf.provider.r_osgi/src/org/eclipse/ecf/provider/r_osgi/identity/R_OSGiWSNamespace.java b/providers/bundles/org.eclipse.ecf.provider.r_osgi/src/org/eclipse/ecf/provider/r_osgi/identity/R_OSGiWSNamespace.java new file mode 100644 index 000000000..ddccc3fb4 --- /dev/null +++ b/providers/bundles/org.eclipse.ecf.provider.r_osgi/src/org/eclipse/ecf/provider/r_osgi/identity/R_OSGiWSNamespace.java @@ -0,0 +1,98 @@ +/******************************************************************************* + * Copyright (c) 2015 Composent, Inc. All rights reserved. This + * program and the accompanying materials are made available under the terms of + * the Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: Scott Lewis - initial API and implementation + ******************************************************************************/ +package org.eclipse.ecf.provider.r_osgi.identity; + +import java.net.URI; +import java.net.URISyntaxException; +import org.eclipse.ecf.core.identity.*; + +/** + * @since 3.5 + */ +public class R_OSGiWSNamespace extends R_OSGiNamespace { + + private static final long serialVersionUID = -3460085239213524498L; + + public static final String NAME_WS = "ecf.namespace.r_osgi.ws"; //$NON-NLS-1$ + + /** + * the namespace scheme. + */ + private static final String NAMESPACE_SCHEME_WS = "r-osgi.ws"; //$NON-NLS-1$ + + /** + * the singleton instance of this namespace. + */ + private static Namespace instance; + + /** + * get the singleton instance of this namespace. + * + * @return the instance. + */ + public static Namespace getDefault() { + if (instance == null) { + new R_OSGiWSNamespace(); + } + return instance; + } + + /** + * constructor. + */ + public R_OSGiWSNamespace() { + super(NAME_WS, "R-OSGi Http Namespace"); //$NON-NLS-1$ + instance = this; + } + + /** + * create a new ID within this namespace. + * + * @param parameters + * the parameter to pass to the ID. + * @return the new ID + * @throws IDCreateException + * if the creation fails. + * @see org.eclipse.ecf.core.identity.Namespace#createInstance(java.lang.Object[]) + */ + public ID createInstance(final Object[] parameters) throws IDCreateException { + try { + String uriString = (String) parameters[0]; + if (uriString == null) + throw new NullPointerException("URI parameter is null"); //$NON-NLS-1$ + if (!uriString.startsWith(NAMESPACE_SCHEME_WS) && !uriString.startsWith("http")) //$NON-NLS-1$ + throw new URISyntaxException(uriString, "URI must have " + NAMESPACE_SCHEME_WS + " as protocol"); //$NON-NLS-1$ //$NON-NLS-2$ + URI uri = new URI(uriString); + return new R_OSGiWSID(false, uri.getHost(), uri.getPort()); + } catch (Exception e) { + throw new IDCreateException(getName() + " createInstance()", e); //$NON-NLS-1$ + } + } + + /** + * get the scheme of this namespace. + * + * @return the scheme. + * @see org.eclipse.ecf.core.identity.Namespace#getScheme() + */ + public String getScheme() { + return NAMESPACE_SCHEME_WS; + } + + /** + * get all supported schemes. + * + * @return an array of supported schemes. + * @see org.eclipse.ecf.core.identity.Namespace#getSupportedSchemes() + */ + public String[] getSupportedSchemes() { + return new String[] {NAMESPACE_SCHEME_WS}; + } + +} diff --git a/providers/bundles/org.eclipse.ecf.provider.r_osgi/src/org/eclipse/ecf/provider/r_osgi/identity/R_OSGiWSSNamespace.java b/providers/bundles/org.eclipse.ecf.provider.r_osgi/src/org/eclipse/ecf/provider/r_osgi/identity/R_OSGiWSSNamespace.java new file mode 100644 index 000000000..345735b04 --- /dev/null +++ b/providers/bundles/org.eclipse.ecf.provider.r_osgi/src/org/eclipse/ecf/provider/r_osgi/identity/R_OSGiWSSNamespace.java @@ -0,0 +1,98 @@ +/******************************************************************************* + * Copyright (c) 2015 Composent, Inc. All rights reserved. This + * program and the accompanying materials are made available under the terms of + * the Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: Scott Lewis - initial API and implementation + ******************************************************************************/ +package org.eclipse.ecf.provider.r_osgi.identity; + +import java.net.URI; +import java.net.URISyntaxException; +import org.eclipse.ecf.core.identity.*; + +/** + * @since 3.5 + */ +public class R_OSGiWSSNamespace extends R_OSGiNamespace { + + private static final long serialVersionUID = 8705667690964282332L; + + public static final String NAME_WSS = "ecf.namespace.r_osgi.wss"; //$NON-NLS-1$ + + /** + * the namespace scheme. + */ + private static final String NAMESPACE_SCHEME_WSS = "r-osgi.wss"; //$NON-NLS-1$ + + /** + * the singleton instance of this namespace. + */ + private static Namespace instance; + + /** + * get the singleton instance of this namespace. + * + * @return the instance. + */ + public static Namespace getDefault() { + if (instance == null) { + new R_OSGiWSSNamespace(); + } + return instance; + } + + /** + * constructor. + */ + public R_OSGiWSSNamespace() { + super(NAME_WSS, "R-OSGi Secure Websockets Namespace"); //$NON-NLS-1$ + instance = this; + } + + /** + * create a new ID within this namespace. + * + * @param parameters + * the parameter to pass to the ID. + * @return the new ID + * @throws IDCreateException + * if the creation fails. + * @see org.eclipse.ecf.core.identity.Namespace#createInstance(java.lang.Object[]) + */ + public ID createInstance(final Object[] parameters) throws IDCreateException { + try { + String uriString = (String) parameters[0]; + if (uriString == null) + throw new NullPointerException("URI parameter is null"); //$NON-NLS-1$ + if (!uriString.startsWith(NAMESPACE_SCHEME_WSS) && !uriString.startsWith("https")) //$NON-NLS-1$ + throw new URISyntaxException(uriString, "URI must have " + NAMESPACE_SCHEME_WSS + " as protocol"); //$NON-NLS-1$ //$NON-NLS-2$ + URI uri = new URI(uriString); + return new R_OSGiWSID(true, uri.getHost(), uri.getPort()); + } catch (Exception e) { + throw new IDCreateException(getName() + " createInstance()", e); //$NON-NLS-1$ + } + } + + /** + * get the scheme of this namespace. + * + * @return the scheme. + * @see org.eclipse.ecf.core.identity.Namespace#getScheme() + */ + public String getScheme() { + return NAMESPACE_SCHEME_WSS; + } + + /** + * get all supported schemes. + * + * @return an array of supported schemes. + * @see org.eclipse.ecf.core.identity.Namespace#getSupportedSchemes() + */ + public String[] getSupportedSchemes() { + return new String[] {NAMESPACE_SCHEME_WSS}; + } + +} |