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