Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / stoc / test / javavm / testjavavm.cxx
blob5b53ae1e681ab9778440dac827c2865cf2f4f065
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #include <jni.h>
23 #include <stdio.h>
24 #include <sal/main.h>
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>
38 using namespace std;
39 using namespace cppu;
40 using namespace com::sun::star::uno;
41 using namespace com::sun::star::lang;
42 using namespace com::sun::star::lang;
43 using namespace com::sun::star::registry;
44 using namespace com::sun::star::java;
46 using ::rtl::OUString;
47 using ::rtl::OUStringToOString;
48 using ::rtl::OString;
51 sal_Bool testJavaVM(const Reference< XMultiServiceFactory > & xMgr )
54 OUString sVMService( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.java.JavaVirtualMachine"));
55 Reference<XInterface> xXInt= xMgr->createInstance(sVMService);
56 if( ! xXInt.is())
57 return sal_False;
58 Reference<XJavaVM> xVM( xXInt, UNO_QUERY);
59 if( ! xVM.is())
60 return sal_False;
61 Reference<XJavaThreadRegister_11> xreg11(xVM, UNO_QUERY);
62 if( ! xreg11.is())
63 return sal_False;
66 sal_Int8 arId[16];
67 rtl_getGlobalProcessId((sal_uInt8*) arId);
68 Any anyVM = xVM->getJavaVM( Sequence<sal_Int8>(arId, 16));
69 if ( ! anyVM.hasValue())
71 OSL_FAIL("could not get Java VM");
72 return sal_False;
75 sal_Bool b= xreg11->isThreadAttached();
76 xreg11->registerThread();
77 b= xreg11->isThreadAttached();
78 xreg11->revokeThread();
79 b= xreg11->isThreadAttached();
82 b= xVM->isVMEnabled();
83 b= xVM->isVMStarted();
86 b= xVM->isVMEnabled();
87 b= xVM->isVMStarted();
90 JavaVM* _jvm= *(JavaVM**) anyVM.getValue();
91 JNIEnv *p_env;
92 if( _jvm->AttachCurrentThread((void**) &p_env, 0))
93 return sal_False;
95 jclass cls = p_env->FindClass( "TestJavaVM");
96 if (cls == 0) {
97 OSL_TRACE( "Can't find Prog class");
98 exit(1);
101 jmethodID id = p_env->GetStaticMethodID( cls, "getInt", "()I");
102 if( id)
104 p_env->CallStaticIntMethod(cls, id);
107 if( p_env->ExceptionOccurred()){
108 p_env->ExceptionDescribe();
109 p_env->ExceptionClear();
113 _jvm->DetachCurrentThread();
114 return sal_True;
117 SAL_IMPLEMENT_MAIN()
119 Reference<XSimpleRegistry> xreg= createSimpleRegistry();
120 xreg->open( OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb")),
121 sal_False, sal_False );
123 Reference< XComponentContext > context= bootstrap_InitialComponentContext(xreg);
124 Reference<XMultiComponentFactory> fac= context->getServiceManager();
125 Reference<XMultiServiceFactory> xMgr( fac, UNO_QUERY);
127 sal_Bool bSucc = sal_False;
130 OUString sImplReg(RTL_CONSTASCII_USTRINGPARAM(
131 "com.sun.star.registry.ImplementationRegistration"));
132 Reference<com::sun::star::registry::XImplementationRegistration> xImplReg(
133 xMgr->createInstance( sImplReg ), UNO_QUERY );
134 OSL_ENSURE( xImplReg.is(), "### no impl reg!" );
137 OUString sLibLoader( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.loader.SharedLibrary"));
138 OUString sJenLib(
139 RTL_CONSTASCII_USTRINGPARAM( "javavm.uno" SAL_DLLEXTENSION ) );
140 xImplReg->registerImplementation(
141 sLibLoader, sJenLib, Reference< XSimpleRegistry >() );
143 bSucc = testJavaVM( xMgr );
145 catch (const Exception & rExc)
147 OSL_FAIL( "### exception occurred!" );
148 OString aMsg( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
149 OSL_TRACE( "### exception occurred: " );
150 OSL_TRACE( "%s", aMsg.getStr() );
151 OSL_TRACE( "\n" );
154 Reference< XComponent > xCompContext( context, UNO_QUERY );
155 xCompContext->dispose();
156 printf("javavm %s", bSucc ? "succeeded" : "failed");
157 return (bSucc ? 0 : -1);
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */