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