bump product version to 5.0.4.1
[LibreOffice.git] / jurt / com / sun / star / lib / connections / socket / socketConnector.java
blobc169b59a6aeb04fc780d75d56bc004bb7076d2c0
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package com.sun.star.lib.connections.socket;
21 import com.sun.star.comp.loader.FactoryHelper;
22 import com.sun.star.connection.ConnectionSetupException;
23 import com.sun.star.connection.NoConnectException;
24 import com.sun.star.connection.XConnection;
25 import com.sun.star.connection.XConnector;
26 import com.sun.star.lang.XMultiServiceFactory;
27 import com.sun.star.lang.XSingleServiceFactory;
28 import com.sun.star.registry.XRegistryKey;
30 import java.io.IOException;
31 import java.net.InetAddress;
32 import java.net.Socket;
33 import java.net.UnknownHostException;
35 /**
36 * A component that implements the <code>XConnector</code> interface.
38 * <p>The <code>socketConnector</code> is a specialized component that uses TCP
39 * sockets for communication. The <code>socketConnector</code> is generally
40 * used by the <code>com.sun.star.connection.Connector</code> service.</p>
42 * @see com.sun.star.connection.XAcceptor
43 * @see com.sun.star.connection.XConnection
44 * @see com.sun.star.connection.XConnector
45 * @see com.sun.star.comp.loader.JavaLoader
47 * @since UDK 1.0
49 public final class socketConnector implements XConnector {
50 /**
51 * The name of the service.
53 * <p>The <code>JavaLoader</code> accesses this through reflection.</p>
55 * @see com.sun.star.comp.loader.JavaLoader
57 public static final String __serviceName
58 = "com.sun.star.connection.socketConnector";
60 /**
61 * Returns a factory for creating the service.
63 * <p>This method is called by the <code>JavaLoader</code>.</p>
65 * @param implName the name of the implementation for which a service is
66 * requested.
67 * @param multiFactory the service manager to be used (if needed).
68 * @param regKey the registry key.
69 * @return an <code>XSingleServiceFactory</code> for creating the component.
71 * @see com.sun.star.comp.loader.JavaLoader
73 public static XSingleServiceFactory __getServiceFactory(
74 String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey)
76 return implName.equals(socketConnector.class.getName())
77 ? FactoryHelper.getServiceFactory(socketConnector.class,
78 __serviceName, multiFactory,
79 regKey)
80 : null;
83 /**
84 * Connects via the described socket to a waiting server.
86 * <p>The connection description has the following format:
87 * <code><var>type</var></code><!--
88 * -->*(<code><var>key</var>=<var>value</var></code>),
89 * where <code><var>type</var></code> should be <code>socket</code>
90 * (ignoring case). Supported keys (ignoring case) currently are</p>
91 * <dl>
92 * <dt><code>host</code>
93 * <dd>The name or address of the server. Must be present.
94 * <dt><code>port</code>
95 * <dd>The TCP port number of the server (defaults to <code>6001</code>).
96 * <dt><code>tcpnodelay</code>
97 * <dd>A flag (<code>0</code>/<code>1</code>) enabling or disabling Nagle's
98 * algorithm on the resulting connection.
99 * </dl>
101 * @param connectionDescription the description of the connection.
102 * @return an <code>XConnection</code> to the server.
104 * @see com.sun.star.connection.XAcceptor
105 * @see com.sun.star.connection.XConnection
107 public synchronized XConnection connect(String connectionDescription)
108 throws NoConnectException, ConnectionSetupException
110 if (connected)
111 throw new ConnectionSetupException("alread connected");
113 ConnectionDescriptor desc;
114 try {
115 desc = new ConnectionDescriptor(connectionDescription);
116 } catch (com.sun.star.lang.IllegalArgumentException e) {
117 throw new ConnectionSetupException(e);
120 if (desc.getHost() == null)
121 throw new ConnectionSetupException("host parameter missing");
122 // Try all (IPv4 and IPv6) addresses, in case this client is on a
123 // dual-stack host and the server process is an IPv4-only process, also
124 // on a dual-stack host (see Stevens, Fenner, Rudoff: "Unix Network
125 // Programming, Volume 1: The Sockets Networking API, 3rd Edition",
126 // p. 359):
127 InetAddress[] adr;
128 try {
129 adr = InetAddress.getAllByName(desc.getHost());
130 } catch (UnknownHostException e) {
131 throw new ConnectionSetupException(e);
133 Socket socket = null;
134 boolean isLoopbackAddress = false;
135 for (int i = 0; i < adr.length; ++i) {
136 try {
137 isLoopbackAddress = adr[i].isLoopbackAddress();
138 socket = new Socket(adr[i], desc.getPort());
139 break;
140 } catch (IOException e) {
141 if (i == adr.length - 1)
142 throw new NoConnectException(e);
145 XConnection con;
146 try {
147 // we enable tcpNoDelay for loopback connections because
148 // it can make a significant speed difference on linux boxes.
149 if (desc.getTcpNoDelay() != null)
150 socket.setTcpNoDelay(desc.getTcpNoDelay().booleanValue());
151 else if (isLoopbackAddress)
152 socket.setTcpNoDelay(true);
154 con = new SocketConnection(connectionDescription, socket);
155 } catch (IOException e) {
156 throw new NoConnectException(e);
158 connected = true;
159 return con;
162 private boolean connected = false;