1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: socketConnector.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package com
.sun
.star
.lib
.connections
.socket
;
33 import com
.sun
.star
.comp
.loader
.FactoryHelper
;
34 import com
.sun
.star
.connection
.ConnectionSetupException
;
35 import com
.sun
.star
.connection
.NoConnectException
;
36 import com
.sun
.star
.connection
.XConnection
;
37 import com
.sun
.star
.connection
.XConnector
;
38 import com
.sun
.star
.lang
.XMultiServiceFactory
;
39 import com
.sun
.star
.lang
.XSingleServiceFactory
;
40 import com
.sun
.star
.registry
.XRegistryKey
;
41 import java
.io
.IOException
;
42 import java
.net
.InetAddress
;
43 import java
.net
.Socket
;
44 import java
.net
.UnknownHostException
;
47 * A component that implements the <code>XConnector</code> interface.
49 * <p>The <code>socketConnector</code> is a specialized component that uses TCP
50 * sockets for communication. The <code>socketConnector</code> is generally
51 * used by the <code>com.sun.star.connection.Connector</code> service.</p>
53 * @see com.sun.star.connections.XAcceptor
54 * @see com.sun.star.connections.XConnection
55 * @see com.sun.star.connections.XConnector
56 * @see com.sun.star.loader.JavaLoader
60 public final class socketConnector
implements XConnector
{
62 * The name of the service.
64 * <p>The <code>JavaLoader</code> acceses this through reflection.</p>
66 * @see com.sun.star.comp.loader.JavaLoader
68 public static final String __serviceName
69 = "com.sun.star.connection.socketConnector";
72 * Returns a factory for creating the service.
74 * <p>This method is called by the <code>JavaLoader</code>.</p>
76 * @param implName the name of the implementation for which a service is
78 * @param multiFactory the service manager to be used (if needed).
79 * @param regKey the registry key.
80 * @return an <code>XSingleServiceFactory</code> for creating the component.
82 * @see com.sun.star.comp.loader.JavaLoader
84 public static XSingleServiceFactory
__getServiceFactory(
85 String implName
, XMultiServiceFactory multiFactory
, XRegistryKey regKey
)
87 return implName
.equals(socketConnector
.class.getName())
88 ? FactoryHelper
.getServiceFactory(socketConnector
.class,
89 __serviceName
, multiFactory
,
95 * Writes the service information into the given registry key.
97 * <p>This method is called by the <code>JavaLoader</code>.</p>
99 * @param regKey the registry key.
100 * @return <code>true</code> if the operation succeeded.
102 * @see com.sun.star.comp.loader.JavaLoader
104 public static boolean __writeRegistryServiceInfo(XRegistryKey regKey
) {
105 return FactoryHelper
.writeRegistryServiceInfo(
106 socketConnector
.class.getName(), __serviceName
, regKey
);
110 * Connects via the described socket to a waiting server.
112 * <p>The connection description has the following format:
113 * <code><var>type</var></code><!--
114 * -->*(<code><var>key</var>=<var>value</var></code>),
115 * where <code><var>type</var></code> should be <code>socket</code>
116 * (ignoring case). Supported keys (ignoring case) currently are
118 * <dt><code>host</code>
119 * <dd>The name or address of the server. Must be present.
120 * <dt><code>port</code>
121 * <dd>The TCP port number of the server (defaults to <code>6001</code>).
122 * <dt><code>tcpnodelay</code>
123 * <dd>A flag (<code>0</code>/<code>1</code>) enabling or disabling Nagle's
124 * algorithm on the resulting connection.
127 * @param connectionDescription the description of the connection.
128 * @return an <code>XConnection</code> to the server.
130 * @see com.sun.star.connections.XAcceptor
131 * @see com.sun.star.connections.XConnection
133 public synchronized XConnection
connect(String connectionDescription
)
134 throws NoConnectException
, ConnectionSetupException
137 throw new ConnectionSetupException("alread connected");
139 ConnectionDescriptor desc
;
141 desc
= new ConnectionDescriptor(connectionDescription
);
142 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
143 throw new ConnectionSetupException(e
.toString());
145 if (desc
.getHost() == null) {
146 throw new ConnectionSetupException("host parameter missing");
148 // Try all (IPv4 and IPv6) addresses, in case this client is on a
149 // dual-stack host and the server process is an IPv4-only process, also
150 // on a dual-stack host (see Stevens, Fenner, Rudoff: "Unix Network
151 // Programming, Volume 1: The Sockets Networking API, 3rd Edition",
155 adr
= InetAddress
.getAllByName(desc
.getHost());
156 } catch (UnknownHostException e
) {
157 throw new ConnectionSetupException(e
.toString());
159 Socket socket
= null;
160 for (int i
= 0; i
< adr
.length
; ++i
) {
162 socket
= new Socket(adr
[i
], desc
.getPort());
164 } catch (IOException e
) {
165 if (i
== adr
.length
- 1) {
166 throw new NoConnectException(e
.toString());
172 if (desc
.getTcpNoDelay() != null) {
173 socket
.setTcpNoDelay(desc
.getTcpNoDelay().booleanValue());
175 con
= new SocketConnection(connectionDescription
, socket
);
176 } catch (IOException e
) {
177 throw new NoConnectException(e
.toString());
183 private boolean connected
= false;