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