diff options
author | Eike Stepper | 2012-06-07 10:14:51 +0000 |
---|---|---|
committer | Eike Stepper | 2012-06-07 10:14:51 +0000 |
commit | a25531ae8a5c85586fdb590c0f806be3a807b634 (patch) | |
tree | 7849f8065be919611dabd87403f9bf5a1743cee7 /plugins/org.eclipse.net4j.tcp | |
parent | b48ccc46da60cf75aef219ae635c627fb49d3cf2 (diff) | |
download | cdo-a25531ae8a5c85586fdb590c0f806be3a807b634.tar.gz cdo-a25531ae8a5c85586fdb590c0f806be3a807b634.tar.xz cdo-a25531ae8a5c85586fdb590c0f806be3a807b634.zip |
[381472] Design a repository administration API
https://bugs.eclipse.org/bugs/show_bug.cgi?id=381472
Diffstat (limited to 'plugins/org.eclipse.net4j.tcp')
7 files changed, 696 insertions, 478 deletions
diff --git a/plugins/org.eclipse.net4j.tcp/META-INF/MANIFEST.MF b/plugins/org.eclipse.net4j.tcp/META-INF/MANIFEST.MF index 9ebb194f4f..bf671b597f 100644 --- a/plugins/org.eclipse.net4j.tcp/META-INF/MANIFEST.MF +++ b/plugins/org.eclipse.net4j.tcp/META-INF/MANIFEST.MF @@ -1,7 +1,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-SymbolicName: org.eclipse.net4j.tcp;singleton:=true -Bundle-Version: 4.0.100.qualifier +Bundle-Version: 4.1.0.qualifier Bundle-Name: %pluginName Bundle-Vendor: %providerName Bundle-Localization: plugin @@ -12,13 +12,13 @@ Bundle-ClassPath: . Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.4.0,4.0.0)";resolution:=optional, org.eclipse.net4j;bundle-version="[4.0.0,5.0.0)";visibility:=reexport Import-Package: org.osgi.framework;version="[1.3.0,2.0.0)";resolution:=optional -Export-Package: org.eclipse.net4j.internal.tcp;version="4.0.100"; +Export-Package: org.eclipse.net4j.internal.tcp;version="4.1.0"; x-friends:="org.eclipse.net4j.tests, org.eclipse.net4j.defs, org.eclipse.emf.cdo.examples, org.eclipse.net4j.ui", - org.eclipse.net4j.internal.tcp.bundle;version="4.0.100";x-internal:=true, - org.eclipse.net4j.internal.tcp.messages;version="4.0.100";x-internal:=true, - org.eclipse.net4j.internal.tcp.ssl;version="4.0.100";x-friends:="org.eclipse.net4j.tests,org.eclipse.net4j.defs,org.eclipse.emf.cdo.examples", - org.eclipse.net4j.tcp;version="4.0.100", - org.eclipse.net4j.tcp.ssl;version="4.0.100" + org.eclipse.net4j.internal.tcp.bundle;version="4.1.0";x-internal:=true, + org.eclipse.net4j.internal.tcp.messages;version="4.1.0";x-internal:=true, + org.eclipse.net4j.internal.tcp.ssl;version="4.1.0";x-friends:="org.eclipse.net4j.tests,org.eclipse.net4j.defs,org.eclipse.emf.cdo.examples", + org.eclipse.net4j.tcp;version="4.1.0", + org.eclipse.net4j.tcp.ssl;version="4.1.0" diff --git a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPAcceptor.java b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPAcceptor.java index 1236e0ba04..73cbb50cd4 100644 --- a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPAcceptor.java +++ b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPAcceptor.java @@ -1,287 +1,292 @@ -/*
- * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
- * 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:
- * Eike Stepper - initial API and implementation
- * Caspar De Groot - maintenance
- */
-package org.eclipse.net4j.internal.tcp;
-
-import org.eclipse.net4j.internal.tcp.bundle.OM;
-import org.eclipse.net4j.tcp.ITCPAcceptor;
-import org.eclipse.net4j.tcp.ITCPPassiveSelectorListener;
-import org.eclipse.net4j.tcp.ITCPSelector;
-import org.eclipse.net4j.util.ReflectUtil.ExcludeFromDump;
-import org.eclipse.net4j.util.concurrent.Worker;
-import org.eclipse.net4j.util.io.IOUtil;
-import org.eclipse.net4j.util.om.trace.ContextTracer;
-
-import org.eclipse.spi.net4j.Acceptor;
-
-import java.io.IOException;
-import java.net.InetAddress;
-import java.net.InetSocketAddress;
-import java.net.ServerSocket;
-import java.nio.channels.ClosedChannelException;
-import java.nio.channels.SelectionKey;
-import java.nio.channels.ServerSocketChannel;
-import java.nio.channels.SocketChannel;
-import java.text.MessageFormat;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.TimeUnit;
-
-/**
- * @author Eike Stepper
- */
-public class TCPAcceptor extends Acceptor implements ITCPAcceptor, ITCPPassiveSelectorListener
-{
- public static final boolean DEFAULT_START_SYNCHRONOUSLY = true;
-
- public static final long DEFAULT_SYNCHRONOUS_START_TIMEOUT = 2 * Worker.DEFAULT_TIMEOUT;
-
- private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, TCPAcceptor.class);
-
- private TCPSelector selector;
-
- @ExcludeFromDump
- private SelectionKey selectionKey;
-
- private boolean startSynchronously = DEFAULT_START_SYNCHRONOUSLY;
-
- private long synchronousStartTimeout = DEFAULT_SYNCHRONOUS_START_TIMEOUT;
-
- @ExcludeFromDump
- private CountDownLatch startLatch;
-
- private ServerSocketChannel serverSocketChannel;
-
- private String address = DEFAULT_ADDRESS;
-
- private int port = DEFAULT_PORT;
-
- public TCPAcceptor()
- {
- }
-
- public String getAddress()
- {
- return address;
- }
-
- public void setAddress(String address)
- {
- this.address = address;
- }
-
- public int getPort()
- {
- return port;
- }
-
- public void setPort(int port)
- {
- this.port = port;
- }
-
- public TCPSelector getSelector()
- {
- return selector;
- }
-
- public void setSelector(TCPSelector selector)
- {
- this.selector = selector;
- }
-
- public boolean isStartSynchronously()
- {
- return startSynchronously;
- }
-
- public void setStartSynchronously(boolean startSynchronously)
- {
- this.startSynchronously = startSynchronously;
- }
-
- public SelectionKey getSelectionKey()
- {
- return selectionKey;
- }
-
- public long getSynchronousStartTimeout()
- {
- return synchronousStartTimeout;
- }
-
- public void setSynchronousStartTimeout(long synchronousStartTimeout)
- {
- this.synchronousStartTimeout = synchronousStartTimeout;
- }
-
- public void handleRegistration(ITCPSelector selector, ServerSocketChannel serverSocketChannel)
- {
- try
- {
- InetSocketAddress addr = null;
- if (address != null)
- {
- addr = new InetSocketAddress(InetAddress.getByName(address), port);
- }
-
- ServerSocket socket = serverSocketChannel.socket();
- socket.bind(addr);
-
- if (addr == null)
- {
- address = socket.getInetAddress().toString();
- if (address.startsWith("/")) //$NON-NLS-1$
- {
- address = address.substring(1);
- }
-
- int colon = address.indexOf(':');
- if (colon != -1)
- {
- port = Integer.parseInt(address.substring(colon + 1));
- address = address.substring(0, colon);
- }
- }
-
- // [MACOSX] Must occur AFTER binding!
- selectionKey = serverSocketChannel.register(selector.getSocketSelector(), SelectionKey.OP_ACCEPT, this);
- }
- catch (Exception ex)
- {
- OM.LOG.error(ex);
- deactivateAsync();
- }
- finally
- {
- if (startLatch != null)
- {
- startLatch.countDown();
- }
- }
- }
-
- public void handleAccept(ITCPSelector selector, ServerSocketChannel serverSocketChannel)
- {
- try
- {
- SocketChannel socketChannel = serverSocketChannel.accept();
- if (socketChannel != null)
- {
- if (TRACER.isEnabled())
- {
- TRACER.trace("Accepted socketChannel " + socketChannel); //$NON-NLS-1$
- }
-
- // socketChannel.socket().setReuseAddress(true);
- // socketChannel.socket().setKeepAlive(true);
- socketChannel.configureBlocking(false);
-
- TCPConnector connector = createConnector();
- prepareConnector(connector);
- connector.setSocketChannel(socketChannel);
- connector.setSelector(selector);
- connector.activate();
- }
- }
- catch (ClosedChannelException ex)
- {
- deactivateAsync();
- }
- catch (Exception ex)
- {
- if (isActive())
- {
- OM.LOG.error(ex);
- }
-
- deactivateAsync();
- }
- }
-
- @Override
- public String toString()
- {
- return MessageFormat.format("TCPAcceptor[{0}:{1}]", address, port); //$NON-NLS-1$
- }
-
- protected TCPConnector createConnector()
- {
- return new TCPServerConnector(this);
- }
-
- @Override
- protected void doBeforeActivate() throws Exception
- {
- super.doBeforeActivate();
- if (selector == null)
- {
- throw new IllegalStateException("selector == null"); //$NON-NLS-1$
- }
-
- if (startSynchronously)
- {
- startLatch = new CountDownLatch(1);
- }
- }
-
- @Override
- protected void doActivate() throws Exception
- {
- super.doActivate();
- serverSocketChannel = ServerSocketChannel.open();
- serverSocketChannel.configureBlocking(false);
- selector.orderRegistration(serverSocketChannel, this);
-
- if (startLatch != null)
- {
- if (!startLatch.await(synchronousStartTimeout, TimeUnit.MILLISECONDS))
- {
- startLatch = null;
- IOUtil.closeSilent(serverSocketChannel);
- throw new IOException("Registration with selector timed out after " + synchronousStartTimeout + " millis"); //$NON-NLS-1$ //$NON-NLS-2$
- }
- }
- }
-
- @Override
- protected void doDeactivate() throws Exception
- {
- cancelSelectionKey();
-
- IOUtil.closeSilent(serverSocketChannel);
- serverSocketChannel = null;
- super.doDeactivate();
- }
-
- protected void deactivateAsync()
- {
- // Cancel the selection immediately
- cancelSelectionKey();
-
- // Do the rest of the deactivation asynchronously
- getConfig().getReceiveExecutor().execute(new Runnable()
- {
- public void run()
- {
- deactivate();
- }
- });
- }
-
- private void cancelSelectionKey()
- {
- if (selectionKey != null)
- {
- selectionKey.cancel();
- selectionKey = null;
- }
- }
-}
+/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others. + * 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: + * Eike Stepper - initial API and implementation + * Caspar De Groot - maintenance + */ +package org.eclipse.net4j.internal.tcp; + +import org.eclipse.net4j.internal.tcp.bundle.OM; +import org.eclipse.net4j.tcp.ITCPAcceptor; +import org.eclipse.net4j.tcp.ITCPPassiveSelectorListener; +import org.eclipse.net4j.tcp.ITCPSelector; +import org.eclipse.net4j.util.ReflectUtil.ExcludeFromDump; +import org.eclipse.net4j.util.concurrent.Worker; +import org.eclipse.net4j.util.io.IOUtil; +import org.eclipse.net4j.util.om.trace.ContextTracer; + +import org.eclipse.spi.net4j.Acceptor; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.nio.channels.ClosedChannelException; +import java.nio.channels.SelectionKey; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; +import java.text.MessageFormat; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +/** + * @author Eike Stepper + */ +public class TCPAcceptor extends Acceptor implements ITCPAcceptor, ITCPPassiveSelectorListener +{ + public static final boolean DEFAULT_START_SYNCHRONOUSLY = true; + + public static final long DEFAULT_SYNCHRONOUS_START_TIMEOUT = Worker.DEFAULT_TIMEOUT; + + private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, TCPAcceptor.class); + + private TCPSelector selector; + + @ExcludeFromDump + private SelectionKey selectionKey; + + private boolean startSynchronously = DEFAULT_START_SYNCHRONOUSLY; + + private long synchronousStartTimeout = DEFAULT_SYNCHRONOUS_START_TIMEOUT; + + @ExcludeFromDump + private CountDownLatch startLatch; + + private ServerSocketChannel serverSocketChannel; + + private String address = DEFAULT_ADDRESS; + + private int port = DEFAULT_PORT; + + public TCPAcceptor() + { + } + + public String getAddress() + { + return address; + } + + public void setAddress(String address) + { + this.address = address; + } + + public int getPort() + { + return port; + } + + public void setPort(int port) + { + this.port = port; + } + + public TCPSelector getSelector() + { + return selector; + } + + public void setSelector(TCPSelector selector) + { + this.selector = selector; + } + + public boolean isStartSynchronously() + { + return startSynchronously; + } + + public void setStartSynchronously(boolean startSynchronously) + { + this.startSynchronously = startSynchronously; + } + + public SelectionKey getSelectionKey() + { + return selectionKey; + } + + public long getSynchronousStartTimeout() + { + return synchronousStartTimeout; + } + + public void setSynchronousStartTimeout(long synchronousStartTimeout) + { + this.synchronousStartTimeout = synchronousStartTimeout; + } + + public void handleRegistration(ITCPSelector selector, ServerSocketChannel serverSocketChannel) + { + try + { + InetSocketAddress addr = null; + if (address != null) + { + addr = new InetSocketAddress(InetAddress.getByName(address), port); + } + + ServerSocket socket = serverSocketChannel.socket(); + socket.bind(addr); + + if (addr == null) + { + address = socket.getInetAddress().toString(); + if (address.startsWith("/")) //$NON-NLS-1$ + { + address = address.substring(1); + } + + int colon = address.indexOf(':'); + if (colon != -1) + { + port = Integer.parseInt(address.substring(colon + 1)); + address = address.substring(0, colon); + } + } + + // [MACOSX] Must occur AFTER binding! + selectionKey = serverSocketChannel.register(selector.getSocketSelector(), SelectionKey.OP_ACCEPT, this); + } + catch (Exception ex) + { + OM.LOG.error(ex); + deactivateAsync(); + } + finally + { + if (startLatch != null) + { + startLatch.countDown(); + } + } + } + + public void handleAccept(ITCPSelector selector, ServerSocketChannel serverSocketChannel) + { + try + { + SocketChannel socketChannel = serverSocketChannel.accept(); + if (socketChannel != null) + { + if (TRACER.isEnabled()) + { + TRACER.trace("Accepted socketChannel " + socketChannel); //$NON-NLS-1$ + } + + // socketChannel.socket().setReuseAddress(true); + // socketChannel.socket().setKeepAlive(true); + socketChannel.configureBlocking(false); + + TCPConnector connector = createConnector(); + prepareConnector(connector); + connector.setSocketChannel(socketChannel); + connector.setSelector(selector); + connector.activate(); + } + } + catch (ClosedChannelException ex) + { + deactivateAsync(); + } + catch (Exception ex) + { + if (isActive()) + { + OM.LOG.error(ex); + } + + deactivateAsync(); + } + } + + @Override + public String toString() + { + return MessageFormat.format("TCPAcceptor[{0}:{1}]", address, port); //$NON-NLS-1$ + } + + protected TCPConnector createConnector() + { + return new TCPServerConnector(this); + } + + @Override + protected void doBeforeActivate() throws Exception + { + super.doBeforeActivate(); + if (selector == null) + { + throw new IllegalStateException("selector == null"); //$NON-NLS-1$ + } + + if (startSynchronously) + { + startLatch = new CountDownLatch(1); + } + } + + @Override + protected void doActivate() throws Exception + { + super.doActivate(); + serverSocketChannel = ServerSocketChannel.open(); + serverSocketChannel.configureBlocking(false); + selector.orderRegistration(serverSocketChannel, this); + + if (startLatch != null) + { + if (!startLatch.await(synchronousStartTimeout, TimeUnit.MILLISECONDS)) + { + startLatch = null; + IOUtil.closeSilent(serverSocketChannel); + throw new IOException("Registration with selector timed out after " + synchronousStartTimeout + " millis"); //$NON-NLS-1$ //$NON-NLS-2$ + } + } + } + + @Override + protected void doDeactivate() throws Exception + { + if (startLatch != null) + { + startLatch.countDown(); + } + + cancelSelectionKey(); + + IOUtil.closeSilent(serverSocketChannel); + serverSocketChannel = null; + super.doDeactivate(); + } + + protected void deactivateAsync() + { + // Cancel the selection immediately + cancelSelectionKey(); + + // Do the rest of the deactivation asynchronously + getConfig().getReceiveExecutor().execute(new Runnable() + { + public void run() + { + deactivate(); + } + }); + } + + private void cancelSelectionKey() + { + if (selectionKey != null) + { + selectionKey.cancel(); + selectionKey = null; + } + } +} diff --git a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPAcceptorFactory.java b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPAcceptorFactory.java index 4d67e9f778..76fb6c8b32 100644 --- a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPAcceptorFactory.java +++ b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPAcceptorFactory.java @@ -12,7 +12,8 @@ package org.eclipse.net4j.internal.tcp; import org.eclipse.net4j.tcp.ITCPAcceptor; -import org.eclipse.net4j.util.StringUtil; +import org.eclipse.net4j.tcp.TCPUtil; +import org.eclipse.net4j.tcp.TCPUtil.AcceptorData; import org.eclipse.spi.net4j.AcceptorFactory; @@ -21,13 +22,9 @@ import org.eclipse.spi.net4j.AcceptorFactory; */ public class TCPAcceptorFactory extends AcceptorFactory { - public static final String TYPE = "tcp"; //$NON-NLS-1$ - - private static final String SEPARATOR = ":"; //$NON-NLS-1$ - public TCPAcceptorFactory() { - super(TYPE); + super(TCPUtil.FACTORY_TYPE); } /** @@ -40,7 +37,7 @@ public class TCPAcceptorFactory extends AcceptorFactory public TCPAcceptor create(String description) { - Data data = new Data(description); + AcceptorData data = new AcceptorData(description); TCPAcceptor acceptor = createAcceptor(); acceptor.setAddress(data.getAddress()); @@ -56,65 +53,12 @@ public class TCPAcceptorFactory extends AcceptorFactory @Override public String getDescriptionFor(Object object) { - if (object instanceof TCPAcceptor) + if (object instanceof ITCPAcceptor) { - TCPAcceptor acceptor = (TCPAcceptor)object; - return acceptor.getAddress() + SEPARATOR + acceptor.getPort(); + ITCPAcceptor acceptor = (ITCPAcceptor)object; + return new AcceptorData(acceptor).toString(); } return null; } - - /** - * @author Eike Stepper - */ - public static class Data - { - private String address = ITCPAcceptor.DEFAULT_ADDRESS; - - private int port = ITCPAcceptor.DEFAULT_PORT; - - public Data() - { - } - - public Data(String address, int port) - { - this.address = address; - this.port = port; - } - - public Data(String description) - { - if (!StringUtil.isEmpty(description)) - { - String[] tokens = description.split(SEPARATOR); - if (!StringUtil.isEmpty(tokens[0])) - { - address = tokens[0]; - } - - if (tokens.length > 1 && !StringUtil.isEmpty(tokens[1])) - { - port = Integer.parseInt(tokens[1]); - } - } - } - - public String getAddress() - { - return address == null ? "" : address; - } - - public int getPort() - { - return port; - } - - @Override - public String toString() - { - return address + SEPARATOR + port; - } - } } diff --git a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPConnectorFactory.java b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPConnectorFactory.java index 3982bedbdd..8f1983b847 100644 --- a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPConnectorFactory.java +++ b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/internal/tcp/TCPConnectorFactory.java @@ -12,14 +12,10 @@ package org.eclipse.net4j.internal.tcp; import org.eclipse.net4j.tcp.ITCPConnector; -import org.eclipse.net4j.util.StringUtil; -import org.eclipse.net4j.util.factory.ProductCreationException; +import org.eclipse.net4j.tcp.TCPUtil.ConnectorData; import org.eclipse.spi.net4j.ConnectorFactory; -import java.net.MalformedURLException; -import java.net.URL; - /** * @author Eike Stepper */ @@ -42,29 +38,14 @@ public class TCPConnectorFactory extends ConnectorFactory public TCPConnector create(String description) { - try - { - // TODO Don't use URL - // Scheme "tcp://" would be rejected! - URL url = new URL("http://" + description); - String userID = url.getUserInfo(); - String host = url.getHost(); - int port = url.getPort(); - if (port == -1) - { - port = ITCPConnector.DEFAULT_PORT; - } + ConnectorData data = new ConnectorData(description); + + TCPConnector connector = createConnector(); + connector.setHost(data.getHost()); + connector.setPort(data.getPort()); + connector.setUserID(data.getUserID()); + return connector; - TCPConnector connector = createConnector(); - connector.setUserID(userID); - connector.setHost(host); - connector.setPort(port); - return connector; - } - catch (MalformedURLException ex) - { - throw new ProductCreationException(ex); - } } protected TCPConnector createConnector() @@ -75,23 +56,10 @@ public class TCPConnectorFactory extends ConnectorFactory @Override public String getDescriptionFor(Object object) { - if (object instanceof TCPConnector) + if (object instanceof ITCPConnector) { - TCPConnector connector = (TCPConnector)object; - String description = connector.getHost(); - String userID = connector.getUserID(); - if (!StringUtil.isEmpty(userID)) - { - description = userID + "@" + description; //$NON-NLS-1$ - } - - int port = connector.getPort(); - if (port != ITCPConnector.DEFAULT_PORT) - { - description = description + ":" + port; //$NON-NLS-1$ - } - - return description; + ITCPConnector connector = (ITCPConnector)object; + return new ConnectorData(connector).toString(); } return null; diff --git a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/tcp/TCPUtil.java b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/tcp/TCPUtil.java index ea99a9e791..57350f617d 100644 --- a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/tcp/TCPUtil.java +++ b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/tcp/TCPUtil.java @@ -1,48 +1,347 @@ -/*
- * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
- * 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:
- * Eike Stepper - initial API and implementation
- */
-package org.eclipse.net4j.tcp;
-
-import org.eclipse.net4j.internal.tcp.TCPAcceptorFactory;
-import org.eclipse.net4j.internal.tcp.TCPConnectorFactory;
-import org.eclipse.net4j.internal.tcp.TCPSelectorFactory;
-import org.eclipse.net4j.internal.tcp.TCPSelectorInjector;
-import org.eclipse.net4j.util.container.IManagedContainer;
-
-/**
- * A utility class with static convenience methods.
- *
- * @author Eike Stepper
- */
-public final class TCPUtil
-{
- private TCPUtil()
- {
- }
-
- public static void prepareContainer(IManagedContainer container)
- {
- container.registerFactory(new TCPSelectorFactory());
- container.registerFactory(new TCPAcceptorFactory());
- container.registerFactory(new TCPConnectorFactory());
- container.addPostProcessor(new TCPSelectorInjector());
- }
-
- public static ITCPAcceptor getAcceptor(IManagedContainer container, String description)
- {
- return (ITCPAcceptor)container.getElement(TCPAcceptorFactory.PRODUCT_GROUP, TCPAcceptorFactory.TYPE, description);
- }
-
- public static ITCPConnector getConnector(IManagedContainer container, String description)
- {
- return (ITCPConnector)container
- .getElement(TCPConnectorFactory.PRODUCT_GROUP, TCPConnectorFactory.TYPE, description);
- }
-}
+/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others. + * 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: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.tcp; + +import org.eclipse.net4j.internal.tcp.TCPAcceptorFactory; +import org.eclipse.net4j.internal.tcp.TCPConnectorFactory; +import org.eclipse.net4j.internal.tcp.TCPSelectorFactory; +import org.eclipse.net4j.internal.tcp.TCPSelectorInjector; +import org.eclipse.net4j.util.StringUtil; +import org.eclipse.net4j.util.container.IManagedContainer; +import org.eclipse.net4j.util.factory.ProductCreationException; + +import java.net.MalformedURLException; +import java.net.URL; + +/** + * A utility class with static convenience methods. + * + * @author Eike Stepper + */ +public final class TCPUtil +{ + /** + * @since 4.1 + */ + public static final String FACTORY_TYPE = "tcp"; //$NON-NLS-1$ + + /** + * @since 4.1 + */ + public static final String PORT_SEPARATOR = ":"; //$NON-NLS-1$ + + /** + * @since 4.1 + */ + public static final String CREDENTIALS_SEPARATOR = "@"; + + private TCPUtil() + { + } + + public static void prepareContainer(IManagedContainer container) + { + container.registerFactory(new TCPSelectorFactory()); + container.registerFactory(new TCPAcceptorFactory()); + container.registerFactory(new TCPConnectorFactory()); + container.addPostProcessor(new TCPSelectorInjector()); + } + + public static ITCPAcceptor getAcceptor(IManagedContainer container, String description) + { + return (ITCPAcceptor)container.getElement(TCPAcceptorFactory.PRODUCT_GROUP, FACTORY_TYPE, description); + } + + public static ITCPConnector getConnector(IManagedContainer container, String description) + { + return (ITCPConnector)container.getElement(TCPConnectorFactory.PRODUCT_GROUP, FACTORY_TYPE, description); + } + + /** + * Encapsulates the data needed to configure a {@link ITCPAcceptor TCP acceptor}. + * + * @author Eike Stepper + * @since 4.1 + */ + public static final class AcceptorData + { + private static final int NO_PORT = -1; + + private String address = ITCPAcceptor.DEFAULT_ADDRESS; + + private int port = NO_PORT; + + public AcceptorData() + { + } + + public AcceptorData(String address, int port) + { + this.address = address; + this.port = port; + } + + public AcceptorData(String description) + { + if (!StringUtil.isEmpty(description)) + { + String[] tokens = description.split(PORT_SEPARATOR); + if (!StringUtil.isEmpty(tokens[0])) + { + address = tokens[0]; + } + + if (tokens.length > 1 && !StringUtil.isEmpty(tokens[1])) + { + port = Integer.parseInt(tokens[1]); + } + } + } + + public AcceptorData(ITCPAcceptor acceptor) + { + this(acceptor.getAddress(), acceptor.getPort()); + } + + public String getAddress() + { + return address; + } + + public int getPort() + { + if (port == NO_PORT) + { + return ITCPAcceptor.DEFAULT_PORT; + } + + return port; + } + + @Override + public int hashCode() + { + final int prime = 31; + int result = 1; + result = prime * result + (address == null ? 0 : address.hashCode()); + result = prime * result + port; + return result; + } + + @Override + public boolean equals(Object obj) + { + if (this == obj) + { + return true; + } + + if (obj == null) + { + return false; + } + + if (getClass() != obj.getClass()) + { + return false; + } + + AcceptorData other = (AcceptorData)obj; + if (address == null) + { + if (other.address != null) + { + return false; + } + } + else if (!address.equals(other.address)) + { + return false; + } + + if (port != other.port) + { + return false; + } + + return true; + } + + @Override + public String toString() + { + String result = StringUtil.isEmpty(address) ? ITCPAcceptor.DEFAULT_ADDRESS : address; + + if (port != NO_PORT) + { + result = result + PORT_SEPARATOR + port; + } + + return result; + } + } + + /** + * Encapsulates the data needed to configure a {@link ITCPConnector TCP connector}. + * + * @author Eike Stepper + * @since 4.1 + */ + public static final class ConnectorData + { + private static final int NO_PORT = -1; + + private String host; + + private int port = NO_PORT; + + private String userID; + + public ConnectorData() + { + } + + public ConnectorData(String host, int port, String userID) + { + this.host = host; + this.port = port; + this.userID = userID; + } + + public ConnectorData(String description) + { + if (!StringUtil.isEmpty(description)) + { + try + { + URL url = parse(description); + userID = url.getUserInfo(); + host = url.getHost(); + port = url.getPort(); + } + catch (MalformedURLException ex) + { + throw new ProductCreationException(ex); + } + } + } + + public ConnectorData(ITCPConnector connector) + { + this(connector.getHost(), connector.getPort(), connector.getUserID()); + } + + public String getHost() + { + return host; + } + + public int getPort() + { + if (port == NO_PORT) + { + return ITCPConnector.DEFAULT_PORT; + } + + return port; + } + + public String getUserID() + { + return userID; + } + + @Override + public int hashCode() + { + final int prime = 31; + int result = 1; + result = prime * result + (host == null ? 0 : host.hashCode()); + result = prime * result + port; + result = prime * result + (userID == null ? 0 : userID.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) + { + if (this == obj) + { + return true; + } + + if (obj == null) + { + return false; + } + + if (getClass() != obj.getClass()) + { + return false; + } + + ConnectorData other = (ConnectorData)obj; + if (host == null) + { + if (other.host != null) + { + return false; + } + } + else if (!host.equals(other.host)) + { + return false; + } + + if (port != other.port) + { + return false; + } + + if (userID == null) + { + if (other.userID != null) + { + return false; + } + } + else if (!userID.equals(other.userID)) + { + return false; + } + + return true; + } + + @Override + public String toString() + { + String result = StringUtil.safe(host); + if (userID != null) + { + result = userID + CREDENTIALS_SEPARATOR + result; + } + + if (port != NO_PORT) + { + result = result + PORT_SEPARATOR + port; + } + + return result; + } + + private static URL parse(String description) throws MalformedURLException + { + // Scheme "tcp://" would be rejected! + return new URL("http://" + description); // TODO Don't use URL + } + } +} diff --git a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/tcp/package-info.java b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/tcp/package-info.java index f7fff8edd3..1b3ce1d453 100644 --- a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/tcp/package-info.java +++ b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/tcp/package-info.java @@ -1,15 +1,16 @@ -/*
- * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
- * 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:
- * Eike Stepper - initial API and implementation
- */
-
-/**
- * Transport layer extension with support for TCP socket connections.
- */
-package org.eclipse.net4j.tcp;
+/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others. + * 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: + * Eike Stepper - initial API and implementation + */ + +/** + * Transport layer extension with support for TCP socket connections. + */ +package org.eclipse.net4j.tcp; + diff --git a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/tcp/ssl/package-info.java b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/tcp/ssl/package-info.java index 8ad10bab16..7fd80a08f8 100644 --- a/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/tcp/ssl/package-info.java +++ b/plugins/org.eclipse.net4j.tcp/src/org/eclipse/net4j/tcp/ssl/package-info.java @@ -1,15 +1,16 @@ -/*
- * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
- * 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:
- * Eike Stepper - initial API and implementation
- */
-
-/**
- * Transport layer extension with support for TCP/SSL socket connections.
- */
-package org.eclipse.net4j.tcp.ssl;
+/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others. + * 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: + * Eike Stepper - initial API and implementation + */ + +/** + * Transport layer extension with support for TCP/SSL socket connections. + */ +package org.eclipse.net4j.tcp.ssl; + |