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