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