diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2020-07-24 08:36:10 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2020-07-24 09:20:25 +0200 |
commit | 785312dc11a78f72784c04a2e8e3183162a1e28b (patch) | |
tree | e1b5a090b4f1be294d0e39b07be99e34f9aba17b | |
parent | 7e70a1004ac012c271ee5c22c88aeb93d0f1f6a3 (diff) |
stoc/javavm: create instances with uno constructors
See tdf#74608 for motivation.
Change-Id: I4eb381d19e453ceb2b035462f0f5de923458a03e
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/99345
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
-rw-r--r-- | stoc/source/javavm/javavm.component | 5 | ||||
-rw-r--r-- | stoc/source/javavm/javavm.cxx | 145 | ||||
-rw-r--r-- | stoc/source/javavm/javavm.hxx | 4 |
3 files changed, 36 insertions, 118 deletions
diff --git a/stoc/source/javavm/javavm.component b/stoc/source/javavm/javavm.component index 04367fad7ee3..79d993ddd62c 100644 --- a/stoc/source/javavm/javavm.component +++ b/stoc/source/javavm/javavm.component @@ -18,8 +18,9 @@ --> <component loader="com.sun.star.loader.SharedLibrary" environment="@CPPU_ENV@" - prefix="javavm" xmlns="http://openoffice.org/2010/uno-components"> - <implementation name="com.sun.star.comp.stoc.JavaVirtualMachine"> + xmlns="http://openoffice.org/2010/uno-components"> + <implementation name="com.sun.star.comp.stoc.JavaVirtualMachine" + constructor="stoc_JavaVM_get_implementation"> <service name="com.sun.star.java.JavaVirtualMachine"/> <singleton name="com.sun.star.java.theJavaVirtualMachine"/> </implementation> diff --git a/stoc/source/javavm/javavm.cxx b/stoc/source/javavm/javavm.cxx index 7df1fed46c57..c2c5f469e914 100644 --- a/stoc/source/javavm/javavm.cxx +++ b/stoc/source/javavm/javavm.cxx @@ -108,111 +108,6 @@ class NoJavaIniException: public css::uno::Exception { }; -class SingletonFactory: - private cppu::WeakImplHelper< css::lang::XEventListener > -{ -public: - static css::uno::Reference< css::uno::XInterface > getSingleton( - css::uno::Reference< css::uno::XComponentContext > const & rContext); - -private: - SingletonFactory() {} - - virtual ~SingletonFactory() override {} - - SingletonFactory(const SingletonFactory&) = delete; - SingletonFactory& operator=(const SingletonFactory&) = delete; - - virtual void SAL_CALL disposing(css::lang::EventObject const &) override; - - static void dispose(); - - // TODO ok to keep these static? - static osl::Mutex m_aMutex; - static css::uno::Reference< css::uno::XInterface > m_xSingleton; - static bool m_bDisposed; -}; - -css::uno::Reference< css::uno::XInterface > SingletonFactory::getSingleton( - css::uno::Reference< css::uno::XComponentContext > const & rContext) -{ - css::uno::Reference< css::uno::XInterface > xSingleton; - css::uno::Reference< css::lang::XComponent > xComponent; - { - osl::MutexGuard aGuard(m_aMutex); - if (!m_xSingleton.is()) - { - if (m_bDisposed) - throw css::lang::DisposedException(); - xComponent.set( rContext, css::uno::UNO_QUERY_THROW); - m_xSingleton = static_cast< cppu::OWeakObject * >( - new JavaVirtualMachine(rContext)); - } - xSingleton = m_xSingleton; - } - if (xComponent.is()) - try - { - xComponent->addEventListener(new SingletonFactory); - } - catch (...) - { - dispose(); - throw; - } - return xSingleton; -} - -void SAL_CALL SingletonFactory::disposing(css::lang::EventObject const &) -{ - dispose(); -} - -void SingletonFactory::dispose() -{ - css::uno::Reference< css::lang::XComponent > xComponent; - { - osl::MutexGuard aGuard(m_aMutex); - xComponent.set( m_xSingleton, css::uno::UNO_QUERY); - m_xSingleton.clear(); - m_bDisposed = true; - } - if (xComponent.is()) - xComponent->dispose(); -} - -osl::Mutex SingletonFactory::m_aMutex; -css::uno::Reference< css::uno::XInterface > SingletonFactory::m_xSingleton; -bool SingletonFactory::m_bDisposed = false; - -OUString serviceGetImplementationName() -{ - return "com.sun.star.comp.stoc.JavaVirtualMachine"; -} - -css::uno::Sequence< OUString > serviceGetSupportedServiceNames() -{ - return css::uno::Sequence< OUString > { "com.sun.star.java.JavaVirtualMachine" }; -} - -css::uno::Reference< css::uno::XInterface > serviceCreateInstance( - css::uno::Reference< css::uno::XComponentContext > const & rContext) -{ - // Only one single instance of this service is ever constructed, and is - // available until the component context used to create this instance is - // disposed. Afterwards, this function throws a DisposedException (as do - // all relevant methods on the single service instance). - return SingletonFactory::getSingleton(rContext); -} - -cppu::ImplementationEntry const aServiceImplementation[] - = { { serviceCreateInstance, - serviceGetImplementationName, - serviceGetSupportedServiceNames, - cppu::createSingleComponentFactory, - nullptr, 0 }, - { nullptr, nullptr, nullptr, nullptr, nullptr, 0 } }; - typedef std::stack< jvmaccess::VirtualMachine::AttachGuard * > GuardStack; extern "C" { @@ -577,15 +472,6 @@ private: } -extern "C" SAL_DLLPUBLIC_EXPORT void * javavm_component_getFactory(char const * pImplName, - void * pServiceManager, - void * pRegistryKey) -{ - return cppu::component_getFactoryHelper(pImplName, pServiceManager, - pRegistryKey, - aServiceImplementation); -} - JavaVirtualMachine::JavaVirtualMachine( css::uno::Reference< css::uno::XComponentContext > const & rContext): JavaVirtualMachine_Impl(m_aMutex), @@ -654,7 +540,7 @@ JavaVirtualMachine::initialize(css::uno::Sequence< css::uno::Any > const & OUString SAL_CALL JavaVirtualMachine::getImplementationName() { - return serviceGetImplementationName(); + return "com.sun.star.comp.stoc.JavaVirtualMachine"; } sal_Bool SAL_CALL @@ -666,7 +552,7 @@ JavaVirtualMachine::supportsService(OUString const & rServiceName) css::uno::Sequence< OUString > SAL_CALL JavaVirtualMachine::getSupportedServiceNames() { - return serviceGetSupportedServiceNames(); + return { "com.sun.star.java.JavaVirtualMachine" }; } css::uno::Any SAL_CALL @@ -1592,4 +1478,31 @@ void JavaVirtualMachine::handleJniException(JNIEnv * environment) { static_cast< cppu::OWeakObject * >(this)); } + +static osl::Mutex m_aMutex; +static rtl::Reference< JavaVirtualMachine > m_xSingleton; + + +void JavaVirtualMachine::dispose() { + JavaVirtualMachine_Impl::dispose(); + osl::MutexGuard aGuard(m_aMutex); + m_xSingleton.clear(); +} + +extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface* +stoc_JavaVM_get_implementation( + css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const&) +{ + // Only one single instance of this service is ever constructed, and is + // available until the component context used to create this instance is + // disposed. Afterwards, this function throws a DisposedException (as do + // all relevant methods on the single service instance). + osl::MutexGuard aGuard(m_aMutex); + if (!m_xSingleton.is()) + m_xSingleton.set(new JavaVirtualMachine(context)); + m_xSingleton->acquire(); + return static_cast<cppu::OWeakObject*>(m_xSingleton.get()); +} + + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/stoc/source/javavm/javavm.hxx b/stoc/source/javavm/javavm.hxx index 13418c30011c..896d380572ec 100644 --- a/stoc/source/javavm/javavm.hxx +++ b/stoc/source/javavm/javavm.hxx @@ -105,6 +105,10 @@ public: virtual void SAL_CALL elementReplaced(css::container::ContainerEvent const & rEvent) override; + // XComponent + virtual void SAL_CALL + dispose() override; + private: JavaVirtualMachine(JavaVirtualMachine const &) = delete; void operator =(const JavaVirtualMachine&) = delete; |