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