Line data Source code
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 : #include "sal/config.h"
21 :
22 : #include "osl/diagnose.h"
23 :
24 : #include "rtl/alloc.h"
25 : #include "rtl/bootstrap.hxx"
26 : #include "rtl/string.hxx"
27 :
28 : #include "uno/mapping.hxx"
29 : #include "uno/environment.hxx"
30 :
31 : #include "cppuhelper/bootstrap.hxx"
32 :
33 : #include "com/sun/star/lang/XComponent.hpp"
34 : #include "com/sun/star/lang/XSingleComponentFactory.hpp"
35 :
36 : #include "jni.h"
37 : #include "jvmaccess/virtualmachine.hxx"
38 : #include "jvmaccess/unovirtualmachine.hxx"
39 :
40 : #include "juhx-export-functions.hxx"
41 : #include "vm.hxx"
42 :
43 : using namespace ::com::sun::star;
44 : using namespace ::com::sun::star::uno;
45 :
46 : namespace javaunohelper
47 : {
48 :
49 0 : inline OUString jstring_to_oustring( jstring jstr, JNIEnv * jni_env )
50 : {
51 : OSL_ASSERT( sizeof (sal_Unicode) == sizeof (jchar) );
52 0 : jsize len = jni_env->GetStringLength( jstr );
53 : rtl_uString * ustr =
54 0 : (rtl_uString *)rtl_allocateMemory( sizeof (rtl_uString) + (len * sizeof (sal_Unicode)) );
55 0 : jni_env->GetStringRegion( jstr, 0, len, ustr->buffer );
56 : OSL_ASSERT( JNI_FALSE == jni_env->ExceptionCheck() );
57 0 : ustr->refCount = 1;
58 0 : ustr->length = len;
59 0 : ustr->buffer[ len ] = '\0';
60 0 : return OUString( ustr, SAL_NO_ACQUIRE );
61 : }
62 :
63 : }
64 :
65 : //==================================================================================================
66 0 : jobject Java_com_sun_star_comp_helper_Bootstrap_cppuhelper_1bootstrap(
67 : JNIEnv * jni_env, SAL_UNUSED_PARAMETER jclass, jstring juno_rc, jobjectArray jpairs,
68 : jobject loader )
69 : {
70 : try
71 : {
72 0 : if (0 != jpairs)
73 : {
74 0 : jsize nPos = 0, len = jni_env->GetArrayLength( jpairs );
75 0 : while (nPos < len)
76 : {
77 : // name
78 0 : jstring jstr = (jstring)jni_env->GetObjectArrayElement( jpairs, nPos );
79 0 : if (JNI_FALSE != jni_env->ExceptionCheck())
80 : {
81 0 : jni_env->ExceptionClear();
82 : throw RuntimeException(
83 0 : "index out of bounds?!", Reference< XInterface >() );
84 : }
85 0 : if (0 != jstr)
86 : {
87 0 : OUString name( ::javaunohelper::jstring_to_oustring( jstr, jni_env ) );
88 : // value
89 0 : jstr = (jstring)jni_env->GetObjectArrayElement( jpairs, nPos +1 );
90 0 : if (JNI_FALSE != jni_env->ExceptionCheck())
91 : {
92 0 : jni_env->ExceptionClear();
93 : throw RuntimeException(
94 0 : "index out of bounds?!", Reference< XInterface >() );
95 : }
96 0 : if (0 != jstr)
97 : {
98 0 : OUString value( ::javaunohelper::jstring_to_oustring( jstr, jni_env ) );
99 :
100 : // set bootstrap parameter
101 0 : ::rtl::Bootstrap::set( name, value );
102 0 : }
103 : }
104 0 : nPos += 2;
105 : }
106 : }
107 :
108 : // bootstrap uno
109 0 : Reference< XComponentContext > xContext;
110 0 : if (0 == juno_rc)
111 : {
112 0 : xContext = ::cppu::defaultBootstrap_InitialComponentContext();
113 : }
114 : else
115 : {
116 0 : OUString uno_rc( ::javaunohelper::jstring_to_oustring( juno_rc, jni_env ) );
117 0 : xContext = ::cppu::defaultBootstrap_InitialComponentContext( uno_rc );
118 : }
119 :
120 : // create vm access
121 : ::rtl::Reference< ::jvmaccess::UnoVirtualMachine > vm_access(
122 0 : ::javaunohelper::create_vm_access( jni_env, loader ) );
123 : // wrap vm singleton entry
124 0 : xContext = ::javaunohelper::install_vm_singleton( xContext, vm_access );
125 :
126 : // get uno envs
127 0 : OUString cpp_env_name = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
128 0 : OUString java_env_name = UNO_LB_JAVA;
129 0 : Environment java_env, cpp_env;
130 0 : uno_getEnvironment((uno_Environment **)&cpp_env, cpp_env_name.pData, NULL);
131 0 : uno_getEnvironment( (uno_Environment **)&java_env, java_env_name.pData, vm_access.get() );
132 :
133 : // map to java
134 0 : Mapping mapping( cpp_env.get(), java_env.get() );
135 0 : if (! mapping.is())
136 : {
137 0 : Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
138 0 : if (xComp.is())
139 0 : xComp->dispose();
140 : throw RuntimeException(
141 : "cannot get mapping C++ <-> Java!",
142 0 : Reference< XInterface >() );
143 : }
144 :
145 0 : jobject jret = (jobject)mapping.mapInterface( xContext.get(), ::getCppuType( &xContext ) );
146 0 : jobject jlocal = jni_env->NewLocalRef( jret );
147 0 : jni_env->DeleteGlobalRef( jret );
148 :
149 0 : return jlocal;
150 : }
151 0 : catch (const RuntimeException & exc)
152 : {
153 0 : jclass c = jni_env->FindClass( "com/sun/star/uno/RuntimeException" );
154 0 : if (0 != c)
155 : {
156 : OString cstr( OUStringToOString(
157 0 : exc.Message, RTL_TEXTENCODING_JAVA_UTF8 ) );
158 : OSL_TRACE( __FILE__": forwarding RuntimeException: %s", cstr.getStr() );
159 0 : jni_env->ThrowNew( c, cstr.getStr() );
160 : }
161 : }
162 0 : catch (const Exception & exc)
163 : {
164 0 : jclass c = jni_env->FindClass( "com/sun/star/uno/Exception" );
165 0 : if (0 != c)
166 : {
167 : OString cstr( OUStringToOString(
168 0 : exc.Message, RTL_TEXTENCODING_JAVA_UTF8 ) );
169 : OSL_TRACE( __FILE__": forwarding Exception: %s", cstr.getStr() );
170 0 : jni_env->ThrowNew( c, cstr.getStr() );
171 : }
172 : }
173 :
174 0 : return 0;
175 : }
176 :
177 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|