xref: /aoo42x/main/stoc/test/javavm/testjavavm.cxx (revision 86e1cf34)
1647a425cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3647a425cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4647a425cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5647a425cSAndrew Rist  * distributed with this work for additional information
6647a425cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7647a425cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8647a425cSAndrew Rist  * "License"); you may not use this file except in compliance
9647a425cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10647a425cSAndrew Rist  *
11647a425cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12647a425cSAndrew Rist  *
13647a425cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14647a425cSAndrew Rist  * software distributed under the License is distributed on an
15647a425cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16647a425cSAndrew Rist  * KIND, either express or implied.  See the License for the
17647a425cSAndrew Rist  * specific language governing permissions and limitations
18647a425cSAndrew Rist  * under the License.
19647a425cSAndrew Rist  *
20647a425cSAndrew Rist  *************************************************************/
21647a425cSAndrew Rist 
22647a425cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_stoc.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <jni.h>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir //#include <iostream>
31cdf0e10cSrcweir #include <stdio.h>
32cdf0e10cSrcweir #include <sal/main.h>
33cdf0e10cSrcweir #include <rtl/process.h>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <cppuhelper/servicefactory.hxx>
36cdf0e10cSrcweir #include <cppuhelper/weak.hxx>
37cdf0e10cSrcweir #include <cppuhelper/bootstrap.hxx>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include <com/sun/star/registry/XSimpleRegistry.hpp>
40cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
41cdf0e10cSrcweir #include <com/sun/star/lang/XMultiComponentFactory.hpp>
42cdf0e10cSrcweir #include <com/sun/star/java/XJavaVM.hpp>
43cdf0e10cSrcweir #include <com/sun/star/registry/XImplementationRegistration.hpp>
44cdf0e10cSrcweir #include <com/sun/star/java/XJavaThreadRegister_11.hpp>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir //#include <cppuhelper/implbase1.hxx>
47cdf0e10cSrcweir 
48cdf0e10cSrcweir using namespace std;
49cdf0e10cSrcweir using namespace rtl;
50cdf0e10cSrcweir using namespace cppu;
51cdf0e10cSrcweir using namespace com::sun::star::uno;
52cdf0e10cSrcweir using namespace com::sun::star::lang;
53cdf0e10cSrcweir //using namespace com::sun::star::reflection;
54cdf0e10cSrcweir using namespace com::sun::star::lang;
55cdf0e10cSrcweir using namespace com::sun::star::registry;
56cdf0e10cSrcweir using namespace com::sun::star::java;
57cdf0e10cSrcweir 
58cdf0e10cSrcweir 
testJavaVM(const Reference<XMultiServiceFactory> & xMgr)59cdf0e10cSrcweir sal_Bool testJavaVM(const Reference< XMultiServiceFactory > & xMgr )
60cdf0e10cSrcweir {
61cdf0e10cSrcweir 
62cdf0e10cSrcweir   	OUString sVMService( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.java.JavaVirtualMachine"));
63cdf0e10cSrcweir 	Reference<XInterface> xXInt= xMgr->createInstance(sVMService);
64cdf0e10cSrcweir 	if( ! xXInt.is())
65cdf0e10cSrcweir 		return sal_False;
66cdf0e10cSrcweir 	Reference<XJavaVM> xVM( xXInt, UNO_QUERY);
67cdf0e10cSrcweir 	if( ! xVM.is())
68cdf0e10cSrcweir 		return sal_False;
69cdf0e10cSrcweir 	Reference<XJavaThreadRegister_11> xreg11(xVM, UNO_QUERY);
70cdf0e10cSrcweir 	if( ! xreg11.is())
71cdf0e10cSrcweir 		return sal_False;
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 	sal_Int8 arId[16];
75cdf0e10cSrcweir 	rtl_getGlobalProcessId((sal_uInt8*) arId);
76cdf0e10cSrcweir 	Any anyVM = xVM->getJavaVM( Sequence<sal_Int8>(arId, 16));
77cdf0e10cSrcweir     if ( ! anyVM.hasValue())
78cdf0e10cSrcweir     {
79cdf0e10cSrcweir         OSL_ENSURE(0,"could not get Java VM");
80cdf0e10cSrcweir         return sal_False;
81cdf0e10cSrcweir     }
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 	sal_Bool b= xreg11->isThreadAttached();
84cdf0e10cSrcweir 	xreg11->registerThread();
85cdf0e10cSrcweir 	b= xreg11->isThreadAttached();
86cdf0e10cSrcweir 	xreg11->revokeThread();
87cdf0e10cSrcweir 	b= xreg11->isThreadAttached();
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 	b= xVM->isVMEnabled();
91cdf0e10cSrcweir 	b= xVM->isVMStarted();
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 	b= xVM->isVMEnabled();
95cdf0e10cSrcweir 	b= xVM->isVMStarted();
96cdf0e10cSrcweir 
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 	JavaVM* _jvm= *(JavaVM**) anyVM.getValue();
99cdf0e10cSrcweir 	JNIEnv *p_env;
100cdf0e10cSrcweir 	if( _jvm->AttachCurrentThread((void**) &p_env, 0))
101cdf0e10cSrcweir 		return sal_False;
102cdf0e10cSrcweir 
103cdf0e10cSrcweir //	jclass aJProg = p_env->FindClass("TestJavaVM");
104cdf0e10cSrcweir //	if( p_env->ExceptionOccurred()){
105cdf0e10cSrcweir //		p_env->ExceptionDescribe();
106cdf0e10cSrcweir //		p_env->ExceptionClear();
107cdf0e10cSrcweir //	}
108cdf0e10cSrcweir //
109cdf0e10cSrcweir //	jmethodID mid= p_env->GetStaticMethodID( aJProg,"main", "([Ljava/lang/String;)V");
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     jclass cls = p_env->FindClass( "TestJavaVM");
112cdf0e10cSrcweir     if (cls == 0) {
113cdf0e10cSrcweir         OSL_TRACE( "Can't find Prog class\n");
114cdf0e10cSrcweir         exit(1);
115cdf0e10cSrcweir     }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir //   jmethodID methid = p_env->GetStaticMethodID( cls, "main", "([Ljava/lang/String;)V");
118cdf0e10cSrcweir //    if (methid == 0) {
119cdf0e10cSrcweir //        OSL_TRACE("Can't find Prog.main\n");
120cdf0e10cSrcweir //        exit(1);
121cdf0e10cSrcweir //    }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir //    jstring jstr = p_env->NewStringUTF(" from C!");
124cdf0e10cSrcweir //    if (jstr == 0) {
125cdf0e10cSrcweir //        OSL_TRACE("Out of memory\n");
126cdf0e10cSrcweir //        exit(1);
127cdf0e10cSrcweir //    }
128cdf0e10cSrcweir //    jobjectArray args = p_env->NewObjectArray( 1,
129cdf0e10cSrcweir //                        p_env->FindClass("java/lang/String"), jstr);
130cdf0e10cSrcweir //    if (args == 0) {
131cdf0e10cSrcweir //        OSL_TRACE( "Out of memory\n");
132cdf0e10cSrcweir //        exit(1);
133cdf0e10cSrcweir //    }
134cdf0e10cSrcweir //    p_env->CallStaticVoidMethod( cls, methid, args);
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 	jmethodID id = p_env->GetStaticMethodID( cls, "getInt", "()I");
138cdf0e10cSrcweir 	if( id)
139cdf0e10cSrcweir 	{
140cdf0e10cSrcweir //		jint _i= p_env->CallStaticIntMethod(cls, id);
141cdf0e10cSrcweir 		p_env->CallStaticIntMethod(cls, id);
142cdf0e10cSrcweir 	}
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 	if( p_env->ExceptionOccurred()){
145cdf0e10cSrcweir 		p_env->ExceptionDescribe();
146cdf0e10cSrcweir 		p_env->ExceptionClear();
147cdf0e10cSrcweir 	}
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 	_jvm->DetachCurrentThread();
151cdf0e10cSrcweir 	return sal_True;
152cdf0e10cSrcweir }
153cdf0e10cSrcweir 
SAL_IMPLEMENT_MAIN()154cdf0e10cSrcweir SAL_IMPLEMENT_MAIN()
155cdf0e10cSrcweir {
156cdf0e10cSrcweir 	Reference<XSimpleRegistry> xreg= createSimpleRegistry();
157cdf0e10cSrcweir 	xreg->open( OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb")),
158cdf0e10cSrcweir 							   sal_False, sal_False );
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 	Reference< XComponentContext > context= bootstrap_InitialComponentContext(xreg);
161cdf0e10cSrcweir 	Reference<XMultiComponentFactory> fac= context->getServiceManager();
162cdf0e10cSrcweir 	Reference<XMultiServiceFactory> xMgr( fac, UNO_QUERY);
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 	sal_Bool bSucc = sal_False;
165cdf0e10cSrcweir 	try
166cdf0e10cSrcweir 	{
167cdf0e10cSrcweir 		OUString sImplReg(RTL_CONSTASCII_USTRINGPARAM(
168cdf0e10cSrcweir 			"com.sun.star.registry.ImplementationRegistration"));
169cdf0e10cSrcweir 		Reference<com::sun::star::registry::XImplementationRegistration> xImplReg(
170cdf0e10cSrcweir 			xMgr->createInstance( sImplReg ), UNO_QUERY );
171cdf0e10cSrcweir 		OSL_ENSURE( xImplReg.is(), "### no impl reg!" );
172cdf0e10cSrcweir 
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 		OUString sLibLoader( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.loader.SharedLibrary"));
175cdf0e10cSrcweir 		OUString sJenLib(
176cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM( "javavm.uno" SAL_DLLEXTENSION ) );
177cdf0e10cSrcweir 		xImplReg->registerImplementation(
178cdf0e10cSrcweir 			sLibLoader, sJenLib, Reference< XSimpleRegistry >() );
179cdf0e10cSrcweir 
180cdf0e10cSrcweir 		bSucc = testJavaVM( xMgr );
181cdf0e10cSrcweir 	}
182cdf0e10cSrcweir 	catch (Exception & rExc)
183cdf0e10cSrcweir 	{
184*86e1cf34SPedro Giffuni 		OSL_ENSURE( sal_False, "### exception occurred!" );
185cdf0e10cSrcweir 		OString aMsg( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
186*86e1cf34SPedro Giffuni 		OSL_TRACE( "### exception occurred: " );
187cdf0e10cSrcweir 		OSL_TRACE( aMsg.getStr() );
188cdf0e10cSrcweir 		OSL_TRACE( "\n" );
189cdf0e10cSrcweir 	}
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 	Reference< XComponent > xCompContext( context, UNO_QUERY );
192cdf0e10cSrcweir 	xCompContext->dispose();
193cdf0e10cSrcweir 	printf("javavm %s", bSucc ? "succeeded" : "failed");
194cdf0e10cSrcweir //	cout << "javavm " << (bSucc ? "succeeded" : "failed") << " !" << endl;
195cdf0e10cSrcweir 	return (bSucc ? 0 : -1);
196cdf0e10cSrcweir }
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 
199