fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / stoc / test / javavm / testjavavm.cxx
blobece8cb16fee56f18ede401c19bafd596ef3005ee
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::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);
52 if( ! xXInt.is())
53 return sal_False;
54 Reference<XJavaVM> xVM( xXInt, UNO_QUERY);
55 if( ! xVM.is())
56 return sal_False;
57 Reference<XJavaThreadRegister_11> xreg11(xVM, UNO_QUERY);
58 if( ! xreg11.is())
59 return sal_False;
62 sal_Int8 arId[16];
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");
68 return sal_False;
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();
87 JNIEnv *p_env;
88 if( _jvm->AttachCurrentThread((void**) &p_env, 0))
89 return sal_False;
91 jclass cls = p_env->FindClass( "TestJavaVM");
92 if (cls == 0) {
93 OSL_TRACE( "Can't find Prog class");
94 exit(1);
97 jmethodID id = p_env->GetStaticMethodID( cls, "getInt", "()I");
98 if( id)
100 p_env->CallStaticIntMethod(cls, id);
103 if( p_env->ExceptionOccurred()){
104 p_env->ExceptionDescribe();
105 p_env->ExceptionClear();
109 _jvm->DetachCurrentThread();
110 return sal_True;
113 SAL_IMPLEMENT_MAIN()
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;
126 OUString sImplReg(
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");
134 OUString sJenLib(
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() );
147 OSL_TRACE( "\n" );
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: */