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