merge the formfield patch from ooo-build
[ooovba.git] / jurt / com / sun / star / comp / connections / Implementation.java
blob45379c8d52328f5af4f711f40a424eda1a294ec2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Implementation.java,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package com.sun.star.comp.connections;
33 import com.sun.star.connection.ConnectionSetupException;
34 import com.sun.star.lang.XMultiServiceFactory;
35 import com.sun.star.uno.UnoRuntime;
37 /**
38 * Helper class for <code>Acceptor</code> and <code>Connector</code>.
40 final class Implementation {
41 /**
42 * Instantiate a service for a given connection type.
44 * @param factory the service factory used to instantiate the requested
45 * service.
46 * @param description has the following format:
47 * <code><var>type</var></code><!--
48 * -->*(<code><var>key</var>=<var>value</var></code>).
49 * The specific service implementation is instantiated through the
50 * service factory as
51 * <code>com.sun.star.connection.<var>type</var>service<var></var><!--
52 * --></code>
53 * (with <code><var>type</var></code> in lower case, and
54 * <code><var>service</var></code> either <code>Acceptor</code> or
55 * <code>Connector</code>).</p>
56 * @param serviceClass the IDL interface type for which to query the
57 * requested service.
58 * @param serviceType must be either <code>Acceptor</code> or
59 * <code>Connector</code>.
60 * @return an instance of the requested service. Never returns
61 * <code>null</code>.
62 * @throws ConnectionSetupException if the requested service can not be
63 * found, or cannot be instantiated.
65 public static Object getConnectionService(XMultiServiceFactory factory,
66 String description,
67 Class serviceClass,
68 String serviceType)
69 throws ConnectionSetupException
71 int i = description.indexOf(',');
72 String type
73 = (i < 0 ? description : description.substring(0, i)).toLowerCase();
74 Object service = null;
75 try {
76 service = UnoRuntime.queryInterface(
77 serviceClass,
78 factory.createInstance("com.sun.star.connection." + type
79 + serviceType));
80 } catch (RuntimeException e) {
81 throw e;
82 } catch (com.sun.star.uno.Exception e) {
84 if (service == null) {
85 // As a fallback, also try to instantiate the service from the
86 // com.sun.star.lib.connections package structure:
87 try {
88 service
89 = Class.forName("com.sun.star.lib.connections." + type
90 + "." + type + serviceType).newInstance();
91 } catch (ClassNotFoundException e) {
92 } catch (IllegalAccessException e) {
93 } catch (InstantiationException e) {
96 if (service == null) {
97 throw new ConnectionSetupException("no " + serviceType + " for "
98 + type);
100 return service;
103 private Implementation() {} // do not instantiate