1*2be43276SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2be43276SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2be43276SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2be43276SAndrew Rist  * distributed with this work for additional information
6*2be43276SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2be43276SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2be43276SAndrew Rist  * "License"); you may not use this file except in compliance
9*2be43276SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2be43276SAndrew Rist  *
11*2be43276SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2be43276SAndrew Rist  *
13*2be43276SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2be43276SAndrew Rist  * software distributed under the License is distributed on an
15*2be43276SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2be43276SAndrew Rist  * KIND, either express or implied.  See the License for the
17*2be43276SAndrew Rist  * specific language governing permissions and limitations
18*2be43276SAndrew Rist  * under the License.
19*2be43276SAndrew Rist  *
20*2be43276SAndrew Rist  *************************************************************/
21*2be43276SAndrew Rist 
22*2be43276SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package com.sun.star.comp.loader;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.lib.unoloader.UnoClassLoader;
27cdf0e10cSrcweir import com.sun.star.lib.util.WeakMap;
28cdf0e10cSrcweir import java.io.File;
29cdf0e10cSrcweir import java.io.IOException;
30cdf0e10cSrcweir import java.net.URL;
31cdf0e10cSrcweir import java.net.URLClassLoader;
32cdf0e10cSrcweir import java.util.StringTokenizer;
33cdf0e10cSrcweir import java.util.jar.Attributes;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir final class RegistrationClassFinder {
find(String locationUrl)36cdf0e10cSrcweir     public static Class find(String locationUrl)
37cdf0e10cSrcweir         throws ClassNotFoundException, IOException
38cdf0e10cSrcweir     {
39cdf0e10cSrcweir         synchronized (map) {
40cdf0e10cSrcweir             Class c = (Class) WeakMap.getValue(map.get(locationUrl));
41cdf0e10cSrcweir             if (c != null) {
42cdf0e10cSrcweir                 return c;
43cdf0e10cSrcweir             }
44cdf0e10cSrcweir         }
45cdf0e10cSrcweir         URL url = new URL(locationUrl);
46cdf0e10cSrcweir         checkAccess(url);
47cdf0e10cSrcweir         Attributes attr = UnoClassLoader.getJarMainAttributes(url);
48cdf0e10cSrcweir         String name = attr == null
49cdf0e10cSrcweir             ? null : attr.getValue("RegistrationClassName");
50cdf0e10cSrcweir         if (name == null) {
51cdf0e10cSrcweir             return null;
52cdf0e10cSrcweir         }
53cdf0e10cSrcweir         ClassLoader cl1 = RegistrationClassFinder.class.getClassLoader();
54cdf0e10cSrcweir         ClassLoader cl2;
55cdf0e10cSrcweir         if (cl1 instanceof UnoClassLoader) {
56cdf0e10cSrcweir             cl2 = ((UnoClassLoader) cl1).getClassLoader(url, attr);
57cdf0e10cSrcweir         } else {
58cdf0e10cSrcweir             cl2 = URLClassLoader.newInstance(new URL[] { url }, cl1);
59cdf0e10cSrcweir         }
60cdf0e10cSrcweir         Class c = cl2.loadClass(name);
61cdf0e10cSrcweir         synchronized (map) {
62cdf0e10cSrcweir             Class c2 = (Class) WeakMap.getValue(map.get(locationUrl));
63cdf0e10cSrcweir             if (c2 != null) {
64cdf0e10cSrcweir                 return c2;
65cdf0e10cSrcweir             }
66cdf0e10cSrcweir             map.put(locationUrl, c);
67cdf0e10cSrcweir         }
68cdf0e10cSrcweir         return c;
69cdf0e10cSrcweir     }
70cdf0e10cSrcweir 
RegistrationClassFinder()71cdf0e10cSrcweir     private RegistrationClassFinder() {} // do not instantiate
72cdf0e10cSrcweir 
checkAccess(URL url)73cdf0e10cSrcweir     private static void checkAccess(URL url) throws ClassNotFoundException {
74cdf0e10cSrcweir         // The system property com.sun.star.comp.loader.CPLD_ACCESSPATH was
75cdf0e10cSrcweir         // introduced as a hack to restrict which UNO components can be
76cdf0e10cSrcweir         // instantiated.  It seems to be unused nowadays, and should probably be
77cdf0e10cSrcweir         // replaced by the native Java security features, anyway.
78cdf0e10cSrcweir         if (accessPath != null) {
79cdf0e10cSrcweir             if (!url.getProtocol().equals("file")) {
80cdf0e10cSrcweir                 throw new ClassNotFoundException(
81cdf0e10cSrcweir                     "Access restriction: <" + url + "> is not a file URL");
82cdf0e10cSrcweir             }
83cdf0e10cSrcweir             String p;
84cdf0e10cSrcweir             try {
85cdf0e10cSrcweir                 p = new File(url.getFile()).getCanonicalPath();
86cdf0e10cSrcweir             } catch (IOException e) {
87cdf0e10cSrcweir                 throw new ClassNotFoundException(
88cdf0e10cSrcweir                     "Access restriction: <" + url + "> is bad: " + e);
89cdf0e10cSrcweir             }
90cdf0e10cSrcweir             for (int i = 0; i < accessPath.length; ++i) {
91cdf0e10cSrcweir                 String p2 = accessPath[i];
92cdf0e10cSrcweir                 if (p.startsWith(p2) && p.length() > p2.length()
93cdf0e10cSrcweir                     && (p2.charAt(p2.length() - 1) == File.separatorChar
94cdf0e10cSrcweir                         || p.charAt(p2.length()) == File.separatorChar))
95cdf0e10cSrcweir                 {
96cdf0e10cSrcweir                     return;
97cdf0e10cSrcweir                 }
98cdf0e10cSrcweir             }
99cdf0e10cSrcweir             throw new ClassNotFoundException(
100cdf0e10cSrcweir                 "Access restriction: <" + url + "> is restricted");
101cdf0e10cSrcweir         }
102cdf0e10cSrcweir     }
103cdf0e10cSrcweir 
104cdf0e10cSrcweir     private static final WeakMap map = new WeakMap();
105cdf0e10cSrcweir 
106cdf0e10cSrcweir     private static final String[] accessPath;
107cdf0e10cSrcweir     static {
108cdf0e10cSrcweir         String[] ap = null;
109cdf0e10cSrcweir         String p = System.getProperty(
110cdf0e10cSrcweir             "com.sun.star.comp.loader.CPLD_ACCESSPATH");
111cdf0e10cSrcweir         if (p != null) {
112cdf0e10cSrcweir             StringTokenizer t = new StringTokenizer(p, ";");
113cdf0e10cSrcweir             ap = new String[t.countTokens()];
114cdf0e10cSrcweir             int i = 0;
115cdf0e10cSrcweir             while (t.hasMoreTokens()) {
116cdf0e10cSrcweir                 try {
117cdf0e10cSrcweir                     ap[i] = new File(t.nextToken()).getCanonicalPath();
118cdf0e10cSrcweir                     ++i;
119cdf0e10cSrcweir                 } catch (IOException e) {}
120cdf0e10cSrcweir             }
121cdf0e10cSrcweir             if (i != ap.length) {
122cdf0e10cSrcweir                 String[] ap2 = new String[i];
System.arraycopy(ap, 0, ap2, 0, i)123cdf0e10cSrcweir                 System.arraycopy(ap, 0, ap2, 0, i);
124cdf0e10cSrcweir                 ap = ap2;
125cdf0e10cSrcweir             }
126cdf0e10cSrcweir         }
127cdf0e10cSrcweir         accessPath = ap;
128cdf0e10cSrcweir     }
129cdf0e10cSrcweir }
130