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