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 #ifndef INCLUDED_JVMACCESS_CLASSPATH_HXX
25 #define INCLUDED_JVMACCESS_CLASSPATH_HXX
26 
27 #include "sal/config.h"
28 #include "com/sun/star/uno/Reference.hxx"
29 #include "jvmaccess/jvmaccessdllapi.h"
30 
31 #if defined SOLAR_JAVA
32 #include "jni.h"
33 #else
34 struct JNIEnv;
35 typedef void * jclass;
36 typedef void * jobjectArray;
37 #endif
38 
39 namespace com { namespace sun { namespace star { namespace uno {
40     class XComponentContext;
41 } } } }
42 namespace rtl { class OUString; }
43 
44 namespace jvmaccess {
45 
46 /**
47    Helper functions for class path handling.
48 */
49 class ClassPath {
50 public:
51     /**
52        translates a class path into a java.net.URL[] instance.
53 
54        @param context
55        a component context; must not be null.
56 
57        @param environment
58        a JNI environment; must not be null.
59 
60        @param classPath
61        a list of zero or more internal (see the
62        com.sun.star.uri.ExternalUriReferenceTranslator service) URI references,
63        where any space characters (U+0020) are ignored (and, in particular,
64        separate adjacent URI references).  Any vnd.sun.star.expand URL
65        references in the list are expanded using the
66        com.sun.star.util.theMacroExpander singleton of the given context.
67 
68        @returns
69        a local reference to a java.net.URL[] instance containing the external
70        (see the com.sun.star.uri.ExternalUriReferenceTranslator service)
71        equivalents of all the URI references in the given classPath.  If null, a
72        (still pending) JNI exception occurred.
73 
74        @throws com::sun::star::uno::RuntimeException
75     */
76     static inline ::jobjectArray
translateToUrls(::com::sun::star::uno::Reference<::com::sun::star::uno::XComponentContext> const & context,::JNIEnv * environment,::rtl::OUString const & classPath)77     translateToUrls(
78         ::com::sun::star::uno::Reference<
79         ::com::sun::star::uno::XComponentContext > const & context,
80         ::JNIEnv * environment, ::rtl::OUString const & classPath)
81     {
82         return
83             static_cast< ::jobjectArray >(
84                 doTranslateToUrls(context, environment, classPath));
85     }
86 
87     /**
88        loads a class via a java.net.URLClassLoader.
89 
90        @param context
91        a component context; must not be null.
92 
93        @param environment
94        a JNI environment; must not be null.
95 
96        @param classPath
97        a list of zero or more internal (see the
98        com.sun.star.uri.ExternalUriReferenceTranslator service) URI references,
99        where any space characters (U+0020) are ignored (and, in particular,
100        separate adjacent URI references).  Any vnd.sun.star.expand URL
101        references in the list are expanded using the
102        com.sun.star.util.theMacroExpander singleton of the given context.
103 
104        @param name
105        the Java binary name of the class to load.
106 
107        @returns
108        a local reference to a java.lang.Class instance.  If null, a (still
109        pending) JNI exception occurred.
110 
111        @throws com::sun::star::uno::RuntimeException
112     */
loadClass(::com::sun::star::uno::Reference<::com::sun::star::uno::XComponentContext> const & context,::JNIEnv * environment,::rtl::OUString const & classPath,::rtl::OUString const & name)113     static inline ::jclass loadClass(
114         ::com::sun::star::uno::Reference<
115         ::com::sun::star::uno::XComponentContext > const & context,
116         ::JNIEnv * environment, ::rtl::OUString const & classPath,
117         ::rtl::OUString const & name)
118     {
119         return
120             static_cast< ::jclass >(
121                 doLoadClass(context, environment, classPath, name));
122     }
123 
124 private:
125     ClassPath(); // not defined
126     ClassPath(ClassPath &); // not defined
127     ~ClassPath(); // not defined
128     void operator =(ClassPath &); // not defined
129 
130     // Functions that replace JNIEnv, jobjectArray, and jclass with void *, so
131     // that their mangled C++ names do not depend on the JDK version used at
132     // compile time:
133 
134     JVMACCESS_DLLPUBLIC static void * doTranslateToUrls(
135         ::com::sun::star::uno::Reference<
136         ::com::sun::star::uno::XComponentContext > const & context,
137         void * environment, ::rtl::OUString const & classPath);
138 
139     JVMACCESS_DLLPUBLIC static void * doLoadClass(
140         ::com::sun::star::uno::Reference<
141         ::com::sun::star::uno::XComponentContext > const & context,
142         void * environment, ::rtl::OUString const & classPath,
143         ::rtl::OUString const & name);
144 };
145 
146 }
147 
148 #endif
149