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