1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "sal/config.h"
22 #include "osl/diagnose.h"
24 #include "rtl/alloc.h"
25 #include "rtl/bootstrap.hxx"
26 #include "rtl/string.hxx"
28 #include <uno/lbnames.h>
29 #include "uno/mapping.hxx"
30 #include "uno/environment.hxx"
32 #include "cppuhelper/bootstrap.hxx"
34 #include "com/sun/star/lang/XComponent.hpp"
35 #include "com/sun/star/lang/XSingleComponentFactory.hpp"
38 #include "jvmaccess/virtualmachine.hxx"
39 #include "jvmaccess/unovirtualmachine.hxx"
41 #include "juhx-export-functions.hxx"
44 using namespace ::com::sun::star
;
45 using namespace ::com::sun::star::uno
;
47 namespace javaunohelper
50 inline OUString
jstring_to_oustring( jstring jstr
, JNIEnv
* jni_env
)
52 OSL_ASSERT( sizeof (sal_Unicode
) == sizeof (jchar
) );
53 jsize len
= jni_env
->GetStringLength( jstr
);
55 (rtl_uString
*)rtl_allocateMemory( sizeof (rtl_uString
) + (len
* sizeof (sal_Unicode
)) );
56 jni_env
->GetStringRegion( jstr
, 0, len
, ustr
->buffer
);
57 OSL_ASSERT( JNI_FALSE
== jni_env
->ExceptionCheck() );
60 ustr
->buffer
[ len
] = '\0';
61 return OUString( ustr
, SAL_NO_ACQUIRE
);
67 jobject
Java_com_sun_star_comp_helper_Bootstrap_cppuhelper_1bootstrap(
68 JNIEnv
* jni_env
, SAL_UNUSED_PARAMETER jclass
, jstring juno_rc
, jobjectArray jpairs
,
75 jsize nPos
= 0, len
= jni_env
->GetArrayLength( jpairs
);
79 jstring jstr
= (jstring
)jni_env
->GetObjectArrayElement( jpairs
, nPos
);
80 if (JNI_FALSE
!= jni_env
->ExceptionCheck())
82 jni_env
->ExceptionClear();
83 throw RuntimeException(
84 "index out of bounds?!", Reference
< XInterface
>() );
88 OUString
name( ::javaunohelper::jstring_to_oustring( jstr
, jni_env
) );
90 jstr
= (jstring
)jni_env
->GetObjectArrayElement( jpairs
, nPos
+1 );
91 if (JNI_FALSE
!= jni_env
->ExceptionCheck())
93 jni_env
->ExceptionClear();
94 throw RuntimeException(
95 "index out of bounds?!", Reference
< XInterface
>() );
99 OUString
value( ::javaunohelper::jstring_to_oustring( jstr
, jni_env
) );
101 // set bootstrap parameter
102 ::rtl::Bootstrap::set( name
, value
);
110 Reference
< XComponentContext
> xContext
;
113 xContext
= ::cppu::defaultBootstrap_InitialComponentContext();
117 OUString
uno_rc( ::javaunohelper::jstring_to_oustring( juno_rc
, jni_env
) );
118 xContext
= ::cppu::defaultBootstrap_InitialComponentContext( uno_rc
);
122 ::rtl::Reference
< ::jvmaccess::UnoVirtualMachine
> vm_access(
123 ::javaunohelper::create_vm_access( jni_env
, loader
) );
124 // wrap vm singleton entry
125 xContext
= ::javaunohelper::install_vm_singleton( xContext
, vm_access
);
128 OUString cpp_env_name
= CPPU_CURRENT_LANGUAGE_BINDING_NAME
;
129 OUString java_env_name
= UNO_LB_JAVA
;
130 Environment java_env
, cpp_env
;
131 uno_getEnvironment((uno_Environment
**)&cpp_env
, cpp_env_name
.pData
, NULL
);
132 uno_getEnvironment( (uno_Environment
**)&java_env
, java_env_name
.pData
, vm_access
.get() );
135 Mapping
mapping( cpp_env
.get(), java_env
.get() );
138 Reference
< lang::XComponent
> xComp( xContext
, UNO_QUERY
);
141 throw RuntimeException(
142 "cannot get mapping C++ <-> Java!",
143 Reference
< XInterface
>() );
146 jobject jret
= (jobject
)mapping
.mapInterface( xContext
.get(), ::getCppuType( &xContext
) );
147 jobject jlocal
= jni_env
->NewLocalRef( jret
);
148 jni_env
->DeleteGlobalRef( jret
);
152 catch (const RuntimeException
& exc
)
154 jclass c
= jni_env
->FindClass( "com/sun/star/uno/RuntimeException" );
157 OString
cstr( OUStringToOString(
158 exc
.Message
, RTL_TEXTENCODING_JAVA_UTF8
) );
159 OSL_TRACE( __FILE__
": forwarding RuntimeException: %s", cstr
.getStr() );
160 jni_env
->ThrowNew( c
, cstr
.getStr() );
163 catch (const Exception
& exc
)
165 jclass c
= jni_env
->FindClass( "com/sun/star/uno/Exception" );
168 OString
cstr( OUStringToOString(
169 exc
.Message
, RTL_TEXTENCODING_JAVA_UTF8
) );
170 OSL_TRACE( __FILE__
": forwarding Exception: %s", cstr
.getStr() );
171 jni_env
->ThrowNew( c
, cstr
.getStr() );
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */