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