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