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
  23  * questions.
  24  */
  25 
  26 package sun.security.tools.policytool;
  27 
  28 import java.io.*;
  29 import java.util.LinkedList;
  30 import java.util.ListIterator;
  31 import java.util.Vector;
  32 import java.util.Enumeration;
  33 import java.net.URL;
  34 import java.net.MalformedURLException;
  35 import java.lang.reflect.*;
  36 import java.text.Collator;
  37 import java.text.MessageFormat;
  38 import sun.security.util.PropertyExpander;
  39 import sun.security.util.PropertyExpander.ExpandException;
  40 import java.awt.Component;
  41 import java.awt.Container;
  42 import java.awt.Dimension;
  43 import java.awt.FileDialog;
  44 import java.awt.GridBagConstraints;
  45 import java.awt.GridBagLayout;
  46 import java.awt.Insets;
  47 import java.awt.Point;
  48 import java.awt.Toolkit;
  49 import java.awt.Window;
  50 import java.awt.event.*;
  51 import java.security.cert.Certificate;
  52 import java.security.cert.CertificateException;
  53 import java.security.*;
  54 import sun.security.provider.*;
  55 import sun.security.util.PolicyUtil;
  56 import javax.security.auth.x500.X500Principal;
  57 import javax.swing.*;
  58 import javax.swing.border.EmptyBorder;
  59 
  60 /**
  61  * PolicyTool may be used by users and administrators to configure the
  62  * overall java security policy (currently stored in the policy file).
  63  * Using PolicyTool administrators may add and remove policies from
  64  * the policy file. <p>
  65  *
  66  * @see java.security.Policy
  67  * @since   1.2
  68  */
  69 
  70 public class PolicyTool {
  71 
  72     // for i18n
  73     static final java.util.ResourceBundle rb =
  74         java.util.ResourceBundle.getBundle(
  75             "sun.security.tools.policytool.Resources");
  76     static final Collator collator = Collator.getInstance();
  77     static {
  78         // this is for case insensitive string comparisons
  79         collator.setStrength(Collator.PRIMARY);
  80 
  81         // Support for Apple menu bar
  82         if (System.getProperty("apple.laf.useScreenMenuBar") == null) {
  83             System.setProperty("apple.laf.useScreenMenuBar", "true");
  84         }
  85         System.setProperty("apple.awt.application.name", getMessage("Policy.Tool"));
  86 
  87         // Apply the system L&F if not specified with a system property.
  88         if (System.getProperty("swing.defaultlaf") == null) {
  89             try {
  90                 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  91             } catch (Exception e) {
  92                 // ignore
  93             }
  94         }
  95     }
  96 
  97     // anyone can add warnings
  98     Vector<String> warnings;
  99     boolean newWarning = false;
 100 
 101     // set to true if policy modified.
 102     // this way upon exit we know if to ask the user to save changes
 103     boolean modified = false;
 104 
 105     private static final boolean testing = false;
 106     private static final Class<?>[] TWOPARAMS = { String.class, String.class };
 107     private static final Class<?>[] ONEPARAMS = { String.class };
 108     private static final Class<?>[] NOPARAMS  = {};
 109     /*
 110      * All of the policy entries are read in from the
 111      * policy file and stored here.  Updates to the policy entries
 112      * using addEntry() and removeEntry() are made here.  To ultimately save
 113      * the policy entries back to the policy file, the SavePolicy button
 114      * must be clicked.
 115      **/
 116     private static String policyFileName = null;
 117     private Vector<PolicyEntry> policyEntries = null;
 118     private PolicyParser parser = null;
 119 
 120     /* The public key alias information is stored here.  */
 121     private KeyStore keyStore = null;
 122     private String keyStoreName = " ";
 123     private String keyStoreType = " ";
 124     private String keyStoreProvider = " ";
 125     private String keyStorePwdURL = " ";
 126 
 127     /* standard PKCS11 KeyStore type */
 128     private static final String P11KEYSTORE = "PKCS11";
 129 
 130     /* reserved word for PKCS11 KeyStores */
 131     private static final String NONE = "NONE";
 132 
 133     /**
 134      * default constructor
 135      */
 136     private PolicyTool() {
 137         policyEntries = new Vector<PolicyEntry>();
 138         parser = new PolicyParser();
 139         warnings = new Vector<String>();
 140     }
 141 
 142     /**
 143      * get the PolicyFileName
 144      */
 145     String getPolicyFileName() {
 146         return policyFileName;
 147     }
 148 
 149     /**
 150      * set the PolicyFileName
 151      */
 152     void setPolicyFileName(String policyFileName) {
 153         PolicyTool.policyFileName = policyFileName;
 154     }
 155 
 156    /**
 157     * clear keyStore info
 158     */
 159     void clearKeyStoreInfo() {
 160         this.keyStoreName = null;
 161         this.keyStoreType = null;
 162         this.keyStoreProvider = null;
 163         this.keyStorePwdURL = null;
 164 
 165         this.keyStore = null;
 166     }
 167 
 168     /**
 169      * get the keyStore URL name
 170      */
 171     String getKeyStoreName() {
 172         return keyStoreName;
 173     }
 174 
 175     /**
 176      * get the keyStore Type
 177      */
 178     String getKeyStoreType() {
 179         return keyStoreType;
 180     }
 181 
 182     /**
 183      * get the keyStore Provider
 184      */
 185     String getKeyStoreProvider() {
 186         return keyStoreProvider;
 187     }
 188 
 189     /**
 190      * get the keyStore password URL
 191      */
 192     String getKeyStorePwdURL() {
 193         return keyStorePwdURL;
 194     }
 195 
 196     /**
 197      * Open and read a policy file
 198      */
 199     void openPolicy(String filename) throws FileNotFoundException,
 200                                         PolicyParser.ParsingException,
 201                                         KeyStoreException,
 202                                         CertificateException,
 203                                         InstantiationException,
 204                                         MalformedURLException,
 205                                         IOException,
 206                                         NoSuchAlgorithmException,
 207                                         IllegalAccessException,
 208                                         NoSuchMethodException,
 209                                         UnrecoverableKeyException,
 210                                         NoSuchProviderException,
 211                                         ClassNotFoundException,
 212                                         PropertyExpander.ExpandException,
 213                                         InvocationTargetException {
 214 
 215         newWarning = false;
 216 
 217         // start fresh - blow away the current state
 218         policyEntries = new Vector<PolicyEntry>();
 219         parser = new PolicyParser();
 220         warnings = new Vector<String>();
 221         setPolicyFileName(null);
 222         clearKeyStoreInfo();
 223 
 224         // see if user is opening a NEW policy file
 225         if (filename == null) {
 226             modified = false;
 227             return;
 228         }
 229 
 230         // Read in the policy entries from the file and
 231         // populate the parser vector table.  The parser vector
 232         // table only holds the entries as strings, so it only
 233         // guarantees that the policies are syntactically
 234         // correct.
 235         setPolicyFileName(filename);
 236         parser.read(new FileReader(filename));
 237 
 238         // open the keystore
 239         openKeyStore(parser.getKeyStoreUrl(), parser.getKeyStoreType(),
 240                 parser.getKeyStoreProvider(), parser.getStorePassURL());
 241 
 242         // Update the local vector with the same policy entries.
 243         // This guarantees that the policy entries are not only
 244         // syntactically correct, but semantically valid as well.
 245         Enumeration<PolicyParser.GrantEntry> enum_ = parser.grantElements();
 246         while (enum_.hasMoreElements()) {
 247             PolicyParser.GrantEntry ge = enum_.nextElement();
 248 
 249             // see if all the signers have public keys
 250             if (ge.signedBy != null) {
 251 
 252                 String signers[] = parseSigners(ge.signedBy);
 253                 for (int i = 0; i < signers.length; i++) {
 254                     PublicKey pubKey = getPublicKeyAlias(signers[i]);
 255                     if (pubKey == null) {
 256                         newWarning = true;
 257                         MessageFormat form = new MessageFormat(getMessage
 258                             ("Warning.A.public.key.for.alias.signers.i.does.not.exist.Make.sure.a.KeyStore.is.properly.configured."));
 259                         Object[] source = {signers[i]};
 260                         warnings.addElement(form.format(source));
 261                     }
 262                 }
 263             }
 264 
 265             // check to see if the Principals are valid
 266             ListIterator<PolicyParser.PrincipalEntry> prinList =
 267                                                 ge.principals.listIterator(0);
 268             while (prinList.hasNext()) {
 269                 PolicyParser.PrincipalEntry pe = prinList.next();
 270                 try {
 271                     verifyPrincipal(pe.getPrincipalClass(),
 272                                 pe.getPrincipalName());
 273                 } catch (ClassNotFoundException fnfe) {
 274                     newWarning = true;
 275                     MessageFormat form = new MessageFormat(getMessage
 276                                 ("Warning.Class.not.found.class"));
 277                     Object[] source = {pe.getPrincipalClass()};
 278                     warnings.addElement(form.format(source));
 279                 }
 280             }
 281 
 282             // check to see if the Permissions are valid
 283             Enumeration<PolicyParser.PermissionEntry> perms =
 284                                                 ge.permissionElements();
 285             while (perms.hasMoreElements()) {
 286                 PolicyParser.PermissionEntry pe = perms.nextElement();
 287                 try {
 288                     verifyPermission(pe.permission, pe.name, pe.action);
 289                 } catch (ClassNotFoundException fnfe) {
 290                     newWarning = true;
 291                     MessageFormat form = new MessageFormat(getMessage
 292                                 ("Warning.Class.not.found.class"));
 293                     Object[] source = {pe.permission};
 294                     warnings.addElement(form.format(source));
 295                 } catch (InvocationTargetException ite) {
 296                     newWarning = true;
 297                     MessageFormat form = new MessageFormat(getMessage
 298                         ("Warning.Invalid.argument.s.for.constructor.arg"));
 299                     Object[] source = {pe.permission};
 300                     warnings.addElement(form.format(source));
 301                 }
 302 
 303                 // see if all the permission signers have public keys
 304                 if (pe.signedBy != null) {
 305 
 306                     String signers[] = parseSigners(pe.signedBy);
 307 
 308                     for (int i = 0; i < signers.length; i++) {
 309                         PublicKey pubKey = getPublicKeyAlias(signers[i]);
 310                         if (pubKey == null) {
 311                             newWarning = true;
 312                             MessageFormat form = new MessageFormat(getMessage
 313                                 ("Warning.A.public.key.for.alias.signers.i.does.not.exist.Make.sure.a.KeyStore.is.properly.configured."));
 314                             Object[] source = {signers[i]};
 315                             warnings.addElement(form.format(source));
 316                         }
 317                     }
 318                 }
 319             }
 320             PolicyEntry pEntry = new PolicyEntry(this, ge);
 321             policyEntries.addElement(pEntry);
 322         }
 323 
 324         // just read in the policy -- nothing has been modified yet
 325         modified = false;
 326     }
 327 
 328 
 329     /**
 330      * Save a policy to a file
 331      */
 332     void savePolicy(String filename)
 333     throws FileNotFoundException, IOException {
 334         // save the policy entries to a file
 335         parser.setKeyStoreUrl(keyStoreName);
 336         parser.setKeyStoreType(keyStoreType);
 337         parser.setKeyStoreProvider(keyStoreProvider);
 338         parser.setStorePassURL(keyStorePwdURL);
 339         parser.write(new FileWriter(filename));
 340         modified = false;
 341     }
 342 
 343     /**
 344      * Open the KeyStore
 345      */
 346     void openKeyStore(String name,
 347                 String type,
 348                 String provider,
 349                 String pwdURL) throws   KeyStoreException,
 350                                         NoSuchAlgorithmException,
 351                                         UnrecoverableKeyException,
 352                                         IOException,
 353                                         CertificateException,
 354                                         NoSuchProviderException,
 355                                         ExpandException {
 356 
 357         if (name == null && type == null &&
 358             provider == null && pwdURL == null) {
 359 
 360             // policy did not specify a keystore during open
 361             // or use wants to reset keystore values
 362 
 363             this.keyStoreName = null;
 364             this.keyStoreType = null;
 365             this.keyStoreProvider = null;
 366             this.keyStorePwdURL = null;
 367 
 368             // caller will set (tool.modified = true) if appropriate
 369 
 370             return;
 371         }
 372 
 373         URL policyURL = null;
 374         if (policyFileName != null) {
 375             File pfile = new File(policyFileName);
 376             policyURL = new URL("file:" + pfile.getCanonicalPath());
 377         }
 378 
 379         // although PolicyUtil.getKeyStore may properly handle
 380         // defaults and property expansion, we do it here so that
 381         // if the call is successful, we can set the proper values
 382         // (PolicyUtil.getKeyStore does not return expanded values)
 383 
 384         if (name != null && name.length() > 0) {
 385             name = PropertyExpander.expand(name).replace
 386                                         (File.separatorChar, '/');
 387         }
 388         if (type == null || type.length() == 0) {
 389             type = KeyStore.getDefaultType();
 390         }
 391         if (pwdURL != null && pwdURL.length() > 0) {
 392             pwdURL = PropertyExpander.expand(pwdURL).replace
 393                                         (File.separatorChar, '/');
 394         }
 395 
 396         try {
 397             this.keyStore = PolicyUtil.getKeyStore(policyURL,
 398                                                 name,
 399                                                 type,
 400                                                 provider,
 401                                                 pwdURL,
 402                                                 null);
 403         } catch (IOException ioe) {
 404 
 405             // copied from sun.security.pkcs11.SunPKCS11
 406             String MSG = "no password provided, and no callback handler " +
 407                         "available for retrieving password";
 408 
 409             Throwable cause = ioe.getCause();
 410             if (cause != null &&
 411                 cause instanceof javax.security.auth.login.LoginException &&
 412                 MSG.equals(cause.getMessage())) {
 413 
 414                 // throw a more friendly exception message
 415                 throw new IOException(MSG);
 416             } else {
 417                 throw ioe;
 418             }
 419         }
 420 
 421         this.keyStoreName = name;
 422         this.keyStoreType = type;
 423         this.keyStoreProvider = provider;
 424         this.keyStorePwdURL = pwdURL;
 425 
 426         // caller will set (tool.modified = true)
 427     }
 428 
 429     /**
 430      * Add a Grant entry to the overall policy at the specified index.
 431      * A policy entry consists of a CodeSource.
 432      */
 433     boolean addEntry(PolicyEntry pe, int index) {
 434 
 435         if (index < 0) {
 436             // new entry -- just add it to the end
 437             policyEntries.addElement(pe);
 438             parser.add(pe.getGrantEntry());
 439         } else {
 440             // existing entry -- replace old one
 441             PolicyEntry origPe = policyEntries.elementAt(index);
 442             parser.replace(origPe.getGrantEntry(), pe.getGrantEntry());
 443             policyEntries.setElementAt(pe, index);
 444         }
 445         return true;
 446     }
 447 
 448     /**
 449      * Add a Principal entry to an existing PolicyEntry at the specified index.
 450      * A Principal entry consists of a class, and name.
 451      *
 452      * If the principal already exists, it is not added again.
 453      */
 454     boolean addPrinEntry(PolicyEntry pe,
 455                         PolicyParser.PrincipalEntry newPrin,
 456                         int index) {
 457 
 458         // first add the principal to the Policy Parser entry
 459         PolicyParser.GrantEntry grantEntry = pe.getGrantEntry();
 460         if (grantEntry.contains(newPrin) == true)
 461             return false;
 462 
 463         LinkedList<PolicyParser.PrincipalEntry> prinList =
 464                                                 grantEntry.principals;
 465         if (index != -1)
 466             prinList.set(index, newPrin);
 467         else
 468             prinList.add(newPrin);
 469 
 470         modified = true;
 471         return true;
 472     }
 473 
 474     /**
 475      * Add a Permission entry to an existing PolicyEntry at the specified index.
 476      * A Permission entry consists of a permission, name, and actions.
 477      *
 478      * If the permission already exists, it is not added again.
 479      */
 480     boolean addPermEntry(PolicyEntry pe,
 481                         PolicyParser.PermissionEntry newPerm,
 482                         int index) {
 483 
 484         // first add the permission to the Policy Parser Vector
 485         PolicyParser.GrantEntry grantEntry = pe.getGrantEntry();
 486         if (grantEntry.contains(newPerm) == true)
 487             return false;
 488 
 489         Vector<PolicyParser.PermissionEntry> permList =
 490                                                 grantEntry.permissionEntries;
 491         if (index != -1)
 492             permList.setElementAt(newPerm, index);
 493         else
 494             permList.addElement(newPerm);
 495 
 496         modified = true;
 497         return true;
 498     }
 499 
 500     /**
 501      * Remove a Permission entry from an existing PolicyEntry.
 502      */
 503     boolean removePermEntry(PolicyEntry pe,
 504                         PolicyParser.PermissionEntry perm) {
 505 
 506         // remove the Permission from the GrantEntry
 507         PolicyParser.GrantEntry ppge = pe.getGrantEntry();
 508         modified = ppge.remove(perm);
 509         return modified;
 510     }
 511 
 512     /**
 513      * remove an entry from the overall policy
 514      */
 515     boolean removeEntry(PolicyEntry pe) {
 516 
 517         parser.remove(pe.getGrantEntry());
 518         modified = true;
 519         return (policyEntries.removeElement(pe));
 520     }
 521 
 522     /**
 523      * retrieve all Policy Entries
 524      */
 525     PolicyEntry[] getEntry() {
 526 
 527         if (policyEntries.size() > 0) {
 528             PolicyEntry entries[] = new PolicyEntry[policyEntries.size()];
 529             for (int i = 0; i < policyEntries.size(); i++)
 530                 entries[i] = policyEntries.elementAt(i);
 531             return entries;
 532         }
 533         return null;
 534     }
 535 
 536     /**
 537      * Retrieve the public key mapped to a particular name.
 538      * If the key has expired, a KeyException is thrown.
 539      */
 540     PublicKey getPublicKeyAlias(String name) throws KeyStoreException {
 541         if (keyStore == null) {
 542             return null;
 543         }
 544 
 545         Certificate cert = keyStore.getCertificate(name);
 546         if (cert == null) {
 547             return null;
 548         }
 549         PublicKey pubKey = cert.getPublicKey();
 550         return pubKey;
 551     }
 552 
 553     /**
 554      * Retrieve all the alias names stored in the certificate database
 555      */
 556     String[] getPublicKeyAlias() throws KeyStoreException {
 557 
 558         int numAliases = 0;
 559         String aliases[] = null;
 560 
 561         if (keyStore == null) {
 562             return null;
 563         }
 564         Enumeration<String> enum_ = keyStore.aliases();
 565 
 566         // first count the number of elements
 567         while (enum_.hasMoreElements()) {
 568             enum_.nextElement();
 569             numAliases++;
 570         }
 571 
 572         if (numAliases > 0) {
 573             // now copy them into an array
 574             aliases = new String[numAliases];
 575             numAliases = 0;
 576             enum_ = keyStore.aliases();
 577             while (enum_.hasMoreElements()) {
 578                 aliases[numAliases] = new String(enum_.nextElement());
 579                 numAliases++;
 580             }
 581         }
 582         return aliases;
 583     }
 584 
 585     /**
 586      * This method parses a single string of signers separated by commas
 587      * ("jordan, duke, pippen") into an array of individual strings.
 588      */
 589     String[] parseSigners(String signedBy) {
 590 
 591         String signers[] = null;
 592         int numSigners = 1;
 593         int signedByIndex = 0;
 594         int commaIndex = 0;
 595         int signerNum = 0;
 596 
 597         // first pass thru "signedBy" counts the number of signers
 598         while (commaIndex >= 0) {
 599             commaIndex = signedBy.indexOf(',', signedByIndex);
 600             if (commaIndex >= 0) {
 601                 numSigners++;
 602                 signedByIndex = commaIndex + 1;
 603             }
 604         }
 605         signers = new String[numSigners];
 606 
 607         // second pass thru "signedBy" transfers signers to array
 608         commaIndex = 0;
 609         signedByIndex = 0;
 610         while (commaIndex >= 0) {
 611             if ((commaIndex = signedBy.indexOf(',', signedByIndex)) >= 0) {
 612                 // transfer signer and ignore trailing part of the string
 613                 signers[signerNum] =
 614                         signedBy.substring(signedByIndex, commaIndex).trim();
 615                 signerNum++;
 616                 signedByIndex = commaIndex + 1;
 617             } else {
 618                 // we are at the end of the string -- transfer signer
 619                 signers[signerNum] = signedBy.substring(signedByIndex).trim();
 620             }
 621         }
 622         return signers;
 623     }
 624 
 625     /**
 626      * Check to see if the Principal contents are OK
 627      */
 628     void verifyPrincipal(String type, String name)
 629         throws ClassNotFoundException,
 630                InstantiationException
 631     {
 632         if (type.equals(PolicyParser.PrincipalEntry.WILDCARD_CLASS) ||
 633             type.equals(PolicyParser.PrincipalEntry.REPLACE_NAME)) {
 634             return;
 635         }
 636         Class<?> PRIN = Class.forName("java.security.Principal");
 637         Class<?> pc = Class.forName(type, true,
 638                 Thread.currentThread().getContextClassLoader());
 639         if (!PRIN.isAssignableFrom(pc)) {
 640             MessageFormat form = new MessageFormat(getMessage
 641                         ("Illegal.Principal.Type.type"));
 642             Object[] source = {type};
 643             throw new InstantiationException(form.format(source));
 644         }
 645 
 646         if (ToolDialog.X500_PRIN_CLASS.equals(pc.getName())) {
 647             // PolicyParser checks validity of X500Principal name
 648             // - PolicyTool needs to as well so that it doesn't store
 649             //   an invalid name that can't be read in later
 650             //
 651             // this can throw an IllegalArgumentException
 652             X500Principal newP = new X500Principal(name);
 653         }
 654     }
 655 
 656     /**
 657      * Check to see if the Permission contents are OK
 658      */
 659     @SuppressWarnings("fallthrough")
 660     void verifyPermission(String type,
 661                                     String name,
 662                                     String actions)
 663         throws ClassNotFoundException,
 664                InstantiationException,
 665                IllegalAccessException,
 666                NoSuchMethodException,
 667                InvocationTargetException
 668     {
 669 
 670         //XXX we might want to keep a hash of created factories...
 671         Class<?> pc = Class.forName(type, true,
 672                 Thread.currentThread().getContextClassLoader());
 673         Constructor<?> c = null;
 674         Vector<String> objects = new Vector<>(2);
 675         if (name != null) objects.add(name);
 676         if (actions != null) objects.add(actions);
 677         switch (objects.size()) {
 678         case 0:
 679             try {
 680                 c = pc.getConstructor(NOPARAMS);
 681                 break;
 682             } catch (NoSuchMethodException ex) {
 683                 // proceed to the one-param constructor
 684                 objects.add(null);
 685             }
 686             /* fall through */
 687         case 1:
 688             try {
 689                 c = pc.getConstructor(ONEPARAMS);
 690                 break;
 691             } catch (NoSuchMethodException ex) {
 692                 // proceed to the two-param constructor
 693                 objects.add(null);
 694             }
 695             /* fall through */
 696         case 2:
 697             c = pc.getConstructor(TWOPARAMS);
 698             break;
 699         }
 700         Object parameters[] = objects.toArray();
 701         Permission p = (Permission)c.newInstance(parameters);
 702     }
 703 
 704     /*
 705      * Parse command line arguments.
 706      */
 707     static void parseArgs(String args[]) {
 708         /* parse flags */
 709         int n = 0;
 710 
 711         for (n=0; (n < args.length) && args[n].startsWith("-"); n++) {
 712 
 713             String flags = args[n];
 714 
 715             if (collator.compare(flags, "-file") == 0) {
 716                 if (++n == args.length) usage();
 717                 policyFileName = args[n];
 718             } else {
 719                 MessageFormat form = new MessageFormat(getMessage
 720                                 ("Illegal.option.option"));
 721                 Object[] source = { flags };
 722                 System.err.println(form.format(source));
 723                 usage();
 724             }
 725         }
 726     }
 727 
 728     static void usage() {
 729         System.out.println(getMessage("Usage.policytool.options."));
 730         System.out.println();
 731         System.out.println(getMessage
 732                 (".file.file.policy.file.location"));
 733         System.out.println();
 734 
 735         System.exit(1);
 736     }
 737 
 738     /**
 739      * run the PolicyTool
 740      */
 741     public static void main(String args[]) {
 742         parseArgs(args);
 743         SwingUtilities.invokeLater(new Runnable() {
 744             public void run() {
 745                 ToolWindow tw = new ToolWindow(new PolicyTool());
 746                 tw.displayToolWindow(args);
 747             }
 748         });
 749     }
 750 
 751     // split instr to words according to capitalization,
 752     // like, AWTControl -> A W T Control
 753     // this method is for easy pronounciation
 754     static String splitToWords(String instr) {
 755         return instr.replaceAll("([A-Z])", " $1");
 756     }
 757 
 758     /**
 759      * Returns the message corresponding to the key in the bundle.
 760      * This is preferred over {@link #getString} because it removes
 761      * any mnemonic '&' character in the string.
 762      *
 763      * @param key the key
 764      *
 765      * @return the message
 766      */
 767     static String getMessage(String key) {
 768         return removeMnemonicAmpersand(rb.getString(key));
 769     }
 770 
 771 
 772     /**
 773      * Returns the mnemonic for a message.
 774      *
 775      * @param key the key
 776      *
 777      * @return the mnemonic <code>int</code>
 778      */
 779     static int getMnemonicInt(String key) {
 780         String message = rb.getString(key);
 781         return (findMnemonicInt(message));
 782     }
 783 
 784     /**
 785      * Returns the mnemonic display index for a message.
 786      *
 787      * @param key the key
 788      *
 789      * @return the mnemonic display index
 790      */
 791     static int getDisplayedMnemonicIndex(String key) {
 792         String message = rb.getString(key);
 793         return (findMnemonicIndex(message));
 794     }
 795 
 796     /**
 797      * Finds the mnemonic character in a message.
 798      *
 799      * The mnemonic character is the first character followed by the first
 800      * <code>&</code> that is not followed by another <code>&</code>.
 801      *
 802      * @return the mnemonic as an <code>int</code>, or <code>0</code> if it
 803      *         can't be found.
 804      */
 805     private static int findMnemonicInt(String s) {
 806         for (int i = 0; i < s.length() - 1; i++) {
 807             if (s.charAt(i) == '&') {
 808                 if (s.charAt(i + 1) != '&') {
 809                     return KeyEvent.getExtendedKeyCodeForChar(s.charAt(i + 1));
 810                 } else {
 811                     i++;
 812                 }
 813             }
 814         }
 815         return 0;
 816     }
 817 
 818     /**
 819      * Finds the index of the mnemonic character in a message.
 820      *
 821      * The mnemonic character is the first character followed by the first
 822      * <code>&</code> that is not followed by another <code>&</code>.
 823      *
 824      * @return the mnemonic character index as an <code>int</code>, or <code>-1</code> if it
 825      *         can't be found.
 826      */
 827     private static int findMnemonicIndex(String s) {
 828         for (int i = 0; i < s.length() - 1; i++) {
 829             if (s.charAt(i) == '&') {
 830                 if (s.charAt(i + 1) != '&') {
 831                     // Return the index of the '&' since it will be removed
 832                     return i;
 833                 } else {
 834                     i++;
 835                 }
 836             }
 837         }
 838         return -1;
 839     }
 840 
 841     /**
 842      * Removes the mnemonic identifier (<code>&</code>) from a string unless
 843      * it's escaped by <code>&&</code> or placed at the end.
 844      *
 845      * @param message the message
 846      *
 847      * @return a message with the mnemonic identifier removed
 848      */
 849     private static String removeMnemonicAmpersand(String message) {
 850         StringBuilder s = new StringBuilder();
 851         for (int i = 0; i < message.length(); i++) {
 852             char current = message.charAt(i);
 853             if (current != '&' || i == message.length() - 1
 854                     || message.charAt(i + 1) == '&') {
 855                 s.append(current);
 856             }
 857         }
 858         return s.toString();
 859     }
 860 }
 861 
 862 /**
 863  * Each entry in the policy configuration file is represented by a
 864  * PolicyEntry object.
 865  *
 866  * A PolicyEntry is a (CodeSource,Permission) pair.  The
 867  * CodeSource contains the (URL, PublicKey) that together identify
 868  * where the Java bytecodes come from and who (if anyone) signed
 869  * them.  The URL could refer to localhost.  The URL could also be
 870  * null, meaning that this policy entry is given to all comers, as
 871  * long as they match the signer field.  The signer could be null,
 872  * meaning the code is not signed.
 873  *
 874  * The Permission contains the (Type, Name, Action) triplet.
 875  *
 876  */
 877 class PolicyEntry {
 878 
 879     private CodeSource codesource;
 880     private PolicyTool tool;
 881     private PolicyParser.GrantEntry grantEntry;
 882     private boolean testing = false;
 883 
 884     /**
 885      * Create a PolicyEntry object from the information read in
 886      * from a policy file.
 887      */
 888     PolicyEntry(PolicyTool tool, PolicyParser.GrantEntry ge)
 889     throws MalformedURLException, NoSuchMethodException,
 890     ClassNotFoundException, InstantiationException, IllegalAccessException,
 891     InvocationTargetException, CertificateException,
 892     IOException, NoSuchAlgorithmException, UnrecoverableKeyException {
 893 
 894         this.tool = tool;
 895 
 896         URL location = null;
 897 
 898         // construct the CodeSource
 899         if (ge.codeBase != null)
 900             location = new URL(ge.codeBase);
 901         this.codesource = new CodeSource(location,
 902             (java.security.cert.Certificate[]) null);
 903 
 904         if (testing) {
 905             System.out.println("Adding Policy Entry:");
 906             System.out.println("    CodeBase = " + location);
 907             System.out.println("    Signers = " + ge.signedBy);
 908             System.out.println("    with " + ge.principals.size() +
 909                     " Principals");
 910         }
 911 
 912         this.grantEntry = ge;
 913     }
 914 
 915     /**
 916      * get the codesource associated with this PolicyEntry
 917      */
 918     CodeSource getCodeSource() {
 919         return codesource;
 920     }
 921 
 922     /**
 923      * get the GrantEntry associated with this PolicyEntry
 924      */
 925     PolicyParser.GrantEntry getGrantEntry() {
 926         return grantEntry;
 927     }
 928 
 929     /**
 930      * convert the header portion, i.e. codebase, signer, principals, of
 931      * this policy entry into a string
 932      */
 933     String headerToString() {
 934         String pString = principalsToString();
 935         if (pString.length() == 0) {
 936             return codebaseToString();
 937         } else {
 938             return codebaseToString() + ", " + pString;
 939         }
 940     }
 941 
 942     /**
 943      * convert the Codebase/signer portion of this policy entry into a string
 944      */
 945     String codebaseToString() {
 946 
 947         String stringEntry = new String();
 948 
 949         if (grantEntry.codeBase != null &&
 950             grantEntry.codeBase.equals("") == false)
 951             stringEntry = stringEntry.concat
 952                                 ("CodeBase \"" +
 953                                 grantEntry.codeBase +
 954                                 "\"");
 955 
 956         if (grantEntry.signedBy != null &&
 957             grantEntry.signedBy.equals("") == false)
 958             stringEntry = ((stringEntry.length() > 0) ?
 959                 stringEntry.concat(", SignedBy \"" +
 960                                 grantEntry.signedBy +
 961                                 "\"") :
 962                 stringEntry.concat("SignedBy \"" +
 963                                 grantEntry.signedBy +
 964                                 "\""));
 965 
 966         if (stringEntry.length() == 0)
 967             return new String("CodeBase <ALL>");
 968         return stringEntry;
 969     }
 970 
 971     /**
 972      * convert the Principals portion of this policy entry into a string
 973      */
 974     String principalsToString() {
 975         String result = "";
 976         if ((grantEntry.principals != null) &&
 977             (!grantEntry.principals.isEmpty())) {
 978             StringBuilder sb = new StringBuilder(200);
 979             ListIterator<PolicyParser.PrincipalEntry> list =
 980                                 grantEntry.principals.listIterator();
 981             while (list.hasNext()) {
 982                 PolicyParser.PrincipalEntry pppe = list.next();
 983                 sb.append(" Principal ").append(pppe.getDisplayClass())
 984                         .append(' ')
 985                         .append(pppe.getDisplayName(true));
 986                 if (list.hasNext()) sb.append(", ");
 987             }
 988             result = sb.toString();
 989         }
 990         return result;
 991     }
 992 
 993     /**
 994      * convert this policy entry into a PolicyParser.PermissionEntry
 995      */
 996     PolicyParser.PermissionEntry toPermissionEntry(Permission perm) {
 997 
 998         String actions = null;
 999 
1000         // get the actions
1001         if (perm.getActions() != null &&
1002             perm.getActions().trim() != "")
1003                 actions = perm.getActions();
1004 
1005         PolicyParser.PermissionEntry pe = new PolicyParser.PermissionEntry
1006                         (perm.getClass().getName(),
1007                         perm.getName(),
1008                         actions);
1009         return pe;
1010     }
1011 }
1012 
1013 /**
1014  * The main window for the PolicyTool
1015  */
1016 class ToolWindow extends JFrame {
1017     // use serialVersionUID from JDK 1.2.2 for interoperability
1018     private static final long serialVersionUID = 5682568601210376777L;
1019 
1020     /* ESCAPE key */
1021     static final KeyStroke escKey = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
1022 
1023     /* external paddings */
1024     public static final Insets TOP_PADDING = new Insets(25,0,0,0);
1025     public static final Insets BOTTOM_PADDING = new Insets(0,0,25,0);
1026     public static final Insets LITE_BOTTOM_PADDING = new Insets(0,0,10,0);
1027     public static final Insets LR_PADDING = new Insets(0,10,0,10);
1028     public static final Insets TOP_BOTTOM_PADDING = new Insets(15, 0, 15, 0);
1029     public static final Insets L_TOP_BOTTOM_PADDING = new Insets(5,10,15,0);
1030     public static final Insets LR_TOP_BOTTOM_PADDING = new Insets(15, 4, 15, 4);
1031     public static final Insets LR_BOTTOM_PADDING = new Insets(0,10,5,10);
1032     public static final Insets L_BOTTOM_PADDING = new Insets(0,10,5,0);
1033     public static final Insets R_BOTTOM_PADDING = new Insets(0, 0, 25, 5);
1034     public static final Insets R_PADDING = new Insets(0, 0, 0, 5);
1035 
1036     /* buttons and menus */
1037     public static final String NEW_POLICY_FILE          = "New";
1038     public static final String OPEN_POLICY_FILE         = "Open";
1039     public static final String SAVE_POLICY_FILE         = "Save";
1040     public static final String SAVE_AS_POLICY_FILE      = "Save.As";
1041     public static final String VIEW_WARNINGS            = "View.Warning.Log";
1042     public static final String QUIT                     = "Exit";
1043     public static final String ADD_POLICY_ENTRY         = "Add.Policy.Entry";
1044     public static final String EDIT_POLICY_ENTRY        = "Edit.Policy.Entry";
1045     public static final String REMOVE_POLICY_ENTRY      = "Remove.Policy.Entry";
1046     public static final String EDIT_KEYSTORE            = "Edit";
1047     public static final String ADD_PUBKEY_ALIAS         = "Add.Public.Key.Alias";
1048     public static final String REMOVE_PUBKEY_ALIAS      = "Remove.Public.Key.Alias";
1049 
1050     /* gridbag index for components in the main window (MW) */
1051     public static final int MW_FILENAME_LABEL           = 0;
1052     public static final int MW_FILENAME_TEXTFIELD       = 1;
1053     public static final int MW_PANEL                    = 2;
1054     public static final int MW_ADD_BUTTON               = 0;
1055     public static final int MW_EDIT_BUTTON              = 1;
1056     public static final int MW_REMOVE_BUTTON            = 2;
1057     public static final int MW_POLICY_LIST              = 3; // follows MW_PANEL
1058 
1059     /* The preferred height of JTextField should match JComboBox. */
1060     static final int TEXTFIELD_HEIGHT = new JComboBox<>().getPreferredSize().height;
1061 
1062     private PolicyTool tool;
1063 
1064     /**
1065      * Constructor
1066      */
1067     ToolWindow(PolicyTool tool) {
1068         this.tool = tool;
1069     }
1070 
1071     /**
1072      * Don't call getComponent directly on the window
1073      */
1074     public Component getComponent(int n) {
1075         Component c = getContentPane().getComponent(n);
1076         if (c instanceof JScrollPane) {
1077             c = ((JScrollPane)c).getViewport().getView();
1078         }
1079         return c;
1080     }
1081 
1082     /**
1083      * Initialize the PolicyTool window with the necessary components
1084      */
1085     private void initWindow() {
1086         // The ToolWindowListener will handle closing the window.
1087         setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
1088 
1089         // create the top menu bar
1090         JMenuBar menuBar = new JMenuBar();
1091 
1092         // create a File menu
1093         JMenu menu = new JMenu();
1094         configureButton(menu, "File");
1095         ActionListener actionListener = new FileMenuListener(tool, this);
1096         addMenuItem(menu, NEW_POLICY_FILE, actionListener, "N");
1097         addMenuItem(menu, OPEN_POLICY_FILE, actionListener, "O");
1098         addMenuItem(menu, SAVE_POLICY_FILE, actionListener, "S");
1099         addMenuItem(menu, SAVE_AS_POLICY_FILE, actionListener, null);
1100         addMenuItem(menu, VIEW_WARNINGS, actionListener, null);
1101         addMenuItem(menu, QUIT, actionListener, null);
1102         menuBar.add(menu);
1103 
1104         // create a KeyStore menu
1105         menu = new JMenu();
1106         configureButton(menu, "KeyStore");
1107         actionListener = new MainWindowListener(tool, this);
1108         addMenuItem(menu, EDIT_KEYSTORE, actionListener, null);
1109         menuBar.add(menu);
1110         setJMenuBar(menuBar);
1111 
1112         // Create some space around components
1113         ((JPanel)getContentPane()).setBorder(new EmptyBorder(6, 6, 6, 6));
1114 
1115         // policy entry listing
1116         JLabel label = new JLabel(PolicyTool.getMessage("Policy.File."));
1117         addNewComponent(this, label, MW_FILENAME_LABEL,
1118                         0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
1119                         LR_TOP_BOTTOM_PADDING);
1120         JTextField tf = new JTextField(50);
1121         tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
1122         tf.getAccessibleContext().setAccessibleName(
1123                 PolicyTool.getMessage("Policy.File."));
1124         tf.setEditable(false);
1125         addNewComponent(this, tf, MW_FILENAME_TEXTFIELD,
1126                         1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
1127                         LR_TOP_BOTTOM_PADDING);
1128 
1129 
1130         // add ADD/REMOVE/EDIT buttons in a new panel
1131         JPanel panel = new JPanel();
1132         panel.setLayout(new GridBagLayout());
1133 
1134         JButton button = new JButton();
1135         configureButton(button, ADD_POLICY_ENTRY);
1136         button.addActionListener(new MainWindowListener(tool, this));
1137         addNewComponent(panel, button, MW_ADD_BUTTON,
1138                         0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
1139                         LR_PADDING);
1140 
1141         button = new JButton();
1142         configureButton(button, EDIT_POLICY_ENTRY);
1143         button.addActionListener(new MainWindowListener(tool, this));
1144         addNewComponent(panel, button, MW_EDIT_BUTTON,
1145                         1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
1146                         LR_PADDING);
1147 
1148         button = new JButton();
1149         configureButton(button, REMOVE_POLICY_ENTRY);
1150         button.addActionListener(new MainWindowListener(tool, this));
1151         addNewComponent(panel, button, MW_REMOVE_BUTTON,
1152                         2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
1153                         LR_PADDING);
1154 
1155         addNewComponent(this, panel, MW_PANEL,
1156                         0, 2, 2, 1, 0.0, 0.0, GridBagConstraints.BOTH,
1157                         BOTTOM_PADDING);
1158 
1159 
1160         String policyFile = tool.getPolicyFileName();
1161         if (policyFile == null) {
1162             String userHome;
1163             userHome = java.security.AccessController.doPrivileged(
1164                 (PrivilegedAction<String>) () -> System.getProperty("user.home"));
1165             policyFile = userHome + File.separatorChar + ".java.policy";
1166         }
1167 
1168         try {
1169             // open the policy file
1170             tool.openPolicy(policyFile);
1171 
1172             // display the policy entries via the policy list textarea
1173             DefaultListModel<String> listModel = new DefaultListModel<>();
1174             JList<String> list = new JList<>(listModel);
1175             list.setVisibleRowCount(15);
1176             list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
1177             list.addMouseListener(new PolicyListListener(tool, this));
1178             PolicyEntry entries[] = tool.getEntry();
1179             if (entries != null) {
1180                 for (int i = 0; i < entries.length; i++) {
1181                     listModel.addElement(entries[i].headerToString());
1182                 }
1183             }
1184             JTextField newFilename = (JTextField)
1185                                 getComponent(MW_FILENAME_TEXTFIELD);
1186             newFilename.setText(policyFile);
1187             initPolicyList(list);
1188 
1189         } catch (FileNotFoundException fnfe) {
1190             // add blank policy listing
1191             JList<String> list = new JList<>(new DefaultListModel<>());
1192             list.setVisibleRowCount(15);
1193             list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
1194             list.addMouseListener(new PolicyListListener(tool, this));
1195             initPolicyList(list);
1196             tool.setPolicyFileName(null);
1197             tool.modified = false;
1198 
1199             // just add warning
1200             tool.warnings.addElement(fnfe.toString());
1201 
1202         } catch (Exception e) {
1203             // add blank policy listing
1204             JList<String> list = new JList<>(new DefaultListModel<>());
1205             list.setVisibleRowCount(15);
1206             list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
1207             list.addMouseListener(new PolicyListListener(tool, this));
1208             initPolicyList(list);
1209             tool.setPolicyFileName(null);
1210             tool.modified = false;
1211 
1212             // display the error
1213             MessageFormat form = new MessageFormat(PolicyTool.getMessage
1214                 ("Could.not.open.policy.file.policyFile.e.toString."));
1215             Object[] source = {policyFile, e.toString()};
1216             displayErrorDialog(null, form.format(source));
1217         }
1218     }
1219 
1220 
1221     // Platform specific modifier (control / command).
1222     private int shortCutModifier = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
1223 
1224     private void addMenuItem(JMenu menu, String key, ActionListener actionListener, String accelerator) {
1225         JMenuItem menuItem = new JMenuItem();
1226         configureButton(menuItem, key);
1227 
1228         if (PolicyTool.rb.containsKey(key + ".accelerator")) {
1229             // Accelerator from resources takes precedence
1230             accelerator = PolicyTool.getMessage(key + ".accelerator");
1231         }
1232 
1233         if (accelerator != null && !accelerator.isEmpty()) {
1234             KeyStroke keyStroke;
1235             if (accelerator.length() == 1) {
1236                 keyStroke = KeyStroke.getKeyStroke(KeyEvent.getExtendedKeyCodeForChar(accelerator.charAt(0)),
1237                                                    shortCutModifier);
1238             } else {
1239                 keyStroke = KeyStroke.getKeyStroke(accelerator);
1240             }
1241             menuItem.setAccelerator(keyStroke);
1242         }
1243 
1244         menuItem.addActionListener(actionListener);
1245         menu.add(menuItem);
1246     }
1247 
1248     static void configureButton(AbstractButton button, String key) {
1249         button.setText(PolicyTool.getMessage(key));
1250         button.setActionCommand(key);
1251 
1252         int mnemonicInt = PolicyTool.getMnemonicInt(key);
1253         if (mnemonicInt > 0) {
1254             button.setMnemonic(mnemonicInt);
1255             button.setDisplayedMnemonicIndex(PolicyTool.getDisplayedMnemonicIndex(key));
1256          }
1257     }
1258 
1259     static void configureLabelFor(JLabel label, JComponent component, String key) {
1260         label.setText(PolicyTool.getMessage(key));
1261         label.setLabelFor(component);
1262 
1263         int mnemonicInt = PolicyTool.getMnemonicInt(key);
1264         if (mnemonicInt > 0) {
1265             label.setDisplayedMnemonic(mnemonicInt);
1266             label.setDisplayedMnemonicIndex(PolicyTool.getDisplayedMnemonicIndex(key));
1267          }
1268     }
1269 
1270 
1271     /**
1272      * Add a component to the PolicyTool window
1273      */
1274     void addNewComponent(Container container, JComponent component,
1275         int index, int gridx, int gridy, int gridwidth, int gridheight,
1276         double weightx, double weighty, int fill, Insets is) {
1277 
1278         if (container instanceof JFrame) {
1279             container = ((JFrame)container).getContentPane();
1280         } else if (container instanceof JDialog) {
1281             container = ((JDialog)container).getContentPane();
1282         }
1283 
1284         // add the component at the specified gridbag index
1285         container.add(component, index);
1286 
1287         // set the constraints
1288         GridBagLayout gbl = (GridBagLayout)container.getLayout();
1289         GridBagConstraints gbc = new GridBagConstraints();
1290         gbc.gridx = gridx;
1291         gbc.gridy = gridy;
1292         gbc.gridwidth = gridwidth;
1293         gbc.gridheight = gridheight;
1294         gbc.weightx = weightx;
1295         gbc.weighty = weighty;
1296         gbc.fill = fill;
1297         if (is != null) gbc.insets = is;
1298         gbl.setConstraints(component, gbc);
1299     }
1300 
1301 
1302     /**
1303      * Add a component to the PolicyTool window without external padding
1304      */
1305     void addNewComponent(Container container, JComponent component,
1306         int index, int gridx, int gridy, int gridwidth, int gridheight,
1307         double weightx, double weighty, int fill) {
1308 
1309         // delegate with "null" external padding
1310         addNewComponent(container, component, index, gridx, gridy,
1311                         gridwidth, gridheight, weightx, weighty,
1312                         fill, null);
1313     }
1314 
1315 
1316     /**
1317      * Init the policy_entry_list TEXTAREA component in the
1318      * PolicyTool window
1319      */
1320     void initPolicyList(JList<String> policyList) {
1321 
1322         // add the policy list to the window
1323         //policyList.setPreferredSize(new Dimension(500, 350));
1324         JScrollPane scrollPane = new JScrollPane(policyList);
1325         addNewComponent(this, scrollPane, MW_POLICY_LIST,
1326                         0, 3, 2, 1, 1.0, 1.0, GridBagConstraints.BOTH);
1327     }
1328 
1329     /**
1330      * Replace the policy_entry_list TEXTAREA component in the
1331      * PolicyTool window with an updated one.
1332      */
1333     void replacePolicyList(JList<String> policyList) {
1334 
1335         // remove the original list of Policy Entries
1336         // and add the new list of entries
1337         @SuppressWarnings("unchecked")
1338         JList<String> list = (JList<String>)getComponent(MW_POLICY_LIST);
1339         list.setModel(policyList.getModel());
1340     }
1341 
1342     /**
1343      * display the main PolicyTool window
1344      */
1345     void displayToolWindow(String args[]) {
1346 
1347         setTitle(PolicyTool.getMessage("Policy.Tool"));
1348         setResizable(true);
1349         addWindowListener(new ToolWindowListener(tool, this));
1350         //setBounds(135, 80, 500, 500);
1351         getContentPane().setLayout(new GridBagLayout());
1352 
1353         initWindow();
1354         pack();
1355         setLocationRelativeTo(null);
1356 
1357         // display it
1358         setVisible(true);
1359 
1360         if (tool.newWarning == true) {
1361             displayStatusDialog(this, PolicyTool.getMessage
1362                 ("Errors.have.occurred.while.opening.the.policy.configuration.View.the.Warning.Log.for.more.information."));
1363         }
1364     }
1365 
1366     /**
1367      * displays a dialog box describing an error which occurred.
1368      */
1369     void displayErrorDialog(Window w, String error) {
1370         ToolDialog ed = new ToolDialog
1371                 (PolicyTool.getMessage("Error"), tool, this, true);
1372 
1373         // find where the PolicyTool gui is
1374         Point location = ((w == null) ?
1375                 getLocationOnScreen() : w.getLocationOnScreen());
1376         //ed.setBounds(location.x + 50, location.y + 50, 600, 100);
1377         ed.setLayout(new GridBagLayout());
1378 
1379         JLabel label = new JLabel(error);
1380         addNewComponent(ed, label, 0,
1381                         0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH);
1382 
1383         JButton okButton = new JButton(PolicyTool.getMessage("OK"));
1384         ActionListener okListener = new ErrorOKButtonListener(ed);
1385         okButton.addActionListener(okListener);
1386         addNewComponent(ed, okButton, 1,
1387                         0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL);
1388 
1389         ed.getRootPane().setDefaultButton(okButton);
1390         ed.getRootPane().registerKeyboardAction(okListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
1391 
1392         ed.pack();
1393         ed.setLocationRelativeTo(w);
1394         ed.setVisible(true);
1395     }
1396 
1397     /**
1398      * displays a dialog box describing an error which occurred.
1399      */
1400     void displayErrorDialog(Window w, Throwable t) {
1401         if (t instanceof NoDisplayException) {
1402             return;
1403         }
1404         displayErrorDialog(w, t.toString());
1405     }
1406 
1407     /**
1408      * displays a dialog box describing the status of an event
1409      */
1410     void displayStatusDialog(Window w, String status) {
1411         ToolDialog sd = new ToolDialog
1412                 (PolicyTool.getMessage("Status"), tool, this, true);
1413 
1414         // find the location of the PolicyTool gui
1415         Point location = ((w == null) ?
1416                 getLocationOnScreen() : w.getLocationOnScreen());
1417         //sd.setBounds(location.x + 50, location.y + 50, 500, 100);
1418         sd.setLayout(new GridBagLayout());
1419 
1420         JLabel label = new JLabel(status);
1421         addNewComponent(sd, label, 0,
1422                         0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH);
1423 
1424         JButton okButton = new JButton(PolicyTool.getMessage("OK"));
1425         ActionListener okListener = new StatusOKButtonListener(sd);
1426         okButton.addActionListener(okListener);
1427         addNewComponent(sd, okButton, 1,
1428                         0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL);
1429 
1430         sd.getRootPane().setDefaultButton(okButton);
1431         sd.getRootPane().registerKeyboardAction(okListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
1432 
1433         sd.pack();
1434         sd.setLocationRelativeTo(w);
1435         sd.setVisible(true);
1436     }
1437 
1438     /**
1439      * display the warning log
1440      */
1441     void displayWarningLog(Window w) {
1442 
1443         ToolDialog wd = new ToolDialog
1444                 (PolicyTool.getMessage("Warning"), tool, this, true);
1445 
1446         // find the location of the PolicyTool gui
1447         Point location = ((w == null) ?
1448                 getLocationOnScreen() : w.getLocationOnScreen());
1449         //wd.setBounds(location.x + 50, location.y + 50, 500, 100);
1450         wd.setLayout(new GridBagLayout());
1451 
1452         JTextArea ta = new JTextArea();
1453         ta.setEditable(false);
1454         for (int i = 0; i < tool.warnings.size(); i++) {
1455             ta.append(tool.warnings.elementAt(i));
1456             ta.append(PolicyTool.getMessage("NEWLINE"));
1457         }
1458         addNewComponent(wd, ta, 0,
1459                         0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
1460                         BOTTOM_PADDING);
1461         ta.setFocusable(false);
1462 
1463         JButton okButton = new JButton(PolicyTool.getMessage("OK"));
1464         ActionListener okListener = new CancelButtonListener(wd);
1465         okButton.addActionListener(okListener);
1466         addNewComponent(wd, okButton, 1,
1467                         0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
1468                         LR_PADDING);
1469 
1470         wd.getRootPane().setDefaultButton(okButton);
1471         wd.getRootPane().registerKeyboardAction(okListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
1472 
1473         wd.pack();
1474         wd.setLocationRelativeTo(w);
1475         wd.setVisible(true);
1476     }
1477 
1478     char displayYesNoDialog(Window w, String title, String prompt, String yes, String no) {
1479 
1480         final ToolDialog tw = new ToolDialog
1481                 (title, tool, this, true);
1482         Point location = ((w == null) ?
1483                 getLocationOnScreen() : w.getLocationOnScreen());
1484         //tw.setBounds(location.x + 75, location.y + 100, 400, 150);
1485         tw.setLayout(new GridBagLayout());
1486 
1487         JTextArea ta = new JTextArea(prompt, 10, 50);
1488         ta.setEditable(false);
1489         ta.setLineWrap(true);
1490         ta.setWrapStyleWord(true);
1491         JScrollPane scrollPane = new JScrollPane(ta,
1492                                                  JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
1493                                                  JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
1494         addNewComponent(tw, scrollPane, 0,
1495                 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH);
1496         ta.setFocusable(false);
1497 
1498         JPanel panel = new JPanel();
1499         panel.setLayout(new GridBagLayout());
1500 
1501         // StringBuffer to store button press. Must be final.
1502         final StringBuffer chooseResult = new StringBuffer();
1503 
1504         JButton button = new JButton(yes);
1505         button.addActionListener(new ActionListener() {
1506             public void actionPerformed(ActionEvent e) {
1507                 chooseResult.append('Y');
1508                 tw.setVisible(false);
1509                 tw.dispose();
1510             }
1511         });
1512         addNewComponent(panel, button, 0,
1513                            0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
1514                            LR_PADDING);
1515 
1516         button = new JButton(no);
1517         button.addActionListener(new ActionListener() {
1518             public void actionPerformed(ActionEvent e) {
1519                 chooseResult.append('N');
1520                 tw.setVisible(false);
1521                 tw.dispose();
1522             }
1523         });
1524         addNewComponent(panel, button, 1,
1525                            1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
1526                            LR_PADDING);
1527 
1528         addNewComponent(tw, panel, 1,
1529                 0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL);
1530 
1531         tw.pack();
1532         tw.setLocationRelativeTo(w);
1533         tw.setVisible(true);
1534         if (chooseResult.length() > 0) {
1535             return chooseResult.charAt(0);
1536         } else {
1537             // I did encounter this once, don't why.
1538             return 'N';
1539         }
1540     }
1541 
1542 }
1543 
1544 /**
1545  * General dialog window
1546  */
1547 class ToolDialog extends JDialog {
1548     // use serialVersionUID from JDK 1.2.2 for interoperability
1549     private static final long serialVersionUID = -372244357011301190L;
1550 
1551     /* ESCAPE key */
1552     static final KeyStroke escKey = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
1553 
1554     /* necessary constants */
1555     public static final int NOACTION            = 0;
1556     public static final int QUIT                = 1;
1557     public static final int NEW                 = 2;
1558     public static final int OPEN                = 3;
1559 
1560     public static final String ALL_PERM_CLASS   =
1561                 "java.security.AllPermission";
1562     public static final String FILE_PERM_CLASS  =
1563                 "java.io.FilePermission";
1564 
1565     public static final String X500_PRIN_CLASS         =
1566                 "javax.security.auth.x500.X500Principal";
1567 
1568     /* popup menus */
1569     public static final String PERM             =
1570         PolicyTool.getMessage
1571         ("Permission.");
1572 
1573     public static final String PRIN_TYPE        =
1574         PolicyTool.getMessage("Principal.Type.");
1575     public static final String PRIN_NAME        =
1576         PolicyTool.getMessage("Principal.Name.");
1577 
1578     /* more popu menus */
1579     public static final String PERM_NAME        =
1580         PolicyTool.getMessage
1581         ("Target.Name.");
1582 
1583     /* and more popup menus */
1584     public static final String PERM_ACTIONS             =
1585       PolicyTool.getMessage
1586       ("Actions.");
1587 
1588     /* gridbag index for display PolicyEntry (PE) components */
1589     public static final int PE_CODEBASE_LABEL           = 0;
1590     public static final int PE_CODEBASE_TEXTFIELD       = 1;
1591     public static final int PE_SIGNEDBY_LABEL           = 2;
1592     public static final int PE_SIGNEDBY_TEXTFIELD       = 3;
1593 
1594     public static final int PE_PANEL0                   = 4;
1595     public static final int PE_ADD_PRIN_BUTTON          = 0;
1596     public static final int PE_EDIT_PRIN_BUTTON         = 1;
1597     public static final int PE_REMOVE_PRIN_BUTTON       = 2;
1598 
1599     public static final int PE_PRIN_LABEL               = 5;
1600     public static final int PE_PRIN_LIST                = 6;
1601 
1602     public static final int PE_PANEL1                   = 7;
1603     public static final int PE_ADD_PERM_BUTTON          = 0;
1604     public static final int PE_EDIT_PERM_BUTTON         = 1;
1605     public static final int PE_REMOVE_PERM_BUTTON       = 2;
1606 
1607     public static final int PE_PERM_LIST                = 8;
1608 
1609     public static final int PE_PANEL2                   = 9;
1610     public static final int PE_CANCEL_BUTTON            = 1;
1611     public static final int PE_DONE_BUTTON              = 0;
1612 
1613     /* the gridbag index for components in the Principal Dialog (PRD) */
1614     public static final int PRD_DESC_LABEL              = 0;
1615     public static final int PRD_PRIN_CHOICE             = 1;
1616     public static final int PRD_PRIN_TEXTFIELD          = 2;
1617     public static final int PRD_NAME_LABEL              = 3;
1618     public static final int PRD_NAME_TEXTFIELD          = 4;
1619     public static final int PRD_CANCEL_BUTTON           = 6;
1620     public static final int PRD_OK_BUTTON               = 5;
1621 
1622     /* the gridbag index for components in the Permission Dialog (PD) */
1623     public static final int PD_DESC_LABEL               = 0;
1624     public static final int PD_PERM_CHOICE              = 1;
1625     public static final int PD_PERM_TEXTFIELD           = 2;
1626     public static final int PD_NAME_CHOICE              = 3;
1627     public static final int PD_NAME_TEXTFIELD           = 4;
1628     public static final int PD_ACTIONS_CHOICE           = 5;
1629     public static final int PD_ACTIONS_TEXTFIELD        = 6;
1630     public static final int PD_SIGNEDBY_LABEL           = 7;
1631     public static final int PD_SIGNEDBY_TEXTFIELD       = 8;
1632     public static final int PD_CANCEL_BUTTON            = 10;
1633     public static final int PD_OK_BUTTON                = 9;
1634 
1635     /* modes for KeyStore */
1636     public static final int EDIT_KEYSTORE               = 0;
1637 
1638     /* the gridbag index for components in the Change KeyStore Dialog (KSD) */
1639     public static final int KSD_NAME_LABEL              = 0;
1640     public static final int KSD_NAME_TEXTFIELD          = 1;
1641     public static final int KSD_TYPE_LABEL              = 2;
1642     public static final int KSD_TYPE_TEXTFIELD          = 3;
1643     public static final int KSD_PROVIDER_LABEL          = 4;
1644     public static final int KSD_PROVIDER_TEXTFIELD      = 5;
1645     public static final int KSD_PWD_URL_LABEL           = 6;
1646     public static final int KSD_PWD_URL_TEXTFIELD       = 7;
1647     public static final int KSD_CANCEL_BUTTON           = 9;
1648     public static final int KSD_OK_BUTTON               = 8;
1649 
1650     /* the gridbag index for components in the User Save Changes Dialog (USC) */
1651     public static final int USC_LABEL                   = 0;
1652     public static final int USC_PANEL                   = 1;
1653     public static final int USC_YES_BUTTON              = 0;
1654     public static final int USC_NO_BUTTON               = 1;
1655     public static final int USC_CANCEL_BUTTON           = 2;
1656 
1657     /* gridbag index for the ConfirmRemovePolicyEntryDialog (CRPE) */
1658     public static final int CRPE_LABEL1                 = 0;
1659     public static final int CRPE_LABEL2                 = 1;
1660     public static final int CRPE_PANEL                  = 2;
1661     public static final int CRPE_PANEL_OK               = 0;
1662     public static final int CRPE_PANEL_CANCEL           = 1;
1663 
1664     /* some private static finals */
1665     private static final int PERMISSION                 = 0;
1666     private static final int PERMISSION_NAME            = 1;
1667     private static final int PERMISSION_ACTIONS         = 2;
1668     private static final int PERMISSION_SIGNEDBY        = 3;
1669     private static final int PRINCIPAL_TYPE             = 4;
1670     private static final int PRINCIPAL_NAME             = 5;
1671 
1672     /* The preferred height of JTextField should match JComboBox. */
1673     static final int TEXTFIELD_HEIGHT = new JComboBox<>().getPreferredSize().height;
1674 
1675     public static java.util.ArrayList<Perm> PERM_ARRAY;
1676     public static java.util.ArrayList<Prin> PRIN_ARRAY;
1677     PolicyTool tool;
1678     ToolWindow tw;
1679 
1680     static {
1681 
1682         // set up permission objects
1683 
1684         PERM_ARRAY = new java.util.ArrayList<Perm>();
1685         PERM_ARRAY.add(new AllPerm());
1686         PERM_ARRAY.add(new AudioPerm());
1687         PERM_ARRAY.add(new AuthPerm());
1688         PERM_ARRAY.add(new AWTPerm());
1689         PERM_ARRAY.add(new DelegationPerm());
1690         PERM_ARRAY.add(new FilePerm());
1691         PERM_ARRAY.add(new URLPerm());
1692         PERM_ARRAY.add(new InqSecContextPerm());
1693         PERM_ARRAY.add(new LogPerm());
1694         PERM_ARRAY.add(new MgmtPerm());
1695         PERM_ARRAY.add(new MBeanPerm());
1696         PERM_ARRAY.add(new MBeanSvrPerm());
1697         PERM_ARRAY.add(new MBeanTrustPerm());
1698         PERM_ARRAY.add(new NetPerm());
1699         PERM_ARRAY.add(new PrivCredPerm());
1700         PERM_ARRAY.add(new PropPerm());
1701         PERM_ARRAY.add(new ReflectPerm());
1702         PERM_ARRAY.add(new RuntimePerm());
1703         PERM_ARRAY.add(new SecurityPerm());
1704         PERM_ARRAY.add(new SerialPerm());
1705         PERM_ARRAY.add(new ServicePerm());
1706         PERM_ARRAY.add(new SocketPerm());
1707         PERM_ARRAY.add(new SQLPerm());
1708         PERM_ARRAY.add(new SSLPerm());
1709         PERM_ARRAY.add(new SubjDelegPerm());
1710 
1711         // set up principal objects
1712 
1713         PRIN_ARRAY = new java.util.ArrayList<Prin>();
1714         PRIN_ARRAY.add(new KrbPrin());
1715         PRIN_ARRAY.add(new X500Prin());
1716     }
1717 
1718     ToolDialog(String title, PolicyTool tool, ToolWindow tw, boolean modal) {
1719         super(tw, modal);
1720         setTitle(title);
1721         this.tool = tool;
1722         this.tw = tw;
1723         addWindowListener(new ChildWindowListener(this));
1724 
1725         // Create some space around components
1726         ((JPanel)getContentPane()).setBorder(new EmptyBorder(6, 6, 6, 6));
1727     }
1728 
1729     /**
1730      * Don't call getComponent directly on the window
1731      */
1732     public Component getComponent(int n) {
1733         Component c = getContentPane().getComponent(n);
1734         if (c instanceof JScrollPane) {
1735             c = ((JScrollPane)c).getViewport().getView();
1736         }
1737         return c;
1738     }
1739 
1740     /**
1741      * get the Perm instance based on either the (shortened) class name
1742      * or the fully qualified class name
1743      */
1744     static Perm getPerm(String clazz, boolean fullClassName) {
1745         for (int i = 0; i < PERM_ARRAY.size(); i++) {
1746             Perm next = PERM_ARRAY.get(i);
1747             if (fullClassName) {
1748                 if (next.FULL_CLASS.equals(clazz)) {
1749                     return next;
1750                 }
1751             } else {
1752                 if (next.CLASS.equals(clazz)) {
1753                     return next;
1754                 }
1755             }
1756         }
1757         return null;
1758     }
1759 
1760     /**
1761      * get the Prin instance based on either the (shortened) class name
1762      * or the fully qualified class name
1763      */
1764     static Prin getPrin(String clazz, boolean fullClassName) {
1765         for (int i = 0; i < PRIN_ARRAY.size(); i++) {
1766             Prin next = PRIN_ARRAY.get(i);
1767             if (fullClassName) {
1768                 if (next.FULL_CLASS.equals(clazz)) {
1769                     return next;
1770                 }
1771             } else {
1772                 if (next.CLASS.equals(clazz)) {
1773                     return next;
1774                 }
1775             }
1776         }
1777         return null;
1778     }
1779 
1780     /**
1781      * pop up a dialog so the user can enter info to add a new PolicyEntry
1782      * - if edit is TRUE, then the user is editing an existing entry
1783      *   and we should display the original info as well.
1784      *
1785      * - the other reason we need the 'edit' boolean is we need to know
1786      *   when we are adding a NEW policy entry.  in this case, we can
1787      *   not simply update the existing entry, because it doesn't exist.
1788      *   we ONLY update the GUI listing/info, and then when the user
1789      *   finally clicks 'OK' or 'DONE', then we can collect that info
1790      *   and add it to the policy.
1791      */
1792     void displayPolicyEntryDialog(boolean edit) {
1793 
1794         int listIndex = 0;
1795         PolicyEntry entries[] = null;
1796         TaggedList prinList = new TaggedList(3, false);
1797         prinList.getAccessibleContext().setAccessibleName(
1798                 PolicyTool.getMessage("Principal.List"));
1799         prinList.addMouseListener
1800                 (new EditPrinButtonListener(tool, tw, this, edit));
1801         TaggedList permList = new TaggedList(10, false);
1802         permList.getAccessibleContext().setAccessibleName(
1803                 PolicyTool.getMessage("Permission.List"));
1804         permList.addMouseListener
1805                 (new EditPermButtonListener(tool, tw, this, edit));
1806 
1807         // find where the PolicyTool gui is
1808         Point location = tw.getLocationOnScreen();
1809         //setBounds(location.x + 75, location.y + 200, 650, 500);
1810         setLayout(new GridBagLayout());
1811         setResizable(true);
1812 
1813         if (edit) {
1814             // get the selected item
1815             entries = tool.getEntry();
1816             @SuppressWarnings("unchecked")
1817             JList<String> policyList = (JList<String>)tw.getComponent(ToolWindow.MW_POLICY_LIST);
1818             listIndex = policyList.getSelectedIndex();
1819 
1820             // get principal list
1821             LinkedList<PolicyParser.PrincipalEntry> principals =
1822                 entries[listIndex].getGrantEntry().principals;
1823             for (int i = 0; i < principals.size(); i++) {
1824                 String prinString = null;
1825                 PolicyParser.PrincipalEntry nextPrin = principals.get(i);
1826                 prinList.addTaggedItem(PrincipalEntryToUserFriendlyString(nextPrin), nextPrin);
1827             }
1828 
1829             // get permission list
1830             Vector<PolicyParser.PermissionEntry> permissions =
1831                 entries[listIndex].getGrantEntry().permissionEntries;
1832             for (int i = 0; i < permissions.size(); i++) {
1833                 String permString = null;
1834                 PolicyParser.PermissionEntry nextPerm =
1835                                                 permissions.elementAt(i);
1836                 permList.addTaggedItem(ToolDialog.PermissionEntryToUserFriendlyString(nextPerm), nextPerm);
1837             }
1838         }
1839 
1840         // codebase label and textfield
1841         JLabel label = new JLabel();
1842         tw.addNewComponent(this, label, PE_CODEBASE_LABEL,
1843                 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
1844                 ToolWindow.R_PADDING);
1845         JTextField tf;
1846         tf = (edit ?
1847                 new JTextField(entries[listIndex].getGrantEntry().codeBase) :
1848                 new JTextField());
1849         ToolWindow.configureLabelFor(label, tf, "CodeBase.");
1850         tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
1851         tf.getAccessibleContext().setAccessibleName(
1852                 PolicyTool.getMessage("Code.Base"));
1853         tw.addNewComponent(this, tf, PE_CODEBASE_TEXTFIELD,
1854                 1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH);
1855 
1856         // signedby label and textfield
1857         label = new JLabel();
1858         tw.addNewComponent(this, label, PE_SIGNEDBY_LABEL,
1859                            0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
1860                            ToolWindow.R_PADDING);
1861         tf = (edit ?
1862                 new JTextField(entries[listIndex].getGrantEntry().signedBy) :
1863                 new JTextField());
1864         ToolWindow.configureLabelFor(label, tf, "SignedBy.");
1865         tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
1866         tf.getAccessibleContext().setAccessibleName(
1867                 PolicyTool.getMessage("Signed.By."));
1868         tw.addNewComponent(this, tf, PE_SIGNEDBY_TEXTFIELD,
1869                            1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH);
1870 
1871         // panel for principal buttons
1872         JPanel panel = new JPanel();
1873         panel.setLayout(new GridBagLayout());
1874 
1875         JButton button = new JButton();
1876         ToolWindow.configureButton(button, "Add.Principal");
1877         button.addActionListener
1878                 (new AddPrinButtonListener(tool, tw, this, edit));
1879         tw.addNewComponent(panel, button, PE_ADD_PRIN_BUTTON,
1880                 0, 0, 1, 1, 100.0, 0.0, GridBagConstraints.HORIZONTAL);
1881 
1882         button = new JButton();
1883         ToolWindow.configureButton(button, "Edit.Principal");
1884         button.addActionListener(new EditPrinButtonListener
1885                                                 (tool, tw, this, edit));
1886         tw.addNewComponent(panel, button, PE_EDIT_PRIN_BUTTON,
1887                 1, 0, 1, 1, 100.0, 0.0, GridBagConstraints.HORIZONTAL);
1888 
1889         button = new JButton();
1890         ToolWindow.configureButton(button, "Remove.Principal");
1891         button.addActionListener(new RemovePrinButtonListener
1892                                         (tool, tw, this, edit));
1893         tw.addNewComponent(panel, button, PE_REMOVE_PRIN_BUTTON,
1894                 2, 0, 1, 1, 100.0, 0.0, GridBagConstraints.HORIZONTAL);
1895 
1896         tw.addNewComponent(this, panel, PE_PANEL0,
1897                 1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.HORIZONTAL,
1898                            ToolWindow.LITE_BOTTOM_PADDING);
1899 
1900         // principal label and list
1901         label = new JLabel();
1902         tw.addNewComponent(this, label, PE_PRIN_LABEL,
1903                            0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
1904                            ToolWindow.R_BOTTOM_PADDING);
1905         JScrollPane scrollPane = new JScrollPane(prinList);
1906         ToolWindow.configureLabelFor(label, scrollPane, "Principals.");
1907         tw.addNewComponent(this, scrollPane, PE_PRIN_LIST,
1908                            1, 3, 3, 1, 0.0, prinList.getVisibleRowCount(), GridBagConstraints.BOTH,
1909                            ToolWindow.BOTTOM_PADDING);
1910 
1911         // panel for permission buttons
1912         panel = new JPanel();
1913         panel.setLayout(new GridBagLayout());
1914 
1915         button = new JButton();
1916         ToolWindow.configureButton(button, ".Add.Permission");
1917         button.addActionListener(new AddPermButtonListener
1918                                                 (tool, tw, this, edit));
1919         tw.addNewComponent(panel, button, PE_ADD_PERM_BUTTON,
1920                 0, 0, 1, 1, 100.0, 0.0, GridBagConstraints.HORIZONTAL);
1921 
1922         button = new JButton();
1923         ToolWindow.configureButton(button, ".Edit.Permission");
1924         button.addActionListener(new EditPermButtonListener
1925                                                 (tool, tw, this, edit));
1926         tw.addNewComponent(panel, button, PE_EDIT_PERM_BUTTON,
1927                 1, 0, 1, 1, 100.0, 0.0, GridBagConstraints.HORIZONTAL);
1928 
1929 
1930         button = new JButton();
1931         ToolWindow.configureButton(button, "Remove.Permission");
1932         button.addActionListener(new RemovePermButtonListener
1933                                         (tool, tw, this, edit));
1934         tw.addNewComponent(panel, button, PE_REMOVE_PERM_BUTTON,
1935                 2, 0, 1, 1, 100.0, 0.0, GridBagConstraints.HORIZONTAL);
1936 
1937         tw.addNewComponent(this, panel, PE_PANEL1,
1938                 0, 4, 2, 1, 0.0, 0.0, GridBagConstraints.HORIZONTAL,
1939                 ToolWindow.LITE_BOTTOM_PADDING);
1940 
1941         // permission list
1942         scrollPane = new JScrollPane(permList);
1943         tw.addNewComponent(this, scrollPane, PE_PERM_LIST,
1944                            0, 5, 3, 1, 0.0, permList.getVisibleRowCount(), GridBagConstraints.BOTH,
1945                            ToolWindow.BOTTOM_PADDING);
1946 
1947 
1948         // panel for Done and Cancel buttons
1949         panel = new JPanel();
1950         panel.setLayout(new GridBagLayout());
1951 
1952         // Done Button
1953         JButton okButton = new JButton(PolicyTool.getMessage("Done"));
1954         okButton.addActionListener
1955                 (new AddEntryDoneButtonListener(tool, tw, this, edit));
1956         tw.addNewComponent(panel, okButton, PE_DONE_BUTTON,
1957                            0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
1958                            ToolWindow.LR_PADDING);
1959 
1960         // Cancel Button
1961         JButton cancelButton = new JButton(PolicyTool.getMessage("Cancel"));
1962         ActionListener cancelListener = new CancelButtonListener(this);
1963         cancelButton.addActionListener(cancelListener);
1964         tw.addNewComponent(panel, cancelButton, PE_CANCEL_BUTTON,
1965                            1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
1966                            ToolWindow.LR_PADDING);
1967 
1968         // add the panel
1969         tw.addNewComponent(this, panel, PE_PANEL2,
1970                 0, 6, 2, 1, 0.0, 0.0, GridBagConstraints.VERTICAL);
1971 
1972         getRootPane().setDefaultButton(okButton);
1973         getRootPane().registerKeyboardAction(cancelListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
1974 
1975         pack();
1976         setLocationRelativeTo(tw);
1977         setVisible(true);
1978     }
1979 
1980     /**
1981      * Read all the Policy information data in the dialog box
1982      * and construct a PolicyEntry object with it.
1983      */
1984     PolicyEntry getPolicyEntryFromDialog()
1985         throws InvalidParameterException, MalformedURLException,
1986         NoSuchMethodException, ClassNotFoundException, InstantiationException,
1987         IllegalAccessException, InvocationTargetException,
1988         CertificateException, IOException, Exception {
1989 
1990         // get the Codebase
1991         JTextField tf = (JTextField)getComponent(PE_CODEBASE_TEXTFIELD);
1992         String codebase = null;
1993         if (tf.getText().trim().equals("") == false)
1994                 codebase = new String(tf.getText().trim());
1995 
1996         // get the SignedBy
1997         tf = (JTextField)getComponent(PE_SIGNEDBY_TEXTFIELD);
1998         String signedby = null;
1999         if (tf.getText().trim().equals("") == false)
2000                 signedby = new String(tf.getText().trim());
2001 
2002         // construct a new GrantEntry
2003         PolicyParser.GrantEntry ge =
2004                         new PolicyParser.GrantEntry(signedby, codebase);
2005 
2006         // get the new Principals
2007         LinkedList<PolicyParser.PrincipalEntry> prins = new LinkedList<>();
2008         TaggedList prinList = (TaggedList)getComponent(PE_PRIN_LIST);
2009         for (int i = 0; i < prinList.getModel().getSize(); i++) {
2010             prins.add((PolicyParser.PrincipalEntry)prinList.getObject(i));
2011         }
2012         ge.principals = prins;
2013 
2014         // get the new Permissions
2015         Vector<PolicyParser.PermissionEntry> perms = new Vector<>();
2016         TaggedList permList = (TaggedList)getComponent(PE_PERM_LIST);
2017         for (int i = 0; i < permList.getModel().getSize(); i++) {
2018             perms.addElement((PolicyParser.PermissionEntry)permList.getObject(i));
2019         }
2020         ge.permissionEntries = perms;
2021 
2022         // construct a new PolicyEntry object
2023         PolicyEntry entry = new PolicyEntry(tool, ge);
2024 
2025         return entry;
2026     }
2027 
2028     /**
2029      * display a dialog box for the user to enter KeyStore information
2030      */
2031     void keyStoreDialog(int mode) {
2032 
2033         // find where the PolicyTool gui is
2034         Point location = tw.getLocationOnScreen();
2035         //setBounds(location.x + 25, location.y + 100, 500, 300);
2036         setLayout(new GridBagLayout());
2037 
2038         if (mode == EDIT_KEYSTORE) {
2039 
2040             // KeyStore label and textfield
2041             JLabel label = new JLabel();
2042             tw.addNewComponent(this, label, KSD_NAME_LABEL,
2043                                0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2044                                ToolWindow.R_BOTTOM_PADDING);
2045             JTextField tf = new JTextField(tool.getKeyStoreName(), 30);
2046             ToolWindow.configureLabelFor(label, tf, "KeyStore.URL.");
2047             tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
2048 
2049             // URL to U R L, so that accessibility reader will pronounce well
2050             tf.getAccessibleContext().setAccessibleName(
2051                 PolicyTool.getMessage("KeyStore.U.R.L."));
2052             tw.addNewComponent(this, tf, KSD_NAME_TEXTFIELD,
2053                                1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH,
2054                                ToolWindow.BOTTOM_PADDING);
2055 
2056             // KeyStore type and textfield
2057             label = new JLabel();
2058             tw.addNewComponent(this, label, KSD_TYPE_LABEL,
2059                                0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2060                                ToolWindow.R_BOTTOM_PADDING);
2061             tf = new JTextField(tool.getKeyStoreType(), 30);
2062             ToolWindow.configureLabelFor(label, tf, "KeyStore.Type.");
2063             tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
2064             tf.getAccessibleContext().setAccessibleName(
2065                 PolicyTool.getMessage("KeyStore.Type."));
2066             tw.addNewComponent(this, tf, KSD_TYPE_TEXTFIELD,
2067                                1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH,
2068                                ToolWindow.BOTTOM_PADDING);
2069 
2070             // KeyStore provider and textfield
2071             label = new JLabel();
2072             tw.addNewComponent(this, label, KSD_PROVIDER_LABEL,
2073                                0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2074                                ToolWindow.R_BOTTOM_PADDING);
2075             tf = new JTextField(tool.getKeyStoreProvider(), 30);
2076             ToolWindow.configureLabelFor(label, tf, "KeyStore.Provider.");
2077             tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
2078             tf.getAccessibleContext().setAccessibleName(
2079                 PolicyTool.getMessage("KeyStore.Provider."));
2080             tw.addNewComponent(this, tf, KSD_PROVIDER_TEXTFIELD,
2081                                1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH,
2082                                ToolWindow.BOTTOM_PADDING);
2083 
2084             // KeyStore password URL and textfield
2085             label = new JLabel();
2086             tw.addNewComponent(this, label, KSD_PWD_URL_LABEL,
2087                                0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2088                                ToolWindow.R_BOTTOM_PADDING);
2089             tf = new JTextField(tool.getKeyStorePwdURL(), 30);
2090             ToolWindow.configureLabelFor(label, tf, "KeyStore.Password.URL.");
2091             tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
2092             tf.getAccessibleContext().setAccessibleName(
2093                 PolicyTool.getMessage("KeyStore.Password.U.R.L."));
2094             tw.addNewComponent(this, tf, KSD_PWD_URL_TEXTFIELD,
2095                                1, 3, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH,
2096                                ToolWindow.BOTTOM_PADDING);
2097 
2098             // OK button
2099             JButton okButton = new JButton(PolicyTool.getMessage("OK"));
2100             okButton.addActionListener
2101                         (new ChangeKeyStoreOKButtonListener(tool, tw, this));
2102             tw.addNewComponent(this, okButton, KSD_OK_BUTTON,
2103                         0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL);
2104 
2105             // cancel button
2106             JButton cancelButton = new JButton(PolicyTool.getMessage("Cancel"));
2107             ActionListener cancelListener = new CancelButtonListener(this);
2108             cancelButton.addActionListener(cancelListener);
2109             tw.addNewComponent(this, cancelButton, KSD_CANCEL_BUTTON,
2110                         1, 4, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL);
2111 
2112             getRootPane().setDefaultButton(okButton);
2113             getRootPane().registerKeyboardAction(cancelListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
2114         }
2115 
2116         pack();
2117         setLocationRelativeTo(tw);
2118         setVisible(true);
2119     }
2120 
2121     /**
2122      * display a dialog box for the user to input Principal info
2123      *
2124      * if editPolicyEntry is false, then we are adding Principals to
2125      * a new PolicyEntry, and we only update the GUI listing
2126      * with the new Principal.
2127      *
2128      * if edit is true, then we are editing an existing Policy entry.
2129      */
2130     void displayPrincipalDialog(boolean editPolicyEntry, boolean edit) {
2131 
2132         PolicyParser.PrincipalEntry editMe = null;
2133 
2134         // get the Principal selected from the Principal List
2135         TaggedList prinList = (TaggedList)getComponent(PE_PRIN_LIST);
2136         int prinIndex = prinList.getSelectedIndex();
2137 
2138         if (edit) {
2139             editMe = (PolicyParser.PrincipalEntry)prinList.getObject(prinIndex);
2140         }
2141 
2142         ToolDialog newTD = new ToolDialog
2143                 (PolicyTool.getMessage("Principals"), tool, tw, true);
2144         newTD.addWindowListener(new ChildWindowListener(newTD));
2145 
2146         // find where the PolicyTool gui is
2147         Point location = getLocationOnScreen();
2148         //newTD.setBounds(location.x + 50, location.y + 100, 650, 190);
2149         newTD.setLayout(new GridBagLayout());
2150         newTD.setResizable(true);
2151 
2152         // description label
2153         JLabel label = (edit ?
2154                 new JLabel(PolicyTool.getMessage(".Edit.Principal.")) :
2155                 new JLabel(PolicyTool.getMessage(".Add.New.Principal.")));
2156         tw.addNewComponent(newTD, label, PRD_DESC_LABEL,
2157                            0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2158                            ToolWindow.TOP_BOTTOM_PADDING);
2159 
2160         // principal choice
2161         JComboBox<String> choice = new JComboBox<>();
2162         choice.addItem(PRIN_TYPE);
2163         choice.getAccessibleContext().setAccessibleName(PRIN_TYPE);
2164         for (int i = 0; i < PRIN_ARRAY.size(); i++) {
2165             Prin next = PRIN_ARRAY.get(i);
2166             choice.addItem(next.CLASS);
2167         }
2168 
2169         if (edit) {
2170             if (PolicyParser.PrincipalEntry.WILDCARD_CLASS.equals
2171                                 (editMe.getPrincipalClass())) {
2172                 choice.setSelectedItem(PRIN_TYPE);
2173             } else {
2174                 Prin inputPrin = getPrin(editMe.getPrincipalClass(), true);
2175                 if (inputPrin != null) {
2176                     choice.setSelectedItem(inputPrin.CLASS);
2177                 }
2178             }
2179         }
2180         // Add listener after selected item is set
2181         choice.addItemListener(new PrincipalTypeMenuListener(newTD));
2182 
2183         tw.addNewComponent(newTD, choice, PRD_PRIN_CHOICE,
2184                            0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2185                            ToolWindow.LR_PADDING);
2186 
2187         // principal textfield
2188         JTextField tf;
2189         tf = (edit ?
2190                 new JTextField(editMe.getDisplayClass(), 30) :
2191                 new JTextField(30));
2192         tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
2193         tf.getAccessibleContext().setAccessibleName(PRIN_TYPE);
2194         tw.addNewComponent(newTD, tf, PRD_PRIN_TEXTFIELD,
2195                            1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH,
2196                            ToolWindow.LR_PADDING);
2197 
2198         // name label and textfield
2199         label = new JLabel(PRIN_NAME);
2200         tf = (edit ?
2201                 new JTextField(editMe.getDisplayName(), 40) :
2202                 new JTextField(40));
2203         tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
2204         tf.getAccessibleContext().setAccessibleName(PRIN_NAME);
2205 
2206         tw.addNewComponent(newTD, label, PRD_NAME_LABEL,
2207                            0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2208                            ToolWindow.LR_PADDING);
2209         tw.addNewComponent(newTD, tf, PRD_NAME_TEXTFIELD,
2210                            1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH,
2211                            ToolWindow.LR_PADDING);
2212 
2213         // OK button
2214         JButton okButton = new JButton(PolicyTool.getMessage("OK"));
2215         okButton.addActionListener(
2216             new NewPolicyPrinOKButtonListener
2217                                         (tool, tw, this, newTD, edit));
2218         tw.addNewComponent(newTD, okButton, PRD_OK_BUTTON,
2219                            0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
2220                            ToolWindow.TOP_BOTTOM_PADDING);
2221         // cancel button
2222         JButton cancelButton = new JButton(PolicyTool.getMessage("Cancel"));
2223         ActionListener cancelListener = new CancelButtonListener(newTD);
2224         cancelButton.addActionListener(cancelListener);
2225         tw.addNewComponent(newTD, cancelButton, PRD_CANCEL_BUTTON,
2226                            1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
2227                            ToolWindow.TOP_BOTTOM_PADDING);
2228 
2229         newTD.getRootPane().setDefaultButton(okButton);
2230         newTD.getRootPane().registerKeyboardAction(cancelListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
2231 
2232         newTD.pack();
2233         newTD.setLocationRelativeTo(tw);
2234         newTD.setVisible(true);
2235     }
2236 
2237     /**
2238      * display a dialog box for the user to input Permission info
2239      *
2240      * if editPolicyEntry is false, then we are adding Permissions to
2241      * a new PolicyEntry, and we only update the GUI listing
2242      * with the new Permission.
2243      *
2244      * if edit is true, then we are editing an existing Permission entry.
2245      */
2246     void displayPermissionDialog(boolean editPolicyEntry, boolean edit) {
2247 
2248         PolicyParser.PermissionEntry editMe = null;
2249 
2250         // get the Permission selected from the Permission List
2251         TaggedList permList = (TaggedList)getComponent(PE_PERM_LIST);
2252         int permIndex = permList.getSelectedIndex();
2253 
2254         if (edit) {
2255             editMe = (PolicyParser.PermissionEntry)permList.getObject(permIndex);
2256         }
2257 
2258         ToolDialog newTD = new ToolDialog
2259                 (PolicyTool.getMessage("Permissions"), tool, tw, true);
2260         newTD.addWindowListener(new ChildWindowListener(newTD));
2261 
2262         // find where the PolicyTool gui is
2263         Point location = getLocationOnScreen();
2264         //newTD.setBounds(location.x + 50, location.y + 100, 700, 250);
2265         newTD.setLayout(new GridBagLayout());
2266         newTD.setResizable(true);
2267 
2268         // description label
2269         JLabel label = (edit ?
2270                 new JLabel(PolicyTool.getMessage(".Edit.Permission.")) :
2271                 new JLabel(PolicyTool.getMessage(".Add.New.Permission.")));
2272         tw.addNewComponent(newTD, label, PD_DESC_LABEL,
2273                            0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2274                            ToolWindow.TOP_BOTTOM_PADDING);
2275 
2276         // permission choice (added in alphabetical order)
2277         JComboBox<String> choice = new JComboBox<>();
2278         choice.addItem(PERM);
2279         choice.getAccessibleContext().setAccessibleName(PERM);
2280         for (int i = 0; i < PERM_ARRAY.size(); i++) {
2281             Perm next = PERM_ARRAY.get(i);
2282             choice.addItem(next.CLASS);
2283         }
2284         tw.addNewComponent(newTD, choice, PD_PERM_CHOICE,
2285                            0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2286                            ToolWindow.LR_BOTTOM_PADDING);
2287 
2288         // permission textfield
2289         JTextField tf;
2290         tf = (edit ? new JTextField(editMe.permission, 30) : new JTextField(30));
2291         tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
2292         tf.getAccessibleContext().setAccessibleName(PERM);
2293         if (edit) {
2294             Perm inputPerm = getPerm(editMe.permission, true);
2295             if (inputPerm != null) {
2296                 choice.setSelectedItem(inputPerm.CLASS);
2297             }
2298         }
2299         tw.addNewComponent(newTD, tf, PD_PERM_TEXTFIELD,
2300                            1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH,
2301                            ToolWindow.LR_BOTTOM_PADDING);
2302         choice.addItemListener(new PermissionMenuListener(newTD));
2303 
2304         // name label and textfield
2305         choice = new JComboBox<>();
2306         choice.addItem(PERM_NAME);
2307         choice.getAccessibleContext().setAccessibleName(PERM_NAME);
2308         tf = (edit ? new JTextField(editMe.name, 40) : new JTextField(40));
2309         tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
2310         tf.getAccessibleContext().setAccessibleName(PERM_NAME);
2311         if (edit) {
2312             setPermissionNames(getPerm(editMe.permission, true), choice, tf);
2313         }
2314         tw.addNewComponent(newTD, choice, PD_NAME_CHOICE,
2315                            0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2316                            ToolWindow.LR_BOTTOM_PADDING);
2317         tw.addNewComponent(newTD, tf, PD_NAME_TEXTFIELD,
2318                            1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH,
2319                            ToolWindow.LR_BOTTOM_PADDING);
2320         choice.addItemListener(new PermissionNameMenuListener(newTD));
2321 
2322         // actions label and textfield
2323         choice = new JComboBox<>();
2324         choice.addItem(PERM_ACTIONS);
2325         choice.getAccessibleContext().setAccessibleName(PERM_ACTIONS);
2326         tf = (edit ? new JTextField(editMe.action, 40) : new JTextField(40));
2327         tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
2328         tf.getAccessibleContext().setAccessibleName(PERM_ACTIONS);
2329         if (edit) {
2330             setPermissionActions(getPerm(editMe.permission, true), choice, tf);
2331         }
2332         tw.addNewComponent(newTD, choice, PD_ACTIONS_CHOICE,
2333                            0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2334                            ToolWindow.LR_BOTTOM_PADDING);
2335         tw.addNewComponent(newTD, tf, PD_ACTIONS_TEXTFIELD,
2336                            1, 3, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH,
2337                            ToolWindow.LR_BOTTOM_PADDING);
2338         choice.addItemListener(new PermissionActionsMenuListener(newTD));
2339 
2340         // signedby label and textfield
2341         label = new JLabel(PolicyTool.getMessage("Signed.By."));
2342         tw.addNewComponent(newTD, label, PD_SIGNEDBY_LABEL,
2343                            0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2344                            ToolWindow.LR_BOTTOM_PADDING);
2345         tf = (edit ? new JTextField(editMe.signedBy, 40) : new JTextField(40));
2346         tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
2347         tf.getAccessibleContext().setAccessibleName(
2348                 PolicyTool.getMessage("Signed.By."));
2349         tw.addNewComponent(newTD, tf, PD_SIGNEDBY_TEXTFIELD,
2350                            1, 4, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH,
2351                            ToolWindow.LR_BOTTOM_PADDING);
2352 
2353         // OK button
2354         JButton okButton = new JButton(PolicyTool.getMessage("OK"));
2355         okButton.addActionListener(
2356             new NewPolicyPermOKButtonListener
2357                                     (tool, tw, this, newTD, edit));
2358         tw.addNewComponent(newTD, okButton, PD_OK_BUTTON,
2359                            0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
2360                            ToolWindow.TOP_BOTTOM_PADDING);
2361 
2362         // cancel button
2363         JButton cancelButton = new JButton(PolicyTool.getMessage("Cancel"));
2364         ActionListener cancelListener = new CancelButtonListener(newTD);
2365         cancelButton.addActionListener(cancelListener);
2366         tw.addNewComponent(newTD, cancelButton, PD_CANCEL_BUTTON,
2367                            1, 5, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
2368                            ToolWindow.TOP_BOTTOM_PADDING);
2369 
2370         newTD.getRootPane().setDefaultButton(okButton);
2371         newTD.getRootPane().registerKeyboardAction(cancelListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
2372 
2373         newTD.pack();
2374         newTD.setLocationRelativeTo(tw);
2375         newTD.setVisible(true);
2376     }
2377 
2378     /**
2379      * construct a Principal object from the Principal Info Dialog Box
2380      */
2381     PolicyParser.PrincipalEntry getPrinFromDialog() throws Exception {
2382 
2383         JTextField tf = (JTextField)getComponent(PRD_PRIN_TEXTFIELD);
2384         String pclass = new String(tf.getText().trim());
2385         tf = (JTextField)getComponent(PRD_NAME_TEXTFIELD);
2386         String pname = new String(tf.getText().trim());
2387         if (pclass.equals("*")) {
2388             pclass = PolicyParser.PrincipalEntry.WILDCARD_CLASS;
2389         }
2390         if (pname.equals("*")) {
2391             pname = PolicyParser.PrincipalEntry.WILDCARD_NAME;
2392         }
2393 
2394         PolicyParser.PrincipalEntry pppe = null;
2395 
2396         if ((pclass.equals(PolicyParser.PrincipalEntry.WILDCARD_CLASS)) &&
2397             (!pname.equals(PolicyParser.PrincipalEntry.WILDCARD_NAME))) {
2398             throw new Exception
2399                         (PolicyTool.getMessage("Cannot.Specify.Principal.with.a.Wildcard.Class.without.a.Wildcard.Name"));
2400         } else if (pname.equals("")) {
2401             throw new Exception
2402                         (PolicyTool.getMessage("Cannot.Specify.Principal.without.a.Name"));
2403         } else if (pclass.equals("")) {
2404             // make this consistent with what PolicyParser does
2405             // when it sees an empty principal class
2406             pclass = PolicyParser.PrincipalEntry.REPLACE_NAME;
2407             tool.warnings.addElement(
2408                         "Warning: Principal name '" + pname +
2409                                 "' specified without a Principal class.\n" +
2410                         "\t'" + pname + "' will be interpreted " +
2411                                 "as a key store alias.\n" +
2412                         "\tThe final principal class will be " +
2413                                 ToolDialog.X500_PRIN_CLASS + ".\n" +
2414                         "\tThe final principal name will be " +
2415                                 "determined by the following:\n" +
2416                         "\n" +
2417                         "\tIf the key store entry identified by '"
2418                                 + pname + "'\n" +
2419                         "\tis a key entry, then the principal name will be\n" +
2420                         "\tthe subject distinguished name from the first\n" +
2421                         "\tcertificate in the entry's certificate chain.\n" +
2422                         "\n" +
2423                         "\tIf the key store entry identified by '" +
2424                                 pname + "'\n" +
2425                         "\tis a trusted certificate entry, then the\n" +
2426                         "\tprincipal name will be the subject distinguished\n" +
2427                         "\tname from the trusted public key certificate.");
2428             tw.displayStatusDialog(this,
2429                         "'" + pname + "' will be interpreted as a key " +
2430                         "store alias.  View Warning Log for details.");
2431         }
2432         return new PolicyParser.PrincipalEntry(pclass, pname);
2433     }
2434 
2435 
2436     /**
2437      * construct a Permission object from the Permission Info Dialog Box
2438      */
2439     PolicyParser.PermissionEntry getPermFromDialog() {
2440 
2441         JTextField tf = (JTextField)getComponent(PD_PERM_TEXTFIELD);
2442         String permission = new String(tf.getText().trim());
2443         tf = (JTextField)getComponent(PD_NAME_TEXTFIELD);
2444         String name = null;
2445         if (tf.getText().trim().equals("") == false)
2446             name = new String(tf.getText().trim());
2447         if (permission.equals("") ||
2448             (!permission.equals(ALL_PERM_CLASS) && name == null)) {
2449             throw new InvalidParameterException(PolicyTool.getMessage
2450                 ("Permission.and.Target.Name.must.have.a.value"));
2451         }
2452 
2453         // When the permission is FilePermission, we need to check the name
2454         // to make sure it's not escaped. We believe --
2455         //
2456         // String             name.lastIndexOf("\\\\")
2457         // ----------------   ------------------------
2458         // c:\foo\bar         -1, legal
2459         // c:\\foo\\bar       2, illegal
2460         // \\server\share     0, legal
2461         // \\\\server\share   2, illegal
2462 
2463         if (permission.equals(FILE_PERM_CLASS) && name.lastIndexOf("\\\\") > 0) {
2464             char result = tw.displayYesNoDialog(this,
2465                     PolicyTool.getMessage("Warning"),
2466                     PolicyTool.getMessage(
2467                         "Warning.File.name.may.include.escaped.backslash.characters.It.is.not.necessary.to.escape.backslash.characters.the.tool.escapes"),
2468                     PolicyTool.getMessage("Retain"),
2469                     PolicyTool.getMessage("Edit")
2470                     );
2471             if (result != 'Y') {
2472                 // an invisible exception
2473                 throw new NoDisplayException();
2474             }
2475         }
2476         // get the Actions
2477         tf = (JTextField)getComponent(PD_ACTIONS_TEXTFIELD);
2478         String actions = null;
2479         if (tf.getText().trim().equals("") == false)
2480             actions = new String(tf.getText().trim());
2481 
2482         // get the Signed By
2483         tf = (JTextField)getComponent(PD_SIGNEDBY_TEXTFIELD);
2484         String signedBy = null;
2485         if (tf.getText().trim().equals("") == false)
2486             signedBy = new String(tf.getText().trim());
2487 
2488         PolicyParser.PermissionEntry pppe = new PolicyParser.PermissionEntry
2489                                 (permission, name, actions);
2490         pppe.signedBy = signedBy;
2491 
2492         // see if the signers have public keys
2493         if (signedBy != null) {
2494                 String signers[] = tool.parseSigners(pppe.signedBy);
2495                 for (int i = 0; i < signers.length; i++) {
2496                 try {
2497                     PublicKey pubKey = tool.getPublicKeyAlias(signers[i]);
2498                     if (pubKey == null) {
2499                         MessageFormat form = new MessageFormat
2500                             (PolicyTool.getMessage
2501                             ("Warning.A.public.key.for.alias.signers.i.does.not.exist.Make.sure.a.KeyStore.is.properly.configured."));
2502                         Object[] source = {signers[i]};
2503                         tool.warnings.addElement(form.format(source));
2504                         tw.displayStatusDialog(this, form.format(source));
2505                     }
2506                 } catch (Exception e) {
2507                     tw.displayErrorDialog(this, e);
2508                 }
2509             }
2510         }
2511         return pppe;
2512     }
2513 
2514     /**
2515      * confirm that the user REALLY wants to remove the Policy Entry
2516      */
2517     void displayConfirmRemovePolicyEntry() {
2518 
2519         // find the entry to be removed
2520         @SuppressWarnings("unchecked")
2521         JList<String> list = (JList<String>)tw.getComponent(ToolWindow.MW_POLICY_LIST);
2522         int index = list.getSelectedIndex();
2523         PolicyEntry entries[] = tool.getEntry();
2524 
2525         // find where the PolicyTool gui is
2526         Point location = tw.getLocationOnScreen();
2527         //setBounds(location.x + 25, location.y + 100, 600, 400);
2528         setLayout(new GridBagLayout());
2529 
2530         // ask the user do they really want to do this?
2531         JLabel label = new JLabel
2532                 (PolicyTool.getMessage("Remove.this.Policy.Entry."));
2533         tw.addNewComponent(this, label, CRPE_LABEL1,
2534                            0, 0, 2, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2535                            ToolWindow.BOTTOM_PADDING);
2536 
2537         // display the policy entry
2538         label = new JLabel(entries[index].codebaseToString());
2539         tw.addNewComponent(this, label, CRPE_LABEL2,
2540                         0, 1, 2, 1, 0.0, 0.0, GridBagConstraints.BOTH);
2541         label = new JLabel(entries[index].principalsToString().trim());
2542         tw.addNewComponent(this, label, CRPE_LABEL2+1,
2543                         0, 2, 2, 1, 0.0, 0.0, GridBagConstraints.BOTH);
2544         Vector<PolicyParser.PermissionEntry> perms =
2545                         entries[index].getGrantEntry().permissionEntries;
2546         for (int i = 0; i < perms.size(); i++) {
2547             PolicyParser.PermissionEntry nextPerm = perms.elementAt(i);
2548             String permString = ToolDialog.PermissionEntryToUserFriendlyString(nextPerm);
2549             label = new JLabel("    " + permString);
2550             if (i == (perms.size()-1)) {
2551                 tw.addNewComponent(this, label, CRPE_LABEL2 + 2 + i,
2552                                  1, 3 + i, 1, 1, 0.0, 0.0,
2553                                  GridBagConstraints.BOTH,
2554                                  ToolWindow.BOTTOM_PADDING);
2555             } else {
2556                 tw.addNewComponent(this, label, CRPE_LABEL2 + 2 + i,
2557                                  1, 3 + i, 1, 1, 0.0, 0.0,
2558                                  GridBagConstraints.BOTH);
2559             }
2560         }
2561 
2562 
2563         // add OK/CANCEL buttons in a new panel
2564         JPanel panel = new JPanel();
2565         panel.setLayout(new GridBagLayout());
2566 
2567         // OK button
2568         JButton okButton = new JButton(PolicyTool.getMessage("OK"));
2569         okButton.addActionListener
2570                 (new ConfirmRemovePolicyEntryOKButtonListener(tool, tw, this));
2571         tw.addNewComponent(panel, okButton, CRPE_PANEL_OK,
2572                            0, 0, 1, 1, 0.0, 0.0,
2573                            GridBagConstraints.VERTICAL, ToolWindow.LR_PADDING);
2574 
2575         // cancel button
2576         JButton cancelButton = new JButton(PolicyTool.getMessage("Cancel"));
2577         ActionListener cancelListener = new CancelButtonListener(this);
2578         cancelButton.addActionListener(cancelListener);
2579         tw.addNewComponent(panel, cancelButton, CRPE_PANEL_CANCEL,
2580                            1, 0, 1, 1, 0.0, 0.0,
2581                            GridBagConstraints.VERTICAL, ToolWindow.LR_PADDING);
2582 
2583         tw.addNewComponent(this, panel, CRPE_LABEL2 + 2 + perms.size(),
2584                            0, 3 + perms.size(), 2, 1, 0.0, 0.0,
2585                            GridBagConstraints.VERTICAL, ToolWindow.TOP_BOTTOM_PADDING);
2586 
2587         getRootPane().setDefaultButton(okButton);
2588         getRootPane().registerKeyboardAction(cancelListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
2589 
2590         pack();
2591         setLocationRelativeTo(tw);
2592         setVisible(true);
2593     }
2594 
2595     /**
2596      * perform SAVE AS
2597      */
2598     void displaySaveAsDialog(int nextEvent) {
2599 
2600         // pop up a dialog box for the user to enter a filename.
2601         FileDialog fd = new FileDialog
2602                 (tw, PolicyTool.getMessage("Save.As"), FileDialog.SAVE);
2603         fd.addWindowListener(new WindowAdapter() {
2604             public void windowClosing(WindowEvent e) {
2605                 e.getWindow().setVisible(false);
2606             }
2607         });
2608         fd.setVisible(true);
2609 
2610         // see if the user hit cancel
2611         if (fd.getFile() == null ||
2612             fd.getFile().equals(""))
2613             return;
2614 
2615         // get the entered filename
2616         File saveAsFile = new File(fd.getDirectory(), fd.getFile());
2617         String filename = saveAsFile.getPath();
2618         fd.dispose();
2619 
2620         try {
2621             // save the policy entries to a file
2622             tool.savePolicy(filename);
2623 
2624             // display status
2625             MessageFormat form = new MessageFormat(PolicyTool.getMessage
2626                     ("Policy.successfully.written.to.filename"));
2627             Object[] source = {filename};
2628             tw.displayStatusDialog(null, form.format(source));
2629 
2630             // display the new policy filename
2631             JTextField newFilename = (JTextField)tw.getComponent
2632                             (ToolWindow.MW_FILENAME_TEXTFIELD);
2633             newFilename.setText(filename);
2634             tw.setVisible(true);
2635 
2636             // now continue with the originally requested command
2637             // (QUIT, NEW, or OPEN)
2638             userSaveContinue(tool, tw, this, nextEvent);
2639 
2640         } catch (FileNotFoundException fnfe) {
2641             if (filename == null || filename.equals("")) {
2642                 tw.displayErrorDialog(null, new FileNotFoundException
2643                             (PolicyTool.getMessage("null.filename")));
2644             } else {
2645                 tw.displayErrorDialog(null, fnfe);
2646             }
2647         } catch (Exception ee) {
2648             tw.displayErrorDialog(null, ee);
2649         }
2650     }
2651 
2652     /**
2653      * ask user if they want to save changes
2654      */
2655     void displayUserSave(int select) {
2656 
2657         if (tool.modified == true) {
2658 
2659             // find where the PolicyTool gui is
2660             Point location = tw.getLocationOnScreen();
2661             //setBounds(location.x + 75, location.y + 100, 400, 150);
2662             setLayout(new GridBagLayout());
2663 
2664             JLabel label = new JLabel
2665                 (PolicyTool.getMessage("Save.changes."));
2666             tw.addNewComponent(this, label, USC_LABEL,
2667                                0, 0, 3, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2668                                ToolWindow.L_TOP_BOTTOM_PADDING);
2669 
2670             JPanel panel = new JPanel();
2671             panel.setLayout(new GridBagLayout());
2672 
2673             JButton yesButton = new JButton();
2674             ToolWindow.configureButton(yesButton, "Yes");
2675             yesButton.addActionListener
2676                         (new UserSaveYesButtonListener(this, tool, tw, select));
2677             tw.addNewComponent(panel, yesButton, USC_YES_BUTTON,
2678                                0, 0, 1, 1, 0.0, 0.0,
2679                                GridBagConstraints.VERTICAL,
2680                                ToolWindow.LR_BOTTOM_PADDING);
2681             JButton noButton = new JButton();
2682             ToolWindow.configureButton(noButton, "No");
2683             noButton.addActionListener
2684                         (new UserSaveNoButtonListener(this, tool, tw, select));
2685             tw.addNewComponent(panel, noButton, USC_NO_BUTTON,
2686                                1, 0, 1, 1, 0.0, 0.0,
2687                                GridBagConstraints.VERTICAL,
2688                                ToolWindow.LR_BOTTOM_PADDING);
2689             JButton cancelButton = new JButton();
2690             ToolWindow.configureButton(cancelButton, "Cancel");
2691             ActionListener cancelListener = new CancelButtonListener(this);
2692             cancelButton.addActionListener(cancelListener);
2693             tw.addNewComponent(panel, cancelButton, USC_CANCEL_BUTTON,
2694                                2, 0, 1, 1, 0.0, 0.0,
2695                                GridBagConstraints.VERTICAL,
2696                                ToolWindow.LR_BOTTOM_PADDING);
2697 
2698             tw.addNewComponent(this, panel, USC_PANEL,
2699                                0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH);
2700 
2701             getRootPane().registerKeyboardAction(cancelListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
2702 
2703             pack();
2704             setLocationRelativeTo(tw);
2705             setVisible(true);
2706         } else {
2707             // just do the original request (QUIT, NEW, or OPEN)
2708             userSaveContinue(tool, tw, this, select);
2709         }
2710     }
2711 
2712     /**
2713      * when the user sees the 'YES', 'NO', 'CANCEL' buttons on the
2714      * displayUserSave dialog, and the click on one of them,
2715      * we need to continue the originally requested action
2716      * (either QUITting, opening NEW policy file, or OPENing an existing
2717      * policy file.  do that now.
2718      */
2719     @SuppressWarnings("fallthrough")
2720     void userSaveContinue(PolicyTool tool, ToolWindow tw,
2721                         ToolDialog us, int select) {
2722 
2723         // now either QUIT, open a NEW policy file, or OPEN an existing policy
2724         switch(select) {
2725         case ToolDialog.QUIT:
2726 
2727             tw.setVisible(false);
2728             tw.dispose();
2729             System.exit(0);
2730 
2731         case ToolDialog.NEW:
2732 
2733             try {
2734                 tool.openPolicy(null);
2735             } catch (Exception ee) {
2736                 tool.modified = false;
2737                 tw.displayErrorDialog(null, ee);
2738             }
2739 
2740             // display the policy entries via the policy list textarea
2741             JList<String> list = new JList<>(new DefaultListModel<>());
2742             list.setVisibleRowCount(15);
2743             list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
2744             list.addMouseListener(new PolicyListListener(tool, tw));
2745             tw.replacePolicyList(list);
2746 
2747             // display null policy filename and keystore
2748             JTextField newFilename = (JTextField)tw.getComponent(
2749                     ToolWindow.MW_FILENAME_TEXTFIELD);
2750             newFilename.setText("");
2751             tw.setVisible(true);
2752             break;
2753 
2754         case ToolDialog.OPEN:
2755 
2756             // pop up a dialog box for the user to enter a filename.
2757             FileDialog fd = new FileDialog
2758                 (tw, PolicyTool.getMessage("Open"), FileDialog.LOAD);
2759             fd.addWindowListener(new WindowAdapter() {
2760                 public void windowClosing(WindowEvent e) {
2761                     e.getWindow().setVisible(false);
2762                 }
2763             });
2764             fd.setVisible(true);
2765 
2766             // see if the user hit 'cancel'
2767             if (fd.getFile() == null ||
2768                 fd.getFile().equals(""))
2769                 return;
2770 
2771             // get the entered filename
2772             String policyFile = new File(fd.getDirectory(), fd.getFile()).getPath();
2773 
2774             try {
2775                 // open the policy file
2776                 tool.openPolicy(policyFile);
2777 
2778                 // display the policy entries via the policy list textarea
2779                 DefaultListModel<String> listModel = new DefaultListModel<>();
2780                 list = new JList<>(listModel);
2781                 list.setVisibleRowCount(15);
2782                 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
2783                 list.addMouseListener(new PolicyListListener(tool, tw));
2784                 PolicyEntry entries[] = tool.getEntry();
2785                 if (entries != null) {
2786                     for (int i = 0; i < entries.length; i++) {
2787                         listModel.addElement(entries[i].headerToString());
2788                     }
2789                 }
2790                 tw.replacePolicyList(list);
2791                 tool.modified = false;
2792 
2793                 // display the new policy filename
2794                 newFilename = (JTextField)tw.getComponent(
2795                         ToolWindow.MW_FILENAME_TEXTFIELD);
2796                 newFilename.setText(policyFile);
2797                 tw.setVisible(true);
2798 
2799                 // inform user of warnings
2800                 if (tool.newWarning == true) {
2801                     tw.displayStatusDialog(null, PolicyTool.getMessage
2802                         ("Errors.have.occurred.while.opening.the.policy.configuration.View.the.Warning.Log.for.more.information."));
2803                 }
2804 
2805             } catch (Exception e) {
2806                 // add blank policy listing
2807                 list = new JList<>(new DefaultListModel<>());
2808                 list.setVisibleRowCount(15);
2809                 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
2810                 list.addMouseListener(new PolicyListListener(tool, tw));
2811                 tw.replacePolicyList(list);
2812                 tool.setPolicyFileName(null);
2813                 tool.modified = false;
2814 
2815                 // display a null policy filename
2816                 newFilename = (JTextField)tw.getComponent(
2817                         ToolWindow.MW_FILENAME_TEXTFIELD);
2818                 newFilename.setText("");
2819                 tw.setVisible(true);
2820 
2821                 // display the error
2822                 MessageFormat form = new MessageFormat(PolicyTool.getMessage
2823                     ("Could.not.open.policy.file.policyFile.e.toString."));
2824                 Object[] source = {policyFile, e.toString()};
2825                 tw.displayErrorDialog(null, form.format(source));
2826             }
2827             break;
2828         }
2829     }
2830 
2831     /**
2832      * Return a Menu list of names for a given permission
2833      *
2834      * If inputPerm's TARGETS are null, then this means TARGETS are
2835      * not allowed to be entered (and the TextField is set to be
2836      * non-editable).
2837      *
2838      * If TARGETS are valid but there are no standard ones
2839      * (user must enter them by hand) then the TARGETS array may be empty
2840      * (and of course non-null).
2841      */
2842     void setPermissionNames(Perm inputPerm, JComboBox<String> names, JTextField field) {
2843         names.removeAllItems();
2844         names.addItem(PERM_NAME);
2845 
2846         if (inputPerm == null) {
2847             // custom permission
2848             field.setEditable(true);
2849         } else if (inputPerm.TARGETS == null) {
2850             // standard permission with no targets
2851             field.setEditable(false);
2852         } else {
2853             // standard permission with standard targets
2854             field.setEditable(true);
2855             for (int i = 0; i < inputPerm.TARGETS.length; i++) {
2856                 names.addItem(inputPerm.TARGETS[i]);
2857             }
2858         }
2859     }
2860 
2861     /**
2862      * Return a Menu list of actions for a given permission
2863      *
2864      * If inputPerm's ACTIONS are null, then this means ACTIONS are
2865      * not allowed to be entered (and the TextField is set to be
2866      * non-editable).  This is typically true for BasicPermissions.
2867      *
2868      * If ACTIONS are valid but there are no standard ones
2869      * (user must enter them by hand) then the ACTIONS array may be empty
2870      * (and of course non-null).
2871      */
2872     void setPermissionActions(Perm inputPerm, JComboBox<String> actions, JTextField field) {
2873         actions.removeAllItems();
2874         actions.addItem(PERM_ACTIONS);
2875 
2876         if (inputPerm == null) {
2877             // custom permission
2878             field.setEditable(true);
2879         } else if (inputPerm.ACTIONS == null) {
2880             // standard permission with no actions
2881             field.setEditable(false);
2882         } else {
2883             // standard permission with standard actions
2884             field.setEditable(true);
2885             for (int i = 0; i < inputPerm.ACTIONS.length; i++) {
2886                 actions.addItem(inputPerm.ACTIONS[i]);
2887             }
2888         }
2889     }
2890 
2891     static String PermissionEntryToUserFriendlyString(PolicyParser.PermissionEntry pppe) {
2892         String result = pppe.permission;
2893         if (pppe.name != null) {
2894             result += " " + pppe.name;
2895         }
2896         if (pppe.action != null) {
2897             result += ", \"" + pppe.action + "\"";
2898         }
2899         if (pppe.signedBy != null) {
2900             result += ", signedBy " + pppe.signedBy;
2901         }
2902         return result;
2903     }
2904 
2905     static String PrincipalEntryToUserFriendlyString(PolicyParser.PrincipalEntry pppe) {
2906         StringWriter sw = new StringWriter();
2907         PrintWriter pw = new PrintWriter(sw);
2908         pppe.write(pw);
2909         return sw.toString();
2910     }
2911 }
2912 
2913 /**
2914  * Event handler for the PolicyTool window
2915  */
2916 class ToolWindowListener implements WindowListener {
2917 
2918     private PolicyTool tool;
2919     private ToolWindow tw;
2920 
2921     ToolWindowListener(PolicyTool tool, ToolWindow tw) {
2922         this.tool = tool;
2923         this.tw = tw;
2924     }
2925 
2926     public void windowOpened(WindowEvent we) {
2927     }
2928 
2929     public void windowClosing(WindowEvent we) {
2930         // Closing the window acts the same as choosing Menu->Exit.
2931 
2932         // ask user if they want to save changes
2933         ToolDialog td = new ToolDialog(PolicyTool.getMessage("Save.Changes"), tool, tw, true);
2934         td.displayUserSave(ToolDialog.QUIT);
2935 
2936         // the above method will perform the QUIT as long as the
2937         // user does not CANCEL the request
2938     }
2939 
2940     public void windowClosed(WindowEvent we) {
2941         System.exit(0);
2942     }
2943 
2944     public void windowIconified(WindowEvent we) {
2945     }
2946 
2947     public void windowDeiconified(WindowEvent we) {
2948     }
2949 
2950     public void windowActivated(WindowEvent we) {
2951     }
2952 
2953     public void windowDeactivated(WindowEvent we) {
2954     }
2955 }
2956 
2957 /**
2958  * Event handler for the Policy List
2959  */
2960 class PolicyListListener extends MouseAdapter implements ActionListener {
2961 
2962     private PolicyTool tool;
2963     private ToolWindow tw;
2964 
2965     PolicyListListener(PolicyTool tool, ToolWindow tw) {
2966         this.tool = tool;
2967         this.tw = tw;
2968 
2969     }
2970 
2971     public void actionPerformed(ActionEvent e) {
2972 
2973         // display the permission list for a policy entry
2974         ToolDialog td = new ToolDialog
2975                 (PolicyTool.getMessage("Policy.Entry"), tool, tw, true);
2976         td.displayPolicyEntryDialog(true);
2977     }
2978 
2979     public void mouseClicked(MouseEvent evt) {
2980         if (evt.getClickCount() == 2) {
2981             actionPerformed(null);
2982         }
2983     }
2984 }
2985 
2986 /**
2987  * Event handler for the File Menu
2988  */
2989 class FileMenuListener implements ActionListener {
2990 
2991     private PolicyTool tool;
2992     private ToolWindow tw;
2993 
2994     FileMenuListener(PolicyTool tool, ToolWindow tw) {
2995         this.tool = tool;
2996         this.tw = tw;
2997     }
2998 
2999     public void actionPerformed(ActionEvent e) {
3000 
3001         if (PolicyTool.collator.compare(e.getActionCommand(),
3002                                        ToolWindow.QUIT) == 0) {
3003 
3004             // ask user if they want to save changes
3005             ToolDialog td = new ToolDialog
3006                 (PolicyTool.getMessage("Save.Changes"), tool, tw, true);
3007             td.displayUserSave(ToolDialog.QUIT);
3008 
3009             // the above method will perform the QUIT as long as the
3010             // user does not CANCEL the request
3011 
3012         } else if (PolicyTool.collator.compare(e.getActionCommand(),
3013                                    ToolWindow.NEW_POLICY_FILE) == 0) {
3014 
3015             // ask user if they want to save changes
3016             ToolDialog td = new ToolDialog
3017                 (PolicyTool.getMessage("Save.Changes"), tool, tw, true);
3018             td.displayUserSave(ToolDialog.NEW);
3019 
3020             // the above method will perform the NEW as long as the
3021             // user does not CANCEL the request
3022 
3023         } else if (PolicyTool.collator.compare(e.getActionCommand(),
3024                                   ToolWindow.OPEN_POLICY_FILE) == 0) {
3025 
3026             // ask user if they want to save changes
3027             ToolDialog td = new ToolDialog
3028                 (PolicyTool.getMessage("Save.Changes"), tool, tw, true);
3029             td.displayUserSave(ToolDialog.OPEN);
3030 
3031             // the above method will perform the OPEN as long as the
3032             // user does not CANCEL the request
3033 
3034         } else if (PolicyTool.collator.compare(e.getActionCommand(),
3035                                   ToolWindow.SAVE_POLICY_FILE) == 0) {
3036 
3037             // get the previously entered filename
3038             String filename = ((JTextField)tw.getComponent(
3039                     ToolWindow.MW_FILENAME_TEXTFIELD)).getText();
3040 
3041             // if there is no filename, do a SAVE_AS
3042             if (filename == null || filename.length() == 0) {
3043                 // user wants to SAVE AS
3044                 ToolDialog td = new ToolDialog
3045                         (PolicyTool.getMessage("Save.As"), tool, tw, true);
3046                 td.displaySaveAsDialog(ToolDialog.NOACTION);
3047             } else {
3048                 try {
3049                     // save the policy entries to a file
3050                     tool.savePolicy(filename);
3051 
3052                     // display status
3053                     MessageFormat form = new MessageFormat
3054                         (PolicyTool.getMessage
3055                         ("Policy.successfully.written.to.filename"));
3056                     Object[] source = {filename};
3057                     tw.displayStatusDialog(null, form.format(source));
3058                 } catch (FileNotFoundException fnfe) {
3059                     if (filename == null || filename.equals("")) {
3060                         tw.displayErrorDialog(null, new FileNotFoundException
3061                                 (PolicyTool.getMessage("null.filename")));
3062                     } else {
3063                         tw.displayErrorDialog(null, fnfe);
3064                     }
3065                 } catch (Exception ee) {
3066                     tw.displayErrorDialog(null, ee);
3067                 }
3068             }
3069         } else if (PolicyTool.collator.compare(e.getActionCommand(),
3070                                ToolWindow.SAVE_AS_POLICY_FILE) == 0) {
3071 
3072             // user wants to SAVE AS
3073             ToolDialog td = new ToolDialog
3074                 (PolicyTool.getMessage("Save.As"), tool, tw, true);
3075             td.displaySaveAsDialog(ToolDialog.NOACTION);
3076 
3077         } else if (PolicyTool.collator.compare(e.getActionCommand(),
3078                                      ToolWindow.VIEW_WARNINGS) == 0) {
3079             tw.displayWarningLog(null);
3080         }
3081     }
3082 }
3083 
3084 /**
3085  * Event handler for the main window buttons and Edit Menu
3086  */
3087 class MainWindowListener implements ActionListener {
3088 
3089     private PolicyTool tool;
3090     private ToolWindow tw;
3091 
3092     MainWindowListener(PolicyTool tool, ToolWindow tw) {
3093         this.tool = tool;
3094         this.tw = tw;
3095     }
3096 
3097     public void actionPerformed(ActionEvent e) {
3098 
3099         if (PolicyTool.collator.compare(e.getActionCommand(),
3100                            ToolWindow.ADD_POLICY_ENTRY) == 0) {
3101 
3102             // display a dialog box for the user to enter policy info
3103             ToolDialog td = new ToolDialog
3104                 (PolicyTool.getMessage("Policy.Entry"), tool, tw, true);
3105             td.displayPolicyEntryDialog(false);
3106 
3107         } else if (PolicyTool.collator.compare(e.getActionCommand(),
3108                                ToolWindow.REMOVE_POLICY_ENTRY) == 0) {
3109 
3110             // get the selected entry
3111             @SuppressWarnings("unchecked")
3112             JList<String> list = (JList<String>)tw.getComponent(ToolWindow.MW_POLICY_LIST);
3113             int index = list.getSelectedIndex();
3114             if (index < 0) {
3115                 tw.displayErrorDialog(null, new Exception
3116                         (PolicyTool.getMessage("No.Policy.Entry.selected")));
3117                 return;
3118             }
3119 
3120             // ask the user if they really want to remove the policy entry
3121             ToolDialog td = new ToolDialog(PolicyTool.getMessage
3122                 ("Remove.Policy.Entry"), tool, tw, true);
3123             td.displayConfirmRemovePolicyEntry();
3124 
3125         } else if (PolicyTool.collator.compare(e.getActionCommand(),
3126                                  ToolWindow.EDIT_POLICY_ENTRY) == 0) {
3127 
3128             // get the selected entry
3129             @SuppressWarnings("unchecked")
3130             JList<String> list = (JList<String>)tw.getComponent(ToolWindow.MW_POLICY_LIST);
3131             int index = list.getSelectedIndex();
3132             if (index < 0) {
3133                 tw.displayErrorDialog(null, new Exception
3134                         (PolicyTool.getMessage("No.Policy.Entry.selected")));
3135                 return;
3136             }
3137 
3138             // display the permission list for a policy entry
3139             ToolDialog td = new ToolDialog
3140                 (PolicyTool.getMessage("Policy.Entry"), tool, tw, true);
3141             td.displayPolicyEntryDialog(true);
3142 
3143         } else if (PolicyTool.collator.compare(e.getActionCommand(),
3144                                      ToolWindow.EDIT_KEYSTORE) == 0) {
3145 
3146             // display a dialog box for the user to enter keystore info
3147             ToolDialog td = new ToolDialog
3148                 (PolicyTool.getMessage("KeyStore"), tool, tw, true);
3149             td.keyStoreDialog(ToolDialog.EDIT_KEYSTORE);
3150         }
3151     }
3152 }
3153 
3154 /**
3155  * Event handler for AddEntryDoneButton button
3156  *
3157  * -- if edit is TRUE, then we are EDITing an existing PolicyEntry
3158  *    and we need to update both the policy and the GUI listing.
3159  *    if edit is FALSE, then we are ADDing a new PolicyEntry,
3160  *    so we only need to update the GUI listing.
3161  */
3162 class AddEntryDoneButtonListener implements ActionListener {
3163 
3164     private PolicyTool tool;
3165     private ToolWindow tw;
3166     private ToolDialog td;
3167     private boolean edit;
3168 
3169     AddEntryDoneButtonListener(PolicyTool tool, ToolWindow tw,
3170                                 ToolDialog td, boolean edit) {
3171         this.tool = tool;
3172         this.tw = tw;
3173         this.td = td;
3174         this.edit = edit;
3175     }
3176 
3177     public void actionPerformed(ActionEvent e) {
3178 
3179         try {
3180             // get a PolicyEntry object from the dialog policy info
3181             PolicyEntry newEntry = td.getPolicyEntryFromDialog();
3182             PolicyParser.GrantEntry newGe = newEntry.getGrantEntry();
3183 
3184             // see if all the signers have public keys
3185             if (newGe.signedBy != null) {
3186                 String signers[] = tool.parseSigners(newGe.signedBy);
3187                 for (int i = 0; i < signers.length; i++) {
3188                     PublicKey pubKey = tool.getPublicKeyAlias(signers[i]);
3189                     if (pubKey == null) {
3190                         MessageFormat form = new MessageFormat
3191                             (PolicyTool.getMessage
3192                             ("Warning.A.public.key.for.alias.signers.i.does.not.exist.Make.sure.a.KeyStore.is.properly.configured."));
3193                         Object[] source = {signers[i]};
3194                         tool.warnings.addElement(form.format(source));
3195                         tw.displayStatusDialog(td, form.format(source));
3196                     }
3197                 }
3198             }
3199 
3200             // add the entry
3201             @SuppressWarnings("unchecked")
3202             JList<String> policyList = (JList<String>)tw.getComponent(ToolWindow.MW_POLICY_LIST);
3203             if (edit) {
3204                 int listIndex = policyList.getSelectedIndex();
3205                 tool.addEntry(newEntry, listIndex);
3206                 String newCodeBaseStr = newEntry.headerToString();
3207                 if (PolicyTool.collator.compare
3208                         (newCodeBaseStr, policyList.getModel().getElementAt(listIndex)) != 0)
3209                     tool.modified = true;
3210                 ((DefaultListModel<String>)policyList.getModel()).set(listIndex, newCodeBaseStr);
3211             } else {
3212                 tool.addEntry(newEntry, -1);
3213                 ((DefaultListModel<String>)policyList.getModel()).addElement(newEntry.headerToString());
3214                 tool.modified = true;
3215             }
3216             td.setVisible(false);
3217             td.dispose();
3218 
3219         } catch (Exception eee) {
3220             tw.displayErrorDialog(td, eee);
3221         }
3222     }
3223 }
3224 
3225 /**
3226  * Event handler for ChangeKeyStoreOKButton button
3227  */
3228 class ChangeKeyStoreOKButtonListener implements ActionListener {
3229 
3230     private PolicyTool tool;
3231     private ToolWindow tw;
3232     private ToolDialog td;
3233 
3234     ChangeKeyStoreOKButtonListener(PolicyTool tool, ToolWindow tw,
3235                 ToolDialog td) {
3236         this.tool = tool;
3237         this.tw = tw;
3238         this.td = td;
3239     }
3240 
3241     public void actionPerformed(ActionEvent e) {
3242 
3243         String URLString = ((JTextField)td.getComponent(
3244                 ToolDialog.KSD_NAME_TEXTFIELD)).getText().trim();
3245         String type = ((JTextField)td.getComponent(
3246                 ToolDialog.KSD_TYPE_TEXTFIELD)).getText().trim();
3247         String provider = ((JTextField)td.getComponent(
3248                 ToolDialog.KSD_PROVIDER_TEXTFIELD)).getText().trim();
3249         String pwdURL = ((JTextField)td.getComponent(
3250                 ToolDialog.KSD_PWD_URL_TEXTFIELD)).getText().trim();
3251 
3252         try {
3253             tool.openKeyStore
3254                         ((URLString.length() == 0 ? null : URLString),
3255                         (type.length() == 0 ? null : type),
3256                         (provider.length() == 0 ? null : provider),
3257                         (pwdURL.length() == 0 ? null : pwdURL));
3258             tool.modified = true;
3259         } catch (Exception ex) {
3260             MessageFormat form = new MessageFormat(PolicyTool.getMessage
3261                 ("Unable.to.open.KeyStore.ex.toString."));
3262             Object[] source = {ex.toString()};
3263             tw.displayErrorDialog(td, form.format(source));
3264             return;
3265         }
3266 
3267         td.dispose();
3268     }
3269 }
3270 
3271 /**
3272  * Event handler for AddPrinButton button
3273  */
3274 class AddPrinButtonListener implements ActionListener {
3275 
3276     private PolicyTool tool;
3277     private ToolWindow tw;
3278     private ToolDialog td;
3279     private boolean editPolicyEntry;
3280 
3281     AddPrinButtonListener(PolicyTool tool, ToolWindow tw,
3282                                 ToolDialog td, boolean editPolicyEntry) {
3283         this.tool = tool;
3284         this.tw = tw;
3285         this.td = td;
3286         this.editPolicyEntry = editPolicyEntry;
3287     }
3288 
3289     public void actionPerformed(ActionEvent e) {
3290 
3291         // display a dialog box for the user to enter principal info
3292         td.displayPrincipalDialog(editPolicyEntry, false);
3293     }
3294 }
3295 
3296 /**
3297  * Event handler for AddPermButton button
3298  */
3299 class AddPermButtonListener implements ActionListener {
3300 
3301     private PolicyTool tool;
3302     private ToolWindow tw;
3303     private ToolDialog td;
3304     private boolean editPolicyEntry;
3305 
3306     AddPermButtonListener(PolicyTool tool, ToolWindow tw,
3307                                 ToolDialog td, boolean editPolicyEntry) {
3308         this.tool = tool;
3309         this.tw = tw;
3310         this.td = td;
3311         this.editPolicyEntry = editPolicyEntry;
3312     }
3313 
3314     public void actionPerformed(ActionEvent e) {
3315 
3316         // display a dialog box for the user to enter permission info
3317         td.displayPermissionDialog(editPolicyEntry, false);
3318     }
3319 }
3320 
3321 /**
3322  * Event handler for AddPrinOKButton button
3323  */
3324 class NewPolicyPrinOKButtonListener implements ActionListener {
3325 
3326     private PolicyTool tool;
3327     private ToolWindow tw;
3328     private ToolDialog listDialog;
3329     private ToolDialog infoDialog;
3330     private boolean edit;
3331 
3332     NewPolicyPrinOKButtonListener(PolicyTool tool,
3333                                 ToolWindow tw,
3334                                 ToolDialog listDialog,
3335                                 ToolDialog infoDialog,
3336                                 boolean edit) {
3337         this.tool = tool;
3338         this.tw = tw;
3339         this.listDialog = listDialog;
3340         this.infoDialog = infoDialog;
3341         this.edit = edit;
3342     }
3343 
3344     public void actionPerformed(ActionEvent e) {
3345 
3346         try {
3347             // read in the new principal info from Dialog Box
3348             PolicyParser.PrincipalEntry pppe =
3349                         infoDialog.getPrinFromDialog();
3350             if (pppe != null) {
3351                 try {
3352                     tool.verifyPrincipal(pppe.getPrincipalClass(),
3353                                         pppe.getPrincipalName());
3354                 } catch (ClassNotFoundException cnfe) {
3355                     MessageFormat form = new MessageFormat
3356                                 (PolicyTool.getMessage
3357                                 ("Warning.Class.not.found.class"));
3358                     Object[] source = {pppe.getPrincipalClass()};
3359                     tool.warnings.addElement(form.format(source));
3360                     tw.displayStatusDialog(infoDialog, form.format(source));
3361                 }
3362 
3363                 // add the principal to the GUI principal list
3364                 TaggedList prinList =
3365                     (TaggedList)listDialog.getComponent(ToolDialog.PE_PRIN_LIST);
3366 
3367                 String prinString = ToolDialog.PrincipalEntryToUserFriendlyString(pppe);
3368                 if (edit) {
3369                     // if editing, replace the original principal
3370                     int index = prinList.getSelectedIndex();
3371                     prinList.replaceTaggedItem(prinString, pppe, index);
3372                 } else {
3373                     // if adding, just add it to the end
3374                     prinList.addTaggedItem(prinString, pppe);
3375                 }
3376             }
3377             infoDialog.dispose();
3378         } catch (Exception ee) {
3379             tw.displayErrorDialog(infoDialog, ee);
3380         }
3381     }
3382 }
3383 
3384 /**
3385  * Event handler for AddPermOKButton button
3386  */
3387 class NewPolicyPermOKButtonListener implements ActionListener {
3388 
3389     private PolicyTool tool;
3390     private ToolWindow tw;
3391     private ToolDialog listDialog;
3392     private ToolDialog infoDialog;
3393     private boolean edit;
3394 
3395     NewPolicyPermOKButtonListener(PolicyTool tool,
3396                                 ToolWindow tw,
3397                                 ToolDialog listDialog,
3398                                 ToolDialog infoDialog,
3399                                 boolean edit) {
3400         this.tool = tool;
3401         this.tw = tw;
3402         this.listDialog = listDialog;
3403         this.infoDialog = infoDialog;
3404         this.edit = edit;
3405     }
3406 
3407     public void actionPerformed(ActionEvent e) {
3408 
3409         try {
3410             // read in the new permission info from Dialog Box
3411             PolicyParser.PermissionEntry pppe =
3412                         infoDialog.getPermFromDialog();
3413 
3414             try {
3415                 tool.verifyPermission(pppe.permission, pppe.name, pppe.action);
3416             } catch (ClassNotFoundException cnfe) {
3417                 MessageFormat form = new MessageFormat(PolicyTool.getMessage
3418                                 ("Warning.Class.not.found.class"));
3419                 Object[] source = {pppe.permission};
3420                 tool.warnings.addElement(form.format(source));
3421                 tw.displayStatusDialog(infoDialog, form.format(source));
3422             }
3423 
3424             // add the permission to the GUI permission list
3425             TaggedList permList =
3426                 (TaggedList)listDialog.getComponent(ToolDialog.PE_PERM_LIST);
3427 
3428             String permString = ToolDialog.PermissionEntryToUserFriendlyString(pppe);
3429             if (edit) {
3430                 // if editing, replace the original permission
3431                 int which = permList.getSelectedIndex();
3432                 permList.replaceTaggedItem(permString, pppe, which);
3433             } else {
3434                 // if adding, just add it to the end
3435                 permList.addTaggedItem(permString, pppe);
3436             }
3437             infoDialog.dispose();
3438 
3439         } catch (InvocationTargetException ite) {
3440             tw.displayErrorDialog(infoDialog, ite.getTargetException());
3441         } catch (Exception ee) {
3442             tw.displayErrorDialog(infoDialog, ee);
3443         }
3444     }
3445 }
3446 
3447 /**
3448  * Event handler for RemovePrinButton button
3449  */
3450 class RemovePrinButtonListener implements ActionListener {
3451 
3452     private PolicyTool tool;
3453     private ToolWindow tw;
3454     private ToolDialog td;
3455     private boolean edit;
3456 
3457     RemovePrinButtonListener(PolicyTool tool, ToolWindow tw,
3458                                 ToolDialog td, boolean edit) {
3459         this.tool = tool;
3460         this.tw = tw;
3461         this.td = td;
3462         this.edit = edit;
3463     }
3464 
3465     public void actionPerformed(ActionEvent e) {
3466 
3467         // get the Principal selected from the Principal List
3468         TaggedList prinList = (TaggedList)td.getComponent(
3469                 ToolDialog.PE_PRIN_LIST);
3470         int prinIndex = prinList.getSelectedIndex();
3471 
3472         if (prinIndex < 0) {
3473             tw.displayErrorDialog(td, new Exception
3474                 (PolicyTool.getMessage("No.principal.selected")));
3475             return;
3476         }
3477         // remove the principal from the display
3478         prinList.removeTaggedItem(prinIndex);
3479     }
3480 }
3481 
3482 /**
3483  * Event handler for RemovePermButton button
3484  */
3485 class RemovePermButtonListener implements ActionListener {
3486 
3487     private PolicyTool tool;
3488     private ToolWindow tw;
3489     private ToolDialog td;
3490     private boolean edit;
3491 
3492     RemovePermButtonListener(PolicyTool tool, ToolWindow tw,
3493                                 ToolDialog td, boolean edit) {
3494         this.tool = tool;
3495         this.tw = tw;
3496         this.td = td;
3497         this.edit = edit;
3498     }
3499 
3500     public void actionPerformed(ActionEvent e) {
3501 
3502         // get the Permission selected from the Permission List
3503         TaggedList permList = (TaggedList)td.getComponent(
3504                 ToolDialog.PE_PERM_LIST);
3505         int permIndex = permList.getSelectedIndex();
3506 
3507         if (permIndex < 0) {
3508             tw.displayErrorDialog(td, new Exception
3509                 (PolicyTool.getMessage("No.permission.selected")));
3510             return;
3511         }
3512         // remove the permission from the display
3513         permList.removeTaggedItem(permIndex);
3514 
3515     }
3516 }
3517 
3518 /**
3519  * Event handler for Edit Principal button
3520  *
3521  * We need the editPolicyEntry boolean to tell us if the user is
3522  * adding a new PolicyEntry at this time, or editing an existing entry.
3523  * If the user is adding a new PolicyEntry, we ONLY update the
3524  * GUI listing.  If the user is editing an existing PolicyEntry, we
3525  * update both the GUI listing and the actual PolicyEntry.
3526  */
3527 class EditPrinButtonListener extends MouseAdapter implements ActionListener {
3528 
3529     private PolicyTool tool;
3530     private ToolWindow tw;
3531     private ToolDialog td;
3532     private boolean editPolicyEntry;
3533 
3534     EditPrinButtonListener(PolicyTool tool, ToolWindow tw,
3535                                 ToolDialog td, boolean editPolicyEntry) {
3536         this.tool = tool;
3537         this.tw = tw;
3538         this.td = td;
3539         this.editPolicyEntry = editPolicyEntry;
3540     }
3541 
3542     public void actionPerformed(ActionEvent e) {
3543 
3544         // get the Principal selected from the Principal List
3545         TaggedList list = (TaggedList)td.getComponent(
3546                 ToolDialog.PE_PRIN_LIST);
3547         int prinIndex = list.getSelectedIndex();
3548 
3549         if (prinIndex < 0) {
3550             tw.displayErrorDialog(td, new Exception
3551                 (PolicyTool.getMessage("No.principal.selected")));
3552             return;
3553         }
3554         td.displayPrincipalDialog(editPolicyEntry, true);
3555     }
3556 
3557     public void mouseClicked(MouseEvent evt) {
3558         if (evt.getClickCount() == 2) {
3559             actionPerformed(null);
3560         }
3561     }
3562 }
3563 
3564 /**
3565  * Event handler for Edit Permission button
3566  *
3567  * We need the editPolicyEntry boolean to tell us if the user is
3568  * adding a new PolicyEntry at this time, or editing an existing entry.
3569  * If the user is adding a new PolicyEntry, we ONLY update the
3570  * GUI listing.  If the user is editing an existing PolicyEntry, we
3571  * update both the GUI listing and the actual PolicyEntry.
3572  */
3573 class EditPermButtonListener extends MouseAdapter implements ActionListener {
3574 
3575     private PolicyTool tool;
3576     private ToolWindow tw;
3577     private ToolDialog td;
3578     private boolean editPolicyEntry;
3579 
3580     EditPermButtonListener(PolicyTool tool, ToolWindow tw,
3581                                 ToolDialog td, boolean editPolicyEntry) {
3582         this.tool = tool;
3583         this.tw = tw;
3584         this.td = td;
3585         this.editPolicyEntry = editPolicyEntry;
3586     }
3587 
3588     public void actionPerformed(ActionEvent e) {
3589 
3590         // get the Permission selected from the Permission List
3591         @SuppressWarnings("unchecked")
3592         JList<String> list = (JList<String>)td.getComponent(ToolDialog.PE_PERM_LIST);
3593         int permIndex = list.getSelectedIndex();
3594 
3595         if (permIndex < 0) {
3596             tw.displayErrorDialog(td, new Exception
3597                 (PolicyTool.getMessage("No.permission.selected")));
3598             return;
3599         }
3600         td.displayPermissionDialog(editPolicyEntry, true);
3601     }
3602 
3603     public void mouseClicked(MouseEvent evt) {
3604         if (evt.getClickCount() == 2) {
3605             actionPerformed(null);
3606         }
3607     }
3608 }
3609 
3610 /**
3611  * Event handler for Principal Popup Menu
3612  */
3613 class PrincipalTypeMenuListener implements ItemListener {
3614 
3615     private ToolDialog td;
3616 
3617     PrincipalTypeMenuListener(ToolDialog td) {
3618         this.td = td;
3619     }
3620 
3621     public void itemStateChanged(ItemEvent e) {
3622         if (e.getStateChange() == ItemEvent.DESELECTED) {
3623             // We're only interested in SELECTED events
3624             return;
3625         }
3626 
3627         @SuppressWarnings("unchecked")
3628         JComboBox<String> prin = (JComboBox<String>)td.getComponent(ToolDialog.PRD_PRIN_CHOICE);
3629         JTextField prinField = (JTextField)td.getComponent(
3630                 ToolDialog.PRD_PRIN_TEXTFIELD);
3631         JTextField nameField = (JTextField)td.getComponent(
3632                 ToolDialog.PRD_NAME_TEXTFIELD);
3633 
3634         prin.getAccessibleContext().setAccessibleName(
3635             PolicyTool.splitToWords((String)e.getItem()));
3636         if (((String)e.getItem()).equals(ToolDialog.PRIN_TYPE)) {
3637             // ignore if they choose "Principal Type:" item
3638             if (prinField.getText() != null &&
3639                 prinField.getText().length() > 0) {
3640                 Prin inputPrin = ToolDialog.getPrin(prinField.getText(), true);
3641                 prin.setSelectedItem(inputPrin.CLASS);
3642             }
3643             return;
3644         }
3645 
3646         // if you change the principal, clear the name
3647         if (prinField.getText().indexOf((String)e.getItem()) == -1) {
3648             nameField.setText("");
3649         }
3650 
3651         // set the text in the textfield and also modify the
3652         // pull-down choice menus to reflect the correct possible
3653         // set of names and actions
3654         Prin inputPrin = ToolDialog.getPrin((String)e.getItem(), false);
3655         if (inputPrin != null) {
3656             prinField.setText(inputPrin.FULL_CLASS);
3657         }
3658     }
3659 }
3660 
3661 /**
3662  * Event handler for Permission Popup Menu
3663  */
3664 class PermissionMenuListener implements ItemListener {
3665 
3666     private ToolDialog td;
3667 
3668     PermissionMenuListener(ToolDialog td) {
3669         this.td = td;
3670     }
3671 
3672     public void itemStateChanged(ItemEvent e) {
3673         if (e.getStateChange() == ItemEvent.DESELECTED) {
3674             // We're only interested in SELECTED events
3675             return;
3676         }
3677 
3678         @SuppressWarnings("unchecked")
3679         JComboBox<String> perms = (JComboBox<String>)td.getComponent(
3680                 ToolDialog.PD_PERM_CHOICE);
3681         @SuppressWarnings("unchecked")
3682         JComboBox<String> names = (JComboBox<String>)td.getComponent(
3683                 ToolDialog.PD_NAME_CHOICE);
3684         @SuppressWarnings("unchecked")
3685         JComboBox<String> actions = (JComboBox<String>)td.getComponent(
3686                 ToolDialog.PD_ACTIONS_CHOICE);
3687         JTextField nameField = (JTextField)td.getComponent(
3688                 ToolDialog.PD_NAME_TEXTFIELD);
3689         JTextField actionsField = (JTextField)td.getComponent(
3690                 ToolDialog.PD_ACTIONS_TEXTFIELD);
3691         JTextField permField = (JTextField)td.getComponent(
3692                 ToolDialog.PD_PERM_TEXTFIELD);
3693         JTextField signedbyField = (JTextField)td.getComponent(
3694                 ToolDialog.PD_SIGNEDBY_TEXTFIELD);
3695 
3696         perms.getAccessibleContext().setAccessibleName(
3697             PolicyTool.splitToWords((String)e.getItem()));
3698 
3699         // ignore if they choose the 'Permission:' item
3700         if (PolicyTool.collator.compare((String)e.getItem(),
3701                                       ToolDialog.PERM) == 0) {
3702             if (permField.getText() != null &&
3703                 permField.getText().length() > 0) {
3704 
3705                 Perm inputPerm = ToolDialog.getPerm(permField.getText(), true);
3706                 if (inputPerm != null) {
3707                     perms.setSelectedItem(inputPerm.CLASS);
3708                 }
3709             }
3710             return;
3711         }
3712 
3713         // if you change the permission, clear the name, actions, and signedBy
3714         if (permField.getText().indexOf((String)e.getItem()) == -1) {
3715             nameField.setText("");
3716             actionsField.setText("");
3717             signedbyField.setText("");
3718         }
3719 
3720         // set the text in the textfield and also modify the
3721         // pull-down choice menus to reflect the correct possible
3722         // set of names and actions
3723 
3724         Perm inputPerm = ToolDialog.getPerm((String)e.getItem(), false);
3725         if (inputPerm == null) {
3726             permField.setText("");
3727         } else {
3728             permField.setText(inputPerm.FULL_CLASS);
3729         }
3730         td.setPermissionNames(inputPerm, names, nameField);
3731         td.setPermissionActions(inputPerm, actions, actionsField);
3732     }
3733 }
3734 
3735 /**
3736  * Event handler for Permission Name Popup Menu
3737  */
3738 class PermissionNameMenuListener implements ItemListener {
3739 
3740     private ToolDialog td;
3741 
3742     PermissionNameMenuListener(ToolDialog td) {
3743         this.td = td;
3744     }
3745 
3746     public void itemStateChanged(ItemEvent e) {
3747         if (e.getStateChange() == ItemEvent.DESELECTED) {
3748             // We're only interested in SELECTED events
3749             return;
3750         }
3751 
3752         @SuppressWarnings("unchecked")
3753         JComboBox<String> names = (JComboBox<String>)td.getComponent(ToolDialog.PD_NAME_CHOICE);
3754         names.getAccessibleContext().setAccessibleName(
3755             PolicyTool.splitToWords((String)e.getItem()));
3756 
3757         if (((String)e.getItem()).indexOf(ToolDialog.PERM_NAME) != -1)
3758             return;
3759 
3760         JTextField tf = (JTextField)td.getComponent(ToolDialog.PD_NAME_TEXTFIELD);
3761         tf.setText((String)e.getItem());
3762     }
3763 }
3764 
3765 /**
3766  * Event handler for Permission Actions Popup Menu
3767  */
3768 class PermissionActionsMenuListener implements ItemListener {
3769 
3770     private ToolDialog td;
3771 
3772     PermissionActionsMenuListener(ToolDialog td) {
3773         this.td = td;
3774     }
3775 
3776     public void itemStateChanged(ItemEvent e) {
3777         if (e.getStateChange() == ItemEvent.DESELECTED) {
3778             // We're only interested in SELECTED events
3779             return;
3780         }
3781 
3782         @SuppressWarnings("unchecked")
3783         JComboBox<String> actions = (JComboBox<String>)td.getComponent(
3784                 ToolDialog.PD_ACTIONS_CHOICE);
3785         actions.getAccessibleContext().setAccessibleName((String)e.getItem());
3786 
3787         if (((String)e.getItem()).indexOf(ToolDialog.PERM_ACTIONS) != -1)
3788             return;
3789 
3790         JTextField tf = (JTextField)td.getComponent(
3791                 ToolDialog.PD_ACTIONS_TEXTFIELD);
3792         if (tf.getText() == null || tf.getText().equals("")) {
3793             tf.setText((String)e.getItem());
3794         } else {
3795             if (tf.getText().indexOf((String)e.getItem()) == -1)
3796                 tf.setText(tf.getText() + ", " + (String)e.getItem());
3797         }
3798     }
3799 }
3800 
3801 /**
3802  * Event handler for all the children dialogs/windows
3803  */
3804 class ChildWindowListener implements WindowListener {
3805 
3806     private ToolDialog td;
3807 
3808     ChildWindowListener(ToolDialog td) {
3809         this.td = td;
3810     }
3811 
3812     public void windowOpened(WindowEvent we) {
3813     }
3814 
3815     public void windowClosing(WindowEvent we) {
3816         // same as pressing the "cancel" button
3817         td.setVisible(false);
3818         td.dispose();
3819     }
3820 
3821     public void windowClosed(WindowEvent we) {
3822     }
3823 
3824     public void windowIconified(WindowEvent we) {
3825     }
3826 
3827     public void windowDeiconified(WindowEvent we) {
3828     }
3829 
3830     public void windowActivated(WindowEvent we) {
3831     }
3832 
3833     public void windowDeactivated(WindowEvent we) {
3834     }
3835 }
3836 
3837 /**
3838  * Event handler for CancelButton button
3839  */
3840 class CancelButtonListener implements ActionListener {
3841 
3842     private ToolDialog td;
3843 
3844     CancelButtonListener(ToolDialog td) {
3845         this.td = td;
3846     }
3847 
3848     public void actionPerformed(ActionEvent e) {
3849         td.setVisible(false);
3850         td.dispose();
3851     }
3852 }
3853 
3854 /**
3855  * Event handler for ErrorOKButton button
3856  */
3857 class ErrorOKButtonListener implements ActionListener {
3858 
3859     private ToolDialog ed;
3860 
3861     ErrorOKButtonListener(ToolDialog ed) {
3862         this.ed = ed;
3863     }
3864 
3865     public void actionPerformed(ActionEvent e) {
3866         ed.setVisible(false);
3867         ed.dispose();
3868     }
3869 }
3870 
3871 /**
3872  * Event handler for StatusOKButton button
3873  */
3874 class StatusOKButtonListener implements ActionListener {
3875 
3876     private ToolDialog sd;
3877 
3878     StatusOKButtonListener(ToolDialog sd) {
3879         this.sd = sd;
3880     }
3881 
3882     public void actionPerformed(ActionEvent e) {
3883         sd.setVisible(false);
3884         sd.dispose();
3885     }
3886 }
3887 
3888 /**
3889  * Event handler for UserSaveYes button
3890  */
3891 class UserSaveYesButtonListener implements ActionListener {
3892 
3893     private ToolDialog us;
3894     private PolicyTool tool;
3895     private ToolWindow tw;
3896     private int select;
3897 
3898     UserSaveYesButtonListener(ToolDialog us, PolicyTool tool,
3899                         ToolWindow tw, int select) {
3900         this.us = us;
3901         this.tool = tool;
3902         this.tw = tw;
3903         this.select = select;
3904     }
3905 
3906     public void actionPerformed(ActionEvent e) {
3907 
3908         // first get rid of the window
3909         us.setVisible(false);
3910         us.dispose();
3911 
3912         try {
3913             String filename = ((JTextField)tw.getComponent(
3914                     ToolWindow.MW_FILENAME_TEXTFIELD)).getText();
3915             if (filename == null || filename.equals("")) {
3916                 us.displaySaveAsDialog(select);
3917 
3918                 // the above dialog will continue with the originally
3919                 // requested command if necessary
3920             } else {
3921                 // save the policy entries to a file
3922                 tool.savePolicy(filename);
3923 
3924                 // display status
3925                 MessageFormat form = new MessageFormat
3926                         (PolicyTool.getMessage
3927                         ("Policy.successfully.written.to.filename"));
3928                 Object[] source = {filename};
3929                 tw.displayStatusDialog(null, form.format(source));
3930 
3931                 // now continue with the originally requested command
3932                 // (QUIT, NEW, or OPEN)
3933                 us.userSaveContinue(tool, tw, us, select);
3934             }
3935         } catch (Exception ee) {
3936             // error -- just report it and bail
3937             tw.displayErrorDialog(null, ee);
3938         }
3939     }
3940 }
3941 
3942 /**
3943  * Event handler for UserSaveNoButton
3944  */
3945 class UserSaveNoButtonListener implements ActionListener {
3946 
3947     private PolicyTool tool;
3948     private ToolWindow tw;
3949     private ToolDialog us;
3950     private int select;
3951 
3952     UserSaveNoButtonListener(ToolDialog us, PolicyTool tool,
3953                         ToolWindow tw, int select) {
3954         this.us = us;
3955         this.tool = tool;
3956         this.tw = tw;
3957         this.select = select;
3958     }
3959 
3960     public void actionPerformed(ActionEvent e) {
3961         us.setVisible(false);
3962         us.dispose();
3963 
3964         // now continue with the originally requested command
3965         // (QUIT, NEW, or OPEN)
3966         us.userSaveContinue(tool, tw, us, select);
3967     }
3968 }
3969 
3970 /**
3971  * Event handler for UserSaveCancelButton
3972  */
3973 class UserSaveCancelButtonListener implements ActionListener {
3974 
3975     private ToolDialog us;
3976 
3977     UserSaveCancelButtonListener(ToolDialog us) {
3978         this.us = us;
3979     }
3980 
3981     public void actionPerformed(ActionEvent e) {
3982         us.setVisible(false);
3983         us.dispose();
3984 
3985         // do NOT continue with the originally requested command
3986         // (QUIT, NEW, or OPEN)
3987     }
3988 }
3989 
3990 /**
3991  * Event handler for ConfirmRemovePolicyEntryOKButtonListener
3992  */
3993 class ConfirmRemovePolicyEntryOKButtonListener implements ActionListener {
3994 
3995     private PolicyTool tool;
3996     private ToolWindow tw;
3997     private ToolDialog us;
3998 
3999     ConfirmRemovePolicyEntryOKButtonListener(PolicyTool tool,
4000                                 ToolWindow tw, ToolDialog us) {
4001         this.tool = tool;
4002         this.tw = tw;
4003         this.us = us;
4004     }
4005 
4006     public void actionPerformed(ActionEvent e) {
4007         // remove the entry
4008         @SuppressWarnings("unchecked")
4009         JList<String> list = (JList<String>)tw.getComponent(ToolWindow.MW_POLICY_LIST);
4010         int index = list.getSelectedIndex();
4011         PolicyEntry entries[] = tool.getEntry();
4012         tool.removeEntry(entries[index]);
4013 
4014         // redraw the window listing
4015         DefaultListModel<String> listModel = new DefaultListModel<>();
4016         list = new JList<>(listModel);
4017         list.setVisibleRowCount(15);
4018         list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
4019         list.addMouseListener(new PolicyListListener(tool, tw));
4020         entries = tool.getEntry();
4021         if (entries != null) {
4022                 for (int i = 0; i < entries.length; i++) {
4023                     listModel.addElement(entries[i].headerToString());
4024                 }
4025         }
4026         tw.replacePolicyList(list);
4027         us.setVisible(false);
4028         us.dispose();
4029     }
4030 }
4031 
4032 /**
4033  * Just a special name, so that the codes dealing with this exception knows
4034  * it's special, and does not pop out a warning box.
4035  */
4036 class NoDisplayException extends RuntimeException {
4037     private static final long serialVersionUID = -4611761427108719794L;
4038 }
4039 
4040 /**
4041  * This is a java.awt.List that bind an Object to each String it holds.
4042  */
4043 class TaggedList extends JList<String> {
4044     private static final long serialVersionUID = -5676238110427785853L;
4045 
4046     private java.util.List<Object> data = new LinkedList<>();
4047     public TaggedList(int i, boolean b) {
4048         super(new DefaultListModel<>());
4049         setVisibleRowCount(i);
4050         setSelectionMode(b ? ListSelectionModel.MULTIPLE_INTERVAL_SELECTION : ListSelectionModel.SINGLE_SELECTION);
4051     }
4052 
4053     public Object getObject(int index) {
4054         return data.get(index);
4055     }
4056 
4057     public void addTaggedItem(String string, Object object) {
4058         ((DefaultListModel<String>)getModel()).addElement(string);
4059         data.add(object);
4060     }
4061 
4062     public void replaceTaggedItem(String string, Object object, int index) {
4063         ((DefaultListModel<String>)getModel()).set(index, string);
4064         data.set(index, object);
4065     }
4066 
4067     public void removeTaggedItem(int index) {
4068         ((DefaultListModel<String>)getModel()).remove(index);
4069         data.remove(index);
4070     }
4071 }
4072 
4073 /**
4074  * Convenience Principal Classes
4075  */
4076 
4077 class Prin {
4078     public final String CLASS;
4079     public final String FULL_CLASS;
4080 
4081     public Prin(String clazz, String fullClass) {
4082         this.CLASS = clazz;
4083         this.FULL_CLASS = fullClass;
4084     }
4085 }
4086 
4087 class KrbPrin extends Prin {
4088     public KrbPrin() {
4089         super("KerberosPrincipal",
4090                 "javax.security.auth.kerberos.KerberosPrincipal");
4091     }
4092 }
4093 
4094 class X500Prin extends Prin {
4095     public X500Prin() {
4096         super("X500Principal",
4097                 "javax.security.auth.x500.X500Principal");
4098     }
4099 }
4100 
4101 /**
4102  * Convenience Permission Classes
4103  */
4104 
4105 class Perm {
4106     public final String CLASS;
4107     public final String FULL_CLASS;
4108     public final String[] TARGETS;
4109     public final String[] ACTIONS;
4110 
4111     public Perm(String clazz, String fullClass,
4112                 String[] targets, String[] actions) {
4113 
4114         this.CLASS = clazz;
4115         this.FULL_CLASS = fullClass;
4116         this.TARGETS = targets;
4117         this.ACTIONS = actions;
4118     }
4119 }
4120 
4121 class AllPerm extends Perm {
4122     public AllPerm() {
4123         super("AllPermission", "java.security.AllPermission", null, null);
4124     }
4125 }
4126 
4127 class AudioPerm extends Perm {
4128     public AudioPerm() {
4129         super("AudioPermission",
4130         "javax.sound.sampled.AudioPermission",
4131         new String[]    {
4132                 "play",
4133                 "record"
4134                 },
4135         null);
4136     }
4137 }
4138 
4139 class AuthPerm extends Perm {
4140     public AuthPerm() {
4141     super("AuthPermission",
4142         "javax.security.auth.AuthPermission",
4143         new String[]    {
4144                 "doAs",
4145                 "doAsPrivileged",
4146                 "getSubject",
4147                 "getSubjectFromDomainCombiner",
4148                 "setReadOnly",
4149                 "modifyPrincipals",
4150                 "modifyPublicCredentials",
4151                 "modifyPrivateCredentials",
4152                 "refreshCredential",
4153                 "destroyCredential",
4154                 "createLoginContext.<" + PolicyTool.getMessage("name") + ">",
4155                 "getLoginConfiguration",
4156                 "setLoginConfiguration",
4157                 "createLoginConfiguration.<" +
4158                         PolicyTool.getMessage("configuration.type") + ">",
4159                 "refreshLoginConfiguration"
4160                 },
4161         null);
4162     }
4163 }
4164 
4165 class AWTPerm extends Perm {
4166     public AWTPerm() {
4167     super("AWTPermission",
4168         "java.awt.AWTPermission",
4169         new String[]    {
4170                 "accessClipboard",
4171                 "accessEventQueue",
4172                 "accessSystemTray",
4173                 "createRobot",
4174                 "fullScreenExclusive",
4175                 "listenToAllAWTEvents",
4176                 "readDisplayPixels",
4177                 "replaceKeyboardFocusManager",
4178                 "setAppletStub",
4179                 "setWindowAlwaysOnTop",
4180                 "showWindowWithoutWarningBanner",
4181                 "toolkitModality",
4182                 "watchMousePointer"
4183         },
4184         null);
4185     }
4186 }
4187 
4188 class DelegationPerm extends Perm {
4189     public DelegationPerm() {
4190     super("DelegationPermission",
4191         "javax.security.auth.kerberos.DelegationPermission",
4192         new String[]    {
4193                 // allow user input
4194                 },
4195         null);
4196     }
4197 }
4198 
4199 class FilePerm extends Perm {
4200     public FilePerm() {
4201     super("FilePermission",
4202         "java.io.FilePermission",
4203         new String[]    {
4204                 "<<ALL FILES>>"
4205                 },
4206         new String[]    {
4207                 "read",
4208                 "write",
4209                 "delete",
4210                 "execute"
4211                 });
4212     }
4213 }
4214 
4215 class URLPerm extends Perm {
4216     public URLPerm() {
4217         super("URLPermission",
4218                 "java.net.URLPermission",
4219                 new String[]    {
4220                     "<"+ PolicyTool.getMessage("url") + ">",
4221                 },
4222                 new String[]    {
4223                     "<" + PolicyTool.getMessage("method.list") + ">:<"
4224                         + PolicyTool.getMessage("request.headers.list") + ">",
4225                 });
4226     }
4227 }
4228 
4229 class InqSecContextPerm extends Perm {
4230     public InqSecContextPerm() {
4231     super("InquireSecContextPermission",
4232         "com.sun.security.jgss.InquireSecContextPermission",
4233         new String[]    {
4234                 "KRB5_GET_SESSION_KEY",
4235                 "KRB5_GET_TKT_FLAGS",
4236                 "KRB5_GET_AUTHZ_DATA",
4237                 "KRB5_GET_AUTHTIME"
4238                 },
4239         null);
4240     }
4241 }
4242 
4243 class LogPerm extends Perm {
4244     public LogPerm() {
4245     super("LoggingPermission",
4246         "java.util.logging.LoggingPermission",
4247         new String[]    {
4248                 "control"
4249                 },
4250         null);
4251     }
4252 }
4253 
4254 class MgmtPerm extends Perm {
4255     public MgmtPerm() {
4256     super("ManagementPermission",
4257         "java.lang.management.ManagementPermission",
4258         new String[]    {
4259                 "control",
4260                 "monitor"
4261                 },
4262         null);
4263     }
4264 }
4265 
4266 class MBeanPerm extends Perm {
4267     public MBeanPerm() {
4268     super("MBeanPermission",
4269         "javax.management.MBeanPermission",
4270         new String[]    {
4271                 // allow user input
4272                 },
4273         new String[]    {
4274                 "addNotificationListener",
4275                 "getAttribute",
4276                 "getClassLoader",
4277                 "getClassLoaderFor",
4278                 "getClassLoaderRepository",
4279                 "getDomains",
4280                 "getMBeanInfo",
4281                 "getObjectInstance",
4282                 "instantiate",
4283                 "invoke",
4284                 "isInstanceOf",
4285                 "queryMBeans",
4286                 "queryNames",
4287                 "registerMBean",
4288                 "removeNotificationListener",
4289                 "setAttribute",
4290                 "unregisterMBean"
4291                 });
4292     }
4293 }
4294 
4295 class MBeanSvrPerm extends Perm {
4296     public MBeanSvrPerm() {
4297     super("MBeanServerPermission",
4298         "javax.management.MBeanServerPermission",
4299         new String[]    {
4300                 "createMBeanServer",
4301                 "findMBeanServer",
4302                 "newMBeanServer",
4303                 "releaseMBeanServer"
4304                 },
4305         null);
4306     }
4307 }
4308 
4309 class MBeanTrustPerm extends Perm {
4310     public MBeanTrustPerm() {
4311     super("MBeanTrustPermission",
4312         "javax.management.MBeanTrustPermission",
4313         new String[]    {
4314                 "register"
4315                 },
4316         null);
4317     }
4318 }
4319 
4320 class NetPerm extends Perm {
4321     public NetPerm() {
4322     super("NetPermission",
4323         "java.net.NetPermission",
4324         new String[]    {
4325                 "setDefaultAuthenticator",
4326                 "requestPasswordAuthentication",
4327                 "specifyStreamHandler",
4328                 "setProxySelector",
4329                 "getProxySelector",
4330                 "setCookieHandler",
4331                 "getCookieHandler",
4332                 "setResponseCache",
4333                 "getResponseCache"
4334                 },
4335         null);
4336     }
4337 }
4338 
4339 class PrivCredPerm extends Perm {
4340     public PrivCredPerm() {
4341     super("PrivateCredentialPermission",
4342         "javax.security.auth.PrivateCredentialPermission",
4343         new String[]    {
4344                 // allow user input
4345                 },
4346         new String[]    {
4347                 "read"
4348                 });
4349     }
4350 }
4351 
4352 class PropPerm extends Perm {
4353     public PropPerm() {
4354     super("PropertyPermission",
4355         "java.util.PropertyPermission",
4356         new String[]    {
4357                 // allow user input
4358                 },
4359         new String[]    {
4360                 "read",
4361                 "write"
4362                 });
4363     }
4364 }
4365 
4366 class ReflectPerm extends Perm {
4367     public ReflectPerm() {
4368     super("ReflectPermission",
4369         "java.lang.reflect.ReflectPermission",
4370         new String[]    {
4371                 "suppressAccessChecks"
4372                 },
4373         null);
4374     }
4375 }
4376 
4377 class RuntimePerm extends Perm {
4378     public RuntimePerm() {
4379     super("RuntimePermission",
4380         "java.lang.RuntimePermission",
4381         new String[]    {
4382                 "createClassLoader",
4383                 "getClassLoader",
4384                 "setContextClassLoader",
4385                 "enableContextClassLoaderOverride",
4386                 "setSecurityManager",
4387                 "createSecurityManager",
4388                 "getenv.<" +
4389                     PolicyTool.getMessage("environment.variable.name") + ">",
4390                 "exitVM",
4391                 "shutdownHooks",
4392                 "setFactory",
4393                 "setIO",
4394                 "modifyThread",
4395                 "stopThread",
4396                 "modifyThreadGroup",
4397                 "getProtectionDomain",
4398                 "readFileDescriptor",
4399                 "writeFileDescriptor",
4400                 "loadLibrary.<" +
4401                     PolicyTool.getMessage("library.name") + ">",
4402                 "accessClassInPackage.<" +
4403                     PolicyTool.getMessage("package.name")+">",
4404                 "defineClassInPackage.<" +
4405                     PolicyTool.getMessage("package.name")+">",
4406                 "accessDeclaredMembers",
4407                 "queuePrintJob",
4408                 "getStackTrace",
4409                 "setDefaultUncaughtExceptionHandler",
4410                 "preferences",
4411                 "usePolicy",
4412                 // "inheritedChannel"
4413                 },
4414         null);
4415     }
4416 }
4417 
4418 class SecurityPerm extends Perm {
4419     public SecurityPerm() {
4420     super("SecurityPermission",
4421         "java.security.SecurityPermission",
4422         new String[]    {
4423                 "createAccessControlContext",
4424                 "getDomainCombiner",
4425                 "getPolicy",
4426                 "setPolicy",
4427                 "createPolicy.<" +
4428                     PolicyTool.getMessage("policy.type") + ">",
4429                 "getProperty.<" +
4430                     PolicyTool.getMessage("property.name") + ">",
4431                 "setProperty.<" +
4432                     PolicyTool.getMessage("property.name") + ">",
4433                 "insertProvider.<" +
4434                     PolicyTool.getMessage("provider.name") + ">",
4435                 "removeProvider.<" +
4436                     PolicyTool.getMessage("provider.name") + ">",
4437                 //"setSystemScope",
4438                 //"setIdentityPublicKey",
4439                 //"setIdentityInfo",
4440                 //"addIdentityCertificate",
4441                 //"removeIdentityCertificate",
4442                 //"printIdentity",
4443                 "clearProviderProperties.<" +
4444                     PolicyTool.getMessage("provider.name") + ">",
4445                 "putProviderProperty.<" +
4446                     PolicyTool.getMessage("provider.name") + ">",
4447                 "removeProviderProperty.<" +
4448                     PolicyTool.getMessage("provider.name") + ">",
4449                 //"getSignerPrivateKey",
4450                 //"setSignerKeyPair"
4451                 },
4452         null);
4453     }
4454 }
4455 
4456 class SerialPerm extends Perm {
4457     public SerialPerm() {
4458     super("SerializablePermission",
4459         "java.io.SerializablePermission",
4460         new String[]    {
4461                 "enableSubclassImplementation",
4462                 "enableSubstitution"
4463                 },
4464         null);
4465     }
4466 }
4467 
4468 class ServicePerm extends Perm {
4469     public ServicePerm() {
4470     super("ServicePermission",
4471         "javax.security.auth.kerberos.ServicePermission",
4472         new String[]    {
4473                 // allow user input
4474                 },
4475         new String[]    {
4476                 "initiate",
4477                 "accept"
4478                 });
4479     }
4480 }
4481 
4482 class SocketPerm extends Perm {
4483     public SocketPerm() {
4484     super("SocketPermission",
4485         "java.net.SocketPermission",
4486         new String[]    {
4487                 // allow user input
4488                 },
4489         new String[]    {
4490                 "accept",
4491                 "connect",
4492                 "listen",
4493                 "resolve"
4494                 });
4495     }
4496 }
4497 
4498 class SQLPerm extends Perm {
4499     public SQLPerm() {
4500     super("SQLPermission",
4501         "java.sql.SQLPermission",
4502         new String[]    {
4503                 "setLog",
4504                 "callAbort",
4505                 "setSyncFactory",
4506                 "setNetworkTimeout",
4507                 },
4508         null);
4509     }
4510 }
4511 
4512 class SSLPerm extends Perm {
4513     public SSLPerm() {
4514     super("SSLPermission",
4515         "javax.net.ssl.SSLPermission",
4516         new String[]    {
4517                 "setHostnameVerifier",
4518                 "getSSLSessionContext"
4519                 },
4520         null);
4521     }
4522 }
4523 
4524 class SubjDelegPerm extends Perm {
4525     public SubjDelegPerm() {
4526     super("SubjectDelegationPermission",
4527         "javax.management.remote.SubjectDelegationPermission",
4528         new String[]    {
4529                 // allow user input
4530                 },
4531         null);
4532     }
4533 }