1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_stoc.hxx"
30 
31 
32 #include <jni.h>
33 
34 //#include <iostream>
35 #include <stdio.h>
36 #include <sal/main.h>
37 #include <rtl/process.h>
38 
39 #include <cppuhelper/servicefactory.hxx>
40 #include <cppuhelper/weak.hxx>
41 #include <cppuhelper/bootstrap.hxx>
42 #include <osl/thread.h>
43 
44 #include <com/sun/star/registry/XSimpleRegistry.hpp>
45 #include <com/sun/star/lang/XComponent.hpp>
46 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
47 #include <com/sun/star/java/XJavaVM.hpp>
48 #include <com/sun/star/registry/XImplementationRegistration.hpp>
49 #include <com/sun/star/java/XJavaThreadRegister_11.hpp>
50 
51 #include <com/sun/star/uno/XCurrentContext.hpp>
52 #include <com/sun/star/task/XInteractionHandler.hpp>
53 #include <com/sun/star/task/XInteractionRequest.hpp>
54 #include <com/sun/star/task/XInteractionContinuation.hpp>
55 #include <com/sun/star/task/XInteractionAbort.hpp>
56 #include <com/sun/star/task/XInteractionRetry.hpp>
57 #include <com/sun/star/java/JavaNotConfiguredException.hpp>
58 #include <com/sun/star/java/MissingJavaRuntimeException.hpp>
59 #include <com/sun/star/java/JavaDisabledException.hpp>
60 #include <com/sun/star/java/JavaVMCreationFailureException.hpp>
61 #include <cppuhelper/implbase1.hxx>
62 #include <uno/current_context.hxx>
63 using namespace std;
64 using namespace rtl;
65 using namespace cppu;
66 using namespace com::sun::star::uno;
67 using namespace com::sun::star::lang;
68 //using namespace com::sun::star::reflection;
69 using namespace com::sun::star::lang;
70 using namespace com::sun::star::registry;
71 using namespace com::sun::star::java;
72 using namespace com::sun::star::task;
73 
74 #define OUSTR( x ) OUString(RTL_CONSTASCII_USTRINGPARAM( x ))
75 #define INTERACTION_HANDLER_NAME "java-vm.interaction-handler"
76 
77 class Context: public WeakImplHelper1<XCurrentContext>
78 {
79     virtual Any SAL_CALL getValueByName( const OUString& Name ) throw (RuntimeException);
80 };
81 
82 class InteractionHandler: public WeakImplHelper1<XInteractionHandler>
83 {
84     virtual void SAL_CALL handle( const Reference< XInteractionRequest >& Request )
85         throw (RuntimeException);
86 };
87 
88 Any SAL_CALL Context::getValueByName( const OUString& Name) throw (RuntimeException)
89 {
90     Any retVal;
91     if( Name.equals( OUSTR(INTERACTION_HANDLER_NAME)))
92     {
93         Reference<XInteractionHandler> handler( static_cast<XWeak*>(new InteractionHandler()),
94                                                 UNO_QUERY);
95         retVal <<= handler;
96     }
97     return retVal;
98 }
99 
100 void SAL_CALL InteractionHandler::handle( const Reference< XInteractionRequest >& Request )
101         throw (RuntimeException)
102 {
103     Any anyExc= Request->getRequest();
104     Sequence<Reference< XInteractionContinuation> >seqCont= Request->getContinuations();
105 
106     Reference<XInteractionAbort> abort;
107     Reference<XInteractionRetry> retry;
108 
109     for (sal_Int32 i= 0; i < seqCont.getLength(); i++)
110     {
111         abort= Reference<XInteractionAbort>::query( seqCont[i]);
112         if(abort.is())
113             break;
114     }
115     for (sal_Int32 i= 0; i < seqCont.getLength(); i++)
116     {
117         retry= Reference<XInteractionRetry>::query( seqCont[i]);
118         if(retry.is())
119             break;
120     }
121 
122 //     if( abort.is())
123 //         abort->select();
124 
125     static int cRetry= 0;
126 
127     if( cRetry++ == 5)
128     {
129         if( abort.is())
130             abort->select();
131         return;
132     }
133     if( retry.is())
134          retry->select();
135 }
136 
137 sal_Bool test1(const Reference< XMultiServiceFactory > & xMgr )
138 {
139     sal_Bool retVal= sal_True;
140 	setCurrentContext( Reference<XCurrentContext>( static_cast<XWeak*>(new Context()), UNO_QUERY));
141 
142   	OUString sVMService( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.java.JavaVirtualMachine"));
143 	Reference<XInterface> xXInt= xMgr->createInstance(sVMService);
144 	if( ! xXInt.is())
145 		return sal_False;
146 	Reference<XJavaVM> xVM( xXInt, UNO_QUERY);
147 	if( ! xVM.is())
148 		return sal_False;
149 
150 
151     sal_Int8 arId[16];
152     rtl_getGlobalProcessId((sal_uInt8*) arId);
153 
154 	Any anyVM;
155 	try
156 	{
157 		anyVM = xVM->getJavaVM( Sequence<sal_Int8>(arId, 16));
158     }
159     catch (JavaNotConfiguredException& e)
160     {
161         OString msg= OUStringToOString(e.Message, osl_getThreadTextEncoding());
162         printf("JavaNotConfiguredException: %s\n", msg.getStr());
163     }
164     catch (JavaVMCreationFailureException& e)
165     {
166         OString msg= OUStringToOString(e.Message, osl_getThreadTextEncoding());
167         printf("JavaVMCreationFailureException: %s\n", msg.getStr());
168     }
169     catch (MissingJavaRuntimeException& e)
170     {
171         OString msg= OUStringToOString(e.Message, osl_getThreadTextEncoding());
172         printf("MissingJavaRuntimeException: %s\n", msg.getStr());
173     }
174     catch (JavaDisabledException& e)
175     {
176         OString msg= OUStringToOString(e.Message, osl_getThreadTextEncoding());
177         printf("JavaDisabledException: %s\n", msg.getStr());
178     }
179     catch (RuntimeException & e)
180     {
181         OString msg= OUStringToOString(e.Message, osl_getThreadTextEncoding());
182         printf("###RuntimeException: %s\n", msg.getStr());
183         retVal= sal_False;
184     }
185     return retVal;
186 }
187 
188 SAL_IMPLEMENT_MAIN()
189 {
190 	Reference<XSimpleRegistry> xreg= createSimpleRegistry();
191 	xreg->open( OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb")),
192 							   sal_False, sal_False );
193 
194 	Reference< XComponentContext > context= bootstrap_InitialComponentContext(xreg);
195 	Reference<XMultiComponentFactory> fac= context->getServiceManager();
196 	Reference<XMultiServiceFactory> xMgr( fac, UNO_QUERY);
197 
198 	sal_Bool bSucc = sal_False;
199     bSucc= test1(xMgr);
200 	Reference< XComponent > xCompContext( context, UNO_QUERY );
201 	xCompContext->dispose();
202 	return (bSucc ? 0 : -1);
203 }
204 
205 
206