merge the formfield patch from ooo-build
[ooovba.git] / io / source / connector / connector.cxx
blob8851b8775fd53b8861df3c80ea4b1d407b2f5e67
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: connector.cxx,v $
10 * $Revision: 1.22 $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_io.hxx"
33 #include <osl/mutex.hxx>
34 #include "osl/security.hxx"
36 #include <uno/mapping.hxx>
38 #include <cppuhelper/factory.hxx>
39 #include <cppuhelper/implbase2.hxx>
40 #include <cppuhelper/implementationentry.hxx>
41 #include "cppuhelper/unourl.hxx"
42 #include "rtl/malformeduriexception.hxx"
44 #include <com/sun/star/lang/XServiceInfo.hpp>
45 #include <com/sun/star/lang/IllegalArgumentException.hpp>
46 #include <com/sun/star/connection/XConnector.hpp>
48 #include "connector.hxx"
50 #define IMPLEMENTATION_NAME "com.sun.star.comp.io.Connector"
51 #define SERVICE_NAME "com.sun.star.connection.Connector"
53 using namespace ::osl;
54 using namespace ::rtl;
55 using namespace ::cppu;
56 using namespace ::com::sun::star::uno;
57 using namespace ::com::sun::star::lang;
58 using namespace ::com::sun::star::registry;
59 using namespace ::com::sun::star::connection;
61 namespace stoc_connector
63 rtl_StandardModuleCount g_moduleCount = MODULE_COUNT_INIT;
65 class OConnector : public WeakImplHelper2< XConnector, XServiceInfo >
67 Reference< XMultiComponentFactory > _xSMgr;
68 Reference< XComponentContext > _xCtx;
69 public:
70 OConnector(const Reference< XComponentContext > &xCtx);
71 ~OConnector();
72 // Methods
73 virtual Reference< XConnection > SAL_CALL connect(
74 const OUString& sConnectionDescription )
75 throw( NoConnectException, ConnectionSetupException, RuntimeException);
77 public: // XServiceInfo
78 virtual OUString SAL_CALL getImplementationName() throw();
79 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(void) throw();
80 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw();
83 OConnector::OConnector(const Reference< XComponentContext > &xCtx)
84 : _xSMgr( xCtx->getServiceManager() )
85 , _xCtx( xCtx )
87 g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
90 OConnector::~OConnector()
92 g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
95 Reference< XConnection > SAL_CALL OConnector::connect( const OUString& sConnectionDescription )
96 throw( NoConnectException, ConnectionSetupException, RuntimeException)
98 OSL_TRACE(
99 "connector %s\n",
100 OUStringToOString(
101 sConnectionDescription, RTL_TEXTENCODING_ASCII_US).getStr());
103 // split string into tokens
106 cppu::UnoUrlDescriptor aDesc(sConnectionDescription);
108 Reference< XConnection > r;
109 if (aDesc.getName().equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(
110 "pipe")))
112 rtl::OUString aName(
113 aDesc.getParameter(
114 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("name"))));
116 PipeConnection *pConn = new PipeConnection( sConnectionDescription );
118 if( pConn->m_pipe.create( aName.pData, osl_Pipe_OPEN, osl::Security() ) )
120 r = Reference < XConnection > ( (XConnection * ) pConn );
122 else
124 OUString sMessage = OUString::createFromAscii( "Connector : couldn't connect to pipe " );
125 sMessage += aName;
126 sMessage += OUString::createFromAscii( "(" );
127 sMessage += OUString::valueOf( (sal_Int32 ) pConn->m_pipe.getError() );
128 sMessage += OUString::createFromAscii( ")" );
129 delete pConn;
130 throw NoConnectException( sMessage ,Reference< XInterface > () );
133 else if (aDesc.getName().equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(
134 "socket")))
136 rtl::OUString aHost;
137 if (aDesc.hasParameter(
138 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("host"))))
139 aHost = aDesc.getParameter(
140 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("host")));
141 else
142 aHost = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
143 "localhost"));
144 sal_uInt16 nPort = static_cast< sal_uInt16 >(
145 aDesc.getParameter(
146 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("port"))).
147 toInt32());
148 bool bTcpNoDelay
149 = aDesc.getParameter(
150 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
151 "tcpnodelay"))).toInt32() != 0;
153 SocketConnection *pConn = new SocketConnection( sConnectionDescription);
155 SocketAddr AddrTarget( aHost.pData, nPort );
156 if(pConn->m_socket.connect(AddrTarget) != osl_Socket_Ok)
158 OUString sMessage = OUString::createFromAscii( "Connector : couldn't connect to socket (" );
159 OUString sError = pConn->m_socket.getErrorAsString();
160 sMessage += sError;
161 sMessage += OUString::createFromAscii( ")" );
162 delete pConn;
163 throw NoConnectException( sMessage, Reference < XInterface > () );
165 if( bTcpNoDelay )
167 sal_Int32 nTcpNoDelay = sal_True;
168 pConn->m_socket.setOption( osl_Socket_OptionTcpNoDelay , &nTcpNoDelay,
169 sizeof( nTcpNoDelay ) , osl_Socket_LevelTcp );
171 pConn->completeConnectionString();
172 r = Reference< XConnection > ( (XConnection * ) pConn );
174 else
176 OUString delegatee = OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Connector."));
177 delegatee += aDesc.getName();
179 OSL_TRACE(
180 "connector: trying to get service %s\n",
181 OUStringToOString(
182 delegatee, RTL_TEXTENCODING_ASCII_US).getStr());
183 Reference<XConnector> xConnector(
184 _xSMgr->createInstanceWithContext(delegatee, _xCtx), UNO_QUERY );
186 if(!xConnector.is())
188 OUString message(RTL_CONSTASCII_USTRINGPARAM("Connector: unknown delegatee "));
189 message += delegatee;
191 throw ConnectionSetupException(message, Reference<XInterface>());
194 sal_Int32 index = sConnectionDescription.indexOf((sal_Unicode) ',');
196 r = xConnector->connect(sConnectionDescription.copy(index + 1).trim());
198 return r;
200 catch (rtl::MalformedUriException & rEx)
202 throw ConnectionSetupException(rEx.getMessage(),
203 Reference< XInterface > ());
207 Sequence< OUString > connector_getSupportedServiceNames()
209 static Sequence < OUString > *pNames = 0;
210 if( ! pNames )
212 MutexGuard guard( Mutex::getGlobalMutex() );
213 if( !pNames )
215 static Sequence< OUString > seqNames(1);
216 seqNames.getArray()[0] = OUString::createFromAscii( SERVICE_NAME );
217 pNames = &seqNames;
220 return *pNames;
223 OUString connector_getImplementationName()
225 return OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) );
228 OUString OConnector::getImplementationName() throw()
230 return connector_getImplementationName();
233 sal_Bool OConnector::supportsService(const OUString& ServiceName) throw()
235 Sequence< OUString > aSNL = getSupportedServiceNames();
236 const OUString * pArray = aSNL.getConstArray();
238 for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
239 if( pArray[i] == ServiceName )
240 return sal_True;
242 return sal_False;
245 Sequence< OUString > OConnector::getSupportedServiceNames(void) throw()
247 return connector_getSupportedServiceNames();
250 Reference< XInterface > SAL_CALL connector_CreateInstance( const Reference< XComponentContext > & xCtx)
252 return Reference < XInterface >( ( OWeakObject * ) new OConnector(xCtx) );
257 using namespace stoc_connector;
259 static struct ImplementationEntry g_entries[] =
262 connector_CreateInstance, connector_getImplementationName ,
263 connector_getSupportedServiceNames, createSingleComponentFactory ,
264 &g_moduleCount.modCnt , 0
266 { 0, 0, 0, 0, 0, 0 }
269 extern "C"
272 sal_Bool SAL_CALL component_canUnload( TimeValue *pTime )
274 return g_moduleCount.canUnload( &g_moduleCount , pTime );
277 //==================================================================================================
278 void SAL_CALL component_getImplementationEnvironment(
279 const sal_Char ** ppEnvTypeName, uno_Environment ** )
281 *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
283 //==================================================================================================
284 sal_Bool SAL_CALL component_writeInfo(
285 void * pServiceManager, void * pRegistryKey )
287 return component_writeInfoHelper( pServiceManager, pRegistryKey, g_entries );
289 //==================================================================================================
290 void * SAL_CALL component_getFactory(
291 const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
293 return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );