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