1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <rtl/malformeduriexception.hxx>
21 
22 #include <com/sun/star/lang/XComponent.hpp>
23 #include <com/sun/star/lang/XServiceInfo.hpp>
24 #include <com/sun/star/bridge/BridgeFactory.hpp>
25 #include <com/sun/star/bridge/XBridgeFactory.hpp>
26 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
27 #include <com/sun/star/connection/XConnector.hpp>
28 #include <com/sun/star/registry/XRegistryKey.hpp>
29 #include <cppuhelper/factory.hxx>
30 #include <cppuhelper/implbase.hxx>
31 #include <cppuhelper/implementationentry.hxx>
32 #include <cppuhelper/supportsservice.hxx>
33 #include <cppuhelper/unourl.hxx>
34 
35 using namespace cppu;
36 using namespace com::sun::star::uno;
37 using namespace com::sun::star::lang;
38 using namespace com::sun::star::connection;
39 using namespace com::sun::star::bridge;
40 using namespace com::sun::star::registry;
41 
42 #define IMPLNAME        "com.sun.star.comp.bridge.UnoUrlResolver"
43 
44 namespace unourl_resolver
45 {
46 
resolver_getSupportedServiceNames()47 static Sequence< OUString > resolver_getSupportedServiceNames()
48 {
49     Sequence< OUString > seqNames { "com.sun.star.bridge.UnoUrlResolver" };
50     return seqNames;
51 }
52 
resolver_getImplementationName()53 static OUString resolver_getImplementationName()
54 {
55     return IMPLNAME;
56 }
57 
58 class ResolverImpl : public WeakImplHelper< XServiceInfo, XUnoUrlResolver >
59 {
60     Reference< XMultiComponentFactory > _xSMgr;
61     Reference< XComponentContext > _xCtx;
62 
63 public:
64     explicit ResolverImpl( const Reference< XComponentContext > & xSMgr );
65 
66     // XServiceInfo
67     virtual OUString SAL_CALL getImplementationName() override;
68     virtual sal_Bool SAL_CALL supportsService( const OUString & rServiceName ) override;
69     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
70 
71     // XUnoUrlResolver
72     virtual Reference< XInterface > SAL_CALL resolve( const OUString & rUnoUrl ) override;
73 };
74 
ResolverImpl(const Reference<XComponentContext> & xCtx)75 ResolverImpl::ResolverImpl( const Reference< XComponentContext > & xCtx )
76     : _xSMgr( xCtx->getServiceManager() )
77     , _xCtx( xCtx )
78 {}
79 
80 // XServiceInfo
getImplementationName()81 OUString ResolverImpl::getImplementationName()
82 {
83     return resolver_getImplementationName();
84 }
85 
supportsService(const OUString & rServiceName)86 sal_Bool ResolverImpl::supportsService( const OUString & rServiceName )
87 {
88     return cppu::supportsService(this, rServiceName);
89 }
90 
getSupportedServiceNames()91 Sequence< OUString > ResolverImpl::getSupportedServiceNames()
92 {
93     return resolver_getSupportedServiceNames();
94 }
95 
96 // XUnoUrlResolver
resolve(const OUString & rUnoUrl)97 Reference< XInterface > ResolverImpl::resolve( const OUString & rUnoUrl )
98 {
99     OUString aProtocolDescr;
100     OUString aConnectDescr;
101     OUString aInstanceName;
102     try
103     {
104         cppu::UnoUrl aUrl(rUnoUrl);
105         aProtocolDescr = aUrl.getProtocol().getDescriptor();
106         aConnectDescr = aUrl.getConnection().getDescriptor();
107         aInstanceName = aUrl.getObjectName();
108     }
109     catch (const rtl::MalformedUriException & rEx)
110     {
111         throw ConnectionSetupException(rEx.getMessage(), nullptr);
112     }
113 
114     Reference< XConnector > xConnector(
115         _xSMgr->createInstanceWithContext( "com.sun.star.connection.Connector", _xCtx ),
116         UNO_QUERY_THROW );
117     Reference< XConnection > xConnection( xConnector->connect( aConnectDescr ) );
118 
119     // As soon as singletons are ready, switch to singleton !
120     Reference< XBridgeFactory2 > xBridgeFactory( BridgeFactory::create(_xCtx) );
121 
122     // bridge
123     Reference< XBridge > xBridge( xBridgeFactory->createBridge(
124         OUString(), aProtocolDescr,
125         xConnection, Reference< XInstanceProvider >() ) );
126 
127     Reference< XInterface > xRet( xBridge->getInstance( aInstanceName ) );
128 
129     return xRet;
130 }
131 
ResolverImpl_create(const Reference<XComponentContext> & xCtx)132 static Reference< XInterface > ResolverImpl_create( const Reference< XComponentContext > & xCtx )
133 {
134     return Reference< XInterface >( *new ResolverImpl( xCtx ) );
135 }
136 
137 }
138 
139 using namespace unourl_resolver;
140 
141 static const struct ImplementationEntry g_entries[] =
142 {
143     {
144         ResolverImpl_create, resolver_getImplementationName,
145         resolver_getSupportedServiceNames, createSingleComponentFactory,
146         nullptr, 0
147     },
148     { nullptr, nullptr, nullptr, nullptr, nullptr, 0 }
149 };
150 
uuresolver_component_getFactory(const sal_Char * pImplName,void * pServiceManager,void * pRegistryKey)151 extern "C" SAL_DLLPUBLIC_EXPORT void * uuresolver_component_getFactory(
152     const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
153 {
154     return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
155 }
156 
157 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
158