xref: /aoo42x/main/jvmaccess/source/classpath.cxx (revision dff3f235)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "sal/config.h"
25 
26 #include "jvmaccess/classpath.hxx"
27 
28 #include <vector>
29 
30 #include "com/sun/star/lang/IllegalArgumentException.hpp"
31 #include "com/sun/star/uno/Any.hxx"
32 #include "com/sun/star/uno/Reference.hxx"
33 #include "com/sun/star/uno/RuntimeException.hpp"
34 #include "com/sun/star/uno/XComponentContext.hpp"
35 #include "com/sun/star/uno/XInterface.hpp"
36 #include "com/sun/star/uri/UriReferenceFactory.hpp"
37 #include "com/sun/star/uri/XVndSunStarExpandUrlReference.hpp"
38 #include "com/sun/star/util/XMacroExpander.hpp"
39 #include "osl/diagnose.h"
40 #include "rtl/ustring.hxx"
41 #include "sal/types.h"
42 
43 #if defined SOLAR_JAVA
44 #include "jni.h"
45 #endif
46 
47 namespace {
48 
49 namespace css = ::com::sun::star;
50 
51 }
52 
doTranslateToUrls(css::uno::Reference<css::uno::XComponentContext> const & context,void * environment,::rtl::OUString const & classPath)53 void * ::jvmaccess::ClassPath::doTranslateToUrls(
54     css::uno::Reference< css::uno::XComponentContext > const & context,
55     void * environment, ::rtl::OUString const & classPath)
56 {
57     OSL_ASSERT(context.is() && environment != 0);
58 #if defined SOLAR_JAVA
59     ::JNIEnv * const env = static_cast< ::JNIEnv * >(environment);
60     jclass classUrl(env->FindClass("java/net/URL"));
61     if (classUrl == 0) {
62         return 0;
63     }
64     jmethodID ctorUrl(
65         env->GetMethodID(classUrl, "<init>", "(Ljava/lang/String;)V"));
66     if (ctorUrl == 0) {
67         return 0;
68     }
69     ::std::vector< jobject > urls;
70     for (::sal_Int32 i = 0; i != -1;) {
71         ::rtl::OUString url(classPath.getToken(0, ' ', i));
72         if (url.getLength() != 0) {
73             css::uno::Reference< css::uri::XVndSunStarExpandUrlReference >
74                 expUrl(
75                     css::uri::UriReferenceFactory::create(context)->parse(url),
76                     css::uno::UNO_QUERY);
77             if (expUrl.is()) {
78                 css::uno::Reference< css::util::XMacroExpander > expander(
79                     context->getValueByName(
80                         ::rtl::OUString(
81                             RTL_CONSTASCII_USTRINGPARAM(
82                                 "/singletons/"
83                                 "com.sun.star.util.theMacroExpander"))),
84                     css::uno::UNO_QUERY_THROW);
85                 try {
86                     url = expUrl->expand(expander);
87                 } catch (css::lang::IllegalArgumentException & e) {
88                     throw css::uno::RuntimeException(
89                         (::rtl::OUString(
90                             RTL_CONSTASCII_USTRINGPARAM(
91                                 "com.sun.star.lang.IllegalArgumentException: "))
92                          + e.Message),
93                         css::uno::Reference< css::uno::XInterface >());
94                 }
95             }
96             jvalue arg;
97             arg.l = env->NewString(
98                 static_cast< jchar const * >(url.getStr()),
99                 static_cast< jsize >(url.getLength()));
100             if (arg.l == 0) {
101                 return 0;
102             }
103             jobject o(env->NewObjectA(classUrl, ctorUrl, &arg));
104             if (o == 0) {
105                 return 0;
106             }
107             urls.push_back(o);
108         }
109     }
110     jobjectArray result = env->NewObjectArray(
111         static_cast< jsize >(urls.size()), classUrl, 0);
112         // static_cast is ok, as each element of urls occupied at least one
113         // character of the ::rtl::OUString classPath
114     if (result == 0) {
115         return 0;
116     }
117     jsize idx = 0;
118     for (std::vector< jobject >::iterator i(urls.begin()); i != urls.end(); ++i)
119     {
120         env->SetObjectArrayElement(result, idx++, *i);
121     }
122     return result;
123 #else
124     return 0;
125 #endif
126 }
127 
doLoadClass(css::uno::Reference<css::uno::XComponentContext> const & context,void * environment,::rtl::OUString const & classPath,::rtl::OUString const & name)128 void * ::jvmaccess::ClassPath::doLoadClass(
129     css::uno::Reference< css::uno::XComponentContext > const & context,
130     void * environment, ::rtl::OUString const & classPath,
131     ::rtl::OUString const & name)
132 {
133     OSL_ASSERT(context.is() && environment != 0);
134 #if defined SOLAR_JAVA
135     ::JNIEnv * const env = static_cast< ::JNIEnv * >(environment);
136     jclass classLoader(env->FindClass("java/net/URLClassLoader"));
137     if (classLoader == 0) {
138         return 0;
139     }
140     jmethodID ctorLoader(
141         env->GetMethodID(classLoader, "<init>", "([Ljava/net/URL;)V"));
142     if (ctorLoader == 0) {
143         return 0;
144     }
145     jvalue arg;
146     arg.l = translateToUrls(context, env, classPath);
147     if (arg.l == 0) {
148         return 0;
149     }
150     jobject cl = env->NewObjectA(classLoader, ctorLoader, &arg);
151     if (cl == 0) {
152         return 0;
153     }
154     jmethodID methLoadClass(
155         env->GetMethodID(
156             classLoader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"));
157     if (methLoadClass == 0) {
158         return 0;
159     }
160     arg.l = env->NewString(
161         static_cast< jchar const * >(name.getStr()),
162         static_cast< jsize >(name.getLength()));
163     if (arg.l == 0) {
164         return 0;
165     }
166     return env->CallObjectMethodA(cl, methLoadClass, &arg);
167 #else
168     return 0;
169 #endif
170 }
171