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