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