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