/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2008 by Sun Microsystems, Inc.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* $RCSfile: socketConnector.java,v $
* $Revision: 1.7 $
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* The The This method is called by the This method is called by the The connection description has the following format:
* XConnector
interface.
*
* socketConnector
is a specialized component that uses TCP
* sockets for communication. The socketConnector
is generally
* used by the com.sun.star.connection.Connector
service.JavaLoader
acceses this through reflection.JavaLoader
.XSingleServiceFactory
for creating the component.
*
* @see com.sun.star.comp.loader.JavaLoader
*/
public static XSingleServiceFactory __getServiceFactory(
String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey)
{
return implName.equals(socketConnector.class.getName())
? FactoryHelper.getServiceFactory(socketConnector.class,
__serviceName, multiFactory,
regKey)
: null;
}
/**
* Writes the service information into the given registry key.
*
* JavaLoader
.true
if the operation succeeded.
*
* @see com.sun.star.comp.loader.JavaLoader
*/
public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
return FactoryHelper.writeRegistryServiceInfo(
socketConnector.class.getName(), __serviceName, regKey);
}
/**
* Connects via the described socket to a waiting server.
*
* type
*(key=value
),
* where type
should be socket
* (ignoring case). Supported keys (ignoring case) currently are
*
*
host
* port
* 6001
).
* tcpnodelay
* 0
/1
) enabling or disabling Nagle's
* algorithm on the resulting connection.
*
XConnection
to the server.
*
* @see com.sun.star.connections.XAcceptor
* @see com.sun.star.connections.XConnection
*/
public synchronized XConnection connect(String connectionDescription)
throws NoConnectException, ConnectionSetupException
{
if (connected) {
throw new ConnectionSetupException("alread connected");
}
ConnectionDescriptor desc;
try {
desc = new ConnectionDescriptor(connectionDescription);
} catch (com.sun.star.lang.IllegalArgumentException e) {
throw new ConnectionSetupException(e.toString());
}
if (desc.getHost() == null) {
throw new ConnectionSetupException("host parameter missing");
}
// Try all (IPv4 and IPv6) addresses, in case this client is on a
// dual-stack host and the server process is an IPv4-only process, also
// on a dual-stack host (see Stevens, Fenner, Rudoff: "Unix Network
// Programming, Volume 1: The Sockets Networking API, 3rd Edition",
// p. 359):
InetAddress[] adr;
try {
adr = InetAddress.getAllByName(desc.getHost());
} catch (UnknownHostException e) {
throw new ConnectionSetupException(e.toString());
}
Socket socket = null;
for (int i = 0; i < adr.length; ++i) {
try {
socket = new Socket(adr[i], desc.getPort());
break;
} catch (IOException e) {
if (i == adr.length - 1) {
throw new NoConnectException(e.toString());
}
}
}
XConnection con;
try {
if (desc.getTcpNoDelay() != null) {
socket.setTcpNoDelay(desc.getTcpNoDelay().booleanValue());
}
con = new SocketConnection(connectionDescription, socket);
} catch (IOException e) {
throw new NoConnectException(e.toString());
}
connected = true;
return con;
}
private boolean connected = false;
}