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 : class OConnector : public WeakImplHelper2< XConnector, XServiceInfo >
50 : {
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);
60 :
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();
65 : };
66 :
67 18 : OConnector::OConnector(const Reference< XComponentContext > &xCtx)
68 18 : : _xSMgr( xCtx->getServiceManager() )
69 36 : , _xCtx( xCtx )
70 18 : {}
71 :
72 36 : OConnector::~OConnector() {}
73 :
74 18 : Reference< XConnection > SAL_CALL OConnector::connect( const OUString& sConnectionDescription )
75 : throw( NoConnectException, ConnectionSetupException, RuntimeException)
76 : {
77 : OSL_TRACE(
78 : "connector %s\n",
79 : OUStringToOString(
80 : sConnectionDescription, RTL_TEXTENCODING_ASCII_US).getStr());
81 :
82 : // split string into tokens
83 : try
84 : {
85 18 : cppu::UnoUrlDescriptor aDesc(sConnectionDescription);
86 :
87 36 : Reference< XConnection > r;
88 18 : if ( aDesc.getName() == "pipe" )
89 : {
90 18 : OUString aName(aDesc.getParameter("name"));
91 :
92 18 : PipeConnection *pConn = new PipeConnection( sConnectionDescription );
93 :
94 18 : if( pConn->m_pipe.create( aName.pData, osl_Pipe_OPEN, osl::Security() ) )
95 : {
96 2 : r = Reference < XConnection > ( (XConnection * ) pConn );
97 : }
98 : else
99 : {
100 16 : OUString sMessage("Connector : couldn't connect to pipe ");
101 16 : sMessage += aName;
102 16 : sMessage += "(";
103 16 : sMessage += OUString::valueOf( (sal_Int32 ) pConn->m_pipe.getError() );
104 16 : sMessage += ")";
105 16 : delete pConn;
106 16 : throw NoConnectException( sMessage ,Reference< XInterface > () );
107 18 : }
108 : }
109 0 : else if ( aDesc.getName() == "socket" )
110 : {
111 0 : OUString aHost;
112 0 : if (aDesc.hasParameter("host"))
113 0 : aHost = aDesc.getParameter("host");
114 : else
115 0 : aHost = "localhost";
116 : sal_uInt16 nPort = static_cast< sal_uInt16 >(
117 : aDesc.getParameter("port").
118 0 : toInt32());
119 : bool bTcpNoDelay
120 0 : = aDesc.getParameter("tcpnodelay").toInt32() != 0;
121 :
122 0 : SocketConnection *pConn = new SocketConnection( sConnectionDescription);
123 :
124 0 : SocketAddr AddrTarget( aHost.pData, nPort );
125 0 : if(pConn->m_socket.connect(AddrTarget) != osl_Socket_Ok)
126 : {
127 0 : OUString sMessage("Connector : couldn't connect to socket (");
128 0 : OUString sError = pConn->m_socket.getErrorAsString();
129 0 : sMessage += sError;
130 0 : sMessage += ")";
131 0 : delete pConn;
132 0 : throw NoConnectException( sMessage, Reference < XInterface > () );
133 : }
134 0 : if( bTcpNoDelay )
135 : {
136 0 : sal_Int32 nTcpNoDelay = sal_True;
137 : pConn->m_socket.setOption( osl_Socket_OptionTcpNoDelay , &nTcpNoDelay,
138 0 : sizeof( nTcpNoDelay ) , osl_Socket_LevelTcp );
139 : }
140 0 : pConn->completeConnectionString();
141 0 : r = Reference< XConnection > ( (XConnection * ) pConn );
142 : }
143 : else
144 : {
145 0 : OUString delegatee("com.sun.star.connection.Connector.");
146 0 : delegatee += aDesc.getName();
147 :
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 0 : _xSMgr->createInstanceWithContext(delegatee, _xCtx), UNO_QUERY );
154 :
155 0 : if(!xConnector.is())
156 : {
157 0 : OUString message("Connector: unknown delegatee ");
158 0 : message += delegatee;
159 :
160 0 : throw ConnectionSetupException(message, Reference<XInterface>());
161 : }
162 :
163 0 : sal_Int32 index = sConnectionDescription.indexOf((sal_Unicode) ',');
164 :
165 0 : r = xConnector->connect(sConnectionDescription.copy(index + 1).trim());
166 : }
167 20 : return r;
168 : }
169 0 : catch (const rtl::MalformedUriException & rEx)
170 : {
171 0 : throw ConnectionSetupException(rEx.getMessage(),
172 0 : Reference< XInterface > ());
173 : }
174 : }
175 :
176 2 : Sequence< OUString > connector_getSupportedServiceNames()
177 : {
178 2 : Sequence< OUString > seqNames(1);
179 2 : seqNames.getArray()[0] = SERVICE_NAME;
180 2 : return seqNames;
181 : }
182 :
183 2 : OUString connector_getImplementationName()
184 : {
185 2 : return OUString( IMPLEMENTATION_NAME );
186 : }
187 :
188 0 : OUString OConnector::getImplementationName() throw()
189 : {
190 0 : return connector_getImplementationName();
191 : }
192 :
193 0 : sal_Bool OConnector::supportsService(const OUString& ServiceName) throw()
194 : {
195 0 : Sequence< OUString > aSNL = getSupportedServiceNames();
196 0 : const OUString * pArray = aSNL.getConstArray();
197 :
198 0 : for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
199 0 : if( pArray[i] == ServiceName )
200 0 : return sal_True;
201 :
202 0 : return sal_False;
203 : }
204 :
205 0 : Sequence< OUString > OConnector::getSupportedServiceNames(void) throw()
206 : {
207 0 : return connector_getSupportedServiceNames();
208 : }
209 :
210 18 : Reference< XInterface > SAL_CALL connector_CreateInstance( const Reference< XComponentContext > & xCtx)
211 : {
212 18 : return Reference < XInterface >( ( OWeakObject * ) new OConnector(xCtx) );
213 : }
214 :
215 :
216 : }
217 : using namespace stoc_connector;
218 :
219 : static struct ImplementationEntry g_entries[] =
220 : {
221 : {
222 : connector_CreateInstance, connector_getImplementationName ,
223 : connector_getSupportedServiceNames, createSingleComponentFactory ,
224 : 0, 0
225 : },
226 : { 0, 0, 0, 0, 0, 0 }
227 : };
228 :
229 2 : extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL connector_component_getFactory(
230 : const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
231 : {
232 2 : return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
233 : }
234 :
235 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|