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