01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.util;
07:
08: import java.sql.SQLException;
09: import java.util.ArrayList;
10: import java.util.HashSet;
11:
12: import org.h2.constant.ErrorCode;
13: import org.h2.constant.SysProperties;
14: import org.h2.message.Message;
15:
16: /**
17: * This utility class contains functions related to class loading.
18: * There is a mechanism to restrict class loading.
19: */
20: public class ClassUtils {
21:
22: private static final boolean ALLOW_ALL;
23: private static final HashSet ALLOWED_CLASS_NAMES = new HashSet();
24: private static final String[] ALLOWED_CLASS_NAME_PREFIXES;
25:
26: static {
27: String s = SysProperties.ALLOWED_CLASSES;
28: String[] list = StringUtils.arraySplit(s, ',', true);
29: ArrayList prefixes = new ArrayList();
30: boolean allowAll = false;
31: for (int i = 0; i < list.length; i++) {
32: String p = list[i];
33: if (p.equals("*")) {
34: allowAll = true;
35: } else if (p.endsWith("*")) {
36: prefixes.add(p.substring(0, p.length() - 1));
37: } else {
38: ALLOWED_CLASS_NAMES.add(p);
39: }
40: }
41: ALLOW_ALL = allowAll;
42: ALLOWED_CLASS_NAME_PREFIXES = new String[prefixes.size()];
43: prefixes.toArray(ALLOWED_CLASS_NAME_PREFIXES);
44: }
45:
46: public static Class loadSystemClass(String className)
47: throws ClassNotFoundException {
48: return Class.forName(className);
49: }
50:
51: public static Class loadUserClass(String className)
52: throws ClassNotFoundException, SQLException {
53: if (!ALLOW_ALL && !ALLOWED_CLASS_NAMES.contains(className)) {
54: boolean allowed = false;
55: for (int i = 0; i < ALLOWED_CLASS_NAME_PREFIXES.length; i++) {
56: String s = ALLOWED_CLASS_NAME_PREFIXES[i];
57: if (className.startsWith(s)) {
58: allowed = true;
59: }
60: }
61: if (!allowed) {
62: throw Message.getSQLException(
63: ErrorCode.ACCESS_DENIED_TO_CLASS_1, className);
64: }
65: }
66: return Class.forName(className);
67: }
68:
69: }
|