Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / tools / development / agent / impl / BlackList.java
blobf3e1576ad1b419991c56d12cb997c95f3eb945ac
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;
7 import java.io.File;
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;
13 import java.util.Set;
14 import java.util.jar.JarEntry;
15 import java.util.jar.JarFile;
16 import java.util.logging.Level;
17 import java.util.logging.Logger;
19 /**
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>();
29 static {
30 initBlackList();
33 private static void initBlackList() {
35 Set<File> jreJars = getCurrentJreJars();
37 for (File f : jreJars) {
38 JarFile jarFile = null;
39 try {
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());
44 continue;
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")) {
53 continue;
55 String className = entryName.replace('/', '.').substring(0, entryName.length() -
56 ".class".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)) {
69 return false;
71 if (className.startsWith("com.sun.xml.internal.bind.")) {
72 return false;
74 return true;
77 /**
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() {
82 return blackList;
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() {
92 @Override
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);