Update ooo320-m1
[ooovba.git] / jurt / com / sun / star / lib / connections / socket / socketAcceptor.java
bloba88a8b1210d27c4db40aec09bb9ac3c06d927488
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: socketAcceptor.java,v $
10 * $Revision: 1.6 $
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.AlreadyAcceptingException;
35 import com.sun.star.connection.ConnectionSetupException;
36 import com.sun.star.connection.XAcceptor;
37 import com.sun.star.connection.XConnection;
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.ServerSocket;
44 import java.net.Socket;
46 /**
47 * A component that implements the <code>XAcceptor</code> interface.
49 * <p>The <code>socketAcceptor</code> is a specialized component that uses TCP
50 * sockets for communication. The <code>socketAcceptor</code> is generally used
51 * by the <code>com.sun.star.connection.Acceptor</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
58 * @since UDK 1.0
60 public final class socketAcceptor implements XAcceptor {
61 /**
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.socketAcceptor";
71 /**
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
77 * requested.
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(socketAcceptor.class.getName())
88 ? FactoryHelper.getServiceFactory(socketAcceptor.class,
89 __serviceName, multiFactory,
90 regKey)
91 : null;
94 /**
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 socketAcceptor.class.getName(), __serviceName, regKey);
110 * Accepts a connection request via the described socket.
112 * <p>This call blocks until a connection has been established.</p>
114 * <p>The connection description has the following format:
115 * <code><var>type</var></code><!--
116 * -->*(<code><var>key</var>=<var>value</var></code>),
117 * where <code><var>type</var></code> should be <code>socket</code>
118 * (ignoring case). Supported keys (ignoring case) currently are
119 * <dl>
120 * <dt><code>host</code>
121 * <dd>The name or address of the accepting interface (defaults to
122 * <code>0</code>, meaning any interface).
123 * <dt><code>port</code>
124 * <dd>The TCP port number to accept on (defaults to <code>6001</code>).
125 * <dt><code>backlog</code>
126 * <dd>The maximum length of the acceptor's queue (defaults to
127 * <code>50</code>).
128 * <dt><code>tcpnodelay</code>
129 * <dd>A flag (<code>0</code>/<code>1</code>) enabling or disabling Nagle's
130 * algorithm on the resulting connection.
131 * </dl></p>
133 * @param connectionDescription the description of the connection.
134 * @return an <code>XConnection</code> to the client.
136 * @see com.sun.star.connections.XConnection
137 * @see com.sun.star.connections.XConnector
139 public XConnection accept(String connectionDescription) throws
140 AlreadyAcceptingException, ConnectionSetupException,
141 com.sun.star.lang.IllegalArgumentException
143 ServerSocket serv;
144 synchronized (this) {
145 if (server == null) {
146 ConnectionDescriptor desc
147 = new ConnectionDescriptor(connectionDescription);
148 String host = desc.getHost();
149 if (host.equals("0")) {
150 host = null;
152 if (DEBUG) {
153 System.err.println("##### " + getClass().getName()
154 + ".accept: creating ServerSocket "
155 + desc.getPort() + ", "
156 + desc.getBacklog() + ", " + host);
158 try {
159 server = new ServerSocket(desc.getPort(), desc.getBacklog(),
160 host == null ? null
161 : InetAddress.getByName(host));
162 } catch (IOException e) {
163 throw new ConnectionSetupException(e.toString());
165 acceptingDescription = connectionDescription;
166 tcpNoDelay = desc.getTcpNoDelay();
167 } else if (!connectionDescription.equals(acceptingDescription)) {
168 throw new AlreadyAcceptingException(acceptingDescription
169 + " vs. "
170 + connectionDescription);
172 serv = server;
174 Socket socket;
175 try {
176 socket = serv.accept();
177 if (DEBUG) {
178 System.err.println("##### " + getClass().getName()
179 + ".accept: accepted " + socket);
181 if (tcpNoDelay != null) {
182 socket.setTcpNoDelay(tcpNoDelay.booleanValue());
184 return new SocketConnection(acceptingDescription, socket);
186 catch(IOException e) {
187 throw new ConnectionSetupException(e.toString());
191 // see com.sun.star.connection.XAcceptor#stopAccepting
192 public void stopAccepting() {
193 ServerSocket serv;
194 synchronized (this) {
195 serv = server;
197 try {
198 serv.close();
200 catch (IOException e) {
201 throw new com.sun.star.uno.RuntimeException(e.toString());
205 private static final boolean DEBUG = false;
207 private ServerSocket server = null;
208 private String acceptingDescription;
209 private Boolean tcpNoDelay;