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 10cdf0e10cSrcweir * 11*2be43276SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 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. 19cdf0e10cSrcweir * 20*2be43276SAndrew Rist *************************************************************/ 21*2be43276SAndrew Rist 22*2be43276SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package com.sun.star.comp.bridgefactory; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import java.math.BigInteger; 27cdf0e10cSrcweir import java.util.Vector; 28cdf0e10cSrcweir 29cdf0e10cSrcweir 30cdf0e10cSrcweir import com.sun.star.bridge.BridgeExistsException; 31cdf0e10cSrcweir import com.sun.star.bridge.XBridge; 32cdf0e10cSrcweir import com.sun.star.bridge.XBridgeFactory; 33cdf0e10cSrcweir import com.sun.star.bridge.XInstanceProvider; 34cdf0e10cSrcweir 35cdf0e10cSrcweir import com.sun.star.comp.loader.FactoryHelper; 36cdf0e10cSrcweir 37cdf0e10cSrcweir import com.sun.star.connection.XConnection; 38cdf0e10cSrcweir 39cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 40cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory; 41cdf0e10cSrcweir 42cdf0e10cSrcweir import com.sun.star.registry.XRegistryKey; 43cdf0e10cSrcweir 44cdf0e10cSrcweir import com.sun.star.uno.IBridge; 45cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 46cdf0e10cSrcweir 47cdf0e10cSrcweir 48cdf0e10cSrcweir /** 49cdf0e10cSrcweir * The BridgeFactory class implements the <code>XBridgeFactory</code> Interface. 50cdf0e10cSrcweir * It wrapps the <code>UnoRuntime#getBridgeByName</code>method and delivers a 51cdf0e10cSrcweir * XBridge component. 52cdf0e10cSrcweir * <p> 53cdf0e10cSrcweir * This component is only usable for remote bridges. 54cdf0e10cSrcweir * <p> 55cdf0e10cSrcweir * @version $Revision: 1.11 $ $ $Date: 2008-04-11 11:07:51 $ 56cdf0e10cSrcweir * @author Kay Ramme 57cdf0e10cSrcweir * @see com.sun.star.uno.UnoRuntime 58cdf0e10cSrcweir * @since UDK1.0 59cdf0e10cSrcweir */ 60cdf0e10cSrcweir public class BridgeFactory implements XBridgeFactory/*, XEventListener*/ { 61cdf0e10cSrcweir static private final boolean DEBUG = false; 62cdf0e10cSrcweir 63cdf0e10cSrcweir /** 64cdf0e10cSrcweir * The name of the service, the <code>JavaLoader</code> acceses this through reflection. 65cdf0e10cSrcweir */ 66cdf0e10cSrcweir public final static String __serviceName = "com.sun.star.bridge.BridgeFactory"; 67cdf0e10cSrcweir 68cdf0e10cSrcweir /** 69cdf0e10cSrcweir * Gives a factory for creating the service. 70cdf0e10cSrcweir * This method is called by the <code>JavaLoader</code> 71cdf0e10cSrcweir * <p> 72cdf0e10cSrcweir * @return returns a <code>XSingleServiceFactory</code> for creating the component 73cdf0e10cSrcweir * @param implName the name of the implementation for which a service is desired 74cdf0e10cSrcweir * @param multiFactory the service manager to be uses if needed 75cdf0e10cSrcweir * @param regKey the registryKey 76cdf0e10cSrcweir * @see com.sun.star.comp.loader.JavaLoader 77cdf0e10cSrcweir */ __getServiceFactory(String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey)78cdf0e10cSrcweir public static XSingleServiceFactory __getServiceFactory(String implName, 79cdf0e10cSrcweir XMultiServiceFactory multiFactory, 80cdf0e10cSrcweir XRegistryKey regKey) 81cdf0e10cSrcweir { 82cdf0e10cSrcweir XSingleServiceFactory xSingleServiceFactory = null; 83cdf0e10cSrcweir 84cdf0e10cSrcweir if (implName.equals(BridgeFactory.class.getName()) ) 85cdf0e10cSrcweir xSingleServiceFactory = FactoryHelper.getServiceFactory(BridgeFactory.class, 86cdf0e10cSrcweir multiFactory, 87cdf0e10cSrcweir regKey); 88cdf0e10cSrcweir 89cdf0e10cSrcweir return xSingleServiceFactory; 90cdf0e10cSrcweir } 91cdf0e10cSrcweir 92cdf0e10cSrcweir /** 93cdf0e10cSrcweir * Creates a remote bridge and memorizes it under <code>sName</code>. 94cdf0e10cSrcweir * <p> 95cdf0e10cSrcweir * @return the bridge 96cdf0e10cSrcweir * @param sName the name to memorize the bridge 97cdf0e10cSrcweir * @param sProtocol the protocol the bridge should use 98cdf0e10cSrcweir * @param anInstanceProvider the instance provider 99cdf0e10cSrcweir * @see com.sun.star.bridge.XBridgeFactory 100cdf0e10cSrcweir */ createBridge(String sName, String sProtocol, XConnection aConnection, XInstanceProvider anInstanceProvider)101cdf0e10cSrcweir public XBridge createBridge(String sName, String sProtocol, XConnection aConnection, XInstanceProvider anInstanceProvider) throws 102cdf0e10cSrcweir BridgeExistsException, 103cdf0e10cSrcweir com.sun.star.lang.IllegalArgumentException, 104cdf0e10cSrcweir com.sun.star.uno.RuntimeException 105cdf0e10cSrcweir { 106cdf0e10cSrcweir boolean hasName = sName.length() != 0; 107cdf0e10cSrcweir Object context = hasName ? sName : new UniqueToken(); 108cdf0e10cSrcweir // UnoRuntime.getBridgeByName internally uses context.toString() to 109cdf0e10cSrcweir // distinguish bridges, so the result of 110cdf0e10cSrcweir // new UniqueToken().toString() might clash with an explicit 111cdf0e10cSrcweir // sName.toString(), but the UnoRuntime bridge management is 112cdf0e10cSrcweir // obsolete anyway and should be removed 113cdf0e10cSrcweir 114cdf0e10cSrcweir // do not create a new bridge, if one already exists 115cdf0e10cSrcweir if (hasName) { 116cdf0e10cSrcweir IBridge iBridges[] = UnoRuntime.getBridges(); 117cdf0e10cSrcweir for(int i = 0; i < iBridges.length; ++ i) { 118cdf0e10cSrcweir XBridge xBridge = UnoRuntime.queryInterface(XBridge.class, iBridges[i]); 119cdf0e10cSrcweir 120cdf0e10cSrcweir if(xBridge != null) { 121cdf0e10cSrcweir if(xBridge.getName().equals(sName)) 122cdf0e10cSrcweir throw new BridgeExistsException(sName + " already exists"); 123cdf0e10cSrcweir } 124cdf0e10cSrcweir } 125cdf0e10cSrcweir } 126cdf0e10cSrcweir 127cdf0e10cSrcweir XBridge xBridge; 128cdf0e10cSrcweir 129cdf0e10cSrcweir try { 130cdf0e10cSrcweir IBridge iBridge = UnoRuntime.getBridgeByName("java", context, "remote", context, hasName ? new Object[]{sProtocol, aConnection, anInstanceProvider, sName} : new Object[]{sProtocol, aConnection, anInstanceProvider}); 131cdf0e10cSrcweir 132cdf0e10cSrcweir xBridge = UnoRuntime.queryInterface(XBridge.class, iBridge); 133cdf0e10cSrcweir } 134cdf0e10cSrcweir catch(Exception exception) { 135cdf0e10cSrcweir throw new com.sun.star.lang.IllegalArgumentException(exception.getMessage()); 136cdf0e10cSrcweir } 137cdf0e10cSrcweir 138cdf0e10cSrcweir if(DEBUG) System.err.println("##### " + getClass().getName() + ".createBridge:" + sName + " " + sProtocol + " " + aConnection + " " + anInstanceProvider + " " + xBridge); 139cdf0e10cSrcweir 140cdf0e10cSrcweir return xBridge; 141cdf0e10cSrcweir } 142cdf0e10cSrcweir 143cdf0e10cSrcweir /** 144cdf0e10cSrcweir * Gets a remote bridge which must already exist. 145cdf0e10cSrcweir * <p> 146cdf0e10cSrcweir * @return the bridge 147cdf0e10cSrcweir * @param sName the name of the bridge 148cdf0e10cSrcweir * @see com.sun.star.bridge.XBridgeFactory 149cdf0e10cSrcweir */ getBridge(String sName)150cdf0e10cSrcweir public XBridge getBridge(String sName) throws com.sun.star.uno.RuntimeException { 151cdf0e10cSrcweir XBridge xBridge = null; 152cdf0e10cSrcweir 153cdf0e10cSrcweir IBridge iBridges[] = UnoRuntime.getBridges(); 154cdf0e10cSrcweir for(int i = 0; i < iBridges.length; ++ i) { 155cdf0e10cSrcweir xBridge = UnoRuntime.queryInterface(XBridge.class, iBridges[i]); 156cdf0e10cSrcweir 157cdf0e10cSrcweir if(xBridge != null) { 158cdf0e10cSrcweir if(xBridge.getName().equals(sName)) 159cdf0e10cSrcweir break; 160cdf0e10cSrcweir 161cdf0e10cSrcweir else 162cdf0e10cSrcweir xBridge = null; 163cdf0e10cSrcweir } 164cdf0e10cSrcweir } 165cdf0e10cSrcweir 166cdf0e10cSrcweir 167cdf0e10cSrcweir if(DEBUG) System.err.println("##### " + getClass().getName() + ".getBridge:" + sName + " " + xBridge); 168cdf0e10cSrcweir 169cdf0e10cSrcweir return xBridge; 170cdf0e10cSrcweir } 171cdf0e10cSrcweir 172cdf0e10cSrcweir /** 173cdf0e10cSrcweir * Gives all created bridges 174cdf0e10cSrcweir * <p> 175cdf0e10cSrcweir * @return the bridges 176cdf0e10cSrcweir * @see com.sun.star.bridge.XBridgeFactory 177cdf0e10cSrcweir */ getExistingBridges()178cdf0e10cSrcweir public synchronized XBridge[] getExistingBridges() throws com.sun.star.uno.RuntimeException { 179cdf0e10cSrcweir Vector vector = new Vector(); 180cdf0e10cSrcweir 181cdf0e10cSrcweir IBridge iBridges[] = UnoRuntime.getBridges(); 182cdf0e10cSrcweir for(int i = 0; i < iBridges.length; ++ i) { 183cdf0e10cSrcweir XBridge xBridge = UnoRuntime.queryInterface(XBridge.class, iBridges[i]); 184cdf0e10cSrcweir 185cdf0e10cSrcweir if(xBridge != null) 186cdf0e10cSrcweir vector.addElement(xBridge); 187cdf0e10cSrcweir } 188cdf0e10cSrcweir 189cdf0e10cSrcweir XBridge xBridges[]= new XBridge[vector.size()]; 190cdf0e10cSrcweir for(int i = 0; i < vector.size(); ++ i) 191cdf0e10cSrcweir xBridges[i] = (XBridge)vector.elementAt(i); 192cdf0e10cSrcweir 193cdf0e10cSrcweir return xBridges; 194cdf0e10cSrcweir } 195cdf0e10cSrcweir 196cdf0e10cSrcweir private static final class UniqueToken { UniqueToken()197cdf0e10cSrcweir public UniqueToken() { 198cdf0e10cSrcweir synchronized (UniqueToken.class) { 199cdf0e10cSrcweir token = counter.toString(); 200cdf0e10cSrcweir counter = counter.add(BigInteger.ONE); 201cdf0e10cSrcweir } 202cdf0e10cSrcweir } 203cdf0e10cSrcweir toString()204cdf0e10cSrcweir public String toString() { 205cdf0e10cSrcweir return token; 206cdf0e10cSrcweir } 207cdf0e10cSrcweir 208cdf0e10cSrcweir private final String token; 209cdf0e10cSrcweir private static BigInteger counter = BigInteger.ZERO; 210cdf0e10cSrcweir } 211cdf0e10cSrcweir } 212cdf0e10cSrcweir 213