Update ooo320-m1
[ooovba.git] / jurt / com / sun / star / comp / bridgefactory / BridgeFactory.java
blob4b86c7e8743afed19b20bd73a1884a4624d4ac9f
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: BridgeFactory.java,v $
10 * $Revision: 1.11 $
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.bridgefactory;
33 import java.math.BigInteger;
34 import java.util.Vector;
37 import com.sun.star.bridge.BridgeExistsException;
38 import com.sun.star.bridge.XBridge;
39 import com.sun.star.bridge.XBridgeFactory;
40 import com.sun.star.bridge.XInstanceProvider;
42 import com.sun.star.comp.loader.FactoryHelper;
44 import com.sun.star.connection.XConnection;
46 import com.sun.star.lang.XMultiServiceFactory;
47 import com.sun.star.lang.XSingleServiceFactory;
49 import com.sun.star.registry.XRegistryKey;
51 import com.sun.star.uno.IBridge;
52 import com.sun.star.uno.UnoRuntime;
55 /**
56 * The BridgeFactory class implements the <code>XBridgeFactory</code> Interface.
57 * It wrapps the <code>UnoRuntime#getBridgeByName</code>method and delivers a
58 * XBridge component.
59 * <p>
60 * This component is only usable for remote bridges.
61 * <p>
62 * @version $Revision: 1.11 $ $ $Date: 2008-04-11 11:07:51 $
63 * @author Kay Ramme
64 * @see com.sun.star.uno.UnoRuntime
65 * @since UDK1.0
67 public class BridgeFactory implements XBridgeFactory/*, XEventListener*/ {
68 static private final boolean DEBUG = false;
70 /**
71 * The name of the service, the <code>JavaLoader</code> acceses this through reflection.
73 public final static String __serviceName = "com.sun.star.bridge.BridgeFactory";
75 /**
76 * Gives a factory for creating the service.
77 * This method is called by the <code>JavaLoader</code>
78 * <p>
79 * @return returns a <code>XSingleServiceFactory</code> for creating the component
80 * @param implName the name of the implementation for which a service is desired
81 * @param multiFactory the service manager to be uses if needed
82 * @param regKey the registryKey
83 * @see com.sun.star.comp.loader.JavaLoader
85 public static XSingleServiceFactory __getServiceFactory(String implName,
86 XMultiServiceFactory multiFactory,
87 XRegistryKey regKey)
89 XSingleServiceFactory xSingleServiceFactory = null;
91 if (implName.equals(BridgeFactory.class.getName()) )
92 xSingleServiceFactory = FactoryHelper.getServiceFactory(BridgeFactory.class,
93 multiFactory,
94 regKey);
96 return xSingleServiceFactory;
99 /**
100 * Writes the service information into the given registry key.
101 * This method is called by the <code>JavaLoader</code>
102 * <p>
103 * @return returns true if the operation succeeded
104 * @param regKey the registryKey
105 * @see com.sun.star.comp.loader.JavaLoader
107 public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
108 return FactoryHelper.writeRegistryServiceInfo(BridgeFactory.class.getName(), __serviceName, regKey);
112 * Creates a remote bridge and memorizes it under <code>sName</code>.
113 * <p>
114 * @return the bridge
115 * @param sName the name to memorize the bridge
116 * @param sProtocol the protocol the bridge should use
117 * @param anInstanceProvider the instance provider
118 * @see com.sun.star.bridge.XBridgeFactory
120 public XBridge createBridge(String sName, String sProtocol, XConnection aConnection, XInstanceProvider anInstanceProvider) throws
121 BridgeExistsException,
122 com.sun.star.lang.IllegalArgumentException,
123 com.sun.star.uno.RuntimeException
125 boolean hasName = sName.length() != 0;
126 Object context = hasName ? (Object) sName : (Object) new UniqueToken();
127 // UnoRuntime.getBridgeByName internally uses context.toString() to
128 // distinguish bridges, so the result of
129 // new UniqueToken().toString() might clash with an explicit
130 // sName.toString(), but the UnoRuntime bridge management is
131 // obsolete anyway and should be removed
133 // do not create a new bridge, if one already exists
134 if (hasName) {
135 IBridge iBridges[] = UnoRuntime.getBridges();
136 for(int i = 0; i < iBridges.length; ++ i) {
137 XBridge xBridge = UnoRuntime.queryInterface(XBridge.class, iBridges[i]);
139 if(xBridge != null) {
140 if(xBridge.getName().equals(sName))
141 throw new BridgeExistsException(sName + " already exists");
146 XBridge xBridge = null;
148 try {
149 IBridge iBridge = UnoRuntime.getBridgeByName("java", context, "remote", context, hasName ? new Object[]{sProtocol, aConnection, anInstanceProvider, sName} : new Object[]{sProtocol, aConnection, anInstanceProvider});
151 xBridge = UnoRuntime.queryInterface(XBridge.class, iBridge);
153 catch(Exception exception) {
154 throw new com.sun.star.lang.IllegalArgumentException(exception.getMessage());
157 if(DEBUG) System.err.println("##### " + getClass().getName() + ".createBridge:" + sName + " " + sProtocol + " " + aConnection + " " + anInstanceProvider + " " + xBridge);
159 return xBridge;
163 * Gets a remote bridge which must already exist.
164 * <p>
165 * @return the bridge
166 * @param sName the name of the bridge
167 * @see com.sun.star.bridge.XBridgeFactory
169 public XBridge getBridge(String sName) throws com.sun.star.uno.RuntimeException {
170 XBridge xBridge = null;
172 IBridge iBridges[] = UnoRuntime.getBridges();
173 for(int i = 0; i < iBridges.length; ++ i) {
174 xBridge = UnoRuntime.queryInterface(XBridge.class, iBridges[i]);
176 if(xBridge != null) {
177 if(xBridge.getName().equals(sName))
178 break;
180 else
181 xBridge = null;
186 if(DEBUG) System.err.println("##### " + getClass().getName() + ".getBridge:" + sName + " " + xBridge);
188 return xBridge;
192 * Gives all created bridges
193 * <p>
194 * @return the bridges
195 * @see com.sun.star.bridge.XBridgeFactory
197 public synchronized XBridge[] getExistingBridges() throws com.sun.star.uno.RuntimeException {
198 Vector vector = new Vector();
200 IBridge iBridges[] = UnoRuntime.getBridges();
201 for(int i = 0; i < iBridges.length; ++ i) {
202 XBridge xBridge = UnoRuntime.queryInterface(XBridge.class, iBridges[i]);
204 if(xBridge != null)
205 vector.addElement(xBridge);
208 XBridge xBridges[]= new XBridge[vector.size()];
209 for(int i = 0; i < vector.size(); ++ i)
210 xBridges[i] = (XBridge)vector.elementAt(i);
212 return xBridges;
215 private static final class UniqueToken {
216 public UniqueToken() {
217 synchronized (UniqueToken.class) {
218 token = counter.toString();
219 counter = counter.add(BigInteger.ONE);
223 public String toString() {
224 return token;
227 private final String token;
228 private static BigInteger counter = BigInteger.ZERO;