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 #include <osl/thread.h>
39 
40 #include <com/sun/star/registry/XSimpleRegistry.hpp>
41 #include <com/sun/star/lang/XComponent.hpp>
42 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
43 #include <com/sun/star/java/XJavaVM.hpp>
44 #include <com/sun/star/registry/XImplementationRegistration.hpp>
45 #include <com/sun/star/java/XJavaThreadRegister_11.hpp>
46 
47 #include <com/sun/star/uno/XCurrentContext.hpp>
48 #include <com/sun/star/task/XInteractionHandler.hpp>
49 #include <com/sun/star/task/XInteractionRequest.hpp>
50 #include <com/sun/star/task/XInteractionContinuation.hpp>
51 #include <com/sun/star/task/XInteractionAbort.hpp>
52 #include <com/sun/star/task/XInteractionRetry.hpp>
53 #include <com/sun/star/java/JavaNotConfiguredException.hpp>
54 #include <com/sun/star/java/MissingJavaRuntimeException.hpp>
55 #include <com/sun/star/java/JavaDisabledException.hpp>
56 #include <com/sun/star/java/JavaVMCreationFailureException.hpp>
57 #include <cppuhelper/implbase1.hxx>
58 #include <uno/current_context.hxx>
59 using namespace std;
60 using namespace rtl;
61 using namespace cppu;
62 using namespace com::sun::star::uno;
63 using namespace com::sun::star::lang;
64 //using namespace com::sun::star::reflection;
65 using namespace com::sun::star::lang;
66 using namespace com::sun::star::registry;
67 using namespace com::sun::star::java;
68 using namespace com::sun::star::task;
69 
70 #define OUSTR( x ) OUString(RTL_CONSTASCII_USTRINGPARAM( x ))
71 #define INTERACTION_HANDLER_NAME "java-vm.interaction-handler"
72 
73 class Context: public WeakImplHelper1<XCurrentContext>
74 {
75     virtual Any SAL_CALL getValueByName( const OUString& Name ) throw (RuntimeException);
76 };
77 
78 class InteractionHandler: public WeakImplHelper1<XInteractionHandler>
79 {
80     virtual void SAL_CALL handle( const Reference< XInteractionRequest >& Request )
81         throw (RuntimeException);
82 };
83 
getValueByName(const OUString & Name)84 Any SAL_CALL Context::getValueByName( const OUString& Name) throw (RuntimeException)
85 {
86     Any retVal;
87     if( Name.equals( OUSTR(INTERACTION_HANDLER_NAME)))
88     {
89         Reference<XInteractionHandler> handler( static_cast<XWeak*>(new InteractionHandler()),
90                                                 UNO_QUERY);
91         retVal <<= handler;
92     }
93     return retVal;
94 }
95 
handle(const Reference<XInteractionRequest> & Request)96 void SAL_CALL InteractionHandler::handle( const Reference< XInteractionRequest >& Request )
97         throw (RuntimeException)
98 {
99     Any anyExc= Request->getRequest();
100     Sequence<Reference< XInteractionContinuation> >seqCont= Request->getContinuations();
101 
102     Reference<XInteractionAbort> abort;
103     Reference<XInteractionRetry> retry;
104 
105     for (sal_Int32 i= 0; i < seqCont.getLength(); i++)
106     {
107         abort= Reference<XInteractionAbort>::query( seqCont[i]);
108         if(abort.is())
109             break;
110     }
111     for (sal_Int32 i= 0; i < seqCont.getLength(); i++)
112     {
113         retry= Reference<XInteractionRetry>::query( seqCont[i]);
114         if(retry.is())
115             break;
116     }
117 
118 //     if( abort.is())
119 //         abort->select();
120 
121     static int cRetry= 0;
122 
123     if( cRetry++ == 5)
124     {
125         if( abort.is())
126             abort->select();
127         return;
128     }
129     if( retry.is())
130          retry->select();
131 }
132 
test1(const Reference<XMultiServiceFactory> & xMgr)133 sal_Bool test1(const Reference< XMultiServiceFactory > & xMgr )
134 {
135     sal_Bool retVal= sal_True;
136 	setCurrentContext( Reference<XCurrentContext>( static_cast<XWeak*>(new Context()), UNO_QUERY));
137 
138   	OUString sVMService( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.java.JavaVirtualMachine"));
139 	Reference<XInterface> xXInt= xMgr->createInstance(sVMService);
140 	if( ! xXInt.is())
141 		return sal_False;
142 	Reference<XJavaVM> xVM( xXInt, UNO_QUERY);
143 	if( ! xVM.is())
144 		return sal_False;
145 
146 
147     sal_Int8 arId[16];
148     rtl_getGlobalProcessId((sal_uInt8*) arId);
149 
150 	Any anyVM;
151 	try
152 	{
153 		anyVM = xVM->getJavaVM( Sequence<sal_Int8>(arId, 16));
154     }
155     catch (JavaNotConfiguredException& e)
156     {
157         OString msg= OUStringToOString(e.Message, osl_getThreadTextEncoding());
158         printf("JavaNotConfiguredException: %s\n", msg.getStr());
159     }
160     catch (JavaVMCreationFailureException& e)
161     {
162         OString msg= OUStringToOString(e.Message, osl_getThreadTextEncoding());
163         printf("JavaVMCreationFailureException: %s\n", msg.getStr());
164     }
165     catch (MissingJavaRuntimeException& e)
166     {
167         OString msg= OUStringToOString(e.Message, osl_getThreadTextEncoding());
168         printf("MissingJavaRuntimeException: %s\n", msg.getStr());
169     }
170     catch (JavaDisabledException& e)
171     {
172         OString msg= OUStringToOString(e.Message, osl_getThreadTextEncoding());
173         printf("JavaDisabledException: %s\n", msg.getStr());
174     }
175     catch (RuntimeException & e)
176     {
177         OString msg= OUStringToOString(e.Message, osl_getThreadTextEncoding());
178         printf("###RuntimeException: %s\n", msg.getStr());
179         retVal= sal_False;
180     }
181     return retVal;
182 }
183 
SAL_IMPLEMENT_MAIN()184 SAL_IMPLEMENT_MAIN()
185 {
186 	Reference<XSimpleRegistry> xreg= createSimpleRegistry();
187 	xreg->open( OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb")),
188 							   sal_False, sal_False );
189 
190 	Reference< XComponentContext > context= bootstrap_InitialComponentContext(xreg);
191 	Reference<XMultiComponentFactory> fac= context->getServiceManager();
192 	Reference<XMultiServiceFactory> xMgr( fac, UNO_QUERY);
193 
194 	sal_Bool bSucc = sal_False;
195     bSucc= test1(xMgr);
196 	Reference< XComponent > xCompContext( context, UNO_QUERY );
197 	xCompContext->dispose();
198 	return (bSucc ? 0 : -1);
199 }
200 
201 
202