bump product version to 5.0.4.1
[LibreOffice.git] / jurt / com / sun / star / lib / connections / socket / socketAcceptor.java
blobda33625e498d46c4c405af26083793674fa77b3c
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.AlreadyAcceptingException;
23 import com.sun.star.connection.ConnectionSetupException;
24 import com.sun.star.connection.XAcceptor;
25 import com.sun.star.connection.XConnection;
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.*;
33 /**
34 * A component that implements the <code>XAcceptor</code> interface.
36 * <p>The <code>socketAcceptor</code> is a specialized component that uses TCP
37 * sockets for communication. The <code>socketAcceptor</code> is generally used
38 * by the <code>com.sun.star.connection.Acceptor</code> service.</p>
40 * @see com.sun.star.connection.XAcceptor
41 * @see com.sun.star.connection.XConnection2
42 * @see com.sun.star.connection.XConnector
43 * @see com.sun.star.comp.loader.JavaLoader
45 * @since UDK 1.0
47 public final class socketAcceptor implements XAcceptor {
48 /**
49 * The name of the service.
51 * <p>The <code>JavaLoader</code> accesses this through reflection.</p>
53 * @see com.sun.star.comp.loader.JavaLoader
55 public static final String __serviceName
56 = "com.sun.star.connection.socketAcceptor";
58 /**
59 * Returns a factory for creating the service.
61 * <p>This method is called by the <code>JavaLoader</code>.</p>
63 * @param implName the name of the implementation for which a service is
64 * requested.
65 * @param multiFactory the service manager to be used (if needed).
66 * @param regKey the registry key.
67 * @return an <code>XSingleServiceFactory</code> for creating the component.
69 * @see com.sun.star.comp.loader.JavaLoader
71 public static XSingleServiceFactory __getServiceFactory(
72 String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey)
74 return implName.equals(socketAcceptor.class.getName())
75 ? FactoryHelper.getServiceFactory(socketAcceptor.class,
76 __serviceName, multiFactory,
77 regKey)
78 : null;
81 /**
82 * Accepts a connection request via the described socket.
84 * <p>This call blocks until a connection has been established.</p>
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 accepting interface (defaults to
94 * <code>0</code>, meaning any interface).
95 * <dt><code>port</code>
96 * <dd>The TCP port number to accept on (defaults to <code>6001</code>).
97 * <dt><code>backlog</code>
98 * <dd>The maximum length of the acceptor's queue (defaults to
99 * <code>50</code>).
100 * <dt><code>tcpnodelay</code>
101 * <dd>A flag (<code>0</code>/<code>1</code>) enabling or disabling Nagle's
102 * algorithm on the resulting connection.
103 * </dl>
105 * @param connectionDescription the description of the connection.
106 * @return an <code>XConnection</code> to the client.
108 * @see com.sun.star.connection.XConnection
109 * @see com.sun.star.connection.XConnector
111 public XConnection accept(String connectionDescription) throws
112 AlreadyAcceptingException, ConnectionSetupException,
113 com.sun.star.lang.IllegalArgumentException
115 ServerSocket serv;
116 synchronized (this) {
117 if (server == null) {
118 ConnectionDescriptor desc
119 = new ConnectionDescriptor(connectionDescription);
120 String host = desc.getHost();
121 if (host.equals("0")) {
122 host = null;
124 if (DEBUG) {
125 System.err.println("##### " + getClass().getName()
126 + ".accept: creating ServerSocket "
127 + desc.getPort() + ", "
128 + desc.getBacklog() + ", " + host);
130 try {
131 server = new ServerSocket(desc.getPort(), desc.getBacklog(),
132 host == null ? null
133 : InetAddress.getByName(host));
134 } catch (IOException e) {
135 throw new ConnectionSetupException(e);
137 acceptingDescription = connectionDescription;
138 tcpNoDelay = desc.getTcpNoDelay();
139 } else if (!connectionDescription.equals(acceptingDescription)) {
140 throw new AlreadyAcceptingException(acceptingDescription
141 + " vs. "
142 + connectionDescription);
144 serv = server;
146 Socket socket;
147 try {
148 socket = serv.accept();
149 if (DEBUG) {
150 System.err.println("##### " + getClass().getName()
151 + ".accept: accepted " + socket);
153 // we enable tcpNoDelay for loopback connections because
154 // it can make a significant speed difference on linux boxes.
155 if (tcpNoDelay != null) {
156 socket.setTcpNoDelay(tcpNoDelay.booleanValue());
158 else if (((InetSocketAddress)socket.getRemoteSocketAddress()).getAddress().isLoopbackAddress()) {
159 socket.setTcpNoDelay(true);
161 return new SocketConnection(acceptingDescription, socket);
163 catch(IOException e) {
164 throw new ConnectionSetupException(e);
170 * @see com.sun.star.connection.XAcceptor#stopAccepting
172 public void stopAccepting() {
173 ServerSocket serv;
174 synchronized (this) {
175 serv = server;
177 try {
178 serv.close();
180 catch (IOException e) {
181 throw new com.sun.star.uno.RuntimeException(e);
185 private static final boolean DEBUG = false;
187 private ServerSocket server = null;
188 private String acceptingDescription;
189 private Boolean tcpNoDelay;