|
1 /* |
|
2 * Copyright 2008-2009 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 package sun.dyn; |
|
27 |
|
28 import sun.reflect.Reflection; |
|
29 |
|
30 /** |
|
31 * Access control to this package. |
|
32 * Classes in other packages can attempt to acquire the access token, |
|
33 * but will fail if they are not recognized as friends. |
|
34 * Certain methods in this package, although public, require a non-null |
|
35 * access token in order to proceed; they act like package-private methods. |
|
36 * @author jrose |
|
37 */ |
|
38 |
|
39 public class Access { |
|
40 |
|
41 private Access() { } |
|
42 |
|
43 /** |
|
44 * The heart of this pattern: The list of classes which are |
|
45 * permitted to acquire the access token, and become honorary |
|
46 * members of this package. |
|
47 */ |
|
48 private static final String[] FRIENDS = { |
|
49 "java.dyn.", "sun.dyn." |
|
50 }; |
|
51 |
|
52 /** |
|
53 * The following object is NOT public. That's the point of the pattern. |
|
54 * It is package-private, so that any member of this package |
|
55 * can acquire the access token, and give it away to trusted friends. |
|
56 */ |
|
57 static final Access TOKEN = new Access(); |
|
58 |
|
59 /** |
|
60 * @return Access.TOKEN, if the caller is a friend of this package |
|
61 */ |
|
62 public static Access getToken() { |
|
63 Class<?> callc = Reflection.getCallerClass(2); |
|
64 if (isFriend(callc)) |
|
65 return TOKEN; |
|
66 else |
|
67 throw new IllegalAccessError("bad caller: " + callc); |
|
68 } |
|
69 |
|
70 /** Is the given name the name of a class which could be our friend? */ |
|
71 public static boolean isFriendName(String name) { |
|
72 for (String friend : FRIENDS) { |
|
73 if (name.startsWith(friend)) |
|
74 return true; |
|
75 } |
|
76 return false; |
|
77 } |
|
78 |
|
79 /** Is the given class a friend? True if {@link #isFriendName}, |
|
80 * and the given class also shares a class loader with us. |
|
81 */ |
|
82 public static boolean isFriend(Class<?> c) { |
|
83 return isFriendName(c.getName()) && c.getClassLoader() == CLASS_LOADER; |
|
84 } |
|
85 |
|
86 private static final ClassLoader CLASS_LOADER = Access.class.getClassLoader(); |
|
87 |
|
88 /** |
|
89 * Throw an IllegalAccessError if the caller does not possess |
|
90 * the Access.TOKEN. |
|
91 * @param must be Access.TOKEN |
|
92 */ |
|
93 public static void check(Access token) { |
|
94 if (token == null) |
|
95 fail(); |
|
96 // else it must be the unique Access.TOKEN |
|
97 assert(token == Access.TOKEN); |
|
98 } |
|
99 private static void fail() { |
|
100 final int CALLER_DEPTH = 3; |
|
101 // 0: Reflection.getCC, 1: this.fail, 2: Access.*, 3: caller |
|
102 Class<?> callc = Reflection.getCallerClass(CALLER_DEPTH); |
|
103 throw new IllegalAccessError("bad caller: " + callc); |
|
104 } |
|
105 |
|
106 static { |
|
107 //sun.reflect.Reflection.registerMethodsToFilter(MH.class, "getToken"); |
|
108 } |
|
109 } |