Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "sal/config.h"
21 :
22 : #include "jvmaccess/classpath.hxx"
23 :
24 : #include <cassert>
25 : #include <vector>
26 :
27 : #include "com/sun/star/lang/IllegalArgumentException.hpp"
28 : #include "com/sun/star/uno/Any.hxx"
29 : #include "com/sun/star/uno/Reference.hxx"
30 : #include "com/sun/star/uno/RuntimeException.hpp"
31 : #include "com/sun/star/uno/XComponentContext.hpp"
32 : #include "com/sun/star/uno/XInterface.hpp"
33 : #include "com/sun/star/uri/UriReferenceFactory.hpp"
34 : #include "com/sun/star/uri/XVndSunStarExpandUrlReference.hpp"
35 : #include "com/sun/star/util/theMacroExpander.hpp"
36 : #include "rtl/ustring.hxx"
37 : #include "sal/types.h"
38 :
39 : #include "jni.h"
40 :
41 11 : jobjectArray jvmaccess::ClassPath::translateToUrls(
42 : css::uno::Reference< css::uno::XComponentContext > const & context,
43 : JNIEnv * environment, OUString const & classPath)
44 : {
45 : assert(context.is());
46 : assert(environment != 0);
47 11 : jclass classUrl(environment->FindClass("java/net/URL"));
48 11 : if (classUrl == 0) {
49 0 : return 0;
50 : }
51 : jmethodID ctorUrl(
52 11 : environment->GetMethodID(classUrl, "<init>", "(Ljava/lang/String;)V"));
53 11 : if (ctorUrl == 0) {
54 0 : return 0;
55 : }
56 11 : ::std::vector< jobject > urls;
57 53 : for (::sal_Int32 i = 0; i != -1;) {
58 31 : OUString url(classPath.getToken(0, ' ', i));
59 31 : if (!url.isEmpty()) {
60 : css::uno::Reference< css::uri::XVndSunStarExpandUrlReference >
61 : expUrl(
62 36 : css::uri::UriReferenceFactory::create(context)->parse(url),
63 18 : css::uno::UNO_QUERY);
64 18 : if (expUrl.is()) {
65 : css::uno::Reference< css::util::XMacroExpander > expander =
66 8 : css::util::theMacroExpander::get(context);
67 : try {
68 8 : url = expUrl->expand( expander );
69 0 : } catch (const css::lang::IllegalArgumentException & e) {
70 : throw css::uno::RuntimeException(
71 : "com.sun.star.lang.IllegalArgumentException: "
72 0 : + e.Message);
73 8 : }
74 : }
75 : jvalue arg;
76 : arg.l = environment->NewString(
77 : static_cast< jchar const * >(url.getStr()),
78 18 : static_cast< jsize >(url.getLength()));
79 18 : if (arg.l == 0) {
80 0 : return 0;
81 : }
82 18 : jobject o(environment->NewObjectA(classUrl, ctorUrl, &arg));
83 18 : if (o == 0) {
84 0 : return 0;
85 : }
86 18 : urls.push_back(o);
87 : }
88 31 : }
89 : jobjectArray result = environment->NewObjectArray(
90 11 : static_cast< jsize >(urls.size()), classUrl, 0);
91 : // static_cast is ok, as each element of urls occupied at least one
92 : // character of the OUString classPath
93 11 : if (result == 0) {
94 0 : return 0;
95 : }
96 11 : jsize idx = 0;
97 29 : for (std::vector< jobject >::iterator i(urls.begin()); i != urls.end(); ++i)
98 : {
99 18 : environment->SetObjectArrayElement(result, idx++, *i);
100 : }
101 11 : return result;
102 : }
103 :
104 0 : jclass jvmaccess::ClassPath::loadClass(
105 : css::uno::Reference< css::uno::XComponentContext > const & context,
106 : JNIEnv * environment, OUString const & classPath, OUString const & name)
107 : {
108 : assert(context.is());
109 : assert(environment != 0);
110 0 : jclass classLoader(environment->FindClass("java/net/URLClassLoader"));
111 0 : if (classLoader == 0) {
112 0 : return 0;
113 : }
114 : jmethodID ctorLoader(
115 0 : environment->GetMethodID(classLoader, "<init>", "([Ljava/net/URL;)V"));
116 0 : if (ctorLoader == 0) {
117 0 : return 0;
118 : }
119 : jvalue arg;
120 0 : arg.l = translateToUrls(context, environment, classPath);
121 0 : if (arg.l == 0) {
122 0 : return 0;
123 : }
124 0 : jobject cl = environment->NewObjectA(classLoader, ctorLoader, &arg);
125 0 : if (cl == 0) {
126 0 : return 0;
127 : }
128 : jmethodID methLoadClass(
129 : environment->GetMethodID(
130 0 : classLoader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"));
131 0 : if (methLoadClass == 0) {
132 0 : return 0;
133 : }
134 : arg.l = environment->NewString(
135 : static_cast< jchar const * >(name.getStr()),
136 0 : static_cast< jsize >(name.getLength()));
137 0 : if (arg.l == 0) {
138 0 : return 0;
139 : }
140 : return static_cast<jclass>(
141 0 : environment->CallObjectMethodA(cl, methLoadClass, &arg));
142 : }
143 :
144 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|