⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wordchecksecuritymanager.java

📁 这是java2核心技术第七版的源代码
💻 JAVA
字号:
/**
   @version 1.20 2004-09-11
   @author Cay Horstmann
*/

import java.io.*;
import java.security.*;
import java.util.*;

/**
   This security manager checks whether bad words are
   encountered when reading a file.
*/
public class WordCheckSecurityManager extends SecurityManager
{  
   public void checkPermission(Permission p)
   {  
      if (p instanceof FilePermission && p.getActions().equals("read"))
      {  
         if (inSameManager())
            return;
         String fileName = p.getName();
         if (containsBadWords(fileName))
            throw new SecurityException("Bad words in " + fileName);
      }
      else super.checkPermission(p);
   }

   /**
      Returns true if this manager is called while there
      is another call to itself pending.
      @return true if there are multiple calls to this manager
   */
   public boolean inSameManager()
   {  
      Class[] cc = getClassContext();

      // skip past current set of calls to this manager
      int i = 0;
      while (i < cc.length && cc[0] == cc[i])
         i++;

      // check if there is another call to this manager
      while (i < cc.length)
      {  
         if (cc[0] == cc[i]) return true;
         i++;
      }
      return false;
   }

   /**
      Checks if a file contains bad words.
      @param fileName the name of the file
      @return true if the file name ends with .txt and it 
      contains at least one bad word.
   */
   boolean containsBadWords(String fileName)
   {  
      if (!fileName.toLowerCase().endsWith(".txt")) return false;
         // only check text files
      Scanner in; 
      try
      {
         in = new Scanner(new FileReader(fileName));
      }
      catch (IOException e)
      {
         return false;
      }
      while (in.hasNext())
         if (badWords.contains(in.next().toLowerCase()))
         {
            in.close();
            System.out.println(fileName);
            return true;
         }
      return false;
   }

   private List badWords = Arrays.asList(new String[] { "sex", "drugs", "c++" });
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -