1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include "acceptor.hxx"
23 #include <com/sun/star/bridge/BridgeFactory.hpp>
24 #include <com/sun/star/connection/Acceptor.hpp>
25 #include <com/sun/star/uno/XNamingService.hpp>
26 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
27 #include <comphelper/processfactory.hxx>
28 #include <cppuhelper/factory.hxx>
29 #include <cppuhelper/supportsservice.hxx>
30 #include <sal/log.hxx>
31 #include <tools/diagnose_ex.h>
33 using namespace css::bridge
;
34 using namespace css::connection
;
35 using namespace css::lang
;
36 using namespace css::uno
;
43 static void offacc_workerfunc (void * acc
)
45 osl_setThreadName("URP Acceptor");
47 static_cast<Acceptor
*>(acc
)->run();
52 Acceptor::Acceptor( const Reference
< XComponentContext
>& rxContext
)
54 , m_rContext(rxContext
)
58 m_rAcceptor
= css::connection::Acceptor::create(m_rContext
);
59 m_rBridgeFactory
= BridgeFactory::create(m_rContext
);
65 m_rAcceptor
->stopAccepting();
68 osl::MutexGuard
g(m_aMutex
);
71 //prevent locking if the thread is still waiting
74 osl_joinWithThread(t
);
77 // Make the final state of m_bridges visible to this thread (since
78 // m_thread is joined, the code that follows is the only one left
79 // accessing m_bridges):
80 osl::MutexGuard
g(m_aMutex
);
83 css::uno::Reference
< css::bridge::XBridge
> b(m_bridges
.remove());
87 css::uno::Reference
< css::lang::XComponent
>(
88 b
, css::uno::UNO_QUERY_THROW
)->dispose();
94 SAL_INFO( "desktop.offacc", "Acceptor::run" );
99 // wait until we get enabled
100 SAL_INFO( "desktop.offacc",
101 "Acceptor::run waiting for office to come up");
103 if (m_bDying
) //see destructor
105 SAL_INFO( "desktop.offacc",
106 "Acceptor::run now enabled and continuing");
109 Reference
< XConnection
> rConnection
= m_rAcceptor
->accept( m_aConnectString
);
110 // if we return without a valid connection we must assume that the acceptor
111 // is destructed so we break out of the run method terminating the thread
112 if (! rConnection
.is()) break;
113 OUString aDescription
= rConnection
->getDescription();
114 SAL_INFO( "desktop.offacc", "Acceptor::run connection " << aDescription
);
116 // create instanceprovider for this connection
117 Reference
< XInstanceProvider
> rInstanceProvider(new AccInstanceProvider(m_rContext
));
118 // create the bridge. The remote end will have a reference to this bridge
119 // thus preventing the bridge from being disposed. When the remote end releases
120 // the bridge, it will be destructed.
121 Reference
< XBridge
> rBridge
= m_rBridgeFactory
->createBridge(
122 "", m_aProtocol
, rConnection
, rInstanceProvider
);
123 osl::MutexGuard
g(m_aMutex
);
124 m_bridges
.add(rBridge
);
125 } catch (const Exception
&) {
126 TOOLS_WARN_EXCEPTION("desktop.offacc", "");
127 // connection failed...
128 // something went wrong during connection setup.
129 // just wait for a new connection to accept
135 void Acceptor::initialize( const Sequence
<Any
>& aArguments
)
137 // prevent multiple initialization
138 osl::MutexGuard
aGuard( m_aMutex
);
139 SAL_INFO( "desktop.offacc", "Acceptor::initialize()" );
144 int nArgs
= aArguments
.getLength();
146 // not yet initialized and accept-string
147 if (!m_bInit
&& nArgs
> 0 && (aArguments
[0] >>= m_aAcceptString
))
149 SAL_INFO( "desktop.offacc", "Acceptor::initialize string=" << m_aAcceptString
);
151 // get connect string and protocol from accept string
152 // "<connectString>;<protocol>"
153 sal_Int32 nIndex1
= m_aAcceptString
.indexOf( ';' );
155 throw IllegalArgumentException(
156 "Invalid accept-string format", m_rContext
, 1);
157 m_aConnectString
= m_aAcceptString
.copy( 0 , nIndex1
).trim();
159 sal_Int32 nIndex2
= m_aAcceptString
.indexOf( ';' , nIndex1
);
160 if (nIndex2
< 0) nIndex2
= m_aAcceptString
.getLength();
161 m_aProtocol
= m_aAcceptString
.copy( nIndex1
, nIndex2
- nIndex1
);
163 // start accepting in new thread...
164 m_thread
= osl_createThread(offacc_workerfunc
, this);
169 // do we want to enable accepting?
170 bool bEnable
= false;
171 if (((nArgs
== 1 && (aArguments
[0] >>= bEnable
)) ||
172 (nArgs
== 2 && (aArguments
[1] >>= bEnable
))) &&
181 throw IllegalArgumentException( "invalid initialization", m_rContext
, 1);
186 OUString
Acceptor::impl_getImplementationName()
188 return "com.sun.star.office.comp.Acceptor";
190 OUString
Acceptor::getImplementationName()
192 return Acceptor::impl_getImplementationName();
194 Sequence
<OUString
> Acceptor::impl_getSupportedServiceNames()
196 return { "com.sun.star.office.Acceptor" };
198 Sequence
<OUString
> Acceptor::getSupportedServiceNames()
200 return Acceptor::impl_getSupportedServiceNames();
203 sal_Bool
Acceptor::supportsService(OUString
const & ServiceName
)
205 return cppu::supportsService(this, ServiceName
);
209 Reference
< XInterface
> Acceptor::impl_getInstance( const Reference
< XMultiServiceFactory
>& aFactory
)
212 return static_cast<cppu::OWeakObject
*>(
213 new Acceptor(comphelper::getComponentContext(aFactory
)));
214 } catch ( const Exception
& ) {
215 return css::uno::Reference
<css::uno::XInterface
>();
220 AccInstanceProvider::AccInstanceProvider(const Reference
<XComponentContext
>& rxContext
)
222 m_rContext
= rxContext
;
225 AccInstanceProvider::~AccInstanceProvider()
229 Reference
<XInterface
> AccInstanceProvider::getInstance (const OUString
& aName
)
232 Reference
<XInterface
> rInstance
;
234 if ( aName
== "StarOffice.ServiceManager" )
236 rInstance
.set( m_rContext
->getServiceManager() );
238 else if ( aName
== "StarOffice.ComponentContext" )
240 rInstance
= m_rContext
;
242 else if ( aName
== "StarOffice.NamingService" )
244 Reference
< XNamingService
> rNamingService(
245 m_rContext
->getServiceManager()->createInstanceWithContext("com.sun.star.uno.NamingService", m_rContext
),
247 if ( rNamingService
.is() )
249 rNamingService
->registerObject( "StarOffice.ServiceManager", m_rContext
->getServiceManager() );
250 rNamingService
->registerObject( "StarOffice.ComponentContext", m_rContext
);
251 rInstance
= rNamingService
;
259 // component management stuff...
263 using namespace desktop
;
265 SAL_DLLPUBLIC_EXPORT
void * offacc_component_getFactory(char const *pImplementationName
, void *pServiceManager
, void *)
267 void* pReturn
= nullptr ;
268 if ( pImplementationName
&& pServiceManager
)
270 // Define variables which are used in following macros.
271 Reference
< XSingleServiceFactory
> xFactory
;
272 Reference
< XMultiServiceFactory
> xServiceManager(
273 static_cast< XMultiServiceFactory
* >(pServiceManager
));
275 if (desktop::Acceptor::impl_getImplementationName().equalsAscii( pImplementationName
) )
277 xFactory
.set( cppu::createSingleFactory(
278 xServiceManager
, desktop::Acceptor::impl_getImplementationName(),
279 desktop::Acceptor::impl_getInstance
, desktop::Acceptor::impl_getSupportedServiceNames()) );
282 // Factory is valid - service was found.
286 pReturn
= xFactory
.get();
290 // Return with result of this operation.
296 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */