Update ooo320-m1
[ooovba.git] / desktop / source / offacc / acceptor.cxx
blob8d6e97313350aa5fb096d641604b16ed516165cc
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: acceptor.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_desktop.hxx"
34 #include "acceptor.hxx"
35 #include <unotools/bootstrap.hxx>
36 #include <vos/process.hxx>
37 #include <tools/urlobj.hxx>
38 #include <tools/stream.hxx>
39 #include <vcl/svapp.hxx>
40 #include <com/sun/star/beans/XPropertySet.hpp>
41 #ifndef _COM_SUN_STAR_UNO_XNAMEINGSERVICE_HPP_
42 #include <com/sun/star/uno/XNamingService.hpp>
43 #endif
45 #include <cppuhelper/factory.hxx>
47 namespace desktop
50 extern "C" void workerfunc (void * acc)
52 ((Acceptor*)acc)->run();
55 static Reference<XInterface> getComponentContext( const Reference<XMultiServiceFactory>& rFactory)
57 Reference<XInterface> rContext;
58 Reference< XPropertySet > rPropSet( rFactory, UNO_QUERY );
59 Any a = rPropSet->getPropertyValue(
60 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ) ) );
61 a >>= rContext;
62 return rContext;
65 Mutex Acceptor::m_aMutex;
67 Acceptor::Acceptor( const Reference< XMultiServiceFactory >& rFactory )
68 : m_thread(NULL)
69 , m_aAcceptString()
70 , m_aConnectString()
71 , m_aProtocol()
72 , m_bInit(sal_False)
74 m_rSMgr = rFactory;
75 m_rAcceptor = Reference< XAcceptor > (m_rSMgr->createInstance(
76 rtl::OUString::createFromAscii( "com.sun.star.connection.Acceptor" )),
77 UNO_QUERY );
78 m_rBridgeFactory = Reference < XBridgeFactory > (m_rSMgr->createInstance(
79 rtl::OUString::createFromAscii( "com.sun.star.bridge.BridgeFactory" )),
80 UNO_QUERY );
81 // get component context
82 m_rContext = getComponentContext(m_rSMgr);
86 Acceptor::~Acceptor()
88 m_rAcceptor->stopAccepting();
89 oslThread t;
91 osl::MutexGuard g(m_aMutex);
92 t = m_thread;
94 osl_joinWithThread(t);
96 // Make the final state of m_bridges visible to this thread (since
97 // m_thread is joined, the code that follows is the only one left
98 // accessing m_bridges):
99 osl::MutexGuard g(m_aMutex);
101 for (;;) {
102 com::sun::star::uno::Reference< com::sun::star::bridge::XBridge > b(
103 m_bridges.remove());
104 if (!b.is()) {
105 break;
107 com::sun::star::uno::Reference< com::sun::star::lang::XComponent >(
108 b, com::sun::star::uno::UNO_QUERY_THROW)->dispose();
112 void SAL_CALL Acceptor::run()
114 while ( m_rAcceptor.is() && m_rBridgeFactory.is() )
116 RTL_LOGFILE_CONTEXT( aLog, "desktop (lo119109) Acceptor::run" );
119 // wait until we get enabled
120 RTL_LOGFILE_CONTEXT_TRACE( aLog, "desktop (lo119109)"\
121 "Acceptor::run waiting for office to come up");
122 m_cEnable.wait();
123 RTL_LOGFILE_CONTEXT_TRACE( aLog, "desktop (lo119109)"\
124 "Acceptor::run now enabled and continuing");
126 // accept connection
127 Reference< XConnection > rConnection = m_rAcceptor->accept( m_aConnectString );
128 // if we return without a valid connection we mus assume that the acceptor
129 // is destructed so we break out of the run method terminating the thread
130 if (! rConnection.is()) break;
131 OUString aDescription = rConnection->getDescription();
132 RTL_LOGFILE_CONTEXT_TRACE1( aLog, "desktop (lo119109) Acceptor::run connection %s",
133 OUStringToOString(aDescription, RTL_TEXTENCODING_ASCII_US).getStr());
135 // create instanceprovider for this connection
136 Reference< XInstanceProvider > rInstanceProvider(
137 (XInstanceProvider*)new AccInstanceProvider(m_rSMgr, rConnection));
138 // create the bridge. The remote end will have a reference to this bridge
139 // thus preventing the bridge from being disposed. When the remote end releases
140 // the bridge, it will be destructed.
141 Reference< XBridge > rBridge = m_rBridgeFactory->createBridge(
142 rtl::OUString() ,m_aProtocol ,rConnection ,rInstanceProvider );
143 osl::MutexGuard g(m_aMutex);
144 m_bridges.add(rBridge);
145 } catch (Exception&) {
146 // connection failed...
147 // something went wrong during connection setup.
148 // just wait for a new connection to accept
153 // XInitialize
154 void SAL_CALL Acceptor::initialize( const Sequence<Any>& aArguments )
155 throw( Exception )
157 // prevent multiple initialization
158 ClearableMutexGuard aGuard( m_aMutex );
159 RTL_LOGFILE_CONTEXT( aLog, "destop (lo119109) Acceptor::initialize()" );
161 sal_Bool bOk = sal_False;
163 // arg count
164 int nArgs = aArguments.getLength();
166 // not yet initialized and acceptstring
167 if (!m_bInit && nArgs > 0 && (aArguments[0] >>= m_aAcceptString))
169 RTL_LOGFILE_CONTEXT_TRACE1( aLog, "desktop (lo119109) Acceptor::initialize string=%s",
170 OUStringToOString(m_aAcceptString, RTL_TEXTENCODING_ASCII_US).getStr());
172 // get connect string and protocol from accept string
173 // "<connectString>;<protocol>"
174 sal_Int32 nIndex1 = m_aAcceptString.indexOf( (sal_Unicode) ';' );
175 if (nIndex1 < 0) throw IllegalArgumentException(
176 OUString::createFromAscii("Invalid accept-string format"), m_rContext, 1);
177 m_aConnectString = m_aAcceptString.copy( 0 , nIndex1 ).trim();
178 nIndex1++;
179 sal_Int32 nIndex2 = m_aAcceptString.indexOf( (sal_Unicode) ';' , nIndex1 );
180 if (nIndex2 < 0) nIndex2 = m_aAcceptString.getLength();
181 m_aProtocol = m_aAcceptString.copy( nIndex1, nIndex2 - nIndex1 );
183 // start accepting in new thread...
184 m_thread = osl_createThread(workerfunc, this);
185 m_bInit = sal_True;
186 bOk = sal_True;
189 // do we want to enable accepting?
190 sal_Bool bEnable = sal_False;
191 if (((nArgs == 1 && (aArguments[0] >>= bEnable)) ||
192 (nArgs == 2 && (aArguments[1] >>= bEnable))) &&
193 bEnable )
195 m_cEnable.set();
196 bOk = sal_True;
199 if (!bOk)
201 throw IllegalArgumentException(
202 OUString::createFromAscii("invalid initialization"), m_rContext, 1);
206 // XServiceInfo
207 const sal_Char *Acceptor::serviceName = "com.sun.star.office.Acceptor";
208 const sal_Char *Acceptor::implementationName = "com.sun.star.office.comp.Acceptor";
209 const sal_Char *Acceptor::supportedServiceNames[] = {"com.sun.star.office.Acceptor", NULL};
210 OUString Acceptor::impl_getImplementationName()
212 return OUString::createFromAscii( implementationName );
214 OUString SAL_CALL Acceptor::getImplementationName()
215 throw (RuntimeException)
217 return Acceptor::impl_getImplementationName();
219 Sequence<OUString> Acceptor::impl_getSupportedServiceNames()
221 Sequence<OUString> aSequence;
222 for (int i=0; supportedServiceNames[i]!=NULL; i++) {
223 aSequence.realloc(i+1);
224 aSequence[i]=(OUString::createFromAscii(supportedServiceNames[i]));
226 return aSequence;
228 Sequence<OUString> SAL_CALL Acceptor::getSupportedServiceNames()
229 throw (RuntimeException)
231 return Acceptor::impl_getSupportedServiceNames();
233 sal_Bool SAL_CALL Acceptor::supportsService( const OUString&)
234 throw (RuntimeException)
236 return sal_False;
239 // Factory
240 Reference< XInterface > Acceptor::impl_getInstance( const Reference< XMultiServiceFactory >& aFactory )
242 try {
243 return (XComponent*) new Acceptor( aFactory );
244 } catch ( Exception& ) {
245 return (XComponent*) NULL;
249 // InstanceProvider
250 AccInstanceProvider::AccInstanceProvider(const Reference<XMultiServiceFactory>& aFactory, const Reference<XConnection>& rConnection)
252 m_rSMgr = aFactory;
253 m_rConnection = rConnection;
256 AccInstanceProvider::~AccInstanceProvider()
260 Reference<XInterface> SAL_CALL AccInstanceProvider::getInstance (const OUString& aName )
261 throw ( NoSuchElementException )
264 Reference<XInterface> rInstance;
266 if ( aName.compareToAscii( "StarOffice.ServiceManager" ) == 0)
268 rInstance = Reference< XInterface >( m_rSMgr );
270 else if(aName.compareToAscii( "StarOffice.ComponentContext" ) == 0 )
272 rInstance = getComponentContext( m_rSMgr );
274 else if ( aName.compareToAscii("StarOffice.NamingService" ) == 0 )
276 Reference< XNamingService > rNamingService(
277 m_rSMgr->createInstance( OUString::createFromAscii( "com.sun.star.uno.NamingService" )),
278 UNO_QUERY );
279 if ( rNamingService.is() )
281 rNamingService->registerObject(
282 OUString::createFromAscii( "StarOffice.ServiceManager" ), m_rSMgr );
283 rNamingService->registerObject(
284 OUString::createFromAscii( "StarOffice.ComponentContext" ), getComponentContext( m_rSMgr ));
285 rInstance = rNamingService;
289 else if ( aName.compareToAscii("com.sun.star.ucb.RemoteContentProviderAcceptor" ))
291 Reference< XMultiServiceFactory > rSMgr = ::comphelper::getProcessServiceFactory();
292 if ( rSMgr.is() ) {
293 try {
294 rInstance = rSMgr->createInstance( sObjectName );
296 catch (Exception const &) {}
300 return rInstance;
305 // component management stuff...
306 // ----------------------------------------------------------------------------
307 extern "C"
309 using namespace desktop;
311 void SAL_CALL
312 component_getImplementationEnvironment(const sal_Char **ppEnvironmentTypeName, uno_Environment **)
314 *ppEnvironmentTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME ;
317 sal_Bool SAL_CALL
318 component_writeInfo(void *pServiceManager, void *pRegistryKey)
320 Reference< XMultiServiceFactory > xMan(reinterpret_cast< XMultiServiceFactory* >(pServiceManager));
321 Reference< XRegistryKey > xKey(reinterpret_cast< XRegistryKey* >(pRegistryKey));
323 // register service
324 ::rtl::OUString aTempStr;
325 ::rtl::OUString aImpl(RTL_CONSTASCII_USTRINGPARAM("/"));
326 aImpl += Acceptor::impl_getImplementationName();
327 aImpl += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/UNO/SERVICES"));
328 Reference< XRegistryKey > xNewKey = xKey->createKey(aImpl);
329 xNewKey->createKey(Acceptor::impl_getSupportedServiceNames()[0]);
331 return sal_True;
334 void * SAL_CALL
335 component_getFactory(const sal_Char *pImplementationName, void *pServiceManager, void *)
337 void* pReturn = NULL ;
338 if ( pImplementationName && pServiceManager )
340 // Define variables which are used in following macros.
341 Reference< XSingleServiceFactory > xFactory;
342 Reference< XMultiServiceFactory > xServiceManager(
343 reinterpret_cast< XMultiServiceFactory* >(pServiceManager));
345 if (Acceptor::impl_getImplementationName().compareToAscii( pImplementationName ) == COMPARE_EQUAL )
347 xFactory = Reference< XSingleServiceFactory >( cppu::createSingleFactory(
348 xServiceManager, Acceptor::impl_getImplementationName(),
349 Acceptor::impl_getInstance, Acceptor::impl_getSupportedServiceNames()) );
352 // Factory is valid - service was found.
353 if ( xFactory.is() )
355 xFactory->acquire();
356 pReturn = xFactory.get();
360 // Return with result of this operation.
361 return pReturn ;
364 } // extern "C"