/hg/icedtea-web: PolicyEditor file/model logic extracted into Po...
aazores at icedtea.classpath.org
aazores at icedtea.classpath.org
Mon Jun 2 13:35:32 UTC 2014
changeset 428af70f3b7c in /hg/icedtea-web
details: http://icedtea.classpath.org/hg/icedtea-web?cmd=changeset;node=428af70f3b7c
author: Andrew Azores <aazores at redhat.com>
date: Mon Jun 02 09:35:20 2014 -0400
PolicyEditor file/model logic extracted into PolicyFileModel
2014-06-02 Andrew Azores <aazores at redhat.com>
* netx/net/sourceforge/jnlp/security/policyeditor/CustomPolicyViewer.java:
store PolicyFileModel as field. (updateCustomPermissions): new method
* netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java
(codebasePermissionsMap, customPermissionsMap, file, fileWatcher,
savePolicyFile, openAndParsePolicyFile): Policy file model logic extracted
into new PolicyFileModel class. (policyFile) new PolicyFileModel field.
(addNewCodebase, getCodebases, getPermissions, getCustomPermissions,
updateCheckboxes, updateCheckboxesImpl, updateCustomPermissions,
resetCodebases, openAndParsePolicyFile, savePolicyFile,
initializeMapForCodebase, checkPolicyChangesWithDialog): refactored to use
PolicyFileModel
* netx/net/sourceforge/jnlp/security/policyeditor/PolicyFileModel.java:
new class for modelling Policy files
* tests/netx/unit/net/sourceforge/jnlp/security/policyeditor/PolicyEditorParsingTest.java:
update to use PolicyEditor's policyFile field
* tests/netx/unit/net/sourceforge/jnlp/security/policyeditor/PolicyEditorTest.java
(testReturnedCustomPermissionsSetIsCopy): initial assertion of empty set
added, final assertion of empty set rephrased
(testReturnedCodebasesAreCopy) renamed testReturnedCodebasesIsCopy,
rephrased and using assertEquals rather than assertTrue
diffstat:
ChangeLog | 23 +
netx/net/sourceforge/jnlp/security/policyeditor/CustomPolicyViewer.java | 33 +-
netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java | 400 ++-------
netx/net/sourceforge/jnlp/security/policyeditor/PolicyFileModel.java | 283 +++++++
tests/netx/unit/net/sourceforge/jnlp/security/policyeditor/PolicyEditorParsingTest.java | 9 +-
tests/netx/unit/net/sourceforge/jnlp/security/policyeditor/PolicyEditorTest.java | 15 +-
6 files changed, 445 insertions(+), 318 deletions(-)
diffs (truncated from 1097 to 500 lines):
diff -r ef96a1c8776b -r 428af70f3b7c ChangeLog
--- a/ChangeLog Fri May 30 16:07:45 2014 -0400
+++ b/ChangeLog Mon Jun 02 09:35:20 2014 -0400
@@ -1,3 +1,26 @@
+2014-06-02 Andrew Azores <aazores at redhat.com>
+
+ * netx/net/sourceforge/jnlp/security/policyeditor/CustomPolicyViewer.java:
+ store PolicyFileModel as field. (updateCustomPermissions): new method
+ * netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java
+ (codebasePermissionsMap, customPermissionsMap, file, fileWatcher,
+ savePolicyFile, openAndParsePolicyFile): Policy file model logic extracted
+ into new PolicyFileModel class. (policyFile) new PolicyFileModel field.
+ (addNewCodebase, getCodebases, getPermissions, getCustomPermissions,
+ updateCheckboxes, updateCheckboxesImpl, updateCustomPermissions,
+ resetCodebases, openAndParsePolicyFile, savePolicyFile,
+ initializeMapForCodebase, checkPolicyChangesWithDialog): refactored to use
+ PolicyFileModel
+ * netx/net/sourceforge/jnlp/security/policyeditor/PolicyFileModel.java:
+ new class for modelling Policy files
+ * tests/netx/unit/net/sourceforge/jnlp/security/policyeditor/PolicyEditorParsingTest.java:
+ update to use PolicyEditor's policyFile field
+ * tests/netx/unit/net/sourceforge/jnlp/security/policyeditor/PolicyEditorTest.java
+ (testReturnedCustomPermissionsSetIsCopy): initial assertion of empty set
+ added, final assertion of empty set rephrased
+ (testReturnedCodebasesAreCopy) renamed testReturnedCodebasesIsCopy,
+ rephrased and using assertEquals rather than assertTrue
+
2014-05-29 Jie Kang <jkang at redhat.com>
Added reproducer for PR1794: Bug where Java variables are not accessed
diff -r ef96a1c8776b -r 428af70f3b7c netx/net/sourceforge/jnlp/security/policyeditor/CustomPolicyViewer.java
--- a/netx/net/sourceforge/jnlp/security/policyeditor/CustomPolicyViewer.java Fri May 30 16:07:45 2014 -0400
+++ b/netx/net/sourceforge/jnlp/security/policyeditor/CustomPolicyViewer.java Mon Jun 02 09:35:20 2014 -0400
@@ -56,6 +56,7 @@
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
+import javax.swing.ScrollPaneConstants;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
@@ -74,17 +75,23 @@
private final JLabel listLabel = new JLabel();
private final ActionListener addButtonAction, removeButtonAction, closeButtonAction;
private final WeakReference<CustomPolicyViewer> weakThis = new WeakReference<>(this);
+ private final PolicyFileModel policyFile;
+ private final PolicyEditor parent;
+ private final String codebase;
/**
* @param parent the parent PolicyEditor which created this CustomPolicyViewer
* @param codebase the codebase for which these custom permissions are enabled
- * @param permissions a collection of CustomPermissions which were found in the policy file
+ * @param policyFile the PolicyFileModel
*/
- public CustomPolicyViewer(final PolicyEditor parent, final String codebase, final Collection<CustomPermission> permissions) {
+ public CustomPolicyViewer(final PolicyEditor parent, final String codebase, final PolicyFileModel policyFile) {
super();
+ this.parent = parent;
+ this.codebase = codebase;
+ this.policyFile = policyFile;
setLayout(new GridBagLayout());
setTitle(R("PECPTitle"));
- customPermissions.addAll(permissions);
+ customPermissions.addAll(policyFile.getCopyOfCustomPermissions().get(codebase));
for (final CustomPermission perm : customPermissions) {
listModel.addElement(perm);
}
@@ -112,7 +119,7 @@
if (perm != null) {
customPermissions.add(perm);
listModel.addElement(perm);
- parent.updateCustomPermissions(codebase, customPermissions);
+ updateCustomPermissions();
}
}
};
@@ -127,7 +134,7 @@
}
customPermissions.remove(list.getSelectedValue());
listModel.removeElement(list.getSelectedValue());
- parent.updateCustomPermissions(codebase, customPermissions);
+ updateCustomPermissions();
}
};
removeButton.setText(R("PECPRemoveButton"));
@@ -152,8 +159,8 @@
}
listLabel.setText(R("PECPListLabel", codebaseText));
- scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
- scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
+ scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
+ scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
list.setSelectedIndex(0);
setupLayout();
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
@@ -209,4 +216,16 @@
setMinimumSize(getPreferredSize());
}
+ /**
+ * Update the custom permissions map. Used by the Custom Policy Viewer to update its parent
+ * PolicyEditor to changes it has made
+ * @param codebase the codebase for which changes were made
+ * @param permissions the permissions granted to this codebase
+ */
+ private void updateCustomPermissions() {
+ parent.setChangesMade();
+ policyFile.clearCustomCodebase(codebase);
+ policyFile.addCustomPermissions(codebase, customPermissions);
+ }
+
}
diff -r ef96a1c8776b -r 428af70f3b7c netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java
--- a/netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java Fri May 30 16:07:45 2014 -0400
+++ b/netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java Mon Jun 02 09:35:20 2014 -0400
@@ -58,13 +58,9 @@
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
-import java.nio.channels.FileLock;
-import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Calendar;
import java.util.Collection;
-import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
@@ -72,8 +68,6 @@
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
import javax.swing.AbstractAction;
import javax.swing.AbstractButton;
@@ -95,6 +89,7 @@
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.ListSelectionModel;
+import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
@@ -106,7 +101,6 @@
import net.sourceforge.jnlp.security.policyeditor.PolicyEditorPermissions.Group;
import net.sourceforge.jnlp.util.FileUtils;
import net.sourceforge.jnlp.util.FileUtils.OpenFileResult;
-import net.sourceforge.jnlp.util.MD5SumWatcher;
import net.sourceforge.jnlp.util.logging.OutputController;
/**
@@ -160,13 +154,9 @@
+ " " + FILE_FLAG + "\t\t\t" + R("PEFileFlag") + "\n"
+ " " + CODEBASE_FLAG + "\t\t" + R("PECodebaseFlag") + "\n";
- private static final String AUTOGENERATED_NOTICE = "/* DO NOT MODIFY! AUTO-GENERATED */";
-
- private File file;
+ private final PolicyFileModel policyFile = new PolicyFileModel();
private boolean changesMade = false;
private boolean closed = false;
- private final Map<String, Map<PolicyEditorPermissions, Boolean>> codebasePermissionsMap = new HashMap<>();
- private final Map<String, Set<CustomPermission>> customPermissionsMap = new HashMap<>();
private final Map<PolicyEditorPermissions, JCheckBox> checkboxMap = new TreeMap<>();
private final List<JCheckBoxWithGroup> groupBoxList = new ArrayList<>(Group.values().length);
private final JScrollPane scrollPane = new JScrollPane();
@@ -177,7 +167,6 @@
private final JFileChooser fileChooser;
private CustomPolicyViewer cpViewer = null;
private final WeakReference<PolicyEditor> weakThis = new WeakReference<>(this);
- private MD5SumWatcher fileWatcher;
private final ActionListener okButtonAction, addCodebaseButtonAction,
removeCodebaseButtonAction, openButtonAction, saveAsButtonAction, viewCustomButtonAction;
@@ -187,9 +176,9 @@
private final PolicyEditorPermissions.Group group;
- private JCheckBoxWithGroup(Group g) {
- super(g.getTitle());
- group = g;
+ private JCheckBoxWithGroup(Group group) {
+ super(group.getTitle());
+ this.group = group;
}
public Group getGroup() {
@@ -233,27 +222,29 @@
}
if (filepath != null) {
- file = new File(filepath);
+ policyFile.setFile(new File(filepath));
openAndParsePolicyFile();
} else {
resetCodebases();
+ addNewCodebase("");
}
+ changesMade = false;
- fileChooser = new JFileChooser(file);
+ fileChooser = new JFileChooser(policyFile.getFile());
fileChooser.setFileHidingEnabled(false);
okButtonAction = new ActionListener() {
@Override
public void actionPerformed(final ActionEvent event) {
- if (file == null) {
+ if (policyFile.getFile() == null) {
final int choice = fileChooser.showOpenDialog(weakThis.get());
if (choice == JFileChooser.APPROVE_OPTION) {
- file = fileChooser.getSelectedFile();
+ policyFile.setFile(fileChooser.getSelectedFile());
}
}
// May still be null if user cancelled the file chooser
- if (file != null) {
+ if (policyFile.getFile() != null) {
savePolicyFile();
}
}
@@ -285,10 +276,10 @@
if (changesMade) {
final int save = JOptionPane.showConfirmDialog(weakThis.get(), R("PESaveChanges"));
if (save == JOptionPane.YES_OPTION) {
- if (file == null) {
+ if (policyFile.getFile() == null) {
final int choice = fileChooser.showSaveDialog(weakThis.get());
if (choice == JFileChooser.APPROVE_OPTION) {
- file = fileChooser.getSelectedFile();
+ policyFile.setFile(fileChooser.getSelectedFile());
} else if (choice == JFileChooser.CANCEL_OPTION) {
return;
}
@@ -300,7 +291,7 @@
}
final int choice = fileChooser.showOpenDialog(weakThis.get());
if (choice == JFileChooser.APPROVE_OPTION) {
- file = fileChooser.getSelectedFile();
+ policyFile.setFile(fileChooser.getSelectedFile());
openAndParsePolicyFile();
}
}
@@ -311,7 +302,7 @@
public void actionPerformed(final ActionEvent e) {
final int choice = fileChooser.showSaveDialog(weakThis.get());
if (choice == JFileChooser.APPROVE_OPTION) {
- file = fileChooser.getSelectedFile();
+ policyFile.setFile(fileChooser.getSelectedFile());
changesMade = true;
savePolicyFile();
}
@@ -329,7 +320,7 @@
return;
}
if (cpViewer == null) {
- cpViewer = new CustomPolicyViewer(weakThis.get(), codebase, customPermissionsMap.get(codebase));
+ cpViewer = new CustomPolicyViewer(PolicyEditor.this, codebase, policyFile);
cpViewer.setVisible(true);
} else {
cpViewer.toFront();
@@ -654,19 +645,21 @@
*/
public void addNewCodebase(final String codebase) {
try {
- new URL(codebase);
- } catch (MalformedURLException mfue) {
+ if (!codebase.isEmpty()) {
+ new URL(codebase);
+ }
+ } catch (final MalformedURLException mfue) {
OutputController.getLogger().log("Could not add codebase " + codebase);
OutputController.getLogger().log(mfue);
return;
}
- final boolean existingCodebase = initializeMapForCodebase(codebase);
final String model;
if (codebase.isEmpty()) {
model = R("PEGlobalSettings");
} else {
model = codebase;
}
+ final boolean existingCodebase = policyFile.addCodebase(codebase);
if (!existingCodebase) {
SwingUtilities.invokeLater(new Runnable() {
@Override
@@ -746,7 +739,7 @@
if (previousIndex < 0) {
previousIndex = 0;
}
- codebasePermissionsMap.remove(codebase);
+ policyFile.removeCodebase(codebase);
final int fIndex = previousIndex;
SwingUtilities.invokeLater(new Runnable() {
@Override
@@ -758,40 +751,23 @@
changesMade = true;
}
- /**
- * @return the set of Codebase entries in the policy file
- */
public Set<String> getCodebases() {
- return new HashSet<String>(codebasePermissionsMap.keySet());
+ return new HashSet<>(policyFile.getCopyOfPermissions().keySet());
}
- /**
- * @param codebase the codebase to query
- * @return a map of permissions to whether these permissions are set for the given codebase
- */
public Map<PolicyEditorPermissions, Boolean> getPermissions(final String codebase) {
- final Map<PolicyEditorPermissions, Boolean> permissions = codebasePermissionsMap.get(codebase);
- if (permissions != null) {
- return new HashMap<PolicyEditorPermissions, Boolean>(permissions);
- } else {
- final Map<PolicyEditorPermissions, Boolean> blank = new HashMap<>();
- for (final PolicyEditorPermissions perm : PolicyEditorPermissions.values()) {
- blank.put(perm, false);
- }
- return blank;
- }
+ policyFile.addCodebase(codebase);
+ return new HashMap<>(policyFile.getCopyOfPermissions().get(codebase));
}
- /**
- * @param codebase the codebase to query
- * @return a collection of CustomPermissions granted to the given codebase
- */
public Collection<CustomPermission> getCustomPermissions(final String codebase) {
- final Collection<CustomPermission> permissions = customPermissionsMap.get(codebase);
- if (permissions != null) {
- return new HashSet<CustomPermission>(permissions);
- } else {
- return Collections.emptySet();
+ policyFile.addCodebase(codebase);
+ return new HashSet<>(policyFile.getCopyOfCustomPermissions().get(codebase));
+ }
+
+ private void updateCheckboxes() {
+ for (String codebase : policyFile.getCopyOfPermissions().keySet()) {
+ updateCheckboxes(codebase);
}
}
@@ -829,30 +805,19 @@
for (final ActionListener l : box.getActionListeners()) {
box.removeActionListener(l);
}
- initializeMapForCodebase(codebase);
- final Map<PolicyEditorPermissions, Boolean> map = codebasePermissionsMap.get(codebase);
- final boolean state;
- if (map != null) {
- final Boolean s = map.get(perm);
- if (s != null) {
- state = s;
- } else {
- state = false;
- }
- } else {
- state = false;
- }
+ policyFile.addCodebase(codebase);
+ final boolean state = policyFile.getPermission(codebase, perm);
for (final JCheckBoxWithGroup jg : groupBoxList) {
- jg.setState(map);
+ jg.setState(policyFile.getCopyOfPermissions().get(codebase));
}
box.setSelected(state);
box.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
changesMade = true;
- map.put(perm, box.isSelected());
+ policyFile.setPermission(codebase, perm, box.isSelected());
for (JCheckBoxWithGroup jg : groupBoxList) {
- jg.setState(map);
+ jg.setState(policyFile.getCopyOfPermissions().get(codebase));
}
}
});
@@ -1003,9 +968,8 @@
backup.add(l);
groupCh.removeActionListener(l);
}
- final Map<PolicyEditorPermissions, Boolean> map = codebasePermissionsMap.get(codebase);
for (final PolicyEditorPermissions p : groupCh.getGroup().getPermissions()) {
- map.put(p, groupCh.isSelected());
+ policyFile.setPermission(codebase, p, groupCh.isSelected());
}
changesMade = true;
updateCheckboxes(codebase);
@@ -1066,8 +1030,8 @@
});
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
- scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
+ scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
+ scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setViewportView(list);
final GridBagConstraints listConstraints = new GridBagConstraints();
listConstraints.fill = GridBagConstraints.BOTH;
@@ -1111,243 +1075,79 @@
setMinimumSize(getPreferredSize());
}
- /**
- * Update the custom permissions map. Used by the Custom Policy Viewer to update its parent
- * PolicyEditor to changes it has made
- * @param codebase the codebase for which changes were made
- * @param permissions the permissions granted to this codebase
- */
- void updateCustomPermissions(final String codebase, final Collection<CustomPermission> permissions) {
+ void setChangesMade() {
changesMade = true;
- customPermissionsMap.get(codebase).clear();
- customPermissionsMap.get(codebase).addAll(permissions);
}
private void resetCodebases() {
listModel.clear();
- codebasePermissionsMap.clear();
- customPermissionsMap.clear();
-
- initializeMapForCodebase("");
- listModel.addElement(R("PEGlobalSettings"));
- list.setSelectedValue(R("PEGlobalSettings"), true);
- updateCheckboxes("");
+ policyFile.clearPermissions();
+ policyFile.clearCustomPermissions();
}
- /**
- * Open the file pointed to by the filePath field. This is either provided by the
- * "-file" command line flag, or if none given, comes from DeploymentConfiguration.
- */
private void openAndParsePolicyFile() {
- new Thread() {
- @Override
- public void run() {
- resetCodebases();
-
- if (!file.exists()) {
- try {
- file.createNewFile();
- } catch (final IOException e) {
- OutputController.getLogger().log(e);
- // If this fails we'll end up handling it a few lines down anyway.
- }
+ resetCodebases();
+ try {
+ policyFile.getFile().createNewFile();
+ } catch (final IOException e) {
+ OutputController.getLogger().log(e);
+ }
+ final OpenFileResult ofr = FileUtils.testFilePermissions(policyFile.getFile());
+ if (ofr == OpenFileResult.FAILURE || ofr == OpenFileResult.NOT_FILE) {
+ FileUtils.showCouldNotOpenFilepathDialog(weakThis.get(), policyFile.getFile().getPath());
+ return;
+ }
+ if (ofr == OpenFileResult.CANT_WRITE) {
+ FileUtils.showReadOnlyDialog(weakThis.get());
+ }
+ try {
+ policyFile.openAndParsePolicyFile();
+ changesMade = false;
+ for (final String codebase : policyFile.getCodebases()) {
+ final String model;
+ if (codebase.isEmpty()) {
+ model = R("PEGlobalSettings");
+ } else {
+ model = codebase;
}
- final OpenFileResult ofr = FileUtils.testFilePermissions(file);
- if (ofr == OpenFileResult.FAILURE || ofr == OpenFileResult.NOT_FILE) {
- FileUtils.showCouldNotOpenFilepathDialog(weakThis.get(), file.getPath());
- return;
- }
- if (ofr == OpenFileResult.CANT_WRITE) {
- FileUtils.showReadOnlyDialog(weakThis.get());
- }
- final String contents;
- try {
- fileWatcher = new MD5SumWatcher(file);
- fileWatcher.update();
- // User-level policy files are expected to be short enough that loading them in as a String
- // should not actually be *too* bad, and it's easy to work with.
- contents = FileUtils.loadFileAsString(file);
- } catch (final IOException e) {
- OutputController.getLogger().log(e);
- OutputController.getLogger().log(OutputController.Level.ERROR_ALL, R("RCantOpenFile", file.getPath()));
- FileUtils.showCouldNotOpenDialog(weakThis.get(), R("PECouldNotOpen"));
More information about the distro-pkg-dev
mailing list