1*2be43276SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2be43276SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2be43276SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2be43276SAndrew Rist  * distributed with this work for additional information
6*2be43276SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2be43276SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2be43276SAndrew Rist  * "License"); you may not use this file except in compliance
9*2be43276SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2be43276SAndrew Rist  *
11*2be43276SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2be43276SAndrew Rist  *
13*2be43276SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2be43276SAndrew Rist  * software distributed under the License is distributed on an
15*2be43276SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2be43276SAndrew Rist  * KIND, either express or implied.  See the License for the
17*2be43276SAndrew Rist  * specific language governing permissions and limitations
18*2be43276SAndrew Rist  * under the License.
19*2be43276SAndrew Rist  *
20*2be43276SAndrew Rist  *************************************************************/
21*2be43276SAndrew Rist 
22*2be43276SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package com.sun.star.comp.connections;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.comp.loader.FactoryHelper;
27cdf0e10cSrcweir import com.sun.star.connection.AlreadyAcceptingException;
28cdf0e10cSrcweir import com.sun.star.connection.ConnectionSetupException;
29cdf0e10cSrcweir import com.sun.star.connection.XAcceptor;
30cdf0e10cSrcweir import com.sun.star.connection.XConnection;
31cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
32cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
33cdf0e10cSrcweir import com.sun.star.registry.XRegistryKey;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir /**
36cdf0e10cSrcweir  * A component that implements the <code>XAcceptor</code> interface.
37cdf0e10cSrcweir  *
38cdf0e10cSrcweir  * <p>The <code>Acceptor</code> is a general component, that uses less general
39cdf0e10cSrcweir  * components (like <code>com.sun.star.connection.socketAcceptor</code>) to
40cdf0e10cSrcweir  * implement its functionality.</p>
41cdf0e10cSrcweir  *
42cdf0e10cSrcweir  * @see com.sun.star.connections.XAcceptor
43cdf0e10cSrcweir  * @see com.sun.star.connections.XConnection
44cdf0e10cSrcweir  * @see com.sun.star.connections.XConnector
45cdf0e10cSrcweir  * @see com.sun.star.loader.JavaLoader
46cdf0e10cSrcweir  *
47cdf0e10cSrcweir  * @since UDK 1.0
48cdf0e10cSrcweir  */
49cdf0e10cSrcweir public final class Acceptor implements XAcceptor {
50cdf0e10cSrcweir     /**
51cdf0e10cSrcweir      * The name of the service.
52cdf0e10cSrcweir      *
53cdf0e10cSrcweir      * <p>The <code>JavaLoader</code> acceses this through reflection.</p>
54cdf0e10cSrcweir      *
55cdf0e10cSrcweir      * @see com.sun.star.comp.loader.JavaLoader
56cdf0e10cSrcweir      */
57cdf0e10cSrcweir     public static final String __serviceName
58cdf0e10cSrcweir     = "com.sun.star.connection.Acceptor";
59cdf0e10cSrcweir 
60cdf0e10cSrcweir     /**
61cdf0e10cSrcweir      * Returns a factory for creating the service.
62cdf0e10cSrcweir      *
63cdf0e10cSrcweir      * <p>This method is called by the <code>JavaLoader</code>.</p>
64cdf0e10cSrcweir      *
65cdf0e10cSrcweir      * @param implName the name of the implementation for which a service is
66cdf0e10cSrcweir      *     requested.
67cdf0e10cSrcweir      * @param multiFactory the service manager to be used (if needed).
68cdf0e10cSrcweir      * @param regKey the registry key.
69cdf0e10cSrcweir      * @return an <code>XSingleServiceFactory</code> for creating the component.
70cdf0e10cSrcweir      *
71cdf0e10cSrcweir      * @see com.sun.star.comp.loader.JavaLoader
72cdf0e10cSrcweir      */
__getServiceFactory( String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey)73cdf0e10cSrcweir     public static XSingleServiceFactory __getServiceFactory(
74cdf0e10cSrcweir         String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey)
75cdf0e10cSrcweir     {
76cdf0e10cSrcweir         return implName.equals(Acceptor.class.getName())
77cdf0e10cSrcweir             ? FactoryHelper.getServiceFactory(Acceptor.class, __serviceName,
78cdf0e10cSrcweir                                               multiFactory, regKey)
79cdf0e10cSrcweir             : null;
80cdf0e10cSrcweir     }
81cdf0e10cSrcweir 
82cdf0e10cSrcweir     /**
83cdf0e10cSrcweir      * Constructs a new <code>Acceptor</code> that uses the given service
84cdf0e10cSrcweir      * factory to create a specific <code>XAcceptor</code>.
85cdf0e10cSrcweir      *
86cdf0e10cSrcweir      * @param serviceFactory the service factory to use.
87cdf0e10cSrcweir      */
Acceptor(XMultiServiceFactory serviceFactory)88cdf0e10cSrcweir     public Acceptor(XMultiServiceFactory serviceFactory) {
89cdf0e10cSrcweir         this.serviceFactory = serviceFactory;
90cdf0e10cSrcweir     }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir     /**
93cdf0e10cSrcweir      * Accepts a connection request via the given connection type.
94cdf0e10cSrcweir      *
95cdf0e10cSrcweir      * <p>This call blocks until a connection has been established.</p>
96cdf0e10cSrcweir      *
97cdf0e10cSrcweir      * <p>The connection description has the following format:
98cdf0e10cSrcweir      * <code><var>type</var></code><!--
99cdf0e10cSrcweir      *     -->*(<code><var>key</var>=<var>value</var></code>).
100cdf0e10cSrcweir      * The specific <code>XAcceptor</code> implementation is instantiated
101cdf0e10cSrcweir      * through the service factory as
102cdf0e10cSrcweir      * <code>com.sun.star.connection.<var>type</var>Acceptor</code> (with
103cdf0e10cSrcweir      * <code><var>type</var></code> in lower case).</p>
104cdf0e10cSrcweir      *
105cdf0e10cSrcweir      * @param connectionDescription the description of the connection.
106cdf0e10cSrcweir      * @return an <code>XConnection</code> to the client.
107cdf0e10cSrcweir      *
108cdf0e10cSrcweir      * @see com.sun.star.connections.XConnection
109cdf0e10cSrcweir      * @see com.sun.star.connections.XConnector
110cdf0e10cSrcweir      */
accept(String connectionDescription)111cdf0e10cSrcweir     public XConnection accept(String connectionDescription) throws
112cdf0e10cSrcweir         AlreadyAcceptingException, ConnectionSetupException,
113cdf0e10cSrcweir         com.sun.star.lang.IllegalArgumentException
114cdf0e10cSrcweir     {
115cdf0e10cSrcweir         if (DEBUG) {
116cdf0e10cSrcweir             System.err.println("##### " + getClass().getName() + ".accept("
117cdf0e10cSrcweir                                + connectionDescription + ")");
118cdf0e10cSrcweir         }
119cdf0e10cSrcweir         XAcceptor acc;
120cdf0e10cSrcweir         synchronized (this) {
121cdf0e10cSrcweir             if (acceptor == null) {
122cdf0e10cSrcweir                 acceptor = (XAcceptor) Implementation.getConnectionService(
123cdf0e10cSrcweir                     serviceFactory, connectionDescription, XAcceptor.class,
124cdf0e10cSrcweir                     "Acceptor");
125cdf0e10cSrcweir                 acceptingDescription = connectionDescription;
126cdf0e10cSrcweir             } else if (!connectionDescription.equals(acceptingDescription)) {
127cdf0e10cSrcweir                 throw new AlreadyAcceptingException(acceptingDescription
128cdf0e10cSrcweir                                                     + " vs. "
129cdf0e10cSrcweir                                                     + connectionDescription);
130cdf0e10cSrcweir             }
131cdf0e10cSrcweir             acc = acceptor;
132cdf0e10cSrcweir         }
133cdf0e10cSrcweir         return acc.accept(connectionDescription);
134cdf0e10cSrcweir     }
135cdf0e10cSrcweir 
136cdf0e10cSrcweir     // see com.sun.star.connection.XAcceptor#stopAccepting
stopAccepting()137cdf0e10cSrcweir     public void stopAccepting() {
138cdf0e10cSrcweir         XAcceptor acc;
139cdf0e10cSrcweir         synchronized (this) {
140cdf0e10cSrcweir             acc = acceptor;
141cdf0e10cSrcweir         }
142cdf0e10cSrcweir         acc.stopAccepting();
143cdf0e10cSrcweir     }
144cdf0e10cSrcweir 
145cdf0e10cSrcweir     private static final boolean DEBUG = false;
146cdf0e10cSrcweir 
147cdf0e10cSrcweir     private final XMultiServiceFactory serviceFactory;
148cdf0e10cSrcweir 
149cdf0e10cSrcweir     private XAcceptor acceptor = null;
150cdf0e10cSrcweir     private String acceptingDescription;
151cdf0e10cSrcweir }
152