Line data Source code
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/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>
36 :
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;
44 :
45 : #define IMPLNAME "com.sun.star.comp.bridge.UnoUrlResolver"
46 :
47 : namespace unourl_resolver
48 : {
49 :
50 4 : Sequence< OUString > resolver_getSupportedServiceNames()
51 : {
52 4 : Sequence< OUString > seqNames(1);
53 4 : seqNames.getArray()[0] = "com.sun.star.bridge.UnoUrlResolver";
54 4 : return seqNames;
55 : }
56 :
57 4 : OUString resolver_getImplementationName()
58 : {
59 4 : return OUString(IMPLNAME);
60 : }
61 :
62 : class ResolverImpl : public WeakImplHelper2< XServiceInfo, XUnoUrlResolver >
63 : {
64 : Reference< XMultiComponentFactory > _xSMgr;
65 : Reference< XComponentContext > _xCtx;
66 :
67 : public:
68 : ResolverImpl( const Reference< XComponentContext > & xSMgr );
69 : virtual ~ResolverImpl();
70 :
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;
75 :
76 : // XUnoUrlResolver
77 : virtual Reference< XInterface > SAL_CALL resolve( const OUString & rUnoUrl )
78 : throw (NoConnectException, ConnectionSetupException, RuntimeException, std::exception) SAL_OVERRIDE;
79 : };
80 :
81 3 : ResolverImpl::ResolverImpl( const Reference< XComponentContext > & xCtx )
82 3 : : _xSMgr( xCtx->getServiceManager() )
83 6 : , _xCtx( xCtx )
84 3 : {}
85 :
86 6 : ResolverImpl::~ResolverImpl() {}
87 :
88 : // XServiceInfo
89 1 : OUString ResolverImpl::getImplementationName()
90 : throw(::com::sun::star::uno::RuntimeException, std::exception)
91 : {
92 1 : return resolver_getImplementationName();
93 : }
94 :
95 0 : sal_Bool ResolverImpl::supportsService( const OUString & rServiceName )
96 : throw(::com::sun::star::uno::RuntimeException, std::exception)
97 : {
98 0 : return cppu::supportsService(this, rServiceName);
99 : }
100 :
101 1 : Sequence< OUString > ResolverImpl::getSupportedServiceNames()
102 : throw(::com::sun::star::uno::RuntimeException, std::exception)
103 : {
104 1 : return resolver_getSupportedServiceNames();
105 : }
106 :
107 : // XUnoUrlResolver
108 9 : Reference< XInterface > ResolverImpl::resolve( const OUString & rUnoUrl )
109 : throw (NoConnectException, ConnectionSetupException, RuntimeException, std::exception)
110 : {
111 9 : OUString aProtocolDescr;
112 18 : OUString aConnectDescr;
113 18 : OUString aInstanceName;
114 : try
115 : {
116 9 : cppu::UnoUrl aUrl(rUnoUrl);
117 9 : aProtocolDescr = aUrl.getProtocol().getDescriptor();
118 9 : aConnectDescr = aUrl.getConnection().getDescriptor();
119 9 : aInstanceName = aUrl.getObjectName();
120 : }
121 0 : catch (const rtl::MalformedUriException & rEx)
122 : {
123 0 : throw ConnectionSetupException(rEx.getMessage(), 0);
124 : }
125 :
126 : Reference< XConnector > xConnector(
127 9 : _xSMgr->createInstanceWithContext(
128 : OUString("com.sun.star.connection.Connector"),
129 9 : _xCtx ),
130 18 : UNO_QUERY );
131 :
132 9 : if (! xConnector.is())
133 0 : throw RuntimeException("no connector!" );
134 :
135 11 : Reference< XConnection > xConnection( xConnector->connect( aConnectDescr ) );
136 :
137 : // As soon as singletons are ready, switch to singleton !
138 4 : Reference< XBridgeFactory2 > xBridgeFactory( BridgeFactory::create(_xCtx) );
139 :
140 : // bridge
141 2 : Reference< XBridge > xBridge( xBridgeFactory->createBridge(
142 : OUString(), aProtocolDescr,
143 4 : xConnection, Reference< XInstanceProvider >() ) );
144 :
145 2 : Reference< XInterface > xRet( xBridge->getInstance( aInstanceName ) );
146 :
147 11 : return xRet;
148 : }
149 :
150 3 : static Reference< XInterface > SAL_CALL ResolverImpl_create( const Reference< XComponentContext > & xCtx )
151 : {
152 3 : return Reference< XInterface >( *new ResolverImpl( xCtx ) );
153 : }
154 :
155 : }
156 :
157 : using namespace unourl_resolver;
158 :
159 : static const struct ImplementationEntry g_entries[] =
160 : {
161 : {
162 : ResolverImpl_create, resolver_getImplementationName,
163 : resolver_getSupportedServiceNames, createSingleComponentFactory,
164 : 0, 0
165 : },
166 : { 0, 0, 0, 0, 0, 0 }
167 : };
168 :
169 3 : extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL uuresolver_component_getFactory(
170 : const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
171 : {
172 3 : return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
173 : }
174 :
175 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|