1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: unourl_resolver.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // #define TRACE(x) OSL_TRACE(x)
34 #include <osl/diagnose.h>
35 #include <osl/mutex.hxx>
36 #include <cppuhelper/factory.hxx>
37 #include <cppuhelper/implbase2.hxx>
38 #include <cppuhelper/implementationentry.hxx>
39 #include "cppuhelper/unourl.hxx"
40 #include "rtl/malformeduriexception.hxx"
42 #include <com/sun/star/lang/XServiceInfo.hpp>
43 #include <com/sun/star/lang/XComponent.hpp>
44 #include <com/sun/star/registry/XRegistryKey.hpp>
45 #include <com/sun/star/connection/XConnector.hpp>
46 #include <com/sun/star/bridge/XBridgeFactory.hpp>
47 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
52 using namespace com::sun::star::uno
;
53 using namespace com::sun::star::lang
;
54 using namespace com::sun::star::connection
;
55 using namespace com::sun::star::bridge
;
56 using namespace com::sun::star::registry
;
58 #define SERVICENAME "com.sun.star.bridge.UnoUrlResolver"
59 #define IMPLNAME "com.sun.star.comp.bridge.UnoUrlResolver"
61 namespace unourl_resolver
63 rtl_StandardModuleCount g_moduleCount
= MODULE_COUNT_INIT
;
64 //--------------------------------------------------------------------------------------------------
65 Sequence
< OUString
> resolver_getSupportedServiceNames()
67 static Sequence
< OUString
> *pNames
= 0;
70 MutexGuard
guard( Mutex::getGlobalMutex() );
73 static Sequence
< OUString
> seqNames(1);
74 seqNames
.getArray()[0] = OUString(RTL_CONSTASCII_USTRINGPARAM(SERVICENAME
));
81 OUString
resolver_getImplementationName()
83 static OUString
*pImplName
= 0;
86 MutexGuard
guard( Mutex::getGlobalMutex() );
89 static OUString
implName(
90 RTL_CONSTASCII_USTRINGPARAM( IMPLNAME
) );
91 pImplName
= &implName
;
97 //==================================================================================================
98 class ResolverImpl
: public WeakImplHelper2
< XServiceInfo
, XUnoUrlResolver
>
100 Reference
< XMultiComponentFactory
> _xSMgr
;
101 Reference
< XComponentContext
> _xCtx
;
104 ResolverImpl( const Reference
< XComponentContext
> & xSMgr
);
105 virtual ~ResolverImpl();
108 virtual OUString SAL_CALL
getImplementationName() throw(::com::sun::star::uno::RuntimeException
);
109 virtual sal_Bool SAL_CALL
supportsService( const OUString
& rServiceName
) throw(::com::sun::star::uno::RuntimeException
);
110 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() throw(::com::sun::star::uno::RuntimeException
);
113 virtual Reference
< XInterface
> SAL_CALL
resolve( const OUString
& rUnoUrl
)
114 throw (NoConnectException
, ConnectionSetupException
, RuntimeException
);
117 //##################################################################################################
119 //__________________________________________________________________________________________________
120 ResolverImpl::ResolverImpl( const Reference
< XComponentContext
> & xCtx
)
121 : _xSMgr( xCtx
->getServiceManager() )
124 g_moduleCount
.modCnt
.acquire( &g_moduleCount
.modCnt
);
126 //__________________________________________________________________________________________________
127 ResolverImpl::~ResolverImpl()
129 g_moduleCount
.modCnt
.release( &g_moduleCount
.modCnt
);
133 //__________________________________________________________________________________________________
134 OUString
ResolverImpl::getImplementationName()
135 throw(::com::sun::star::uno::RuntimeException
)
137 return resolver_getImplementationName();
139 //__________________________________________________________________________________________________
140 sal_Bool
ResolverImpl::supportsService( const OUString
& rServiceName
)
141 throw(::com::sun::star::uno::RuntimeException
)
143 const Sequence
< OUString
> & rSNL
= getSupportedServiceNames();
144 const OUString
* pArray
= rSNL
.getConstArray();
145 for ( sal_Int32 nPos
= rSNL
.getLength(); nPos
--; )
147 if (pArray
[nPos
] == rServiceName
)
152 //__________________________________________________________________________________________________
153 Sequence
< OUString
> ResolverImpl::getSupportedServiceNames()
154 throw(::com::sun::star::uno::RuntimeException
)
156 return resolver_getSupportedServiceNames();
160 //__________________________________________________________________________________________________
161 Reference
< XInterface
> ResolverImpl::resolve( const OUString
& rUnoUrl
)
162 throw (NoConnectException
, ConnectionSetupException
, RuntimeException
)
164 OUString aProtocolDescr
;
165 OUString aConnectDescr
;
166 OUString aInstanceName
;
169 cppu::UnoUrl
aUrl(rUnoUrl
);
170 aProtocolDescr
= aUrl
.getProtocol().getDescriptor();
171 aConnectDescr
= aUrl
.getConnection().getDescriptor();
172 aInstanceName
= aUrl
.getObjectName();
174 catch (rtl::MalformedUriException
& rEx
)
176 throw ConnectionSetupException(rEx
.getMessage(), 0);
179 Reference
< XConnector
> xConnector(
180 _xSMgr
->createInstanceWithContext(
181 OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Connector") ),
185 if (! xConnector
.is())
186 throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM("no connector!" ) ), Reference
< XInterface
>() );
188 Reference
< XConnection
> xConnection( xConnector
->connect( aConnectDescr
) );
190 // As soon as singletons are ready, switch to singleton !
191 Reference
< XBridgeFactory
> xBridgeFactory(
192 _xSMgr
->createInstanceWithContext(
193 OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bridge.BridgeFactory") ),
197 if (! xBridgeFactory
.is())
198 throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM("no bridge factory!" ) ), Reference
< XInterface
>() );
201 Reference
< XBridge
> xBridge( xBridgeFactory
->createBridge(
202 OUString(), aProtocolDescr
,
203 xConnection
, Reference
< XInstanceProvider
>() ) );
205 Reference
< XInterface
> xRet( xBridge
->getInstance( aInstanceName
) );
210 //==================================================================================================
211 static Reference
< XInterface
> SAL_CALL
ResolverImpl_create( const Reference
< XComponentContext
> & xCtx
)
213 return Reference
< XInterface
>( *new ResolverImpl( xCtx
) );
219 using namespace unourl_resolver
;
221 static struct ImplementationEntry g_entries
[] =
224 ResolverImpl_create
, resolver_getImplementationName
,
225 resolver_getSupportedServiceNames
, createSingleComponentFactory
,
226 &g_moduleCount
.modCnt
, 0
233 sal_Bool SAL_CALL
component_canUnload( TimeValue
*pTime
)
235 return g_moduleCount
.canUnload( &g_moduleCount
, pTime
);
238 //==================================================================================================
239 void SAL_CALL
component_getImplementationEnvironment(
240 const sal_Char
** ppEnvTypeName
, uno_Environment
** )
242 *ppEnvTypeName
= CPPU_CURRENT_LANGUAGE_BINDING_NAME
;
244 //==================================================================================================
245 sal_Bool SAL_CALL
component_writeInfo(
246 void * pServiceManager
, void * pRegistryKey
)
248 return component_writeInfoHelper( pServiceManager
, pRegistryKey
, g_entries
);
250 //==================================================================================================
251 void * SAL_CALL
component_getFactory(
252 const sal_Char
* pImplName
, void * pServiceManager
, void * pRegistryKey
)
254 return component_getFactoryHelper( pImplName
, pServiceManager
, pRegistryKey
, g_entries
);