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