1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir package com.sun.star.comp.loader; 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir import com.sun.star.lib.unoloader.UnoClassLoader; 31*cdf0e10cSrcweir import com.sun.star.lib.util.WeakMap; 32*cdf0e10cSrcweir import java.io.File; 33*cdf0e10cSrcweir import java.io.IOException; 34*cdf0e10cSrcweir import java.net.URL; 35*cdf0e10cSrcweir import java.net.URLClassLoader; 36*cdf0e10cSrcweir import java.util.StringTokenizer; 37*cdf0e10cSrcweir import java.util.jar.Attributes; 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir final class RegistrationClassFinder { 40*cdf0e10cSrcweir public static Class find(String locationUrl) 41*cdf0e10cSrcweir throws ClassNotFoundException, IOException 42*cdf0e10cSrcweir { 43*cdf0e10cSrcweir synchronized (map) { 44*cdf0e10cSrcweir Class c = (Class) WeakMap.getValue(map.get(locationUrl)); 45*cdf0e10cSrcweir if (c != null) { 46*cdf0e10cSrcweir return c; 47*cdf0e10cSrcweir } 48*cdf0e10cSrcweir } 49*cdf0e10cSrcweir URL url = new URL(locationUrl); 50*cdf0e10cSrcweir checkAccess(url); 51*cdf0e10cSrcweir Attributes attr = UnoClassLoader.getJarMainAttributes(url); 52*cdf0e10cSrcweir String name = attr == null 53*cdf0e10cSrcweir ? null : attr.getValue("RegistrationClassName"); 54*cdf0e10cSrcweir if (name == null) { 55*cdf0e10cSrcweir return null; 56*cdf0e10cSrcweir } 57*cdf0e10cSrcweir ClassLoader cl1 = RegistrationClassFinder.class.getClassLoader(); 58*cdf0e10cSrcweir ClassLoader cl2; 59*cdf0e10cSrcweir if (cl1 instanceof UnoClassLoader) { 60*cdf0e10cSrcweir cl2 = ((UnoClassLoader) cl1).getClassLoader(url, attr); 61*cdf0e10cSrcweir } else { 62*cdf0e10cSrcweir cl2 = URLClassLoader.newInstance(new URL[] { url }, cl1); 63*cdf0e10cSrcweir } 64*cdf0e10cSrcweir Class c = cl2.loadClass(name); 65*cdf0e10cSrcweir synchronized (map) { 66*cdf0e10cSrcweir Class c2 = (Class) WeakMap.getValue(map.get(locationUrl)); 67*cdf0e10cSrcweir if (c2 != null) { 68*cdf0e10cSrcweir return c2; 69*cdf0e10cSrcweir } 70*cdf0e10cSrcweir map.put(locationUrl, c); 71*cdf0e10cSrcweir } 72*cdf0e10cSrcweir return c; 73*cdf0e10cSrcweir } 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir private RegistrationClassFinder() {} // do not instantiate 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir private static void checkAccess(URL url) throws ClassNotFoundException { 78*cdf0e10cSrcweir // The system property com.sun.star.comp.loader.CPLD_ACCESSPATH was 79*cdf0e10cSrcweir // introduced as a hack to restrict which UNO components can be 80*cdf0e10cSrcweir // instantiated. It seems to be unused nowadays, and should probably be 81*cdf0e10cSrcweir // replaced by the native Java security features, anyway. 82*cdf0e10cSrcweir if (accessPath != null) { 83*cdf0e10cSrcweir if (!url.getProtocol().equals("file")) { 84*cdf0e10cSrcweir throw new ClassNotFoundException( 85*cdf0e10cSrcweir "Access restriction: <" + url + "> is not a file URL"); 86*cdf0e10cSrcweir } 87*cdf0e10cSrcweir String p; 88*cdf0e10cSrcweir try { 89*cdf0e10cSrcweir p = new File(url.getFile()).getCanonicalPath(); 90*cdf0e10cSrcweir } catch (IOException e) { 91*cdf0e10cSrcweir throw new ClassNotFoundException( 92*cdf0e10cSrcweir "Access restriction: <" + url + "> is bad: " + e); 93*cdf0e10cSrcweir } 94*cdf0e10cSrcweir for (int i = 0; i < accessPath.length; ++i) { 95*cdf0e10cSrcweir String p2 = accessPath[i]; 96*cdf0e10cSrcweir if (p.startsWith(p2) && p.length() > p2.length() 97*cdf0e10cSrcweir && (p2.charAt(p2.length() - 1) == File.separatorChar 98*cdf0e10cSrcweir || p.charAt(p2.length()) == File.separatorChar)) 99*cdf0e10cSrcweir { 100*cdf0e10cSrcweir return; 101*cdf0e10cSrcweir } 102*cdf0e10cSrcweir } 103*cdf0e10cSrcweir throw new ClassNotFoundException( 104*cdf0e10cSrcweir "Access restriction: <" + url + "> is restricted"); 105*cdf0e10cSrcweir } 106*cdf0e10cSrcweir } 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir private static final WeakMap map = new WeakMap(); 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir private static final String[] accessPath; 111*cdf0e10cSrcweir static { 112*cdf0e10cSrcweir String[] ap = null; 113*cdf0e10cSrcweir String p = System.getProperty( 114*cdf0e10cSrcweir "com.sun.star.comp.loader.CPLD_ACCESSPATH"); 115*cdf0e10cSrcweir if (p != null) { 116*cdf0e10cSrcweir StringTokenizer t = new StringTokenizer(p, ";"); 117*cdf0e10cSrcweir ap = new String[t.countTokens()]; 118*cdf0e10cSrcweir int i = 0; 119*cdf0e10cSrcweir while (t.hasMoreTokens()) { 120*cdf0e10cSrcweir try { 121*cdf0e10cSrcweir ap[i] = new File(t.nextToken()).getCanonicalPath(); 122*cdf0e10cSrcweir ++i; 123*cdf0e10cSrcweir } catch (IOException e) {} 124*cdf0e10cSrcweir } 125*cdf0e10cSrcweir if (i != ap.length) { 126*cdf0e10cSrcweir String[] ap2 = new String[i]; 127*cdf0e10cSrcweir System.arraycopy(ap, 0, ap2, 0, i); 128*cdf0e10cSrcweir ap = ap2; 129*cdf0e10cSrcweir } 130*cdf0e10cSrcweir } 131*cdf0e10cSrcweir accessPath = ap; 132*cdf0e10cSrcweir } 133*cdf0e10cSrcweir } 134