1d47d18a2SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3d47d18a2SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4d47d18a2SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5d47d18a2SAndrew Rist  * distributed with this work for additional information
6d47d18a2SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7d47d18a2SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8d47d18a2SAndrew Rist  * "License"); you may not use this file except in compliance
9d47d18a2SAndrew Rist  * with the License.  You may obtain a copy of the License at
10d47d18a2SAndrew Rist  *
11d47d18a2SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12d47d18a2SAndrew Rist  *
13d47d18a2SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14d47d18a2SAndrew Rist  * software distributed under the License is distributed on an
15d47d18a2SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16d47d18a2SAndrew Rist  * KIND, either express or implied.  See the License for the
17d47d18a2SAndrew Rist  * specific language governing permissions and limitations
18d47d18a2SAndrew Rist  * under the License.
19d47d18a2SAndrew Rist  *
20d47d18a2SAndrew Rist  *************************************************************/
21d47d18a2SAndrew Rist 
22d47d18a2SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir //  #define TRACE(x) OSL_TRACE(x)
25cdf0e10cSrcweir #define TRACE(x)
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <osl/diagnose.h>
28cdf0e10cSrcweir #include <osl/mutex.hxx>
29cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
30cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
31cdf0e10cSrcweir #include <cppuhelper/implementationentry.hxx>
32cdf0e10cSrcweir #include "cppuhelper/unourl.hxx"
33cdf0e10cSrcweir #include "rtl/malformeduriexception.hxx"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
36cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
37cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
38cdf0e10cSrcweir #include <com/sun/star/connection/XConnector.hpp>
39cdf0e10cSrcweir #include <com/sun/star/bridge/XBridgeFactory.hpp>
40cdf0e10cSrcweir #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir using namespace cppu;
43cdf0e10cSrcweir using namespace rtl;
44cdf0e10cSrcweir using namespace osl;
45cdf0e10cSrcweir using namespace com::sun::star::uno;
46cdf0e10cSrcweir using namespace com::sun::star::lang;
47cdf0e10cSrcweir using namespace com::sun::star::connection;
48cdf0e10cSrcweir using namespace com::sun::star::bridge;
49cdf0e10cSrcweir using namespace com::sun::star::registry;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir #define SERVICENAME		"com.sun.star.bridge.UnoUrlResolver"
52cdf0e10cSrcweir #define IMPLNAME		"com.sun.star.comp.bridge.UnoUrlResolver"
53cdf0e10cSrcweir 
54cdf0e10cSrcweir namespace unourl_resolver
55cdf0e10cSrcweir {
56cdf0e10cSrcweir 	rtl_StandardModuleCount g_moduleCount = MODULE_COUNT_INIT;
57cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
resolver_getSupportedServiceNames()58cdf0e10cSrcweir Sequence< OUString > resolver_getSupportedServiceNames()
59cdf0e10cSrcweir {
60cdf0e10cSrcweir 	static Sequence < OUString > *pNames = 0;
61cdf0e10cSrcweir 	if( ! pNames )
62cdf0e10cSrcweir 	{
63cdf0e10cSrcweir 		MutexGuard guard( Mutex::getGlobalMutex() );
64cdf0e10cSrcweir 		if( !pNames )
65cdf0e10cSrcweir 		{
66cdf0e10cSrcweir 			static Sequence< OUString > seqNames(1);
67cdf0e10cSrcweir 			seqNames.getArray()[0] = OUString(RTL_CONSTASCII_USTRINGPARAM(SERVICENAME));
68cdf0e10cSrcweir 			pNames = &seqNames;
69cdf0e10cSrcweir 		}
70cdf0e10cSrcweir 	}
71cdf0e10cSrcweir 	return *pNames;
72cdf0e10cSrcweir }
73cdf0e10cSrcweir 
resolver_getImplementationName()74cdf0e10cSrcweir OUString resolver_getImplementationName()
75cdf0e10cSrcweir {
76cdf0e10cSrcweir 	static OUString *pImplName = 0;
77cdf0e10cSrcweir 	if( ! pImplName )
78cdf0e10cSrcweir 	{
79cdf0e10cSrcweir 		MutexGuard guard( Mutex::getGlobalMutex() );
80cdf0e10cSrcweir 		if( ! pImplName )
81cdf0e10cSrcweir 		{
82cdf0e10cSrcweir 			static OUString implName(
83cdf0e10cSrcweir 				RTL_CONSTASCII_USTRINGPARAM( IMPLNAME ) );
84cdf0e10cSrcweir 			pImplName = &implName;
85cdf0e10cSrcweir 		}
86cdf0e10cSrcweir 	}
87cdf0e10cSrcweir 	return *pImplName;
88cdf0e10cSrcweir }
89cdf0e10cSrcweir 
90cdf0e10cSrcweir //==================================================================================================
91cdf0e10cSrcweir class ResolverImpl : public WeakImplHelper2< XServiceInfo, XUnoUrlResolver >
92cdf0e10cSrcweir {
93cdf0e10cSrcweir 	Reference< XMultiComponentFactory > _xSMgr;
94cdf0e10cSrcweir 	Reference< XComponentContext > _xCtx;
95cdf0e10cSrcweir 
96cdf0e10cSrcweir public:
97cdf0e10cSrcweir 	ResolverImpl( const Reference< XComponentContext > & xSMgr );
98cdf0e10cSrcweir 	virtual ~ResolverImpl();
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 	// XServiceInfo
101cdf0e10cSrcweir 	virtual OUString SAL_CALL getImplementationName() throw(::com::sun::star::uno::RuntimeException);
102cdf0e10cSrcweir 	virtual sal_Bool SAL_CALL supportsService( const OUString & rServiceName ) throw(::com::sun::star::uno::RuntimeException);
103cdf0e10cSrcweir 	virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw(::com::sun::star::uno::RuntimeException);
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 	// XUnoUrlResolver
106cdf0e10cSrcweir     virtual Reference< XInterface > SAL_CALL resolve( const OUString & rUnoUrl )
107cdf0e10cSrcweir 		throw (NoConnectException, ConnectionSetupException, RuntimeException);
108cdf0e10cSrcweir };
109cdf0e10cSrcweir 
110cdf0e10cSrcweir //##################################################################################################
111cdf0e10cSrcweir 
112cdf0e10cSrcweir //__________________________________________________________________________________________________
ResolverImpl(const Reference<XComponentContext> & xCtx)113cdf0e10cSrcweir ResolverImpl::ResolverImpl( const Reference< XComponentContext > & xCtx )
114cdf0e10cSrcweir 	: _xSMgr( xCtx->getServiceManager() )
115cdf0e10cSrcweir 	, _xCtx( xCtx )
116cdf0e10cSrcweir {
117cdf0e10cSrcweir 	g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
118cdf0e10cSrcweir }
119cdf0e10cSrcweir //__________________________________________________________________________________________________
~ResolverImpl()120cdf0e10cSrcweir ResolverImpl::~ResolverImpl()
121cdf0e10cSrcweir {
122cdf0e10cSrcweir 	g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
123cdf0e10cSrcweir }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir // XServiceInfo
126cdf0e10cSrcweir //__________________________________________________________________________________________________
getImplementationName()127cdf0e10cSrcweir OUString ResolverImpl::getImplementationName()
128cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
129cdf0e10cSrcweir {
130cdf0e10cSrcweir 	return resolver_getImplementationName();
131cdf0e10cSrcweir }
132cdf0e10cSrcweir //__________________________________________________________________________________________________
supportsService(const OUString & rServiceName)133cdf0e10cSrcweir sal_Bool ResolverImpl::supportsService( const OUString & rServiceName )
134cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
135cdf0e10cSrcweir {
136cdf0e10cSrcweir 	const Sequence< OUString > & rSNL = getSupportedServiceNames();
137cdf0e10cSrcweir 	const OUString * pArray = rSNL.getConstArray();
138cdf0e10cSrcweir 	for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
139cdf0e10cSrcweir 	{
140cdf0e10cSrcweir 		if (pArray[nPos] == rServiceName)
141cdf0e10cSrcweir 			return sal_True;
142cdf0e10cSrcweir 	}
143cdf0e10cSrcweir 	return sal_False;
144cdf0e10cSrcweir }
145cdf0e10cSrcweir //__________________________________________________________________________________________________
getSupportedServiceNames()146cdf0e10cSrcweir Sequence< OUString > ResolverImpl::getSupportedServiceNames()
147cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
148cdf0e10cSrcweir {
149cdf0e10cSrcweir 	return resolver_getSupportedServiceNames();
150cdf0e10cSrcweir }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir // XUnoUrlResolver
153cdf0e10cSrcweir //__________________________________________________________________________________________________
resolve(const OUString & rUnoUrl)154cdf0e10cSrcweir Reference< XInterface > ResolverImpl::resolve( const OUString & rUnoUrl )
155cdf0e10cSrcweir 	throw (NoConnectException, ConnectionSetupException, RuntimeException)
156cdf0e10cSrcweir {
157cdf0e10cSrcweir     OUString aProtocolDescr;
158cdf0e10cSrcweir     OUString aConnectDescr;
159cdf0e10cSrcweir     OUString aInstanceName;
160cdf0e10cSrcweir     try
161cdf0e10cSrcweir     {
162cdf0e10cSrcweir         cppu::UnoUrl aUrl(rUnoUrl);
163cdf0e10cSrcweir         aProtocolDescr = aUrl.getProtocol().getDescriptor();
164cdf0e10cSrcweir         aConnectDescr = aUrl.getConnection().getDescriptor();
165cdf0e10cSrcweir         aInstanceName = aUrl.getObjectName();
166cdf0e10cSrcweir     }
167cdf0e10cSrcweir     catch (rtl::MalformedUriException & rEx)
168cdf0e10cSrcweir     {
169cdf0e10cSrcweir         throw ConnectionSetupException(rEx.getMessage(), 0);
170cdf0e10cSrcweir     }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir 	Reference< XConnector > xConnector(
173cdf0e10cSrcweir 		_xSMgr->createInstanceWithContext(
174cdf0e10cSrcweir 			OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Connector") ),
175cdf0e10cSrcweir 			_xCtx ),
176cdf0e10cSrcweir 		UNO_QUERY );
177cdf0e10cSrcweir 
178cdf0e10cSrcweir 	if (! xConnector.is())
179cdf0e10cSrcweir 		throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM("no connector!" ) ), Reference< XInterface >() );
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 	Reference< XConnection > xConnection( xConnector->connect( aConnectDescr ) );
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 	// As soon as singletons are ready, switch to singleton !
184cdf0e10cSrcweir 	Reference< XBridgeFactory > xBridgeFactory(
185cdf0e10cSrcweir 		_xSMgr->createInstanceWithContext(
186cdf0e10cSrcweir 			OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bridge.BridgeFactory") ),
187cdf0e10cSrcweir 			_xCtx ),
188cdf0e10cSrcweir 		UNO_QUERY );
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 	if (! xBridgeFactory.is())
191cdf0e10cSrcweir 		throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM("no bridge factory!" ) ), Reference< XInterface >() );
192cdf0e10cSrcweir 
193cdf0e10cSrcweir 	// bridge
194cdf0e10cSrcweir 	Reference< XBridge > xBridge( xBridgeFactory->createBridge(
195cdf0e10cSrcweir 		OUString(), aProtocolDescr,
196cdf0e10cSrcweir 		xConnection, Reference< XInstanceProvider >() ) );
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 	Reference< XInterface > xRet( xBridge->getInstance( aInstanceName ) );
199cdf0e10cSrcweir 
200cdf0e10cSrcweir 	return xRet;
201cdf0e10cSrcweir }
202cdf0e10cSrcweir 
203cdf0e10cSrcweir //==================================================================================================
ResolverImpl_create(const Reference<XComponentContext> & xCtx)204cdf0e10cSrcweir static Reference< XInterface > SAL_CALL ResolverImpl_create( const Reference< XComponentContext > & xCtx )
205cdf0e10cSrcweir {
206cdf0e10cSrcweir 	return Reference< XInterface >( *new ResolverImpl( xCtx ) );
207cdf0e10cSrcweir }
208cdf0e10cSrcweir 
209cdf0e10cSrcweir 
210cdf0e10cSrcweir }
211cdf0e10cSrcweir 
212cdf0e10cSrcweir using namespace unourl_resolver;
213cdf0e10cSrcweir 
214cdf0e10cSrcweir static struct ImplementationEntry g_entries[] =
215cdf0e10cSrcweir {
216cdf0e10cSrcweir 	{
217cdf0e10cSrcweir 		ResolverImpl_create, resolver_getImplementationName,
218cdf0e10cSrcweir 		resolver_getSupportedServiceNames, createSingleComponentFactory,
219cdf0e10cSrcweir 		&g_moduleCount.modCnt , 0
220cdf0e10cSrcweir 	},
221cdf0e10cSrcweir 	{ 0, 0, 0, 0, 0, 0 }
222cdf0e10cSrcweir };
223cdf0e10cSrcweir 
224cdf0e10cSrcweir extern "C"
225cdf0e10cSrcweir {
component_canUnload(TimeValue * pTime)226*34de53acSdamjan SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL component_canUnload( TimeValue *pTime )
227cdf0e10cSrcweir {
228cdf0e10cSrcweir 	return g_moduleCount.canUnload( &g_moduleCount , pTime );
229cdf0e10cSrcweir }
230cdf0e10cSrcweir 
231cdf0e10cSrcweir //==================================================================================================
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment **)232*34de53acSdamjan SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment(
233cdf0e10cSrcweir 	const sal_Char ** ppEnvTypeName, uno_Environment ** )
234cdf0e10cSrcweir {
235cdf0e10cSrcweir 	*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
236cdf0e10cSrcweir }
237cdf0e10cSrcweir //==================================================================================================
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void * pRegistryKey)238*34de53acSdamjan SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
239cdf0e10cSrcweir 	const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
240cdf0e10cSrcweir {
241cdf0e10cSrcweir 	return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
242cdf0e10cSrcweir }
243cdf0e10cSrcweir }
244