| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | /* |
| 3 | * This file is part of the LibreOffice project. |
| 4 | * |
| 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | * |
| 9 | * This file incorporates work covered by the following license notice: |
| 10 | * |
| 11 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 12 | * contributor license agreements. See the NOTICE file distributed |
| 13 | * with this work for additional information regarding copyright |
| 14 | * ownership. The ASF licenses this file to you under the Apache |
| 15 | * License, Version 2.0 (the "License"); you may not use this file |
| 16 | * except in compliance with the License. You may obtain a copy of |
| 17 | * the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 18 | */ |
| 19 | |
| 20 | |
| 21 | #include <jni.h> |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <sal/main.h> |
| 25 | #include <rtl/process.h> |
| 26 | |
| 27 | #include <cppuhelper/servicefactory.hxx> |
| 28 | #include <cppuhelper/weak.hxx> |
| 29 | #include <cppuhelper/bootstrap.hxx> |
| 30 | #include <osl/thread.h> |
| 31 | |
| 32 | #include <com/sun/star/registry/XSimpleRegistry.hpp> |
| 33 | #include <com/sun/star/lang/XComponent.hpp> |
| 34 | #include <com/sun/star/lang/XMultiComponentFactory.hpp> |
| 35 | #include <com/sun/star/java/XJavaVM.hpp> |
| 36 | #include <com/sun/star/registry/XImplementationRegistration.hpp> |
| 37 | #include <com/sun/star/java/XJavaThreadRegister_11.hpp> |
| 38 | |
| 39 | #include <com/sun/star/uno/XCurrentContext.hpp> |
| 40 | #include <com/sun/star/task/XInteractionHandler.hpp> |
| 41 | #include <com/sun/star/task/XInteractionRequest.hpp> |
| 42 | #include <com/sun/star/task/XInteractionContinuation.hpp> |
| 43 | #include <com/sun/star/task/XInteractionAbort.hpp> |
| 44 | #include <com/sun/star/task/XInteractionRetry.hpp> |
| 45 | #include <com/sun/star/java/JavaNotConfiguredException.hpp> |
| 46 | #include <com/sun/star/java/MissingJavaRuntimeException.hpp> |
| 47 | #include <com/sun/star/java/JavaDisabledException.hpp> |
| 48 | #include <com/sun/star/java/JavaVMCreationFailureException.hpp> |
| 49 | #include <cppuhelper/implbase.hxx> |
| 50 | #include <uno/current_context.hxx> |
| 51 | |
| 52 | using namespace cppu; |
| 53 | using namespace css::uno; |
| 54 | using namespace css::lang; |
| 55 | using namespace css::registry; |
| 56 | using namespace css::java; |
| 57 | using namespace css::task; |
| 58 | |
| 59 | |
| 60 | #define INTERACTION_HANDLER_NAME "java-vm.interaction-handler" |
| 61 | |
| 62 | class Context: public WeakImplHelper<XCurrentContext> |
| 63 | { |
| 64 | virtual Any SAL_CALL getValueByName( const OUString& Name ) throw (RuntimeException); |
| 65 | }; |
| 66 | |
| 67 | class InteractionHandler: public WeakImplHelper<XInteractionHandler> |
| 68 | { |
| 69 | virtual void SAL_CALL handle( const Reference< XInteractionRequest >& Request ) |
| 70 | throw (RuntimeException); |
| 71 | }; |
| 72 | |
| 73 | Any SAL_CALL Context::getValueByName( const OUString& Name) throw (RuntimeException) |
| 74 | { |
| 75 | Any retVal; |
| 76 | if( Name.equals( INTERACTION_HANDLER_NAME)) |
| 77 | { |
| 78 | Reference<XInteractionHandler> handler(new InteractionHandler()); |
| 79 | retVal <<= handler; |
| 80 | } |
| 81 | return retVal; |
| 82 | } |
| 83 | |
| 84 | void SAL_CALL InteractionHandler::handle( const Reference< XInteractionRequest >& Request ) |
| 85 | throw (RuntimeException) |
| 86 | { |
| 87 | Any anyExc= Request->getRequest(); |
| 88 | Sequence<Reference< XInteractionContinuation> >seqCont= Request->getContinuations(); |
| 89 | |
| 90 | Reference<XInteractionAbort> abort; |
| 91 | Reference<XInteractionRetry> retry; |
| 92 | |
| 93 | for (sal_Int32 i= 0; i < seqCont.getLength(); i++) |
| 94 | { |
| 95 | abort.set( seqCont[i], UNO_QUERY ); |
| 96 | if(abort.is()) |
| 97 | break; |
| 98 | } |
| 99 | for (sal_Int32 i= 0; i < seqCont.getLength(); i++) |
| 100 | { |
| 101 | retry.set( seqCont[i], UNO_QUERY ); |
| 102 | if(retry.is()) |
| 103 | break; |
| 104 | } |
| 105 | |
| 106 | static int cRetry= 0; |
| 107 | |
| 108 | if( cRetry++ == 5) |
| 109 | { |
| 110 | if( abort.is()) |
| 111 | abort->select(); |
| 112 | return; |
| 113 | } |
| 114 | if( retry.is()) |
| 115 | retry->select(); |
| 116 | } |
| 117 | |
| 118 | sal_Bool test1(const Reference< XMultiServiceFactory > & xMgr ) |
| 119 | { |
| 120 | sal_Bool retVal= sal_True; |
| 121 | setCurrentContext(Reference<XCurrentContext>(new Context()); |
| 122 | |
| 123 | OUString sVMService("com.sun.star.java.JavaVirtualMachine"); |
| 124 | Reference<XInterface> xXInt= xMgr->createInstance(sVMService); |
| 125 | if( ! xXInt.is()) |
| 126 | return sal_False; |
| 127 | Reference<XJavaVM> xVM( xXInt, UNO_QUERY); |
| 128 | if( ! xVM.is()) |
| 129 | return sal_False; |
| 130 | |
| 131 | |
| 132 | sal_Int8 arId[16]; |
| 133 | rtl_getGlobalProcessId((sal_uInt8*) arId); |
| 134 | |
| 135 | Any anyVM; |
| 136 | try |
| 137 | { |
| 138 | anyVM = xVM->getJavaVM( Sequence<sal_Int8>(arId, 16)); |
| 139 | } |
| 140 | catch (const JavaNotConfiguredException& e) |
| 141 | { |
| 142 | OString msg= OUStringToOString(e.Message, osl_getThreadTextEncoding()); |
| 143 | printf("JavaNotConfiguredException: %s\n", msg.getStr()); |
| 144 | } |
| 145 | catch (const JavaVMCreationFailureException& e) |
| 146 | { |
| 147 | OString msg= OUStringToOString(e.Message, osl_getThreadTextEncoding()); |
| 148 | printf("JavaVMCreationFailureException: %s\n", msg.getStr()); |
| 149 | } |
| 150 | catch (const MissingJavaRuntimeException& e) |
| 151 | { |
| 152 | OString msg= OUStringToOString(e.Message, osl_getThreadTextEncoding()); |
| 153 | printf("MissingJavaRuntimeException: %s\n", msg.getStr()); |
| 154 | } |
| 155 | catch (const JavaDisabledException& e) |
| 156 | { |
| 157 | OString msg= OUStringToOString(e.Message, osl_getThreadTextEncoding()); |
| 158 | printf("JavaDisabledException: %s\n", msg.getStr()); |
| 159 | } |
| 160 | catch (const RuntimeException & e) |
| 161 | { |
| 162 | OString msg= OUStringToOString(e.Message, osl_getThreadTextEncoding()); |
| 163 | printf("###RuntimeException: %s\n", msg.getStr()); |
| 164 | retVal= sal_False; |
| 165 | } |
| 166 | return retVal; |
| 167 | } |
| 168 | |
| 169 | SAL_IMPLEMENT_MAIN() |
| 170 | { |
| 171 | Reference<XSimpleRegistry> xreg= createSimpleRegistry(); |
| 172 | xreg->open( OUString("applicat.rdb"), |
| 173 | sal_False, sal_False ); |
| 174 | |
| 175 | Reference< XComponentContext > context= bootstrap_InitialComponentContext(xreg); |
| 176 | Reference<XMultiComponentFactory> fac= context->getServiceManager(); |
| 177 | Reference<XMultiServiceFactory> xMgr( fac, UNO_QUERY); |
| 178 | |
| 179 | sal_Bool bSucc = test1(xMgr); |
| 180 | Reference< XComponent > xCompContext( context, UNO_QUERY ); |
| 181 | xCompContext->dispose(); |
| 182 | return (bSucc ? 0 : -1); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |