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.urlresolver;
29 
30 
31 import com.sun.star.bridge.XBridge;
32 import com.sun.star.bridge.XBridgeFactory;
33 import com.sun.star.bridge.XUnoUrlResolver;
34 
35 import com.sun.star.comp.loader.FactoryHelper;
36 
37 import com.sun.star.connection.ConnectionSetupException;
38 import com.sun.star.connection.NoConnectException;
39 import com.sun.star.connection.XConnection;
40 import com.sun.star.connection.XConnector;
41 
42 import com.sun.star.lang.IllegalArgumentException;
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.UnoRuntime;
49 
50 
51 /**
52  * This component gives a factory for an <code>UnoUrlResolver</code> service.
53  * <p>
54  * @version 	$Revision: 1.6 $ $ $Date: 2008-04-11 11:12:25 $
55  * @author 	    Kay Ramme
56  * @see         com.sun.star.brige.XBrideFactory
57  * @see         com.sun.star.connection.Connector
58  * @since       UDK1.0
59  */
60 public class UrlResolver {
61 	static private final boolean DEBUG = false;
62 
63 
64 	static public class _UrlResolver implements XUnoUrlResolver {
65 		static private final String __serviceName = "com.sun.star.bridge.UnoUrlResolver";
66 
67 		private XMultiServiceFactory _xMultiServiceFactory;
68 
69 		public _UrlResolver(XMultiServiceFactory xMultiServiceFactory) {
70 			_xMultiServiceFactory = xMultiServiceFactory;
71 		}
72 
73 		public Object resolve(/*IN*/String dcp) throws NoConnectException, ConnectionSetupException, IllegalArgumentException, com.sun.star.uno.RuntimeException {
74 			String conDcp  ;
75 			String protDcp  ;
76 			String rootOid  ;
77 
78 			if(dcp.indexOf(';') == -1) {// use old style
79 				conDcp = dcp;
80 				protDcp = "iiop";
81 				rootOid = "classic_uno";
82 			}
83 			else { // new style
84 				int index = dcp.indexOf(':');
85 				String url = dcp.substring(0, index).trim();
86 				dcp = dcp.substring(index + 1).trim();
87 
88 				index = dcp.indexOf(';');
89 				conDcp = dcp.substring(0, index).trim();
90 				dcp = dcp.substring(index + 1).trim();
91 
92 				index = dcp.indexOf(';');
93 				protDcp = dcp.substring(0, index).trim();
94 				dcp = dcp.substring(index + 1).trim();
95 
96 				rootOid = dcp.trim().trim();
97 			}
98 
99 			Object rootObject  ;
100 			XBridgeFactory xBridgeFactory ;
101 			try {
102 				xBridgeFactory = UnoRuntime.queryInterface(XBridgeFactory.class,
103 																		  _xMultiServiceFactory.createInstance("com.sun.star.bridge.BridgeFactory"));
104 			} catch (com.sun.star.uno.Exception e) {
105 				throw new com.sun.star.uno.RuntimeException(e.getMessage());
106 			}
107 			XBridge xBridge = xBridgeFactory.getBridge(conDcp + ";" + protDcp);
108 
109 			if(xBridge == null) {
110 				Object connector ;
111 				try {
112 					connector = _xMultiServiceFactory.createInstance("com.sun.star.connection.Connector");
113 				} catch (com.sun.star.uno.Exception e) {
114 						throw new com.sun.star.uno.RuntimeException(e.getMessage());
115 				}
116 
117 				XConnector connector_xConnector = UnoRuntime.queryInterface(XConnector.class, connector);
118 
119 				// connect to the server
120 				XConnection xConnection = connector_xConnector.connect(conDcp);
121 				try {
122 					xBridge = xBridgeFactory.createBridge(conDcp + ";" + protDcp, protDcp, xConnection, null);
123 				} catch (com.sun.star.bridge.BridgeExistsException e) {
124 					throw new com.sun.star.uno.RuntimeException(e.getMessage());
125 				}
126 			}
127 			rootObject = xBridge.getInstance(rootOid);
128 			return rootObject;
129 		}
130 	}
131 
132 
133 	/**
134 	 * Gives a factory for creating the service.
135 	 * This method is called by the <code>JavaLoader</code>
136 	 * <p>
137 	 * @return  returns a <code>XSingleServiceFactory</code> for creating the component
138 	 * @param   implName     the name of the implementation for which a service is desired
139 	 * @param   multiFactory the service manager to be uses if needed
140 	 * @param   regKey       the registryKey
141 	 * @see                  com.sun.star.comp.loader.JavaLoader
142 	 */
143 	public static XSingleServiceFactory __getServiceFactory(String implName,
144 															XMultiServiceFactory multiFactory,
145 															XRegistryKey regKey)
146 	{
147 		XSingleServiceFactory xSingleServiceFactory = null;
148 
149   	    if (implName.equals(UrlResolver.class.getName()) )
150 	        xSingleServiceFactory = FactoryHelper.getServiceFactory(_UrlResolver.class,
151 																	_UrlResolver.__serviceName,
152 																	multiFactory,
153 																	regKey);
154 
155 	    return xSingleServiceFactory;
156 	}
157 }
158