1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: classpath.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "sal/config.h"
33 #include "jvmaccess/classpath.hxx"
37 #include "com/sun/star/lang/IllegalArgumentException.hpp"
38 #include "com/sun/star/uno/Any.hxx"
39 #include "com/sun/star/uno/Reference.hxx"
40 #include "com/sun/star/uno/RuntimeException.hpp"
41 #include "com/sun/star/uno/XComponentContext.hpp"
42 #include "com/sun/star/uno/XInterface.hpp"
43 #include "com/sun/star/uri/UriReferenceFactory.hpp"
44 #include "com/sun/star/uri/XVndSunStarExpandUrlReference.hpp"
45 #include "com/sun/star/util/XMacroExpander.hpp"
46 #include "osl/diagnose.h"
47 #include "rtl/ustring.hxx"
48 #include "sal/types.h"
50 #if defined SOLAR_JAVA
56 namespace css
= ::com::sun::star
;
60 void * ::jvmaccess::ClassPath::doTranslateToUrls(
61 css::uno::Reference
< css::uno::XComponentContext
> const & context
,
62 void * environment
, ::rtl::OUString
const & classPath
)
64 OSL_ASSERT(context
.is() && environment
!= 0);
65 #if defined SOLAR_JAVA
66 ::JNIEnv
* const env
= static_cast< ::JNIEnv
* >(environment
);
67 jclass
classUrl(env
->FindClass("java/net/URL"));
72 env
->GetMethodID(classUrl
, "<init>", "(Ljava/lang/String;)V"));
76 ::std::vector
< jobject
> urls
;
77 for (::sal_Int32 i
= 0; i
!= -1;) {
78 ::rtl::OUString
url(classPath
.getToken(0, ' ', i
));
79 if (url
.getLength() != 0) {
80 css::uno::Reference
< css::uri::XVndSunStarExpandUrlReference
>
82 css::uri::UriReferenceFactory::create(context
)->parse(url
),
85 css::uno::Reference
< css::util::XMacroExpander
> expander(
86 context
->getValueByName(
88 RTL_CONSTASCII_USTRINGPARAM(
90 "com.sun.star.util.theMacroExpander"))),
91 css::uno::UNO_QUERY_THROW
);
93 url
= expUrl
->expand(expander
);
94 } catch (css::lang::IllegalArgumentException
& e
) {
95 throw css::uno::RuntimeException(
97 RTL_CONSTASCII_USTRINGPARAM(
98 "com.sun.star.lang.IllegalArgumentException: "))
100 css::uno::Reference
< css::uno::XInterface
>());
104 arg
.l
= env
->NewString(
105 static_cast< jchar
const * >(url
.getStr()),
106 static_cast< jsize
>(url
.getLength()));
110 jobject
o(env
->NewObjectA(classUrl
, ctorUrl
, &arg
));
117 jobjectArray result
= env
->NewObjectArray(
118 static_cast< jsize
>(urls
.size()), classUrl
, 0);
119 // static_cast is ok, as each element of urls occupied at least one
120 // character of the ::rtl::OUString classPath
125 for (std::vector
< jobject
>::iterator
i(urls
.begin()); i
!= urls
.end(); ++i
)
127 env
->SetObjectArrayElement(result
, idx
++, *i
);
135 void * ::jvmaccess::ClassPath::doLoadClass(
136 css::uno::Reference
< css::uno::XComponentContext
> const & context
,
137 void * environment
, ::rtl::OUString
const & classPath
,
138 ::rtl::OUString
const & name
)
140 OSL_ASSERT(context
.is() && environment
!= 0);
141 #if defined SOLAR_JAVA
142 ::JNIEnv
* const env
= static_cast< ::JNIEnv
* >(environment
);
143 jclass
classLoader(env
->FindClass("java/net/URLClassLoader"));
144 if (classLoader
== 0) {
147 jmethodID
ctorLoader(
148 env
->GetMethodID(classLoader
, "<init>", "([Ljava/net/URL;)V"));
149 if (ctorLoader
== 0) {
153 arg
.l
= translateToUrls(context
, env
, classPath
);
157 jobject cl
= env
->NewObjectA(classLoader
, ctorLoader
, &arg
);
161 jmethodID
methLoadClass(
163 classLoader
, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"));
164 if (methLoadClass
== 0) {
167 arg
.l
= env
->NewString(
168 static_cast< jchar
const * >(name
.getStr()),
169 static_cast< jsize
>(name
.getLength()));
173 return env
->CallObjectMethodA(cl
, methLoadClass
, &arg
);