xref: /trunk/main/io/source/connector/connector.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_io.hxx"
30*cdf0e10cSrcweir #include <osl/mutex.hxx>
31*cdf0e10cSrcweir #include "osl/security.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <uno/mapping.hxx>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
36*cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
37*cdf0e10cSrcweir #include <cppuhelper/implementationentry.hxx>
38*cdf0e10cSrcweir #include "cppuhelper/unourl.hxx"
39*cdf0e10cSrcweir #include "rtl/malformeduriexception.hxx"
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/connection/XConnector.hpp>
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir #include "connector.hxx"
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir #define IMPLEMENTATION_NAME "com.sun.star.comp.io.Connector"
48*cdf0e10cSrcweir #define SERVICE_NAME "com.sun.star.connection.Connector"
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir using namespace ::osl;
51*cdf0e10cSrcweir using namespace ::rtl;
52*cdf0e10cSrcweir using namespace ::cppu;
53*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
54*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
55*cdf0e10cSrcweir using namespace ::com::sun::star::registry;
56*cdf0e10cSrcweir using namespace ::com::sun::star::connection;
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir namespace stoc_connector
59*cdf0e10cSrcweir {
60*cdf0e10cSrcweir     rtl_StandardModuleCount g_moduleCount = MODULE_COUNT_INIT;
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir     class OConnector : public WeakImplHelper2< XConnector, XServiceInfo >
63*cdf0e10cSrcweir     {
64*cdf0e10cSrcweir         Reference< XMultiComponentFactory > _xSMgr;
65*cdf0e10cSrcweir         Reference< XComponentContext > _xCtx;
66*cdf0e10cSrcweir     public:
67*cdf0e10cSrcweir         OConnector(const Reference< XComponentContext > &xCtx);
68*cdf0e10cSrcweir         ~OConnector();
69*cdf0e10cSrcweir         // Methods
70*cdf0e10cSrcweir         virtual Reference< XConnection > SAL_CALL connect(
71*cdf0e10cSrcweir             const OUString& sConnectionDescription )
72*cdf0e10cSrcweir             throw( NoConnectException, ConnectionSetupException, RuntimeException);
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir     public: // XServiceInfo
75*cdf0e10cSrcweir                 virtual OUString              SAL_CALL getImplementationName() throw();
76*cdf0e10cSrcweir                 virtual Sequence< OUString >  SAL_CALL getSupportedServiceNames(void) throw();
77*cdf0e10cSrcweir                 virtual sal_Bool              SAL_CALL supportsService(const OUString& ServiceName) throw();
78*cdf0e10cSrcweir     };
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir     OConnector::OConnector(const Reference< XComponentContext > &xCtx)
81*cdf0e10cSrcweir         : _xSMgr( xCtx->getServiceManager() )
82*cdf0e10cSrcweir         , _xCtx( xCtx )
83*cdf0e10cSrcweir     {
84*cdf0e10cSrcweir         g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
85*cdf0e10cSrcweir     }
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir     OConnector::~OConnector()
88*cdf0e10cSrcweir     {
89*cdf0e10cSrcweir         g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
90*cdf0e10cSrcweir     }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir     Reference< XConnection > SAL_CALL OConnector::connect( const OUString& sConnectionDescription )
93*cdf0e10cSrcweir         throw( NoConnectException, ConnectionSetupException, RuntimeException)
94*cdf0e10cSrcweir     {
95*cdf0e10cSrcweir         OSL_TRACE(
96*cdf0e10cSrcweir             "connector %s\n",
97*cdf0e10cSrcweir             OUStringToOString(
98*cdf0e10cSrcweir                 sConnectionDescription, RTL_TEXTENCODING_ASCII_US).getStr());
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir         // split string into tokens
101*cdf0e10cSrcweir         try
102*cdf0e10cSrcweir         {
103*cdf0e10cSrcweir             cppu::UnoUrlDescriptor aDesc(sConnectionDescription);
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir             Reference< XConnection > r;
106*cdf0e10cSrcweir             if (aDesc.getName().equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(
107*cdf0e10cSrcweir                                                  "pipe")))
108*cdf0e10cSrcweir             {
109*cdf0e10cSrcweir                 rtl::OUString aName(
110*cdf0e10cSrcweir                     aDesc.getParameter(
111*cdf0e10cSrcweir                         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("name"))));
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir                 PipeConnection *pConn = new PipeConnection( sConnectionDescription );
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir                 if( pConn->m_pipe.create( aName.pData, osl_Pipe_OPEN, osl::Security() ) )
116*cdf0e10cSrcweir                 {
117*cdf0e10cSrcweir                     r = Reference < XConnection > ( (XConnection * ) pConn );
118*cdf0e10cSrcweir                 }
119*cdf0e10cSrcweir                 else
120*cdf0e10cSrcweir                 {
121*cdf0e10cSrcweir                     OUString sMessage = OUString::createFromAscii( "Connector : couldn't connect to pipe " );
122*cdf0e10cSrcweir                     sMessage += aName;
123*cdf0e10cSrcweir                     sMessage += OUString::createFromAscii( "(" );
124*cdf0e10cSrcweir                     sMessage += OUString::valueOf( (sal_Int32 ) pConn->m_pipe.getError() );
125*cdf0e10cSrcweir                     sMessage += OUString::createFromAscii( ")" );
126*cdf0e10cSrcweir                     delete pConn;
127*cdf0e10cSrcweir                     throw NoConnectException( sMessage ,Reference< XInterface > () );
128*cdf0e10cSrcweir                 }
129*cdf0e10cSrcweir             }
130*cdf0e10cSrcweir             else if (aDesc.getName().equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(
131*cdf0e10cSrcweir                                                       "socket")))
132*cdf0e10cSrcweir             {
133*cdf0e10cSrcweir                 rtl::OUString aHost;
134*cdf0e10cSrcweir                 if (aDesc.hasParameter(
135*cdf0e10cSrcweir                         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("host"))))
136*cdf0e10cSrcweir                     aHost = aDesc.getParameter(
137*cdf0e10cSrcweir                         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("host")));
138*cdf0e10cSrcweir                 else
139*cdf0e10cSrcweir                     aHost = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
140*cdf0e10cSrcweir                                               "localhost"));
141*cdf0e10cSrcweir                 sal_uInt16 nPort = static_cast< sal_uInt16 >(
142*cdf0e10cSrcweir                     aDesc.getParameter(
143*cdf0e10cSrcweir                         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("port"))).
144*cdf0e10cSrcweir                     toInt32());
145*cdf0e10cSrcweir                 bool bTcpNoDelay
146*cdf0e10cSrcweir                     = aDesc.getParameter(
147*cdf0e10cSrcweir                         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
148*cdf0e10cSrcweir                                           "tcpnodelay"))).toInt32() != 0;
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir                 SocketConnection *pConn = new SocketConnection( sConnectionDescription);
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir                 SocketAddr AddrTarget( aHost.pData, nPort );
153*cdf0e10cSrcweir                 if(pConn->m_socket.connect(AddrTarget) != osl_Socket_Ok)
154*cdf0e10cSrcweir                 {
155*cdf0e10cSrcweir                     OUString sMessage = OUString::createFromAscii( "Connector : couldn't connect to socket (" );
156*cdf0e10cSrcweir                     OUString sError = pConn->m_socket.getErrorAsString();
157*cdf0e10cSrcweir                     sMessage += sError;
158*cdf0e10cSrcweir                     sMessage += OUString::createFromAscii( ")" );
159*cdf0e10cSrcweir                     delete pConn;
160*cdf0e10cSrcweir                     throw NoConnectException( sMessage, Reference < XInterface > () );
161*cdf0e10cSrcweir                 }
162*cdf0e10cSrcweir                 if( bTcpNoDelay )
163*cdf0e10cSrcweir                 {
164*cdf0e10cSrcweir                     sal_Int32 nTcpNoDelay = sal_True;
165*cdf0e10cSrcweir                     pConn->m_socket.setOption( osl_Socket_OptionTcpNoDelay , &nTcpNoDelay,
166*cdf0e10cSrcweir                                                sizeof( nTcpNoDelay ) , osl_Socket_LevelTcp );
167*cdf0e10cSrcweir                 }
168*cdf0e10cSrcweir                 pConn->completeConnectionString();
169*cdf0e10cSrcweir                 r = Reference< XConnection > ( (XConnection * ) pConn );
170*cdf0e10cSrcweir             }
171*cdf0e10cSrcweir             else
172*cdf0e10cSrcweir             {
173*cdf0e10cSrcweir                 OUString delegatee = OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Connector."));
174*cdf0e10cSrcweir                 delegatee += aDesc.getName();
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir                 OSL_TRACE(
177*cdf0e10cSrcweir                     "connector: trying to get service %s\n",
178*cdf0e10cSrcweir                     OUStringToOString(
179*cdf0e10cSrcweir                         delegatee, RTL_TEXTENCODING_ASCII_US).getStr());
180*cdf0e10cSrcweir                 Reference<XConnector> xConnector(
181*cdf0e10cSrcweir                     _xSMgr->createInstanceWithContext(delegatee, _xCtx), UNO_QUERY );
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir                 if(!xConnector.is())
184*cdf0e10cSrcweir                 {
185*cdf0e10cSrcweir                     OUString message(RTL_CONSTASCII_USTRINGPARAM("Connector: unknown delegatee "));
186*cdf0e10cSrcweir                     message += delegatee;
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir                     throw ConnectionSetupException(message, Reference<XInterface>());
189*cdf0e10cSrcweir                 }
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir                 sal_Int32 index = sConnectionDescription.indexOf((sal_Unicode) ',');
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir                 r = xConnector->connect(sConnectionDescription.copy(index + 1).trim());
194*cdf0e10cSrcweir             }
195*cdf0e10cSrcweir             return r;
196*cdf0e10cSrcweir         }
197*cdf0e10cSrcweir         catch (rtl::MalformedUriException & rEx)
198*cdf0e10cSrcweir         {
199*cdf0e10cSrcweir             throw ConnectionSetupException(rEx.getMessage(),
200*cdf0e10cSrcweir                                            Reference< XInterface > ());
201*cdf0e10cSrcweir         }
202*cdf0e10cSrcweir     }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir     Sequence< OUString > connector_getSupportedServiceNames()
205*cdf0e10cSrcweir     {
206*cdf0e10cSrcweir         static Sequence < OUString > *pNames = 0;
207*cdf0e10cSrcweir         if( ! pNames )
208*cdf0e10cSrcweir         {
209*cdf0e10cSrcweir             MutexGuard guard( Mutex::getGlobalMutex() );
210*cdf0e10cSrcweir             if( !pNames )
211*cdf0e10cSrcweir             {
212*cdf0e10cSrcweir                 static Sequence< OUString > seqNames(1);
213*cdf0e10cSrcweir                 seqNames.getArray()[0] = OUString::createFromAscii( SERVICE_NAME );
214*cdf0e10cSrcweir                 pNames = &seqNames;
215*cdf0e10cSrcweir             }
216*cdf0e10cSrcweir         }
217*cdf0e10cSrcweir         return *pNames;
218*cdf0e10cSrcweir     }
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir     OUString connector_getImplementationName()
221*cdf0e10cSrcweir     {
222*cdf0e10cSrcweir         return OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) );
223*cdf0e10cSrcweir     }
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir         OUString OConnector::getImplementationName() throw()
226*cdf0e10cSrcweir     {
227*cdf0e10cSrcweir         return connector_getImplementationName();
228*cdf0e10cSrcweir     }
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir         sal_Bool OConnector::supportsService(const OUString& ServiceName) throw()
231*cdf0e10cSrcweir     {
232*cdf0e10cSrcweir         Sequence< OUString > aSNL = getSupportedServiceNames();
233*cdf0e10cSrcweir         const OUString * pArray = aSNL.getConstArray();
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir         for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
236*cdf0e10cSrcweir             if( pArray[i] == ServiceName )
237*cdf0e10cSrcweir                 return sal_True;
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir         return sal_False;
240*cdf0e10cSrcweir     }
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir         Sequence< OUString > OConnector::getSupportedServiceNames(void) throw()
243*cdf0e10cSrcweir     {
244*cdf0e10cSrcweir         return connector_getSupportedServiceNames();
245*cdf0e10cSrcweir     }
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir     Reference< XInterface > SAL_CALL connector_CreateInstance( const Reference< XComponentContext > & xCtx)
248*cdf0e10cSrcweir     {
249*cdf0e10cSrcweir         return Reference < XInterface >( ( OWeakObject * ) new OConnector(xCtx) );
250*cdf0e10cSrcweir     }
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir }
254*cdf0e10cSrcweir using namespace stoc_connector;
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir static struct ImplementationEntry g_entries[] =
257*cdf0e10cSrcweir {
258*cdf0e10cSrcweir     {
259*cdf0e10cSrcweir         connector_CreateInstance, connector_getImplementationName ,
260*cdf0e10cSrcweir         connector_getSupportedServiceNames, createSingleComponentFactory ,
261*cdf0e10cSrcweir         &g_moduleCount.modCnt , 0
262*cdf0e10cSrcweir     },
263*cdf0e10cSrcweir     { 0, 0, 0, 0, 0, 0 }
264*cdf0e10cSrcweir };
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir extern "C"
267*cdf0e10cSrcweir {
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir sal_Bool SAL_CALL component_canUnload( TimeValue *pTime )
270*cdf0e10cSrcweir {
271*cdf0e10cSrcweir     return g_moduleCount.canUnload( &g_moduleCount , pTime );
272*cdf0e10cSrcweir }
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir //==================================================================================================
275*cdf0e10cSrcweir void SAL_CALL component_getImplementationEnvironment(
276*cdf0e10cSrcweir     const sal_Char ** ppEnvTypeName, uno_Environment ** )
277*cdf0e10cSrcweir {
278*cdf0e10cSrcweir     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
279*cdf0e10cSrcweir }
280*cdf0e10cSrcweir //==================================================================================================
281*cdf0e10cSrcweir void * SAL_CALL component_getFactory(
282*cdf0e10cSrcweir     const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
283*cdf0e10cSrcweir {
284*cdf0e10cSrcweir     return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
285*cdf0e10cSrcweir }
286*cdf0e10cSrcweir 
287*cdf0e10cSrcweir }
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir 
290