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