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