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