/**
* @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(400, 300);
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 = (WordCheckPermission) other;
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 = (WordCheckPermission) other;
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 = (String) iter.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";
};
*/
|