Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / jvmaccess / source / classpath.cxx
blobc99290b0ac0508f527e546e377e81bf63eea3b4c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "sal/config.h"
31 #include "jvmaccess/classpath.hxx"
33 #include <vector>
35 #include "com/sun/star/lang/IllegalArgumentException.hpp"
36 #include "com/sun/star/uno/Any.hxx"
37 #include "com/sun/star/uno/Reference.hxx"
38 #include "com/sun/star/uno/RuntimeException.hpp"
39 #include "com/sun/star/uno/XComponentContext.hpp"
40 #include "com/sun/star/uno/XInterface.hpp"
41 #include "com/sun/star/uri/UriReferenceFactory.hpp"
42 #include "com/sun/star/uri/XVndSunStarExpandUrlReference.hpp"
43 #include "com/sun/star/util/XMacroExpander.hpp"
44 #include "osl/diagnose.h"
45 #include "rtl/ustring.hxx"
46 #include "sal/types.h"
48 #if defined SOLAR_JAVA
49 #include "jni.h"
50 #endif
52 namespace {
54 namespace css = ::com::sun::star;
58 void * ::jvmaccess::ClassPath::doTranslateToUrls(
59 css::uno::Reference< css::uno::XComponentContext > const & context,
60 void * environment, ::rtl::OUString const & classPath)
62 OSL_ASSERT(context.is() && environment != 0);
63 #if defined SOLAR_JAVA
64 ::JNIEnv * const env = static_cast< ::JNIEnv * >(environment);
65 jclass classUrl(env->FindClass("java/net/URL"));
66 if (classUrl == 0) {
67 return 0;
69 jmethodID ctorUrl(
70 env->GetMethodID(classUrl, "<init>", "(Ljava/lang/String;)V"));
71 if (ctorUrl == 0) {
72 return 0;
74 ::std::vector< jobject > urls;
75 for (::sal_Int32 i = 0; i != -1;) {
76 ::rtl::OUString url(classPath.getToken(0, ' ', i));
77 if (!url.isEmpty()) {
78 css::uno::Reference< css::uri::XVndSunStarExpandUrlReference >
79 expUrl(
80 css::uri::UriReferenceFactory::create(context)->parse(url),
81 css::uno::UNO_QUERY);
82 if (expUrl.is()) {
83 css::uno::Reference< css::util::XMacroExpander > expander(
84 context->getValueByName(
85 ::rtl::OUString(
86 RTL_CONSTASCII_USTRINGPARAM(
87 "/singletons/"
88 "com.sun.star.util.theMacroExpander"))),
89 css::uno::UNO_QUERY_THROW);
90 try {
91 url = expUrl->expand(expander);
92 } catch (const css::lang::IllegalArgumentException & e) {
93 throw css::uno::RuntimeException(
94 (::rtl::OUString(
95 RTL_CONSTASCII_USTRINGPARAM(
96 "com.sun.star.lang.IllegalArgumentException: "))
97 + e.Message),
98 css::uno::Reference< css::uno::XInterface >());
101 jvalue arg;
102 arg.l = env->NewString(
103 static_cast< jchar const * >(url.getStr()),
104 static_cast< jsize >(url.getLength()));
105 if (arg.l == 0) {
106 return 0;
108 jobject o(env->NewObjectA(classUrl, ctorUrl, &arg));
109 if (o == 0) {
110 return 0;
112 urls.push_back(o);
115 jobjectArray result = env->NewObjectArray(
116 static_cast< jsize >(urls.size()), classUrl, 0);
117 // static_cast is ok, as each element of urls occupied at least one
118 // character of the ::rtl::OUString classPath
119 if (result == 0) {
120 return 0;
122 jsize idx = 0;
123 for (std::vector< jobject >::iterator i(urls.begin()); i != urls.end(); ++i)
125 env->SetObjectArrayElement(result, idx++, *i);
127 return result;
128 #else
129 (void) context;
130 (void) environment;
131 (void) classPath;
132 return 0;
133 #endif
136 void * ::jvmaccess::ClassPath::doLoadClass(
137 css::uno::Reference< css::uno::XComponentContext > const & context,
138 void * environment, ::rtl::OUString const & classPath,
139 ::rtl::OUString const & name)
141 OSL_ASSERT(context.is() && environment != 0);
142 #if defined SOLAR_JAVA
143 ::JNIEnv * const env = static_cast< ::JNIEnv * >(environment);
144 jclass classLoader(env->FindClass("java/net/URLClassLoader"));
145 if (classLoader == 0) {
146 return 0;
148 jmethodID ctorLoader(
149 env->GetMethodID(classLoader, "<init>", "([Ljava/net/URL;)V"));
150 if (ctorLoader == 0) {
151 return 0;
153 jvalue arg;
154 arg.l = translateToUrls(context, env, classPath);
155 if (arg.l == 0) {
156 return 0;
158 jobject cl = env->NewObjectA(classLoader, ctorLoader, &arg);
159 if (cl == 0) {
160 return 0;
162 jmethodID methLoadClass(
163 env->GetMethodID(
164 classLoader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"));
165 if (methLoadClass == 0) {
166 return 0;
168 arg.l = env->NewString(
169 static_cast< jchar const * >(name.getStr()),
170 static_cast< jsize >(name.getLength()));
171 if (arg.l == 0) {
172 return 0;
174 return env->CallObjectMethodA(cl, methLoadClass, &arg);
175 #else
176 (void) context;
177 (void) environment;
178 (void) classPath;
179 (void) name;
180 return 0;
181 #endif
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */