bump product version to 4.1.6.2
[LibreOffice.git] / io / source / connector / connector.cxx
blobe6df22775a70ddc54ef6cef0b4f1d44327d3d1c3
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 "osl/security.hxx"
22 #include <uno/mapping.hxx>
24 #include <cppuhelper/factory.hxx>
25 #include <cppuhelper/implbase2.hxx>
26 #include <cppuhelper/implementationentry.hxx>
27 #include "cppuhelper/unourl.hxx"
28 #include "rtl/malformeduriexception.hxx"
30 #include <com/sun/star/lang/XServiceInfo.hpp>
31 #include <com/sun/star/lang/IllegalArgumentException.hpp>
32 #include <com/sun/star/connection/XConnector.hpp>
34 #include "connector.hxx"
36 #define IMPLEMENTATION_NAME "com.sun.star.comp.io.Connector"
37 #define SERVICE_NAME "com.sun.star.connection.Connector"
39 using namespace ::osl;
40 using namespace ::rtl;
41 using namespace ::cppu;
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::lang;
44 using namespace ::com::sun::star::registry;
45 using namespace ::com::sun::star::connection;
47 namespace stoc_connector
49 class OConnector : public WeakImplHelper2< XConnector, XServiceInfo >
51 Reference< XMultiComponentFactory > _xSMgr;
52 Reference< XComponentContext > _xCtx;
53 public:
54 OConnector(const Reference< XComponentContext > &xCtx);
55 ~OConnector();
56 // Methods
57 virtual Reference< XConnection > SAL_CALL connect(
58 const OUString& sConnectionDescription )
59 throw( NoConnectException, ConnectionSetupException, RuntimeException);
61 public: // XServiceInfo
62 virtual OUString SAL_CALL getImplementationName() throw();
63 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(void) throw();
64 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw();
67 OConnector::OConnector(const Reference< XComponentContext > &xCtx)
68 : _xSMgr( xCtx->getServiceManager() )
69 , _xCtx( xCtx )
72 OConnector::~OConnector() {}
74 Reference< XConnection > SAL_CALL OConnector::connect( const OUString& sConnectionDescription )
75 throw( NoConnectException, ConnectionSetupException, RuntimeException)
77 OSL_TRACE(
78 "connector %s\n",
79 OUStringToOString(
80 sConnectionDescription, RTL_TEXTENCODING_ASCII_US).getStr());
82 // split string into tokens
83 try
85 cppu::UnoUrlDescriptor aDesc(sConnectionDescription);
87 Reference< XConnection > r;
88 if ( aDesc.getName() == "pipe" )
90 OUString aName(aDesc.getParameter("name"));
92 PipeConnection *pConn = new PipeConnection( sConnectionDescription );
94 if( pConn->m_pipe.create( aName.pData, osl_Pipe_OPEN, osl::Security() ) )
96 r = Reference < XConnection > ( (XConnection * ) pConn );
98 else
100 OUString sMessage("Connector : couldn't connect to pipe ");
101 sMessage += aName;
102 sMessage += "(";
103 sMessage += OUString::valueOf( (sal_Int32 ) pConn->m_pipe.getError() );
104 sMessage += ")";
105 delete pConn;
106 throw NoConnectException( sMessage ,Reference< XInterface > () );
109 else if ( aDesc.getName() == "socket" )
111 OUString aHost;
112 if (aDesc.hasParameter("host"))
113 aHost = aDesc.getParameter("host");
114 else
115 aHost = "localhost";
116 sal_uInt16 nPort = static_cast< sal_uInt16 >(
117 aDesc.getParameter("port").
118 toInt32());
119 bool bTcpNoDelay
120 = aDesc.getParameter("tcpnodelay").toInt32() != 0;
122 SocketConnection *pConn = new SocketConnection( sConnectionDescription);
124 SocketAddr AddrTarget( aHost.pData, nPort );
125 if(pConn->m_socket.connect(AddrTarget) != osl_Socket_Ok)
127 OUString sMessage("Connector : couldn't connect to socket (");
128 OUString sError = pConn->m_socket.getErrorAsString();
129 sMessage += sError;
130 sMessage += ")";
131 delete pConn;
132 throw NoConnectException( sMessage, Reference < XInterface > () );
134 if( bTcpNoDelay )
136 sal_Int32 nTcpNoDelay = sal_True;
137 pConn->m_socket.setOption( osl_Socket_OptionTcpNoDelay , &nTcpNoDelay,
138 sizeof( nTcpNoDelay ) , osl_Socket_LevelTcp );
140 pConn->completeConnectionString();
141 r = Reference< XConnection > ( (XConnection * ) pConn );
143 else
145 OUString delegatee("com.sun.star.connection.Connector.");
146 delegatee += aDesc.getName();
148 OSL_TRACE(
149 "connector: trying to get service %s\n",
150 OUStringToOString(
151 delegatee, RTL_TEXTENCODING_ASCII_US).getStr());
152 Reference<XConnector> xConnector(
153 _xSMgr->createInstanceWithContext(delegatee, _xCtx), UNO_QUERY );
155 if(!xConnector.is())
157 OUString message("Connector: unknown delegatee ");
158 message += delegatee;
160 throw ConnectionSetupException(message, Reference<XInterface>());
163 sal_Int32 index = sConnectionDescription.indexOf((sal_Unicode) ',');
165 r = xConnector->connect(sConnectionDescription.copy(index + 1).trim());
167 return r;
169 catch (const rtl::MalformedUriException & rEx)
171 throw ConnectionSetupException(rEx.getMessage(),
172 Reference< XInterface > ());
176 Sequence< OUString > connector_getSupportedServiceNames()
178 Sequence< OUString > seqNames(1);
179 seqNames.getArray()[0] = SERVICE_NAME;
180 return seqNames;
183 OUString connector_getImplementationName()
185 return OUString( IMPLEMENTATION_NAME );
188 OUString OConnector::getImplementationName() throw()
190 return connector_getImplementationName();
193 sal_Bool OConnector::supportsService(const OUString& ServiceName) throw()
195 Sequence< OUString > aSNL = getSupportedServiceNames();
196 const OUString * pArray = aSNL.getConstArray();
198 for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
199 if( pArray[i] == ServiceName )
200 return sal_True;
202 return sal_False;
205 Sequence< OUString > OConnector::getSupportedServiceNames(void) throw()
207 return connector_getSupportedServiceNames();
210 Reference< XInterface > SAL_CALL connector_CreateInstance( const Reference< XComponentContext > & xCtx)
212 return Reference < XInterface >( ( OWeakObject * ) new OConnector(xCtx) );
217 using namespace stoc_connector;
219 static struct ImplementationEntry g_entries[] =
222 connector_CreateInstance, connector_getImplementationName ,
223 connector_getSupportedServiceNames, createSingleComponentFactory ,
224 0, 0
226 { 0, 0, 0, 0, 0, 0 }
229 extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL connector_component_getFactory(
230 const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
232 return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */