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