bump product version to 5.0.4.1
[LibreOffice.git] / jurt / com / sun / star / comp / bridgefactory / BridgeFactory.java
blobd84b7397e3ed99d1cfd658a3421d77b472e72826
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package com.sun.star.comp.bridgefactory;
21 import java.math.BigInteger;
22 import java.util.ArrayList;
24 import com.sun.star.bridge.BridgeExistsException;
25 import com.sun.star.bridge.XBridge;
26 import com.sun.star.bridge.XBridgeFactory;
27 import com.sun.star.bridge.XInstanceProvider;
28 import com.sun.star.comp.loader.FactoryHelper;
29 import com.sun.star.connection.XConnection;
30 import com.sun.star.lang.XMultiServiceFactory;
31 import com.sun.star.lang.XSingleServiceFactory;
32 import com.sun.star.registry.XRegistryKey;
33 import com.sun.star.uno.IBridge;
34 import com.sun.star.uno.UnoRuntime;
37 /**
38 * The BridgeFactory class implements the <code>XBridgeFactory</code> Interface.
40 * <p>It wrapps the <code>UnoRuntime#getBridgeByName</code>method and delivers a
41 * XBridge component.</p>
43 * <p>This component is only usable for remote bridges.</p>
45 * @see com.sun.star.uno.UnoRuntime
46 * @since UDK1.0
48 public class BridgeFactory implements XBridgeFactory/*, XEventListener*/ {
49 static private final boolean DEBUG = false;
51 /**
52 * The name of the service, the <code>JavaLoader</code> accesses this through
53 * reflection.
55 public final static String __serviceName = "com.sun.star.bridge.BridgeFactory";
57 /**
58 * Gives a factory for creating the service.
60 * <p>This method is called by the <code>JavaLoader</code>.</p>
62 * @param implName the name of the implementation for which a service is desired.
63 * @param multiFactory the service manager to be uses if needed.
64 * @param regKey the registryKey.
65 * @return returns a <code>XSingleServiceFactory</code> for creating the component.
67 * @see com.sun.star.comp.loader.JavaLoader
69 public static XSingleServiceFactory __getServiceFactory(String implName,
70 XMultiServiceFactory multiFactory,
71 XRegistryKey regKey)
73 XSingleServiceFactory xSingleServiceFactory = null;
75 if (implName.equals(BridgeFactory.class.getName()) )
76 xSingleServiceFactory = FactoryHelper.getServiceFactory(BridgeFactory.class,
77 multiFactory,
78 regKey);
80 return xSingleServiceFactory;
83 /**
84 * Creates a remote bridge and memorizes it under <code>sName</code>.
86 * @param sName the name to memorize the bridge.
87 * @param sProtocol the protocol the bridge should use.
88 * @param anInstanceProvider the instance provider.
89 * @return the bridge.
91 * @see com.sun.star.bridge.XBridgeFactory
93 public XBridge createBridge(String sName, String sProtocol, XConnection aConnection, XInstanceProvider anInstanceProvider) throws
94 BridgeExistsException,
95 com.sun.star.lang.IllegalArgumentException,
96 com.sun.star.uno.RuntimeException
98 boolean hasName = sName.length() != 0;
99 Object context = hasName ? sName : new UniqueToken();
100 // UnoRuntime.getBridgeByName internally uses context.toString() to
101 // distinguish bridges, so the result of
102 // new UniqueToken().toString() might clash with an explicit
103 // sName.toString(), but the UnoRuntime bridge management is
104 // obsolete anyway and should be removed
106 // do not create a new bridge, if one already exists
107 if (hasName) {
108 IBridge iBridges[] = UnoRuntime.getBridges();
109 for(int i = 0; i < iBridges.length; ++ i) {
110 XBridge xBridge = UnoRuntime.queryInterface(XBridge.class, iBridges[i]);
112 if(xBridge != null) {
113 if(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;