12722ceddSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 32722ceddSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 42722ceddSAndrew Rist * or more contributor license agreements. See the NOTICE file 52722ceddSAndrew Rist * distributed with this work for additional information 62722ceddSAndrew Rist * regarding copyright ownership. The ASF licenses this file 72722ceddSAndrew Rist * to you under the Apache License, Version 2.0 (the 82722ceddSAndrew Rist * "License"); you may not use this file except in compliance 92722ceddSAndrew Rist * with the License. You may obtain a copy of the License at 102722ceddSAndrew Rist * 112722ceddSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 122722ceddSAndrew Rist * 132722ceddSAndrew Rist * Unless required by applicable law or agreed to in writing, 142722ceddSAndrew Rist * software distributed under the License is distributed on an 152722ceddSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 162722ceddSAndrew Rist * KIND, either express or implied. See the License for the 172722ceddSAndrew Rist * specific language governing permissions and limitations 182722ceddSAndrew Rist * under the License. 192722ceddSAndrew Rist * 202722ceddSAndrew Rist *************************************************************/ 212722ceddSAndrew Rist 222722ceddSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_desktop.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include "acceptor.hxx" 28cdf0e10cSrcweir #include <unotools/bootstrap.hxx> 29cdf0e10cSrcweir #include <vos/process.hxx> 30cdf0e10cSrcweir #include <tools/urlobj.hxx> 31cdf0e10cSrcweir #include <tools/stream.hxx> 32cdf0e10cSrcweir #include <vcl/svapp.hxx> 33cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp> 34cdf0e10cSrcweir #ifndef _COM_SUN_STAR_UNO_XNAMEINGSERVICE_HPP_ 35cdf0e10cSrcweir #include <com/sun/star/uno/XNamingService.hpp> 36cdf0e10cSrcweir #endif 37cdf0e10cSrcweir 38cdf0e10cSrcweir #include <cppuhelper/factory.hxx> 39cdf0e10cSrcweir 40cdf0e10cSrcweir namespace desktop 41cdf0e10cSrcweir { 42cdf0e10cSrcweir 43cdf0e10cSrcweir extern "C" void workerfunc (void * acc) 44cdf0e10cSrcweir { 45cdf0e10cSrcweir ((Acceptor*)acc)->run(); 46cdf0e10cSrcweir } 47cdf0e10cSrcweir 48cdf0e10cSrcweir static Reference<XInterface> getComponentContext( const Reference<XMultiServiceFactory>& rFactory) 49cdf0e10cSrcweir { 50cdf0e10cSrcweir Reference<XInterface> rContext; 51cdf0e10cSrcweir Reference< XPropertySet > rPropSet( rFactory, UNO_QUERY ); 52cdf0e10cSrcweir Any a = rPropSet->getPropertyValue( 53cdf0e10cSrcweir ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ) ) ); 54cdf0e10cSrcweir a >>= rContext; 55cdf0e10cSrcweir return rContext; 56cdf0e10cSrcweir } 57cdf0e10cSrcweir 58cdf0e10cSrcweir Mutex Acceptor::m_aMutex; 59cdf0e10cSrcweir 60cdf0e10cSrcweir Acceptor::Acceptor( const Reference< XMultiServiceFactory >& rFactory ) 61cdf0e10cSrcweir : m_thread(NULL) 62cdf0e10cSrcweir , m_aAcceptString() 63cdf0e10cSrcweir , m_aConnectString() 64cdf0e10cSrcweir , m_aProtocol() 65cdf0e10cSrcweir , m_bInit(sal_False) 66cdf0e10cSrcweir , m_bDying(false) 67cdf0e10cSrcweir { 68cdf0e10cSrcweir m_rSMgr = rFactory; 69cdf0e10cSrcweir m_rAcceptor = Reference< XAcceptor > (m_rSMgr->createInstance( 70cdf0e10cSrcweir rtl::OUString::createFromAscii( "com.sun.star.connection.Acceptor" )), 71cdf0e10cSrcweir UNO_QUERY ); 72cdf0e10cSrcweir m_rBridgeFactory = Reference < XBridgeFactory > (m_rSMgr->createInstance( 73cdf0e10cSrcweir rtl::OUString::createFromAscii( "com.sun.star.bridge.BridgeFactory" )), 74cdf0e10cSrcweir UNO_QUERY ); 75cdf0e10cSrcweir // get component context 76cdf0e10cSrcweir m_rContext = getComponentContext(m_rSMgr); 77cdf0e10cSrcweir } 78cdf0e10cSrcweir 79cdf0e10cSrcweir 80cdf0e10cSrcweir Acceptor::~Acceptor() 81cdf0e10cSrcweir { 82cdf0e10cSrcweir m_rAcceptor->stopAccepting(); 83cdf0e10cSrcweir oslThread t; 84cdf0e10cSrcweir { 85cdf0e10cSrcweir osl::MutexGuard g(m_aMutex); 86cdf0e10cSrcweir t = m_thread; 87cdf0e10cSrcweir } 88cdf0e10cSrcweir //prevent locking if the thread is still waiting 89cdf0e10cSrcweir m_bDying = true; 90cdf0e10cSrcweir m_cEnable.set(); 91cdf0e10cSrcweir osl_joinWithThread(t); 92cdf0e10cSrcweir { 93cdf0e10cSrcweir // Make the final state of m_bridges visible to this thread (since 94cdf0e10cSrcweir // m_thread is joined, the code that follows is the only one left 95cdf0e10cSrcweir // accessing m_bridges): 96cdf0e10cSrcweir osl::MutexGuard g(m_aMutex); 97cdf0e10cSrcweir } 98cdf0e10cSrcweir for (;;) { 99cdf0e10cSrcweir com::sun::star::uno::Reference< com::sun::star::bridge::XBridge > b( 100cdf0e10cSrcweir m_bridges.remove()); 101cdf0e10cSrcweir if (!b.is()) { 102cdf0e10cSrcweir break; 103cdf0e10cSrcweir } 104cdf0e10cSrcweir com::sun::star::uno::Reference< com::sun::star::lang::XComponent >( 105cdf0e10cSrcweir b, com::sun::star::uno::UNO_QUERY_THROW)->dispose(); 106cdf0e10cSrcweir } 107cdf0e10cSrcweir } 108cdf0e10cSrcweir 109cdf0e10cSrcweir void SAL_CALL Acceptor::run() 110cdf0e10cSrcweir { 111cdf0e10cSrcweir while ( m_rAcceptor.is() && m_rBridgeFactory.is() ) 112cdf0e10cSrcweir { 113cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "desktop (lo119109) Acceptor::run" ); 114cdf0e10cSrcweir try 115cdf0e10cSrcweir { 116cdf0e10cSrcweir // wait until we get enabled 117cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_TRACE( aLog, "desktop (lo119109)"\ 118cdf0e10cSrcweir "Acceptor::run waiting for office to come up"); 119cdf0e10cSrcweir m_cEnable.wait(); 120cdf0e10cSrcweir if (m_bDying) //see destructor 121cdf0e10cSrcweir break; 122cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_TRACE( aLog, "desktop (lo119109)"\ 123cdf0e10cSrcweir "Acceptor::run now enabled and continuing"); 124cdf0e10cSrcweir 125cdf0e10cSrcweir // accept connection 126cdf0e10cSrcweir Reference< XConnection > rConnection = m_rAcceptor->accept( m_aConnectString ); 127*796b7e2aSmseidel // if we return without a valid connection we must assume that the acceptor 128cdf0e10cSrcweir // is destructed so we break out of the run method terminating the thread 129cdf0e10cSrcweir if (! rConnection.is()) break; 130cdf0e10cSrcweir OUString aDescription = rConnection->getDescription(); 131cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_TRACE1( aLog, "desktop (lo119109) Acceptor::run connection %s", 132cdf0e10cSrcweir OUStringToOString(aDescription, RTL_TEXTENCODING_ASCII_US).getStr()); 133cdf0e10cSrcweir 134cdf0e10cSrcweir // create instanceprovider for this connection 135cdf0e10cSrcweir Reference< XInstanceProvider > rInstanceProvider( 136cdf0e10cSrcweir (XInstanceProvider*)new AccInstanceProvider(m_rSMgr, rConnection)); 137cdf0e10cSrcweir // create the bridge. The remote end will have a reference to this bridge 138cdf0e10cSrcweir // thus preventing the bridge from being disposed. When the remote end releases 139cdf0e10cSrcweir // the bridge, it will be destructed. 140cdf0e10cSrcweir Reference< XBridge > rBridge = m_rBridgeFactory->createBridge( 141cdf0e10cSrcweir rtl::OUString() ,m_aProtocol ,rConnection ,rInstanceProvider ); 142cdf0e10cSrcweir osl::MutexGuard g(m_aMutex); 143cdf0e10cSrcweir m_bridges.add(rBridge); 144cdf0e10cSrcweir } catch (Exception&) { 145cdf0e10cSrcweir // connection failed... 146cdf0e10cSrcweir // something went wrong during connection setup. 147cdf0e10cSrcweir // just wait for a new connection to accept 148cdf0e10cSrcweir } 149cdf0e10cSrcweir } 150cdf0e10cSrcweir } 151cdf0e10cSrcweir 152cdf0e10cSrcweir // XInitialize 153cdf0e10cSrcweir void SAL_CALL Acceptor::initialize( const Sequence<Any>& aArguments ) 154cdf0e10cSrcweir throw( Exception ) 155cdf0e10cSrcweir { 156cdf0e10cSrcweir // prevent multiple initialization 157cdf0e10cSrcweir ClearableMutexGuard aGuard( m_aMutex ); 158cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "destop (lo119109) Acceptor::initialize()" ); 159cdf0e10cSrcweir 160cdf0e10cSrcweir sal_Bool bOk = sal_False; 161cdf0e10cSrcweir 162cdf0e10cSrcweir // arg count 163cdf0e10cSrcweir int nArgs = aArguments.getLength(); 164cdf0e10cSrcweir 165cdf0e10cSrcweir // not yet initialized and acceptstring 166cdf0e10cSrcweir if (!m_bInit && nArgs > 0 && (aArguments[0] >>= m_aAcceptString)) 167cdf0e10cSrcweir { 168cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_TRACE1( aLog, "desktop (lo119109) Acceptor::initialize string=%s", 169cdf0e10cSrcweir OUStringToOString(m_aAcceptString, RTL_TEXTENCODING_ASCII_US).getStr()); 170cdf0e10cSrcweir 171cdf0e10cSrcweir // get connect string and protocol from accept string 172cdf0e10cSrcweir // "<connectString>;<protocol>" 173cdf0e10cSrcweir sal_Int32 nIndex1 = m_aAcceptString.indexOf( (sal_Unicode) ';' ); 174cdf0e10cSrcweir if (nIndex1 < 0) throw IllegalArgumentException( 175cdf0e10cSrcweir OUString::createFromAscii("Invalid accept-string format"), m_rContext, 1); 176cdf0e10cSrcweir m_aConnectString = m_aAcceptString.copy( 0 , nIndex1 ).trim(); 177cdf0e10cSrcweir nIndex1++; 178cdf0e10cSrcweir sal_Int32 nIndex2 = m_aAcceptString.indexOf( (sal_Unicode) ';' , nIndex1 ); 179cdf0e10cSrcweir if (nIndex2 < 0) nIndex2 = m_aAcceptString.getLength(); 180cdf0e10cSrcweir m_aProtocol = m_aAcceptString.copy( nIndex1, nIndex2 - nIndex1 ); 181cdf0e10cSrcweir 182cdf0e10cSrcweir // start accepting in new thread... 183cdf0e10cSrcweir m_thread = osl_createThread(workerfunc, this); 184cdf0e10cSrcweir m_bInit = sal_True; 185cdf0e10cSrcweir bOk = sal_True; 186cdf0e10cSrcweir } 187cdf0e10cSrcweir 188cdf0e10cSrcweir // do we want to enable accepting? 189cdf0e10cSrcweir sal_Bool bEnable = sal_False; 190cdf0e10cSrcweir if (((nArgs == 1 && (aArguments[0] >>= bEnable)) || 191cdf0e10cSrcweir (nArgs == 2 && (aArguments[1] >>= bEnable))) && 192cdf0e10cSrcweir bEnable ) 193cdf0e10cSrcweir { 194cdf0e10cSrcweir m_cEnable.set(); 195cdf0e10cSrcweir bOk = sal_True; 196cdf0e10cSrcweir } 197cdf0e10cSrcweir 198cdf0e10cSrcweir if (!bOk) 199cdf0e10cSrcweir { 200cdf0e10cSrcweir throw IllegalArgumentException( 201cdf0e10cSrcweir OUString::createFromAscii("invalid initialization"), m_rContext, 1); 202cdf0e10cSrcweir } 203cdf0e10cSrcweir } 204cdf0e10cSrcweir 205cdf0e10cSrcweir // XServiceInfo 206cdf0e10cSrcweir const sal_Char *Acceptor::serviceName = "com.sun.star.office.Acceptor"; 207cdf0e10cSrcweir const sal_Char *Acceptor::implementationName = "com.sun.star.office.comp.Acceptor"; 208cdf0e10cSrcweir const sal_Char *Acceptor::supportedServiceNames[] = {"com.sun.star.office.Acceptor", NULL}; 209cdf0e10cSrcweir OUString Acceptor::impl_getImplementationName() 210cdf0e10cSrcweir { 211cdf0e10cSrcweir return OUString::createFromAscii( implementationName ); 212cdf0e10cSrcweir } 213cdf0e10cSrcweir OUString SAL_CALL Acceptor::getImplementationName() 214cdf0e10cSrcweir throw (RuntimeException) 215cdf0e10cSrcweir { 216cdf0e10cSrcweir return Acceptor::impl_getImplementationName(); 217cdf0e10cSrcweir } 218cdf0e10cSrcweir Sequence<OUString> Acceptor::impl_getSupportedServiceNames() 219cdf0e10cSrcweir { 220cdf0e10cSrcweir Sequence<OUString> aSequence; 221cdf0e10cSrcweir for (int i=0; supportedServiceNames[i]!=NULL; i++) { 222cdf0e10cSrcweir aSequence.realloc(i+1); 223cdf0e10cSrcweir aSequence[i]=(OUString::createFromAscii(supportedServiceNames[i])); 224cdf0e10cSrcweir } 225cdf0e10cSrcweir return aSequence; 226cdf0e10cSrcweir } 227cdf0e10cSrcweir Sequence<OUString> SAL_CALL Acceptor::getSupportedServiceNames() 228cdf0e10cSrcweir throw (RuntimeException) 229cdf0e10cSrcweir { 230cdf0e10cSrcweir return Acceptor::impl_getSupportedServiceNames(); 231cdf0e10cSrcweir } 232cdf0e10cSrcweir sal_Bool SAL_CALL Acceptor::supportsService( const OUString&) 233cdf0e10cSrcweir throw (RuntimeException) 234cdf0e10cSrcweir { 235cdf0e10cSrcweir return sal_False; 236cdf0e10cSrcweir } 237cdf0e10cSrcweir 238cdf0e10cSrcweir // Factory 239cdf0e10cSrcweir Reference< XInterface > Acceptor::impl_getInstance( const Reference< XMultiServiceFactory >& aFactory ) 240cdf0e10cSrcweir { 241cdf0e10cSrcweir try { 242cdf0e10cSrcweir return (XComponent*) new Acceptor( aFactory ); 243cdf0e10cSrcweir } catch ( Exception& ) { 244cdf0e10cSrcweir return (XComponent*) NULL; 245cdf0e10cSrcweir } 246cdf0e10cSrcweir } 247cdf0e10cSrcweir 248cdf0e10cSrcweir // InstanceProvider 249cdf0e10cSrcweir AccInstanceProvider::AccInstanceProvider(const Reference<XMultiServiceFactory>& aFactory, const Reference<XConnection>& rConnection) 250cdf0e10cSrcweir { 251cdf0e10cSrcweir m_rSMgr = aFactory; 252cdf0e10cSrcweir m_rConnection = rConnection; 253cdf0e10cSrcweir } 254cdf0e10cSrcweir 255cdf0e10cSrcweir AccInstanceProvider::~AccInstanceProvider() 256cdf0e10cSrcweir { 257cdf0e10cSrcweir } 258cdf0e10cSrcweir 259cdf0e10cSrcweir Reference<XInterface> SAL_CALL AccInstanceProvider::getInstance (const OUString& aName ) 260cdf0e10cSrcweir throw ( NoSuchElementException ) 261cdf0e10cSrcweir { 262cdf0e10cSrcweir 263cdf0e10cSrcweir Reference<XInterface> rInstance; 264cdf0e10cSrcweir 265cdf0e10cSrcweir if ( aName.compareToAscii( "StarOffice.ServiceManager" ) == 0) 266cdf0e10cSrcweir { 267cdf0e10cSrcweir rInstance = Reference< XInterface >( m_rSMgr ); 268cdf0e10cSrcweir } 269cdf0e10cSrcweir else if(aName.compareToAscii( "StarOffice.ComponentContext" ) == 0 ) 270cdf0e10cSrcweir { 271cdf0e10cSrcweir rInstance = getComponentContext( m_rSMgr ); 272cdf0e10cSrcweir } 273cdf0e10cSrcweir else if ( aName.compareToAscii("StarOffice.NamingService" ) == 0 ) 274cdf0e10cSrcweir { 275cdf0e10cSrcweir Reference< XNamingService > rNamingService( 276cdf0e10cSrcweir m_rSMgr->createInstance( OUString::createFromAscii( "com.sun.star.uno.NamingService" )), 277cdf0e10cSrcweir UNO_QUERY ); 278cdf0e10cSrcweir if ( rNamingService.is() ) 279cdf0e10cSrcweir { 280cdf0e10cSrcweir rNamingService->registerObject( 281cdf0e10cSrcweir OUString::createFromAscii( "StarOffice.ServiceManager" ), m_rSMgr ); 282cdf0e10cSrcweir rNamingService->registerObject( 283cdf0e10cSrcweir OUString::createFromAscii( "StarOffice.ComponentContext" ), getComponentContext( m_rSMgr )); 284cdf0e10cSrcweir rInstance = rNamingService; 285cdf0e10cSrcweir } 286cdf0e10cSrcweir } 287cdf0e10cSrcweir /* 288cdf0e10cSrcweir else if ( aName.compareToAscii("com.sun.star.ucb.RemoteContentProviderAcceptor" )) 289cdf0e10cSrcweir { 290cdf0e10cSrcweir Reference< XMultiServiceFactory > rSMgr = ::comphelper::getProcessServiceFactory(); 291cdf0e10cSrcweir if ( rSMgr.is() ) { 292cdf0e10cSrcweir try { 293cdf0e10cSrcweir rInstance = rSMgr->createInstance( sObjectName ); 294cdf0e10cSrcweir } 295cdf0e10cSrcweir catch (Exception const &) {} 296cdf0e10cSrcweir } 297cdf0e10cSrcweir } 298cdf0e10cSrcweir */ 299cdf0e10cSrcweir return rInstance; 300cdf0e10cSrcweir } 301cdf0e10cSrcweir 302cdf0e10cSrcweir } 303cdf0e10cSrcweir 304cdf0e10cSrcweir // component management stuff... 305cdf0e10cSrcweir // ---------------------------------------------------------------------------- 306cdf0e10cSrcweir extern "C" 307cdf0e10cSrcweir { 308cdf0e10cSrcweir using namespace desktop; 309cdf0e10cSrcweir 310cdf0e10cSrcweir void SAL_CALL 311cdf0e10cSrcweir component_getImplementationEnvironment(const sal_Char **ppEnvironmentTypeName, uno_Environment **) 312cdf0e10cSrcweir { 313cdf0e10cSrcweir *ppEnvironmentTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME ; 314cdf0e10cSrcweir } 315cdf0e10cSrcweir 316cdf0e10cSrcweir void * SAL_CALL 317cdf0e10cSrcweir component_getFactory(const sal_Char *pImplementationName, void *pServiceManager, void *) 318cdf0e10cSrcweir { 319cdf0e10cSrcweir void* pReturn = NULL ; 320cdf0e10cSrcweir if ( pImplementationName && pServiceManager ) 321cdf0e10cSrcweir { 322cdf0e10cSrcweir // Define variables which are used in following macros. 323cdf0e10cSrcweir Reference< XSingleServiceFactory > xFactory; 324cdf0e10cSrcweir Reference< XMultiServiceFactory > xServiceManager( 325cdf0e10cSrcweir reinterpret_cast< XMultiServiceFactory* >(pServiceManager)); 326cdf0e10cSrcweir 327cdf0e10cSrcweir if (Acceptor::impl_getImplementationName().compareToAscii( pImplementationName ) == COMPARE_EQUAL ) 328cdf0e10cSrcweir { 329cdf0e10cSrcweir xFactory = Reference< XSingleServiceFactory >( cppu::createSingleFactory( 330cdf0e10cSrcweir xServiceManager, Acceptor::impl_getImplementationName(), 331cdf0e10cSrcweir Acceptor::impl_getInstance, Acceptor::impl_getSupportedServiceNames()) ); 332cdf0e10cSrcweir } 333cdf0e10cSrcweir 334cdf0e10cSrcweir // Factory is valid - service was found. 335cdf0e10cSrcweir if ( xFactory.is() ) 336cdf0e10cSrcweir { 337cdf0e10cSrcweir xFactory->acquire(); 338cdf0e10cSrcweir pReturn = xFactory.get(); 339cdf0e10cSrcweir } 340cdf0e10cSrcweir } 341cdf0e10cSrcweir 342cdf0e10cSrcweir // Return with result of this operation. 343cdf0e10cSrcweir return pReturn ; 344cdf0e10cSrcweir } 345cdf0e10cSrcweir 346cdf0e10cSrcweir } // extern "C" 347