bump product version to 6.4.0.3
[LibreOffice.git] / jurt / com / sun / star / comp / bridgefactory / BridgeFactory.java
blob031e60d3f1dc57088d179550587e01e47626333b
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 package com.sun.star.comp.bridgefactory;
22 import java.math.BigInteger;
23 import java.util.ArrayList;
25 import com.sun.star.bridge.BridgeExistsException;
26 import com.sun.star.bridge.XBridge;
27 import com.sun.star.bridge.XBridgeFactory;
28 import com.sun.star.bridge.XInstanceProvider;
29 import com.sun.star.comp.loader.FactoryHelper;
30 import com.sun.star.connection.XConnection;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.lang.XSingleServiceFactory;
33 import com.sun.star.registry.XRegistryKey;
34 import com.sun.star.uno.IBridge;
35 import com.sun.star.uno.UnoRuntime;
38 /**
39 * The BridgeFactory class implements the <code>XBridgeFactory</code> Interface.
41 * <p>It wraps the <code>UnoRuntime#getBridgeByName</code>method and delivers a
42 * XBridge component.</p>
44 * <p>This component is only usable for remote bridges.</p>
46 * @see com.sun.star.uno.UnoRuntime
47 * @since UDK1.0
49 public class BridgeFactory implements XBridgeFactory/*, XEventListener*/ {
50 private static final boolean DEBUG = false;
52 /**
53 * The name of the service, the <code>JavaLoader</code> accesses this through
54 * reflection.
56 public static final String __serviceName = "com.sun.star.bridge.BridgeFactory";
58 /**
59 * Gives a factory for creating the service.
61 * <p>This method is called by the <code>JavaLoader</code>.</p>
63 * @param implName the name of the implementation for which a service is desired.
64 * @param multiFactory the service manager to be uses if needed.
65 * @param regKey the registryKey.
66 * @return returns a <code>XSingleServiceFactory</code> for creating the component.
68 * @see com.sun.star.comp.loader.JavaLoader
70 public static XSingleServiceFactory __getServiceFactory(String implName,
71 XMultiServiceFactory multiFactory,
72 XRegistryKey regKey)
74 XSingleServiceFactory xSingleServiceFactory = null;
76 if (implName.equals(BridgeFactory.class.getName()) )
77 xSingleServiceFactory = FactoryHelper.getServiceFactory(BridgeFactory.class,
78 multiFactory,
79 regKey);
81 return xSingleServiceFactory;
84 /**
85 * Creates a remote bridge and memorizes it under <code>sName</code>.
87 * @param sName the name to memorize the bridge.
88 * @param sProtocol the protocol the bridge should use.
89 * @param anInstanceProvider the instance provider.
90 * @return the bridge.
92 * @see com.sun.star.bridge.XBridgeFactory
94 public XBridge createBridge(String sName, String sProtocol, XConnection aConnection, XInstanceProvider anInstanceProvider) throws
95 BridgeExistsException,
96 com.sun.star.lang.IllegalArgumentException,
97 com.sun.star.uno.RuntimeException
99 boolean hasName = sName.length() != 0;
100 Object context = hasName ? sName : new UniqueToken();
101 // UnoRuntime.getBridgeByName internally uses context.toString() to
102 // distinguish bridges, so the result of
103 // new UniqueToken().toString() might clash with an explicit
104 // sName.toString(), but the UnoRuntime bridge management is
105 // obsolete anyway and should be removed
107 // do not create a new bridge, if one already exists
108 if (hasName) {
109 IBridge iBridges[] = UnoRuntime.getBridges();
110 for(int i = 0; i < iBridges.length; ++ i) {
111 XBridge xBridge = UnoRuntime.queryInterface(XBridge.class, iBridges[i]);
113 if(xBridge != null && xBridge.getName().equals(sName)) {
114 throw new BridgeExistsException(sName + " already exists");
119 XBridge xBridge;
121 try {
122 IBridge iBridge = UnoRuntime.getBridgeByName("java", context, "remote", context, hasName ? new Object[]{sProtocol, aConnection, anInstanceProvider, sName} : new Object[]{sProtocol, aConnection, anInstanceProvider});
124 xBridge = UnoRuntime.queryInterface(XBridge.class, iBridge);
126 catch (Exception e) {
127 throw new com.sun.star.lang.IllegalArgumentException(e, e.getMessage());
130 if(DEBUG) System.err.println("##### " + getClass().getName() + ".createBridge:" + sName + " " + sProtocol + " " + aConnection + " " + anInstanceProvider + " " + xBridge);
132 return xBridge;
136 * Gets a remote bridge which must already exist.
138 * @param sName the name of the bridge.
139 * @return the bridge.
140 * @see com.sun.star.bridge.XBridgeFactory
142 public XBridge getBridge(String sName) throws com.sun.star.uno.RuntimeException {
143 XBridge xBridge = null;
145 IBridge iBridges[] = UnoRuntime.getBridges();
146 for(int i = 0; i < iBridges.length; ++ i) {
147 xBridge = UnoRuntime.queryInterface(XBridge.class, iBridges[i]);
149 if(xBridge != null) {
150 if(xBridge.getName().equals(sName))
151 break;
153 else
154 xBridge = null;
159 if(DEBUG) System.err.println("##### " + getClass().getName() + ".getBridge:" + sName + " " + xBridge);
161 return xBridge;
165 * Gives all created bridges.
167 * @return the bridges.
168 * @see com.sun.star.bridge.XBridgeFactory
170 public synchronized XBridge[] getExistingBridges() throws com.sun.star.uno.RuntimeException {
171 ArrayList<XBridge> vector = new ArrayList<XBridge>();
173 IBridge iBridges[] = UnoRuntime.getBridges();
174 for(int i = 0; i < iBridges.length; ++ i) {
175 XBridge xBridge = UnoRuntime.queryInterface(XBridge.class, iBridges[i]);
177 if(xBridge != null)
178 vector.add(xBridge);
181 XBridge xBridges[]= new XBridge[vector.size()];
182 for(int i = 0; i < vector.size(); ++ i)
183 xBridges[i] = vector.get(i);
185 return xBridges;
188 private static final class UniqueToken {
189 public UniqueToken() {
190 synchronized (UniqueToken.class) {
191 token = counter.toString();
192 counter = counter.add(BigInteger.ONE);
197 * Returns a string representation of the object.
199 * @return a string representation of the object.
200 * @see java.lang.Object#toString
202 @Override
203 public String toString() {
204 return token;
207 private final String token;
208 private static BigInteger counter = BigInteger.ZERO;
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */