1*647a425cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*647a425cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*647a425cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*647a425cSAndrew Rist  * distributed with this work for additional information
6*647a425cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*647a425cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*647a425cSAndrew Rist  * "License"); you may not use this file except in compliance
9*647a425cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*647a425cSAndrew Rist  *
11*647a425cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*647a425cSAndrew Rist  *
13*647a425cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*647a425cSAndrew Rist  * software distributed under the License is distributed on an
15*647a425cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*647a425cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*647a425cSAndrew Rist  * specific language governing permissions and limitations
18*647a425cSAndrew Rist  * under the License.
19*647a425cSAndrew Rist  *
20*647a425cSAndrew Rist  *************************************************************/
21*647a425cSAndrew Rist 
22*647a425cSAndrew 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 #include <osl/thread.h>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #include <com/sun/star/registry/XSimpleRegistry.hpp>
41cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
42cdf0e10cSrcweir #include <com/sun/star/lang/XMultiComponentFactory.hpp>
43cdf0e10cSrcweir #include <com/sun/star/java/XJavaVM.hpp>
44cdf0e10cSrcweir #include <com/sun/star/registry/XImplementationRegistration.hpp>
45cdf0e10cSrcweir #include <com/sun/star/java/XJavaThreadRegister_11.hpp>
46cdf0e10cSrcweir 
47cdf0e10cSrcweir #include <com/sun/star/uno/XCurrentContext.hpp>
48cdf0e10cSrcweir #include <com/sun/star/task/XInteractionHandler.hpp>
49cdf0e10cSrcweir #include <com/sun/star/task/XInteractionRequest.hpp>
50cdf0e10cSrcweir #include <com/sun/star/task/XInteractionContinuation.hpp>
51cdf0e10cSrcweir #include <com/sun/star/task/XInteractionAbort.hpp>
52cdf0e10cSrcweir #include <com/sun/star/task/XInteractionRetry.hpp>
53cdf0e10cSrcweir #include <com/sun/star/java/JavaNotConfiguredException.hpp>
54cdf0e10cSrcweir #include <com/sun/star/java/MissingJavaRuntimeException.hpp>
55cdf0e10cSrcweir #include <com/sun/star/java/JavaDisabledException.hpp>
56cdf0e10cSrcweir #include <com/sun/star/java/JavaVMCreationFailureException.hpp>
57cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
58cdf0e10cSrcweir #include <uno/current_context.hxx>
59cdf0e10cSrcweir using namespace std;
60cdf0e10cSrcweir using namespace rtl;
61cdf0e10cSrcweir using namespace cppu;
62cdf0e10cSrcweir using namespace com::sun::star::uno;
63cdf0e10cSrcweir using namespace com::sun::star::lang;
64cdf0e10cSrcweir //using namespace com::sun::star::reflection;
65cdf0e10cSrcweir using namespace com::sun::star::lang;
66cdf0e10cSrcweir using namespace com::sun::star::registry;
67cdf0e10cSrcweir using namespace com::sun::star::java;
68cdf0e10cSrcweir using namespace com::sun::star::task;
69cdf0e10cSrcweir 
70cdf0e10cSrcweir #define OUSTR( x ) OUString(RTL_CONSTASCII_USTRINGPARAM( x ))
71cdf0e10cSrcweir #define INTERACTION_HANDLER_NAME "java-vm.interaction-handler"
72cdf0e10cSrcweir 
73cdf0e10cSrcweir class Context: public WeakImplHelper1<XCurrentContext>
74cdf0e10cSrcweir {
75cdf0e10cSrcweir     virtual Any SAL_CALL getValueByName( const OUString& Name ) throw (RuntimeException);
76cdf0e10cSrcweir };
77cdf0e10cSrcweir 
78cdf0e10cSrcweir class InteractionHandler: public WeakImplHelper1<XInteractionHandler>
79cdf0e10cSrcweir {
80cdf0e10cSrcweir     virtual void SAL_CALL handle( const Reference< XInteractionRequest >& Request )
81cdf0e10cSrcweir         throw (RuntimeException);
82cdf0e10cSrcweir };
83cdf0e10cSrcweir 
getValueByName(const OUString & Name)84cdf0e10cSrcweir Any SAL_CALL Context::getValueByName( const OUString& Name) throw (RuntimeException)
85cdf0e10cSrcweir {
86cdf0e10cSrcweir     Any retVal;
87cdf0e10cSrcweir     if( Name.equals( OUSTR(INTERACTION_HANDLER_NAME)))
88cdf0e10cSrcweir     {
89cdf0e10cSrcweir         Reference<XInteractionHandler> handler( static_cast<XWeak*>(new InteractionHandler()),
90cdf0e10cSrcweir                                                 UNO_QUERY);
91cdf0e10cSrcweir         retVal <<= handler;
92cdf0e10cSrcweir     }
93cdf0e10cSrcweir     return retVal;
94cdf0e10cSrcweir }
95cdf0e10cSrcweir 
handle(const Reference<XInteractionRequest> & Request)96cdf0e10cSrcweir void SAL_CALL InteractionHandler::handle( const Reference< XInteractionRequest >& Request )
97cdf0e10cSrcweir         throw (RuntimeException)
98cdf0e10cSrcweir {
99cdf0e10cSrcweir     Any anyExc= Request->getRequest();
100cdf0e10cSrcweir     Sequence<Reference< XInteractionContinuation> >seqCont= Request->getContinuations();
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     Reference<XInteractionAbort> abort;
103cdf0e10cSrcweir     Reference<XInteractionRetry> retry;
104cdf0e10cSrcweir 
105cdf0e10cSrcweir     for (sal_Int32 i= 0; i < seqCont.getLength(); i++)
106cdf0e10cSrcweir     {
107cdf0e10cSrcweir         abort= Reference<XInteractionAbort>::query( seqCont[i]);
108cdf0e10cSrcweir         if(abort.is())
109cdf0e10cSrcweir             break;
110cdf0e10cSrcweir     }
111cdf0e10cSrcweir     for (sal_Int32 i= 0; i < seqCont.getLength(); i++)
112cdf0e10cSrcweir     {
113cdf0e10cSrcweir         retry= Reference<XInteractionRetry>::query( seqCont[i]);
114cdf0e10cSrcweir         if(retry.is())
115cdf0e10cSrcweir             break;
116cdf0e10cSrcweir     }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir //     if( abort.is())
119cdf0e10cSrcweir //         abort->select();
120cdf0e10cSrcweir 
121cdf0e10cSrcweir     static int cRetry= 0;
122cdf0e10cSrcweir 
123cdf0e10cSrcweir     if( cRetry++ == 5)
124cdf0e10cSrcweir     {
125cdf0e10cSrcweir         if( abort.is())
126cdf0e10cSrcweir             abort->select();
127cdf0e10cSrcweir         return;
128cdf0e10cSrcweir     }
129cdf0e10cSrcweir     if( retry.is())
130cdf0e10cSrcweir          retry->select();
131cdf0e10cSrcweir }
132cdf0e10cSrcweir 
test1(const Reference<XMultiServiceFactory> & xMgr)133cdf0e10cSrcweir sal_Bool test1(const Reference< XMultiServiceFactory > & xMgr )
134cdf0e10cSrcweir {
135cdf0e10cSrcweir     sal_Bool retVal= sal_True;
136cdf0e10cSrcweir 	setCurrentContext( Reference<XCurrentContext>( static_cast<XWeak*>(new Context()), UNO_QUERY));
137cdf0e10cSrcweir 
138cdf0e10cSrcweir   	OUString sVMService( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.java.JavaVirtualMachine"));
139cdf0e10cSrcweir 	Reference<XInterface> xXInt= xMgr->createInstance(sVMService);
140cdf0e10cSrcweir 	if( ! xXInt.is())
141cdf0e10cSrcweir 		return sal_False;
142cdf0e10cSrcweir 	Reference<XJavaVM> xVM( xXInt, UNO_QUERY);
143cdf0e10cSrcweir 	if( ! xVM.is())
144cdf0e10cSrcweir 		return sal_False;
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 
147cdf0e10cSrcweir     sal_Int8 arId[16];
148cdf0e10cSrcweir     rtl_getGlobalProcessId((sal_uInt8*) arId);
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 	Any anyVM;
151cdf0e10cSrcweir 	try
152cdf0e10cSrcweir 	{
153cdf0e10cSrcweir 		anyVM = xVM->getJavaVM( Sequence<sal_Int8>(arId, 16));
154cdf0e10cSrcweir     }
155cdf0e10cSrcweir     catch (JavaNotConfiguredException& e)
156cdf0e10cSrcweir     {
157cdf0e10cSrcweir         OString msg= OUStringToOString(e.Message, osl_getThreadTextEncoding());
158cdf0e10cSrcweir         printf("JavaNotConfiguredException: %s\n", msg.getStr());
159cdf0e10cSrcweir     }
160cdf0e10cSrcweir     catch (JavaVMCreationFailureException& e)
161cdf0e10cSrcweir     {
162cdf0e10cSrcweir         OString msg= OUStringToOString(e.Message, osl_getThreadTextEncoding());
163cdf0e10cSrcweir         printf("JavaVMCreationFailureException: %s\n", msg.getStr());
164cdf0e10cSrcweir     }
165cdf0e10cSrcweir     catch (MissingJavaRuntimeException& e)
166cdf0e10cSrcweir     {
167cdf0e10cSrcweir         OString msg= OUStringToOString(e.Message, osl_getThreadTextEncoding());
168cdf0e10cSrcweir         printf("MissingJavaRuntimeException: %s\n", msg.getStr());
169cdf0e10cSrcweir     }
170cdf0e10cSrcweir     catch (JavaDisabledException& e)
171cdf0e10cSrcweir     {
172cdf0e10cSrcweir         OString msg= OUStringToOString(e.Message, osl_getThreadTextEncoding());
173cdf0e10cSrcweir         printf("JavaDisabledException: %s\n", msg.getStr());
174cdf0e10cSrcweir     }
175cdf0e10cSrcweir     catch (RuntimeException & e)
176cdf0e10cSrcweir     {
177cdf0e10cSrcweir         OString msg= OUStringToOString(e.Message, osl_getThreadTextEncoding());
178cdf0e10cSrcweir         printf("###RuntimeException: %s\n", msg.getStr());
179cdf0e10cSrcweir         retVal= sal_False;
180cdf0e10cSrcweir     }
181cdf0e10cSrcweir     return retVal;
182cdf0e10cSrcweir }
183cdf0e10cSrcweir 
SAL_IMPLEMENT_MAIN()184cdf0e10cSrcweir SAL_IMPLEMENT_MAIN()
185cdf0e10cSrcweir {
186cdf0e10cSrcweir 	Reference<XSimpleRegistry> xreg= createSimpleRegistry();
187cdf0e10cSrcweir 	xreg->open( OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb")),
188cdf0e10cSrcweir 							   sal_False, sal_False );
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 	Reference< XComponentContext > context= bootstrap_InitialComponentContext(xreg);
191cdf0e10cSrcweir 	Reference<XMultiComponentFactory> fac= context->getServiceManager();
192cdf0e10cSrcweir 	Reference<XMultiServiceFactory> xMgr( fac, UNO_QUERY);
193cdf0e10cSrcweir 
194cdf0e10cSrcweir 	sal_Bool bSucc = sal_False;
195cdf0e10cSrcweir     bSucc= test1(xMgr);
196cdf0e10cSrcweir 	Reference< XComponent > xCompContext( context, UNO_QUERY );
197cdf0e10cSrcweir 	xCompContext->dispose();
198cdf0e10cSrcweir 	return (bSucc ? 0 : -1);
199cdf0e10cSrcweir }
200cdf0e10cSrcweir 
201cdf0e10cSrcweir 
202