xref: /trunk/main/stoc/test/javavm/jvm_interaction/interactionhandler.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 );
76 };
77 
78 class InteractionHandler: public WeakImplHelper1<XInteractionHandler>
79 {
80     virtual void SAL_CALL handle( const Reference< XInteractionRequest >& Request );
81 };
82 
getValueByName(const OUString & Name)83 Any SAL_CALL Context::getValueByName( const OUString& Name)
84 {
85     Any retVal;
86     if( Name.equals( OUSTR(INTERACTION_HANDLER_NAME)))
87     {
88         Reference<XInteractionHandler> handler( static_cast<XWeak*>(new InteractionHandler()),
89                                                 UNO_QUERY);
90         retVal <<= handler;
91     }
92     return retVal;
93 }
94 
handle(const Reference<XInteractionRequest> & Request)95 void SAL_CALL InteractionHandler::handle( const Reference< XInteractionRequest >& Request )
96 {
97     Any anyExc= Request->getRequest();
98     Sequence<Reference< XInteractionContinuation> >seqCont= Request->getContinuations();
99 
100     Reference<XInteractionAbort> abort;
101     Reference<XInteractionRetry> retry;
102 
103     for (sal_Int32 i= 0; i < seqCont.getLength(); i++)
104     {
105         abort= Reference<XInteractionAbort>::query( seqCont[i]);
106         if(abort.is())
107             break;
108     }
109     for (sal_Int32 i= 0; i < seqCont.getLength(); i++)
110     {
111         retry= Reference<XInteractionRetry>::query( seqCont[i]);
112         if(retry.is())
113             break;
114     }
115 
116 //     if( abort.is())
117 //         abort->select();
118 
119     static int cRetry= 0;
120 
121     if( cRetry++ == 5)
122     {
123         if( abort.is())
124             abort->select();
125         return;
126     }
127     if( retry.is())
128          retry->select();
129 }
130 
test1(const Reference<XMultiServiceFactory> & xMgr)131 sal_Bool test1(const Reference< XMultiServiceFactory > & xMgr )
132 {
133     sal_Bool retVal= sal_True;
134     setCurrentContext( Reference<XCurrentContext>( static_cast<XWeak*>(new Context()), UNO_QUERY));
135 
136     OUString sVMService( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.java.JavaVirtualMachine"));
137     Reference<XInterface> xXInt= xMgr->createInstance(sVMService);
138     if( ! xXInt.is())
139         return sal_False;
140     Reference<XJavaVM> xVM( xXInt, UNO_QUERY);
141     if( ! xVM.is())
142         return sal_False;
143 
144 
145     sal_Int8 arId[16];
146     rtl_getGlobalProcessId((sal_uInt8*) arId);
147 
148     Any anyVM;
149     try
150     {
151         anyVM = xVM->getJavaVM( Sequence<sal_Int8>(arId, 16));
152     }
153     catch (JavaNotConfiguredException& e)
154     {
155         OString msg= OUStringToOString(e.Message, osl_getThreadTextEncoding());
156         printf("JavaNotConfiguredException: %s\n", msg.getStr());
157     }
158     catch (JavaVMCreationFailureException& e)
159     {
160         OString msg= OUStringToOString(e.Message, osl_getThreadTextEncoding());
161         printf("JavaVMCreationFailureException: %s\n", msg.getStr());
162     }
163     catch (MissingJavaRuntimeException& e)
164     {
165         OString msg= OUStringToOString(e.Message, osl_getThreadTextEncoding());
166         printf("MissingJavaRuntimeException: %s\n", msg.getStr());
167     }
168     catch (JavaDisabledException& e)
169     {
170         OString msg= OUStringToOString(e.Message, osl_getThreadTextEncoding());
171         printf("JavaDisabledException: %s\n", msg.getStr());
172     }
173     catch (RuntimeException & e)
174     {
175         OString msg= OUStringToOString(e.Message, osl_getThreadTextEncoding());
176         printf("###RuntimeException: %s\n", msg.getStr());
177         retVal= sal_False;
178     }
179     return retVal;
180 }
181 
SAL_IMPLEMENT_MAIN()182 SAL_IMPLEMENT_MAIN()
183 {
184     Reference<XSimpleRegistry> xreg= createSimpleRegistry();
185     xreg->open( OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb")),
186                                sal_False, sal_False );
187 
188     Reference< XComponentContext > context= bootstrap_InitialComponentContext(xreg);
189     Reference<XMultiComponentFactory> fac= context->getServiceManager();
190     Reference<XMultiServiceFactory> xMgr( fac, UNO_QUERY);
191 
192     sal_Bool bSucc = sal_False;
193     bSucc= test1(xMgr);
194     Reference< XComponent > xCompContext( context, UNO_QUERY );
195     xCompContext->dispose();
196     return (bSucc ? 0 : -1);
197 }
198