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