1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir //  #define TRACE(x) OSL_TRACE(x)
29*cdf0e10cSrcweir #define TRACE(x)
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <osl/diagnose.h>
32*cdf0e10cSrcweir #include <osl/mutex.hxx>
33*cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
34*cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
35*cdf0e10cSrcweir #include <cppuhelper/implementationentry.hxx>
36*cdf0e10cSrcweir #include "cppuhelper/unourl.hxx"
37*cdf0e10cSrcweir #include "rtl/malformeduriexception.hxx"
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/connection/XConnector.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/bridge/XBridgeFactory.hpp>
44*cdf0e10cSrcweir #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir using namespace cppu;
47*cdf0e10cSrcweir using namespace rtl;
48*cdf0e10cSrcweir using namespace osl;
49*cdf0e10cSrcweir using namespace com::sun::star::uno;
50*cdf0e10cSrcweir using namespace com::sun::star::lang;
51*cdf0e10cSrcweir using namespace com::sun::star::connection;
52*cdf0e10cSrcweir using namespace com::sun::star::bridge;
53*cdf0e10cSrcweir using namespace com::sun::star::registry;
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir #define SERVICENAME		"com.sun.star.bridge.UnoUrlResolver"
56*cdf0e10cSrcweir #define IMPLNAME		"com.sun.star.comp.bridge.UnoUrlResolver"
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir namespace unourl_resolver
59*cdf0e10cSrcweir {
60*cdf0e10cSrcweir 	rtl_StandardModuleCount g_moduleCount = MODULE_COUNT_INIT;
61*cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
62*cdf0e10cSrcweir Sequence< OUString > resolver_getSupportedServiceNames()
63*cdf0e10cSrcweir {
64*cdf0e10cSrcweir 	static Sequence < OUString > *pNames = 0;
65*cdf0e10cSrcweir 	if( ! pNames )
66*cdf0e10cSrcweir 	{
67*cdf0e10cSrcweir 		MutexGuard guard( Mutex::getGlobalMutex() );
68*cdf0e10cSrcweir 		if( !pNames )
69*cdf0e10cSrcweir 		{
70*cdf0e10cSrcweir 			static Sequence< OUString > seqNames(1);
71*cdf0e10cSrcweir 			seqNames.getArray()[0] = OUString(RTL_CONSTASCII_USTRINGPARAM(SERVICENAME));
72*cdf0e10cSrcweir 			pNames = &seqNames;
73*cdf0e10cSrcweir 		}
74*cdf0e10cSrcweir 	}
75*cdf0e10cSrcweir 	return *pNames;
76*cdf0e10cSrcweir }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir OUString resolver_getImplementationName()
79*cdf0e10cSrcweir {
80*cdf0e10cSrcweir 	static OUString *pImplName = 0;
81*cdf0e10cSrcweir 	if( ! pImplName )
82*cdf0e10cSrcweir 	{
83*cdf0e10cSrcweir 		MutexGuard guard( Mutex::getGlobalMutex() );
84*cdf0e10cSrcweir 		if( ! pImplName )
85*cdf0e10cSrcweir 		{
86*cdf0e10cSrcweir 			static OUString implName(
87*cdf0e10cSrcweir 				RTL_CONSTASCII_USTRINGPARAM( IMPLNAME ) );
88*cdf0e10cSrcweir 			pImplName = &implName;
89*cdf0e10cSrcweir 		}
90*cdf0e10cSrcweir 	}
91*cdf0e10cSrcweir 	return *pImplName;
92*cdf0e10cSrcweir }
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir //==================================================================================================
95*cdf0e10cSrcweir class ResolverImpl : public WeakImplHelper2< XServiceInfo, XUnoUrlResolver >
96*cdf0e10cSrcweir {
97*cdf0e10cSrcweir 	Reference< XMultiComponentFactory > _xSMgr;
98*cdf0e10cSrcweir 	Reference< XComponentContext > _xCtx;
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir public:
101*cdf0e10cSrcweir 	ResolverImpl( const Reference< XComponentContext > & xSMgr );
102*cdf0e10cSrcweir 	virtual ~ResolverImpl();
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 	// XServiceInfo
105*cdf0e10cSrcweir 	virtual OUString SAL_CALL getImplementationName() throw(::com::sun::star::uno::RuntimeException);
106*cdf0e10cSrcweir 	virtual sal_Bool SAL_CALL supportsService( const OUString & rServiceName ) throw(::com::sun::star::uno::RuntimeException);
107*cdf0e10cSrcweir 	virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw(::com::sun::star::uno::RuntimeException);
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 	// XUnoUrlResolver
110*cdf0e10cSrcweir     virtual Reference< XInterface > SAL_CALL resolve( const OUString & rUnoUrl )
111*cdf0e10cSrcweir 		throw (NoConnectException, ConnectionSetupException, RuntimeException);
112*cdf0e10cSrcweir };
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir //##################################################################################################
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir //__________________________________________________________________________________________________
117*cdf0e10cSrcweir ResolverImpl::ResolverImpl( const Reference< XComponentContext > & xCtx )
118*cdf0e10cSrcweir 	: _xSMgr( xCtx->getServiceManager() )
119*cdf0e10cSrcweir 	, _xCtx( xCtx )
120*cdf0e10cSrcweir {
121*cdf0e10cSrcweir 	g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
122*cdf0e10cSrcweir }
123*cdf0e10cSrcweir //__________________________________________________________________________________________________
124*cdf0e10cSrcweir ResolverImpl::~ResolverImpl()
125*cdf0e10cSrcweir {
126*cdf0e10cSrcweir 	g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
127*cdf0e10cSrcweir }
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir // XServiceInfo
130*cdf0e10cSrcweir //__________________________________________________________________________________________________
131*cdf0e10cSrcweir OUString ResolverImpl::getImplementationName()
132*cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
133*cdf0e10cSrcweir {
134*cdf0e10cSrcweir 	return resolver_getImplementationName();
135*cdf0e10cSrcweir }
136*cdf0e10cSrcweir //__________________________________________________________________________________________________
137*cdf0e10cSrcweir sal_Bool ResolverImpl::supportsService( const OUString & rServiceName )
138*cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
139*cdf0e10cSrcweir {
140*cdf0e10cSrcweir 	const Sequence< OUString > & rSNL = getSupportedServiceNames();
141*cdf0e10cSrcweir 	const OUString * pArray = rSNL.getConstArray();
142*cdf0e10cSrcweir 	for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
143*cdf0e10cSrcweir 	{
144*cdf0e10cSrcweir 		if (pArray[nPos] == rServiceName)
145*cdf0e10cSrcweir 			return sal_True;
146*cdf0e10cSrcweir 	}
147*cdf0e10cSrcweir 	return sal_False;
148*cdf0e10cSrcweir }
149*cdf0e10cSrcweir //__________________________________________________________________________________________________
150*cdf0e10cSrcweir Sequence< OUString > ResolverImpl::getSupportedServiceNames()
151*cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
152*cdf0e10cSrcweir {
153*cdf0e10cSrcweir 	return resolver_getSupportedServiceNames();
154*cdf0e10cSrcweir }
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir // XUnoUrlResolver
157*cdf0e10cSrcweir //__________________________________________________________________________________________________
158*cdf0e10cSrcweir Reference< XInterface > ResolverImpl::resolve( const OUString & rUnoUrl )
159*cdf0e10cSrcweir 	throw (NoConnectException, ConnectionSetupException, RuntimeException)
160*cdf0e10cSrcweir {
161*cdf0e10cSrcweir     OUString aProtocolDescr;
162*cdf0e10cSrcweir     OUString aConnectDescr;
163*cdf0e10cSrcweir     OUString aInstanceName;
164*cdf0e10cSrcweir     try
165*cdf0e10cSrcweir     {
166*cdf0e10cSrcweir         cppu::UnoUrl aUrl(rUnoUrl);
167*cdf0e10cSrcweir         aProtocolDescr = aUrl.getProtocol().getDescriptor();
168*cdf0e10cSrcweir         aConnectDescr = aUrl.getConnection().getDescriptor();
169*cdf0e10cSrcweir         aInstanceName = aUrl.getObjectName();
170*cdf0e10cSrcweir     }
171*cdf0e10cSrcweir     catch (rtl::MalformedUriException & rEx)
172*cdf0e10cSrcweir     {
173*cdf0e10cSrcweir         throw ConnectionSetupException(rEx.getMessage(), 0);
174*cdf0e10cSrcweir     }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir 	Reference< XConnector > xConnector(
177*cdf0e10cSrcweir 		_xSMgr->createInstanceWithContext(
178*cdf0e10cSrcweir 			OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Connector") ),
179*cdf0e10cSrcweir 			_xCtx ),
180*cdf0e10cSrcweir 		UNO_QUERY );
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 	if (! xConnector.is())
183*cdf0e10cSrcweir 		throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM("no connector!" ) ), Reference< XInterface >() );
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 	Reference< XConnection > xConnection( xConnector->connect( aConnectDescr ) );
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir 	// As soon as singletons are ready, switch to singleton !
188*cdf0e10cSrcweir 	Reference< XBridgeFactory > xBridgeFactory(
189*cdf0e10cSrcweir 		_xSMgr->createInstanceWithContext(
190*cdf0e10cSrcweir 			OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bridge.BridgeFactory") ),
191*cdf0e10cSrcweir 			_xCtx ),
192*cdf0e10cSrcweir 		UNO_QUERY );
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir 	if (! xBridgeFactory.is())
195*cdf0e10cSrcweir 		throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM("no bridge factory!" ) ), Reference< XInterface >() );
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir 	// bridge
198*cdf0e10cSrcweir 	Reference< XBridge > xBridge( xBridgeFactory->createBridge(
199*cdf0e10cSrcweir 		OUString(), aProtocolDescr,
200*cdf0e10cSrcweir 		xConnection, Reference< XInstanceProvider >() ) );
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir 	Reference< XInterface > xRet( xBridge->getInstance( aInstanceName ) );
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir 	return xRet;
205*cdf0e10cSrcweir }
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir //==================================================================================================
208*cdf0e10cSrcweir static Reference< XInterface > SAL_CALL ResolverImpl_create( const Reference< XComponentContext > & xCtx )
209*cdf0e10cSrcweir {
210*cdf0e10cSrcweir 	return Reference< XInterface >( *new ResolverImpl( xCtx ) );
211*cdf0e10cSrcweir }
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir }
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir using namespace unourl_resolver;
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir static struct ImplementationEntry g_entries[] =
219*cdf0e10cSrcweir {
220*cdf0e10cSrcweir 	{
221*cdf0e10cSrcweir 		ResolverImpl_create, resolver_getImplementationName,
222*cdf0e10cSrcweir 		resolver_getSupportedServiceNames, createSingleComponentFactory,
223*cdf0e10cSrcweir 		&g_moduleCount.modCnt , 0
224*cdf0e10cSrcweir 	},
225*cdf0e10cSrcweir 	{ 0, 0, 0, 0, 0, 0 }
226*cdf0e10cSrcweir };
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir extern "C"
229*cdf0e10cSrcweir {
230*cdf0e10cSrcweir sal_Bool SAL_CALL component_canUnload( TimeValue *pTime )
231*cdf0e10cSrcweir {
232*cdf0e10cSrcweir 	return g_moduleCount.canUnload( &g_moduleCount , pTime );
233*cdf0e10cSrcweir }
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir //==================================================================================================
236*cdf0e10cSrcweir void SAL_CALL component_getImplementationEnvironment(
237*cdf0e10cSrcweir 	const sal_Char ** ppEnvTypeName, uno_Environment ** )
238*cdf0e10cSrcweir {
239*cdf0e10cSrcweir 	*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
240*cdf0e10cSrcweir }
241*cdf0e10cSrcweir //==================================================================================================
242*cdf0e10cSrcweir void * SAL_CALL component_getFactory(
243*cdf0e10cSrcweir 	const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
244*cdf0e10cSrcweir {
245*cdf0e10cSrcweir 	return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
246*cdf0e10cSrcweir }
247*cdf0e10cSrcweir }
248