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 .
25 #include <sal/log.hxx>
26 #include <rtl/process.h>
28 #include <cppuhelper/servicefactory.hxx>
29 #include <cppuhelper/weak.hxx>
30 #include <cppuhelper/bootstrap.hxx>
32 #include <com/sun/star/registry/XSimpleRegistry.hpp>
33 #include <com/sun/star/lang/XComponent.hpp>
34 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
35 #include <com/sun/star/java/XJavaVM.hpp>
36 #include <com/sun/star/registry/XImplementationRegistration.hpp>
37 #include <com/sun/star/java/XJavaThreadRegister_11.hpp>
40 using namespace css::uno
;
41 using namespace css::lang
;
42 using namespace css::registry
;
43 using namespace css::java
;
46 sal_Bool
testJavaVM(const Reference
< XMultiServiceFactory
> & xMgr
)
49 OUString
sVMService("com.sun.star.java.JavaVirtualMachine");
50 Reference
<XInterface
> xXInt
= xMgr
->createInstance(sVMService
);
53 Reference
<XJavaVM
> xVM( xXInt
, UNO_QUERY
);
56 Reference
<XJavaThreadRegister_11
> xreg11(xVM
, UNO_QUERY
);
62 rtl_getGlobalProcessId((sal_uInt8
*) arId
);
63 Any anyVM
= xVM
->getJavaVM( Sequence
<sal_Int8
>(arId
, 16));
64 if ( ! anyVM
.hasValue())
66 OSL_FAIL("could not get Java VM");
70 sal_Bool b
= xreg11
->isThreadAttached();
71 xreg11
->registerThread();
72 b
= xreg11
->isThreadAttached();
73 xreg11
->revokeThread();
74 b
= xreg11
->isThreadAttached();
77 b
= xVM
->isVMEnabled();
78 b
= xVM
->isVMStarted();
81 b
= xVM
->isVMEnabled();
82 b
= xVM
->isVMStarted();
85 JavaVM
* _jvm
= *(JavaVM
**) anyVM
.getValue();
87 if( _jvm
->AttachCurrentThread((void**) &p_env
, 0))
90 jclass cls
= p_env
->FindClass( "TestJavaVM");
92 SAL_WARN("stoc", "Can't find Prog class");
96 jmethodID id
= p_env
->GetStaticMethodID( cls
, "getInt", "()I");
99 p_env
->CallStaticIntMethod(cls
, id
);
102 if( p_env
->ExceptionOccurred()){
103 p_env
->ExceptionDescribe();
104 p_env
->ExceptionClear();
108 _jvm
->DetachCurrentThread();
114 Reference
<XSimpleRegistry
> xreg
= createSimpleRegistry();
115 xreg
->open( OUString("applicat.rdb"),
116 sal_False
, sal_False
);
118 Reference
< XComponentContext
> context
= bootstrap_InitialComponentContext(xreg
);
119 Reference
<XMultiComponentFactory
> fac
= context
->getServiceManager();
120 Reference
<XMultiServiceFactory
> xMgr( fac
, UNO_QUERY
);
122 sal_Bool bSucc
= sal_False
;
126 "com.sun.star.registry.ImplementationRegistration");
127 Reference
<css::registry::XImplementationRegistration
> xImplReg(
128 xMgr
->createInstance( sImplReg
), UNO_QUERY
);
129 OSL_ENSURE( xImplReg
.is(), "### no impl reg!" );
132 OUString
sLibLoader("com.sun.star.loader.SharedLibrary");
133 OUString
sJenLib( "javavm.uno" SAL_DLLEXTENSION
);
134 xImplReg
->registerImplementation(
135 sLibLoader
, sJenLib
, Reference
< XSimpleRegistry
>() );
137 bSucc
= testJavaVM( xMgr
);
139 catch (const Exception
& rExc
)
141 DBG_UNHANDLED_EXCEPTION("stoc", "### exception occurred: " << rExc
);
144 Reference
< XComponent
> xCompContext( context
, UNO_QUERY
);
145 xCompContext
->dispose();
146 printf("javavm %s", bSucc
? "succeeded" : "failed");
147 return (bSucc
? 0 : -1);
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */