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(void) 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 8 : 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 8 : cppu::UnoUrlDescriptor aDesc(sConnectionDescription);
87 :
88 16 : Reference< XConnection > r;
89 8 : if ( aDesc.getName() == "pipe" )
90 : {
91 8 : OUString aName(aDesc.getParameter("name"));
92 :
93 8 : PipeConnection *pConn = new PipeConnection( sConnectionDescription );
94 :
95 8 : if( pConn->m_pipe.create( aName.pData, osl_Pipe_OPEN, osl::Security() ) )
96 : {
97 4 : r = Reference < XConnection > ( (XConnection * ) pConn );
98 : }
99 : else
100 : {
101 4 : OUString sMessage("Connector : couldn't connect to pipe ");
102 4 : sMessage += aName;
103 4 : sMessage += "(";
104 4 : sMessage += OUString::number( pConn->m_pipe.getError() );
105 4 : sMessage += ")";
106 4 : delete pConn;
107 4 : throw NoConnectException( sMessage );
108 8 : }
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 0 : if( bTcpNoDelay )
136 : {
137 0 : sal_Int32 nTcpNoDelay = sal_True;
138 : pConn->m_socket.setOption( osl_Socket_OptionTcpNoDelay , &nTcpNoDelay,
139 0 : sizeof( nTcpNoDelay ) , osl_Socket_LevelTcp );
140 : }
141 0 : pConn->completeConnectionString();
142 0 : r = Reference< XConnection > ( (XConnection * ) pConn );
143 : }
144 : else
145 : {
146 0 : OUString delegatee("com.sun.star.connection.Connector.");
147 0 : delegatee += aDesc.getName();
148 :
149 : OSL_TRACE(
150 : "connector: trying to get service %s\n",
151 : OUStringToOString(
152 : delegatee, RTL_TEXTENCODING_ASCII_US).getStr());
153 : Reference<XConnector> xConnector(
154 0 : _xSMgr->createInstanceWithContext(delegatee, _xCtx), UNO_QUERY );
155 :
156 0 : if(!xConnector.is())
157 : {
158 0 : OUString message("Connector: unknown delegatee ");
159 0 : message += delegatee;
160 :
161 0 : throw ConnectionSetupException(message);
162 : }
163 :
164 0 : sal_Int32 index = sConnectionDescription.indexOf((sal_Unicode) ',');
165 :
166 0 : r = xConnector->connect(sConnectionDescription.copy(index + 1).trim());
167 : }
168 12 : return r;
169 : }
170 0 : catch (const rtl::MalformedUriException & rEx)
171 : {
172 0 : throw ConnectionSetupException(rEx.getMessage());
173 : }
174 : }
175 :
176 6 : Sequence< OUString > connector_getSupportedServiceNames()
177 : {
178 6 : Sequence< OUString > seqNames(1);
179 6 : seqNames.getArray()[0] = SERVICE_NAME;
180 6 : return seqNames;
181 : }
182 :
183 208 : OUString connector_getImplementationName()
184 : {
185 208 : return OUString( IMPLEMENTATION_NAME );
186 : }
187 :
188 0 : OUString OConnector::getImplementationName() throw(std::exception)
189 : {
190 0 : return connector_getImplementationName();
191 : }
192 :
193 0 : sal_Bool OConnector::supportsService(const OUString& ServiceName) throw(std::exception)
194 : {
195 0 : return cppu::supportsService(this, ServiceName);
196 : }
197 :
198 0 : Sequence< OUString > OConnector::getSupportedServiceNames(void) throw(std::exception)
199 : {
200 0 : return connector_getSupportedServiceNames();
201 : }
202 :
203 10 : Reference< XInterface > SAL_CALL connector_CreateInstance( const Reference< XComponentContext > & xCtx)
204 : {
205 10 : return Reference < XInterface >( ( OWeakObject * ) new OConnector(xCtx) );
206 : }
207 :
208 :
209 : }
210 :
211 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|