1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "osl/diagnose.h"
32 #include "rtl/alloc.h"
33 #include "rtl/bootstrap.hxx"
34 #include "rtl/string.hxx"
36 #include "uno/mapping.hxx"
37 #include "uno/environment.hxx"
39 #include "cppuhelper/bootstrap.hxx"
41 #include "com/sun/star/lang/XComponent.hpp"
42 #include "com/sun/star/lang/XSingleComponentFactory.hpp"
45 #include "jvmaccess/virtualmachine.hxx"
46 #include "jvmaccess/unovirtualmachine.hxx"
50 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
53 using namespace ::com::sun::star
;
54 using namespace ::com::sun::star::uno
;
56 using ::rtl::OUString
;
58 namespace javaunohelper
61 inline ::rtl::OUString
jstring_to_oustring( jstring jstr
, JNIEnv
* jni_env
)
63 OSL_ASSERT( sizeof (sal_Unicode
) == sizeof (jchar
) );
64 jsize len
= jni_env
->GetStringLength( jstr
);
66 (rtl_uString
*)rtl_allocateMemory( sizeof (rtl_uString
) + (len
* sizeof (sal_Unicode
)) );
67 jni_env
->GetStringRegion( jstr
, 0, len
, ustr
->buffer
);
68 OSL_ASSERT( JNI_FALSE
== jni_env
->ExceptionCheck() );
71 ustr
->buffer
[ len
] = '\0';
72 return ::rtl::OUString( ustr
, SAL_NO_ACQUIRE
);
77 //==================================================================================================
78 extern "C" JNIEXPORT jobject JNICALL
Java_com_sun_star_comp_helper_Bootstrap_cppuhelper_1bootstrap(
79 JNIEnv
* jni_env
, SAL_UNUSED_PARAMETER jclass
, jstring juno_rc
, jobjectArray jpairs
,
86 jsize nPos
= 0, len
= jni_env
->GetArrayLength( jpairs
);
90 jstring jstr
= (jstring
)jni_env
->GetObjectArrayElement( jpairs
, nPos
);
91 if (JNI_FALSE
!= jni_env
->ExceptionCheck())
93 jni_env
->ExceptionClear();
94 throw RuntimeException(
95 OUSTR("index out of bounds?!"), Reference
< XInterface
>() );
99 OUString
name( ::javaunohelper::jstring_to_oustring( jstr
, jni_env
) );
101 jstr
= (jstring
)jni_env
->GetObjectArrayElement( jpairs
, nPos
+1 );
102 if (JNI_FALSE
!= jni_env
->ExceptionCheck())
104 jni_env
->ExceptionClear();
105 throw RuntimeException(
106 OUSTR("index out of bounds?!"), Reference
< XInterface
>() );
110 OUString
value( ::javaunohelper::jstring_to_oustring( jstr
, jni_env
) );
112 // set bootstrap parameter
113 ::rtl::Bootstrap::set( name
, value
);
121 Reference
< XComponentContext
> xContext
;
124 xContext
= ::cppu::defaultBootstrap_InitialComponentContext();
128 OUString
uno_rc( ::javaunohelper::jstring_to_oustring( juno_rc
, jni_env
) );
129 xContext
= ::cppu::defaultBootstrap_InitialComponentContext( uno_rc
);
133 ::rtl::Reference
< ::jvmaccess::UnoVirtualMachine
> vm_access(
134 ::javaunohelper::create_vm_access( jni_env
, loader
) );
135 // wrap vm singleton entry
136 xContext
= ::javaunohelper::install_vm_singleton( xContext
, vm_access
);
139 OUString cpp_env_name
= OUSTR(CPPU_CURRENT_LANGUAGE_BINDING_NAME
);
140 OUString java_env_name
= OUSTR(UNO_LB_JAVA
);
141 Environment java_env
, cpp_env
;
142 uno_getEnvironment((uno_Environment
**)&cpp_env
, cpp_env_name
.pData
, NULL
);
143 uno_getEnvironment( (uno_Environment
**)&java_env
, java_env_name
.pData
, vm_access
.get() );
146 Mapping
mapping( cpp_env
.get(), java_env
.get() );
149 Reference
< lang::XComponent
> xComp( xContext
, UNO_QUERY
);
152 throw RuntimeException(
153 OUSTR("cannot get mapping C++ <-> Java!"),
154 Reference
< XInterface
>() );
157 jobject jret
= (jobject
)mapping
.mapInterface( xContext
.get(), ::getCppuType( &xContext
) );
158 jobject jlocal
= jni_env
->NewLocalRef( jret
);
159 jni_env
->DeleteGlobalRef( jret
);
163 catch (const RuntimeException
& exc
)
165 jclass c
= jni_env
->FindClass( "com/sun/star/uno/RuntimeException" );
168 OString
cstr( ::rtl::OUStringToOString(
169 exc
.Message
, RTL_TEXTENCODING_JAVA_UTF8
) );
170 OSL_TRACE( __FILE__
": forwarding RuntimeException: %s", cstr
.getStr() );
171 jni_env
->ThrowNew( c
, cstr
.getStr() );
174 catch (const Exception
& exc
)
176 jclass c
= jni_env
->FindClass( "com/sun/star/uno/Exception" );
179 OString
cstr( ::rtl::OUStringToOString(
180 exc
.Message
, RTL_TEXTENCODING_JAVA_UTF8
) );
181 OSL_TRACE( __FILE__
": forwarding Exception: %s", cstr
.getStr() );
182 jni_env
->ThrowNew( c
, cstr
.getStr() );
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */