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_bridges.hxx"
30 
31 #include "jni.h"
32 
33 #include "uno/mapping.hxx"
34 #include "uno/environment.hxx"
35 #include "jvmaccess/virtualmachine.hxx"
36 #include "jvmaccess/unovirtualmachine.hxx"
37 #include "cppuhelper/implbase1.hxx"
38 
39 #include "test/java_uno/anytest/XTransport.hpp"
40 #include "test/java_uno/anytest/DerivedInterface.hpp"
41 
42 
43 using namespace ::com::sun::star::uno;
44 using ::test::java_uno::anytest::XTransport;
45 using ::rtl::OUString;
46 
47 namespace
48 {
49 //==================================================================================================
50 class Transport : public ::cppu::WeakImplHelper1< XTransport >
51 {
52 public:
53     virtual Any SAL_CALL mapAny( Any const & any )
54         throw (RuntimeException);
55 };
56 //__________________________________________________________________________________________________
57 Any Transport::mapAny( Any const & any )
58     throw (RuntimeException)
59 {
60     return any;
61 }
62 }
63 
64 //##################################################################################################
65 extern "C" JNIEXPORT jobject JNICALL Java_test_java_1uno_anytest_TestJni_create_1jni_1transport(
66     JNIEnv * jni_env, jclass, jobject loader )
67     SAL_THROW_EXTERN_C()
68 {
69     // publish some idl types
70     ::getCppuType( (Reference< XTransport > const *)0 );
71     ::getCppuType( (Reference< ::test::java_uno::anytest::DerivedInterface > const *)0 );
72 
73     Reference< XTransport > xRet( new Transport() );
74 
75     // get java vm
76     JavaVM * java_vm;
77     OSL_VERIFY( 0 == jni_env->GetJavaVM( &java_vm ) );
78     // create jvmaccess vm
79     ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm;
80     try {
81         vm = new ::jvmaccess::UnoVirtualMachine(
82             new ::jvmaccess::VirtualMachine(
83                 java_vm, JNI_VERSION_1_2, false, jni_env ),
84             loader );
85     } catch ( ::jvmaccess::UnoVirtualMachine::CreationException & ) {
86         OSL_ASSERT( false );
87         throw;
88     }
89     // create uno envs
90     OUString java_name( RTL_CONSTASCII_USTRINGPARAM(UNO_LB_JAVA) );
91     OUString cpp_name( RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME) );
92     Environment java_env, cpp_env;
93     uno_getEnvironment( (uno_Environment **)&java_env, java_name.pData, vm.get() );
94     OSL_ASSERT( java_env.is() );
95     uno_getEnvironment( (uno_Environment **)&cpp_env, cpp_name.pData, 0 );
96     OSL_ASSERT( cpp_env.is() );
97 
98     // map interface
99     Mapping mapping( cpp_env.get(), java_env.get() );
100     OSL_ASSERT( mapping.is() );
101     jobject jo_global = (jobject)mapping.mapInterface( xRet.get(), ::getCppuType( &xRet ) );
102     OSL_ASSERT( 0 != jo_global );
103 
104     // return
105     jobject jo_ret = jni_env->NewLocalRef( jo_global );
106     jni_env->DeleteGlobalRef( jo_global );
107     return jo_ret;
108 }
109