2010-10-12 15:53:47 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-11-15 17:28:16 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2003-03-25 12:52:54 +00:00
|
|
|
|
2014-06-04 10:55:43 +02:00
|
|
|
#include <sal/config.h>
|
2006-09-17 08:47:08 +00:00
|
|
|
|
2003-03-25 12:52:54 +00:00
|
|
|
#include "acceptor.hxx"
|
2012-09-13 13:30:07 +02:00
|
|
|
#include <com/sun/star/bridge/BridgeFactory.hpp>
|
2013-06-06 16:38:03 +02:00
|
|
|
#include <com/sun/star/connection/Acceptor.hpp>
|
2003-03-25 12:52:54 +00:00
|
|
|
#include <com/sun/star/uno/XNamingService.hpp>
|
2012-09-29 17:20:22 +02:00
|
|
|
#include <comphelper/processfactory.hxx>
|
2006-05-02 16:13:10 +00:00
|
|
|
#include <cppuhelper/factory.hxx>
|
2013-02-16 12:20:44 +01:00
|
|
|
#include <cppuhelper/supportsservice.hxx>
|
2015-04-22 09:42:28 +02:00
|
|
|
#include <sal/log.hxx>
|
2003-03-25 12:52:54 +00:00
|
|
|
|
2013-04-28 10:36:40 +02:00
|
|
|
using namespace css::bridge;
|
|
|
|
using namespace css::connection;
|
|
|
|
using namespace css::container;
|
|
|
|
using namespace css::lang;
|
|
|
|
using namespace css::uno;
|
|
|
|
|
2003-03-25 12:52:54 +00:00
|
|
|
namespace desktop
|
|
|
|
{
|
|
|
|
|
2013-04-15 14:46:15 -04:00
|
|
|
extern "C" void offacc_workerfunc (void * acc)
|
2003-03-25 12:52:54 +00:00
|
|
|
{
|
2014-08-22 23:51:08 +02:00
|
|
|
osl_setThreadName("URP Acceptor");
|
|
|
|
|
2015-03-28 19:00:39 +01:00
|
|
|
static_cast<Acceptor*>(acc)->run();
|
2003-03-25 12:52:54 +00:00
|
|
|
}
|
|
|
|
|
2013-06-04 08:45:46 +02:00
|
|
|
Acceptor::Acceptor( const Reference< XComponentContext >& rxContext )
|
2007-10-15 11:59:58 +00:00
|
|
|
: m_thread(NULL)
|
2013-06-04 08:45:46 +02:00
|
|
|
, m_rContext(rxContext)
|
2013-08-22 11:03:55 +02:00
|
|
|
, m_bInit(false)
|
2010-05-17 16:42:05 +02:00
|
|
|
, m_bDying(false)
|
2003-03-25 12:52:54 +00:00
|
|
|
{
|
2013-06-06 16:38:03 +02:00
|
|
|
m_rAcceptor = css::connection::Acceptor::create(m_rContext);
|
2012-09-13 13:30:07 +02:00
|
|
|
m_rBridgeFactory = BridgeFactory::create(m_rContext);
|
2003-03-25 12:52:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Acceptor::~Acceptor()
|
|
|
|
{
|
|
|
|
m_rAcceptor->stopAccepting();
|
2007-10-15 11:59:58 +00:00
|
|
|
oslThread t;
|
|
|
|
{
|
|
|
|
osl::MutexGuard g(m_aMutex);
|
|
|
|
t = m_thread;
|
|
|
|
}
|
2010-05-17 16:42:05 +02:00
|
|
|
//prevent locking if the thread is still waiting
|
|
|
|
m_bDying = true;
|
|
|
|
m_cEnable.set();
|
2007-10-15 11:59:58 +00:00
|
|
|
osl_joinWithThread(t);
|
|
|
|
{
|
|
|
|
// Make the final state of m_bridges visible to this thread (since
|
|
|
|
// m_thread is joined, the code that follows is the only one left
|
|
|
|
// accessing m_bridges):
|
|
|
|
osl::MutexGuard g(m_aMutex);
|
|
|
|
}
|
|
|
|
for (;;) {
|
2013-08-22 11:03:55 +02:00
|
|
|
css::uno::Reference< css::bridge::XBridge > b(m_bridges.remove());
|
2007-10-15 11:59:58 +00:00
|
|
|
if (!b.is()) {
|
|
|
|
break;
|
|
|
|
}
|
2013-08-22 11:03:55 +02:00
|
|
|
css::uno::Reference< css::lang::XComponent >(
|
|
|
|
b, css::uno::UNO_QUERY_THROW)->dispose();
|
2007-10-15 11:59:58 +00:00
|
|
|
}
|
2003-03-25 12:52:54 +00:00
|
|
|
}
|
|
|
|
|
2013-08-22 11:03:55 +02:00
|
|
|
void Acceptor::run()
|
2003-03-25 12:52:54 +00:00
|
|
|
{
|
2013-08-22 11:03:55 +02:00
|
|
|
SAL_INFO( "desktop.offacc", "Acceptor::run" );
|
|
|
|
for (;;)
|
2003-03-25 12:52:54 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// wait until we get enabled
|
2013-08-22 11:03:55 +02:00
|
|
|
SAL_INFO( "desktop.offacc",
|
2003-03-25 12:52:54 +00:00
|
|
|
"Acceptor::run waiting for office to come up");
|
|
|
|
m_cEnable.wait();
|
2010-05-17 16:42:05 +02:00
|
|
|
if (m_bDying) //see destructor
|
|
|
|
break;
|
2013-08-22 11:03:55 +02:00
|
|
|
SAL_INFO( "desktop.offacc",
|
2003-03-25 12:52:54 +00:00
|
|
|
"Acceptor::run now enabled and continuing");
|
|
|
|
|
|
|
|
// accept connection
|
|
|
|
Reference< XConnection > rConnection = m_rAcceptor->accept( m_aConnectString );
|
|
|
|
// if we return without a valid connection we mus assume that the acceptor
|
|
|
|
// is destructed so we break out of the run method terminating the thread
|
|
|
|
if (! rConnection.is()) break;
|
|
|
|
OUString aDescription = rConnection->getDescription();
|
2013-08-22 11:03:55 +02:00
|
|
|
SAL_INFO( "desktop.offacc", "Acceptor::run connection " << aDescription );
|
2003-03-25 12:52:54 +00:00
|
|
|
|
|
|
|
// create instanceprovider for this connection
|
|
|
|
Reference< XInstanceProvider > rInstanceProvider(
|
2013-08-22 11:03:55 +02:00
|
|
|
new AccInstanceProvider(m_rContext, rConnection));
|
2003-03-25 12:52:54 +00:00
|
|
|
// create the bridge. The remote end will have a reference to this bridge
|
|
|
|
// thus preventing the bridge from being disposed. When the remote end releases
|
|
|
|
// the bridge, it will be destructed.
|
|
|
|
Reference< XBridge > rBridge = m_rBridgeFactory->createBridge(
|
2013-08-22 11:03:55 +02:00
|
|
|
"", m_aProtocol, rConnection, rInstanceProvider);
|
2007-10-15 11:59:58 +00:00
|
|
|
osl::MutexGuard g(m_aMutex);
|
|
|
|
m_bridges.add(rBridge);
|
2013-01-24 10:25:20 +01:00
|
|
|
} catch (const Exception& e) {
|
2013-05-27 00:22:20 +03:00
|
|
|
SAL_WARN("desktop.offacc", "caught Exception \"" << e.Message << "\"");
|
2003-03-25 12:52:54 +00:00
|
|
|
// connection failed...
|
|
|
|
// something went wrong during connection setup.
|
|
|
|
// just wait for a new connection to accept
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// XInitialize
|
2013-08-22 11:03:55 +02:00
|
|
|
void Acceptor::initialize( const Sequence<Any>& aArguments )
|
2014-02-25 21:31:58 +01:00
|
|
|
throw( Exception, std::exception )
|
2003-03-25 12:52:54 +00:00
|
|
|
{
|
|
|
|
// prevent multiple initialization
|
2013-08-22 11:03:55 +02:00
|
|
|
osl::ClearableMutexGuard aGuard( m_aMutex );
|
|
|
|
SAL_INFO( "desktop.offacc", "Acceptor::initialize()" );
|
2003-03-25 12:52:54 +00:00
|
|
|
|
2013-08-22 11:03:55 +02:00
|
|
|
bool bOk = false;
|
2003-03-25 12:52:54 +00:00
|
|
|
|
|
|
|
// arg count
|
|
|
|
int nArgs = aArguments.getLength();
|
|
|
|
|
2015-06-15 12:17:57 +02:00
|
|
|
// not yet initialized and accept-string
|
2003-03-25 12:52:54 +00:00
|
|
|
if (!m_bInit && nArgs > 0 && (aArguments[0] >>= m_aAcceptString))
|
|
|
|
{
|
2013-08-22 11:03:55 +02:00
|
|
|
SAL_INFO( "desktop.offacc", "Acceptor::initialize string=" << m_aAcceptString );
|
2003-03-25 12:52:54 +00:00
|
|
|
|
|
|
|
// get connect string and protocol from accept string
|
|
|
|
// "<connectString>;<protocol>"
|
2013-08-22 11:03:55 +02:00
|
|
|
sal_Int32 nIndex1 = m_aAcceptString.indexOf( ';' );
|
2014-05-28 12:50:02 +02:00
|
|
|
if (nIndex1 < 0)
|
|
|
|
throw IllegalArgumentException(
|
|
|
|
"Invalid accept-string format", m_rContext, 1);
|
2003-03-25 12:52:54 +00:00
|
|
|
m_aConnectString = m_aAcceptString.copy( 0 , nIndex1 ).trim();
|
|
|
|
nIndex1++;
|
2013-08-22 11:03:55 +02:00
|
|
|
sal_Int32 nIndex2 = m_aAcceptString.indexOf( ';' , nIndex1 );
|
2003-03-25 12:52:54 +00:00
|
|
|
if (nIndex2 < 0) nIndex2 = m_aAcceptString.getLength();
|
|
|
|
m_aProtocol = m_aAcceptString.copy( nIndex1, nIndex2 - nIndex1 );
|
|
|
|
|
|
|
|
// start accepting in new thread...
|
2013-04-15 14:46:15 -04:00
|
|
|
m_thread = osl_createThread(offacc_workerfunc, this);
|
2013-08-22 11:03:55 +02:00
|
|
|
m_bInit = true;
|
|
|
|
bOk = true;
|
2003-03-25 12:52:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// do we want to enable accepting?
|
2013-08-22 11:03:55 +02:00
|
|
|
bool bEnable = false;
|
2009-01-21 15:28:14 +00:00
|
|
|
if (((nArgs == 1 && (aArguments[0] >>= bEnable)) ||
|
|
|
|
(nArgs == 2 && (aArguments[1] >>= bEnable))) &&
|
|
|
|
bEnable )
|
2003-03-25 12:52:54 +00:00
|
|
|
{
|
|
|
|
m_cEnable.set();
|
2013-08-22 11:03:55 +02:00
|
|
|
bOk = true;
|
2003-03-25 12:52:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!bOk)
|
|
|
|
{
|
2014-05-28 12:50:02 +02:00
|
|
|
throw IllegalArgumentException( "invalid initialization", m_rContext, 1);
|
2003-03-25 12:52:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// XServiceInfo
|
|
|
|
OUString Acceptor::impl_getImplementationName()
|
|
|
|
{
|
2013-08-22 11:03:55 +02:00
|
|
|
return OUString("com.sun.star.office.comp.Acceptor");
|
2003-03-25 12:52:54 +00:00
|
|
|
}
|
2013-08-22 11:03:55 +02:00
|
|
|
OUString Acceptor::getImplementationName()
|
2014-02-25 21:31:58 +01:00
|
|
|
throw (RuntimeException, std::exception)
|
2003-03-25 12:52:54 +00:00
|
|
|
{
|
|
|
|
return Acceptor::impl_getImplementationName();
|
|
|
|
}
|
|
|
|
Sequence<OUString> Acceptor::impl_getSupportedServiceNames()
|
|
|
|
{
|
2013-08-22 11:03:55 +02:00
|
|
|
Sequence<OUString> aSequence(1);
|
|
|
|
aSequence[0] = "com.sun.star.office.Acceptor";
|
2003-03-25 12:52:54 +00:00
|
|
|
return aSequence;
|
|
|
|
}
|
2013-08-22 11:03:55 +02:00
|
|
|
Sequence<OUString> Acceptor::getSupportedServiceNames()
|
2014-02-25 21:31:58 +01:00
|
|
|
throw (RuntimeException, std::exception)
|
2003-03-25 12:52:54 +00:00
|
|
|
{
|
|
|
|
return Acceptor::impl_getSupportedServiceNames();
|
|
|
|
}
|
2013-02-16 12:20:44 +01:00
|
|
|
|
|
|
|
sal_Bool Acceptor::supportsService(OUString const & ServiceName)
|
2014-02-25 21:31:58 +01:00
|
|
|
throw (css::uno::RuntimeException, std::exception)
|
2003-03-25 12:52:54 +00:00
|
|
|
{
|
2013-02-16 12:20:44 +01:00
|
|
|
return cppu::supportsService(this, ServiceName);
|
2003-03-25 12:52:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Factory
|
|
|
|
Reference< XInterface > Acceptor::impl_getInstance( const Reference< XMultiServiceFactory >& aFactory )
|
|
|
|
{
|
|
|
|
try {
|
2015-01-17 18:23:42 +01:00
|
|
|
return static_cast<cppu::OWeakObject *>(
|
|
|
|
new Acceptor(comphelper::getComponentContext(aFactory)));
|
2011-12-06 00:19:44 +09:00
|
|
|
} catch ( const Exception& ) {
|
2015-01-17 18:23:42 +01:00
|
|
|
return css::uno::Reference<css::uno::XInterface>();
|
2003-03-25 12:52:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// InstanceProvider
|
2013-06-04 08:45:46 +02:00
|
|
|
AccInstanceProvider::AccInstanceProvider(const Reference<XComponentContext>& rxContext, const Reference<XConnection>& rConnection)
|
2003-03-25 12:52:54 +00:00
|
|
|
{
|
2013-06-04 08:45:46 +02:00
|
|
|
m_rContext = rxContext;
|
2003-03-25 12:52:54 +00:00
|
|
|
m_rConnection = rConnection;
|
|
|
|
}
|
|
|
|
|
|
|
|
AccInstanceProvider::~AccInstanceProvider()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-22 11:03:55 +02:00
|
|
|
Reference<XInterface> AccInstanceProvider::getInstance (const OUString& aName )
|
2014-02-25 21:31:58 +01:00
|
|
|
throw ( NoSuchElementException, std::exception )
|
2003-03-25 12:52:54 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
Reference<XInterface> rInstance;
|
|
|
|
|
2014-12-12 17:51:36 +01:00
|
|
|
if ( aName == "StarOffice.ServiceManager" )
|
2003-03-25 12:52:54 +00:00
|
|
|
{
|
2013-06-04 08:45:46 +02:00
|
|
|
rInstance = Reference< XInterface >( m_rContext->getServiceManager() );
|
2003-03-25 12:52:54 +00:00
|
|
|
}
|
2014-12-12 17:51:36 +01:00
|
|
|
else if ( aName == "StarOffice.ComponentContext" )
|
2003-03-25 12:52:54 +00:00
|
|
|
{
|
2013-06-04 08:45:46 +02:00
|
|
|
rInstance = m_rContext;
|
2003-03-25 12:52:54 +00:00
|
|
|
}
|
2014-12-12 17:51:36 +01:00
|
|
|
else if ( aName == "StarOffice.NamingService" )
|
2003-03-25 12:52:54 +00:00
|
|
|
{
|
|
|
|
Reference< XNamingService > rNamingService(
|
2013-06-04 08:45:46 +02:00
|
|
|
m_rContext->getServiceManager()->createInstanceWithContext("com.sun.star.uno.NamingService", m_rContext),
|
2003-03-25 12:52:54 +00:00
|
|
|
UNO_QUERY );
|
|
|
|
if ( rNamingService.is() )
|
|
|
|
{
|
|
|
|
rNamingService->registerObject(
|
2013-06-04 08:45:46 +02:00
|
|
|
OUString("StarOffice.ServiceManager" ), m_rContext->getServiceManager() );
|
2003-03-25 12:52:54 +00:00
|
|
|
rNamingService->registerObject(
|
2013-06-04 08:45:46 +02:00
|
|
|
OUString("StarOffice.ComponentContext" ), m_rContext );
|
2003-03-25 12:52:54 +00:00
|
|
|
rInstance = rNamingService;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// component management stuff...
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2003-03-25 12:52:54 +00:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
using namespace desktop;
|
|
|
|
|
2013-08-22 11:03:55 +02:00
|
|
|
SAL_DLLPUBLIC_EXPORT void * SAL_CALL offacc_component_getFactory(char const *pImplementationName, void *pServiceManager, void *)
|
2003-03-25 12:52:54 +00:00
|
|
|
{
|
|
|
|
void* pReturn = NULL ;
|
|
|
|
if ( pImplementationName && pServiceManager )
|
|
|
|
{
|
|
|
|
// Define variables which are used in following macros.
|
|
|
|
Reference< XSingleServiceFactory > xFactory;
|
|
|
|
Reference< XMultiServiceFactory > xServiceManager(
|
2015-03-31 13:13:34 +02:00
|
|
|
static_cast< XMultiServiceFactory* >(pServiceManager));
|
2003-03-25 12:52:54 +00:00
|
|
|
|
2013-06-06 16:38:03 +02:00
|
|
|
if (desktop::Acceptor::impl_getImplementationName().equalsAscii( pImplementationName ) )
|
2003-03-25 12:52:54 +00:00
|
|
|
{
|
|
|
|
xFactory = Reference< XSingleServiceFactory >( cppu::createSingleFactory(
|
2013-06-06 16:38:03 +02:00
|
|
|
xServiceManager, desktop::Acceptor::impl_getImplementationName(),
|
|
|
|
desktop::Acceptor::impl_getInstance, desktop::Acceptor::impl_getSupportedServiceNames()) );
|
2003-03-25 12:52:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Factory is valid - service was found.
|
|
|
|
if ( xFactory.is() )
|
|
|
|
{
|
|
|
|
xFactory->acquire();
|
|
|
|
pReturn = xFactory.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return with result of this operation.
|
|
|
|
return pReturn ;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // extern "C"
|
2010-10-12 15:53:47 +02:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|