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.connection.ConnectionSetupException;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.uno.UnoRuntime;
33 
34 /**
35  * Helper class for <code>Acceptor</code> and <code>Connector</code>.
36  */
37 final class Implementation {
38     /**
39      * Instantiate a service for a given connection type.
40      *
41      * @param factory the service factory used to instantiate the requested
42      *     service.
43      * @param description has the following format:
44      *     <code><var>type</var></code><!--
45      *         -->*(<code><var>key</var>=<var>value</var></code>).
46      *     The specific service implementation is instantiated through the
47      *     service factory as
48      *     <code>com.sun.star.connection.<var>type</var>service<var></var><!--
49      *         --></code>
50      *     (with <code><var>type</var></code> in lower case, and
51      *     <code><var>service</var></code> either <code>Acceptor</code> or
52      *     <code>Connector</code>).</p>
53      * @param serviceClass the IDL interface type for which to query the
54      *     requested service.
55      * @param serviceType must be either <code>Acceptor</code> or
56      *     <code>Connector</code>.
57      * @return an instance of the requested service.  Never returns
58      *     <code>null</code>.
59      * @throws ConnectionSetupException if the requested service can not be
60      *     found, or cannot be instantiated.
61      */
62     public static Object getConnectionService(XMultiServiceFactory factory,
63                                               String description,
64                                               Class serviceClass,
65                                               String serviceType)
66         throws ConnectionSetupException
67     {
68         int i = description.indexOf(',');
69         String type
70             = (i < 0 ? description : description.substring(0, i)).toLowerCase();
71         Object service = null;
72         try {
73             service = UnoRuntime.queryInterface(
74                 serviceClass,
75                 factory.createInstance("com.sun.star.connection." + type
76                                        + serviceType));
77         } catch (RuntimeException e) {
78             throw e;
79         } catch (com.sun.star.uno.Exception e) {
80         }
81         if (service == null) {
82             // As a fallback, also try to instantiate the service from the
83             // com.sun.star.lib.connections package structure:
84             try {
85                 service
86                     = Class.forName("com.sun.star.lib.connections." + type
87                                     + "." + type + serviceType).newInstance();
88             } catch (ClassNotFoundException e) {
89             } catch (IllegalAccessException e) {
90             } catch (InstantiationException e) {
91             }
92         }
93         if (service == null) {
94             throw new ConnectionSetupException("no " + serviceType + " for "
95                                                + type);
96         }
97         return service;
98     }
99 
100     private Implementation() {} // do not instantiate
101 }
102