update dev300-m58
[ooovba.git] / jvmaccess / source / classpath.cxx
blob2561946f24c597bfc47d368b8e4a61df7e32916b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: classpath.cxx,v $
10 * $Revision: 1.4 $
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"
35 #include <vector>
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
51 #include "jni.h"
52 #endif
54 namespace {
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"));
68 if (classUrl == 0) {
69 return 0;
71 jmethodID ctorUrl(
72 env->GetMethodID(classUrl, "<init>", "(Ljava/lang/String;)V"));
73 if (ctorUrl == 0) {
74 return 0;
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 >
81 expUrl(
82 css::uri::UriReferenceFactory::create(context)->parse(url),
83 css::uno::UNO_QUERY);
84 if (expUrl.is()) {
85 css::uno::Reference< css::util::XMacroExpander > expander(
86 context->getValueByName(
87 ::rtl::OUString(
88 RTL_CONSTASCII_USTRINGPARAM(
89 "/singletons/"
90 "com.sun.star.util.theMacroExpander"))),
91 css::uno::UNO_QUERY_THROW);
92 try {
93 url = expUrl->expand(expander);
94 } catch (css::lang::IllegalArgumentException & e) {
95 throw css::uno::RuntimeException(
96 (::rtl::OUString(
97 RTL_CONSTASCII_USTRINGPARAM(
98 "com.sun.star.lang.IllegalArgumentException: "))
99 + e.Message),
100 css::uno::Reference< css::uno::XInterface >());
103 jvalue arg;
104 arg.l = env->NewString(
105 static_cast< jchar const * >(url.getStr()),
106 static_cast< jsize >(url.getLength()));
107 if (arg.l == 0) {
108 return 0;
110 jobject o(env->NewObjectA(classUrl, ctorUrl, &arg));
111 if (o == 0) {
112 return 0;
114 urls.push_back(o);
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
121 if (result == 0) {
122 return 0;
124 jsize idx = 0;
125 for (std::vector< jobject >::iterator i(urls.begin()); i != urls.end(); ++i)
127 env->SetObjectArrayElement(result, idx++, *i);
129 return result;
130 #else
131 return 0;
132 #endif
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) {
145 return 0;
147 jmethodID ctorLoader(
148 env->GetMethodID(classLoader, "<init>", "([Ljava/net/URL;)V"));
149 if (ctorLoader == 0) {
150 return 0;
152 jvalue arg;
153 arg.l = translateToUrls(context, env, classPath);
154 if (arg.l == 0) {
155 return 0;
157 jobject cl = env->NewObjectA(classLoader, ctorLoader, &arg);
158 if (cl == 0) {
159 return 0;
161 jmethodID methLoadClass(
162 env->GetMethodID(
163 classLoader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"));
164 if (methLoadClass == 0) {
165 return 0;
167 arg.l = env->NewString(
168 static_cast< jchar const * >(name.getStr()),
169 static_cast< jsize >(name.getLength()));
170 if (arg.l == 0) {
171 return 0;
173 return env->CallObjectMethodA(cl, methLoadClass, &arg);
174 #else
175 return 0;
176 #endif