Branch data 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 "osl/security.hxx"
21 : :
22 : : #include <uno/mapping.hxx>
23 : :
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"
29 : :
30 : : #include <com/sun/star/lang/XServiceInfo.hpp>
31 : : #include <com/sun/star/lang/IllegalArgumentException.hpp>
32 : : #include <com/sun/star/connection/XConnector.hpp>
33 : :
34 : : #include "connector.hxx"
35 : :
36 : : #define IMPLEMENTATION_NAME "com.sun.star.comp.io.Connector"
37 : : #define SERVICE_NAME "com.sun.star.connection.Connector"
38 : :
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;
46 : :
47 : : namespace stoc_connector
48 : : {
49 : : rtl_StandardModuleCount g_moduleCount = MODULE_COUNT_INIT;
50 : :
51 : : class OConnector : public WeakImplHelper2< XConnector, XServiceInfo >
52 : : {
53 : : Reference< XMultiComponentFactory > _xSMgr;
54 : : Reference< XComponentContext > _xCtx;
55 : : public:
56 : : OConnector(const Reference< XComponentContext > &xCtx);
57 : : ~OConnector();
58 : : // Methods
59 : : virtual Reference< XConnection > SAL_CALL connect(
60 : : const OUString& sConnectionDescription )
61 : : throw( NoConnectException, ConnectionSetupException, RuntimeException);
62 : :
63 : : public: // XServiceInfo
64 : : virtual OUString SAL_CALL getImplementationName() throw();
65 : : virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(void) throw();
66 : : virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw();
67 : : };
68 : :
69 : 26 : OConnector::OConnector(const Reference< XComponentContext > &xCtx)
70 [ + - ]: 26 : : _xSMgr( xCtx->getServiceManager() )
71 [ + - ]: 52 : , _xCtx( xCtx )
72 : : {
73 [ + - ]: 26 : g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
74 : 26 : }
75 : :
76 : 26 : OConnector::~OConnector()
77 : : {
78 [ + - ]: 26 : g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
79 [ - + ]: 52 : }
80 : :
81 : 26 : Reference< XConnection > SAL_CALL OConnector::connect( const OUString& sConnectionDescription )
82 : : throw( NoConnectException, ConnectionSetupException, RuntimeException)
83 : : {
84 : : OSL_TRACE(
85 : : "connector %s\n",
86 : : OUStringToOString(
87 : : sConnectionDescription, RTL_TEXTENCODING_ASCII_US).getStr());
88 : :
89 : : // split string into tokens
90 : : try
91 : : {
92 [ + - ]: 26 : cppu::UnoUrlDescriptor aDesc(sConnectionDescription);
93 : :
94 : 26 : Reference< XConnection > r;
95 [ + - ][ + - ]: 26 : if ( aDesc.getName() == "pipe" )
96 : : {
97 : : rtl::OUString aName(
98 : : aDesc.getParameter(
99 [ + - ][ + - ]: 26 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("name"))));
100 : :
101 [ + - ]: 26 : PipeConnection *pConn = new PipeConnection( sConnectionDescription );
102 : :
103 [ + - ][ + - ]: 26 : if( pConn->m_pipe.create( aName.pData, osl_Pipe_OPEN, osl::Security() ) )
[ + - ][ + + ]
104 : : {
105 [ + - ][ + - ]: 4 : r = Reference < XConnection > ( (XConnection * ) pConn );
[ + - ]
106 : : }
107 : : else
108 : : {
109 [ + - ]: 22 : OUString sMessage = OUString(RTL_CONSTASCII_USTRINGPARAM("Connector : couldn't connect to pipe "));
110 : 22 : sMessage += aName;
111 [ + - ]: 22 : sMessage += OUString(RTL_CONSTASCII_USTRINGPARAM("("));
112 [ + - ]: 22 : sMessage += OUString::valueOf( (sal_Int32 ) pConn->m_pipe.getError() );
113 [ + - ]: 22 : sMessage += OUString(RTL_CONSTASCII_USTRINGPARAM(")"));
114 [ + - ][ + - ]: 22 : delete pConn;
115 [ + - ]: 22 : throw NoConnectException( sMessage ,Reference< XInterface > () );
116 : 26 : }
117 : : }
118 [ # # ][ # # ]: 0 : else if ( aDesc.getName() == "socket" )
119 : : {
120 : 0 : rtl::OUString aHost;
121 [ # # # # ]: 0 : if (aDesc.hasParameter(
122 [ # # ]: 0 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("host"))))
123 : : aHost = aDesc.getParameter(
124 [ # # ][ # # ]: 0 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("host")));
125 : : else
126 : : aHost = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
127 [ # # ]: 0 : "localhost"));
128 : : sal_uInt16 nPort = static_cast< sal_uInt16 >(
129 : : aDesc.getParameter(
130 : : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("port"))).
131 [ # # ][ # # ]: 0 : toInt32());
132 : : bool bTcpNoDelay
133 : : = aDesc.getParameter(
134 : : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
135 [ # # ][ # # ]: 0 : "tcpnodelay"))).toInt32() != 0;
136 : :
137 [ # # ]: 0 : SocketConnection *pConn = new SocketConnection( sConnectionDescription);
138 : :
139 [ # # ]: 0 : SocketAddr AddrTarget( aHost.pData, nPort );
140 [ # # ][ # # ]: 0 : if(pConn->m_socket.connect(AddrTarget) != osl_Socket_Ok)
141 : : {
142 [ # # ]: 0 : OUString sMessage = OUString(RTL_CONSTASCII_USTRINGPARAM("Connector : couldn't connect to socket ("));
143 [ # # ]: 0 : OUString sError = pConn->m_socket.getErrorAsString();
144 : 0 : sMessage += sError;
145 [ # # ]: 0 : sMessage += OUString(RTL_CONSTASCII_USTRINGPARAM(")"));
146 [ # # ][ # # ]: 0 : delete pConn;
147 [ # # ]: 0 : throw NoConnectException( sMessage, Reference < XInterface > () );
148 : : }
149 [ # # ]: 0 : if( bTcpNoDelay )
150 : : {
151 : 0 : sal_Int32 nTcpNoDelay = sal_True;
152 : : pConn->m_socket.setOption( osl_Socket_OptionTcpNoDelay , &nTcpNoDelay,
153 [ # # ]: 0 : sizeof( nTcpNoDelay ) , osl_Socket_LevelTcp );
154 : : }
155 [ # # ]: 0 : pConn->completeConnectionString();
156 [ # # ][ # # ]: 0 : r = Reference< XConnection > ( (XConnection * ) pConn );
[ # # ][ # # ]
157 : : }
158 : : else
159 : : {
160 [ # # ]: 0 : OUString delegatee = OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Connector."));
161 [ # # ]: 0 : delegatee += aDesc.getName();
162 : :
163 : : OSL_TRACE(
164 : : "connector: trying to get service %s\n",
165 : : OUStringToOString(
166 : : delegatee, RTL_TEXTENCODING_ASCII_US).getStr());
167 : : Reference<XConnector> xConnector(
168 [ # # ][ # # ]: 0 : _xSMgr->createInstanceWithContext(delegatee, _xCtx), UNO_QUERY );
[ # # ]
169 : :
170 [ # # ]: 0 : if(!xConnector.is())
171 : : {
172 [ # # ]: 0 : OUString message(RTL_CONSTASCII_USTRINGPARAM("Connector: unknown delegatee "));
173 : 0 : message += delegatee;
174 : :
175 [ # # ]: 0 : throw ConnectionSetupException(message, Reference<XInterface>());
176 : : }
177 : :
178 : 0 : sal_Int32 index = sConnectionDescription.indexOf((sal_Unicode) ',');
179 : :
180 [ # # ][ # # ]: 0 : r = xConnector->connect(sConnectionDescription.copy(index + 1).trim());
[ # # ]
181 : : }
182 [ + - ]: 26 : return r;
183 : : }
184 [ + - ]: 22 : catch (const rtl::MalformedUriException & rEx)
185 : : {
186 : 0 : throw ConnectionSetupException(rEx.getMessage(),
187 [ # # ]: 0 : Reference< XInterface > ());
188 : : }
189 : : }
190 : :
191 : 4 : Sequence< OUString > connector_getSupportedServiceNames()
192 : : {
193 : 4 : Sequence< OUString > seqNames(1);
194 [ + - ][ + - ]: 4 : seqNames.getArray()[0] = OUString(RTL_CONSTASCII_USTRINGPARAM(SERVICE_NAME));
195 : 4 : return seqNames;
196 : : }
197 : :
198 : 4 : OUString connector_getImplementationName()
199 : : {
200 : 4 : return OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) );
201 : : }
202 : :
203 : 0 : OUString OConnector::getImplementationName() throw()
204 : : {
205 : 0 : return connector_getImplementationName();
206 : : }
207 : :
208 : 0 : sal_Bool OConnector::supportsService(const OUString& ServiceName) throw()
209 : : {
210 : 0 : Sequence< OUString > aSNL = getSupportedServiceNames();
211 : 0 : const OUString * pArray = aSNL.getConstArray();
212 : :
213 [ # # ]: 0 : for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
214 [ # # ]: 0 : if( pArray[i] == ServiceName )
215 : 0 : return sal_True;
216 : :
217 [ # # ]: 0 : return sal_False;
218 : : }
219 : :
220 : 0 : Sequence< OUString > OConnector::getSupportedServiceNames(void) throw()
221 : : {
222 : 0 : return connector_getSupportedServiceNames();
223 : : }
224 : :
225 : 26 : Reference< XInterface > SAL_CALL connector_CreateInstance( const Reference< XComponentContext > & xCtx)
226 : : {
227 [ + - ]: 26 : return Reference < XInterface >( ( OWeakObject * ) new OConnector(xCtx) );
228 : : }
229 : :
230 : :
231 : : }
232 : : using namespace stoc_connector;
233 : :
234 : : static struct ImplementationEntry g_entries[] =
235 : : {
236 : : {
237 : : connector_CreateInstance, connector_getImplementationName ,
238 : : connector_getSupportedServiceNames, createSingleComponentFactory ,
239 : : &g_moduleCount.modCnt , 0
240 : : },
241 : : { 0, 0, 0, 0, 0, 0 }
242 : : };
243 : :
244 : : extern "C"
245 : : {
246 : :
247 : 0 : SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL component_canUnload( TimeValue *pTime )
248 : : {
249 : 0 : return g_moduleCount.canUnload( &g_moduleCount , pTime );
250 : : }
251 : :
252 : : //==================================================================================================
253 : 4 : SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
254 : : const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
255 : : {
256 : 4 : return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
257 : : }
258 : :
259 : : }
260 : :
261 : :
262 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|