update dev300-m58
[ooovba.git] / remotebridges / source / unourl_resolver / unourl_resolver.cxx
blobc8bccd0fa0fbbc27c9ce4313a3580d027cc4bbfb
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: unourl_resolver.cxx,v $
10 * $Revision: 1.8 $
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)
32 #define 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>
49 using namespace cppu;
50 using namespace rtl;
51 using namespace osl;
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;
68 if( ! pNames )
70 MutexGuard guard( Mutex::getGlobalMutex() );
71 if( !pNames )
73 static Sequence< OUString > seqNames(1);
74 seqNames.getArray()[0] = OUString(RTL_CONSTASCII_USTRINGPARAM(SERVICENAME));
75 pNames = &seqNames;
78 return *pNames;
81 OUString resolver_getImplementationName()
83 static OUString *pImplName = 0;
84 if( ! pImplName )
86 MutexGuard guard( Mutex::getGlobalMutex() );
87 if( ! pImplName )
89 static OUString implName(
90 RTL_CONSTASCII_USTRINGPARAM( IMPLNAME ) );
91 pImplName = &implName;
94 return *pImplName;
97 //==================================================================================================
98 class ResolverImpl : public WeakImplHelper2< XServiceInfo, XUnoUrlResolver >
100 Reference< XMultiComponentFactory > _xSMgr;
101 Reference< XComponentContext > _xCtx;
103 public:
104 ResolverImpl( const Reference< XComponentContext > & xSMgr );
105 virtual ~ResolverImpl();
107 // XServiceInfo
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);
112 // XUnoUrlResolver
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() )
122 , _xCtx( xCtx )
124 g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
126 //__________________________________________________________________________________________________
127 ResolverImpl::~ResolverImpl()
129 g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
132 // XServiceInfo
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)
148 return sal_True;
150 return sal_False;
152 //__________________________________________________________________________________________________
153 Sequence< OUString > ResolverImpl::getSupportedServiceNames()
154 throw(::com::sun::star::uno::RuntimeException)
156 return resolver_getSupportedServiceNames();
159 // XUnoUrlResolver
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") ),
182 _xCtx ),
183 UNO_QUERY );
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") ),
194 _xCtx ),
195 UNO_QUERY );
197 if (! xBridgeFactory.is())
198 throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM("no bridge factory!" ) ), Reference< XInterface >() );
200 // bridge
201 Reference< XBridge > xBridge( xBridgeFactory->createBridge(
202 OUString(), aProtocolDescr,
203 xConnection, Reference< XInstanceProvider >() ) );
205 Reference< XInterface > xRet( xBridge->getInstance( aInstanceName ) );
207 return xRet;
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
228 { 0, 0, 0, 0, 0, 0 }
231 extern "C"
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 );