12722ceddSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
32722ceddSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
42722ceddSAndrew Rist * or more contributor license agreements. See the NOTICE file
52722ceddSAndrew Rist * distributed with this work for additional information
62722ceddSAndrew Rist * regarding copyright ownership. The ASF licenses this file
72722ceddSAndrew Rist * to you under the Apache License, Version 2.0 (the
82722ceddSAndrew Rist * "License"); you may not use this file except in compliance
92722ceddSAndrew Rist * with the License. You may obtain a copy of the License at
102722ceddSAndrew Rist *
112722ceddSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
122722ceddSAndrew Rist *
132722ceddSAndrew Rist * Unless required by applicable law or agreed to in writing,
142722ceddSAndrew Rist * software distributed under the License is distributed on an
152722ceddSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
162722ceddSAndrew Rist * KIND, either express or implied. See the License for the
172722ceddSAndrew Rist * specific language governing permissions and limitations
182722ceddSAndrew Rist * under the License.
192722ceddSAndrew Rist *
202722ceddSAndrew Rist *************************************************************/
212722ceddSAndrew Rist
222722ceddSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_desktop.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include "acceptor.hxx"
28cdf0e10cSrcweir #include <unotools/bootstrap.hxx>
29cdf0e10cSrcweir #include <vos/process.hxx>
30cdf0e10cSrcweir #include <tools/urlobj.hxx>
31cdf0e10cSrcweir #include <tools/stream.hxx>
32cdf0e10cSrcweir #include <vcl/svapp.hxx>
33cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
34cdf0e10cSrcweir #ifndef _COM_SUN_STAR_UNO_XNAMEINGSERVICE_HPP_
35cdf0e10cSrcweir #include <com/sun/star/uno/XNamingService.hpp>
36cdf0e10cSrcweir #endif
37cdf0e10cSrcweir
38cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
39cdf0e10cSrcweir
40cdf0e10cSrcweir namespace desktop
41cdf0e10cSrcweir {
42cdf0e10cSrcweir
workerfunc(void * acc)43cdf0e10cSrcweir extern "C" void workerfunc (void * acc)
44cdf0e10cSrcweir {
45cdf0e10cSrcweir ((Acceptor*)acc)->run();
46cdf0e10cSrcweir }
47cdf0e10cSrcweir
getComponentContext(const Reference<XMultiServiceFactory> & rFactory)48cdf0e10cSrcweir static Reference<XInterface> getComponentContext( const Reference<XMultiServiceFactory>& rFactory)
49cdf0e10cSrcweir {
50cdf0e10cSrcweir Reference<XInterface> rContext;
51cdf0e10cSrcweir Reference< XPropertySet > rPropSet( rFactory, UNO_QUERY );
52cdf0e10cSrcweir Any a = rPropSet->getPropertyValue(
53cdf0e10cSrcweir ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ) ) );
54cdf0e10cSrcweir a >>= rContext;
55cdf0e10cSrcweir return rContext;
56cdf0e10cSrcweir }
57cdf0e10cSrcweir
58cdf0e10cSrcweir Mutex Acceptor::m_aMutex;
59cdf0e10cSrcweir
Acceptor(const Reference<XMultiServiceFactory> & rFactory)60cdf0e10cSrcweir Acceptor::Acceptor( const Reference< XMultiServiceFactory >& rFactory )
61cdf0e10cSrcweir : m_thread(NULL)
62cdf0e10cSrcweir , m_aAcceptString()
63cdf0e10cSrcweir , m_aConnectString()
64cdf0e10cSrcweir , m_aProtocol()
65cdf0e10cSrcweir , m_bInit(sal_False)
66cdf0e10cSrcweir , m_bDying(false)
67cdf0e10cSrcweir {
68cdf0e10cSrcweir m_rSMgr = rFactory;
69cdf0e10cSrcweir m_rAcceptor = Reference< XAcceptor > (m_rSMgr->createInstance(
70cdf0e10cSrcweir rtl::OUString::createFromAscii( "com.sun.star.connection.Acceptor" )),
71cdf0e10cSrcweir UNO_QUERY );
72cdf0e10cSrcweir m_rBridgeFactory = Reference < XBridgeFactory > (m_rSMgr->createInstance(
73cdf0e10cSrcweir rtl::OUString::createFromAscii( "com.sun.star.bridge.BridgeFactory" )),
74cdf0e10cSrcweir UNO_QUERY );
75cdf0e10cSrcweir // get component context
76cdf0e10cSrcweir m_rContext = getComponentContext(m_rSMgr);
77cdf0e10cSrcweir }
78cdf0e10cSrcweir
79cdf0e10cSrcweir
~Acceptor()80cdf0e10cSrcweir Acceptor::~Acceptor()
81cdf0e10cSrcweir {
82cdf0e10cSrcweir m_rAcceptor->stopAccepting();
83cdf0e10cSrcweir oslThread t;
84cdf0e10cSrcweir {
85cdf0e10cSrcweir osl::MutexGuard g(m_aMutex);
86cdf0e10cSrcweir t = m_thread;
87cdf0e10cSrcweir }
88cdf0e10cSrcweir //prevent locking if the thread is still waiting
89cdf0e10cSrcweir m_bDying = true;
90cdf0e10cSrcweir m_cEnable.set();
91cdf0e10cSrcweir osl_joinWithThread(t);
92*55f5063aSJim Jagielski // osl_destroyThread(t);
93cdf0e10cSrcweir {
94cdf0e10cSrcweir // Make the final state of m_bridges visible to this thread (since
95cdf0e10cSrcweir // m_thread is joined, the code that follows is the only one left
96cdf0e10cSrcweir // accessing m_bridges):
97cdf0e10cSrcweir osl::MutexGuard g(m_aMutex);
98cdf0e10cSrcweir }
99cdf0e10cSrcweir for (;;) {
100cdf0e10cSrcweir com::sun::star::uno::Reference< com::sun::star::bridge::XBridge > b(
101cdf0e10cSrcweir m_bridges.remove());
102cdf0e10cSrcweir if (!b.is()) {
103cdf0e10cSrcweir break;
104cdf0e10cSrcweir }
105cdf0e10cSrcweir com::sun::star::uno::Reference< com::sun::star::lang::XComponent >(
106cdf0e10cSrcweir b, com::sun::star::uno::UNO_QUERY_THROW)->dispose();
107cdf0e10cSrcweir }
108cdf0e10cSrcweir }
109cdf0e10cSrcweir
run()110cdf0e10cSrcweir void SAL_CALL Acceptor::run()
111cdf0e10cSrcweir {
112cdf0e10cSrcweir while ( m_rAcceptor.is() && m_rBridgeFactory.is() )
113cdf0e10cSrcweir {
114cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "desktop (lo119109) Acceptor::run" );
115cdf0e10cSrcweir try
116cdf0e10cSrcweir {
117cdf0e10cSrcweir // wait until we get enabled
118cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_TRACE( aLog, "desktop (lo119109)"\
119cdf0e10cSrcweir "Acceptor::run waiting for office to come up");
120cdf0e10cSrcweir m_cEnable.wait();
121cdf0e10cSrcweir if (m_bDying) //see destructor
122cdf0e10cSrcweir break;
123cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_TRACE( aLog, "desktop (lo119109)"\
124cdf0e10cSrcweir "Acceptor::run now enabled and continuing");
125cdf0e10cSrcweir
126cdf0e10cSrcweir // accept connection
127cdf0e10cSrcweir Reference< XConnection > rConnection = m_rAcceptor->accept( m_aConnectString );
128796b7e2aSmseidel // if we return without a valid connection we must assume that the acceptor
129cdf0e10cSrcweir // is destructed so we break out of the run method terminating the thread
130cdf0e10cSrcweir if (! rConnection.is()) break;
131cdf0e10cSrcweir OUString aDescription = rConnection->getDescription();
132cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_TRACE1( aLog, "desktop (lo119109) Acceptor::run connection %s",
133cdf0e10cSrcweir OUStringToOString(aDescription, RTL_TEXTENCODING_ASCII_US).getStr());
134cdf0e10cSrcweir
135cdf0e10cSrcweir // create instanceprovider for this connection
136cdf0e10cSrcweir Reference< XInstanceProvider > rInstanceProvider(
137cdf0e10cSrcweir (XInstanceProvider*)new AccInstanceProvider(m_rSMgr, rConnection));
138cdf0e10cSrcweir // create the bridge. The remote end will have a reference to this bridge
139cdf0e10cSrcweir // thus preventing the bridge from being disposed. When the remote end releases
140cdf0e10cSrcweir // the bridge, it will be destructed.
141cdf0e10cSrcweir Reference< XBridge > rBridge = m_rBridgeFactory->createBridge(
142cdf0e10cSrcweir rtl::OUString() ,m_aProtocol ,rConnection ,rInstanceProvider );
143cdf0e10cSrcweir osl::MutexGuard g(m_aMutex);
144cdf0e10cSrcweir m_bridges.add(rBridge);
145cdf0e10cSrcweir } catch (Exception&) {
146cdf0e10cSrcweir // connection failed...
147cdf0e10cSrcweir // something went wrong during connection setup.
148cdf0e10cSrcweir // just wait for a new connection to accept
149cdf0e10cSrcweir }
150cdf0e10cSrcweir }
151cdf0e10cSrcweir }
152cdf0e10cSrcweir
153cdf0e10cSrcweir // XInitialize
initialize(const Sequence<Any> & aArguments)154cdf0e10cSrcweir void SAL_CALL Acceptor::initialize( const Sequence<Any>& aArguments )
155cdf0e10cSrcweir throw( Exception )
156cdf0e10cSrcweir {
157cdf0e10cSrcweir // prevent multiple initialization
158cdf0e10cSrcweir ClearableMutexGuard aGuard( m_aMutex );
159cdf0e10cSrcweir RTL_LOGFILE_CONTEXT( aLog, "destop (lo119109) Acceptor::initialize()" );
160cdf0e10cSrcweir
161cdf0e10cSrcweir sal_Bool bOk = sal_False;
162cdf0e10cSrcweir
163cdf0e10cSrcweir // arg count
164cdf0e10cSrcweir int nArgs = aArguments.getLength();
165cdf0e10cSrcweir
166cdf0e10cSrcweir // not yet initialized and acceptstring
167cdf0e10cSrcweir if (!m_bInit && nArgs > 0 && (aArguments[0] >>= m_aAcceptString))
168cdf0e10cSrcweir {
169cdf0e10cSrcweir RTL_LOGFILE_CONTEXT_TRACE1( aLog, "desktop (lo119109) Acceptor::initialize string=%s",
170cdf0e10cSrcweir OUStringToOString(m_aAcceptString, RTL_TEXTENCODING_ASCII_US).getStr());
171cdf0e10cSrcweir
172cdf0e10cSrcweir // get connect string and protocol from accept string
173cdf0e10cSrcweir // "<connectString>;<protocol>"
174cdf0e10cSrcweir sal_Int32 nIndex1 = m_aAcceptString.indexOf( (sal_Unicode) ';' );
175cdf0e10cSrcweir if (nIndex1 < 0) throw IllegalArgumentException(
176cdf0e10cSrcweir OUString::createFromAscii("Invalid accept-string format"), m_rContext, 1);
177cdf0e10cSrcweir m_aConnectString = m_aAcceptString.copy( 0 , nIndex1 ).trim();
178cdf0e10cSrcweir nIndex1++;
179cdf0e10cSrcweir sal_Int32 nIndex2 = m_aAcceptString.indexOf( (sal_Unicode) ';' , nIndex1 );
180cdf0e10cSrcweir if (nIndex2 < 0) nIndex2 = m_aAcceptString.getLength();
181cdf0e10cSrcweir m_aProtocol = m_aAcceptString.copy( nIndex1, nIndex2 - nIndex1 );
182cdf0e10cSrcweir
183cdf0e10cSrcweir // start accepting in new thread...
184cdf0e10cSrcweir m_thread = osl_createThread(workerfunc, this);
185cdf0e10cSrcweir m_bInit = sal_True;
186cdf0e10cSrcweir bOk = sal_True;
187cdf0e10cSrcweir }
188cdf0e10cSrcweir
189cdf0e10cSrcweir // do we want to enable accepting?
190cdf0e10cSrcweir sal_Bool bEnable = sal_False;
191cdf0e10cSrcweir if (((nArgs == 1 && (aArguments[0] >>= bEnable)) ||
192cdf0e10cSrcweir (nArgs == 2 && (aArguments[1] >>= bEnable))) &&
193cdf0e10cSrcweir bEnable )
194cdf0e10cSrcweir {
195cdf0e10cSrcweir m_cEnable.set();
196cdf0e10cSrcweir bOk = sal_True;
197cdf0e10cSrcweir }
198cdf0e10cSrcweir
199cdf0e10cSrcweir if (!bOk)
200cdf0e10cSrcweir {
201cdf0e10cSrcweir throw IllegalArgumentException(
202cdf0e10cSrcweir OUString::createFromAscii("invalid initialization"), m_rContext, 1);
203cdf0e10cSrcweir }
204cdf0e10cSrcweir }
205cdf0e10cSrcweir
206cdf0e10cSrcweir // XServiceInfo
207cdf0e10cSrcweir const sal_Char *Acceptor::serviceName = "com.sun.star.office.Acceptor";
208cdf0e10cSrcweir const sal_Char *Acceptor::implementationName = "com.sun.star.office.comp.Acceptor";
209cdf0e10cSrcweir const sal_Char *Acceptor::supportedServiceNames[] = {"com.sun.star.office.Acceptor", NULL};
impl_getImplementationName()210cdf0e10cSrcweir OUString Acceptor::impl_getImplementationName()
211cdf0e10cSrcweir {
212cdf0e10cSrcweir return OUString::createFromAscii( implementationName );
213cdf0e10cSrcweir }
getImplementationName()214cdf0e10cSrcweir OUString SAL_CALL Acceptor::getImplementationName()
215cdf0e10cSrcweir throw (RuntimeException)
216cdf0e10cSrcweir {
217cdf0e10cSrcweir return Acceptor::impl_getImplementationName();
218cdf0e10cSrcweir }
impl_getSupportedServiceNames()219cdf0e10cSrcweir Sequence<OUString> Acceptor::impl_getSupportedServiceNames()
220cdf0e10cSrcweir {
221cdf0e10cSrcweir Sequence<OUString> aSequence;
222cdf0e10cSrcweir for (int i=0; supportedServiceNames[i]!=NULL; i++) {
223cdf0e10cSrcweir aSequence.realloc(i+1);
224cdf0e10cSrcweir aSequence[i]=(OUString::createFromAscii(supportedServiceNames[i]));
225cdf0e10cSrcweir }
226cdf0e10cSrcweir return aSequence;
227cdf0e10cSrcweir }
getSupportedServiceNames()228cdf0e10cSrcweir Sequence<OUString> SAL_CALL Acceptor::getSupportedServiceNames()
229cdf0e10cSrcweir throw (RuntimeException)
230cdf0e10cSrcweir {
231cdf0e10cSrcweir return Acceptor::impl_getSupportedServiceNames();
232cdf0e10cSrcweir }
supportsService(const OUString &)233cdf0e10cSrcweir sal_Bool SAL_CALL Acceptor::supportsService( const OUString&)
234cdf0e10cSrcweir throw (RuntimeException)
235cdf0e10cSrcweir {
236cdf0e10cSrcweir return sal_False;
237cdf0e10cSrcweir }
238cdf0e10cSrcweir
239cdf0e10cSrcweir // Factory
impl_getInstance(const Reference<XMultiServiceFactory> & aFactory)240cdf0e10cSrcweir Reference< XInterface > Acceptor::impl_getInstance( const Reference< XMultiServiceFactory >& aFactory )
241cdf0e10cSrcweir {
242cdf0e10cSrcweir try {
243cdf0e10cSrcweir return (XComponent*) new Acceptor( aFactory );
244cdf0e10cSrcweir } catch ( Exception& ) {
245cdf0e10cSrcweir return (XComponent*) NULL;
246cdf0e10cSrcweir }
247cdf0e10cSrcweir }
248cdf0e10cSrcweir
249cdf0e10cSrcweir // InstanceProvider
AccInstanceProvider(const Reference<XMultiServiceFactory> & aFactory,const Reference<XConnection> & rConnection)250cdf0e10cSrcweir AccInstanceProvider::AccInstanceProvider(const Reference<XMultiServiceFactory>& aFactory, const Reference<XConnection>& rConnection)
251cdf0e10cSrcweir {
252cdf0e10cSrcweir m_rSMgr = aFactory;
253cdf0e10cSrcweir m_rConnection = rConnection;
254cdf0e10cSrcweir }
255cdf0e10cSrcweir
~AccInstanceProvider()256cdf0e10cSrcweir AccInstanceProvider::~AccInstanceProvider()
257cdf0e10cSrcweir {
258cdf0e10cSrcweir }
259cdf0e10cSrcweir
getInstance(const OUString & aName)260cdf0e10cSrcweir Reference<XInterface> SAL_CALL AccInstanceProvider::getInstance (const OUString& aName )
261cdf0e10cSrcweir throw ( NoSuchElementException )
262cdf0e10cSrcweir {
263cdf0e10cSrcweir
264cdf0e10cSrcweir Reference<XInterface> rInstance;
265cdf0e10cSrcweir
266cdf0e10cSrcweir if ( aName.compareToAscii( "StarOffice.ServiceManager" ) == 0)
267cdf0e10cSrcweir {
268cdf0e10cSrcweir rInstance = Reference< XInterface >( m_rSMgr );
269cdf0e10cSrcweir }
270cdf0e10cSrcweir else if(aName.compareToAscii( "StarOffice.ComponentContext" ) == 0 )
271cdf0e10cSrcweir {
272cdf0e10cSrcweir rInstance = getComponentContext( m_rSMgr );
273cdf0e10cSrcweir }
274cdf0e10cSrcweir else if ( aName.compareToAscii("StarOffice.NamingService" ) == 0 )
275cdf0e10cSrcweir {
276cdf0e10cSrcweir Reference< XNamingService > rNamingService(
277cdf0e10cSrcweir m_rSMgr->createInstance( OUString::createFromAscii( "com.sun.star.uno.NamingService" )),
278cdf0e10cSrcweir UNO_QUERY );
279cdf0e10cSrcweir if ( rNamingService.is() )
280cdf0e10cSrcweir {
281cdf0e10cSrcweir rNamingService->registerObject(
282cdf0e10cSrcweir OUString::createFromAscii( "StarOffice.ServiceManager" ), m_rSMgr );
283cdf0e10cSrcweir rNamingService->registerObject(
284cdf0e10cSrcweir OUString::createFromAscii( "StarOffice.ComponentContext" ), getComponentContext( m_rSMgr ));
285cdf0e10cSrcweir rInstance = rNamingService;
286cdf0e10cSrcweir }
287cdf0e10cSrcweir }
288cdf0e10cSrcweir /*
289cdf0e10cSrcweir else if ( aName.compareToAscii("com.sun.star.ucb.RemoteContentProviderAcceptor" ))
290cdf0e10cSrcweir {
291cdf0e10cSrcweir Reference< XMultiServiceFactory > rSMgr = ::comphelper::getProcessServiceFactory();
292cdf0e10cSrcweir if ( rSMgr.is() ) {
293cdf0e10cSrcweir try {
294cdf0e10cSrcweir rInstance = rSMgr->createInstance( sObjectName );
295cdf0e10cSrcweir }
296cdf0e10cSrcweir catch (Exception const &) {}
297cdf0e10cSrcweir }
298cdf0e10cSrcweir }
299cdf0e10cSrcweir */
300cdf0e10cSrcweir return rInstance;
301cdf0e10cSrcweir }
302cdf0e10cSrcweir
303cdf0e10cSrcweir }
304cdf0e10cSrcweir
305cdf0e10cSrcweir // component management stuff...
306cdf0e10cSrcweir // ----------------------------------------------------------------------------
307cdf0e10cSrcweir extern "C"
308cdf0e10cSrcweir {
309cdf0e10cSrcweir using namespace desktop;
310cdf0e10cSrcweir
311cdf0e10cSrcweir void SAL_CALL
component_getImplementationEnvironment(const sal_Char ** ppEnvironmentTypeName,uno_Environment **)312cdf0e10cSrcweir component_getImplementationEnvironment(const sal_Char **ppEnvironmentTypeName, uno_Environment **)
313cdf0e10cSrcweir {
314cdf0e10cSrcweir *ppEnvironmentTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME ;
315cdf0e10cSrcweir }
316cdf0e10cSrcweir
317cdf0e10cSrcweir void * SAL_CALL
component_getFactory(const sal_Char * pImplementationName,void * pServiceManager,void *)318cdf0e10cSrcweir component_getFactory(const sal_Char *pImplementationName, void *pServiceManager, void *)
319cdf0e10cSrcweir {
320cdf0e10cSrcweir void* pReturn = NULL ;
321cdf0e10cSrcweir if ( pImplementationName && pServiceManager )
322cdf0e10cSrcweir {
323cdf0e10cSrcweir // Define variables which are used in following macros.
324cdf0e10cSrcweir Reference< XSingleServiceFactory > xFactory;
325cdf0e10cSrcweir Reference< XMultiServiceFactory > xServiceManager(
326cdf0e10cSrcweir reinterpret_cast< XMultiServiceFactory* >(pServiceManager));
327cdf0e10cSrcweir
328cdf0e10cSrcweir if (Acceptor::impl_getImplementationName().compareToAscii( pImplementationName ) == COMPARE_EQUAL )
329cdf0e10cSrcweir {
330cdf0e10cSrcweir xFactory = Reference< XSingleServiceFactory >( cppu::createSingleFactory(
331cdf0e10cSrcweir xServiceManager, Acceptor::impl_getImplementationName(),
332cdf0e10cSrcweir Acceptor::impl_getInstance, Acceptor::impl_getSupportedServiceNames()) );
333cdf0e10cSrcweir }
334cdf0e10cSrcweir
335cdf0e10cSrcweir // Factory is valid - service was found.
336cdf0e10cSrcweir if ( xFactory.is() )
337cdf0e10cSrcweir {
338cdf0e10cSrcweir xFactory->acquire();
339cdf0e10cSrcweir pReturn = xFactory.get();
340cdf0e10cSrcweir }
341cdf0e10cSrcweir }
342cdf0e10cSrcweir
343cdf0e10cSrcweir // Return with result of this operation.
344cdf0e10cSrcweir return pReturn ;
345cdf0e10cSrcweir }
346cdf0e10cSrcweir
347cdf0e10cSrcweir } // extern "C"
348