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/supportsservice.hxx>
28 : #include "cppuhelper/unourl.hxx"
29 : #include "rtl/malformeduriexception.hxx"
30 :
31 : #include <com/sun/star/lang/XServiceInfo.hpp>
32 : #include <com/sun/star/lang/IllegalArgumentException.hpp>
33 : #include <com/sun/star/connection/XConnector.hpp>
34 :
35 : #include "services.hxx"
36 : #include "connector.hxx"
37 :
38 : #define IMPLEMENTATION_NAME "com.sun.star.comp.io.Connector"
39 : #define SERVICE_NAME "com.sun.star.connection.Connector"
40 :
41 : using namespace ::osl;
42 : using namespace ::cppu;
43 : using namespace ::com::sun::star::uno;
44 : using namespace ::com::sun::star::lang;
45 : using namespace ::com::sun::star::registry;
46 : using namespace ::com::sun::star::connection;
47 :
48 : namespace stoc_connector
49 : {
50 : class OConnector : public WeakImplHelper2< XConnector, XServiceInfo >
51 : {
52 : Reference< XMultiComponentFactory > _xSMgr;
53 : Reference< XComponentContext > _xCtx;
54 : public:
55 : OConnector(const Reference< XComponentContext > &xCtx);
56 : virtual ~OConnector();
57 : // Methods
58 : virtual Reference< XConnection > SAL_CALL connect(
59 : const OUString& sConnectionDescription )
60 : throw( NoConnectException, ConnectionSetupException, RuntimeException, std::exception) SAL_OVERRIDE;
61 :
62 : public: // XServiceInfo
63 : virtual OUString SAL_CALL getImplementationName() throw(std::exception) SAL_OVERRIDE;
64 : virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw(std::exception) SAL_OVERRIDE;
65 : virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw(std::exception) SAL_OVERRIDE;
66 : };
67 :
68 10 : OConnector::OConnector(const Reference< XComponentContext > &xCtx)
69 10 : : _xSMgr( xCtx->getServiceManager() )
70 20 : , _xCtx( xCtx )
71 10 : {}
72 :
73 20 : OConnector::~OConnector() {}
74 :
75 9 : Reference< XConnection > SAL_CALL OConnector::connect( const OUString& sConnectionDescription )
76 : throw( NoConnectException, ConnectionSetupException, RuntimeException, std::exception)
77 : {
78 : OSL_TRACE(
79 : "connector %s\n",
80 : OUStringToOString(
81 : sConnectionDescription, RTL_TEXTENCODING_ASCII_US).getStr());
82 :
83 : // split string into tokens
84 : try
85 : {
86 9 : cppu::UnoUrlDescriptor aDesc(sConnectionDescription);
87 :
88 18 : Reference< XConnection > r;
89 9 : if ( aDesc.getName() == "pipe" )
90 : {
91 9 : OUString aName(aDesc.getParameter("name"));
92 :
93 9 : PipeConnection *pConn = new PipeConnection( sConnectionDescription );
94 :
95 9 : if( pConn->m_pipe.create( aName.pData, osl_Pipe_OPEN, osl::Security() ) )
96 : {
97 2 : r = Reference < XConnection > ( static_cast<XConnection *>(pConn) );
98 : }
99 : else
100 : {
101 7 : OUString sMessage("Connector : couldn't connect to pipe ");
102 7 : sMessage += aName;
103 7 : sMessage += "(";
104 7 : sMessage += OUString::number( pConn->m_pipe.getError() );
105 7 : sMessage += ")";
106 7 : delete pConn;
107 7 : throw NoConnectException( sMessage );
108 9 : }
109 : }
110 0 : else if ( aDesc.getName() == "socket" )
111 : {
112 0 : OUString aHost;
113 0 : if (aDesc.hasParameter("host"))
114 0 : aHost = aDesc.getParameter("host");
115 : else
116 0 : aHost = "localhost";
117 : sal_uInt16 nPort = static_cast< sal_uInt16 >(
118 : aDesc.getParameter("port").
119 0 : toInt32());
120 : bool bTcpNoDelay
121 0 : = aDesc.getParameter("tcpnodelay").toInt32() != 0;
122 :
123 0 : SocketConnection *pConn = new SocketConnection( sConnectionDescription);
124 :
125 0 : SocketAddr AddrTarget( aHost.pData, nPort );
126 0 : if(pConn->m_socket.connect(AddrTarget) != osl_Socket_Ok)
127 : {
128 0 : OUString sMessage("Connector : couldn't connect to socket (");
129 0 : OUString sError = pConn->m_socket.getErrorAsString();
130 0 : sMessage += sError;
131 0 : sMessage += ")";
132 0 : delete pConn;
133 0 : throw NoConnectException( sMessage );
134 : }
135 : // we enable tcpNoDelay for loopback connections because
136 : // it can make a significant speed difference on linux boxes.
137 0 : if( bTcpNoDelay || aHost == "localhost" || aHost.startsWith("127.0.0.") )
138 : {
139 0 : sal_Int32 nTcpNoDelay = sal_Int32(true);
140 : pConn->m_socket.setOption( osl_Socket_OptionTcpNoDelay , &nTcpNoDelay,
141 0 : sizeof( nTcpNoDelay ) , osl_Socket_LevelTcp );
142 : }
143 0 : pConn->completeConnectionString();
144 0 : r = Reference< XConnection > ( static_cast<XConnection *>(pConn) );
145 : }
146 : else
147 : {
148 0 : OUString delegatee("com.sun.star.connection.Connector.");
149 0 : delegatee += aDesc.getName();
150 :
151 : OSL_TRACE(
152 : "connector: trying to get service %s\n",
153 : OUStringToOString(
154 : delegatee, RTL_TEXTENCODING_ASCII_US).getStr());
155 : Reference<XConnector> xConnector(
156 0 : _xSMgr->createInstanceWithContext(delegatee, _xCtx), UNO_QUERY );
157 :
158 0 : if(!xConnector.is())
159 : {
160 0 : OUString message("Connector: unknown delegatee ");
161 0 : message += delegatee;
162 :
163 0 : throw ConnectionSetupException(message);
164 : }
165 :
166 0 : sal_Int32 index = sConnectionDescription.indexOf((sal_Unicode) ',');
167 :
168 0 : r = xConnector->connect(sConnectionDescription.copy(index + 1).trim());
169 : }
170 11 : return r;
171 : }
172 0 : catch (const rtl::MalformedUriException & rEx)
173 : {
174 0 : throw ConnectionSetupException(rEx.getMessage());
175 : }
176 : }
177 :
178 4 : Sequence< OUString > connector_getSupportedServiceNames()
179 : {
180 4 : Sequence< OUString > seqNames(1);
181 4 : seqNames.getArray()[0] = SERVICE_NAME;
182 4 : return seqNames;
183 : }
184 :
185 174 : OUString connector_getImplementationName()
186 : {
187 174 : return OUString( IMPLEMENTATION_NAME );
188 : }
189 :
190 1 : OUString OConnector::getImplementationName() throw(std::exception)
191 : {
192 1 : return connector_getImplementationName();
193 : }
194 :
195 0 : sal_Bool OConnector::supportsService(const OUString& ServiceName) throw(std::exception)
196 : {
197 0 : return cppu::supportsService(this, ServiceName);
198 : }
199 :
200 1 : Sequence< OUString > OConnector::getSupportedServiceNames() throw(std::exception)
201 : {
202 1 : return connector_getSupportedServiceNames();
203 : }
204 :
205 10 : Reference< XInterface > SAL_CALL connector_CreateInstance( const Reference< XComponentContext > & xCtx)
206 : {
207 10 : return Reference < XInterface >( static_cast<OWeakObject *>(new OConnector(xCtx)) );
208 : }
209 :
210 :
211 : }
212 :
213 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|