Bump for 3.6-28
[LibreOffice.git] / io / source / connector / connector.cxx
blobdea5099b60a00e042abb533b8b8ae94280558569
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "osl/security.hxx"
31 #include <uno/mapping.hxx>
33 #include <cppuhelper/factory.hxx>
34 #include <cppuhelper/implbase2.hxx>
35 #include <cppuhelper/implementationentry.hxx>
36 #include "cppuhelper/unourl.hxx"
37 #include "rtl/malformeduriexception.hxx"
39 #include <com/sun/star/lang/XServiceInfo.hpp>
40 #include <com/sun/star/lang/IllegalArgumentException.hpp>
41 #include <com/sun/star/connection/XConnector.hpp>
43 #include "connector.hxx"
45 #define IMPLEMENTATION_NAME "com.sun.star.comp.io.Connector"
46 #define SERVICE_NAME "com.sun.star.connection.Connector"
48 using namespace ::osl;
49 using namespace ::rtl;
50 using namespace ::cppu;
51 using namespace ::com::sun::star::uno;
52 using namespace ::com::sun::star::lang;
53 using namespace ::com::sun::star::registry;
54 using namespace ::com::sun::star::connection;
56 namespace stoc_connector
58 rtl_StandardModuleCount g_moduleCount = MODULE_COUNT_INIT;
60 class OConnector : public WeakImplHelper2< XConnector, XServiceInfo >
62 Reference< XMultiComponentFactory > _xSMgr;
63 Reference< XComponentContext > _xCtx;
64 public:
65 OConnector(const Reference< XComponentContext > &xCtx);
66 ~OConnector();
67 // Methods
68 virtual Reference< XConnection > SAL_CALL connect(
69 const OUString& sConnectionDescription )
70 throw( NoConnectException, ConnectionSetupException, RuntimeException);
72 public: // XServiceInfo
73 virtual OUString SAL_CALL getImplementationName() throw();
74 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(void) throw();
75 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw();
78 OConnector::OConnector(const Reference< XComponentContext > &xCtx)
79 : _xSMgr( xCtx->getServiceManager() )
80 , _xCtx( xCtx )
82 g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
85 OConnector::~OConnector()
87 g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
90 Reference< XConnection > SAL_CALL OConnector::connect( const OUString& sConnectionDescription )
91 throw( NoConnectException, ConnectionSetupException, RuntimeException)
93 OSL_TRACE(
94 "connector %s\n",
95 OUStringToOString(
96 sConnectionDescription, RTL_TEXTENCODING_ASCII_US).getStr());
98 // split string into tokens
99 try
101 cppu::UnoUrlDescriptor aDesc(sConnectionDescription);
103 Reference< XConnection > r;
104 if ( aDesc.getName() == "pipe" )
106 rtl::OUString aName(
107 aDesc.getParameter(
108 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("name"))));
110 PipeConnection *pConn = new PipeConnection( sConnectionDescription );
112 if( pConn->m_pipe.create( aName.pData, osl_Pipe_OPEN, osl::Security() ) )
114 r = Reference < XConnection > ( (XConnection * ) pConn );
116 else
118 OUString sMessage = OUString(RTL_CONSTASCII_USTRINGPARAM("Connector : couldn't connect to pipe "));
119 sMessage += aName;
120 sMessage += OUString(RTL_CONSTASCII_USTRINGPARAM("("));
121 sMessage += OUString::valueOf( (sal_Int32 ) pConn->m_pipe.getError() );
122 sMessage += OUString(RTL_CONSTASCII_USTRINGPARAM(")"));
123 delete pConn;
124 throw NoConnectException( sMessage ,Reference< XInterface > () );
127 else if ( aDesc.getName() == "socket" )
129 rtl::OUString aHost;
130 if (aDesc.hasParameter(
131 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("host"))))
132 aHost = aDesc.getParameter(
133 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("host")));
134 else
135 aHost = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
136 "localhost"));
137 sal_uInt16 nPort = static_cast< sal_uInt16 >(
138 aDesc.getParameter(
139 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("port"))).
140 toInt32());
141 bool bTcpNoDelay
142 = aDesc.getParameter(
143 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
144 "tcpnodelay"))).toInt32() != 0;
146 SocketConnection *pConn = new SocketConnection( sConnectionDescription);
148 SocketAddr AddrTarget( aHost.pData, nPort );
149 if(pConn->m_socket.connect(AddrTarget) != osl_Socket_Ok)
151 OUString sMessage = OUString(RTL_CONSTASCII_USTRINGPARAM("Connector : couldn't connect to socket ("));
152 OUString sError = pConn->m_socket.getErrorAsString();
153 sMessage += sError;
154 sMessage += OUString(RTL_CONSTASCII_USTRINGPARAM(")"));
155 delete pConn;
156 throw NoConnectException( sMessage, Reference < XInterface > () );
158 if( bTcpNoDelay )
160 sal_Int32 nTcpNoDelay = sal_True;
161 pConn->m_socket.setOption( osl_Socket_OptionTcpNoDelay , &nTcpNoDelay,
162 sizeof( nTcpNoDelay ) , osl_Socket_LevelTcp );
164 pConn->completeConnectionString();
165 r = Reference< XConnection > ( (XConnection * ) pConn );
167 else
169 OUString delegatee = OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Connector."));
170 delegatee += aDesc.getName();
172 OSL_TRACE(
173 "connector: trying to get service %s\n",
174 OUStringToOString(
175 delegatee, RTL_TEXTENCODING_ASCII_US).getStr());
176 Reference<XConnector> xConnector(
177 _xSMgr->createInstanceWithContext(delegatee, _xCtx), UNO_QUERY );
179 if(!xConnector.is())
181 OUString message(RTL_CONSTASCII_USTRINGPARAM("Connector: unknown delegatee "));
182 message += delegatee;
184 throw ConnectionSetupException(message, Reference<XInterface>());
187 sal_Int32 index = sConnectionDescription.indexOf((sal_Unicode) ',');
189 r = xConnector->connect(sConnectionDescription.copy(index + 1).trim());
191 return r;
193 catch (const rtl::MalformedUriException & rEx)
195 throw ConnectionSetupException(rEx.getMessage(),
196 Reference< XInterface > ());
200 Sequence< OUString > connector_getSupportedServiceNames()
202 Sequence< OUString > seqNames(1);
203 seqNames.getArray()[0] = OUString(RTL_CONSTASCII_USTRINGPARAM(SERVICE_NAME));
204 return seqNames;
207 OUString connector_getImplementationName()
209 return OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) );
212 OUString OConnector::getImplementationName() throw()
214 return connector_getImplementationName();
217 sal_Bool OConnector::supportsService(const OUString& ServiceName) throw()
219 Sequence< OUString > aSNL = getSupportedServiceNames();
220 const OUString * pArray = aSNL.getConstArray();
222 for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
223 if( pArray[i] == ServiceName )
224 return sal_True;
226 return sal_False;
229 Sequence< OUString > OConnector::getSupportedServiceNames(void) throw()
231 return connector_getSupportedServiceNames();
234 Reference< XInterface > SAL_CALL connector_CreateInstance( const Reference< XComponentContext > & xCtx)
236 return Reference < XInterface >( ( OWeakObject * ) new OConnector(xCtx) );
241 using namespace stoc_connector;
243 static struct ImplementationEntry g_entries[] =
246 connector_CreateInstance, connector_getImplementationName ,
247 connector_getSupportedServiceNames, createSingleComponentFactory ,
248 &g_moduleCount.modCnt , 0
250 { 0, 0, 0, 0, 0, 0 }
253 extern "C"
256 SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL component_canUnload( TimeValue *pTime )
258 return g_moduleCount.canUnload( &g_moduleCount , pTime );
261 //==================================================================================================
262 SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
263 const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
265 return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
271 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */