Branch libreoffice-5-0-4
[LibreOffice.git] / remotebridges / source / unourl_resolver / unourl_resolver.cxx
blob9468392c072f060c9e68a280a202fa3be554460d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "rtl/malformeduriexception.hxx"
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/implbase2.hxx>
31 #include <cppuhelper/implementationentry.hxx>
32 #include <cppuhelper/supportsservice.hxx>
33 #include "cppuhelper/unourl.hxx"
34 #include <osl/diagnose.h>
35 #include <osl/mutex.hxx>
37 using namespace cppu;
38 using namespace osl;
39 using namespace com::sun::star::uno;
40 using namespace com::sun::star::lang;
41 using namespace com::sun::star::connection;
42 using namespace com::sun::star::bridge;
43 using namespace com::sun::star::registry;
45 #define IMPLNAME "com.sun.star.comp.bridge.UnoUrlResolver"
47 namespace unourl_resolver
50 Sequence< OUString > resolver_getSupportedServiceNames()
52 Sequence< OUString > seqNames(1);
53 seqNames.getArray()[0] = "com.sun.star.bridge.UnoUrlResolver";
54 return seqNames;
57 OUString resolver_getImplementationName()
59 return OUString(IMPLNAME);
62 class ResolverImpl : public WeakImplHelper2< XServiceInfo, XUnoUrlResolver >
64 Reference< XMultiComponentFactory > _xSMgr;
65 Reference< XComponentContext > _xCtx;
67 public:
68 ResolverImpl( const Reference< XComponentContext > & xSMgr );
69 virtual ~ResolverImpl();
71 // XServiceInfo
72 virtual OUString SAL_CALL getImplementationName() throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
73 virtual sal_Bool SAL_CALL supportsService( const OUString & rServiceName ) throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
74 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
76 // XUnoUrlResolver
77 virtual Reference< XInterface > SAL_CALL resolve( const OUString & rUnoUrl )
78 throw (NoConnectException, ConnectionSetupException, RuntimeException, std::exception) SAL_OVERRIDE;
81 ResolverImpl::ResolverImpl( const Reference< XComponentContext > & xCtx )
82 : _xSMgr( xCtx->getServiceManager() )
83 , _xCtx( xCtx )
86 ResolverImpl::~ResolverImpl() {}
88 // XServiceInfo
89 OUString ResolverImpl::getImplementationName()
90 throw(::com::sun::star::uno::RuntimeException, std::exception)
92 return resolver_getImplementationName();
95 sal_Bool ResolverImpl::supportsService( const OUString & rServiceName )
96 throw(::com::sun::star::uno::RuntimeException, std::exception)
98 return cppu::supportsService(this, rServiceName);
101 Sequence< OUString > ResolverImpl::getSupportedServiceNames()
102 throw(::com::sun::star::uno::RuntimeException, std::exception)
104 return resolver_getSupportedServiceNames();
107 // XUnoUrlResolver
108 Reference< XInterface > ResolverImpl::resolve( const OUString & rUnoUrl )
109 throw (NoConnectException, ConnectionSetupException, RuntimeException, std::exception)
111 OUString aProtocolDescr;
112 OUString aConnectDescr;
113 OUString aInstanceName;
116 cppu::UnoUrl aUrl(rUnoUrl);
117 aProtocolDescr = aUrl.getProtocol().getDescriptor();
118 aConnectDescr = aUrl.getConnection().getDescriptor();
119 aInstanceName = aUrl.getObjectName();
121 catch (const rtl::MalformedUriException & rEx)
123 throw ConnectionSetupException(rEx.getMessage(), 0);
126 Reference< XConnector > xConnector(
127 _xSMgr->createInstanceWithContext(
128 OUString("com.sun.star.connection.Connector"),
129 _xCtx ),
130 UNO_QUERY );
132 if (! xConnector.is())
133 throw RuntimeException("no connector!" );
135 Reference< XConnection > xConnection( xConnector->connect( aConnectDescr ) );
137 // As soon as singletons are ready, switch to singleton !
138 Reference< XBridgeFactory2 > xBridgeFactory( BridgeFactory::create(_xCtx) );
140 // bridge
141 Reference< XBridge > xBridge( xBridgeFactory->createBridge(
142 OUString(), aProtocolDescr,
143 xConnection, Reference< XInstanceProvider >() ) );
145 Reference< XInterface > xRet( xBridge->getInstance( aInstanceName ) );
147 return xRet;
150 static Reference< XInterface > SAL_CALL ResolverImpl_create( const Reference< XComponentContext > & xCtx )
152 return Reference< XInterface >( *new ResolverImpl( xCtx ) );
157 using namespace unourl_resolver;
159 static const struct ImplementationEntry g_entries[] =
162 ResolverImpl_create, resolver_getImplementationName,
163 resolver_getSupportedServiceNames, createSingleComponentFactory,
164 0, 0
166 { 0, 0, 0, 0, 0, 0 }
169 extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL uuresolver_component_getFactory(
170 const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
172 return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */