1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <osl/diagnose.h>
21 #include <osl/mutex.hxx>
22 #include <cppuhelper/factory.hxx>
23 #include <cppuhelper/implbase2.hxx>
24 #include <cppuhelper/implementationentry.hxx>
25 #include "cppuhelper/unourl.hxx"
26 #include "rtl/malformeduriexception.hxx"
28 #include <com/sun/star/lang/XServiceInfo.hpp>
29 #include <com/sun/star/lang/XComponent.hpp>
30 #include <com/sun/star/registry/XRegistryKey.hpp>
31 #include <com/sun/star/connection/XConnector.hpp>
32 #include <com/sun/star/bridge/BridgeFactory.hpp>
33 #include <com/sun/star/bridge/XBridgeFactory.hpp>
34 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
38 using namespace com::sun::star::uno
;
39 using namespace com::sun::star::lang
;
40 using namespace com::sun::star::connection
;
41 using namespace com::sun::star::bridge
;
42 using namespace com::sun::star::registry
;
45 #define SERVICENAME "com.sun.star.bridge.UnoUrlResolver"
46 #define IMPLNAME "com.sun.star.comp.bridge.UnoUrlResolver"
48 namespace unourl_resolver
50 //--------------------------------------------------------------------------------------------------
51 Sequence
< OUString
> resolver_getSupportedServiceNames()
53 Sequence
< OUString
> seqNames(1);
54 seqNames
.getArray()[0] = OUString(SERVICENAME
);
58 OUString
resolver_getImplementationName()
60 return OUString(IMPLNAME
);
63 //==================================================================================================
64 class ResolverImpl
: public WeakImplHelper2
< XServiceInfo
, XUnoUrlResolver
>
66 Reference
< XMultiComponentFactory
> _xSMgr
;
67 Reference
< XComponentContext
> _xCtx
;
70 ResolverImpl( const Reference
< XComponentContext
> & xSMgr
);
71 virtual ~ResolverImpl();
74 virtual OUString SAL_CALL
getImplementationName() throw(::com::sun::star::uno::RuntimeException
);
75 virtual sal_Bool SAL_CALL
supportsService( const OUString
& rServiceName
) throw(::com::sun::star::uno::RuntimeException
);
76 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() throw(::com::sun::star::uno::RuntimeException
);
79 virtual Reference
< XInterface
> SAL_CALL
resolve( const OUString
& rUnoUrl
)
80 throw (NoConnectException
, ConnectionSetupException
, RuntimeException
);
83 //##################################################################################################
85 //__________________________________________________________________________________________________
86 ResolverImpl::ResolverImpl( const Reference
< XComponentContext
> & xCtx
)
87 : _xSMgr( xCtx
->getServiceManager() )
90 //__________________________________________________________________________________________________
91 ResolverImpl::~ResolverImpl() {}
94 //__________________________________________________________________________________________________
95 OUString
ResolverImpl::getImplementationName()
96 throw(::com::sun::star::uno::RuntimeException
)
98 return resolver_getImplementationName();
100 //__________________________________________________________________________________________________
101 sal_Bool
ResolverImpl::supportsService( const OUString
& rServiceName
)
102 throw(::com::sun::star::uno::RuntimeException
)
104 const Sequence
< OUString
> & rSNL
= getSupportedServiceNames();
105 const OUString
* pArray
= rSNL
.getConstArray();
106 for ( sal_Int32 nPos
= rSNL
.getLength(); nPos
--; )
108 if (pArray
[nPos
] == rServiceName
)
113 //__________________________________________________________________________________________________
114 Sequence
< OUString
> ResolverImpl::getSupportedServiceNames()
115 throw(::com::sun::star::uno::RuntimeException
)
117 return resolver_getSupportedServiceNames();
121 //__________________________________________________________________________________________________
122 Reference
< XInterface
> ResolverImpl::resolve( const OUString
& rUnoUrl
)
123 throw (NoConnectException
, ConnectionSetupException
, RuntimeException
)
125 OUString aProtocolDescr
;
126 OUString aConnectDescr
;
127 OUString aInstanceName
;
130 cppu::UnoUrl
aUrl(rUnoUrl
);
131 aProtocolDescr
= aUrl
.getProtocol().getDescriptor();
132 aConnectDescr
= aUrl
.getConnection().getDescriptor();
133 aInstanceName
= aUrl
.getObjectName();
135 catch (const rtl::MalformedUriException
& rEx
)
137 throw ConnectionSetupException(rEx
.getMessage(), 0);
140 Reference
< XConnector
> xConnector(
141 _xSMgr
->createInstanceWithContext(
142 OUString("com.sun.star.connection.Connector"),
146 if (! xConnector
.is())
147 throw RuntimeException( OUString("no connector!" ), Reference
< XInterface
>() );
149 Reference
< XConnection
> xConnection( xConnector
->connect( aConnectDescr
) );
151 // As soon as singletons are ready, switch to singleton !
152 Reference
< XBridgeFactory2
> xBridgeFactory( BridgeFactory::create(_xCtx
) );
155 Reference
< XBridge
> xBridge( xBridgeFactory
->createBridge(
156 OUString(), aProtocolDescr
,
157 xConnection
, Reference
< XInstanceProvider
>() ) );
159 Reference
< XInterface
> xRet( xBridge
->getInstance( aInstanceName
) );
164 //==================================================================================================
165 static Reference
< XInterface
> SAL_CALL
ResolverImpl_create( const Reference
< XComponentContext
> & xCtx
)
167 return Reference
< XInterface
>( *new ResolverImpl( xCtx
) );
173 using namespace unourl_resolver
;
175 static struct ImplementationEntry g_entries
[] =
178 ResolverImpl_create
, resolver_getImplementationName
,
179 resolver_getSupportedServiceNames
, createSingleComponentFactory
,
185 extern "C" SAL_DLLPUBLIC_EXPORT
void * SAL_CALL
uuresolver_component_getFactory(
186 const sal_Char
* pImplName
, void * pServiceManager
, void * pRegistryKey
)
188 return component_getFactoryHelper( pImplName
, pServiceManager
, pRegistryKey
, g_entries
);
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */