1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
23 #include <sal/log.hxx>
25 #include <com/sun/star/io/XActiveDataSource.hpp>
26 #include <com/sun/star/io/XActiveDataSink.hpp>
27 #include <com/sun/star/io/XActiveDataControl.hpp>
28 #include <com/sun/star/io/XConnectable.hpp>
29 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/lang/XServiceInfo.hpp>
32 #include <com/sun/star/registry/XRegistryKey.hpp>
34 #include <uno/dispatcher.h>
35 #include <uno/mapping.hxx>
36 #include <cppuhelper/implbase5.hxx>
37 #include <cppuhelper/factory.hxx>
38 #include <cppuhelper/interfacecontainer.hxx>
39 #include <osl/mutex.hxx>
40 #include <osl/thread.h>
46 using namespace com::sun::star::uno
;
47 using namespace com::sun::star::lang
;
48 using namespace com::sun::star::registry
;
49 using namespace com::sun::star::io
;
51 #include "services.hxx"
55 class Pump
: public WeakImplHelper5
<
56 XActiveDataSource
, XActiveDataSink
, XActiveDataControl
, XConnectable
, XServiceInfo
>
61 Reference
< XConnectable
> m_xPred
;
62 Reference
< XConnectable
> m_xSucc
;
63 Reference
< XInputStream
> m_xInput
;
64 Reference
< XOutputStream
> m_xOutput
;
65 OInterfaceContainerHelper m_cnt
;
66 sal_Bool m_closeFired
;
69 static void static_run( void* pObject
);
74 void fireTerminated();
75 void fireError( const Any
&a
);
82 virtual void SAL_CALL
setOutputStream( const Reference
< ::com::sun::star::io::XOutputStream
>& xOutput
) throw();
83 virtual Reference
< ::com::sun::star::io::XOutputStream
> SAL_CALL
getOutputStream() throw();
86 virtual void SAL_CALL
setInputStream( const Reference
< ::com::sun::star::io::XInputStream
>& xStream
) throw();
87 virtual Reference
< ::com::sun::star::io::XInputStream
> SAL_CALL
getInputStream() throw();
90 virtual void SAL_CALL
addListener( const Reference
< ::com::sun::star::io::XStreamListener
>& xListener
) throw();
91 virtual void SAL_CALL
removeListener( const Reference
< ::com::sun::star::io::XStreamListener
>& xListener
) throw();
92 virtual void SAL_CALL
start() throw( RuntimeException
);
93 virtual void SAL_CALL
terminate() throw();
96 virtual void SAL_CALL
setPredecessor( const Reference
< ::com::sun::star::io::XConnectable
>& xPred
) throw();
97 virtual Reference
< ::com::sun::star::io::XConnectable
> SAL_CALL
getPredecessor() throw();
98 virtual void SAL_CALL
setSuccessor( const Reference
< ::com::sun::star::io::XConnectable
>& xSucc
) throw();
99 virtual Reference
< ::com::sun::star::io::XConnectable
> SAL_CALL
getSuccessor() throw();
101 public: // XServiceInfo
102 virtual OUString SAL_CALL
getImplementationName() throw( );
103 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames(void) throw( );
104 virtual sal_Bool SAL_CALL
supportsService(const OUString
& ServiceName
) throw( );
107 Pump::Pump() : m_aThread( 0 ),
109 m_closeFired( sal_False
)
118 osl_joinWithThread( m_aThread
);
119 osl_destroyThread( m_aThread
);
123 void Pump::fireError( const Any
& exception
)
125 OInterfaceIteratorHelper
iter( m_cnt
);
126 while( iter
.hasMoreElements() )
130 static_cast< XStreamListener
* > ( iter
.next() )->error( exception
);
132 catch ( const RuntimeException
&e
)
134 SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e
.Message
);
139 void Pump::fireClose()
141 sal_Bool bFire
= sal_False
;
143 MutexGuard
guard( m_aMutex
);
146 m_closeFired
= sal_True
;
153 OInterfaceIteratorHelper
iter( m_cnt
);
154 while( iter
.hasMoreElements() )
158 static_cast< XStreamListener
* > ( iter
.next() )->closed( );
160 catch ( const RuntimeException
&e
)
162 SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e
.Message
);
168 void Pump::fireStarted()
170 OInterfaceIteratorHelper
iter( m_cnt
);
171 while( iter
.hasMoreElements() )
175 static_cast< XStreamListener
* > ( iter
.next() )->started( );
177 catch ( const RuntimeException
&e
)
179 SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e
.Message
);
184 void Pump::fireTerminated()
186 OInterfaceIteratorHelper
iter( m_cnt
);
187 while( iter
.hasMoreElements() )
191 static_cast< XStreamListener
* > ( iter
.next() )->terminated();
193 catch ( const RuntimeException
&e
)
195 SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e
.Message
);
204 // close streams and release references
205 Reference
< XInputStream
> rInput
;
206 Reference
< XOutputStream
> rOutput
;
208 MutexGuard
guard( m_aMutex
);
221 rInput
->closeInput();
232 rOutput
->closeOutput();
241 void Pump::static_run( void* pObject
)
243 ((Pump
*)pObject
)->run();
244 ((Pump
*)pObject
)->release();
254 Reference
< XInputStream
> rInput
;
255 Reference
< XOutputStream
> rOutput
;
257 Guard
< Mutex
> aGuard( m_aMutex
);
264 NotConnectedException
exception(
265 OUString("no input stream set") , Reference
<XInterface
>((OWeakObject
*)this) );
268 Sequence
< sal_Int8
> aData
;
269 while( rInput
->readSomeBytes( aData
, 65536 ) )
273 NotConnectedException
exception(
274 OUString("no output stream set") , Reference
<XInterface
>( (OWeakObject
*)this) );
277 rOutput
->writeBytes( aData
);
281 catch ( const IOException
& e
)
283 fireError( makeAny( e
) );
285 catch ( const RuntimeException
& e
)
287 fireError( makeAny( e
) );
289 catch ( const Exception
& e
)
291 fireError( makeAny( e
) );
297 catch ( const com::sun::star::uno::Exception
&e
)
299 // we are the last on the stack.
300 // this is to avoid crashing the program, when e.g. a bridge crashes
301 SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e
.Message
);
305 // ------------------------------------------------------------
311 void Pump::setPredecessor( const Reference
< XConnectable
>& xPred
) throw()
313 Guard
< Mutex
> aGuard( m_aMutex
);
317 // ------------------------------------------------------------
319 Reference
< XConnectable
> Pump::getPredecessor() throw()
321 Guard
< Mutex
> aGuard( m_aMutex
);
325 // ------------------------------------------------------------
327 void Pump::setSuccessor( const Reference
< XConnectable
>& xSucc
) throw()
329 Guard
< Mutex
> aGuard( m_aMutex
);
333 // ------------------------------------------------------------
335 Reference
< XConnectable
> Pump::getSuccessor() throw()
337 Guard
< Mutex
> aGuard( m_aMutex
);
341 // -----------------------------------------------------------------
347 void Pump::addListener( const Reference
< XStreamListener
>& xListener
) throw()
349 m_cnt
.addInterface( xListener
);
352 // ------------------------------------------------------------
354 void Pump::removeListener( const Reference
< XStreamListener
>& xListener
) throw()
356 m_cnt
.removeInterface( xListener
);
359 // ------------------------------------------------------------
361 void Pump::start() throw( RuntimeException
)
363 Guard
< Mutex
> aGuard( m_aMutex
);
364 m_aThread
= osl_createSuspendedThread((oslWorkerFunction
)Pump::static_run
,this);
367 // will be released by OPump::static_run
369 osl_resumeThread( m_aThread
);
373 throw RuntimeException(
374 OUString("Pump::start Couldn't create worker thread"),
379 // ------------------------------------------------------------
381 void Pump::terminate() throw()
385 // wait for the worker to die
387 osl_joinWithThread( m_aThread
);
393 // ------------------------------------------------------------
399 void Pump::setInputStream( const Reference
< XInputStream
>& xStream
) throw()
401 Guard
< Mutex
> aGuard( m_aMutex
);
403 Reference
< XConnectable
> xConnect( xStream
, UNO_QUERY
);
405 xConnect
->setSuccessor( this );
406 // data transfer starts in XActiveDataControl::start
409 // ------------------------------------------------------------
411 Reference
< XInputStream
> Pump::getInputStream() throw()
413 Guard
< Mutex
> aGuard( m_aMutex
);
417 // ------------------------------------------------------------
423 void Pump::setOutputStream( const Reference
< XOutputStream
>& xOut
) throw()
425 Guard
< Mutex
> aGuard( m_aMutex
);
427 Reference
< XConnectable
> xConnect( xOut
, UNO_QUERY
);
429 xConnect
->setPredecessor( this );
430 // data transfer starts in XActiveDataControl::start
433 // ------------------------------------------------------------
435 Reference
< XOutputStream
> Pump::getOutputStream() throw()
437 Guard
< Mutex
> aGuard( m_aMutex
);
443 OUString
Pump::getImplementationName() throw( )
445 return OPumpImpl_getImplementationName();
449 sal_Bool
Pump::supportsService(const OUString
& ServiceName
) throw( )
451 Sequence
< OUString
> aSNL
= getSupportedServiceNames();
452 const OUString
* pArray
= aSNL
.getConstArray();
454 for( sal_Int32 i
= 0; i
< aSNL
.getLength(); i
++ )
455 if( pArray
[i
] == ServiceName
)
462 Sequence
< OUString
> Pump::getSupportedServiceNames(void) throw( )
464 return OPumpImpl_getSupportedServiceNames();
468 Reference
< XInterface
> SAL_CALL
OPumpImpl_CreateInstance(
469 SAL_UNUSED_PARAMETER
const Reference
< XComponentContext
> & )
472 return Reference
< XInterface
>( *new Pump
);
475 OUString
OPumpImpl_getImplementationName()
477 return OUString("com.sun.star.comp.io.Pump");
480 Sequence
<OUString
> OPumpImpl_getSupportedServiceNames(void)
482 OUString
s("com.sun.star.io.Pump");
483 Sequence
< OUString
> seq( &s
, 1 );
489 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */