1*2be43276SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2be43276SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2be43276SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2be43276SAndrew Rist  * distributed with this work for additional information
6*2be43276SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2be43276SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2be43276SAndrew Rist  * "License"); you may not use this file except in compliance
9*2be43276SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2be43276SAndrew Rist  *
11*2be43276SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2be43276SAndrew Rist  *
13*2be43276SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2be43276SAndrew Rist  * software distributed under the License is distributed on an
15*2be43276SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2be43276SAndrew Rist  * KIND, either express or implied.  See the License for the
17*2be43276SAndrew Rist  * specific language governing permissions and limitations
18*2be43276SAndrew Rist  * under the License.
19*2be43276SAndrew Rist  *
20*2be43276SAndrew Rist  *************************************************************/
21*2be43276SAndrew Rist 
22*2be43276SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package com.sun.star.comp.connections;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.connection.ConnectionSetupException;
27cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
28cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir /**
31cdf0e10cSrcweir  * Helper class for <code>Acceptor</code> and <code>Connector</code>.
32cdf0e10cSrcweir  */
33cdf0e10cSrcweir final class Implementation {
34cdf0e10cSrcweir     /**
35cdf0e10cSrcweir      * Instantiate a service for a given connection type.
36cdf0e10cSrcweir      *
37cdf0e10cSrcweir      * @param factory the service factory used to instantiate the requested
38cdf0e10cSrcweir      *     service.
39cdf0e10cSrcweir      * @param description has the following format:
40cdf0e10cSrcweir      *     <code><var>type</var></code><!--
41cdf0e10cSrcweir      *         -->*(<code><var>key</var>=<var>value</var></code>).
42cdf0e10cSrcweir      *     The specific service implementation is instantiated through the
43cdf0e10cSrcweir      *     service factory as
44cdf0e10cSrcweir      *     <code>com.sun.star.connection.<var>type</var>service<var></var><!--
45cdf0e10cSrcweir      *         --></code>
46cdf0e10cSrcweir      *     (with <code><var>type</var></code> in lower case, and
47cdf0e10cSrcweir      *     <code><var>service</var></code> either <code>Acceptor</code> or
48cdf0e10cSrcweir      *     <code>Connector</code>).</p>
49cdf0e10cSrcweir      * @param serviceClass the IDL interface type for which to query the
50cdf0e10cSrcweir      *     requested service.
51cdf0e10cSrcweir      * @param serviceType must be either <code>Acceptor</code> or
52cdf0e10cSrcweir      *     <code>Connector</code>.
53cdf0e10cSrcweir      * @return an instance of the requested service.  Never returns
54cdf0e10cSrcweir      *     <code>null</code>.
55cdf0e10cSrcweir      * @throws ConnectionSetupException if the requested service can not be
56cdf0e10cSrcweir      *     found, or cannot be instantiated.
57cdf0e10cSrcweir      */
getConnectionService(XMultiServiceFactory factory, String description, Class serviceClass, String serviceType)58cdf0e10cSrcweir     public static Object getConnectionService(XMultiServiceFactory factory,
59cdf0e10cSrcweir                                               String description,
60cdf0e10cSrcweir                                               Class serviceClass,
61cdf0e10cSrcweir                                               String serviceType)
62cdf0e10cSrcweir         throws ConnectionSetupException
63cdf0e10cSrcweir     {
64cdf0e10cSrcweir         int i = description.indexOf(',');
65cdf0e10cSrcweir         String type
66cdf0e10cSrcweir             = (i < 0 ? description : description.substring(0, i)).toLowerCase();
67cdf0e10cSrcweir         Object service = null;
68cdf0e10cSrcweir         try {
69cdf0e10cSrcweir             service = UnoRuntime.queryInterface(
70cdf0e10cSrcweir                 serviceClass,
71cdf0e10cSrcweir                 factory.createInstance("com.sun.star.connection." + type
72cdf0e10cSrcweir                                        + serviceType));
73cdf0e10cSrcweir         } catch (RuntimeException e) {
74cdf0e10cSrcweir             throw e;
75cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
76cdf0e10cSrcweir         }
77cdf0e10cSrcweir         if (service == null) {
78cdf0e10cSrcweir             // As a fallback, also try to instantiate the service from the
79cdf0e10cSrcweir             // com.sun.star.lib.connections package structure:
80cdf0e10cSrcweir             try {
81cdf0e10cSrcweir                 service
82cdf0e10cSrcweir                     = Class.forName("com.sun.star.lib.connections." + type
83cdf0e10cSrcweir                                     + "." + type + serviceType).newInstance();
84cdf0e10cSrcweir             } catch (ClassNotFoundException e) {
85cdf0e10cSrcweir             } catch (IllegalAccessException e) {
86cdf0e10cSrcweir             } catch (InstantiationException e) {
87cdf0e10cSrcweir             }
88cdf0e10cSrcweir         }
89cdf0e10cSrcweir         if (service == null) {
90cdf0e10cSrcweir             throw new ConnectionSetupException("no " + serviceType + " for "
91cdf0e10cSrcweir                                                + type);
92cdf0e10cSrcweir         }
93cdf0e10cSrcweir         return service;
94cdf0e10cSrcweir     }
95cdf0e10cSrcweir 
Implementation()96cdf0e10cSrcweir     private Implementation() {} // do not instantiate
97cdf0e10cSrcweir }
98