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 ************************************************************************/
29 #include "sal/config.h"
31 #include "jvmaccess/classpath.hxx"
35 #include "com/sun/star/lang/IllegalArgumentException.hpp"
36 #include "com/sun/star/uno/Any.hxx"
37 #include "com/sun/star/uno/Reference.hxx"
38 #include "com/sun/star/uno/RuntimeException.hpp"
39 #include "com/sun/star/uno/XComponentContext.hpp"
40 #include "com/sun/star/uno/XInterface.hpp"
41 #include "com/sun/star/uri/UriReferenceFactory.hpp"
42 #include "com/sun/star/uri/XVndSunStarExpandUrlReference.hpp"
43 #include "com/sun/star/util/XMacroExpander.hpp"
44 #include "osl/diagnose.h"
45 #include "rtl/ustring.hxx"
46 #include "sal/types.h"
48 #if defined SOLAR_JAVA
54 namespace css
= ::com::sun::star
;
58 void * ::jvmaccess::ClassPath::doTranslateToUrls(
59 css::uno::Reference
< css::uno::XComponentContext
> const & context
,
60 void * environment
, ::rtl::OUString
const & classPath
)
62 OSL_ASSERT(context
.is() && environment
!= 0);
63 #if defined SOLAR_JAVA
64 ::JNIEnv
* const env
= static_cast< ::JNIEnv
* >(environment
);
65 jclass
classUrl(env
->FindClass("java/net/URL"));
70 env
->GetMethodID(classUrl
, "<init>", "(Ljava/lang/String;)V"));
74 ::std::vector
< jobject
> urls
;
75 for (::sal_Int32 i
= 0; i
!= -1;) {
76 ::rtl::OUString
url(classPath
.getToken(0, ' ', i
));
78 css::uno::Reference
< css::uri::XVndSunStarExpandUrlReference
>
80 css::uri::UriReferenceFactory::create(context
)->parse(url
),
83 css::uno::Reference
< css::util::XMacroExpander
> expander(
84 context
->getValueByName(
86 RTL_CONSTASCII_USTRINGPARAM(
88 "com.sun.star.util.theMacroExpander"))),
89 css::uno::UNO_QUERY_THROW
);
91 url
= expUrl
->expand(expander
);
92 } catch (const css::lang::IllegalArgumentException
& e
) {
93 throw css::uno::RuntimeException(
95 RTL_CONSTASCII_USTRINGPARAM(
96 "com.sun.star.lang.IllegalArgumentException: "))
98 css::uno::Reference
< css::uno::XInterface
>());
102 arg
.l
= env
->NewString(
103 static_cast< jchar
const * >(url
.getStr()),
104 static_cast< jsize
>(url
.getLength()));
108 jobject
o(env
->NewObjectA(classUrl
, ctorUrl
, &arg
));
115 jobjectArray result
= env
->NewObjectArray(
116 static_cast< jsize
>(urls
.size()), classUrl
, 0);
117 // static_cast is ok, as each element of urls occupied at least one
118 // character of the ::rtl::OUString classPath
123 for (std::vector
< jobject
>::iterator
i(urls
.begin()); i
!= urls
.end(); ++i
)
125 env
->SetObjectArrayElement(result
, idx
++, *i
);
136 void * ::jvmaccess::ClassPath::doLoadClass(
137 css::uno::Reference
< css::uno::XComponentContext
> const & context
,
138 void * environment
, ::rtl::OUString
const & classPath
,
139 ::rtl::OUString
const & name
)
141 OSL_ASSERT(context
.is() && environment
!= 0);
142 #if defined SOLAR_JAVA
143 ::JNIEnv
* const env
= static_cast< ::JNIEnv
* >(environment
);
144 jclass
classLoader(env
->FindClass("java/net/URLClassLoader"));
145 if (classLoader
== 0) {
148 jmethodID
ctorLoader(
149 env
->GetMethodID(classLoader
, "<init>", "([Ljava/net/URL;)V"));
150 if (ctorLoader
== 0) {
154 arg
.l
= translateToUrls(context
, env
, classPath
);
158 jobject cl
= env
->NewObjectA(classLoader
, ctorLoader
, &arg
);
162 jmethodID
methLoadClass(
164 classLoader
, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"));
165 if (methLoadClass
== 0) {
168 arg
.l
= env
->NewString(
169 static_cast< jchar
const * >(name
.getStr()),
170 static_cast< jsize
>(name
.getLength()));
174 return env
->CallObjectMethodA(cl
, methLoadClass
, &arg
);
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */