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 <rtl/process.h>
27 #include <cppuhelper/servicefactory.hxx>
28 #include <cppuhelper/weak.hxx>
29 #include <cppuhelper/bootstrap.hxx>
31 #include <com/sun/star/registry/XSimpleRegistry.hpp>
32 #include <com/sun/star/lang/XComponent.hpp>
33 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
34 #include <com/sun/star/java/XJavaVM.hpp>
35 #include <com/sun/star/registry/XImplementationRegistration.hpp>
36 #include <com/sun/star/java/XJavaThreadRegister_11.hpp>
40 using namespace com::sun::star::uno
;
41 using namespace com::sun::star::lang
;
42 using namespace com::sun::star::registry
;
43 using namespace com::sun::star::java
;
47 sal_Bool
testJavaVM(const Reference
< XMultiServiceFactory
> & xMgr
)
50 OUString
sVMService("com.sun.star.java.JavaVirtualMachine");
51 Reference
<XInterface
> xXInt
= xMgr
->createInstance(sVMService
);
54 Reference
<XJavaVM
> xVM( xXInt
, UNO_QUERY
);
57 Reference
<XJavaThreadRegister_11
> xreg11(xVM
, UNO_QUERY
);
63 rtl_getGlobalProcessId((sal_uInt8
*) arId
);
64 Any anyVM
= xVM
->getJavaVM( Sequence
<sal_Int8
>(arId
, 16));
65 if ( ! anyVM
.hasValue())
67 OSL_FAIL("could not get Java VM");
71 sal_Bool b
= xreg11
->isThreadAttached();
72 xreg11
->registerThread();
73 b
= xreg11
->isThreadAttached();
74 xreg11
->revokeThread();
75 b
= xreg11
->isThreadAttached();
78 b
= xVM
->isVMEnabled();
79 b
= xVM
->isVMStarted();
82 b
= xVM
->isVMEnabled();
83 b
= xVM
->isVMStarted();
86 JavaVM
* _jvm
= *(JavaVM
**) anyVM
.getValue();
88 if( _jvm
->AttachCurrentThread((void**) &p_env
, 0))
91 jclass cls
= p_env
->FindClass( "TestJavaVM");
93 OSL_TRACE( "Can't find Prog class");
97 jmethodID id
= p_env
->GetStaticMethodID( cls
, "getInt", "()I");
100 p_env
->CallStaticIntMethod(cls
, id
);
103 if( p_env
->ExceptionOccurred()){
104 p_env
->ExceptionDescribe();
105 p_env
->ExceptionClear();
109 _jvm
->DetachCurrentThread();
115 Reference
<XSimpleRegistry
> xreg
= createSimpleRegistry();
116 xreg
->open( OUString("applicat.rdb"),
117 sal_False
, sal_False
);
119 Reference
< XComponentContext
> context
= bootstrap_InitialComponentContext(xreg
);
120 Reference
<XMultiComponentFactory
> fac
= context
->getServiceManager();
121 Reference
<XMultiServiceFactory
> xMgr( fac
, UNO_QUERY
);
123 sal_Bool bSucc
= sal_False
;
127 "com.sun.star.registry.ImplementationRegistration");
128 Reference
<com::sun::star::registry::XImplementationRegistration
> xImplReg(
129 xMgr
->createInstance( sImplReg
), UNO_QUERY
);
130 OSL_ENSURE( xImplReg
.is(), "### no impl reg!" );
133 OUString
sLibLoader("com.sun.star.loader.SharedLibrary");
135 RTL_CONSTASCII_USTRINGPARAM( "javavm.uno" SAL_DLLEXTENSION
) );
136 xImplReg
->registerImplementation(
137 sLibLoader
, sJenLib
, Reference
< XSimpleRegistry
>() );
139 bSucc
= testJavaVM( xMgr
);
141 catch (const Exception
& rExc
)
143 OSL_FAIL( "### exception occurred!" );
144 OString
aMsg( OUStringToOString( rExc
.Message
, RTL_TEXTENCODING_ASCII_US
) );
145 OSL_TRACE( "### exception occurred: " );
146 OSL_TRACE( "%s", aMsg
.getStr() );
150 Reference
< XComponent
> xCompContext( context
, UNO_QUERY
);
151 xCompContext
->dispose();
152 printf("javavm %s", bSucc
? "succeeded" : "failed");
153 return (bSucc
? 0 : -1);
157 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */