1 // Copyright 2009 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.tools
.development
.agent
.impl
;
5 import com
.google
.apphosting
.runtime
.security
.WhiteList
;
8 import java
.io
.FilenameFilter
;
9 import java
.io
.IOException
;
10 import java
.util
.Collections
;
11 import java
.util
.Enumeration
;
12 import java
.util
.HashSet
;
14 import java
.util
.jar
.JarEntry
;
15 import java
.util
.jar
.JarFile
;
16 import java
.util
.logging
.Level
;
17 import java
.util
.logging
.Logger
;
20 * The list of JRE classes that we don't support in user code.
23 public class BlackList
{
25 private static final Logger logger
= Logger
.getLogger(BlackList
.class.getName());
27 private static Set
<String
> blackList
= new HashSet
<String
>();
33 private static void initBlackList() {
35 Set
<File
> jreJars
= getCurrentJreJars();
37 for (File f
: jreJars
) {
38 JarFile jarFile
= null;
40 jarFile
= new JarFile(f
);
41 } catch (IOException e
) {
42 logger
.log(Level
.SEVERE
, "Unable to read a jre library while constructing the blacklist. " +
43 "Security restrictions may not be entirely emulated. " + f
.getAbsolutePath());
47 Enumeration
<JarEntry
> entries
= jarFile
.entries();
49 while (entries
.hasMoreElements()) {
50 JarEntry entry
= entries
.nextElement();
51 String entryName
= entry
.getName();
52 if (!entryName
.endsWith(".class")) {
55 String className
= entryName
.replace('/', '.').substring(0, entryName
.length() -
57 if (isBlackListed(className
)) {
58 blackList
.add(className
.replace('.', '/'));
63 blackList
= Collections
.unmodifiableSet(blackList
);
66 private static boolean isBlackListed(String className
) {
67 Set
<String
> whiteList
= WhiteList
.getWhiteList();
68 if (whiteList
.contains(className
)) {
71 if (className
.startsWith("com.sun.xml.internal.bind.")) {
78 * Returns a list of classes that are blacklisted. Unlike the WhiteList,
79 * classes are in JVMS binary format, for example, "java/lang/String".
81 public static Set
<String
> getBlackList() {
85 private static Set
<File
> getCurrentJreJars() {
86 return getJreJars(System
.getProperty("java.home"));
89 private static Set
<File
> getJreJars(String jreHome
) {
90 Set
<File
> matchingFiles
= new HashSet
<File
>();
91 FilenameFilter filter
= new FilenameFilter() {
93 public boolean accept(File dir
, String name
) {
94 return name
.endsWith(".jar");
98 getFilesRecursive(matchingFiles
, new File(jreHome
+ File
.separator
+ "lib"), filter
);
100 return matchingFiles
;
103 private static void getFilesRecursive(final Set
<File
> matchingFiles
, final File dir
,
104 final FilenameFilter filter
) {
105 File
[] files
= dir
.listFiles();
107 for (File f
: files
) {
108 if (f
.isDirectory()) {
109 getFilesRecursive(matchingFiles
, f
, filter
);
110 } else if (filter
.accept(dir
, f
.getName())) {
111 matchingFiles
.add(f
);