1 /*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
1039 public static final String SAVE_AS_POLICY_FILE = "Save.As";
1040 public static final String VIEW_WARNINGS = "View.Warning.Log";
1041 public static final String QUIT = "Exit";
1042 public static final String ADD_POLICY_ENTRY = "Add.Policy.Entry";
1043 public static final String EDIT_POLICY_ENTRY = "Edit.Policy.Entry";
1044 public static final String REMOVE_POLICY_ENTRY = "Remove.Policy.Entry";
1045 public static final String EDIT_KEYSTORE = "Edit";
1046 public static final String ADD_PUBKEY_ALIAS = "Add.Public.Key.Alias";
1047 public static final String REMOVE_PUBKEY_ALIAS = "Remove.Public.Key.Alias";
1048
1049 /* gridbag index for components in the main window (MW) */
1050 public static final int MW_FILENAME_LABEL = 0;
1051 public static final int MW_FILENAME_TEXTFIELD = 1;
1052 public static final int MW_PANEL = 2;
1053 public static final int MW_ADD_BUTTON = 0;
1054 public static final int MW_EDIT_BUTTON = 1;
1055 public static final int MW_REMOVE_BUTTON = 2;
1056 public static final int MW_POLICY_LIST = 3; // follows MW_PANEL
1057
1058 /* The preferred height of JTextField should match JComboBox. */
1059 static final int TEXTFIELD_HEIGHT = new JComboBox().getPreferredSize().height;
1060
1061 private PolicyTool tool;
1062
1063 /**
1064 * Constructor
1065 */
1066 ToolWindow(PolicyTool tool) {
1067 this.tool = tool;
1068 }
1069
1070 /**
1071 * Don't call getComponent directly on the window
1072 */
1073 public Component getComponent(int n) {
1074 Component c = getContentPane().getComponent(n);
1075 if (c instanceof JScrollPane) {
1076 c = ((JScrollPane)c).getViewport().getView();
1077 }
1078 return c;
1079 }
1152 LR_PADDING);
1153
1154 addNewComponent(this, panel, MW_PANEL,
1155 0, 2, 2, 1, 0.0, 0.0, GridBagConstraints.BOTH,
1156 BOTTOM_PADDING);
1157
1158
1159 String policyFile = tool.getPolicyFileName();
1160 if (policyFile == null) {
1161 String userHome;
1162 userHome = java.security.AccessController.doPrivileged(
1163 new sun.security.action.GetPropertyAction("user.home"));
1164 policyFile = userHome + File.separatorChar + ".java.policy";
1165 }
1166
1167 try {
1168 // open the policy file
1169 tool.openPolicy(policyFile);
1170
1171 // display the policy entries via the policy list textarea
1172 DefaultListModel listModel = new DefaultListModel();
1173 JList list = new JList(listModel);
1174 list.setVisibleRowCount(15);
1175 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
1176 list.addMouseListener(new PolicyListListener(tool, this));
1177 PolicyEntry entries[] = tool.getEntry();
1178 if (entries != null) {
1179 for (int i = 0; i < entries.length; i++) {
1180 listModel.addElement(entries[i].headerToString());
1181 }
1182 }
1183 JTextField newFilename = (JTextField)
1184 getComponent(MW_FILENAME_TEXTFIELD);
1185 newFilename.setText(policyFile);
1186 initPolicyList(list);
1187
1188 } catch (FileNotFoundException fnfe) {
1189 // add blank policy listing
1190 JList list = new JList(new DefaultListModel());
1191 list.setVisibleRowCount(15);
1192 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
1193 list.addMouseListener(new PolicyListListener(tool, this));
1194 initPolicyList(list);
1195 tool.setPolicyFileName(null);
1196 tool.modified = false;
1197
1198 // just add warning
1199 tool.warnings.addElement(fnfe.toString());
1200
1201 } catch (Exception e) {
1202 // add blank policy listing
1203 JList list = new JList(new DefaultListModel());
1204 list.setVisibleRowCount(15);
1205 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
1206 list.addMouseListener(new PolicyListListener(tool, this));
1207 initPolicyList(list);
1208 tool.setPolicyFileName(null);
1209 tool.modified = false;
1210
1211 // display the error
1212 MessageFormat form = new MessageFormat(PolicyTool.getMessage
1213 ("Could.not.open.policy.file.policyFile.e.toString."));
1214 Object[] source = {policyFile, e.toString()};
1215 displayErrorDialog(null, form.format(source));
1216 }
1217 }
1218
1219
1220 // Platform specific modifier (control / command).
1221 private int shortCutModifier = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
1222
1223 private void addMenuItem(JMenu menu, String key, ActionListener actionListener, String accelerator) {
1299
1300
1301 /**
1302 * Add a component to the PolicyTool window without external padding
1303 */
1304 void addNewComponent(Container container, JComponent component,
1305 int index, int gridx, int gridy, int gridwidth, int gridheight,
1306 double weightx, double weighty, int fill) {
1307
1308 // delegate with "null" external padding
1309 addNewComponent(container, component, index, gridx, gridy,
1310 gridwidth, gridheight, weightx, weighty,
1311 fill, null);
1312 }
1313
1314
1315 /**
1316 * Init the policy_entry_list TEXTAREA component in the
1317 * PolicyTool window
1318 */
1319 void initPolicyList(JList policyList) {
1320
1321 // add the policy list to the window
1322 //policyList.setPreferredSize(new Dimension(500, 350));
1323 JScrollPane scrollPane = new JScrollPane(policyList);
1324 addNewComponent(this, scrollPane, MW_POLICY_LIST,
1325 0, 3, 2, 1, 1.0, 1.0, GridBagConstraints.BOTH);
1326 }
1327
1328 /**
1329 * Replace the policy_entry_list TEXTAREA component in the
1330 * PolicyTool window with an updated one.
1331 */
1332 void replacePolicyList(JList policyList) {
1333
1334 // remove the original list of Policy Entries
1335 // and add the new list of entries
1336 JList list = (JList)getComponent(MW_POLICY_LIST);
1337 list.setModel(policyList.getModel());
1338 }
1339
1340 /**
1341 * display the main PolicyTool window
1342 */
1343 void displayToolWindow(String args[]) {
1344
1345 setTitle(PolicyTool.getMessage("Policy.Tool"));
1346 setResizable(true);
1347 addWindowListener(new ToolWindowListener(tool, this));
1348 //setBounds(135, 80, 500, 500);
1349 getContentPane().setLayout(new GridBagLayout());
1350
1351 initWindow();
1352 pack();
1353 setLocationRelativeTo(null);
1354
1355 // display it
1356 setVisible(true);
1651 public static final int USC_YES_BUTTON = 0;
1652 public static final int USC_NO_BUTTON = 1;
1653 public static final int USC_CANCEL_BUTTON = 2;
1654
1655 /* gridbag index for the ConfirmRemovePolicyEntryDialog (CRPE) */
1656 public static final int CRPE_LABEL1 = 0;
1657 public static final int CRPE_LABEL2 = 1;
1658 public static final int CRPE_PANEL = 2;
1659 public static final int CRPE_PANEL_OK = 0;
1660 public static final int CRPE_PANEL_CANCEL = 1;
1661
1662 /* some private static finals */
1663 private static final int PERMISSION = 0;
1664 private static final int PERMISSION_NAME = 1;
1665 private static final int PERMISSION_ACTIONS = 2;
1666 private static final int PERMISSION_SIGNEDBY = 3;
1667 private static final int PRINCIPAL_TYPE = 4;
1668 private static final int PRINCIPAL_NAME = 5;
1669
1670 /* The preferred height of JTextField should match JComboBox. */
1671 static final int TEXTFIELD_HEIGHT = new JComboBox().getPreferredSize().height;
1672
1673 public static java.util.ArrayList<Perm> PERM_ARRAY;
1674 public static java.util.ArrayList<Prin> PRIN_ARRAY;
1675 PolicyTool tool;
1676 ToolWindow tw;
1677
1678 static {
1679
1680 // set up permission objects
1681
1682 PERM_ARRAY = new java.util.ArrayList<Perm>();
1683 PERM_ARRAY.add(new AllPerm());
1684 PERM_ARRAY.add(new AudioPerm());
1685 PERM_ARRAY.add(new AuthPerm());
1686 PERM_ARRAY.add(new AWTPerm());
1687 PERM_ARRAY.add(new DelegationPerm());
1688 PERM_ARRAY.add(new FilePerm());
1689 PERM_ARRAY.add(new URLPerm());
1690 PERM_ARRAY.add(new InqSecContextPerm());
1691 PERM_ARRAY.add(new LogPerm());
1794 TaggedList prinList = new TaggedList(3, false);
1795 prinList.getAccessibleContext().setAccessibleName(
1796 PolicyTool.getMessage("Principal.List"));
1797 prinList.addMouseListener
1798 (new EditPrinButtonListener(tool, tw, this, edit));
1799 TaggedList permList = new TaggedList(10, false);
1800 permList.getAccessibleContext().setAccessibleName(
1801 PolicyTool.getMessage("Permission.List"));
1802 permList.addMouseListener
1803 (new EditPermButtonListener(tool, tw, this, edit));
1804
1805 // find where the PolicyTool gui is
1806 Point location = tw.getLocationOnScreen();
1807 //setBounds(location.x + 75, location.y + 200, 650, 500);
1808 setLayout(new GridBagLayout());
1809 setResizable(true);
1810
1811 if (edit) {
1812 // get the selected item
1813 entries = tool.getEntry();
1814 JList policyList = (JList)tw.getComponent(ToolWindow.MW_POLICY_LIST);
1815 listIndex = policyList.getSelectedIndex();
1816
1817 // get principal list
1818 LinkedList<PolicyParser.PrincipalEntry> principals =
1819 entries[listIndex].getGrantEntry().principals;
1820 for (int i = 0; i < principals.size(); i++) {
1821 String prinString = null;
1822 PolicyParser.PrincipalEntry nextPrin = principals.get(i);
1823 prinList.addTaggedItem(PrincipalEntryToUserFriendlyString(nextPrin), nextPrin);
1824 }
1825
1826 // get permission list
1827 Vector<PolicyParser.PermissionEntry> permissions =
1828 entries[listIndex].getGrantEntry().permissionEntries;
1829 for (int i = 0; i < permissions.size(); i++) {
1830 String permString = null;
1831 PolicyParser.PermissionEntry nextPerm =
1832 permissions.elementAt(i);
1833 permList.addTaggedItem(ToolDialog.PermissionEntryToUserFriendlyString(nextPerm), nextPerm);
1834 }
2138
2139 ToolDialog newTD = new ToolDialog
2140 (PolicyTool.getMessage("Principals"), tool, tw, true);
2141 newTD.addWindowListener(new ChildWindowListener(newTD));
2142
2143 // find where the PolicyTool gui is
2144 Point location = getLocationOnScreen();
2145 //newTD.setBounds(location.x + 50, location.y + 100, 650, 190);
2146 newTD.setLayout(new GridBagLayout());
2147 newTD.setResizable(true);
2148
2149 // description label
2150 JLabel label = (edit ?
2151 new JLabel(PolicyTool.getMessage(".Edit.Principal.")) :
2152 new JLabel(PolicyTool.getMessage(".Add.New.Principal.")));
2153 tw.addNewComponent(newTD, label, PRD_DESC_LABEL,
2154 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2155 ToolWindow.TOP_BOTTOM_PADDING);
2156
2157 // principal choice
2158 JComboBox choice = new JComboBox();
2159 choice.addItem(PRIN_TYPE);
2160 choice.getAccessibleContext().setAccessibleName(PRIN_TYPE);
2161 for (int i = 0; i < PRIN_ARRAY.size(); i++) {
2162 Prin next = PRIN_ARRAY.get(i);
2163 choice.addItem(next.CLASS);
2164 }
2165
2166 if (edit) {
2167 if (PolicyParser.PrincipalEntry.WILDCARD_CLASS.equals
2168 (editMe.getPrincipalClass())) {
2169 choice.setSelectedItem(PRIN_TYPE);
2170 } else {
2171 Prin inputPrin = getPrin(editMe.getPrincipalClass(), true);
2172 if (inputPrin != null) {
2173 choice.setSelectedItem(inputPrin.CLASS);
2174 }
2175 }
2176 }
2177 // Add listener after selected item is set
2178 choice.addItemListener(new PrincipalTypeMenuListener(newTD));
2254
2255 ToolDialog newTD = new ToolDialog
2256 (PolicyTool.getMessage("Permissions"), tool, tw, true);
2257 newTD.addWindowListener(new ChildWindowListener(newTD));
2258
2259 // find where the PolicyTool gui is
2260 Point location = getLocationOnScreen();
2261 //newTD.setBounds(location.x + 50, location.y + 100, 700, 250);
2262 newTD.setLayout(new GridBagLayout());
2263 newTD.setResizable(true);
2264
2265 // description label
2266 JLabel label = (edit ?
2267 new JLabel(PolicyTool.getMessage(".Edit.Permission.")) :
2268 new JLabel(PolicyTool.getMessage(".Add.New.Permission.")));
2269 tw.addNewComponent(newTD, label, PD_DESC_LABEL,
2270 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2271 ToolWindow.TOP_BOTTOM_PADDING);
2272
2273 // permission choice (added in alphabetical order)
2274 JComboBox choice = new JComboBox();
2275 choice.addItem(PERM);
2276 choice.getAccessibleContext().setAccessibleName(PERM);
2277 for (int i = 0; i < PERM_ARRAY.size(); i++) {
2278 Perm next = PERM_ARRAY.get(i);
2279 choice.addItem(next.CLASS);
2280 }
2281 tw.addNewComponent(newTD, choice, PD_PERM_CHOICE,
2282 0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2283 ToolWindow.LR_BOTTOM_PADDING);
2284
2285 // permission textfield
2286 JTextField tf;
2287 tf = (edit ? new JTextField(editMe.permission, 30) : new JTextField(30));
2288 tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
2289 tf.getAccessibleContext().setAccessibleName(PERM);
2290 if (edit) {
2291 Perm inputPerm = getPerm(editMe.permission, true);
2292 if (inputPerm != null) {
2293 choice.setSelectedItem(inputPerm.CLASS);
2294 }
2295 }
2296 tw.addNewComponent(newTD, tf, PD_PERM_TEXTFIELD,
2297 1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH,
2298 ToolWindow.LR_BOTTOM_PADDING);
2299 choice.addItemListener(new PermissionMenuListener(newTD));
2300
2301 // name label and textfield
2302 choice = new JComboBox();
2303 choice.addItem(PERM_NAME);
2304 choice.getAccessibleContext().setAccessibleName(PERM_NAME);
2305 tf = (edit ? new JTextField(editMe.name, 40) : new JTextField(40));
2306 tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
2307 tf.getAccessibleContext().setAccessibleName(PERM_NAME);
2308 if (edit) {
2309 setPermissionNames(getPerm(editMe.permission, true), choice, tf);
2310 }
2311 tw.addNewComponent(newTD, choice, PD_NAME_CHOICE,
2312 0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2313 ToolWindow.LR_BOTTOM_PADDING);
2314 tw.addNewComponent(newTD, tf, PD_NAME_TEXTFIELD,
2315 1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH,
2316 ToolWindow.LR_BOTTOM_PADDING);
2317 choice.addItemListener(new PermissionNameMenuListener(newTD));
2318
2319 // actions label and textfield
2320 choice = new JComboBox();
2321 choice.addItem(PERM_ACTIONS);
2322 choice.getAccessibleContext().setAccessibleName(PERM_ACTIONS);
2323 tf = (edit ? new JTextField(editMe.action, 40) : new JTextField(40));
2324 tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
2325 tf.getAccessibleContext().setAccessibleName(PERM_ACTIONS);
2326 if (edit) {
2327 setPermissionActions(getPerm(editMe.permission, true), choice, tf);
2328 }
2329 tw.addNewComponent(newTD, choice, PD_ACTIONS_CHOICE,
2330 0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2331 ToolWindow.LR_BOTTOM_PADDING);
2332 tw.addNewComponent(newTD, tf, PD_ACTIONS_TEXTFIELD,
2333 1, 3, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH,
2334 ToolWindow.LR_BOTTOM_PADDING);
2335 choice.addItemListener(new PermissionActionsMenuListener(newTD));
2336
2337 // signedby label and textfield
2338 label = new JLabel(PolicyTool.getMessage("Signed.By."));
2339 tw.addNewComponent(newTD, label, PD_SIGNEDBY_LABEL,
2340 0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2497 (PolicyTool.getMessage
2498 ("Warning.A.public.key.for.alias.signers.i.does.not.exist.Make.sure.a.KeyStore.is.properly.configured."));
2499 Object[] source = {signers[i]};
2500 tool.warnings.addElement(form.format(source));
2501 tw.displayStatusDialog(this, form.format(source));
2502 }
2503 } catch (Exception e) {
2504 tw.displayErrorDialog(this, e);
2505 }
2506 }
2507 }
2508 return pppe;
2509 }
2510
2511 /**
2512 * confirm that the user REALLY wants to remove the Policy Entry
2513 */
2514 void displayConfirmRemovePolicyEntry() {
2515
2516 // find the entry to be removed
2517 JList list = (JList)tw.getComponent(ToolWindow.MW_POLICY_LIST);
2518 int index = list.getSelectedIndex();
2519 PolicyEntry entries[] = tool.getEntry();
2520
2521 // find where the PolicyTool gui is
2522 Point location = tw.getLocationOnScreen();
2523 //setBounds(location.x + 25, location.y + 100, 600, 400);
2524 setLayout(new GridBagLayout());
2525
2526 // ask the user do they really want to do this?
2527 JLabel label = new JLabel
2528 (PolicyTool.getMessage("Remove.this.Policy.Entry."));
2529 tw.addNewComponent(this, label, CRPE_LABEL1,
2530 0, 0, 2, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2531 ToolWindow.BOTTOM_PADDING);
2532
2533 // display the policy entry
2534 label = new JLabel(entries[index].codebaseToString());
2535 tw.addNewComponent(this, label, CRPE_LABEL2,
2536 0, 1, 2, 1, 0.0, 0.0, GridBagConstraints.BOTH);
2537 label = new JLabel(entries[index].principalsToString().trim());
2717 ToolDialog us, int select) {
2718
2719 // now either QUIT, open a NEW policy file, or OPEN an existing policy
2720 switch(select) {
2721 case ToolDialog.QUIT:
2722
2723 tw.setVisible(false);
2724 tw.dispose();
2725 System.exit(0);
2726
2727 case ToolDialog.NEW:
2728
2729 try {
2730 tool.openPolicy(null);
2731 } catch (Exception ee) {
2732 tool.modified = false;
2733 tw.displayErrorDialog(null, ee);
2734 }
2735
2736 // display the policy entries via the policy list textarea
2737 JList list = new JList(new DefaultListModel());
2738 list.setVisibleRowCount(15);
2739 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
2740 list.addMouseListener(new PolicyListListener(tool, tw));
2741 tw.replacePolicyList(list);
2742
2743 // display null policy filename and keystore
2744 JTextField newFilename = (JTextField)tw.getComponent(
2745 ToolWindow.MW_FILENAME_TEXTFIELD);
2746 newFilename.setText("");
2747 tw.setVisible(true);
2748 break;
2749
2750 case ToolDialog.OPEN:
2751
2752 // pop up a dialog box for the user to enter a filename.
2753 FileDialog fd = new FileDialog
2754 (tw, PolicyTool.getMessage("Open"), FileDialog.LOAD);
2755 fd.addWindowListener(new WindowAdapter() {
2756 public void windowClosing(WindowEvent e) {
2757 e.getWindow().setVisible(false);
2758 }
2759 });
2760 fd.setVisible(true);
2761
2762 // see if the user hit 'cancel'
2763 if (fd.getFile() == null ||
2764 fd.getFile().equals(""))
2765 return;
2766
2767 // get the entered filename
2768 String policyFile = new File(fd.getDirectory(), fd.getFile()).getPath();
2769
2770 try {
2771 // open the policy file
2772 tool.openPolicy(policyFile);
2773
2774 // display the policy entries via the policy list textarea
2775 DefaultListModel listModel = new DefaultListModel();
2776 list = new JList(listModel);
2777 list.setVisibleRowCount(15);
2778 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
2779 list.addMouseListener(new PolicyListListener(tool, tw));
2780 PolicyEntry entries[] = tool.getEntry();
2781 if (entries != null) {
2782 for (int i = 0; i < entries.length; i++) {
2783 listModel.addElement(entries[i].headerToString());
2784 }
2785 }
2786 tw.replacePolicyList(list);
2787 tool.modified = false;
2788
2789 // display the new policy filename
2790 newFilename = (JTextField)tw.getComponent(
2791 ToolWindow.MW_FILENAME_TEXTFIELD);
2792 newFilename.setText(policyFile);
2793 tw.setVisible(true);
2794
2795 // inform user of warnings
2796 if (tool.newWarning == true) {
2797 tw.displayStatusDialog(null, PolicyTool.getMessage
2798 ("Errors.have.occurred.while.opening.the.policy.configuration.View.the.Warning.Log.for.more.information."));
2799 }
2800
2801 } catch (Exception e) {
2802 // add blank policy listing
2803 list = new JList(new DefaultListModel());
2804 list.setVisibleRowCount(15);
2805 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
2806 list.addMouseListener(new PolicyListListener(tool, tw));
2807 tw.replacePolicyList(list);
2808 tool.setPolicyFileName(null);
2809 tool.modified = false;
2810
2811 // display a null policy filename
2812 newFilename = (JTextField)tw.getComponent(
2813 ToolWindow.MW_FILENAME_TEXTFIELD);
2814 newFilename.setText("");
2815 tw.setVisible(true);
2816
2817 // display the error
2818 MessageFormat form = new MessageFormat(PolicyTool.getMessage
2819 ("Could.not.open.policy.file.policyFile.e.toString."));
2820 Object[] source = {policyFile, e.toString()};
2821 tw.displayErrorDialog(null, form.format(source));
2822 }
2823 break;
2824 }
2825 }
2826
2827 /**
2828 * Return a Menu list of names for a given permission
2829 *
2830 * If inputPerm's TARGETS are null, then this means TARGETS are
2831 * not allowed to be entered (and the TextField is set to be
2832 * non-editable).
2833 *
2834 * If TARGETS are valid but there are no standard ones
2835 * (user must enter them by hand) then the TARGETS array may be empty
2836 * (and of course non-null).
2837 */
2838 void setPermissionNames(Perm inputPerm, JComboBox names, JTextField field) {
2839 names.removeAllItems();
2840 names.addItem(PERM_NAME);
2841
2842 if (inputPerm == null) {
2843 // custom permission
2844 field.setEditable(true);
2845 } else if (inputPerm.TARGETS == null) {
2846 // standard permission with no targets
2847 field.setEditable(false);
2848 } else {
2849 // standard permission with standard targets
2850 field.setEditable(true);
2851 for (int i = 0; i < inputPerm.TARGETS.length; i++) {
2852 names.addItem(inputPerm.TARGETS[i]);
2853 }
2854 }
2855 }
2856
2857 /**
2858 * Return a Menu list of actions for a given permission
2859 *
2860 * If inputPerm's ACTIONS are null, then this means ACTIONS are
2861 * not allowed to be entered (and the TextField is set to be
2862 * non-editable). This is typically true for BasicPermissions.
2863 *
2864 * If ACTIONS are valid but there are no standard ones
2865 * (user must enter them by hand) then the ACTIONS array may be empty
2866 * (and of course non-null).
2867 */
2868 void setPermissionActions(Perm inputPerm, JComboBox actions, JTextField field) {
2869 actions.removeAllItems();
2870 actions.addItem(PERM_ACTIONS);
2871
2872 if (inputPerm == null) {
2873 // custom permission
2874 field.setEditable(true);
2875 } else if (inputPerm.ACTIONS == null) {
2876 // standard permission with no actions
2877 field.setEditable(false);
2878 } else {
2879 // standard permission with standard actions
2880 field.setEditable(true);
2881 for (int i = 0; i < inputPerm.ACTIONS.length; i++) {
2882 actions.addItem(inputPerm.ACTIONS[i]);
2883 }
2884 }
2885 }
2886
2887 static String PermissionEntryToUserFriendlyString(PolicyParser.PermissionEntry pppe) {
2888 String result = pppe.permission;
3087
3088 MainWindowListener(PolicyTool tool, ToolWindow tw) {
3089 this.tool = tool;
3090 this.tw = tw;
3091 }
3092
3093 public void actionPerformed(ActionEvent e) {
3094
3095 if (PolicyTool.collator.compare(e.getActionCommand(),
3096 ToolWindow.ADD_POLICY_ENTRY) == 0) {
3097
3098 // display a dialog box for the user to enter policy info
3099 ToolDialog td = new ToolDialog
3100 (PolicyTool.getMessage("Policy.Entry"), tool, tw, true);
3101 td.displayPolicyEntryDialog(false);
3102
3103 } else if (PolicyTool.collator.compare(e.getActionCommand(),
3104 ToolWindow.REMOVE_POLICY_ENTRY) == 0) {
3105
3106 // get the selected entry
3107 JList list = (JList)tw.getComponent(ToolWindow.MW_POLICY_LIST);
3108 int index = list.getSelectedIndex();
3109 if (index < 0) {
3110 tw.displayErrorDialog(null, new Exception
3111 (PolicyTool.getMessage("No.Policy.Entry.selected")));
3112 return;
3113 }
3114
3115 // ask the user if they really want to remove the policy entry
3116 ToolDialog td = new ToolDialog(PolicyTool.getMessage
3117 ("Remove.Policy.Entry"), tool, tw, true);
3118 td.displayConfirmRemovePolicyEntry();
3119
3120 } else if (PolicyTool.collator.compare(e.getActionCommand(),
3121 ToolWindow.EDIT_POLICY_ENTRY) == 0) {
3122
3123 // get the selected entry
3124 JList list = (JList)tw.getComponent(ToolWindow.MW_POLICY_LIST);
3125 int index = list.getSelectedIndex();
3126 if (index < 0) {
3127 tw.displayErrorDialog(null, new Exception
3128 (PolicyTool.getMessage("No.Policy.Entry.selected")));
3129 return;
3130 }
3131
3132 // display the permission list for a policy entry
3133 ToolDialog td = new ToolDialog
3134 (PolicyTool.getMessage("Policy.Entry"), tool, tw, true);
3135 td.displayPolicyEntryDialog(true);
3136
3137 } else if (PolicyTool.collator.compare(e.getActionCommand(),
3138 ToolWindow.EDIT_KEYSTORE) == 0) {
3139
3140 // display a dialog box for the user to enter keystore info
3141 ToolDialog td = new ToolDialog
3142 (PolicyTool.getMessage("KeyStore"), tool, tw, true);
3143 td.keyStoreDialog(ToolDialog.EDIT_KEYSTORE);
3144 }
3175 PolicyEntry newEntry = td.getPolicyEntryFromDialog();
3176 PolicyParser.GrantEntry newGe = newEntry.getGrantEntry();
3177
3178 // see if all the signers have public keys
3179 if (newGe.signedBy != null) {
3180 String signers[] = tool.parseSigners(newGe.signedBy);
3181 for (int i = 0; i < signers.length; i++) {
3182 PublicKey pubKey = tool.getPublicKeyAlias(signers[i]);
3183 if (pubKey == null) {
3184 MessageFormat form = new MessageFormat
3185 (PolicyTool.getMessage
3186 ("Warning.A.public.key.for.alias.signers.i.does.not.exist.Make.sure.a.KeyStore.is.properly.configured."));
3187 Object[] source = {signers[i]};
3188 tool.warnings.addElement(form.format(source));
3189 tw.displayStatusDialog(td, form.format(source));
3190 }
3191 }
3192 }
3193
3194 // add the entry
3195 JList policyList = (JList)tw.getComponent(ToolWindow.MW_POLICY_LIST);
3196 if (edit) {
3197 int listIndex = policyList.getSelectedIndex();
3198 tool.addEntry(newEntry, listIndex);
3199 String newCodeBaseStr = newEntry.headerToString();
3200 if (PolicyTool.collator.compare
3201 (newCodeBaseStr, policyList.getModel().getElementAt(listIndex)) != 0)
3202 tool.modified = true;
3203 ((DefaultListModel)policyList.getModel()).set(listIndex, newCodeBaseStr);
3204 } else {
3205 tool.addEntry(newEntry, -1);
3206 ((DefaultListModel)policyList.getModel()).addElement(newEntry.headerToString());
3207 tool.modified = true;
3208 }
3209 td.setVisible(false);
3210 td.dispose();
3211
3212 } catch (Exception eee) {
3213 tw.displayErrorDialog(td, eee);
3214 }
3215 }
3216 }
3217
3218 /**
3219 * Event handler for ChangeKeyStoreOKButton button
3220 */
3221 class ChangeKeyStoreOKButtonListener implements ActionListener {
3222
3223 private PolicyTool tool;
3224 private ToolWindow tw;
3225 private ToolDialog td;
3226
3564 * update both the GUI listing and the actual PolicyEntry.
3565 */
3566 class EditPermButtonListener extends MouseAdapter implements ActionListener {
3567
3568 private PolicyTool tool;
3569 private ToolWindow tw;
3570 private ToolDialog td;
3571 private boolean editPolicyEntry;
3572
3573 EditPermButtonListener(PolicyTool tool, ToolWindow tw,
3574 ToolDialog td, boolean editPolicyEntry) {
3575 this.tool = tool;
3576 this.tw = tw;
3577 this.td = td;
3578 this.editPolicyEntry = editPolicyEntry;
3579 }
3580
3581 public void actionPerformed(ActionEvent e) {
3582
3583 // get the Permission selected from the Permission List
3584 JList list = (JList)td.getComponent(ToolDialog.PE_PERM_LIST);
3585 int permIndex = list.getSelectedIndex();
3586
3587 if (permIndex < 0) {
3588 tw.displayErrorDialog(td, new Exception
3589 (PolicyTool.getMessage("No.permission.selected")));
3590 return;
3591 }
3592 td.displayPermissionDialog(editPolicyEntry, true);
3593 }
3594
3595 public void mouseClicked(MouseEvent evt) {
3596 if (evt.getClickCount() == 2) {
3597 actionPerformed(null);
3598 }
3599 }
3600 }
3601
3602 /**
3603 * Event handler for Principal Popup Menu
3604 */
3605 class PrincipalTypeMenuListener implements ItemListener {
3606
3607 private ToolDialog td;
3608
3609 PrincipalTypeMenuListener(ToolDialog td) {
3610 this.td = td;
3611 }
3612
3613 public void itemStateChanged(ItemEvent e) {
3614 if (e.getStateChange() == ItemEvent.DESELECTED) {
3615 // We're only interested in SELECTED events
3616 return;
3617 }
3618
3619 JComboBox prin = (JComboBox)td.getComponent(ToolDialog.PRD_PRIN_CHOICE);
3620 JTextField prinField = (JTextField)td.getComponent(
3621 ToolDialog.PRD_PRIN_TEXTFIELD);
3622 JTextField nameField = (JTextField)td.getComponent(
3623 ToolDialog.PRD_NAME_TEXTFIELD);
3624
3625 prin.getAccessibleContext().setAccessibleName(
3626 PolicyTool.splitToWords((String)e.getItem()));
3627 if (((String)e.getItem()).equals(ToolDialog.PRIN_TYPE)) {
3628 // ignore if they choose "Principal Type:" item
3629 if (prinField.getText() != null &&
3630 prinField.getText().length() > 0) {
3631 Prin inputPrin = ToolDialog.getPrin(prinField.getText(), true);
3632 prin.setSelectedItem(inputPrin.CLASS);
3633 }
3634 return;
3635 }
3636
3637 // if you change the principal, clear the name
3638 if (prinField.getText().indexOf((String)e.getItem()) == -1) {
3639 nameField.setText("");
3649 }
3650 }
3651
3652 /**
3653 * Event handler for Permission Popup Menu
3654 */
3655 class PermissionMenuListener implements ItemListener {
3656
3657 private ToolDialog td;
3658
3659 PermissionMenuListener(ToolDialog td) {
3660 this.td = td;
3661 }
3662
3663 public void itemStateChanged(ItemEvent e) {
3664 if (e.getStateChange() == ItemEvent.DESELECTED) {
3665 // We're only interested in SELECTED events
3666 return;
3667 }
3668
3669 JComboBox perms = (JComboBox)td.getComponent(
3670 ToolDialog.PD_PERM_CHOICE);
3671 JComboBox names = (JComboBox)td.getComponent(
3672 ToolDialog.PD_NAME_CHOICE);
3673 JComboBox actions = (JComboBox)td.getComponent(
3674 ToolDialog.PD_ACTIONS_CHOICE);
3675 JTextField nameField = (JTextField)td.getComponent(
3676 ToolDialog.PD_NAME_TEXTFIELD);
3677 JTextField actionsField = (JTextField)td.getComponent(
3678 ToolDialog.PD_ACTIONS_TEXTFIELD);
3679 JTextField permField = (JTextField)td.getComponent(
3680 ToolDialog.PD_PERM_TEXTFIELD);
3681 JTextField signedbyField = (JTextField)td.getComponent(
3682 ToolDialog.PD_SIGNEDBY_TEXTFIELD);
3683
3684 perms.getAccessibleContext().setAccessibleName(
3685 PolicyTool.splitToWords((String)e.getItem()));
3686
3687 // ignore if they choose the 'Permission:' item
3688 if (PolicyTool.collator.compare((String)e.getItem(),
3689 ToolDialog.PERM) == 0) {
3690 if (permField.getText() != null &&
3691 permField.getText().length() > 0) {
3692
3693 Perm inputPerm = ToolDialog.getPerm(permField.getText(), true);
3720 }
3721 }
3722
3723 /**
3724 * Event handler for Permission Name Popup Menu
3725 */
3726 class PermissionNameMenuListener implements ItemListener {
3727
3728 private ToolDialog td;
3729
3730 PermissionNameMenuListener(ToolDialog td) {
3731 this.td = td;
3732 }
3733
3734 public void itemStateChanged(ItemEvent e) {
3735 if (e.getStateChange() == ItemEvent.DESELECTED) {
3736 // We're only interested in SELECTED events
3737 return;
3738 }
3739
3740 JComboBox names = (JComboBox)td.getComponent(ToolDialog.PD_NAME_CHOICE);
3741 names.getAccessibleContext().setAccessibleName(
3742 PolicyTool.splitToWords((String)e.getItem()));
3743
3744 if (((String)e.getItem()).indexOf(ToolDialog.PERM_NAME) != -1)
3745 return;
3746
3747 JTextField tf = (JTextField)td.getComponent(ToolDialog.PD_NAME_TEXTFIELD);
3748 tf.setText((String)e.getItem());
3749 }
3750 }
3751
3752 /**
3753 * Event handler for Permission Actions Popup Menu
3754 */
3755 class PermissionActionsMenuListener implements ItemListener {
3756
3757 private ToolDialog td;
3758
3759 PermissionActionsMenuListener(ToolDialog td) {
3760 this.td = td;
3761 }
3762
3763 public void itemStateChanged(ItemEvent e) {
3764 if (e.getStateChange() == ItemEvent.DESELECTED) {
3765 // We're only interested in SELECTED events
3766 return;
3767 }
3768
3769 JComboBox actions = (JComboBox)td.getComponent(
3770 ToolDialog.PD_ACTIONS_CHOICE);
3771 actions.getAccessibleContext().setAccessibleName((String)e.getItem());
3772
3773 if (((String)e.getItem()).indexOf(ToolDialog.PERM_ACTIONS) != -1)
3774 return;
3775
3776 JTextField tf = (JTextField)td.getComponent(
3777 ToolDialog.PD_ACTIONS_TEXTFIELD);
3778 if (tf.getText() == null || tf.getText().equals("")) {
3779 tf.setText((String)e.getItem());
3780 } else {
3781 if (tf.getText().indexOf((String)e.getItem()) == -1)
3782 tf.setText(tf.getText() + ", " + (String)e.getItem());
3783 }
3784 }
3785 }
3786
3787 /**
3788 * Event handler for all the children dialogs/windows
3789 */
3974 }
3975
3976 /**
3977 * Event handler for ConfirmRemovePolicyEntryOKButtonListener
3978 */
3979 class ConfirmRemovePolicyEntryOKButtonListener implements ActionListener {
3980
3981 private PolicyTool tool;
3982 private ToolWindow tw;
3983 private ToolDialog us;
3984
3985 ConfirmRemovePolicyEntryOKButtonListener(PolicyTool tool,
3986 ToolWindow tw, ToolDialog us) {
3987 this.tool = tool;
3988 this.tw = tw;
3989 this.us = us;
3990 }
3991
3992 public void actionPerformed(ActionEvent e) {
3993 // remove the entry
3994 JList list = (JList)tw.getComponent(ToolWindow.MW_POLICY_LIST);
3995 int index = list.getSelectedIndex();
3996 PolicyEntry entries[] = tool.getEntry();
3997 tool.removeEntry(entries[index]);
3998
3999 // redraw the window listing
4000 DefaultListModel listModel = new DefaultListModel();
4001 list = new JList(listModel);
4002 list.setVisibleRowCount(15);
4003 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
4004 list.addMouseListener(new PolicyListListener(tool, tw));
4005 entries = tool.getEntry();
4006 if (entries != null) {
4007 for (int i = 0; i < entries.length; i++) {
4008 listModel.addElement(entries[i].headerToString());
4009 }
4010 }
4011 tw.replacePolicyList(list);
4012 us.setVisible(false);
4013 us.dispose();
4014 }
4015 }
4016
4017 /**
4018 * Just a special name, so that the codes dealing with this exception knows
4019 * it's special, and does not pop out a warning box.
4020 */
4021 class NoDisplayException extends RuntimeException {
4022 private static final long serialVersionUID = -4611761427108719794L;
4023 }
4024
4025 /**
4026 * This is a java.awt.List that bind an Object to each String it holds.
4027 */
4028 class TaggedList extends JList {
4029 private static final long serialVersionUID = -5676238110427785853L;
4030
4031 private java.util.List<Object> data = new LinkedList<>();
4032 public TaggedList(int i, boolean b) {
4033 super(new DefaultListModel());
4034 setVisibleRowCount(i);
4035 setSelectionMode(b ? ListSelectionModel.MULTIPLE_INTERVAL_SELECTION : ListSelectionModel.SINGLE_SELECTION);
4036 }
4037
4038 public Object getObject(int index) {
4039 return data.get(index);
4040 }
4041
4042 public void addTaggedItem(String string, Object object) {
4043 ((DefaultListModel)getModel()).addElement(string);
4044 data.add(object);
4045 }
4046
4047 public void replaceTaggedItem(String string, Object object, int index) {
4048 ((DefaultListModel)getModel()).set(index, string);
4049 data.set(index, object);
4050 }
4051
4052 public void removeTaggedItem(int index) {
4053 ((DefaultListModel)getModel()).remove(index);
4054 data.remove(index);
4055 }
4056 }
4057
4058 /**
4059 * Convenience Principal Classes
4060 */
4061
4062 class Prin {
4063 public final String CLASS;
4064 public final String FULL_CLASS;
4065
4066 public Prin(String clazz, String fullClass) {
4067 this.CLASS = clazz;
4068 this.FULL_CLASS = fullClass;
4069 }
4070 }
4071
4072 class KrbPrin extends Prin {
4073 public KrbPrin() {
|
1 /*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
1039 public static final String SAVE_AS_POLICY_FILE = "Save.As";
1040 public static final String VIEW_WARNINGS = "View.Warning.Log";
1041 public static final String QUIT = "Exit";
1042 public static final String ADD_POLICY_ENTRY = "Add.Policy.Entry";
1043 public static final String EDIT_POLICY_ENTRY = "Edit.Policy.Entry";
1044 public static final String REMOVE_POLICY_ENTRY = "Remove.Policy.Entry";
1045 public static final String EDIT_KEYSTORE = "Edit";
1046 public static final String ADD_PUBKEY_ALIAS = "Add.Public.Key.Alias";
1047 public static final String REMOVE_PUBKEY_ALIAS = "Remove.Public.Key.Alias";
1048
1049 /* gridbag index for components in the main window (MW) */
1050 public static final int MW_FILENAME_LABEL = 0;
1051 public static final int MW_FILENAME_TEXTFIELD = 1;
1052 public static final int MW_PANEL = 2;
1053 public static final int MW_ADD_BUTTON = 0;
1054 public static final int MW_EDIT_BUTTON = 1;
1055 public static final int MW_REMOVE_BUTTON = 2;
1056 public static final int MW_POLICY_LIST = 3; // follows MW_PANEL
1057
1058 /* The preferred height of JTextField should match JComboBox. */
1059 static final int TEXTFIELD_HEIGHT = new JComboBox<>().getPreferredSize().height;
1060
1061 private PolicyTool tool;
1062
1063 /**
1064 * Constructor
1065 */
1066 ToolWindow(PolicyTool tool) {
1067 this.tool = tool;
1068 }
1069
1070 /**
1071 * Don't call getComponent directly on the window
1072 */
1073 public Component getComponent(int n) {
1074 Component c = getContentPane().getComponent(n);
1075 if (c instanceof JScrollPane) {
1076 c = ((JScrollPane)c).getViewport().getView();
1077 }
1078 return c;
1079 }
1152 LR_PADDING);
1153
1154 addNewComponent(this, panel, MW_PANEL,
1155 0, 2, 2, 1, 0.0, 0.0, GridBagConstraints.BOTH,
1156 BOTTOM_PADDING);
1157
1158
1159 String policyFile = tool.getPolicyFileName();
1160 if (policyFile == null) {
1161 String userHome;
1162 userHome = java.security.AccessController.doPrivileged(
1163 new sun.security.action.GetPropertyAction("user.home"));
1164 policyFile = userHome + File.separatorChar + ".java.policy";
1165 }
1166
1167 try {
1168 // open the policy file
1169 tool.openPolicy(policyFile);
1170
1171 // display the policy entries via the policy list textarea
1172 DefaultListModel<String> listModel = new DefaultListModel<>();
1173 JList<String> list = new JList<>(listModel);
1174 list.setVisibleRowCount(15);
1175 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
1176 list.addMouseListener(new PolicyListListener(tool, this));
1177 PolicyEntry entries[] = tool.getEntry();
1178 if (entries != null) {
1179 for (int i = 0; i < entries.length; i++) {
1180 listModel.addElement(entries[i].headerToString());
1181 }
1182 }
1183 JTextField newFilename = (JTextField)
1184 getComponent(MW_FILENAME_TEXTFIELD);
1185 newFilename.setText(policyFile);
1186 initPolicyList(list);
1187
1188 } catch (FileNotFoundException fnfe) {
1189 // add blank policy listing
1190 JList<String> list = new JList<>(new DefaultListModel<>());
1191 list.setVisibleRowCount(15);
1192 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
1193 list.addMouseListener(new PolicyListListener(tool, this));
1194 initPolicyList(list);
1195 tool.setPolicyFileName(null);
1196 tool.modified = false;
1197
1198 // just add warning
1199 tool.warnings.addElement(fnfe.toString());
1200
1201 } catch (Exception e) {
1202 // add blank policy listing
1203 JList<String> list = new JList<>(new DefaultListModel<>());
1204 list.setVisibleRowCount(15);
1205 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
1206 list.addMouseListener(new PolicyListListener(tool, this));
1207 initPolicyList(list);
1208 tool.setPolicyFileName(null);
1209 tool.modified = false;
1210
1211 // display the error
1212 MessageFormat form = new MessageFormat(PolicyTool.getMessage
1213 ("Could.not.open.policy.file.policyFile.e.toString."));
1214 Object[] source = {policyFile, e.toString()};
1215 displayErrorDialog(null, form.format(source));
1216 }
1217 }
1218
1219
1220 // Platform specific modifier (control / command).
1221 private int shortCutModifier = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
1222
1223 private void addMenuItem(JMenu menu, String key, ActionListener actionListener, String accelerator) {
1299
1300
1301 /**
1302 * Add a component to the PolicyTool window without external padding
1303 */
1304 void addNewComponent(Container container, JComponent component,
1305 int index, int gridx, int gridy, int gridwidth, int gridheight,
1306 double weightx, double weighty, int fill) {
1307
1308 // delegate with "null" external padding
1309 addNewComponent(container, component, index, gridx, gridy,
1310 gridwidth, gridheight, weightx, weighty,
1311 fill, null);
1312 }
1313
1314
1315 /**
1316 * Init the policy_entry_list TEXTAREA component in the
1317 * PolicyTool window
1318 */
1319 void initPolicyList(JList<String> policyList) {
1320
1321 // add the policy list to the window
1322 //policyList.setPreferredSize(new Dimension(500, 350));
1323 JScrollPane scrollPane = new JScrollPane(policyList);
1324 addNewComponent(this, scrollPane, MW_POLICY_LIST,
1325 0, 3, 2, 1, 1.0, 1.0, GridBagConstraints.BOTH);
1326 }
1327
1328 /**
1329 * Replace the policy_entry_list TEXTAREA component in the
1330 * PolicyTool window with an updated one.
1331 */
1332 void replacePolicyList(JList<String> policyList) {
1333
1334 // remove the original list of Policy Entries
1335 // and add the new list of entries
1336 @SuppressWarnings("unchecked")
1337 JList<String> list = (JList<String>)getComponent(MW_POLICY_LIST);
1338 list.setModel(policyList.getModel());
1339 }
1340
1341 /**
1342 * display the main PolicyTool window
1343 */
1344 void displayToolWindow(String args[]) {
1345
1346 setTitle(PolicyTool.getMessage("Policy.Tool"));
1347 setResizable(true);
1348 addWindowListener(new ToolWindowListener(tool, this));
1349 //setBounds(135, 80, 500, 500);
1350 getContentPane().setLayout(new GridBagLayout());
1351
1352 initWindow();
1353 pack();
1354 setLocationRelativeTo(null);
1355
1356 // display it
1357 setVisible(true);
1652 public static final int USC_YES_BUTTON = 0;
1653 public static final int USC_NO_BUTTON = 1;
1654 public static final int USC_CANCEL_BUTTON = 2;
1655
1656 /* gridbag index for the ConfirmRemovePolicyEntryDialog (CRPE) */
1657 public static final int CRPE_LABEL1 = 0;
1658 public static final int CRPE_LABEL2 = 1;
1659 public static final int CRPE_PANEL = 2;
1660 public static final int CRPE_PANEL_OK = 0;
1661 public static final int CRPE_PANEL_CANCEL = 1;
1662
1663 /* some private static finals */
1664 private static final int PERMISSION = 0;
1665 private static final int PERMISSION_NAME = 1;
1666 private static final int PERMISSION_ACTIONS = 2;
1667 private static final int PERMISSION_SIGNEDBY = 3;
1668 private static final int PRINCIPAL_TYPE = 4;
1669 private static final int PRINCIPAL_NAME = 5;
1670
1671 /* The preferred height of JTextField should match JComboBox. */
1672 static final int TEXTFIELD_HEIGHT = new JComboBox<>().getPreferredSize().height;
1673
1674 public static java.util.ArrayList<Perm> PERM_ARRAY;
1675 public static java.util.ArrayList<Prin> PRIN_ARRAY;
1676 PolicyTool tool;
1677 ToolWindow tw;
1678
1679 static {
1680
1681 // set up permission objects
1682
1683 PERM_ARRAY = new java.util.ArrayList<Perm>();
1684 PERM_ARRAY.add(new AllPerm());
1685 PERM_ARRAY.add(new AudioPerm());
1686 PERM_ARRAY.add(new AuthPerm());
1687 PERM_ARRAY.add(new AWTPerm());
1688 PERM_ARRAY.add(new DelegationPerm());
1689 PERM_ARRAY.add(new FilePerm());
1690 PERM_ARRAY.add(new URLPerm());
1691 PERM_ARRAY.add(new InqSecContextPerm());
1692 PERM_ARRAY.add(new LogPerm());
1795 TaggedList prinList = new TaggedList(3, false);
1796 prinList.getAccessibleContext().setAccessibleName(
1797 PolicyTool.getMessage("Principal.List"));
1798 prinList.addMouseListener
1799 (new EditPrinButtonListener(tool, tw, this, edit));
1800 TaggedList permList = new TaggedList(10, false);
1801 permList.getAccessibleContext().setAccessibleName(
1802 PolicyTool.getMessage("Permission.List"));
1803 permList.addMouseListener
1804 (new EditPermButtonListener(tool, tw, this, edit));
1805
1806 // find where the PolicyTool gui is
1807 Point location = tw.getLocationOnScreen();
1808 //setBounds(location.x + 75, location.y + 200, 650, 500);
1809 setLayout(new GridBagLayout());
1810 setResizable(true);
1811
1812 if (edit) {
1813 // get the selected item
1814 entries = tool.getEntry();
1815 @SuppressWarnings("unchecked")
1816 JList<String> policyList = (JList<String>)tw.getComponent(ToolWindow.MW_POLICY_LIST);
1817 listIndex = policyList.getSelectedIndex();
1818
1819 // get principal list
1820 LinkedList<PolicyParser.PrincipalEntry> principals =
1821 entries[listIndex].getGrantEntry().principals;
1822 for (int i = 0; i < principals.size(); i++) {
1823 String prinString = null;
1824 PolicyParser.PrincipalEntry nextPrin = principals.get(i);
1825 prinList.addTaggedItem(PrincipalEntryToUserFriendlyString(nextPrin), nextPrin);
1826 }
1827
1828 // get permission list
1829 Vector<PolicyParser.PermissionEntry> permissions =
1830 entries[listIndex].getGrantEntry().permissionEntries;
1831 for (int i = 0; i < permissions.size(); i++) {
1832 String permString = null;
1833 PolicyParser.PermissionEntry nextPerm =
1834 permissions.elementAt(i);
1835 permList.addTaggedItem(ToolDialog.PermissionEntryToUserFriendlyString(nextPerm), nextPerm);
1836 }
2140
2141 ToolDialog newTD = new ToolDialog
2142 (PolicyTool.getMessage("Principals"), tool, tw, true);
2143 newTD.addWindowListener(new ChildWindowListener(newTD));
2144
2145 // find where the PolicyTool gui is
2146 Point location = getLocationOnScreen();
2147 //newTD.setBounds(location.x + 50, location.y + 100, 650, 190);
2148 newTD.setLayout(new GridBagLayout());
2149 newTD.setResizable(true);
2150
2151 // description label
2152 JLabel label = (edit ?
2153 new JLabel(PolicyTool.getMessage(".Edit.Principal.")) :
2154 new JLabel(PolicyTool.getMessage(".Add.New.Principal.")));
2155 tw.addNewComponent(newTD, label, PRD_DESC_LABEL,
2156 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2157 ToolWindow.TOP_BOTTOM_PADDING);
2158
2159 // principal choice
2160 JComboBox<String> choice = new JComboBox<>();
2161 choice.addItem(PRIN_TYPE);
2162 choice.getAccessibleContext().setAccessibleName(PRIN_TYPE);
2163 for (int i = 0; i < PRIN_ARRAY.size(); i++) {
2164 Prin next = PRIN_ARRAY.get(i);
2165 choice.addItem(next.CLASS);
2166 }
2167
2168 if (edit) {
2169 if (PolicyParser.PrincipalEntry.WILDCARD_CLASS.equals
2170 (editMe.getPrincipalClass())) {
2171 choice.setSelectedItem(PRIN_TYPE);
2172 } else {
2173 Prin inputPrin = getPrin(editMe.getPrincipalClass(), true);
2174 if (inputPrin != null) {
2175 choice.setSelectedItem(inputPrin.CLASS);
2176 }
2177 }
2178 }
2179 // Add listener after selected item is set
2180 choice.addItemListener(new PrincipalTypeMenuListener(newTD));
2256
2257 ToolDialog newTD = new ToolDialog
2258 (PolicyTool.getMessage("Permissions"), tool, tw, true);
2259 newTD.addWindowListener(new ChildWindowListener(newTD));
2260
2261 // find where the PolicyTool gui is
2262 Point location = getLocationOnScreen();
2263 //newTD.setBounds(location.x + 50, location.y + 100, 700, 250);
2264 newTD.setLayout(new GridBagLayout());
2265 newTD.setResizable(true);
2266
2267 // description label
2268 JLabel label = (edit ?
2269 new JLabel(PolicyTool.getMessage(".Edit.Permission.")) :
2270 new JLabel(PolicyTool.getMessage(".Add.New.Permission.")));
2271 tw.addNewComponent(newTD, label, PD_DESC_LABEL,
2272 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2273 ToolWindow.TOP_BOTTOM_PADDING);
2274
2275 // permission choice (added in alphabetical order)
2276 JComboBox<String> choice = new JComboBox<>();
2277 choice.addItem(PERM);
2278 choice.getAccessibleContext().setAccessibleName(PERM);
2279 for (int i = 0; i < PERM_ARRAY.size(); i++) {
2280 Perm next = PERM_ARRAY.get(i);
2281 choice.addItem(next.CLASS);
2282 }
2283 tw.addNewComponent(newTD, choice, PD_PERM_CHOICE,
2284 0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2285 ToolWindow.LR_BOTTOM_PADDING);
2286
2287 // permission textfield
2288 JTextField tf;
2289 tf = (edit ? new JTextField(editMe.permission, 30) : new JTextField(30));
2290 tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
2291 tf.getAccessibleContext().setAccessibleName(PERM);
2292 if (edit) {
2293 Perm inputPerm = getPerm(editMe.permission, true);
2294 if (inputPerm != null) {
2295 choice.setSelectedItem(inputPerm.CLASS);
2296 }
2297 }
2298 tw.addNewComponent(newTD, tf, PD_PERM_TEXTFIELD,
2299 1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH,
2300 ToolWindow.LR_BOTTOM_PADDING);
2301 choice.addItemListener(new PermissionMenuListener(newTD));
2302
2303 // name label and textfield
2304 choice = new JComboBox<>();
2305 choice.addItem(PERM_NAME);
2306 choice.getAccessibleContext().setAccessibleName(PERM_NAME);
2307 tf = (edit ? new JTextField(editMe.name, 40) : new JTextField(40));
2308 tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
2309 tf.getAccessibleContext().setAccessibleName(PERM_NAME);
2310 if (edit) {
2311 setPermissionNames(getPerm(editMe.permission, true), choice, tf);
2312 }
2313 tw.addNewComponent(newTD, choice, PD_NAME_CHOICE,
2314 0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2315 ToolWindow.LR_BOTTOM_PADDING);
2316 tw.addNewComponent(newTD, tf, PD_NAME_TEXTFIELD,
2317 1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH,
2318 ToolWindow.LR_BOTTOM_PADDING);
2319 choice.addItemListener(new PermissionNameMenuListener(newTD));
2320
2321 // actions label and textfield
2322 choice = new JComboBox<>();
2323 choice.addItem(PERM_ACTIONS);
2324 choice.getAccessibleContext().setAccessibleName(PERM_ACTIONS);
2325 tf = (edit ? new JTextField(editMe.action, 40) : new JTextField(40));
2326 tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
2327 tf.getAccessibleContext().setAccessibleName(PERM_ACTIONS);
2328 if (edit) {
2329 setPermissionActions(getPerm(editMe.permission, true), choice, tf);
2330 }
2331 tw.addNewComponent(newTD, choice, PD_ACTIONS_CHOICE,
2332 0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2333 ToolWindow.LR_BOTTOM_PADDING);
2334 tw.addNewComponent(newTD, tf, PD_ACTIONS_TEXTFIELD,
2335 1, 3, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH,
2336 ToolWindow.LR_BOTTOM_PADDING);
2337 choice.addItemListener(new PermissionActionsMenuListener(newTD));
2338
2339 // signedby label and textfield
2340 label = new JLabel(PolicyTool.getMessage("Signed.By."));
2341 tw.addNewComponent(newTD, label, PD_SIGNEDBY_LABEL,
2342 0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2499 (PolicyTool.getMessage
2500 ("Warning.A.public.key.for.alias.signers.i.does.not.exist.Make.sure.a.KeyStore.is.properly.configured."));
2501 Object[] source = {signers[i]};
2502 tool.warnings.addElement(form.format(source));
2503 tw.displayStatusDialog(this, form.format(source));
2504 }
2505 } catch (Exception e) {
2506 tw.displayErrorDialog(this, e);
2507 }
2508 }
2509 }
2510 return pppe;
2511 }
2512
2513 /**
2514 * confirm that the user REALLY wants to remove the Policy Entry
2515 */
2516 void displayConfirmRemovePolicyEntry() {
2517
2518 // find the entry to be removed
2519 @SuppressWarnings("unchecked")
2520 JList<String> list = (JList<String>)tw.getComponent(ToolWindow.MW_POLICY_LIST);
2521 int index = list.getSelectedIndex();
2522 PolicyEntry entries[] = tool.getEntry();
2523
2524 // find where the PolicyTool gui is
2525 Point location = tw.getLocationOnScreen();
2526 //setBounds(location.x + 25, location.y + 100, 600, 400);
2527 setLayout(new GridBagLayout());
2528
2529 // ask the user do they really want to do this?
2530 JLabel label = new JLabel
2531 (PolicyTool.getMessage("Remove.this.Policy.Entry."));
2532 tw.addNewComponent(this, label, CRPE_LABEL1,
2533 0, 0, 2, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2534 ToolWindow.BOTTOM_PADDING);
2535
2536 // display the policy entry
2537 label = new JLabel(entries[index].codebaseToString());
2538 tw.addNewComponent(this, label, CRPE_LABEL2,
2539 0, 1, 2, 1, 0.0, 0.0, GridBagConstraints.BOTH);
2540 label = new JLabel(entries[index].principalsToString().trim());
2720 ToolDialog us, int select) {
2721
2722 // now either QUIT, open a NEW policy file, or OPEN an existing policy
2723 switch(select) {
2724 case ToolDialog.QUIT:
2725
2726 tw.setVisible(false);
2727 tw.dispose();
2728 System.exit(0);
2729
2730 case ToolDialog.NEW:
2731
2732 try {
2733 tool.openPolicy(null);
2734 } catch (Exception ee) {
2735 tool.modified = false;
2736 tw.displayErrorDialog(null, ee);
2737 }
2738
2739 // display the policy entries via the policy list textarea
2740 JList<String> list = new JList<>(new DefaultListModel<>());
2741 list.setVisibleRowCount(15);
2742 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
2743 list.addMouseListener(new PolicyListListener(tool, tw));
2744 tw.replacePolicyList(list);
2745
2746 // display null policy filename and keystore
2747 JTextField newFilename = (JTextField)tw.getComponent(
2748 ToolWindow.MW_FILENAME_TEXTFIELD);
2749 newFilename.setText("");
2750 tw.setVisible(true);
2751 break;
2752
2753 case ToolDialog.OPEN:
2754
2755 // pop up a dialog box for the user to enter a filename.
2756 FileDialog fd = new FileDialog
2757 (tw, PolicyTool.getMessage("Open"), FileDialog.LOAD);
2758 fd.addWindowListener(new WindowAdapter() {
2759 public void windowClosing(WindowEvent e) {
2760 e.getWindow().setVisible(false);
2761 }
2762 });
2763 fd.setVisible(true);
2764
2765 // see if the user hit 'cancel'
2766 if (fd.getFile() == null ||
2767 fd.getFile().equals(""))
2768 return;
2769
2770 // get the entered filename
2771 String policyFile = new File(fd.getDirectory(), fd.getFile()).getPath();
2772
2773 try {
2774 // open the policy file
2775 tool.openPolicy(policyFile);
2776
2777 // display the policy entries via the policy list textarea
2778 DefaultListModel<String> listModel = new DefaultListModel<>();
2779 list = new JList<>(listModel);
2780 list.setVisibleRowCount(15);
2781 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
2782 list.addMouseListener(new PolicyListListener(tool, tw));
2783 PolicyEntry entries[] = tool.getEntry();
2784 if (entries != null) {
2785 for (int i = 0; i < entries.length; i++) {
2786 listModel.addElement(entries[i].headerToString());
2787 }
2788 }
2789 tw.replacePolicyList(list);
2790 tool.modified = false;
2791
2792 // display the new policy filename
2793 newFilename = (JTextField)tw.getComponent(
2794 ToolWindow.MW_FILENAME_TEXTFIELD);
2795 newFilename.setText(policyFile);
2796 tw.setVisible(true);
2797
2798 // inform user of warnings
2799 if (tool.newWarning == true) {
2800 tw.displayStatusDialog(null, PolicyTool.getMessage
2801 ("Errors.have.occurred.while.opening.the.policy.configuration.View.the.Warning.Log.for.more.information."));
2802 }
2803
2804 } catch (Exception e) {
2805 // add blank policy listing
2806 list = new JList<>(new DefaultListModel<>());
2807 list.setVisibleRowCount(15);
2808 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
2809 list.addMouseListener(new PolicyListListener(tool, tw));
2810 tw.replacePolicyList(list);
2811 tool.setPolicyFileName(null);
2812 tool.modified = false;
2813
2814 // display a null policy filename
2815 newFilename = (JTextField)tw.getComponent(
2816 ToolWindow.MW_FILENAME_TEXTFIELD);
2817 newFilename.setText("");
2818 tw.setVisible(true);
2819
2820 // display the error
2821 MessageFormat form = new MessageFormat(PolicyTool.getMessage
2822 ("Could.not.open.policy.file.policyFile.e.toString."));
2823 Object[] source = {policyFile, e.toString()};
2824 tw.displayErrorDialog(null, form.format(source));
2825 }
2826 break;
2827 }
2828 }
2829
2830 /**
2831 * Return a Menu list of names for a given permission
2832 *
2833 * If inputPerm's TARGETS are null, then this means TARGETS are
2834 * not allowed to be entered (and the TextField is set to be
2835 * non-editable).
2836 *
2837 * If TARGETS are valid but there are no standard ones
2838 * (user must enter them by hand) then the TARGETS array may be empty
2839 * (and of course non-null).
2840 */
2841 void setPermissionNames(Perm inputPerm, JComboBox<String> names, JTextField field) {
2842 names.removeAllItems();
2843 names.addItem(PERM_NAME);
2844
2845 if (inputPerm == null) {
2846 // custom permission
2847 field.setEditable(true);
2848 } else if (inputPerm.TARGETS == null) {
2849 // standard permission with no targets
2850 field.setEditable(false);
2851 } else {
2852 // standard permission with standard targets
2853 field.setEditable(true);
2854 for (int i = 0; i < inputPerm.TARGETS.length; i++) {
2855 names.addItem(inputPerm.TARGETS[i]);
2856 }
2857 }
2858 }
2859
2860 /**
2861 * Return a Menu list of actions for a given permission
2862 *
2863 * If inputPerm's ACTIONS are null, then this means ACTIONS are
2864 * not allowed to be entered (and the TextField is set to be
2865 * non-editable). This is typically true for BasicPermissions.
2866 *
2867 * If ACTIONS are valid but there are no standard ones
2868 * (user must enter them by hand) then the ACTIONS array may be empty
2869 * (and of course non-null).
2870 */
2871 void setPermissionActions(Perm inputPerm, JComboBox<String> actions, JTextField field) {
2872 actions.removeAllItems();
2873 actions.addItem(PERM_ACTIONS);
2874
2875 if (inputPerm == null) {
2876 // custom permission
2877 field.setEditable(true);
2878 } else if (inputPerm.ACTIONS == null) {
2879 // standard permission with no actions
2880 field.setEditable(false);
2881 } else {
2882 // standard permission with standard actions
2883 field.setEditable(true);
2884 for (int i = 0; i < inputPerm.ACTIONS.length; i++) {
2885 actions.addItem(inputPerm.ACTIONS[i]);
2886 }
2887 }
2888 }
2889
2890 static String PermissionEntryToUserFriendlyString(PolicyParser.PermissionEntry pppe) {
2891 String result = pppe.permission;
3090
3091 MainWindowListener(PolicyTool tool, ToolWindow tw) {
3092 this.tool = tool;
3093 this.tw = tw;
3094 }
3095
3096 public void actionPerformed(ActionEvent e) {
3097
3098 if (PolicyTool.collator.compare(e.getActionCommand(),
3099 ToolWindow.ADD_POLICY_ENTRY) == 0) {
3100
3101 // display a dialog box for the user to enter policy info
3102 ToolDialog td = new ToolDialog
3103 (PolicyTool.getMessage("Policy.Entry"), tool, tw, true);
3104 td.displayPolicyEntryDialog(false);
3105
3106 } else if (PolicyTool.collator.compare(e.getActionCommand(),
3107 ToolWindow.REMOVE_POLICY_ENTRY) == 0) {
3108
3109 // get the selected entry
3110 @SuppressWarnings("unchecked")
3111 JList<String> list = (JList<String>)tw.getComponent(ToolWindow.MW_POLICY_LIST);
3112 int index = list.getSelectedIndex();
3113 if (index < 0) {
3114 tw.displayErrorDialog(null, new Exception
3115 (PolicyTool.getMessage("No.Policy.Entry.selected")));
3116 return;
3117 }
3118
3119 // ask the user if they really want to remove the policy entry
3120 ToolDialog td = new ToolDialog(PolicyTool.getMessage
3121 ("Remove.Policy.Entry"), tool, tw, true);
3122 td.displayConfirmRemovePolicyEntry();
3123
3124 } else if (PolicyTool.collator.compare(e.getActionCommand(),
3125 ToolWindow.EDIT_POLICY_ENTRY) == 0) {
3126
3127 // get the selected entry
3128 @SuppressWarnings("unchecked")
3129 JList<String> list = (JList<String>)tw.getComponent(ToolWindow.MW_POLICY_LIST);
3130 int index = list.getSelectedIndex();
3131 if (index < 0) {
3132 tw.displayErrorDialog(null, new Exception
3133 (PolicyTool.getMessage("No.Policy.Entry.selected")));
3134 return;
3135 }
3136
3137 // display the permission list for a policy entry
3138 ToolDialog td = new ToolDialog
3139 (PolicyTool.getMessage("Policy.Entry"), tool, tw, true);
3140 td.displayPolicyEntryDialog(true);
3141
3142 } else if (PolicyTool.collator.compare(e.getActionCommand(),
3143 ToolWindow.EDIT_KEYSTORE) == 0) {
3144
3145 // display a dialog box for the user to enter keystore info
3146 ToolDialog td = new ToolDialog
3147 (PolicyTool.getMessage("KeyStore"), tool, tw, true);
3148 td.keyStoreDialog(ToolDialog.EDIT_KEYSTORE);
3149 }
3180 PolicyEntry newEntry = td.getPolicyEntryFromDialog();
3181 PolicyParser.GrantEntry newGe = newEntry.getGrantEntry();
3182
3183 // see if all the signers have public keys
3184 if (newGe.signedBy != null) {
3185 String signers[] = tool.parseSigners(newGe.signedBy);
3186 for (int i = 0; i < signers.length; i++) {
3187 PublicKey pubKey = tool.getPublicKeyAlias(signers[i]);
3188 if (pubKey == null) {
3189 MessageFormat form = new MessageFormat
3190 (PolicyTool.getMessage
3191 ("Warning.A.public.key.for.alias.signers.i.does.not.exist.Make.sure.a.KeyStore.is.properly.configured."));
3192 Object[] source = {signers[i]};
3193 tool.warnings.addElement(form.format(source));
3194 tw.displayStatusDialog(td, form.format(source));
3195 }
3196 }
3197 }
3198
3199 // add the entry
3200 @SuppressWarnings("unchecked")
3201 JList<String> policyList = (JList<String>)tw.getComponent(ToolWindow.MW_POLICY_LIST);
3202 if (edit) {
3203 int listIndex = policyList.getSelectedIndex();
3204 tool.addEntry(newEntry, listIndex);
3205 String newCodeBaseStr = newEntry.headerToString();
3206 if (PolicyTool.collator.compare
3207 (newCodeBaseStr, policyList.getModel().getElementAt(listIndex)) != 0)
3208 tool.modified = true;
3209 ((DefaultListModel<String>)policyList.getModel()).set(listIndex, newCodeBaseStr);
3210 } else {
3211 tool.addEntry(newEntry, -1);
3212 ((DefaultListModel<String>)policyList.getModel()).addElement(newEntry.headerToString());
3213 tool.modified = true;
3214 }
3215 td.setVisible(false);
3216 td.dispose();
3217
3218 } catch (Exception eee) {
3219 tw.displayErrorDialog(td, eee);
3220 }
3221 }
3222 }
3223
3224 /**
3225 * Event handler for ChangeKeyStoreOKButton button
3226 */
3227 class ChangeKeyStoreOKButtonListener implements ActionListener {
3228
3229 private PolicyTool tool;
3230 private ToolWindow tw;
3231 private ToolDialog td;
3232
3570 * update both the GUI listing and the actual PolicyEntry.
3571 */
3572 class EditPermButtonListener extends MouseAdapter implements ActionListener {
3573
3574 private PolicyTool tool;
3575 private ToolWindow tw;
3576 private ToolDialog td;
3577 private boolean editPolicyEntry;
3578
3579 EditPermButtonListener(PolicyTool tool, ToolWindow tw,
3580 ToolDialog td, boolean editPolicyEntry) {
3581 this.tool = tool;
3582 this.tw = tw;
3583 this.td = td;
3584 this.editPolicyEntry = editPolicyEntry;
3585 }
3586
3587 public void actionPerformed(ActionEvent e) {
3588
3589 // get the Permission selected from the Permission List
3590 @SuppressWarnings("unchecked")
3591 JList<String> list = (JList<String>)td.getComponent(ToolDialog.PE_PERM_LIST);
3592 int permIndex = list.getSelectedIndex();
3593
3594 if (permIndex < 0) {
3595 tw.displayErrorDialog(td, new Exception
3596 (PolicyTool.getMessage("No.permission.selected")));
3597 return;
3598 }
3599 td.displayPermissionDialog(editPolicyEntry, true);
3600 }
3601
3602 public void mouseClicked(MouseEvent evt) {
3603 if (evt.getClickCount() == 2) {
3604 actionPerformed(null);
3605 }
3606 }
3607 }
3608
3609 /**
3610 * Event handler for Principal Popup Menu
3611 */
3612 class PrincipalTypeMenuListener implements ItemListener {
3613
3614 private ToolDialog td;
3615
3616 PrincipalTypeMenuListener(ToolDialog td) {
3617 this.td = td;
3618 }
3619
3620 public void itemStateChanged(ItemEvent e) {
3621 if (e.getStateChange() == ItemEvent.DESELECTED) {
3622 // We're only interested in SELECTED events
3623 return;
3624 }
3625
3626 @SuppressWarnings("unchecked")
3627 JComboBox<String> prin = (JComboBox<String>)td.getComponent(ToolDialog.PRD_PRIN_CHOICE);
3628 JTextField prinField = (JTextField)td.getComponent(
3629 ToolDialog.PRD_PRIN_TEXTFIELD);
3630 JTextField nameField = (JTextField)td.getComponent(
3631 ToolDialog.PRD_NAME_TEXTFIELD);
3632
3633 prin.getAccessibleContext().setAccessibleName(
3634 PolicyTool.splitToWords((String)e.getItem()));
3635 if (((String)e.getItem()).equals(ToolDialog.PRIN_TYPE)) {
3636 // ignore if they choose "Principal Type:" item
3637 if (prinField.getText() != null &&
3638 prinField.getText().length() > 0) {
3639 Prin inputPrin = ToolDialog.getPrin(prinField.getText(), true);
3640 prin.setSelectedItem(inputPrin.CLASS);
3641 }
3642 return;
3643 }
3644
3645 // if you change the principal, clear the name
3646 if (prinField.getText().indexOf((String)e.getItem()) == -1) {
3647 nameField.setText("");
3657 }
3658 }
3659
3660 /**
3661 * Event handler for Permission Popup Menu
3662 */
3663 class PermissionMenuListener implements ItemListener {
3664
3665 private ToolDialog td;
3666
3667 PermissionMenuListener(ToolDialog td) {
3668 this.td = td;
3669 }
3670
3671 public void itemStateChanged(ItemEvent e) {
3672 if (e.getStateChange() == ItemEvent.DESELECTED) {
3673 // We're only interested in SELECTED events
3674 return;
3675 }
3676
3677 @SuppressWarnings("unchecked")
3678 JComboBox<String> perms = (JComboBox<String>)td.getComponent(
3679 ToolDialog.PD_PERM_CHOICE);
3680 @SuppressWarnings("unchecked")
3681 JComboBox<String> names = (JComboBox<String>)td.getComponent(
3682 ToolDialog.PD_NAME_CHOICE);
3683 @SuppressWarnings("unchecked")
3684 JComboBox<String> actions = (JComboBox<String>)td.getComponent(
3685 ToolDialog.PD_ACTIONS_CHOICE);
3686 JTextField nameField = (JTextField)td.getComponent(
3687 ToolDialog.PD_NAME_TEXTFIELD);
3688 JTextField actionsField = (JTextField)td.getComponent(
3689 ToolDialog.PD_ACTIONS_TEXTFIELD);
3690 JTextField permField = (JTextField)td.getComponent(
3691 ToolDialog.PD_PERM_TEXTFIELD);
3692 JTextField signedbyField = (JTextField)td.getComponent(
3693 ToolDialog.PD_SIGNEDBY_TEXTFIELD);
3694
3695 perms.getAccessibleContext().setAccessibleName(
3696 PolicyTool.splitToWords((String)e.getItem()));
3697
3698 // ignore if they choose the 'Permission:' item
3699 if (PolicyTool.collator.compare((String)e.getItem(),
3700 ToolDialog.PERM) == 0) {
3701 if (permField.getText() != null &&
3702 permField.getText().length() > 0) {
3703
3704 Perm inputPerm = ToolDialog.getPerm(permField.getText(), true);
3731 }
3732 }
3733
3734 /**
3735 * Event handler for Permission Name Popup Menu
3736 */
3737 class PermissionNameMenuListener implements ItemListener {
3738
3739 private ToolDialog td;
3740
3741 PermissionNameMenuListener(ToolDialog td) {
3742 this.td = td;
3743 }
3744
3745 public void itemStateChanged(ItemEvent e) {
3746 if (e.getStateChange() == ItemEvent.DESELECTED) {
3747 // We're only interested in SELECTED events
3748 return;
3749 }
3750
3751 @SuppressWarnings("unchecked")
3752 JComboBox<String> names = (JComboBox<String>)td.getComponent(ToolDialog.PD_NAME_CHOICE);
3753 names.getAccessibleContext().setAccessibleName(
3754 PolicyTool.splitToWords((String)e.getItem()));
3755
3756 if (((String)e.getItem()).indexOf(ToolDialog.PERM_NAME) != -1)
3757 return;
3758
3759 JTextField tf = (JTextField)td.getComponent(ToolDialog.PD_NAME_TEXTFIELD);
3760 tf.setText((String)e.getItem());
3761 }
3762 }
3763
3764 /**
3765 * Event handler for Permission Actions Popup Menu
3766 */
3767 class PermissionActionsMenuListener implements ItemListener {
3768
3769 private ToolDialog td;
3770
3771 PermissionActionsMenuListener(ToolDialog td) {
3772 this.td = td;
3773 }
3774
3775 public void itemStateChanged(ItemEvent e) {
3776 if (e.getStateChange() == ItemEvent.DESELECTED) {
3777 // We're only interested in SELECTED events
3778 return;
3779 }
3780
3781 @SuppressWarnings("unchecked")
3782 JComboBox<String> actions = (JComboBox<String>)td.getComponent(
3783 ToolDialog.PD_ACTIONS_CHOICE);
3784 actions.getAccessibleContext().setAccessibleName((String)e.getItem());
3785
3786 if (((String)e.getItem()).indexOf(ToolDialog.PERM_ACTIONS) != -1)
3787 return;
3788
3789 JTextField tf = (JTextField)td.getComponent(
3790 ToolDialog.PD_ACTIONS_TEXTFIELD);
3791 if (tf.getText() == null || tf.getText().equals("")) {
3792 tf.setText((String)e.getItem());
3793 } else {
3794 if (tf.getText().indexOf((String)e.getItem()) == -1)
3795 tf.setText(tf.getText() + ", " + (String)e.getItem());
3796 }
3797 }
3798 }
3799
3800 /**
3801 * Event handler for all the children dialogs/windows
3802 */
3987 }
3988
3989 /**
3990 * Event handler for ConfirmRemovePolicyEntryOKButtonListener
3991 */
3992 class ConfirmRemovePolicyEntryOKButtonListener implements ActionListener {
3993
3994 private PolicyTool tool;
3995 private ToolWindow tw;
3996 private ToolDialog us;
3997
3998 ConfirmRemovePolicyEntryOKButtonListener(PolicyTool tool,
3999 ToolWindow tw, ToolDialog us) {
4000 this.tool = tool;
4001 this.tw = tw;
4002 this.us = us;
4003 }
4004
4005 public void actionPerformed(ActionEvent e) {
4006 // remove the entry
4007 @SuppressWarnings("unchecked")
4008 JList<String> list = (JList<String>)tw.getComponent(ToolWindow.MW_POLICY_LIST);
4009 int index = list.getSelectedIndex();
4010 PolicyEntry entries[] = tool.getEntry();
4011 tool.removeEntry(entries[index]);
4012
4013 // redraw the window listing
4014 DefaultListModel<String> listModel = new DefaultListModel<>();
4015 list = new JList<>(listModel);
4016 list.setVisibleRowCount(15);
4017 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
4018 list.addMouseListener(new PolicyListListener(tool, tw));
4019 entries = tool.getEntry();
4020 if (entries != null) {
4021 for (int i = 0; i < entries.length; i++) {
4022 listModel.addElement(entries[i].headerToString());
4023 }
4024 }
4025 tw.replacePolicyList(list);
4026 us.setVisible(false);
4027 us.dispose();
4028 }
4029 }
4030
4031 /**
4032 * Just a special name, so that the codes dealing with this exception knows
4033 * it's special, and does not pop out a warning box.
4034 */
4035 class NoDisplayException extends RuntimeException {
4036 private static final long serialVersionUID = -4611761427108719794L;
4037 }
4038
4039 /**
4040 * This is a java.awt.List that bind an Object to each String it holds.
4041 */
4042 class TaggedList extends JList<String> {
4043 private static final long serialVersionUID = -5676238110427785853L;
4044
4045 private java.util.List<Object> data = new LinkedList<>();
4046 public TaggedList(int i, boolean b) {
4047 super(new DefaultListModel<>());
4048 setVisibleRowCount(i);
4049 setSelectionMode(b ? ListSelectionModel.MULTIPLE_INTERVAL_SELECTION : ListSelectionModel.SINGLE_SELECTION);
4050 }
4051
4052 public Object getObject(int index) {
4053 return data.get(index);
4054 }
4055
4056 public void addTaggedItem(String string, Object object) {
4057 ((DefaultListModel<String>)getModel()).addElement(string);
4058 data.add(object);
4059 }
4060
4061 public void replaceTaggedItem(String string, Object object, int index) {
4062 ((DefaultListModel<String>)getModel()).set(index, string);
4063 data.set(index, object);
4064 }
4065
4066 public void removeTaggedItem(int index) {
4067 ((DefaultListModel<String>)getModel()).remove(index);
4068 data.remove(index);
4069 }
4070 }
4071
4072 /**
4073 * Convenience Principal Classes
4074 */
4075
4076 class Prin {
4077 public final String CLASS;
4078 public final String FULL_CLASS;
4079
4080 public Prin(String clazz, String fullClass) {
4081 this.CLASS = clazz;
4082 this.FULL_CLASS = fullClass;
4083 }
4084 }
4085
4086 class KrbPrin extends Prin {
4087 public KrbPrin() {
|