Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <sal/config.h>
21 :
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 <comphelper/processfactory.hxx>
27 : #include <cppuhelper/factory.hxx>
28 : #include <cppuhelper/supportsservice.hxx>
29 : #include <sal/log.hxx>
30 :
31 : using namespace css::bridge;
32 : using namespace css::connection;
33 : using namespace css::container;
34 : using namespace css::lang;
35 : using namespace css::uno;
36 :
37 : namespace desktop
38 : {
39 :
40 115 : extern "C" void offacc_workerfunc (void * acc)
41 : {
42 115 : osl_setThreadName("URP Acceptor");
43 :
44 115 : static_cast<Acceptor*>(acc)->run();
45 115 : }
46 :
47 115 : Acceptor::Acceptor( const Reference< XComponentContext >& rxContext )
48 : : m_thread(NULL)
49 : , m_rContext(rxContext)
50 : , m_bInit(false)
51 115 : , m_bDying(false)
52 : {
53 115 : m_rAcceptor = css::connection::Acceptor::create(m_rContext);
54 115 : m_rBridgeFactory = BridgeFactory::create(m_rContext);
55 115 : }
56 :
57 :
58 345 : Acceptor::~Acceptor()
59 : {
60 115 : m_rAcceptor->stopAccepting();
61 : oslThread t;
62 : {
63 115 : osl::MutexGuard g(m_aMutex);
64 115 : t = m_thread;
65 : }
66 : //prevent locking if the thread is still waiting
67 115 : m_bDying = true;
68 115 : m_cEnable.set();
69 115 : osl_joinWithThread(t);
70 : {
71 : // Make the final state of m_bridges visible to this thread (since
72 : // m_thread is joined, the code that follows is the only one left
73 : // accessing m_bridges):
74 115 : osl::MutexGuard g(m_aMutex);
75 : }
76 : for (;;) {
77 177 : css::uno::Reference< css::bridge::XBridge > b(m_bridges.remove());
78 177 : if (!b.is()) {
79 115 : break;
80 : }
81 : css::uno::Reference< css::lang::XComponent >(
82 62 : b, css::uno::UNO_QUERY_THROW)->dispose();
83 62 : }
84 230 : }
85 :
86 115 : void Acceptor::run()
87 : {
88 : SAL_INFO( "desktop.offacc", "Acceptor::run" );
89 : for (;;)
90 : {
91 : try
92 : {
93 : // wait until we get enabled
94 : SAL_INFO( "desktop.offacc",
95 : "Acceptor::run waiting for office to come up");
96 178 : m_cEnable.wait();
97 178 : if (m_bDying) //see destructor
98 167 : break;
99 : SAL_INFO( "desktop.offacc",
100 : "Acceptor::run now enabled and continuing");
101 :
102 : // accept connection
103 126 : Reference< XConnection > rConnection = m_rAcceptor->accept( m_aConnectString );
104 : // if we return without a valid connection we mus assume that the acceptor
105 : // is destructed so we break out of the run method terminating the thread
106 126 : if (! rConnection.is()) break;
107 126 : OUString aDescription = rConnection->getDescription();
108 : SAL_INFO( "desktop.offacc", "Acceptor::run connection " << aDescription );
109 :
110 : // create instanceprovider for this connection
111 : Reference< XInstanceProvider > rInstanceProvider(
112 126 : new AccInstanceProvider(m_rContext, rConnection));
113 : // create the bridge. The remote end will have a reference to this bridge
114 : // thus preventing the bridge from being disposed. When the remote end releases
115 : // the bridge, it will be destructed.
116 63 : Reference< XBridge > rBridge = m_rBridgeFactory->createBridge(
117 126 : "", m_aProtocol, rConnection, rInstanceProvider);
118 126 : osl::MutexGuard g(m_aMutex);
119 126 : m_bridges.add(rBridge);
120 0 : } catch (const Exception& e) {
121 : SAL_WARN("desktop.offacc", "caught Exception \"" << e.Message << "\"");
122 : // connection failed...
123 : // something went wrong during connection setup.
124 : // just wait for a new connection to accept
125 : }
126 63 : }
127 115 : }
128 :
129 : // XInitialize
130 178 : void Acceptor::initialize( const Sequence<Any>& aArguments )
131 : throw( Exception, std::exception )
132 : {
133 : // prevent multiple initialization
134 178 : osl::ClearableMutexGuard aGuard( m_aMutex );
135 : SAL_INFO( "desktop.offacc", "Acceptor::initialize()" );
136 :
137 178 : bool bOk = false;
138 :
139 : // arg count
140 178 : int nArgs = aArguments.getLength();
141 :
142 : // not yet initialized and acceptstring
143 178 : if (!m_bInit && nArgs > 0 && (aArguments[0] >>= m_aAcceptString))
144 : {
145 : SAL_INFO( "desktop.offacc", "Acceptor::initialize string=" << m_aAcceptString );
146 :
147 : // get connect string and protocol from accept string
148 : // "<connectString>;<protocol>"
149 115 : sal_Int32 nIndex1 = m_aAcceptString.indexOf( ';' );
150 115 : if (nIndex1 < 0)
151 : throw IllegalArgumentException(
152 0 : "Invalid accept-string format", m_rContext, 1);
153 115 : m_aConnectString = m_aAcceptString.copy( 0 , nIndex1 ).trim();
154 115 : nIndex1++;
155 115 : sal_Int32 nIndex2 = m_aAcceptString.indexOf( ';' , nIndex1 );
156 115 : if (nIndex2 < 0) nIndex2 = m_aAcceptString.getLength();
157 115 : m_aProtocol = m_aAcceptString.copy( nIndex1, nIndex2 - nIndex1 );
158 :
159 : // start accepting in new thread...
160 115 : m_thread = osl_createThread(offacc_workerfunc, this);
161 115 : m_bInit = true;
162 115 : bOk = true;
163 : }
164 :
165 : // do we want to enable accepting?
166 178 : bool bEnable = false;
167 241 : if (((nArgs == 1 && (aArguments[0] >>= bEnable)) ||
168 293 : (nArgs == 2 && (aArguments[1] >>= bEnable))) &&
169 : bEnable )
170 : {
171 63 : m_cEnable.set();
172 63 : bOk = true;
173 : }
174 :
175 178 : if (!bOk)
176 : {
177 0 : throw IllegalArgumentException( "invalid initialization", m_rContext, 1);
178 178 : }
179 178 : }
180 :
181 : // XServiceInfo
182 230 : OUString Acceptor::impl_getImplementationName()
183 : {
184 230 : return OUString("com.sun.star.office.comp.Acceptor");
185 : }
186 0 : OUString Acceptor::getImplementationName()
187 : throw (RuntimeException, std::exception)
188 : {
189 0 : return Acceptor::impl_getImplementationName();
190 : }
191 115 : Sequence<OUString> Acceptor::impl_getSupportedServiceNames()
192 : {
193 115 : Sequence<OUString> aSequence(1);
194 115 : aSequence[0] = "com.sun.star.office.Acceptor";
195 115 : return aSequence;
196 : }
197 0 : Sequence<OUString> Acceptor::getSupportedServiceNames()
198 : throw (RuntimeException, std::exception)
199 : {
200 0 : return Acceptor::impl_getSupportedServiceNames();
201 : }
202 :
203 0 : sal_Bool Acceptor::supportsService(OUString const & ServiceName)
204 : throw (css::uno::RuntimeException, std::exception)
205 : {
206 0 : return cppu::supportsService(this, ServiceName);
207 : }
208 :
209 : // Factory
210 115 : Reference< XInterface > Acceptor::impl_getInstance( const Reference< XMultiServiceFactory >& aFactory )
211 : {
212 : try {
213 : return static_cast<cppu::OWeakObject *>(
214 115 : new Acceptor(comphelper::getComponentContext(aFactory)));
215 0 : } catch ( const Exception& ) {
216 0 : return css::uno::Reference<css::uno::XInterface>();
217 : }
218 : }
219 :
220 : // InstanceProvider
221 63 : AccInstanceProvider::AccInstanceProvider(const Reference<XComponentContext>& rxContext, const Reference<XConnection>& rConnection)
222 : {
223 63 : m_rContext = rxContext;
224 63 : m_rConnection = rConnection;
225 63 : }
226 :
227 122 : AccInstanceProvider::~AccInstanceProvider()
228 : {
229 122 : }
230 :
231 95 : Reference<XInterface> AccInstanceProvider::getInstance (const OUString& aName )
232 : throw ( NoSuchElementException, std::exception )
233 : {
234 :
235 95 : Reference<XInterface> rInstance;
236 :
237 95 : if ( aName == "StarOffice.ServiceManager" )
238 : {
239 32 : rInstance = Reference< XInterface >( m_rContext->getServiceManager() );
240 : }
241 63 : else if ( aName == "StarOffice.ComponentContext" )
242 : {
243 63 : rInstance = m_rContext;
244 : }
245 0 : else if ( aName == "StarOffice.NamingService" )
246 : {
247 : Reference< XNamingService > rNamingService(
248 0 : m_rContext->getServiceManager()->createInstanceWithContext("com.sun.star.uno.NamingService", m_rContext),
249 0 : UNO_QUERY );
250 0 : if ( rNamingService.is() )
251 : {
252 0 : rNamingService->registerObject(
253 0 : OUString("StarOffice.ServiceManager" ), m_rContext->getServiceManager() );
254 0 : rNamingService->registerObject(
255 0 : OUString("StarOffice.ComponentContext" ), m_rContext );
256 0 : rInstance = rNamingService;
257 0 : }
258 : }
259 95 : return rInstance;
260 : }
261 :
262 : }
263 :
264 : // component management stuff...
265 :
266 : extern "C"
267 : {
268 : using namespace desktop;
269 :
270 115 : SAL_DLLPUBLIC_EXPORT void * SAL_CALL offacc_component_getFactory(char const *pImplementationName, void *pServiceManager, void *)
271 : {
272 115 : void* pReturn = NULL ;
273 115 : if ( pImplementationName && pServiceManager )
274 : {
275 : // Define variables which are used in following macros.
276 115 : Reference< XSingleServiceFactory > xFactory;
277 : Reference< XMultiServiceFactory > xServiceManager(
278 230 : static_cast< XMultiServiceFactory* >(pServiceManager));
279 :
280 115 : if (desktop::Acceptor::impl_getImplementationName().equalsAscii( pImplementationName ) )
281 : {
282 230 : xFactory = Reference< XSingleServiceFactory >( cppu::createSingleFactory(
283 : xServiceManager, desktop::Acceptor::impl_getImplementationName(),
284 115 : desktop::Acceptor::impl_getInstance, desktop::Acceptor::impl_getSupportedServiceNames()) );
285 : }
286 :
287 : // Factory is valid - service was found.
288 115 : if ( xFactory.is() )
289 : {
290 115 : xFactory->acquire();
291 115 : pReturn = xFactory.get();
292 115 : }
293 : }
294 :
295 : // Return with result of this operation.
296 115 : return pReturn ;
297 : }
298 :
299 : } // extern "C"
300 :
301 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|