Permission Test : Certificate « Security « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Security » CertificateScreenshots 
Permission Test
Permission Test
 
/**
 @version 1.00 1999-10-23
 @author Cay Horstmann
 */

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.security.Permission;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class PermissionTest {
  public static void main(String[] args) {
    System.setSecurityManager(new SecurityManager());
    JFrame f = new PermissionTestFrame();
    f.show();
  }
}

class PermissionTestFrame extends JFrame {
  public PermissionTestFrame() {
    setTitle("PermissionTest");
    setSize(400300);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    textField = new JTextField(20);
    JPanel panel = new JPanel();
    panel.add(textField);
    JButton openButton = new JButton("Insert");
    panel.add(openButton);
    openButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        insertWords(textField.getText());
      }
    });

    Container contentPane = getContentPane();
    contentPane.add(panel, "North");

    textArea = new WordCheckTextArea();
    contentPane.add(new JScrollPane(textArea)"Center");
  }

  public void insertWords(String words) {
    try {
      textArea.append(words + "\n");
    catch (SecurityException e) {
      JOptionPane.showMessageDialog(this,
          "I am sorry, but I cannot do that.");
    }
  }

  private JTextField textField;

  private WordCheckTextArea textArea;
}

class WordCheckTextArea extends JTextArea {
  public void append(String text) {
    WordCheckPermission p = new WordCheckPermission(text, "insert");
    SecurityManager manager = System.getSecurityManager();
    if (manager != null)
      manager.checkPermission(p);
    super.append(text);
  }
}

class WordCheckPermission extends Permission {
  public WordCheckPermission(String target, String anAction) {
    super(target);
    action = anAction;
  }

  public String getActions() {
    return action;
  }

  public boolean equals(Object other) {
    if (other == null)
      return false;
    if (!getClass().equals(other.getClass()))
      return false;
    WordCheckPermission b = (WordCheckPermissionother;
    if (!action.equals(b.action))
      return false;
    if (action.equals("insert"))
      return getName().equals(b.getName());
    else if (action.equals("avoid"))
      return badWordSet().equals(b.badWordSet());
    else
      return false;
  }

  public int hashCode() {
    return getName().hashCode() + action.hashCode();
  }

  public boolean implies(Permission other) {
    if (!(other instanceof WordCheckPermission))
      return false;
    WordCheckPermission b = (WordCheckPermissionother;
    if (action.equals("insert")) {
      return b.action.equals("insert")
          && getName().indexOf(b.getName()) >= 0;
    else if (action.equals("avoid")) {
      if (b.action.equals("avoid")) {
        return b.badWordSet().containsAll(badWordSet());
      else if (b.action.equals("insert")) {
        Iterator iter = badWordSet().iterator();
        while (iter.hasNext()) {
          String badWord = (Stringiter.next();
          if (b.getName().indexOf(badWord>= 0)
            return false;
        }
        return true;
      else
        return false;
    else
      return false;
  }

  public Set badWordSet() {
    StringTokenizer tokenizer = new StringTokenizer(getName()",");
    Set set = new HashSet();
    while (tokenizer.hasMoreTokens())
      set.add(tokenizer.nextToken());
    return set;
  }

  private String action;
}


//File: PermissionTest.policy
/*

grant 
{  permission WordCheckPermission "sex,drugs,C++", "avoid";
};

*/


           
         
  
Related examples in the same category
1. Certificate Signer
2. Signature Test
3. Security Manager TestSecurity Manager Test
4. Specify the keystore of certificates using the javax.net.ssl.keyStore system property:
5. Retrieving a Certificate from a Key Store
6. Adding a Certificate to a Key Store
7. Creating a Certification Path
8. Listing the Most-Trusted Certificate Authorities (CA) in a Key Store
9. Validating a Certification Path using the most-trusted CAs in the JDK's cacerts file.
10. Importing a Certificate from a File
11. Retrieving the Certification Path of an SSL Server
12. Getting the Subject and Issuer Distinguished Names of an X509 Certificate
ww__w.__j_ava___2_s___.c___o___m_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.