1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_stoc.hxx" 30 31 32 #include <jni.h> 33 34 //#include <iostream> 35 #include <stdio.h> 36 #include <sal/main.h> 37 #include <rtl/process.h> 38 39 #include <cppuhelper/servicefactory.hxx> 40 #include <cppuhelper/weak.hxx> 41 #include <cppuhelper/bootstrap.hxx> 42 43 #include <com/sun/star/registry/XSimpleRegistry.hpp> 44 #include <com/sun/star/lang/XComponent.hpp> 45 #include <com/sun/star/lang/XMultiComponentFactory.hpp> 46 #include <com/sun/star/java/XJavaVM.hpp> 47 #include <com/sun/star/registry/XImplementationRegistration.hpp> 48 #include <com/sun/star/java/XJavaThreadRegister_11.hpp> 49 50 //#include <cppuhelper/implbase1.hxx> 51 52 using namespace std; 53 using namespace rtl; 54 using namespace cppu; 55 using namespace com::sun::star::uno; 56 using namespace com::sun::star::lang; 57 //using namespace com::sun::star::reflection; 58 using namespace com::sun::star::lang; 59 using namespace com::sun::star::registry; 60 using namespace com::sun::star::java; 61 62 63 sal_Bool testJavaVM(const Reference< XMultiServiceFactory > & xMgr ) 64 { 65 66 OUString sVMService( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.java.JavaVirtualMachine")); 67 Reference<XInterface> xXInt= xMgr->createInstance(sVMService); 68 if( ! xXInt.is()) 69 return sal_False; 70 Reference<XJavaVM> xVM( xXInt, UNO_QUERY); 71 if( ! xVM.is()) 72 return sal_False; 73 Reference<XJavaThreadRegister_11> xreg11(xVM, UNO_QUERY); 74 if( ! xreg11.is()) 75 return sal_False; 76 77 78 sal_Int8 arId[16]; 79 rtl_getGlobalProcessId((sal_uInt8*) arId); 80 Any anyVM = xVM->getJavaVM( Sequence<sal_Int8>(arId, 16)); 81 if ( ! anyVM.hasValue()) 82 { 83 OSL_ENSURE(0,"could not get Java VM"); 84 return sal_False; 85 } 86 87 sal_Bool b= xreg11->isThreadAttached(); 88 xreg11->registerThread(); 89 b= xreg11->isThreadAttached(); 90 xreg11->revokeThread(); 91 b= xreg11->isThreadAttached(); 92 93 94 b= xVM->isVMEnabled(); 95 b= xVM->isVMStarted(); 96 97 98 b= xVM->isVMEnabled(); 99 b= xVM->isVMStarted(); 100 101 102 JavaVM* _jvm= *(JavaVM**) anyVM.getValue(); 103 JNIEnv *p_env; 104 if( _jvm->AttachCurrentThread((void**) &p_env, 0)) 105 return sal_False; 106 107 // jclass aJProg = p_env->FindClass("TestJavaVM"); 108 // if( p_env->ExceptionOccurred()){ 109 // p_env->ExceptionDescribe(); 110 // p_env->ExceptionClear(); 111 // } 112 // 113 // jmethodID mid= p_env->GetStaticMethodID( aJProg,"main", "([Ljava/lang/String;)V"); 114 115 jclass cls = p_env->FindClass( "TestJavaVM"); 116 if (cls == 0) { 117 OSL_TRACE( "Can't find Prog class\n"); 118 exit(1); 119 } 120 121 // jmethodID methid = p_env->GetStaticMethodID( cls, "main", "([Ljava/lang/String;)V"); 122 // if (methid == 0) { 123 // OSL_TRACE("Can't find Prog.main\n"); 124 // exit(1); 125 // } 126 127 // jstring jstr = p_env->NewStringUTF(" from C!"); 128 // if (jstr == 0) { 129 // OSL_TRACE("Out of memory\n"); 130 // exit(1); 131 // } 132 // jobjectArray args = p_env->NewObjectArray( 1, 133 // p_env->FindClass("java/lang/String"), jstr); 134 // if (args == 0) { 135 // OSL_TRACE( "Out of memory\n"); 136 // exit(1); 137 // } 138 // p_env->CallStaticVoidMethod( cls, methid, args); 139 140 141 jmethodID id = p_env->GetStaticMethodID( cls, "getInt", "()I"); 142 if( id) 143 { 144 // jint _i= p_env->CallStaticIntMethod(cls, id); 145 p_env->CallStaticIntMethod(cls, id); 146 } 147 148 if( p_env->ExceptionOccurred()){ 149 p_env->ExceptionDescribe(); 150 p_env->ExceptionClear(); 151 } 152 153 154 _jvm->DetachCurrentThread(); 155 return sal_True; 156 } 157 158 SAL_IMPLEMENT_MAIN() 159 { 160 Reference<XSimpleRegistry> xreg= createSimpleRegistry(); 161 xreg->open( OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb")), 162 sal_False, sal_False ); 163 164 Reference< XComponentContext > context= bootstrap_InitialComponentContext(xreg); 165 Reference<XMultiComponentFactory> fac= context->getServiceManager(); 166 Reference<XMultiServiceFactory> xMgr( fac, UNO_QUERY); 167 168 sal_Bool bSucc = sal_False; 169 try 170 { 171 OUString sImplReg(RTL_CONSTASCII_USTRINGPARAM( 172 "com.sun.star.registry.ImplementationRegistration")); 173 Reference<com::sun::star::registry::XImplementationRegistration> xImplReg( 174 xMgr->createInstance( sImplReg ), UNO_QUERY ); 175 OSL_ENSURE( xImplReg.is(), "### no impl reg!" ); 176 177 178 OUString sLibLoader( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.loader.SharedLibrary")); 179 OUString sJenLib( 180 RTL_CONSTASCII_USTRINGPARAM( "javavm.uno" SAL_DLLEXTENSION ) ); 181 xImplReg->registerImplementation( 182 sLibLoader, sJenLib, Reference< XSimpleRegistry >() ); 183 184 bSucc = testJavaVM( xMgr ); 185 } 186 catch (Exception & rExc) 187 { 188 OSL_ENSURE( sal_False, "### exception occured!" ); 189 OString aMsg( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) ); 190 OSL_TRACE( "### exception occured: " ); 191 OSL_TRACE( aMsg.getStr() ); 192 OSL_TRACE( "\n" ); 193 } 194 195 Reference< XComponent > xCompContext( context, UNO_QUERY ); 196 xCompContext->dispose(); 197 printf("javavm %s", bSucc ? "succeeded" : "failed"); 198 // cout << "javavm " << (bSucc ? "succeeded" : "failed") << " !" << endl; 199 return (bSucc ? 0 : -1); 200 } 201 202 203