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