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.ConnectionSetupException;
28 import com.sun.star.connection.NoConnectException;
29 import com.sun.star.connection.XConnection;
30 import com.sun.star.connection.XConnector;
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>XConnector</code> interface.
37  *
38  * <p>The <code>Connector</code> is a general component, that uses less general
39  * components (like <code>com.sun.star.connection.socketConnector</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 class Connector implements XConnector {
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.Connector";
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(Connector.class.getName())
77             ? FactoryHelper.getServiceFactory(Connector.class, __serviceName,
78                                               multiFactory, regKey)
79             : null;
80     }
81 
82     /**
83      * Constructs a new <code>Connector</code> that uses the given service
84      * factory to create a specific <code>XConnector</code>.
85      *
86      * @param serviceFactory the service factory to use.
87      */
Connector(XMultiServiceFactory serviceFactory)88     public Connector(XMultiServiceFactory serviceFactory) {
89         this.serviceFactory = serviceFactory;
90     }
91 
92     /**
93      * Connects via the given connection type to a waiting server.
94      *
95      * <p>The connection description has the following format:
96      * <code><var>type</var></code><!--
97      *     -->*(<code><var>key</var>=<var>value</var></code>).
98      * The specific <code>XConnector</code> implementation is instantiated
99      * through the service factory as
100      * <code>com.sun.star.connection.<var>type</var>Connector</code> (with
101      * <code><var>type</var></code> in lower case).</p>
102      *
103      * @param connectionDescription the description of the connection.
104      * @return an <code>XConnection</code> to the server.
105      *
106      * @see com.sun.star.connections.XAcceptor
107      * @see com.sun.star.connections.XConnection
108      */
connect(String connectionDescription)109     public synchronized XConnection connect(String connectionDescription)
110         throws NoConnectException, ConnectionSetupException
111     {
112         if (DEBUG) {
113             System.err.println("##### " + getClass().getName() + ".connect("
114                                + connectionDescription + ")");
115         }
116         if (connected) {
117             throw new ConnectionSetupException("already connected");
118         }
119         XConnection con
120             = ((XConnector) Implementation.getConnectionService(
121                    serviceFactory, connectionDescription, XConnector.class,
122                    "Connector")).connect(connectionDescription);
123         connected = true;
124         return con;
125     }
126 
127     private static final boolean DEBUG = false;
128 
129     private final XMultiServiceFactory serviceFactory;
130 
131     private boolean connected = false;
132 }
133