Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / jvmaccess / source / classpath.cxx
blob4861849e2789686eb25c44fe69f7e04eb82439be
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "jvmaccess/classpath.hxx"
24 #include <vector>
26 #include "com/sun/star/lang/IllegalArgumentException.hpp"
27 #include "com/sun/star/uno/Any.hxx"
28 #include "com/sun/star/uno/Reference.hxx"
29 #include "com/sun/star/uno/RuntimeException.hpp"
30 #include "com/sun/star/uno/XComponentContext.hpp"
31 #include "com/sun/star/uno/XInterface.hpp"
32 #include "com/sun/star/uri/UriReferenceFactory.hpp"
33 #include "com/sun/star/uri/XVndSunStarExpandUrlReference.hpp"
34 #include "com/sun/star/util/XMacroExpander.hpp"
35 #include "osl/diagnose.h"
36 #include "rtl/ustring.hxx"
37 #include "sal/types.h"
39 #if defined SOLAR_JAVA
40 #include "jni.h"
41 #endif
43 void * ::jvmaccess::ClassPath::doTranslateToUrls(
44 css::uno::Reference< css::uno::XComponentContext > const & context,
45 void * environment, ::rtl::OUString const & classPath)
47 OSL_ASSERT(context.is() && environment != 0);
48 #if defined SOLAR_JAVA
49 ::JNIEnv * const env = static_cast< ::JNIEnv * >(environment);
50 jclass classUrl(env->FindClass("java/net/URL"));
51 if (classUrl == 0) {
52 return 0;
54 jmethodID ctorUrl(
55 env->GetMethodID(classUrl, "<init>", "(Ljava/lang/String;)V"));
56 if (ctorUrl == 0) {
57 return 0;
59 ::std::vector< jobject > urls;
60 for (::sal_Int32 i = 0; i != -1;) {
61 ::rtl::OUString url(classPath.getToken(0, ' ', i));
62 if (!url.isEmpty()) {
63 css::uno::Reference< css::uri::XVndSunStarExpandUrlReference >
64 expUrl(
65 css::uri::UriReferenceFactory::create(context)->parse(url),
66 css::uno::UNO_QUERY);
67 if (expUrl.is()) {
68 css::uno::Reference< css::util::XMacroExpander > expander(
69 context->getValueByName(
70 ::rtl::OUString(
71 RTL_CONSTASCII_USTRINGPARAM(
72 "/singletons/"
73 "com.sun.star.util.theMacroExpander"))),
74 css::uno::UNO_QUERY_THROW);
75 try {
76 url = expUrl->expand(expander);
77 } catch (const css::lang::IllegalArgumentException & e) {
78 throw css::uno::RuntimeException(
79 (::rtl::OUString(
80 RTL_CONSTASCII_USTRINGPARAM(
81 "com.sun.star.lang.IllegalArgumentException: "))
82 + e.Message),
83 css::uno::Reference< css::uno::XInterface >());
86 jvalue arg;
87 arg.l = env->NewString(
88 static_cast< jchar const * >(url.getStr()),
89 static_cast< jsize >(url.getLength()));
90 if (arg.l == 0) {
91 return 0;
93 jobject o(env->NewObjectA(classUrl, ctorUrl, &arg));
94 if (o == 0) {
95 return 0;
97 urls.push_back(o);
100 jobjectArray result = env->NewObjectArray(
101 static_cast< jsize >(urls.size()), classUrl, 0);
102 // static_cast is ok, as each element of urls occupied at least one
103 // character of the ::rtl::OUString classPath
104 if (result == 0) {
105 return 0;
107 jsize idx = 0;
108 for (std::vector< jobject >::iterator i(urls.begin()); i != urls.end(); ++i)
110 env->SetObjectArrayElement(result, idx++, *i);
112 return result;
113 #else
114 (void) context;
115 (void) environment;
116 (void) classPath;
117 return 0;
118 #endif
121 void * ::jvmaccess::ClassPath::doLoadClass(
122 css::uno::Reference< css::uno::XComponentContext > const & context,
123 void * environment, ::rtl::OUString const & classPath,
124 ::rtl::OUString const & name)
126 OSL_ASSERT(context.is() && environment != 0);
127 #if defined SOLAR_JAVA
128 ::JNIEnv * const env = static_cast< ::JNIEnv * >(environment);
129 jclass classLoader(env->FindClass("java/net/URLClassLoader"));
130 if (classLoader == 0) {
131 return 0;
133 jmethodID ctorLoader(
134 env->GetMethodID(classLoader, "<init>", "([Ljava/net/URL;)V"));
135 if (ctorLoader == 0) {
136 return 0;
138 jvalue arg;
139 arg.l = translateToUrls(context, env, classPath);
140 if (arg.l == 0) {
141 return 0;
143 jobject cl = env->NewObjectA(classLoader, ctorLoader, &arg);
144 if (cl == 0) {
145 return 0;
147 jmethodID methLoadClass(
148 env->GetMethodID(
149 classLoader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"));
150 if (methLoadClass == 0) {
151 return 0;
153 arg.l = env->NewString(
154 static_cast< jchar const * >(name.getStr()),
155 static_cast< jsize >(name.getLength()));
156 if (arg.l == 0) {
157 return 0;
159 return env->CallObjectMethodA(cl, methLoadClass, &arg);
160 #else
161 (void) context;
162 (void) environment;
163 (void) classPath;
164 (void) name;
165 return 0;
166 #endif
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */