1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package com.sun.star.comp.connections;
29 
30 import com.sun.star.comp.loader.FactoryHelper;
31 import com.sun.star.connection.AlreadyAcceptingException;
32 import com.sun.star.connection.ConnectionSetupException;
33 import com.sun.star.connection.XAcceptor;
34 import com.sun.star.connection.XConnection;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.lang.XSingleServiceFactory;
37 import com.sun.star.registry.XRegistryKey;
38 
39 /**
40  * A component that implements the <code>XAcceptor</code> interface.
41  *
42  * <p>The <code>Acceptor</code> is a general component, that uses less general
43  * components (like <code>com.sun.star.connection.socketAcceptor</code>) to
44  * implement its functionality.</p>
45  *
46  * @see com.sun.star.connections.XAcceptor
47  * @see com.sun.star.connections.XConnection
48  * @see com.sun.star.connections.XConnector
49  * @see com.sun.star.loader.JavaLoader
50  *
51  * @since UDK 1.0
52  */
53 public final class Acceptor implements XAcceptor {
54     /**
55      * The name of the service.
56      *
57      * <p>The <code>JavaLoader</code> acceses this through reflection.</p>
58      *
59      * @see com.sun.star.comp.loader.JavaLoader
60      */
61     public static final String __serviceName
62     = "com.sun.star.connection.Acceptor";
63 
64     /**
65      * Returns a factory for creating the service.
66      *
67      * <p>This method is called by the <code>JavaLoader</code>.</p>
68      *
69      * @param implName the name of the implementation for which a service is
70      *     requested.
71      * @param multiFactory the service manager to be used (if needed).
72      * @param regKey the registry key.
73      * @return an <code>XSingleServiceFactory</code> for creating the component.
74      *
75      * @see com.sun.star.comp.loader.JavaLoader
76      */
77     public static XSingleServiceFactory __getServiceFactory(
78         String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey)
79     {
80         return implName.equals(Acceptor.class.getName())
81             ? FactoryHelper.getServiceFactory(Acceptor.class, __serviceName,
82                                               multiFactory, regKey)
83             : null;
84     }
85 
86     /**
87      * Constructs a new <code>Acceptor</code> that uses the given service
88      * factory to create a specific <code>XAcceptor</code>.
89      *
90      * @param serviceFactory the service factory to use.
91      */
92     public Acceptor(XMultiServiceFactory serviceFactory) {
93         this.serviceFactory = serviceFactory;
94     }
95 
96     /**
97      * Accepts a connection request via the given connection type.
98      *
99      * <p>This call blocks until a connection has been established.</p>
100      *
101      * <p>The connection description has the following format:
102      * <code><var>type</var></code><!--
103      *     -->*(<code><var>key</var>=<var>value</var></code>).
104      * The specific <code>XAcceptor</code> implementation is instantiated
105      * through the service factory as
106      * <code>com.sun.star.connection.<var>type</var>Acceptor</code> (with
107      * <code><var>type</var></code> in lower case).</p>
108      *
109      * @param connectionDescription the description of the connection.
110      * @return an <code>XConnection</code> to the client.
111      *
112      * @see com.sun.star.connections.XConnection
113      * @see com.sun.star.connections.XConnector
114      */
115     public XConnection accept(String connectionDescription) throws
116         AlreadyAcceptingException, ConnectionSetupException,
117         com.sun.star.lang.IllegalArgumentException
118     {
119         if (DEBUG) {
120             System.err.println("##### " + getClass().getName() + ".accept("
121                                + connectionDescription + ")");
122         }
123         XAcceptor acc;
124         synchronized (this) {
125             if (acceptor == null) {
126                 acceptor = (XAcceptor) Implementation.getConnectionService(
127                     serviceFactory, connectionDescription, XAcceptor.class,
128                     "Acceptor");
129                 acceptingDescription = connectionDescription;
130             } else if (!connectionDescription.equals(acceptingDescription)) {
131                 throw new AlreadyAcceptingException(acceptingDescription
132                                                     + " vs. "
133                                                     + connectionDescription);
134             }
135             acc = acceptor;
136         }
137         return acc.accept(connectionDescription);
138     }
139 
140     // see com.sun.star.connection.XAcceptor#stopAccepting
141     public void stopAccepting() {
142         XAcceptor acc;
143         synchronized (this) {
144             acc = acceptor;
145         }
146         acc.stopAccepting();
147     }
148 
149     private static final boolean DEBUG = false;
150 
151     private final XMultiServiceFactory serviceFactory;
152 
153     private XAcceptor acceptor = null;
154     private String acceptingDescription;
155 }
156