1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  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             StringBuffer buffer = new StringBuffer(200);
 979             ListIterator<PolicyParser.PrincipalEntry> list =
 980                                 grantEntry.principals.listIterator();
 981             while (list.hasNext()) {
 982                 PolicyParser.PrincipalEntry pppe = list.next();
 983                 buffer.append(" Principal " + pppe.getDisplayClass() + " " +
 984                     pppe.getDisplayName(true));
 985                 if (list.hasNext()) buffer.append(", ");
 986             }
 987             result = buffer.toString();
 988         }
 989         return result;
 990     }
 991 
 992     /**
 993      * convert this policy entry into a PolicyParser.PermissionEntry
 994      */
 995     PolicyParser.PermissionEntry toPermissionEntry(Permission perm) {
 996 
 997         String actions = null;
 998 
 999         // get the actions
1000         if (perm.getActions() != null &&
1001             perm.getActions().trim() != "")
1002                 actions = perm.getActions();
1003 
1004         PolicyParser.PermissionEntry pe = new PolicyParser.PermissionEntry
1005                         (perm.getClass().getName(),
1006                         perm.getName(),
1007                         actions);
1008         return pe;
1009     }
1010 }
1011 
1012 /**
1013  * The main window for the PolicyTool
1014  */
1015 class ToolWindow extends JFrame {
1016     // use serialVersionUID from JDK 1.2.2 for interoperability
1017     private static final long serialVersionUID = 5682568601210376777L;
1018 
1019     /* ESCAPE key */
1020     static final KeyStroke escKey = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
1021 
1022     /* external paddings */
1023     public static final Insets TOP_PADDING = new Insets(25,0,0,0);
1024     public static final Insets BOTTOM_PADDING = new Insets(0,0,25,0);
1025     public static final Insets LITE_BOTTOM_PADDING = new Insets(0,0,10,0);
1026     public static final Insets LR_PADDING = new Insets(0,10,0,10);
1027     public static final Insets TOP_BOTTOM_PADDING = new Insets(15, 0, 15, 0);
1028     public static final Insets L_TOP_BOTTOM_PADDING = new Insets(5,10,15,0);
1029     public static final Insets LR_TOP_BOTTOM_PADDING = new Insets(15, 4, 15, 4);
1030     public static final Insets LR_BOTTOM_PADDING = new Insets(0,10,5,10);
1031     public static final Insets L_BOTTOM_PADDING = new Insets(0,10,5,0);
1032     public static final Insets R_BOTTOM_PADDING = new Insets(0, 0, 25, 5);
1033     public static final Insets R_PADDING = new Insets(0, 0, 0, 5);
1034 
1035     /* buttons and menus */
1036     public static final String NEW_POLICY_FILE          = "New";
1037     public static final String OPEN_POLICY_FILE         = "Open";
1038     public static final String SAVE_POLICY_FILE         = "Save";
1039     public static final String SAVE_AS_POLICY_FILE      = "Save.As";
1040     public static final String VIEW_WARNINGS            = "View.Warning.Log";
1041     public static final String QUIT                     = "Exit";
1042     public static final String ADD_POLICY_ENTRY         = "Add.Policy.Entry";
1043     public static final String EDIT_POLICY_ENTRY        = "Edit.Policy.Entry";
1044     public static final String REMOVE_POLICY_ENTRY      = "Remove.Policy.Entry";
1045     public static final String EDIT_KEYSTORE            = "Edit";
1046     public static final String ADD_PUBKEY_ALIAS         = "Add.Public.Key.Alias";
1047     public static final String REMOVE_PUBKEY_ALIAS      = "Remove.Public.Key.Alias";
1048 
1049     /* gridbag index for components in the main window (MW) */
1050     public static final int MW_FILENAME_LABEL           = 0;
1051     public static final int MW_FILENAME_TEXTFIELD       = 1;
1052     public static final int MW_PANEL                    = 2;
1053     public static final int MW_ADD_BUTTON               = 0;
1054     public static final int MW_EDIT_BUTTON              = 1;
1055     public static final int MW_REMOVE_BUTTON            = 2;
1056     public static final int MW_POLICY_LIST              = 3; // follows MW_PANEL
1057 
1058     /* The preferred height of JTextField should match JComboBox. */
1059     static final int TEXTFIELD_HEIGHT = new JComboBox().getPreferredSize().height;
1060 
1061     private PolicyTool tool;
1062 
1063     /**
1064      * Constructor
1065      */
1066     ToolWindow(PolicyTool tool) {
1067         this.tool = tool;
1068     }
1069 
1070     /**
1071      * Don't call getComponent directly on the window
1072      */
1073     public Component getComponent(int n) {
1074         Component c = getContentPane().getComponent(n);
1075         if (c instanceof JScrollPane) {
1076             c = ((JScrollPane)c).getViewport().getView();
1077         }
1078         return c;
1079     }
1080 
1081     /**
1082      * Initialize the PolicyTool window with the necessary components
1083      */
1084     private void initWindow() {
1085         // The ToolWindowListener will handle closing the window.
1086         setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
1087 
1088         // create the top menu bar
1089         JMenuBar menuBar = new JMenuBar();
1090 
1091         // create a File menu
1092         JMenu menu = new JMenu();
1093         configureButton(menu, "File");
1094         ActionListener actionListener = new FileMenuListener(tool, this);
1095         addMenuItem(menu, NEW_POLICY_FILE, actionListener, "N");
1096         addMenuItem(menu, OPEN_POLICY_FILE, actionListener, "O");
1097         addMenuItem(menu, SAVE_POLICY_FILE, actionListener, "S");
1098         addMenuItem(menu, SAVE_AS_POLICY_FILE, actionListener, null);
1099         addMenuItem(menu, VIEW_WARNINGS, actionListener, null);
1100         addMenuItem(menu, QUIT, actionListener, null);
1101         menuBar.add(menu);
1102 
1103         // create a KeyStore menu
1104         menu = new JMenu();
1105         configureButton(menu, "KeyStore");
1106         actionListener = new MainWindowListener(tool, this);
1107         addMenuItem(menu, EDIT_KEYSTORE, actionListener, null);
1108         menuBar.add(menu);
1109         setJMenuBar(menuBar);
1110 
1111         // Create some space around components
1112         ((JPanel)getContentPane()).setBorder(new EmptyBorder(6, 6, 6, 6));
1113 
1114         // policy entry listing
1115         JLabel label = new JLabel(PolicyTool.getMessage("Policy.File."));
1116         addNewComponent(this, label, MW_FILENAME_LABEL,
1117                         0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
1118                         LR_TOP_BOTTOM_PADDING);
1119         JTextField tf = new JTextField(50);
1120         tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
1121         tf.getAccessibleContext().setAccessibleName(
1122                 PolicyTool.getMessage("Policy.File."));
1123         tf.setEditable(false);
1124         addNewComponent(this, tf, MW_FILENAME_TEXTFIELD,
1125                         1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
1126                         LR_TOP_BOTTOM_PADDING);
1127 
1128 
1129         // add ADD/REMOVE/EDIT buttons in a new panel
1130         JPanel panel = new JPanel();
1131         panel.setLayout(new GridBagLayout());
1132 
1133         JButton button = new JButton();
1134         configureButton(button, ADD_POLICY_ENTRY);
1135         button.addActionListener(new MainWindowListener(tool, this));
1136         addNewComponent(panel, button, MW_ADD_BUTTON,
1137                         0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
1138                         LR_PADDING);
1139 
1140         button = new JButton();
1141         configureButton(button, EDIT_POLICY_ENTRY);
1142         button.addActionListener(new MainWindowListener(tool, this));
1143         addNewComponent(panel, button, MW_EDIT_BUTTON,
1144                         1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
1145                         LR_PADDING);
1146 
1147         button = new JButton();
1148         configureButton(button, REMOVE_POLICY_ENTRY);
1149         button.addActionListener(new MainWindowListener(tool, this));
1150         addNewComponent(panel, button, MW_REMOVE_BUTTON,
1151                         2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
1152                         LR_PADDING);
1153 
1154         addNewComponent(this, panel, MW_PANEL,
1155                         0, 2, 2, 1, 0.0, 0.0, GridBagConstraints.BOTH,
1156                         BOTTOM_PADDING);
1157 
1158 
1159         String policyFile = tool.getPolicyFileName();
1160         if (policyFile == null) {
1161             String userHome;
1162             userHome = java.security.AccessController.doPrivileged(
1163                     new sun.security.action.GetPropertyAction("user.home"));
1164             policyFile = userHome + File.separatorChar + ".java.policy";
1165         }
1166 
1167         try {
1168             // open the policy file
1169             tool.openPolicy(policyFile);
1170 
1171             // display the policy entries via the policy list textarea
1172             DefaultListModel listModel = new DefaultListModel();
1173             JList list = new JList(listModel);
1174             list.setVisibleRowCount(15);
1175             list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
1176             list.addMouseListener(new PolicyListListener(tool, this));
1177             PolicyEntry entries[] = tool.getEntry();
1178             if (entries != null) {
1179                 for (int i = 0; i < entries.length; i++) {
1180                     listModel.addElement(entries[i].headerToString());
1181                 }
1182             }
1183             JTextField newFilename = (JTextField)
1184                                 getComponent(MW_FILENAME_TEXTFIELD);
1185             newFilename.setText(policyFile);
1186             initPolicyList(list);
1187 
1188         } catch (FileNotFoundException fnfe) {
1189             // add blank policy listing
1190             JList list = new JList(new DefaultListModel());
1191             list.setVisibleRowCount(15);
1192             list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
1193             list.addMouseListener(new PolicyListListener(tool, this));
1194             initPolicyList(list);
1195             tool.setPolicyFileName(null);
1196             tool.modified = false;
1197 
1198             // just add warning
1199             tool.warnings.addElement(fnfe.toString());
1200 
1201         } catch (Exception e) {
1202             // add blank policy listing
1203             JList list = new JList(new DefaultListModel());
1204             list.setVisibleRowCount(15);
1205             list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
1206             list.addMouseListener(new PolicyListListener(tool, this));
1207             initPolicyList(list);
1208             tool.setPolicyFileName(null);
1209             tool.modified = false;
1210 
1211             // display the error
1212             MessageFormat form = new MessageFormat(PolicyTool.getMessage
1213                 ("Could.not.open.policy.file.policyFile.e.toString."));
1214             Object[] source = {policyFile, e.toString()};
1215             displayErrorDialog(null, form.format(source));
1216         }
1217     }
1218 
1219 
1220     // Platform specific modifier (control / command).
1221     private int shortCutModifier = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
1222 
1223     private void addMenuItem(JMenu menu, String key, ActionListener actionListener, String accelerator) {
1224         JMenuItem menuItem = new JMenuItem();
1225         configureButton(menuItem, key);
1226 
1227         if (PolicyTool.rb.containsKey(key + ".accelerator")) {
1228             // Accelerator from resources takes precedence
1229             accelerator = PolicyTool.getMessage(key + ".accelerator");
1230         }
1231 
1232         if (accelerator != null && !accelerator.isEmpty()) {
1233             KeyStroke keyStroke;
1234             if (accelerator.length() == 1) {
1235                 keyStroke = KeyStroke.getKeyStroke(KeyEvent.getExtendedKeyCodeForChar(accelerator.charAt(0)),
1236                                                    shortCutModifier);
1237             } else {
1238                 keyStroke = KeyStroke.getKeyStroke(accelerator);
1239             }
1240             menuItem.setAccelerator(keyStroke);
1241         }
1242 
1243         menuItem.addActionListener(actionListener);
1244         menu.add(menuItem);
1245     }
1246 
1247     static void configureButton(AbstractButton button, String key) {
1248         button.setText(PolicyTool.getMessage(key));
1249         button.setActionCommand(key);
1250 
1251         int mnemonicInt = PolicyTool.getMnemonicInt(key);
1252         if (mnemonicInt > 0) {
1253             button.setMnemonic(mnemonicInt);
1254             button.setDisplayedMnemonicIndex(PolicyTool.getDisplayedMnemonicIndex(key));
1255          }
1256     }
1257 
1258     static void configureLabelFor(JLabel label, JComponent component, String key) {
1259         label.setText(PolicyTool.getMessage(key));
1260         label.setLabelFor(component);
1261 
1262         int mnemonicInt = PolicyTool.getMnemonicInt(key);
1263         if (mnemonicInt > 0) {
1264             label.setDisplayedMnemonic(mnemonicInt);
1265             label.setDisplayedMnemonicIndex(PolicyTool.getDisplayedMnemonicIndex(key));
1266          }
1267     }
1268 
1269 
1270     /**
1271      * Add a component to the PolicyTool window
1272      */
1273     void addNewComponent(Container container, JComponent component,
1274         int index, int gridx, int gridy, int gridwidth, int gridheight,
1275         double weightx, double weighty, int fill, Insets is) {
1276 
1277         if (container instanceof JFrame) {
1278             container = ((JFrame)container).getContentPane();
1279         } else if (container instanceof JDialog) {
1280             container = ((JDialog)container).getContentPane();
1281         }
1282 
1283         // add the component at the specified gridbag index
1284         container.add(component, index);
1285 
1286         // set the constraints
1287         GridBagLayout gbl = (GridBagLayout)container.getLayout();
1288         GridBagConstraints gbc = new GridBagConstraints();
1289         gbc.gridx = gridx;
1290         gbc.gridy = gridy;
1291         gbc.gridwidth = gridwidth;
1292         gbc.gridheight = gridheight;
1293         gbc.weightx = weightx;
1294         gbc.weighty = weighty;
1295         gbc.fill = fill;
1296         if (is != null) gbc.insets = is;
1297         gbl.setConstraints(component, gbc);
1298     }
1299 
1300 
1301     /**
1302      * Add a component to the PolicyTool window without external padding
1303      */
1304     void addNewComponent(Container container, JComponent component,
1305         int index, int gridx, int gridy, int gridwidth, int gridheight,
1306         double weightx, double weighty, int fill) {
1307 
1308         // delegate with "null" external padding
1309         addNewComponent(container, component, index, gridx, gridy,
1310                         gridwidth, gridheight, weightx, weighty,
1311                         fill, null);
1312     }
1313 
1314 
1315     /**
1316      * Init the policy_entry_list TEXTAREA component in the
1317      * PolicyTool window
1318      */
1319     void initPolicyList(JList policyList) {
1320 
1321         // add the policy list to the window
1322         //policyList.setPreferredSize(new Dimension(500, 350));
1323         JScrollPane scrollPane = new JScrollPane(policyList);
1324         addNewComponent(this, scrollPane, MW_POLICY_LIST,
1325                         0, 3, 2, 1, 1.0, 1.0, GridBagConstraints.BOTH);
1326     }
1327 
1328     /**
1329      * Replace the policy_entry_list TEXTAREA component in the
1330      * PolicyTool window with an updated one.
1331      */
1332     void replacePolicyList(JList policyList) {
1333 
1334         // remove the original list of Policy Entries
1335         // and add the new list of entries
1336         JList list = (JList)getComponent(MW_POLICY_LIST);
1337         list.setModel(policyList.getModel());
1338     }
1339 
1340     /**
1341      * display the main PolicyTool window
1342      */
1343     void displayToolWindow(String args[]) {
1344 
1345         setTitle(PolicyTool.getMessage("Policy.Tool"));
1346         setResizable(true);
1347         addWindowListener(new ToolWindowListener(tool, this));
1348         //setBounds(135, 80, 500, 500);
1349         getContentPane().setLayout(new GridBagLayout());
1350 
1351         initWindow();
1352         pack();
1353         setLocationRelativeTo(null);
1354 
1355         // display it
1356         setVisible(true);
1357 
1358         if (tool.newWarning == true) {
1359             displayStatusDialog(this, PolicyTool.getMessage
1360                 ("Errors.have.occurred.while.opening.the.policy.configuration.View.the.Warning.Log.for.more.information."));
1361         }
1362     }
1363 
1364     /**
1365      * displays a dialog box describing an error which occurred.
1366      */
1367     void displayErrorDialog(Window w, String error) {
1368         ToolDialog ed = new ToolDialog
1369                 (PolicyTool.getMessage("Error"), tool, this, true);
1370 
1371         // find where the PolicyTool gui is
1372         Point location = ((w == null) ?
1373                 getLocationOnScreen() : w.getLocationOnScreen());
1374         //ed.setBounds(location.x + 50, location.y + 50, 600, 100);
1375         ed.setLayout(new GridBagLayout());
1376 
1377         JLabel label = new JLabel(error);
1378         addNewComponent(ed, label, 0,
1379                         0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH);
1380 
1381         JButton okButton = new JButton(PolicyTool.getMessage("OK"));
1382         ActionListener okListener = new ErrorOKButtonListener(ed);
1383         okButton.addActionListener(okListener);
1384         addNewComponent(ed, okButton, 1,
1385                         0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL);
1386 
1387         ed.getRootPane().setDefaultButton(okButton);
1388         ed.getRootPane().registerKeyboardAction(okListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
1389 
1390         ed.pack();
1391         ed.setLocationRelativeTo(w);
1392         ed.setVisible(true);
1393     }
1394 
1395     /**
1396      * displays a dialog box describing an error which occurred.
1397      */
1398     void displayErrorDialog(Window w, Throwable t) {
1399         if (t instanceof NoDisplayException) {
1400             return;
1401         }
1402         displayErrorDialog(w, t.toString());
1403     }
1404 
1405     /**
1406      * displays a dialog box describing the status of an event
1407      */
1408     void displayStatusDialog(Window w, String status) {
1409         ToolDialog sd = new ToolDialog
1410                 (PolicyTool.getMessage("Status"), tool, this, true);
1411 
1412         // find the location of the PolicyTool gui
1413         Point location = ((w == null) ?
1414                 getLocationOnScreen() : w.getLocationOnScreen());
1415         //sd.setBounds(location.x + 50, location.y + 50, 500, 100);
1416         sd.setLayout(new GridBagLayout());
1417 
1418         JLabel label = new JLabel(status);
1419         addNewComponent(sd, label, 0,
1420                         0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH);
1421 
1422         JButton okButton = new JButton(PolicyTool.getMessage("OK"));
1423         ActionListener okListener = new StatusOKButtonListener(sd);
1424         okButton.addActionListener(okListener);
1425         addNewComponent(sd, okButton, 1,
1426                         0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL);
1427 
1428         sd.getRootPane().setDefaultButton(okButton);
1429         sd.getRootPane().registerKeyboardAction(okListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
1430 
1431         sd.pack();
1432         sd.setLocationRelativeTo(w);
1433         sd.setVisible(true);
1434     }
1435 
1436     /**
1437      * display the warning log
1438      */
1439     void displayWarningLog(Window w) {
1440 
1441         ToolDialog wd = new ToolDialog
1442                 (PolicyTool.getMessage("Warning"), tool, this, true);
1443 
1444         // find the location of the PolicyTool gui
1445         Point location = ((w == null) ?
1446                 getLocationOnScreen() : w.getLocationOnScreen());
1447         //wd.setBounds(location.x + 50, location.y + 50, 500, 100);
1448         wd.setLayout(new GridBagLayout());
1449 
1450         JTextArea ta = new JTextArea();
1451         ta.setEditable(false);
1452         for (int i = 0; i < tool.warnings.size(); i++) {
1453             ta.append(tool.warnings.elementAt(i));
1454             ta.append(PolicyTool.getMessage("NEWLINE"));
1455         }
1456         addNewComponent(wd, ta, 0,
1457                         0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
1458                         BOTTOM_PADDING);
1459         ta.setFocusable(false);
1460 
1461         JButton okButton = new JButton(PolicyTool.getMessage("OK"));
1462         ActionListener okListener = new CancelButtonListener(wd);
1463         okButton.addActionListener(okListener);
1464         addNewComponent(wd, okButton, 1,
1465                         0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
1466                         LR_PADDING);
1467 
1468         wd.getRootPane().setDefaultButton(okButton);
1469         wd.getRootPane().registerKeyboardAction(okListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
1470 
1471         wd.pack();
1472         wd.setLocationRelativeTo(w);
1473         wd.setVisible(true);
1474     }
1475 
1476     char displayYesNoDialog(Window w, String title, String prompt, String yes, String no) {
1477 
1478         final ToolDialog tw = new ToolDialog
1479                 (title, tool, this, true);
1480         Point location = ((w == null) ?
1481                 getLocationOnScreen() : w.getLocationOnScreen());
1482         //tw.setBounds(location.x + 75, location.y + 100, 400, 150);
1483         tw.setLayout(new GridBagLayout());
1484 
1485         JTextArea ta = new JTextArea(prompt, 10, 50);
1486         ta.setEditable(false);
1487         ta.setLineWrap(true);
1488         ta.setWrapStyleWord(true);
1489         JScrollPane scrollPane = new JScrollPane(ta,
1490                                                  JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
1491                                                  JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
1492         addNewComponent(tw, scrollPane, 0,
1493                 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH);
1494         ta.setFocusable(false);
1495 
1496         JPanel panel = new JPanel();
1497         panel.setLayout(new GridBagLayout());
1498 
1499         // StringBuffer to store button press. Must be final.
1500         final StringBuffer chooseResult = new StringBuffer();
1501 
1502         JButton button = new JButton(yes);
1503         button.addActionListener(new ActionListener() {
1504             public void actionPerformed(ActionEvent e) {
1505                 chooseResult.append('Y');
1506                 tw.setVisible(false);
1507                 tw.dispose();
1508             }
1509         });
1510         addNewComponent(panel, button, 0,
1511                            0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
1512                            LR_PADDING);
1513 
1514         button = new JButton(no);
1515         button.addActionListener(new ActionListener() {
1516             public void actionPerformed(ActionEvent e) {
1517                 chooseResult.append('N');
1518                 tw.setVisible(false);
1519                 tw.dispose();
1520             }
1521         });
1522         addNewComponent(panel, button, 1,
1523                            1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
1524                            LR_PADDING);
1525 
1526         addNewComponent(tw, panel, 1,
1527                 0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL);
1528 
1529         tw.pack();
1530         tw.setLocationRelativeTo(w);
1531         tw.setVisible(true);
1532         if (chooseResult.length() > 0) {
1533             return chooseResult.charAt(0);
1534         } else {
1535             // I did encounter this once, don't why.
1536             return 'N';
1537         }
1538     }
1539 
1540 }
1541 
1542 /**
1543  * General dialog window
1544  */
1545 class ToolDialog extends JDialog {
1546     // use serialVersionUID from JDK 1.2.2 for interoperability
1547     private static final long serialVersionUID = -372244357011301190L;
1548 
1549     /* ESCAPE key */
1550     static final KeyStroke escKey = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
1551 
1552     /* necessary constants */
1553     public static final int NOACTION            = 0;
1554     public static final int QUIT                = 1;
1555     public static final int NEW                 = 2;
1556     public static final int OPEN                = 3;
1557 
1558     public static final String ALL_PERM_CLASS   =
1559                 "java.security.AllPermission";
1560     public static final String FILE_PERM_CLASS  =
1561                 "java.io.FilePermission";
1562 
1563     public static final String X500_PRIN_CLASS         =
1564                 "javax.security.auth.x500.X500Principal";
1565 
1566     /* popup menus */
1567     public static final String PERM             =
1568         PolicyTool.getMessage
1569         ("Permission.");
1570 
1571     public static final String PRIN_TYPE        =
1572         PolicyTool.getMessage("Principal.Type.");
1573     public static final String PRIN_NAME        =
1574         PolicyTool.getMessage("Principal.Name.");
1575 
1576     /* more popu menus */
1577     public static final String PERM_NAME        =
1578         PolicyTool.getMessage
1579         ("Target.Name.");
1580 
1581     /* and more popup menus */
1582     public static final String PERM_ACTIONS             =
1583       PolicyTool.getMessage
1584       ("Actions.");
1585 
1586     /* gridbag index for display PolicyEntry (PE) components */
1587     public static final int PE_CODEBASE_LABEL           = 0;
1588     public static final int PE_CODEBASE_TEXTFIELD       = 1;
1589     public static final int PE_SIGNEDBY_LABEL           = 2;
1590     public static final int PE_SIGNEDBY_TEXTFIELD       = 3;
1591 
1592     public static final int PE_PANEL0                   = 4;
1593     public static final int PE_ADD_PRIN_BUTTON          = 0;
1594     public static final int PE_EDIT_PRIN_BUTTON         = 1;
1595     public static final int PE_REMOVE_PRIN_BUTTON       = 2;
1596 
1597     public static final int PE_PRIN_LABEL               = 5;
1598     public static final int PE_PRIN_LIST                = 6;
1599 
1600     public static final int PE_PANEL1                   = 7;
1601     public static final int PE_ADD_PERM_BUTTON          = 0;
1602     public static final int PE_EDIT_PERM_BUTTON         = 1;
1603     public static final int PE_REMOVE_PERM_BUTTON       = 2;
1604 
1605     public static final int PE_PERM_LIST                = 8;
1606 
1607     public static final int PE_PANEL2                   = 9;
1608     public static final int PE_CANCEL_BUTTON            = 1;
1609     public static final int PE_DONE_BUTTON              = 0;
1610 
1611     /* the gridbag index for components in the Principal Dialog (PRD) */
1612     public static final int PRD_DESC_LABEL              = 0;
1613     public static final int PRD_PRIN_CHOICE             = 1;
1614     public static final int PRD_PRIN_TEXTFIELD          = 2;
1615     public static final int PRD_NAME_LABEL              = 3;
1616     public static final int PRD_NAME_TEXTFIELD          = 4;
1617     public static final int PRD_CANCEL_BUTTON           = 6;
1618     public static final int PRD_OK_BUTTON               = 5;
1619 
1620     /* the gridbag index for components in the Permission Dialog (PD) */
1621     public static final int PD_DESC_LABEL               = 0;
1622     public static final int PD_PERM_CHOICE              = 1;
1623     public static final int PD_PERM_TEXTFIELD           = 2;
1624     public static final int PD_NAME_CHOICE              = 3;
1625     public static final int PD_NAME_TEXTFIELD           = 4;
1626     public static final int PD_ACTIONS_CHOICE           = 5;
1627     public static final int PD_ACTIONS_TEXTFIELD        = 6;
1628     public static final int PD_SIGNEDBY_LABEL           = 7;
1629     public static final int PD_SIGNEDBY_TEXTFIELD       = 8;
1630     public static final int PD_CANCEL_BUTTON            = 10;
1631     public static final int PD_OK_BUTTON                = 9;
1632 
1633     /* modes for KeyStore */
1634     public static final int EDIT_KEYSTORE               = 0;
1635 
1636     /* the gridbag index for components in the Change KeyStore Dialog (KSD) */
1637     public static final int KSD_NAME_LABEL              = 0;
1638     public static final int KSD_NAME_TEXTFIELD          = 1;
1639     public static final int KSD_TYPE_LABEL              = 2;
1640     public static final int KSD_TYPE_TEXTFIELD          = 3;
1641     public static final int KSD_PROVIDER_LABEL          = 4;
1642     public static final int KSD_PROVIDER_TEXTFIELD      = 5;
1643     public static final int KSD_PWD_URL_LABEL           = 6;
1644     public static final int KSD_PWD_URL_TEXTFIELD       = 7;
1645     public static final int KSD_CANCEL_BUTTON           = 9;
1646     public static final int KSD_OK_BUTTON               = 8;
1647 
1648     /* the gridbag index for components in the User Save Changes Dialog (USC) */
1649     public static final int USC_LABEL                   = 0;
1650     public static final int USC_PANEL                   = 1;
1651     public static final int USC_YES_BUTTON              = 0;
1652     public static final int USC_NO_BUTTON               = 1;
1653     public static final int USC_CANCEL_BUTTON           = 2;
1654 
1655     /* gridbag index for the ConfirmRemovePolicyEntryDialog (CRPE) */
1656     public static final int CRPE_LABEL1                 = 0;
1657     public static final int CRPE_LABEL2                 = 1;
1658     public static final int CRPE_PANEL                  = 2;
1659     public static final int CRPE_PANEL_OK               = 0;
1660     public static final int CRPE_PANEL_CANCEL           = 1;
1661 
1662     /* some private static finals */
1663     private static final int PERMISSION                 = 0;
1664     private static final int PERMISSION_NAME            = 1;
1665     private static final int PERMISSION_ACTIONS         = 2;
1666     private static final int PERMISSION_SIGNEDBY        = 3;
1667     private static final int PRINCIPAL_TYPE             = 4;
1668     private static final int PRINCIPAL_NAME             = 5;
1669 
1670     /* The preferred height of JTextField should match JComboBox. */
1671     static final int TEXTFIELD_HEIGHT = new JComboBox().getPreferredSize().height;
1672 
1673     public static java.util.ArrayList<Perm> PERM_ARRAY;
1674     public static java.util.ArrayList<Prin> PRIN_ARRAY;
1675     PolicyTool tool;
1676     ToolWindow tw;
1677 
1678     static {
1679 
1680         // set up permission objects
1681 
1682         PERM_ARRAY = new java.util.ArrayList<Perm>();
1683         PERM_ARRAY.add(new AllPerm());
1684         PERM_ARRAY.add(new AudioPerm());
1685         PERM_ARRAY.add(new AuthPerm());
1686         PERM_ARRAY.add(new AWTPerm());
1687         PERM_ARRAY.add(new DelegationPerm());
1688         PERM_ARRAY.add(new FilePerm());
1689         PERM_ARRAY.add(new URLPerm());
1690         PERM_ARRAY.add(new InqSecContextPerm());
1691         PERM_ARRAY.add(new LogPerm());
1692         PERM_ARRAY.add(new MgmtPerm());
1693         PERM_ARRAY.add(new MBeanPerm());
1694         PERM_ARRAY.add(new MBeanSvrPerm());
1695         PERM_ARRAY.add(new MBeanTrustPerm());
1696         PERM_ARRAY.add(new NetPerm());
1697         PERM_ARRAY.add(new PrivCredPerm());
1698         PERM_ARRAY.add(new PropPerm());
1699         PERM_ARRAY.add(new ReflectPerm());
1700         PERM_ARRAY.add(new RuntimePerm());
1701         PERM_ARRAY.add(new SecurityPerm());
1702         PERM_ARRAY.add(new SerialPerm());
1703         PERM_ARRAY.add(new ServicePerm());
1704         PERM_ARRAY.add(new SocketPerm());
1705         PERM_ARRAY.add(new SQLPerm());
1706         PERM_ARRAY.add(new SSLPerm());
1707         PERM_ARRAY.add(new SubjDelegPerm());
1708 
1709         // set up principal objects
1710 
1711         PRIN_ARRAY = new java.util.ArrayList<Prin>();
1712         PRIN_ARRAY.add(new KrbPrin());
1713         PRIN_ARRAY.add(new X500Prin());
1714     }
1715 
1716     ToolDialog(String title, PolicyTool tool, ToolWindow tw, boolean modal) {
1717         super(tw, modal);
1718         setTitle(title);
1719         this.tool = tool;
1720         this.tw = tw;
1721         addWindowListener(new ChildWindowListener(this));
1722 
1723         // Create some space around components
1724         ((JPanel)getContentPane()).setBorder(new EmptyBorder(6, 6, 6, 6));
1725     }
1726 
1727     /**
1728      * Don't call getComponent directly on the window
1729      */
1730     public Component getComponent(int n) {
1731         Component c = getContentPane().getComponent(n);
1732         if (c instanceof JScrollPane) {
1733             c = ((JScrollPane)c).getViewport().getView();
1734         }
1735         return c;
1736     }
1737 
1738     /**
1739      * get the Perm instance based on either the (shortened) class name
1740      * or the fully qualified class name
1741      */
1742     static Perm getPerm(String clazz, boolean fullClassName) {
1743         for (int i = 0; i < PERM_ARRAY.size(); i++) {
1744             Perm next = PERM_ARRAY.get(i);
1745             if (fullClassName) {
1746                 if (next.FULL_CLASS.equals(clazz)) {
1747                     return next;
1748                 }
1749             } else {
1750                 if (next.CLASS.equals(clazz)) {
1751                     return next;
1752                 }
1753             }
1754         }
1755         return null;
1756     }
1757 
1758     /**
1759      * get the Prin instance based on either the (shortened) class name
1760      * or the fully qualified class name
1761      */
1762     static Prin getPrin(String clazz, boolean fullClassName) {
1763         for (int i = 0; i < PRIN_ARRAY.size(); i++) {
1764             Prin next = PRIN_ARRAY.get(i);
1765             if (fullClassName) {
1766                 if (next.FULL_CLASS.equals(clazz)) {
1767                     return next;
1768                 }
1769             } else {
1770                 if (next.CLASS.equals(clazz)) {
1771                     return next;
1772                 }
1773             }
1774         }
1775         return null;
1776     }
1777 
1778     /**
1779      * pop up a dialog so the user can enter info to add a new PolicyEntry
1780      * - if edit is TRUE, then the user is editing an existing entry
1781      *   and we should display the original info as well.
1782      *
1783      * - the other reason we need the 'edit' boolean is we need to know
1784      *   when we are adding a NEW policy entry.  in this case, we can
1785      *   not simply update the existing entry, because it doesn't exist.
1786      *   we ONLY update the GUI listing/info, and then when the user
1787      *   finally clicks 'OK' or 'DONE', then we can collect that info
1788      *   and add it to the policy.
1789      */
1790     void displayPolicyEntryDialog(boolean edit) {
1791 
1792         int listIndex = 0;
1793         PolicyEntry entries[] = null;
1794         TaggedList prinList = new TaggedList(3, false);
1795         prinList.getAccessibleContext().setAccessibleName(
1796                 PolicyTool.getMessage("Principal.List"));
1797         prinList.addMouseListener
1798                 (new EditPrinButtonListener(tool, tw, this, edit));
1799         TaggedList permList = new TaggedList(10, false);
1800         permList.getAccessibleContext().setAccessibleName(
1801                 PolicyTool.getMessage("Permission.List"));
1802         permList.addMouseListener
1803                 (new EditPermButtonListener(tool, tw, this, edit));
1804 
1805         // find where the PolicyTool gui is
1806         Point location = tw.getLocationOnScreen();
1807         //setBounds(location.x + 75, location.y + 200, 650, 500);
1808         setLayout(new GridBagLayout());
1809         setResizable(true);
1810 
1811         if (edit) {
1812             // get the selected item
1813             entries = tool.getEntry();
1814             JList policyList = (JList)tw.getComponent(ToolWindow.MW_POLICY_LIST);
1815             listIndex = policyList.getSelectedIndex();
1816 
1817             // get principal list
1818             LinkedList<PolicyParser.PrincipalEntry> principals =
1819                 entries[listIndex].getGrantEntry().principals;
1820             for (int i = 0; i < principals.size(); i++) {
1821                 String prinString = null;
1822                 PolicyParser.PrincipalEntry nextPrin = principals.get(i);
1823                 prinList.addTaggedItem(PrincipalEntryToUserFriendlyString(nextPrin), nextPrin);
1824             }
1825 
1826             // get permission list
1827             Vector<PolicyParser.PermissionEntry> permissions =
1828                 entries[listIndex].getGrantEntry().permissionEntries;
1829             for (int i = 0; i < permissions.size(); i++) {
1830                 String permString = null;
1831                 PolicyParser.PermissionEntry nextPerm =
1832                                                 permissions.elementAt(i);
1833                 permList.addTaggedItem(ToolDialog.PermissionEntryToUserFriendlyString(nextPerm), nextPerm);
1834             }
1835         }
1836 
1837         // codebase label and textfield
1838         JLabel label = new JLabel();
1839         tw.addNewComponent(this, label, PE_CODEBASE_LABEL,
1840                 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
1841                 ToolWindow.R_PADDING);
1842         JTextField tf;
1843         tf = (edit ?
1844                 new JTextField(entries[listIndex].getGrantEntry().codeBase) :
1845                 new JTextField());
1846         ToolWindow.configureLabelFor(label, tf, "CodeBase.");
1847         tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
1848         tf.getAccessibleContext().setAccessibleName(
1849                 PolicyTool.getMessage("Code.Base"));
1850         tw.addNewComponent(this, tf, PE_CODEBASE_TEXTFIELD,
1851                 1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH);
1852 
1853         // signedby label and textfield
1854         label = new JLabel();
1855         tw.addNewComponent(this, label, PE_SIGNEDBY_LABEL,
1856                            0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
1857                            ToolWindow.R_PADDING);
1858         tf = (edit ?
1859                 new JTextField(entries[listIndex].getGrantEntry().signedBy) :
1860                 new JTextField());
1861         ToolWindow.configureLabelFor(label, tf, "SignedBy.");
1862         tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
1863         tf.getAccessibleContext().setAccessibleName(
1864                 PolicyTool.getMessage("Signed.By."));
1865         tw.addNewComponent(this, tf, PE_SIGNEDBY_TEXTFIELD,
1866                            1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH);
1867 
1868         // panel for principal buttons
1869         JPanel panel = new JPanel();
1870         panel.setLayout(new GridBagLayout());
1871 
1872         JButton button = new JButton();
1873         ToolWindow.configureButton(button, "Add.Principal");
1874         button.addActionListener
1875                 (new AddPrinButtonListener(tool, tw, this, edit));
1876         tw.addNewComponent(panel, button, PE_ADD_PRIN_BUTTON,
1877                 0, 0, 1, 1, 100.0, 0.0, GridBagConstraints.HORIZONTAL);
1878 
1879         button = new JButton();
1880         ToolWindow.configureButton(button, "Edit.Principal");
1881         button.addActionListener(new EditPrinButtonListener
1882                                                 (tool, tw, this, edit));
1883         tw.addNewComponent(panel, button, PE_EDIT_PRIN_BUTTON,
1884                 1, 0, 1, 1, 100.0, 0.0, GridBagConstraints.HORIZONTAL);
1885 
1886         button = new JButton();
1887         ToolWindow.configureButton(button, "Remove.Principal");
1888         button.addActionListener(new RemovePrinButtonListener
1889                                         (tool, tw, this, edit));
1890         tw.addNewComponent(panel, button, PE_REMOVE_PRIN_BUTTON,
1891                 2, 0, 1, 1, 100.0, 0.0, GridBagConstraints.HORIZONTAL);
1892 
1893         tw.addNewComponent(this, panel, PE_PANEL0,
1894                 1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.HORIZONTAL,
1895                            ToolWindow.LITE_BOTTOM_PADDING);
1896 
1897         // principal label and list
1898         label = new JLabel();
1899         tw.addNewComponent(this, label, PE_PRIN_LABEL,
1900                            0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
1901                            ToolWindow.R_BOTTOM_PADDING);
1902         JScrollPane scrollPane = new JScrollPane(prinList);
1903         ToolWindow.configureLabelFor(label, scrollPane, "Principals.");
1904         tw.addNewComponent(this, scrollPane, PE_PRIN_LIST,
1905                            1, 3, 3, 1, 0.0, prinList.getVisibleRowCount(), GridBagConstraints.BOTH,
1906                            ToolWindow.BOTTOM_PADDING);
1907 
1908         // panel for permission buttons
1909         panel = new JPanel();
1910         panel.setLayout(new GridBagLayout());
1911 
1912         button = new JButton();
1913         ToolWindow.configureButton(button, ".Add.Permission");
1914         button.addActionListener(new AddPermButtonListener
1915                                                 (tool, tw, this, edit));
1916         tw.addNewComponent(panel, button, PE_ADD_PERM_BUTTON,
1917                 0, 0, 1, 1, 100.0, 0.0, GridBagConstraints.HORIZONTAL);
1918 
1919         button = new JButton();
1920         ToolWindow.configureButton(button, ".Edit.Permission");
1921         button.addActionListener(new EditPermButtonListener
1922                                                 (tool, tw, this, edit));
1923         tw.addNewComponent(panel, button, PE_EDIT_PERM_BUTTON,
1924                 1, 0, 1, 1, 100.0, 0.0, GridBagConstraints.HORIZONTAL);
1925 
1926 
1927         button = new JButton();
1928         ToolWindow.configureButton(button, "Remove.Permission");
1929         button.addActionListener(new RemovePermButtonListener
1930                                         (tool, tw, this, edit));
1931         tw.addNewComponent(panel, button, PE_REMOVE_PERM_BUTTON,
1932                 2, 0, 1, 1, 100.0, 0.0, GridBagConstraints.HORIZONTAL);
1933 
1934         tw.addNewComponent(this, panel, PE_PANEL1,
1935                 0, 4, 2, 1, 0.0, 0.0, GridBagConstraints.HORIZONTAL,
1936                 ToolWindow.LITE_BOTTOM_PADDING);
1937 
1938         // permission list
1939         scrollPane = new JScrollPane(permList);
1940         tw.addNewComponent(this, scrollPane, PE_PERM_LIST,
1941                            0, 5, 3, 1, 0.0, permList.getVisibleRowCount(), GridBagConstraints.BOTH,
1942                            ToolWindow.BOTTOM_PADDING);
1943 
1944 
1945         // panel for Done and Cancel buttons
1946         panel = new JPanel();
1947         panel.setLayout(new GridBagLayout());
1948 
1949         // Done Button
1950         JButton okButton = new JButton(PolicyTool.getMessage("Done"));
1951         okButton.addActionListener
1952                 (new AddEntryDoneButtonListener(tool, tw, this, edit));
1953         tw.addNewComponent(panel, okButton, PE_DONE_BUTTON,
1954                            0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
1955                            ToolWindow.LR_PADDING);
1956 
1957         // Cancel Button
1958         JButton cancelButton = new JButton(PolicyTool.getMessage("Cancel"));
1959         ActionListener cancelListener = new CancelButtonListener(this);
1960         cancelButton.addActionListener(cancelListener);
1961         tw.addNewComponent(panel, cancelButton, PE_CANCEL_BUTTON,
1962                            1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
1963                            ToolWindow.LR_PADDING);
1964 
1965         // add the panel
1966         tw.addNewComponent(this, panel, PE_PANEL2,
1967                 0, 6, 2, 1, 0.0, 0.0, GridBagConstraints.VERTICAL);
1968 
1969         getRootPane().setDefaultButton(okButton);
1970         getRootPane().registerKeyboardAction(cancelListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
1971 
1972         pack();
1973         setLocationRelativeTo(tw);
1974         setVisible(true);
1975     }
1976 
1977     /**
1978      * Read all the Policy information data in the dialog box
1979      * and construct a PolicyEntry object with it.
1980      */
1981     PolicyEntry getPolicyEntryFromDialog()
1982         throws InvalidParameterException, MalformedURLException,
1983         NoSuchMethodException, ClassNotFoundException, InstantiationException,
1984         IllegalAccessException, InvocationTargetException,
1985         CertificateException, IOException, Exception {
1986 
1987         // get the Codebase
1988         JTextField tf = (JTextField)getComponent(PE_CODEBASE_TEXTFIELD);
1989         String codebase = null;
1990         if (tf.getText().trim().equals("") == false)
1991                 codebase = new String(tf.getText().trim());
1992 
1993         // get the SignedBy
1994         tf = (JTextField)getComponent(PE_SIGNEDBY_TEXTFIELD);
1995         String signedby = null;
1996         if (tf.getText().trim().equals("") == false)
1997                 signedby = new String(tf.getText().trim());
1998 
1999         // construct a new GrantEntry
2000         PolicyParser.GrantEntry ge =
2001                         new PolicyParser.GrantEntry(signedby, codebase);
2002 
2003         // get the new Principals
2004         LinkedList<PolicyParser.PrincipalEntry> prins = new LinkedList<>();
2005         TaggedList prinList = (TaggedList)getComponent(PE_PRIN_LIST);
2006         for (int i = 0; i < prinList.getModel().getSize(); i++) {
2007             prins.add((PolicyParser.PrincipalEntry)prinList.getObject(i));
2008         }
2009         ge.principals = prins;
2010 
2011         // get the new Permissions
2012         Vector<PolicyParser.PermissionEntry> perms = new Vector<>();
2013         TaggedList permList = (TaggedList)getComponent(PE_PERM_LIST);
2014         for (int i = 0; i < permList.getModel().getSize(); i++) {
2015             perms.addElement((PolicyParser.PermissionEntry)permList.getObject(i));
2016         }
2017         ge.permissionEntries = perms;
2018 
2019         // construct a new PolicyEntry object
2020         PolicyEntry entry = new PolicyEntry(tool, ge);
2021 
2022         return entry;
2023     }
2024 
2025     /**
2026      * display a dialog box for the user to enter KeyStore information
2027      */
2028     void keyStoreDialog(int mode) {
2029 
2030         // find where the PolicyTool gui is
2031         Point location = tw.getLocationOnScreen();
2032         //setBounds(location.x + 25, location.y + 100, 500, 300);
2033         setLayout(new GridBagLayout());
2034 
2035         if (mode == EDIT_KEYSTORE) {
2036 
2037             // KeyStore label and textfield
2038             JLabel label = new JLabel();
2039             tw.addNewComponent(this, label, KSD_NAME_LABEL,
2040                                0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2041                                ToolWindow.R_BOTTOM_PADDING);
2042             JTextField tf = new JTextField(tool.getKeyStoreName(), 30);
2043             ToolWindow.configureLabelFor(label, tf, "KeyStore.URL.");
2044             tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
2045 
2046             // URL to U R L, so that accessibility reader will pronounce well
2047             tf.getAccessibleContext().setAccessibleName(
2048                 PolicyTool.getMessage("KeyStore.U.R.L."));
2049             tw.addNewComponent(this, tf, KSD_NAME_TEXTFIELD,
2050                                1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH,
2051                                ToolWindow.BOTTOM_PADDING);
2052 
2053             // KeyStore type and textfield
2054             label = new JLabel();
2055             tw.addNewComponent(this, label, KSD_TYPE_LABEL,
2056                                0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2057                                ToolWindow.R_BOTTOM_PADDING);
2058             tf = new JTextField(tool.getKeyStoreType(), 30);
2059             ToolWindow.configureLabelFor(label, tf, "KeyStore.Type.");
2060             tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
2061             tf.getAccessibleContext().setAccessibleName(
2062                 PolicyTool.getMessage("KeyStore.Type."));
2063             tw.addNewComponent(this, tf, KSD_TYPE_TEXTFIELD,
2064                                1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH,
2065                                ToolWindow.BOTTOM_PADDING);
2066 
2067             // KeyStore provider and textfield
2068             label = new JLabel();
2069             tw.addNewComponent(this, label, KSD_PROVIDER_LABEL,
2070                                0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2071                                ToolWindow.R_BOTTOM_PADDING);
2072             tf = new JTextField(tool.getKeyStoreProvider(), 30);
2073             ToolWindow.configureLabelFor(label, tf, "KeyStore.Provider.");
2074             tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
2075             tf.getAccessibleContext().setAccessibleName(
2076                 PolicyTool.getMessage("KeyStore.Provider."));
2077             tw.addNewComponent(this, tf, KSD_PROVIDER_TEXTFIELD,
2078                                1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH,
2079                                ToolWindow.BOTTOM_PADDING);
2080 
2081             // KeyStore password URL and textfield
2082             label = new JLabel();
2083             tw.addNewComponent(this, label, KSD_PWD_URL_LABEL,
2084                                0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2085                                ToolWindow.R_BOTTOM_PADDING);
2086             tf = new JTextField(tool.getKeyStorePwdURL(), 30);
2087             ToolWindow.configureLabelFor(label, tf, "KeyStore.Password.URL.");
2088             tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
2089             tf.getAccessibleContext().setAccessibleName(
2090                 PolicyTool.getMessage("KeyStore.Password.U.R.L."));
2091             tw.addNewComponent(this, tf, KSD_PWD_URL_TEXTFIELD,
2092                                1, 3, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH,
2093                                ToolWindow.BOTTOM_PADDING);
2094 
2095             // OK button
2096             JButton okButton = new JButton(PolicyTool.getMessage("OK"));
2097             okButton.addActionListener
2098                         (new ChangeKeyStoreOKButtonListener(tool, tw, this));
2099             tw.addNewComponent(this, okButton, KSD_OK_BUTTON,
2100                         0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL);
2101 
2102             // cancel button
2103             JButton cancelButton = new JButton(PolicyTool.getMessage("Cancel"));
2104             ActionListener cancelListener = new CancelButtonListener(this);
2105             cancelButton.addActionListener(cancelListener);
2106             tw.addNewComponent(this, cancelButton, KSD_CANCEL_BUTTON,
2107                         1, 4, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL);
2108 
2109             getRootPane().setDefaultButton(okButton);
2110             getRootPane().registerKeyboardAction(cancelListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
2111         }
2112 
2113         pack();
2114         setLocationRelativeTo(tw);
2115         setVisible(true);
2116     }
2117 
2118     /**
2119      * display a dialog box for the user to input Principal info
2120      *
2121      * if editPolicyEntry is false, then we are adding Principals to
2122      * a new PolicyEntry, and we only update the GUI listing
2123      * with the new Principal.
2124      *
2125      * if edit is true, then we are editing an existing Policy entry.
2126      */
2127     void displayPrincipalDialog(boolean editPolicyEntry, boolean edit) {
2128 
2129         PolicyParser.PrincipalEntry editMe = null;
2130 
2131         // get the Principal selected from the Principal List
2132         TaggedList prinList = (TaggedList)getComponent(PE_PRIN_LIST);
2133         int prinIndex = prinList.getSelectedIndex();
2134 
2135         if (edit) {
2136             editMe = (PolicyParser.PrincipalEntry)prinList.getObject(prinIndex);
2137         }
2138 
2139         ToolDialog newTD = new ToolDialog
2140                 (PolicyTool.getMessage("Principals"), tool, tw, true);
2141         newTD.addWindowListener(new ChildWindowListener(newTD));
2142 
2143         // find where the PolicyTool gui is
2144         Point location = getLocationOnScreen();
2145         //newTD.setBounds(location.x + 50, location.y + 100, 650, 190);
2146         newTD.setLayout(new GridBagLayout());
2147         newTD.setResizable(true);
2148 
2149         // description label
2150         JLabel label = (edit ?
2151                 new JLabel(PolicyTool.getMessage(".Edit.Principal.")) :
2152                 new JLabel(PolicyTool.getMessage(".Add.New.Principal.")));
2153         tw.addNewComponent(newTD, label, PRD_DESC_LABEL,
2154                            0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2155                            ToolWindow.TOP_BOTTOM_PADDING);
2156 
2157         // principal choice
2158         JComboBox choice = new JComboBox();
2159         choice.addItem(PRIN_TYPE);
2160         choice.getAccessibleContext().setAccessibleName(PRIN_TYPE);
2161         for (int i = 0; i < PRIN_ARRAY.size(); i++) {
2162             Prin next = PRIN_ARRAY.get(i);
2163             choice.addItem(next.CLASS);
2164         }
2165 
2166         if (edit) {
2167             if (PolicyParser.PrincipalEntry.WILDCARD_CLASS.equals
2168                                 (editMe.getPrincipalClass())) {
2169                 choice.setSelectedItem(PRIN_TYPE);
2170             } else {
2171                 Prin inputPrin = getPrin(editMe.getPrincipalClass(), true);
2172                 if (inputPrin != null) {
2173                     choice.setSelectedItem(inputPrin.CLASS);
2174                 }
2175             }
2176         }
2177         // Add listener after selected item is set
2178         choice.addItemListener(new PrincipalTypeMenuListener(newTD));
2179 
2180         tw.addNewComponent(newTD, choice, PRD_PRIN_CHOICE,
2181                            0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2182                            ToolWindow.LR_PADDING);
2183 
2184         // principal textfield
2185         JTextField tf;
2186         tf = (edit ?
2187                 new JTextField(editMe.getDisplayClass(), 30) :
2188                 new JTextField(30));
2189         tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
2190         tf.getAccessibleContext().setAccessibleName(PRIN_TYPE);
2191         tw.addNewComponent(newTD, tf, PRD_PRIN_TEXTFIELD,
2192                            1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH,
2193                            ToolWindow.LR_PADDING);
2194 
2195         // name label and textfield
2196         label = new JLabel(PRIN_NAME);
2197         tf = (edit ?
2198                 new JTextField(editMe.getDisplayName(), 40) :
2199                 new JTextField(40));
2200         tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
2201         tf.getAccessibleContext().setAccessibleName(PRIN_NAME);
2202 
2203         tw.addNewComponent(newTD, label, PRD_NAME_LABEL,
2204                            0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2205                            ToolWindow.LR_PADDING);
2206         tw.addNewComponent(newTD, tf, PRD_NAME_TEXTFIELD,
2207                            1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH,
2208                            ToolWindow.LR_PADDING);
2209 
2210         // OK button
2211         JButton okButton = new JButton(PolicyTool.getMessage("OK"));
2212         okButton.addActionListener(
2213             new NewPolicyPrinOKButtonListener
2214                                         (tool, tw, this, newTD, edit));
2215         tw.addNewComponent(newTD, okButton, PRD_OK_BUTTON,
2216                            0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
2217                            ToolWindow.TOP_BOTTOM_PADDING);
2218         // cancel button
2219         JButton cancelButton = new JButton(PolicyTool.getMessage("Cancel"));
2220         ActionListener cancelListener = new CancelButtonListener(newTD);
2221         cancelButton.addActionListener(cancelListener);
2222         tw.addNewComponent(newTD, cancelButton, PRD_CANCEL_BUTTON,
2223                            1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
2224                            ToolWindow.TOP_BOTTOM_PADDING);
2225 
2226         newTD.getRootPane().setDefaultButton(okButton);
2227         newTD.getRootPane().registerKeyboardAction(cancelListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
2228 
2229         newTD.pack();
2230         newTD.setLocationRelativeTo(tw);
2231         newTD.setVisible(true);
2232     }
2233 
2234     /**
2235      * display a dialog box for the user to input Permission info
2236      *
2237      * if editPolicyEntry is false, then we are adding Permissions to
2238      * a new PolicyEntry, and we only update the GUI listing
2239      * with the new Permission.
2240      *
2241      * if edit is true, then we are editing an existing Permission entry.
2242      */
2243     void displayPermissionDialog(boolean editPolicyEntry, boolean edit) {
2244 
2245         PolicyParser.PermissionEntry editMe = null;
2246 
2247         // get the Permission selected from the Permission List
2248         TaggedList permList = (TaggedList)getComponent(PE_PERM_LIST);
2249         int permIndex = permList.getSelectedIndex();
2250 
2251         if (edit) {
2252             editMe = (PolicyParser.PermissionEntry)permList.getObject(permIndex);
2253         }
2254 
2255         ToolDialog newTD = new ToolDialog
2256                 (PolicyTool.getMessage("Permissions"), tool, tw, true);
2257         newTD.addWindowListener(new ChildWindowListener(newTD));
2258 
2259         // find where the PolicyTool gui is
2260         Point location = getLocationOnScreen();
2261         //newTD.setBounds(location.x + 50, location.y + 100, 700, 250);
2262         newTD.setLayout(new GridBagLayout());
2263         newTD.setResizable(true);
2264 
2265         // description label
2266         JLabel label = (edit ?
2267                 new JLabel(PolicyTool.getMessage(".Edit.Permission.")) :
2268                 new JLabel(PolicyTool.getMessage(".Add.New.Permission.")));
2269         tw.addNewComponent(newTD, label, PD_DESC_LABEL,
2270                            0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2271                            ToolWindow.TOP_BOTTOM_PADDING);
2272 
2273         // permission choice (added in alphabetical order)
2274         JComboBox choice = new JComboBox();
2275         choice.addItem(PERM);
2276         choice.getAccessibleContext().setAccessibleName(PERM);
2277         for (int i = 0; i < PERM_ARRAY.size(); i++) {
2278             Perm next = PERM_ARRAY.get(i);
2279             choice.addItem(next.CLASS);
2280         }
2281         tw.addNewComponent(newTD, choice, PD_PERM_CHOICE,
2282                            0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2283                            ToolWindow.LR_BOTTOM_PADDING);
2284 
2285         // permission textfield
2286         JTextField tf;
2287         tf = (edit ? new JTextField(editMe.permission, 30) : new JTextField(30));
2288         tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
2289         tf.getAccessibleContext().setAccessibleName(PERM);
2290         if (edit) {
2291             Perm inputPerm = getPerm(editMe.permission, true);
2292             if (inputPerm != null) {
2293                 choice.setSelectedItem(inputPerm.CLASS);
2294             }
2295         }
2296         tw.addNewComponent(newTD, tf, PD_PERM_TEXTFIELD,
2297                            1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH,
2298                            ToolWindow.LR_BOTTOM_PADDING);
2299         choice.addItemListener(new PermissionMenuListener(newTD));
2300 
2301         // name label and textfield
2302         choice = new JComboBox();
2303         choice.addItem(PERM_NAME);
2304         choice.getAccessibleContext().setAccessibleName(PERM_NAME);
2305         tf = (edit ? new JTextField(editMe.name, 40) : new JTextField(40));
2306         tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
2307         tf.getAccessibleContext().setAccessibleName(PERM_NAME);
2308         if (edit) {
2309             setPermissionNames(getPerm(editMe.permission, true), choice, tf);
2310         }
2311         tw.addNewComponent(newTD, choice, PD_NAME_CHOICE,
2312                            0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2313                            ToolWindow.LR_BOTTOM_PADDING);
2314         tw.addNewComponent(newTD, tf, PD_NAME_TEXTFIELD,
2315                            1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH,
2316                            ToolWindow.LR_BOTTOM_PADDING);
2317         choice.addItemListener(new PermissionNameMenuListener(newTD));
2318 
2319         // actions label and textfield
2320         choice = new JComboBox();
2321         choice.addItem(PERM_ACTIONS);
2322         choice.getAccessibleContext().setAccessibleName(PERM_ACTIONS);
2323         tf = (edit ? new JTextField(editMe.action, 40) : new JTextField(40));
2324         tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
2325         tf.getAccessibleContext().setAccessibleName(PERM_ACTIONS);
2326         if (edit) {
2327             setPermissionActions(getPerm(editMe.permission, true), choice, tf);
2328         }
2329         tw.addNewComponent(newTD, choice, PD_ACTIONS_CHOICE,
2330                            0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2331                            ToolWindow.LR_BOTTOM_PADDING);
2332         tw.addNewComponent(newTD, tf, PD_ACTIONS_TEXTFIELD,
2333                            1, 3, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH,
2334                            ToolWindow.LR_BOTTOM_PADDING);
2335         choice.addItemListener(new PermissionActionsMenuListener(newTD));
2336 
2337         // signedby label and textfield
2338         label = new JLabel(PolicyTool.getMessage("Signed.By."));
2339         tw.addNewComponent(newTD, label, PD_SIGNEDBY_LABEL,
2340                            0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2341                            ToolWindow.LR_BOTTOM_PADDING);
2342         tf = (edit ? new JTextField(editMe.signedBy, 40) : new JTextField(40));
2343         tf.setPreferredSize(new Dimension(tf.getPreferredSize().width, TEXTFIELD_HEIGHT));
2344         tf.getAccessibleContext().setAccessibleName(
2345                 PolicyTool.getMessage("Signed.By."));
2346         tw.addNewComponent(newTD, tf, PD_SIGNEDBY_TEXTFIELD,
2347                            1, 4, 1, 1, 1.0, 0.0, GridBagConstraints.BOTH,
2348                            ToolWindow.LR_BOTTOM_PADDING);
2349 
2350         // OK button
2351         JButton okButton = new JButton(PolicyTool.getMessage("OK"));
2352         okButton.addActionListener(
2353             new NewPolicyPermOKButtonListener
2354                                     (tool, tw, this, newTD, edit));
2355         tw.addNewComponent(newTD, okButton, PD_OK_BUTTON,
2356                            0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
2357                            ToolWindow.TOP_BOTTOM_PADDING);
2358 
2359         // cancel button
2360         JButton cancelButton = new JButton(PolicyTool.getMessage("Cancel"));
2361         ActionListener cancelListener = new CancelButtonListener(newTD);
2362         cancelButton.addActionListener(cancelListener);
2363         tw.addNewComponent(newTD, cancelButton, PD_CANCEL_BUTTON,
2364                            1, 5, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
2365                            ToolWindow.TOP_BOTTOM_PADDING);
2366 
2367         newTD.getRootPane().setDefaultButton(okButton);
2368         newTD.getRootPane().registerKeyboardAction(cancelListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
2369 
2370         newTD.pack();
2371         newTD.setLocationRelativeTo(tw);
2372         newTD.setVisible(true);
2373     }
2374 
2375     /**
2376      * construct a Principal object from the Principal Info Dialog Box
2377      */
2378     PolicyParser.PrincipalEntry getPrinFromDialog() throws Exception {
2379 
2380         JTextField tf = (JTextField)getComponent(PRD_PRIN_TEXTFIELD);
2381         String pclass = new String(tf.getText().trim());
2382         tf = (JTextField)getComponent(PRD_NAME_TEXTFIELD);
2383         String pname = new String(tf.getText().trim());
2384         if (pclass.equals("*")) {
2385             pclass = PolicyParser.PrincipalEntry.WILDCARD_CLASS;
2386         }
2387         if (pname.equals("*")) {
2388             pname = PolicyParser.PrincipalEntry.WILDCARD_NAME;
2389         }
2390 
2391         PolicyParser.PrincipalEntry pppe = null;
2392 
2393         if ((pclass.equals(PolicyParser.PrincipalEntry.WILDCARD_CLASS)) &&
2394             (!pname.equals(PolicyParser.PrincipalEntry.WILDCARD_NAME))) {
2395             throw new Exception
2396                         (PolicyTool.getMessage("Cannot.Specify.Principal.with.a.Wildcard.Class.without.a.Wildcard.Name"));
2397         } else if (pname.equals("")) {
2398             throw new Exception
2399                         (PolicyTool.getMessage("Cannot.Specify.Principal.without.a.Name"));
2400         } else if (pclass.equals("")) {
2401             // make this consistent with what PolicyParser does
2402             // when it sees an empty principal class
2403             pclass = PolicyParser.PrincipalEntry.REPLACE_NAME;
2404             tool.warnings.addElement(
2405                         "Warning: Principal name '" + pname +
2406                                 "' specified without a Principal class.\n" +
2407                         "\t'" + pname + "' will be interpreted " +
2408                                 "as a key store alias.\n" +
2409                         "\tThe final principal class will be " +
2410                                 ToolDialog.X500_PRIN_CLASS + ".\n" +
2411                         "\tThe final principal name will be " +
2412                                 "determined by the following:\n" +
2413                         "\n" +
2414                         "\tIf the key store entry identified by '"
2415                                 + pname + "'\n" +
2416                         "\tis a key entry, then the principal name will be\n" +
2417                         "\tthe subject distinguished name from the first\n" +
2418                         "\tcertificate in the entry's certificate chain.\n" +
2419                         "\n" +
2420                         "\tIf the key store entry identified by '" +
2421                                 pname + "'\n" +
2422                         "\tis a trusted certificate entry, then the\n" +
2423                         "\tprincipal name will be the subject distinguished\n" +
2424                         "\tname from the trusted public key certificate.");
2425             tw.displayStatusDialog(this,
2426                         "'" + pname + "' will be interpreted as a key " +
2427                         "store alias.  View Warning Log for details.");
2428         }
2429         return new PolicyParser.PrincipalEntry(pclass, pname);
2430     }
2431 
2432 
2433     /**
2434      * construct a Permission object from the Permission Info Dialog Box
2435      */
2436     PolicyParser.PermissionEntry getPermFromDialog() {
2437 
2438         JTextField tf = (JTextField)getComponent(PD_PERM_TEXTFIELD);
2439         String permission = new String(tf.getText().trim());
2440         tf = (JTextField)getComponent(PD_NAME_TEXTFIELD);
2441         String name = null;
2442         if (tf.getText().trim().equals("") == false)
2443             name = new String(tf.getText().trim());
2444         if (permission.equals("") ||
2445             (!permission.equals(ALL_PERM_CLASS) && name == null)) {
2446             throw new InvalidParameterException(PolicyTool.getMessage
2447                 ("Permission.and.Target.Name.must.have.a.value"));
2448         }
2449 
2450         // When the permission is FilePermission, we need to check the name
2451         // to make sure it's not escaped. We believe --
2452         //
2453         // String             name.lastIndexOf("\\\\")
2454         // ----------------   ------------------------
2455         // c:\foo\bar         -1, legal
2456         // c:\\foo\\bar       2, illegal
2457         // \\server\share     0, legal
2458         // \\\\server\share   2, illegal
2459 
2460         if (permission.equals(FILE_PERM_CLASS) && name.lastIndexOf("\\\\") > 0) {
2461             char result = tw.displayYesNoDialog(this,
2462                     PolicyTool.getMessage("Warning"),
2463                     PolicyTool.getMessage(
2464                         "Warning.File.name.may.include.escaped.backslash.characters.It.is.not.necessary.to.escape.backslash.characters.the.tool.escapes"),
2465                     PolicyTool.getMessage("Retain"),
2466                     PolicyTool.getMessage("Edit")
2467                     );
2468             if (result != 'Y') {
2469                 // an invisible exception
2470                 throw new NoDisplayException();
2471             }
2472         }
2473         // get the Actions
2474         tf = (JTextField)getComponent(PD_ACTIONS_TEXTFIELD);
2475         String actions = null;
2476         if (tf.getText().trim().equals("") == false)
2477             actions = new String(tf.getText().trim());
2478 
2479         // get the Signed By
2480         tf = (JTextField)getComponent(PD_SIGNEDBY_TEXTFIELD);
2481         String signedBy = null;
2482         if (tf.getText().trim().equals("") == false)
2483             signedBy = new String(tf.getText().trim());
2484 
2485         PolicyParser.PermissionEntry pppe = new PolicyParser.PermissionEntry
2486                                 (permission, name, actions);
2487         pppe.signedBy = signedBy;
2488 
2489         // see if the signers have public keys
2490         if (signedBy != null) {
2491                 String signers[] = tool.parseSigners(pppe.signedBy);
2492                 for (int i = 0; i < signers.length; i++) {
2493                 try {
2494                     PublicKey pubKey = tool.getPublicKeyAlias(signers[i]);
2495                     if (pubKey == null) {
2496                         MessageFormat form = new MessageFormat
2497                             (PolicyTool.getMessage
2498                             ("Warning.A.public.key.for.alias.signers.i.does.not.exist.Make.sure.a.KeyStore.is.properly.configured."));
2499                         Object[] source = {signers[i]};
2500                         tool.warnings.addElement(form.format(source));
2501                         tw.displayStatusDialog(this, form.format(source));
2502                     }
2503                 } catch (Exception e) {
2504                     tw.displayErrorDialog(this, e);
2505                 }
2506             }
2507         }
2508         return pppe;
2509     }
2510 
2511     /**
2512      * confirm that the user REALLY wants to remove the Policy Entry
2513      */
2514     void displayConfirmRemovePolicyEntry() {
2515 
2516         // find the entry to be removed
2517         JList list = (JList)tw.getComponent(ToolWindow.MW_POLICY_LIST);
2518         int index = list.getSelectedIndex();
2519         PolicyEntry entries[] = tool.getEntry();
2520 
2521         // find where the PolicyTool gui is
2522         Point location = tw.getLocationOnScreen();
2523         //setBounds(location.x + 25, location.y + 100, 600, 400);
2524         setLayout(new GridBagLayout());
2525 
2526         // ask the user do they really want to do this?
2527         JLabel label = new JLabel
2528                 (PolicyTool.getMessage("Remove.this.Policy.Entry."));
2529         tw.addNewComponent(this, label, CRPE_LABEL1,
2530                            0, 0, 2, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2531                            ToolWindow.BOTTOM_PADDING);
2532 
2533         // display the policy entry
2534         label = new JLabel(entries[index].codebaseToString());
2535         tw.addNewComponent(this, label, CRPE_LABEL2,
2536                         0, 1, 2, 1, 0.0, 0.0, GridBagConstraints.BOTH);
2537         label = new JLabel(entries[index].principalsToString().trim());
2538         tw.addNewComponent(this, label, CRPE_LABEL2+1,
2539                         0, 2, 2, 1, 0.0, 0.0, GridBagConstraints.BOTH);
2540         Vector<PolicyParser.PermissionEntry> perms =
2541                         entries[index].getGrantEntry().permissionEntries;
2542         for (int i = 0; i < perms.size(); i++) {
2543             PolicyParser.PermissionEntry nextPerm = perms.elementAt(i);
2544             String permString = ToolDialog.PermissionEntryToUserFriendlyString(nextPerm);
2545             label = new JLabel("    " + permString);
2546             if (i == (perms.size()-1)) {
2547                 tw.addNewComponent(this, label, CRPE_LABEL2 + 2 + i,
2548                                  1, 3 + i, 1, 1, 0.0, 0.0,
2549                                  GridBagConstraints.BOTH,
2550                                  ToolWindow.BOTTOM_PADDING);
2551             } else {
2552                 tw.addNewComponent(this, label, CRPE_LABEL2 + 2 + i,
2553                                  1, 3 + i, 1, 1, 0.0, 0.0,
2554                                  GridBagConstraints.BOTH);
2555             }
2556         }
2557 
2558 
2559         // add OK/CANCEL buttons in a new panel
2560         JPanel panel = new JPanel();
2561         panel.setLayout(new GridBagLayout());
2562 
2563         // OK button
2564         JButton okButton = new JButton(PolicyTool.getMessage("OK"));
2565         okButton.addActionListener
2566                 (new ConfirmRemovePolicyEntryOKButtonListener(tool, tw, this));
2567         tw.addNewComponent(panel, okButton, CRPE_PANEL_OK,
2568                            0, 0, 1, 1, 0.0, 0.0,
2569                            GridBagConstraints.VERTICAL, ToolWindow.LR_PADDING);
2570 
2571         // cancel button
2572         JButton cancelButton = new JButton(PolicyTool.getMessage("Cancel"));
2573         ActionListener cancelListener = new CancelButtonListener(this);
2574         cancelButton.addActionListener(cancelListener);
2575         tw.addNewComponent(panel, cancelButton, CRPE_PANEL_CANCEL,
2576                            1, 0, 1, 1, 0.0, 0.0,
2577                            GridBagConstraints.VERTICAL, ToolWindow.LR_PADDING);
2578 
2579         tw.addNewComponent(this, panel, CRPE_LABEL2 + 2 + perms.size(),
2580                            0, 3 + perms.size(), 2, 1, 0.0, 0.0,
2581                            GridBagConstraints.VERTICAL, ToolWindow.TOP_BOTTOM_PADDING);
2582 
2583         getRootPane().setDefaultButton(okButton);
2584         getRootPane().registerKeyboardAction(cancelListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
2585 
2586         pack();
2587         setLocationRelativeTo(tw);
2588         setVisible(true);
2589     }
2590 
2591     /**
2592      * perform SAVE AS
2593      */
2594     void displaySaveAsDialog(int nextEvent) {
2595 
2596         // pop up a dialog box for the user to enter a filename.
2597         FileDialog fd = new FileDialog
2598                 (tw, PolicyTool.getMessage("Save.As"), FileDialog.SAVE);
2599         fd.addWindowListener(new WindowAdapter() {
2600             public void windowClosing(WindowEvent e) {
2601                 e.getWindow().setVisible(false);
2602             }
2603         });
2604         fd.setVisible(true);
2605 
2606         // see if the user hit cancel
2607         if (fd.getFile() == null ||
2608             fd.getFile().equals(""))
2609             return;
2610 
2611         // get the entered filename
2612         File saveAsFile = new File(fd.getDirectory(), fd.getFile());
2613         String filename = saveAsFile.getPath();
2614         fd.dispose();
2615 
2616         try {
2617             // save the policy entries to a file
2618             tool.savePolicy(filename);
2619 
2620             // display status
2621             MessageFormat form = new MessageFormat(PolicyTool.getMessage
2622                     ("Policy.successfully.written.to.filename"));
2623             Object[] source = {filename};
2624             tw.displayStatusDialog(null, form.format(source));
2625 
2626             // display the new policy filename
2627             JTextField newFilename = (JTextField)tw.getComponent
2628                             (ToolWindow.MW_FILENAME_TEXTFIELD);
2629             newFilename.setText(filename);
2630             tw.setVisible(true);
2631 
2632             // now continue with the originally requested command
2633             // (QUIT, NEW, or OPEN)
2634             userSaveContinue(tool, tw, this, nextEvent);
2635 
2636         } catch (FileNotFoundException fnfe) {
2637             if (filename == null || filename.equals("")) {
2638                 tw.displayErrorDialog(null, new FileNotFoundException
2639                             (PolicyTool.getMessage("null.filename")));
2640             } else {
2641                 tw.displayErrorDialog(null, fnfe);
2642             }
2643         } catch (Exception ee) {
2644             tw.displayErrorDialog(null, ee);
2645         }
2646     }
2647 
2648     /**
2649      * ask user if they want to save changes
2650      */
2651     void displayUserSave(int select) {
2652 
2653         if (tool.modified == true) {
2654 
2655             // find where the PolicyTool gui is
2656             Point location = tw.getLocationOnScreen();
2657             //setBounds(location.x + 75, location.y + 100, 400, 150);
2658             setLayout(new GridBagLayout());
2659 
2660             JLabel label = new JLabel
2661                 (PolicyTool.getMessage("Save.changes."));
2662             tw.addNewComponent(this, label, USC_LABEL,
2663                                0, 0, 3, 1, 0.0, 0.0, GridBagConstraints.BOTH,
2664                                ToolWindow.L_TOP_BOTTOM_PADDING);
2665 
2666             JPanel panel = new JPanel();
2667             panel.setLayout(new GridBagLayout());
2668 
2669             JButton yesButton = new JButton();
2670             ToolWindow.configureButton(yesButton, "Yes");
2671             yesButton.addActionListener
2672                         (new UserSaveYesButtonListener(this, tool, tw, select));
2673             tw.addNewComponent(panel, yesButton, USC_YES_BUTTON,
2674                                0, 0, 1, 1, 0.0, 0.0,
2675                                GridBagConstraints.VERTICAL,
2676                                ToolWindow.LR_BOTTOM_PADDING);
2677             JButton noButton = new JButton();
2678             ToolWindow.configureButton(noButton, "No");
2679             noButton.addActionListener
2680                         (new UserSaveNoButtonListener(this, tool, tw, select));
2681             tw.addNewComponent(panel, noButton, USC_NO_BUTTON,
2682                                1, 0, 1, 1, 0.0, 0.0,
2683                                GridBagConstraints.VERTICAL,
2684                                ToolWindow.LR_BOTTOM_PADDING);
2685             JButton cancelButton = new JButton();
2686             ToolWindow.configureButton(cancelButton, "Cancel");
2687             ActionListener cancelListener = new CancelButtonListener(this);
2688             cancelButton.addActionListener(cancelListener);
2689             tw.addNewComponent(panel, cancelButton, USC_CANCEL_BUTTON,
2690                                2, 0, 1, 1, 0.0, 0.0,
2691                                GridBagConstraints.VERTICAL,
2692                                ToolWindow.LR_BOTTOM_PADDING);
2693 
2694             tw.addNewComponent(this, panel, USC_PANEL,
2695                                0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH);
2696 
2697             getRootPane().registerKeyboardAction(cancelListener, escKey, JComponent.WHEN_IN_FOCUSED_WINDOW);
2698 
2699             pack();
2700             setLocationRelativeTo(tw);
2701             setVisible(true);
2702         } else {
2703             // just do the original request (QUIT, NEW, or OPEN)
2704             userSaveContinue(tool, tw, this, select);
2705         }
2706     }
2707 
2708     /**
2709      * when the user sees the 'YES', 'NO', 'CANCEL' buttons on the
2710      * displayUserSave dialog, and the click on one of them,
2711      * we need to continue the originally requested action
2712      * (either QUITting, opening NEW policy file, or OPENing an existing
2713      * policy file.  do that now.
2714      */
2715     @SuppressWarnings("fallthrough")
2716     void userSaveContinue(PolicyTool tool, ToolWindow tw,
2717                         ToolDialog us, int select) {
2718 
2719         // now either QUIT, open a NEW policy file, or OPEN an existing policy
2720         switch(select) {
2721         case ToolDialog.QUIT:
2722 
2723             tw.setVisible(false);
2724             tw.dispose();
2725             System.exit(0);
2726 
2727         case ToolDialog.NEW:
2728 
2729             try {
2730                 tool.openPolicy(null);
2731             } catch (Exception ee) {
2732                 tool.modified = false;
2733                 tw.displayErrorDialog(null, ee);
2734             }
2735 
2736             // display the policy entries via the policy list textarea
2737             JList list = new JList(new DefaultListModel());
2738             list.setVisibleRowCount(15);
2739             list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
2740             list.addMouseListener(new PolicyListListener(tool, tw));
2741             tw.replacePolicyList(list);
2742 
2743             // display null policy filename and keystore
2744             JTextField newFilename = (JTextField)tw.getComponent(
2745                     ToolWindow.MW_FILENAME_TEXTFIELD);
2746             newFilename.setText("");
2747             tw.setVisible(true);
2748             break;
2749 
2750         case ToolDialog.OPEN:
2751 
2752             // pop up a dialog box for the user to enter a filename.
2753             FileDialog fd = new FileDialog
2754                 (tw, PolicyTool.getMessage("Open"), FileDialog.LOAD);
2755             fd.addWindowListener(new WindowAdapter() {
2756                 public void windowClosing(WindowEvent e) {
2757                     e.getWindow().setVisible(false);
2758                 }
2759             });
2760             fd.setVisible(true);
2761 
2762             // see if the user hit 'cancel'
2763             if (fd.getFile() == null ||
2764                 fd.getFile().equals(""))
2765                 return;
2766 
2767             // get the entered filename
2768             String policyFile = new File(fd.getDirectory(), fd.getFile()).getPath();
2769 
2770             try {
2771                 // open the policy file
2772                 tool.openPolicy(policyFile);
2773 
2774                 // display the policy entries via the policy list textarea
2775                 DefaultListModel listModel = new DefaultListModel();
2776                 list = new JList(listModel);
2777                 list.setVisibleRowCount(15);
2778                 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
2779                 list.addMouseListener(new PolicyListListener(tool, tw));
2780                 PolicyEntry entries[] = tool.getEntry();
2781                 if (entries != null) {
2782                     for (int i = 0; i < entries.length; i++) {
2783                         listModel.addElement(entries[i].headerToString());
2784                     }
2785                 }
2786                 tw.replacePolicyList(list);
2787                 tool.modified = false;
2788 
2789                 // display the new policy filename
2790                 newFilename = (JTextField)tw.getComponent(
2791                         ToolWindow.MW_FILENAME_TEXTFIELD);
2792                 newFilename.setText(policyFile);
2793                 tw.setVisible(true);
2794 
2795                 // inform user of warnings
2796                 if (tool.newWarning == true) {
2797                     tw.displayStatusDialog(null, PolicyTool.getMessage
2798                         ("Errors.have.occurred.while.opening.the.policy.configuration.View.the.Warning.Log.for.more.information."));
2799                 }
2800 
2801             } catch (Exception e) {
2802                 // add blank policy listing
2803                 list = new JList(new DefaultListModel());
2804                 list.setVisibleRowCount(15);
2805                 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
2806                 list.addMouseListener(new PolicyListListener(tool, tw));
2807                 tw.replacePolicyList(list);
2808                 tool.setPolicyFileName(null);
2809                 tool.modified = false;
2810 
2811                 // display a null policy filename
2812                 newFilename = (JTextField)tw.getComponent(
2813                         ToolWindow.MW_FILENAME_TEXTFIELD);
2814                 newFilename.setText("");
2815                 tw.setVisible(true);
2816 
2817                 // display the error
2818                 MessageFormat form = new MessageFormat(PolicyTool.getMessage
2819                     ("Could.not.open.policy.file.policyFile.e.toString."));
2820                 Object[] source = {policyFile, e.toString()};
2821                 tw.displayErrorDialog(null, form.format(source));
2822             }
2823             break;
2824         }
2825     }
2826 
2827     /**
2828      * Return a Menu list of names for a given permission
2829      *
2830      * If inputPerm's TARGETS are null, then this means TARGETS are
2831      * not allowed to be entered (and the TextField is set to be
2832      * non-editable).
2833      *
2834      * If TARGETS are valid but there are no standard ones
2835      * (user must enter them by hand) then the TARGETS array may be empty
2836      * (and of course non-null).
2837      */
2838     void setPermissionNames(Perm inputPerm, JComboBox names, JTextField field) {
2839         names.removeAllItems();
2840         names.addItem(PERM_NAME);
2841 
2842         if (inputPerm == null) {
2843             // custom permission
2844             field.setEditable(true);
2845         } else if (inputPerm.TARGETS == null) {
2846             // standard permission with no targets
2847             field.setEditable(false);
2848         } else {
2849             // standard permission with standard targets
2850             field.setEditable(true);
2851             for (int i = 0; i < inputPerm.TARGETS.length; i++) {
2852                 names.addItem(inputPerm.TARGETS[i]);
2853             }
2854         }
2855     }
2856 
2857     /**
2858      * Return a Menu list of actions for a given permission
2859      *
2860      * If inputPerm's ACTIONS are null, then this means ACTIONS are
2861      * not allowed to be entered (and the TextField is set to be
2862      * non-editable).  This is typically true for BasicPermissions.
2863      *
2864      * If ACTIONS are valid but there are no standard ones
2865      * (user must enter them by hand) then the ACTIONS array may be empty
2866      * (and of course non-null).
2867      */
2868     void setPermissionActions(Perm inputPerm, JComboBox actions, JTextField field) {
2869         actions.removeAllItems();
2870         actions.addItem(PERM_ACTIONS);
2871 
2872         if (inputPerm == null) {
2873             // custom permission
2874             field.setEditable(true);
2875         } else if (inputPerm.ACTIONS == null) {
2876             // standard permission with no actions
2877             field.setEditable(false);
2878         } else {
2879             // standard permission with standard actions
2880             field.setEditable(true);
2881             for (int i = 0; i < inputPerm.ACTIONS.length; i++) {
2882                 actions.addItem(inputPerm.ACTIONS[i]);
2883             }
2884         }
2885     }
2886 
2887     static String PermissionEntryToUserFriendlyString(PolicyParser.PermissionEntry pppe) {
2888         String result = pppe.permission;
2889         if (pppe.name != null) {
2890             result += " " + pppe.name;
2891         }
2892         if (pppe.action != null) {
2893             result += ", \"" + pppe.action + "\"";
2894         }
2895         if (pppe.signedBy != null) {
2896             result += ", signedBy " + pppe.signedBy;
2897         }
2898         return result;
2899     }
2900 
2901     static String PrincipalEntryToUserFriendlyString(PolicyParser.PrincipalEntry pppe) {
2902         StringWriter sw = new StringWriter();
2903         PrintWriter pw = new PrintWriter(sw);
2904         pppe.write(pw);
2905         return sw.toString();
2906     }
2907 }
2908 
2909 /**
2910  * Event handler for the PolicyTool window
2911  */
2912 class ToolWindowListener implements WindowListener {
2913 
2914     private PolicyTool tool;
2915     private ToolWindow tw;
2916 
2917     ToolWindowListener(PolicyTool tool, ToolWindow tw) {
2918         this.tool = tool;
2919         this.tw = tw;
2920     }
2921 
2922     public void windowOpened(WindowEvent we) {
2923     }
2924 
2925     public void windowClosing(WindowEvent we) {
2926         // Closing the window acts the same as choosing Menu->Exit.
2927 
2928         // ask user if they want to save changes
2929         ToolDialog td = new ToolDialog(PolicyTool.getMessage("Save.Changes"), tool, tw, true);
2930         td.displayUserSave(ToolDialog.QUIT);
2931 
2932         // the above method will perform the QUIT as long as the
2933         // user does not CANCEL the request
2934     }
2935 
2936     public void windowClosed(WindowEvent we) {
2937         System.exit(0);
2938     }
2939 
2940     public void windowIconified(WindowEvent we) {
2941     }
2942 
2943     public void windowDeiconified(WindowEvent we) {
2944     }
2945 
2946     public void windowActivated(WindowEvent we) {
2947     }
2948 
2949     public void windowDeactivated(WindowEvent we) {
2950     }
2951 }
2952 
2953 /**
2954  * Event handler for the Policy List
2955  */
2956 class PolicyListListener extends MouseAdapter implements ActionListener {
2957 
2958     private PolicyTool tool;
2959     private ToolWindow tw;
2960 
2961     PolicyListListener(PolicyTool tool, ToolWindow tw) {
2962         this.tool = tool;
2963         this.tw = tw;
2964 
2965     }
2966 
2967     public void actionPerformed(ActionEvent e) {
2968 
2969         // display the permission list for a policy entry
2970         ToolDialog td = new ToolDialog
2971                 (PolicyTool.getMessage("Policy.Entry"), tool, tw, true);
2972         td.displayPolicyEntryDialog(true);
2973     }
2974 
2975     public void mouseClicked(MouseEvent evt) {
2976         if (evt.getClickCount() == 2) {
2977             actionPerformed(null);
2978         }
2979     }
2980 }
2981 
2982 /**
2983  * Event handler for the File Menu
2984  */
2985 class FileMenuListener implements ActionListener {
2986 
2987     private PolicyTool tool;
2988     private ToolWindow tw;
2989 
2990     FileMenuListener(PolicyTool tool, ToolWindow tw) {
2991         this.tool = tool;
2992         this.tw = tw;
2993     }
2994 
2995     public void actionPerformed(ActionEvent e) {
2996 
2997         if (PolicyTool.collator.compare(e.getActionCommand(),
2998                                        ToolWindow.QUIT) == 0) {
2999 
3000             // ask user if they want to save changes
3001             ToolDialog td = new ToolDialog
3002                 (PolicyTool.getMessage("Save.Changes"), tool, tw, true);
3003             td.displayUserSave(ToolDialog.QUIT);
3004 
3005             // the above method will perform the QUIT as long as the
3006             // user does not CANCEL the request
3007 
3008         } else if (PolicyTool.collator.compare(e.getActionCommand(),
3009                                    ToolWindow.NEW_POLICY_FILE) == 0) {
3010 
3011             // ask user if they want to save changes
3012             ToolDialog td = new ToolDialog
3013                 (PolicyTool.getMessage("Save.Changes"), tool, tw, true);
3014             td.displayUserSave(ToolDialog.NEW);
3015 
3016             // the above method will perform the NEW as long as the
3017             // user does not CANCEL the request
3018 
3019         } else if (PolicyTool.collator.compare(e.getActionCommand(),
3020                                   ToolWindow.OPEN_POLICY_FILE) == 0) {
3021 
3022             // ask user if they want to save changes
3023             ToolDialog td = new ToolDialog
3024                 (PolicyTool.getMessage("Save.Changes"), tool, tw, true);
3025             td.displayUserSave(ToolDialog.OPEN);
3026 
3027             // the above method will perform the OPEN as long as the
3028             // user does not CANCEL the request
3029 
3030         } else if (PolicyTool.collator.compare(e.getActionCommand(),
3031                                   ToolWindow.SAVE_POLICY_FILE) == 0) {
3032 
3033             // get the previously entered filename
3034             String filename = ((JTextField)tw.getComponent(
3035                     ToolWindow.MW_FILENAME_TEXTFIELD)).getText();
3036 
3037             // if there is no filename, do a SAVE_AS
3038             if (filename == null || filename.length() == 0) {
3039                 // user wants to SAVE AS
3040                 ToolDialog td = new ToolDialog
3041                         (PolicyTool.getMessage("Save.As"), tool, tw, true);
3042                 td.displaySaveAsDialog(ToolDialog.NOACTION);
3043             } else {
3044                 try {
3045                     // save the policy entries to a file
3046                     tool.savePolicy(filename);
3047 
3048                     // display status
3049                     MessageFormat form = new MessageFormat
3050                         (PolicyTool.getMessage
3051                         ("Policy.successfully.written.to.filename"));
3052                     Object[] source = {filename};
3053                     tw.displayStatusDialog(null, form.format(source));
3054                 } catch (FileNotFoundException fnfe) {
3055                     if (filename == null || filename.equals("")) {
3056                         tw.displayErrorDialog(null, new FileNotFoundException
3057                                 (PolicyTool.getMessage("null.filename")));
3058                     } else {
3059                         tw.displayErrorDialog(null, fnfe);
3060                     }
3061                 } catch (Exception ee) {
3062                     tw.displayErrorDialog(null, ee);
3063                 }
3064             }
3065         } else if (PolicyTool.collator.compare(e.getActionCommand(),
3066                                ToolWindow.SAVE_AS_POLICY_FILE) == 0) {
3067 
3068             // user wants to SAVE AS
3069             ToolDialog td = new ToolDialog
3070                 (PolicyTool.getMessage("Save.As"), tool, tw, true);
3071             td.displaySaveAsDialog(ToolDialog.NOACTION);
3072 
3073         } else if (PolicyTool.collator.compare(e.getActionCommand(),
3074                                      ToolWindow.VIEW_WARNINGS) == 0) {
3075             tw.displayWarningLog(null);
3076         }
3077     }
3078 }
3079 
3080 /**
3081  * Event handler for the main window buttons and Edit Menu
3082  */
3083 class MainWindowListener implements ActionListener {
3084 
3085     private PolicyTool tool;
3086     private ToolWindow tw;
3087 
3088     MainWindowListener(PolicyTool tool, ToolWindow tw) {
3089         this.tool = tool;
3090         this.tw = tw;
3091     }
3092 
3093     public void actionPerformed(ActionEvent e) {
3094 
3095         if (PolicyTool.collator.compare(e.getActionCommand(),
3096                            ToolWindow.ADD_POLICY_ENTRY) == 0) {
3097 
3098             // display a dialog box for the user to enter policy info
3099             ToolDialog td = new ToolDialog
3100                 (PolicyTool.getMessage("Policy.Entry"), tool, tw, true);
3101             td.displayPolicyEntryDialog(false);
3102 
3103         } else if (PolicyTool.collator.compare(e.getActionCommand(),
3104                                ToolWindow.REMOVE_POLICY_ENTRY) == 0) {
3105 
3106             // get the selected entry
3107             JList list = (JList)tw.getComponent(ToolWindow.MW_POLICY_LIST);
3108             int index = list.getSelectedIndex();
3109             if (index < 0) {
3110                 tw.displayErrorDialog(null, new Exception
3111                         (PolicyTool.getMessage("No.Policy.Entry.selected")));
3112                 return;
3113             }
3114 
3115             // ask the user if they really want to remove the policy entry
3116             ToolDialog td = new ToolDialog(PolicyTool.getMessage
3117                 ("Remove.Policy.Entry"), tool, tw, true);
3118             td.displayConfirmRemovePolicyEntry();
3119 
3120         } else if (PolicyTool.collator.compare(e.getActionCommand(),
3121                                  ToolWindow.EDIT_POLICY_ENTRY) == 0) {
3122 
3123             // get the selected entry
3124             JList list = (JList)tw.getComponent(ToolWindow.MW_POLICY_LIST);
3125             int index = list.getSelectedIndex();
3126             if (index < 0) {
3127                 tw.displayErrorDialog(null, new Exception
3128                         (PolicyTool.getMessage("No.Policy.Entry.selected")));
3129                 return;
3130             }
3131 
3132             // display the permission list for a policy entry
3133             ToolDialog td = new ToolDialog
3134                 (PolicyTool.getMessage("Policy.Entry"), tool, tw, true);
3135             td.displayPolicyEntryDialog(true);
3136 
3137         } else if (PolicyTool.collator.compare(e.getActionCommand(),
3138                                      ToolWindow.EDIT_KEYSTORE) == 0) {
3139 
3140             // display a dialog box for the user to enter keystore info
3141             ToolDialog td = new ToolDialog
3142                 (PolicyTool.getMessage("KeyStore"), tool, tw, true);
3143             td.keyStoreDialog(ToolDialog.EDIT_KEYSTORE);
3144         }
3145     }
3146 }
3147 
3148 /**
3149  * Event handler for AddEntryDoneButton button
3150  *
3151  * -- if edit is TRUE, then we are EDITing an existing PolicyEntry
3152  *    and we need to update both the policy and the GUI listing.
3153  *    if edit is FALSE, then we are ADDing a new PolicyEntry,
3154  *    so we only need to update the GUI listing.
3155  */
3156 class AddEntryDoneButtonListener implements ActionListener {
3157 
3158     private PolicyTool tool;
3159     private ToolWindow tw;
3160     private ToolDialog td;
3161     private boolean edit;
3162 
3163     AddEntryDoneButtonListener(PolicyTool tool, ToolWindow tw,
3164                                 ToolDialog td, boolean edit) {
3165         this.tool = tool;
3166         this.tw = tw;
3167         this.td = td;
3168         this.edit = edit;
3169     }
3170 
3171     public void actionPerformed(ActionEvent e) {
3172 
3173         try {
3174             // get a PolicyEntry object from the dialog policy info
3175             PolicyEntry newEntry = td.getPolicyEntryFromDialog();
3176             PolicyParser.GrantEntry newGe = newEntry.getGrantEntry();
3177 
3178             // see if all the signers have public keys
3179             if (newGe.signedBy != null) {
3180                 String signers[] = tool.parseSigners(newGe.signedBy);
3181                 for (int i = 0; i < signers.length; i++) {
3182                     PublicKey pubKey = tool.getPublicKeyAlias(signers[i]);
3183                     if (pubKey == null) {
3184                         MessageFormat form = new MessageFormat
3185                             (PolicyTool.getMessage
3186                             ("Warning.A.public.key.for.alias.signers.i.does.not.exist.Make.sure.a.KeyStore.is.properly.configured."));
3187                         Object[] source = {signers[i]};
3188                         tool.warnings.addElement(form.format(source));
3189                         tw.displayStatusDialog(td, form.format(source));
3190                     }
3191                 }
3192             }
3193 
3194             // add the entry
3195             JList policyList = (JList)tw.getComponent(ToolWindow.MW_POLICY_LIST);
3196             if (edit) {
3197                 int listIndex = policyList.getSelectedIndex();
3198                 tool.addEntry(newEntry, listIndex);
3199                 String newCodeBaseStr = newEntry.headerToString();
3200                 if (PolicyTool.collator.compare
3201                         (newCodeBaseStr, policyList.getModel().getElementAt(listIndex)) != 0)
3202                     tool.modified = true;
3203                 ((DefaultListModel)policyList.getModel()).set(listIndex, newCodeBaseStr);
3204             } else {
3205                 tool.addEntry(newEntry, -1);
3206                 ((DefaultListModel)policyList.getModel()).addElement(newEntry.headerToString());
3207                 tool.modified = true;
3208             }
3209             td.setVisible(false);
3210             td.dispose();
3211 
3212         } catch (Exception eee) {
3213             tw.displayErrorDialog(td, eee);
3214         }
3215     }
3216 }
3217 
3218 /**
3219  * Event handler for ChangeKeyStoreOKButton button
3220  */
3221 class ChangeKeyStoreOKButtonListener implements ActionListener {
3222 
3223     private PolicyTool tool;
3224     private ToolWindow tw;
3225     private ToolDialog td;
3226 
3227     ChangeKeyStoreOKButtonListener(PolicyTool tool, ToolWindow tw,
3228                 ToolDialog td) {
3229         this.tool = tool;
3230         this.tw = tw;
3231         this.td = td;
3232     }
3233 
3234     public void actionPerformed(ActionEvent e) {
3235 
3236         String URLString = ((JTextField)td.getComponent(
3237                 ToolDialog.KSD_NAME_TEXTFIELD)).getText().trim();
3238         String type = ((JTextField)td.getComponent(
3239                 ToolDialog.KSD_TYPE_TEXTFIELD)).getText().trim();
3240         String provider = ((JTextField)td.getComponent(
3241                 ToolDialog.KSD_PROVIDER_TEXTFIELD)).getText().trim();
3242         String pwdURL = ((JTextField)td.getComponent(
3243                 ToolDialog.KSD_PWD_URL_TEXTFIELD)).getText().trim();
3244 
3245         try {
3246             tool.openKeyStore
3247                         ((URLString.length() == 0 ? null : URLString),
3248                         (type.length() == 0 ? null : type),
3249                         (provider.length() == 0 ? null : provider),
3250                         (pwdURL.length() == 0 ? null : pwdURL));
3251             tool.modified = true;
3252         } catch (Exception ex) {
3253             MessageFormat form = new MessageFormat(PolicyTool.getMessage
3254                 ("Unable.to.open.KeyStore.ex.toString."));
3255             Object[] source = {ex.toString()};
3256             tw.displayErrorDialog(td, form.format(source));
3257             return;
3258         }
3259 
3260         td.dispose();
3261     }
3262 }
3263 
3264 /**
3265  * Event handler for AddPrinButton button
3266  */
3267 class AddPrinButtonListener implements ActionListener {
3268 
3269     private PolicyTool tool;
3270     private ToolWindow tw;
3271     private ToolDialog td;
3272     private boolean editPolicyEntry;
3273 
3274     AddPrinButtonListener(PolicyTool tool, ToolWindow tw,
3275                                 ToolDialog td, boolean editPolicyEntry) {
3276         this.tool = tool;
3277         this.tw = tw;
3278         this.td = td;
3279         this.editPolicyEntry = editPolicyEntry;
3280     }
3281 
3282     public void actionPerformed(ActionEvent e) {
3283 
3284         // display a dialog box for the user to enter principal info
3285         td.displayPrincipalDialog(editPolicyEntry, false);
3286     }
3287 }
3288 
3289 /**
3290  * Event handler for AddPermButton button
3291  */
3292 class AddPermButtonListener implements ActionListener {
3293 
3294     private PolicyTool tool;
3295     private ToolWindow tw;
3296     private ToolDialog td;
3297     private boolean editPolicyEntry;
3298 
3299     AddPermButtonListener(PolicyTool tool, ToolWindow tw,
3300                                 ToolDialog td, boolean editPolicyEntry) {
3301         this.tool = tool;
3302         this.tw = tw;
3303         this.td = td;
3304         this.editPolicyEntry = editPolicyEntry;
3305     }
3306 
3307     public void actionPerformed(ActionEvent e) {
3308 
3309         // display a dialog box for the user to enter permission info
3310         td.displayPermissionDialog(editPolicyEntry, false);
3311     }
3312 }
3313 
3314 /**
3315  * Event handler for AddPrinOKButton button
3316  */
3317 class NewPolicyPrinOKButtonListener implements ActionListener {
3318 
3319     private PolicyTool tool;
3320     private ToolWindow tw;
3321     private ToolDialog listDialog;
3322     private ToolDialog infoDialog;
3323     private boolean edit;
3324 
3325     NewPolicyPrinOKButtonListener(PolicyTool tool,
3326                                 ToolWindow tw,
3327                                 ToolDialog listDialog,
3328                                 ToolDialog infoDialog,
3329                                 boolean edit) {
3330         this.tool = tool;
3331         this.tw = tw;
3332         this.listDialog = listDialog;
3333         this.infoDialog = infoDialog;
3334         this.edit = edit;
3335     }
3336 
3337     public void actionPerformed(ActionEvent e) {
3338 
3339         try {
3340             // read in the new principal info from Dialog Box
3341             PolicyParser.PrincipalEntry pppe =
3342                         infoDialog.getPrinFromDialog();
3343             if (pppe != null) {
3344                 try {
3345                     tool.verifyPrincipal(pppe.getPrincipalClass(),
3346                                         pppe.getPrincipalName());
3347                 } catch (ClassNotFoundException cnfe) {
3348                     MessageFormat form = new MessageFormat
3349                                 (PolicyTool.getMessage
3350                                 ("Warning.Class.not.found.class"));
3351                     Object[] source = {pppe.getPrincipalClass()};
3352                     tool.warnings.addElement(form.format(source));
3353                     tw.displayStatusDialog(infoDialog, form.format(source));
3354                 }
3355 
3356                 // add the principal to the GUI principal list
3357                 TaggedList prinList =
3358                     (TaggedList)listDialog.getComponent(ToolDialog.PE_PRIN_LIST);
3359 
3360                 String prinString = ToolDialog.PrincipalEntryToUserFriendlyString(pppe);
3361                 if (edit) {
3362                     // if editing, replace the original principal
3363                     int index = prinList.getSelectedIndex();
3364                     prinList.replaceTaggedItem(prinString, pppe, index);
3365                 } else {
3366                     // if adding, just add it to the end
3367                     prinList.addTaggedItem(prinString, pppe);
3368                 }
3369             }
3370             infoDialog.dispose();
3371         } catch (Exception ee) {
3372             tw.displayErrorDialog(infoDialog, ee);
3373         }
3374     }
3375 }
3376 
3377 /**
3378  * Event handler for AddPermOKButton button
3379  */
3380 class NewPolicyPermOKButtonListener implements ActionListener {
3381 
3382     private PolicyTool tool;
3383     private ToolWindow tw;
3384     private ToolDialog listDialog;
3385     private ToolDialog infoDialog;
3386     private boolean edit;
3387 
3388     NewPolicyPermOKButtonListener(PolicyTool tool,
3389                                 ToolWindow tw,
3390                                 ToolDialog listDialog,
3391                                 ToolDialog infoDialog,
3392                                 boolean edit) {
3393         this.tool = tool;
3394         this.tw = tw;
3395         this.listDialog = listDialog;
3396         this.infoDialog = infoDialog;
3397         this.edit = edit;
3398     }
3399 
3400     public void actionPerformed(ActionEvent e) {
3401 
3402         try {
3403             // read in the new permission info from Dialog Box
3404             PolicyParser.PermissionEntry pppe =
3405                         infoDialog.getPermFromDialog();
3406 
3407             try {
3408                 tool.verifyPermission(pppe.permission, pppe.name, pppe.action);
3409             } catch (ClassNotFoundException cnfe) {
3410                 MessageFormat form = new MessageFormat(PolicyTool.getMessage
3411                                 ("Warning.Class.not.found.class"));
3412                 Object[] source = {pppe.permission};
3413                 tool.warnings.addElement(form.format(source));
3414                 tw.displayStatusDialog(infoDialog, form.format(source));
3415             }
3416 
3417             // add the permission to the GUI permission list
3418             TaggedList permList =
3419                 (TaggedList)listDialog.getComponent(ToolDialog.PE_PERM_LIST);
3420 
3421             String permString = ToolDialog.PermissionEntryToUserFriendlyString(pppe);
3422             if (edit) {
3423                 // if editing, replace the original permission
3424                 int which = permList.getSelectedIndex();
3425                 permList.replaceTaggedItem(permString, pppe, which);
3426             } else {
3427                 // if adding, just add it to the end
3428                 permList.addTaggedItem(permString, pppe);
3429             }
3430             infoDialog.dispose();
3431 
3432         } catch (InvocationTargetException ite) {
3433             tw.displayErrorDialog(infoDialog, ite.getTargetException());
3434         } catch (Exception ee) {
3435             tw.displayErrorDialog(infoDialog, ee);
3436         }
3437     }
3438 }
3439 
3440 /**
3441  * Event handler for RemovePrinButton button
3442  */
3443 class RemovePrinButtonListener implements ActionListener {
3444 
3445     private PolicyTool tool;
3446     private ToolWindow tw;
3447     private ToolDialog td;
3448     private boolean edit;
3449 
3450     RemovePrinButtonListener(PolicyTool tool, ToolWindow tw,
3451                                 ToolDialog td, boolean edit) {
3452         this.tool = tool;
3453         this.tw = tw;
3454         this.td = td;
3455         this.edit = edit;
3456     }
3457 
3458     public void actionPerformed(ActionEvent e) {
3459 
3460         // get the Principal selected from the Principal List
3461         TaggedList prinList = (TaggedList)td.getComponent(
3462                 ToolDialog.PE_PRIN_LIST);
3463         int prinIndex = prinList.getSelectedIndex();
3464 
3465         if (prinIndex < 0) {
3466             tw.displayErrorDialog(td, new Exception
3467                 (PolicyTool.getMessage("No.principal.selected")));
3468             return;
3469         }
3470         // remove the principal from the display
3471         prinList.removeTaggedItem(prinIndex);
3472     }
3473 }
3474 
3475 /**
3476  * Event handler for RemovePermButton button
3477  */
3478 class RemovePermButtonListener implements ActionListener {
3479 
3480     private PolicyTool tool;
3481     private ToolWindow tw;
3482     private ToolDialog td;
3483     private boolean edit;
3484 
3485     RemovePermButtonListener(PolicyTool tool, ToolWindow tw,
3486                                 ToolDialog td, boolean edit) {
3487         this.tool = tool;
3488         this.tw = tw;
3489         this.td = td;
3490         this.edit = edit;
3491     }
3492 
3493     public void actionPerformed(ActionEvent e) {
3494 
3495         // get the Permission selected from the Permission List
3496         TaggedList permList = (TaggedList)td.getComponent(
3497                 ToolDialog.PE_PERM_LIST);
3498         int permIndex = permList.getSelectedIndex();
3499 
3500         if (permIndex < 0) {
3501             tw.displayErrorDialog(td, new Exception
3502                 (PolicyTool.getMessage("No.permission.selected")));
3503             return;
3504         }
3505         // remove the permission from the display
3506         permList.removeTaggedItem(permIndex);
3507 
3508     }
3509 }
3510 
3511 /**
3512  * Event handler for Edit Principal button
3513  *
3514  * We need the editPolicyEntry boolean to tell us if the user is
3515  * adding a new PolicyEntry at this time, or editing an existing entry.
3516  * If the user is adding a new PolicyEntry, we ONLY update the
3517  * GUI listing.  If the user is editing an existing PolicyEntry, we
3518  * update both the GUI listing and the actual PolicyEntry.
3519  */
3520 class EditPrinButtonListener extends MouseAdapter implements ActionListener {
3521 
3522     private PolicyTool tool;
3523     private ToolWindow tw;
3524     private ToolDialog td;
3525     private boolean editPolicyEntry;
3526 
3527     EditPrinButtonListener(PolicyTool tool, ToolWindow tw,
3528                                 ToolDialog td, boolean editPolicyEntry) {
3529         this.tool = tool;
3530         this.tw = tw;
3531         this.td = td;
3532         this.editPolicyEntry = editPolicyEntry;
3533     }
3534 
3535     public void actionPerformed(ActionEvent e) {
3536 
3537         // get the Principal selected from the Principal List
3538         TaggedList list = (TaggedList)td.getComponent(
3539                 ToolDialog.PE_PRIN_LIST);
3540         int prinIndex = list.getSelectedIndex();
3541 
3542         if (prinIndex < 0) {
3543             tw.displayErrorDialog(td, new Exception
3544                 (PolicyTool.getMessage("No.principal.selected")));
3545             return;
3546         }
3547         td.displayPrincipalDialog(editPolicyEntry, true);
3548     }
3549 
3550     public void mouseClicked(MouseEvent evt) {
3551         if (evt.getClickCount() == 2) {
3552             actionPerformed(null);
3553         }
3554     }
3555 }
3556 
3557 /**
3558  * Event handler for Edit Permission button
3559  *
3560  * We need the editPolicyEntry boolean to tell us if the user is
3561  * adding a new PolicyEntry at this time, or editing an existing entry.
3562  * If the user is adding a new PolicyEntry, we ONLY update the
3563  * GUI listing.  If the user is editing an existing PolicyEntry, we
3564  * update both the GUI listing and the actual PolicyEntry.
3565  */
3566 class EditPermButtonListener extends MouseAdapter implements ActionListener {
3567 
3568     private PolicyTool tool;
3569     private ToolWindow tw;
3570     private ToolDialog td;
3571     private boolean editPolicyEntry;
3572 
3573     EditPermButtonListener(PolicyTool tool, ToolWindow tw,
3574                                 ToolDialog td, boolean editPolicyEntry) {
3575         this.tool = tool;
3576         this.tw = tw;
3577         this.td = td;
3578         this.editPolicyEntry = editPolicyEntry;
3579     }
3580 
3581     public void actionPerformed(ActionEvent e) {
3582 
3583         // get the Permission selected from the Permission List
3584         JList list = (JList)td.getComponent(ToolDialog.PE_PERM_LIST);
3585         int permIndex = list.getSelectedIndex();
3586 
3587         if (permIndex < 0) {
3588             tw.displayErrorDialog(td, new Exception
3589                 (PolicyTool.getMessage("No.permission.selected")));
3590             return;
3591         }
3592         td.displayPermissionDialog(editPolicyEntry, true);
3593     }
3594 
3595     public void mouseClicked(MouseEvent evt) {
3596         if (evt.getClickCount() == 2) {
3597             actionPerformed(null);
3598         }
3599     }
3600 }
3601 
3602 /**
3603  * Event handler for Principal Popup Menu
3604  */
3605 class PrincipalTypeMenuListener implements ItemListener {
3606 
3607     private ToolDialog td;
3608 
3609     PrincipalTypeMenuListener(ToolDialog td) {
3610         this.td = td;
3611     }
3612 
3613     public void itemStateChanged(ItemEvent e) {
3614         if (e.getStateChange() == ItemEvent.DESELECTED) {
3615             // We're only interested in SELECTED events
3616             return;
3617         }
3618 
3619         JComboBox prin = (JComboBox)td.getComponent(ToolDialog.PRD_PRIN_CHOICE);
3620         JTextField prinField = (JTextField)td.getComponent(
3621                 ToolDialog.PRD_PRIN_TEXTFIELD);
3622         JTextField nameField = (JTextField)td.getComponent(
3623                 ToolDialog.PRD_NAME_TEXTFIELD);
3624 
3625         prin.getAccessibleContext().setAccessibleName(
3626             PolicyTool.splitToWords((String)e.getItem()));
3627         if (((String)e.getItem()).equals(ToolDialog.PRIN_TYPE)) {
3628             // ignore if they choose "Principal Type:" item
3629             if (prinField.getText() != null &&
3630                 prinField.getText().length() > 0) {
3631                 Prin inputPrin = ToolDialog.getPrin(prinField.getText(), true);
3632                 prin.setSelectedItem(inputPrin.CLASS);
3633             }
3634             return;
3635         }
3636 
3637         // if you change the principal, clear the name
3638         if (prinField.getText().indexOf((String)e.getItem()) == -1) {
3639             nameField.setText("");
3640         }
3641 
3642         // set the text in the textfield and also modify the
3643         // pull-down choice menus to reflect the correct possible
3644         // set of names and actions
3645         Prin inputPrin = ToolDialog.getPrin((String)e.getItem(), false);
3646         if (inputPrin != null) {
3647             prinField.setText(inputPrin.FULL_CLASS);
3648         }
3649     }
3650 }
3651 
3652 /**
3653  * Event handler for Permission Popup Menu
3654  */
3655 class PermissionMenuListener implements ItemListener {
3656 
3657     private ToolDialog td;
3658 
3659     PermissionMenuListener(ToolDialog td) {
3660         this.td = td;
3661     }
3662 
3663     public void itemStateChanged(ItemEvent e) {
3664         if (e.getStateChange() == ItemEvent.DESELECTED) {
3665             // We're only interested in SELECTED events
3666             return;
3667         }
3668 
3669         JComboBox perms = (JComboBox)td.getComponent(
3670                 ToolDialog.PD_PERM_CHOICE);
3671         JComboBox names = (JComboBox)td.getComponent(
3672                 ToolDialog.PD_NAME_CHOICE);
3673         JComboBox actions = (JComboBox)td.getComponent(
3674                 ToolDialog.PD_ACTIONS_CHOICE);
3675         JTextField nameField = (JTextField)td.getComponent(
3676                 ToolDialog.PD_NAME_TEXTFIELD);
3677         JTextField actionsField = (JTextField)td.getComponent(
3678                 ToolDialog.PD_ACTIONS_TEXTFIELD);
3679         JTextField permField = (JTextField)td.getComponent(
3680                 ToolDialog.PD_PERM_TEXTFIELD);
3681         JTextField signedbyField = (JTextField)td.getComponent(
3682                 ToolDialog.PD_SIGNEDBY_TEXTFIELD);
3683 
3684         perms.getAccessibleContext().setAccessibleName(
3685             PolicyTool.splitToWords((String)e.getItem()));
3686 
3687         // ignore if they choose the 'Permission:' item
3688         if (PolicyTool.collator.compare((String)e.getItem(),
3689                                       ToolDialog.PERM) == 0) {
3690             if (permField.getText() != null &&
3691                 permField.getText().length() > 0) {
3692 
3693                 Perm inputPerm = ToolDialog.getPerm(permField.getText(), true);
3694                 if (inputPerm != null) {
3695                     perms.setSelectedItem(inputPerm.CLASS);
3696                 }
3697             }
3698             return;
3699         }
3700 
3701         // if you change the permission, clear the name, actions, and signedBy
3702         if (permField.getText().indexOf((String)e.getItem()) == -1) {
3703             nameField.setText("");
3704             actionsField.setText("");
3705             signedbyField.setText("");
3706         }
3707 
3708         // set the text in the textfield and also modify the
3709         // pull-down choice menus to reflect the correct possible
3710         // set of names and actions
3711 
3712         Perm inputPerm = ToolDialog.getPerm((String)e.getItem(), false);
3713         if (inputPerm == null) {
3714             permField.setText("");
3715         } else {
3716             permField.setText(inputPerm.FULL_CLASS);
3717         }
3718         td.setPermissionNames(inputPerm, names, nameField);
3719         td.setPermissionActions(inputPerm, actions, actionsField);
3720     }
3721 }
3722 
3723 /**
3724  * Event handler for Permission Name Popup Menu
3725  */
3726 class PermissionNameMenuListener implements ItemListener {
3727 
3728     private ToolDialog td;
3729 
3730     PermissionNameMenuListener(ToolDialog td) {
3731         this.td = td;
3732     }
3733 
3734     public void itemStateChanged(ItemEvent e) {
3735         if (e.getStateChange() == ItemEvent.DESELECTED) {
3736             // We're only interested in SELECTED events
3737             return;
3738         }
3739 
3740         JComboBox names = (JComboBox)td.getComponent(ToolDialog.PD_NAME_CHOICE);
3741         names.getAccessibleContext().setAccessibleName(
3742             PolicyTool.splitToWords((String)e.getItem()));
3743 
3744         if (((String)e.getItem()).indexOf(ToolDialog.PERM_NAME) != -1)
3745             return;
3746 
3747         JTextField tf = (JTextField)td.getComponent(ToolDialog.PD_NAME_TEXTFIELD);
3748         tf.setText((String)e.getItem());
3749     }
3750 }
3751 
3752 /**
3753  * Event handler for Permission Actions Popup Menu
3754  */
3755 class PermissionActionsMenuListener implements ItemListener {
3756 
3757     private ToolDialog td;
3758 
3759     PermissionActionsMenuListener(ToolDialog td) {
3760         this.td = td;
3761     }
3762 
3763     public void itemStateChanged(ItemEvent e) {
3764         if (e.getStateChange() == ItemEvent.DESELECTED) {
3765             // We're only interested in SELECTED events
3766             return;
3767         }
3768 
3769         JComboBox actions = (JComboBox)td.getComponent(
3770                 ToolDialog.PD_ACTIONS_CHOICE);
3771         actions.getAccessibleContext().setAccessibleName((String)e.getItem());
3772 
3773         if (((String)e.getItem()).indexOf(ToolDialog.PERM_ACTIONS) != -1)
3774             return;
3775 
3776         JTextField tf = (JTextField)td.getComponent(
3777                 ToolDialog.PD_ACTIONS_TEXTFIELD);
3778         if (tf.getText() == null || tf.getText().equals("")) {
3779             tf.setText((String)e.getItem());
3780         } else {
3781             if (tf.getText().indexOf((String)e.getItem()) == -1)
3782                 tf.setText(tf.getText() + ", " + (String)e.getItem());
3783         }
3784     }
3785 }
3786 
3787 /**
3788  * Event handler for all the children dialogs/windows
3789  */
3790 class ChildWindowListener implements WindowListener {
3791 
3792     private ToolDialog td;
3793 
3794     ChildWindowListener(ToolDialog td) {
3795         this.td = td;
3796     }
3797 
3798     public void windowOpened(WindowEvent we) {
3799     }
3800 
3801     public void windowClosing(WindowEvent we) {
3802         // same as pressing the "cancel" button
3803         td.setVisible(false);
3804         td.dispose();
3805     }
3806 
3807     public void windowClosed(WindowEvent we) {
3808     }
3809 
3810     public void windowIconified(WindowEvent we) {
3811     }
3812 
3813     public void windowDeiconified(WindowEvent we) {
3814     }
3815 
3816     public void windowActivated(WindowEvent we) {
3817     }
3818 
3819     public void windowDeactivated(WindowEvent we) {
3820     }
3821 }
3822 
3823 /**
3824  * Event handler for CancelButton button
3825  */
3826 class CancelButtonListener implements ActionListener {
3827 
3828     private ToolDialog td;
3829 
3830     CancelButtonListener(ToolDialog td) {
3831         this.td = td;
3832     }
3833 
3834     public void actionPerformed(ActionEvent e) {
3835         td.setVisible(false);
3836         td.dispose();
3837     }
3838 }
3839 
3840 /**
3841  * Event handler for ErrorOKButton button
3842  */
3843 class ErrorOKButtonListener implements ActionListener {
3844 
3845     private ToolDialog ed;
3846 
3847     ErrorOKButtonListener(ToolDialog ed) {
3848         this.ed = ed;
3849     }
3850 
3851     public void actionPerformed(ActionEvent e) {
3852         ed.setVisible(false);
3853         ed.dispose();
3854     }
3855 }
3856 
3857 /**
3858  * Event handler for StatusOKButton button
3859  */
3860 class StatusOKButtonListener implements ActionListener {
3861 
3862     private ToolDialog sd;
3863 
3864     StatusOKButtonListener(ToolDialog sd) {
3865         this.sd = sd;
3866     }
3867 
3868     public void actionPerformed(ActionEvent e) {
3869         sd.setVisible(false);
3870         sd.dispose();
3871     }
3872 }
3873 
3874 /**
3875  * Event handler for UserSaveYes button
3876  */
3877 class UserSaveYesButtonListener implements ActionListener {
3878 
3879     private ToolDialog us;
3880     private PolicyTool tool;
3881     private ToolWindow tw;
3882     private int select;
3883 
3884     UserSaveYesButtonListener(ToolDialog us, PolicyTool tool,
3885                         ToolWindow tw, int select) {
3886         this.us = us;
3887         this.tool = tool;
3888         this.tw = tw;
3889         this.select = select;
3890     }
3891 
3892     public void actionPerformed(ActionEvent e) {
3893 
3894         // first get rid of the window
3895         us.setVisible(false);
3896         us.dispose();
3897 
3898         try {
3899             String filename = ((JTextField)tw.getComponent(
3900                     ToolWindow.MW_FILENAME_TEXTFIELD)).getText();
3901             if (filename == null || filename.equals("")) {
3902                 us.displaySaveAsDialog(select);
3903 
3904                 // the above dialog will continue with the originally
3905                 // requested command if necessary
3906             } else {
3907                 // save the policy entries to a file
3908                 tool.savePolicy(filename);
3909 
3910                 // display status
3911                 MessageFormat form = new MessageFormat
3912                         (PolicyTool.getMessage
3913                         ("Policy.successfully.written.to.filename"));
3914                 Object[] source = {filename};
3915                 tw.displayStatusDialog(null, form.format(source));
3916 
3917                 // now continue with the originally requested command
3918                 // (QUIT, NEW, or OPEN)
3919                 us.userSaveContinue(tool, tw, us, select);
3920             }
3921         } catch (Exception ee) {
3922             // error -- just report it and bail
3923             tw.displayErrorDialog(null, ee);
3924         }
3925     }
3926 }
3927 
3928 /**
3929  * Event handler for UserSaveNoButton
3930  */
3931 class UserSaveNoButtonListener implements ActionListener {
3932 
3933     private PolicyTool tool;
3934     private ToolWindow tw;
3935     private ToolDialog us;
3936     private int select;
3937 
3938     UserSaveNoButtonListener(ToolDialog us, PolicyTool tool,
3939                         ToolWindow tw, int select) {
3940         this.us = us;
3941         this.tool = tool;
3942         this.tw = tw;
3943         this.select = select;
3944     }
3945 
3946     public void actionPerformed(ActionEvent e) {
3947         us.setVisible(false);
3948         us.dispose();
3949 
3950         // now continue with the originally requested command
3951         // (QUIT, NEW, or OPEN)
3952         us.userSaveContinue(tool, tw, us, select);
3953     }
3954 }
3955 
3956 /**
3957  * Event handler for UserSaveCancelButton
3958  */
3959 class UserSaveCancelButtonListener implements ActionListener {
3960 
3961     private ToolDialog us;
3962 
3963     UserSaveCancelButtonListener(ToolDialog us) {
3964         this.us = us;
3965     }
3966 
3967     public void actionPerformed(ActionEvent e) {
3968         us.setVisible(false);
3969         us.dispose();
3970 
3971         // do NOT continue with the originally requested command
3972         // (QUIT, NEW, or OPEN)
3973     }
3974 }
3975 
3976 /**
3977  * Event handler for ConfirmRemovePolicyEntryOKButtonListener
3978  */
3979 class ConfirmRemovePolicyEntryOKButtonListener implements ActionListener {
3980 
3981     private PolicyTool tool;
3982     private ToolWindow tw;
3983     private ToolDialog us;
3984 
3985     ConfirmRemovePolicyEntryOKButtonListener(PolicyTool tool,
3986                                 ToolWindow tw, ToolDialog us) {
3987         this.tool = tool;
3988         this.tw = tw;
3989         this.us = us;
3990     }
3991 
3992     public void actionPerformed(ActionEvent e) {
3993         // remove the entry
3994         JList list = (JList)tw.getComponent(ToolWindow.MW_POLICY_LIST);
3995         int index = list.getSelectedIndex();
3996         PolicyEntry entries[] = tool.getEntry();
3997         tool.removeEntry(entries[index]);
3998 
3999         // redraw the window listing
4000         DefaultListModel listModel = new DefaultListModel();
4001         list = new JList(listModel);
4002         list.setVisibleRowCount(15);
4003         list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
4004         list.addMouseListener(new PolicyListListener(tool, tw));
4005         entries = tool.getEntry();
4006         if (entries != null) {
4007                 for (int i = 0; i < entries.length; i++) {
4008                     listModel.addElement(entries[i].headerToString());
4009                 }
4010         }
4011         tw.replacePolicyList(list);
4012         us.setVisible(false);
4013         us.dispose();
4014     }
4015 }
4016 
4017 /**
4018  * Just a special name, so that the codes dealing with this exception knows
4019  * it's special, and does not pop out a warning box.
4020  */
4021 class NoDisplayException extends RuntimeException {
4022     private static final long serialVersionUID = -4611761427108719794L;
4023 }
4024 
4025 /**
4026  * This is a java.awt.List that bind an Object to each String it holds.
4027  */
4028 class TaggedList extends JList {
4029     private static final long serialVersionUID = -5676238110427785853L;
4030 
4031     private java.util.List<Object> data = new LinkedList<>();
4032     public TaggedList(int i, boolean b) {
4033         super(new DefaultListModel());
4034         setVisibleRowCount(i);
4035         setSelectionMode(b ? ListSelectionModel.MULTIPLE_INTERVAL_SELECTION : ListSelectionModel.SINGLE_SELECTION);
4036     }
4037 
4038     public Object getObject(int index) {
4039         return data.get(index);
4040     }
4041 
4042     public void addTaggedItem(String string, Object object) {
4043         ((DefaultListModel)getModel()).addElement(string);
4044         data.add(object);
4045     }
4046 
4047     public void replaceTaggedItem(String string, Object object, int index) {
4048         ((DefaultListModel)getModel()).set(index, string);
4049         data.set(index, object);
4050     }
4051 
4052     public void removeTaggedItem(int index) {
4053         ((DefaultListModel)getModel()).remove(index);
4054         data.remove(index);
4055     }
4056 }
4057 
4058 /**
4059  * Convenience Principal Classes
4060  */
4061 
4062 class Prin {
4063     public final String CLASS;
4064     public final String FULL_CLASS;
4065 
4066     public Prin(String clazz, String fullClass) {
4067         this.CLASS = clazz;
4068         this.FULL_CLASS = fullClass;
4069     }
4070 }
4071 
4072 class KrbPrin extends Prin {
4073     public KrbPrin() {
4074         super("KerberosPrincipal",
4075                 "javax.security.auth.kerberos.KerberosPrincipal");
4076     }
4077 }
4078 
4079 class X500Prin extends Prin {
4080     public X500Prin() {
4081         super("X500Principal",
4082                 "javax.security.auth.x500.X500Principal");
4083     }
4084 }
4085 
4086 /**
4087  * Convenience Permission Classes
4088  */
4089 
4090 class Perm {
4091     public final String CLASS;
4092     public final String FULL_CLASS;
4093     public final String[] TARGETS;
4094     public final String[] ACTIONS;
4095 
4096     public Perm(String clazz, String fullClass,
4097                 String[] targets, String[] actions) {
4098 
4099         this.CLASS = clazz;
4100         this.FULL_CLASS = fullClass;
4101         this.TARGETS = targets;
4102         this.ACTIONS = actions;
4103     }
4104 }
4105 
4106 class AllPerm extends Perm {
4107     public AllPerm() {
4108         super("AllPermission", "java.security.AllPermission", null, null);
4109     }
4110 }
4111 
4112 class AudioPerm extends Perm {
4113     public AudioPerm() {
4114         super("AudioPermission",
4115         "javax.sound.sampled.AudioPermission",
4116         new String[]    {
4117                 "play",
4118                 "record"
4119                 },
4120         null);
4121     }
4122 }
4123 
4124 class AuthPerm extends Perm {
4125     public AuthPerm() {
4126     super("AuthPermission",
4127         "javax.security.auth.AuthPermission",
4128         new String[]    {
4129                 "doAs",
4130                 "doAsPrivileged",
4131                 "getSubject",
4132                 "getSubjectFromDomainCombiner",
4133                 "setReadOnly",
4134                 "modifyPrincipals",
4135                 "modifyPublicCredentials",
4136                 "modifyPrivateCredentials",
4137                 "refreshCredential",
4138                 "destroyCredential",
4139                 "createLoginContext.<" + PolicyTool.getMessage("name") + ">",
4140                 "getLoginConfiguration",
4141                 "setLoginConfiguration",
4142                 "createLoginConfiguration.<" +
4143                         PolicyTool.getMessage("configuration.type") + ">",
4144                 "refreshLoginConfiguration"
4145                 },
4146         null);
4147     }
4148 }
4149 
4150 class AWTPerm extends Perm {
4151     public AWTPerm() {
4152     super("AWTPermission",
4153         "java.awt.AWTPermission",
4154         new String[]    {
4155                 "accessClipboard",
4156                 "accessEventQueue",
4157                 "accessSystemTray",
4158                 "createRobot",
4159                 "fullScreenExclusive",
4160                 "listenToAllAWTEvents",
4161                 "readDisplayPixels",
4162                 "replaceKeyboardFocusManager",
4163                 "setAppletStub",
4164                 "setWindowAlwaysOnTop",
4165                 "showWindowWithoutWarningBanner",
4166                 "toolkitModality",
4167                 "watchMousePointer"
4168         },
4169         null);
4170     }
4171 }
4172 
4173 class DelegationPerm extends Perm {
4174     public DelegationPerm() {
4175     super("DelegationPermission",
4176         "javax.security.auth.kerberos.DelegationPermission",
4177         new String[]    {
4178                 // allow user input
4179                 },
4180         null);
4181     }
4182 }
4183 
4184 class FilePerm extends Perm {
4185     public FilePerm() {
4186     super("FilePermission",
4187         "java.io.FilePermission",
4188         new String[]    {
4189                 "<<ALL FILES>>"
4190                 },
4191         new String[]    {
4192                 "read",
4193                 "write",
4194                 "delete",
4195                 "execute"
4196                 });
4197     }
4198 }
4199 
4200 class URLPerm extends Perm {
4201     public URLPerm() {
4202         super("URLPermission",
4203                 "java.net.URLPermission",
4204                 new String[]    {
4205                     "<"+ PolicyTool.getMessage("url") + ">",
4206                 },
4207                 new String[]    {
4208                     "<" + PolicyTool.getMessage("method.list") + ">:<"
4209                         + PolicyTool.getMessage("request.headers.list") + ">",
4210                 });
4211     }
4212 }
4213 
4214 class InqSecContextPerm extends Perm {
4215     public InqSecContextPerm() {
4216     super("InquireSecContextPermission",
4217         "com.sun.security.jgss.InquireSecContextPermission",
4218         new String[]    {
4219                 "KRB5_GET_SESSION_KEY",
4220                 "KRB5_GET_TKT_FLAGS",
4221                 "KRB5_GET_AUTHZ_DATA",
4222                 "KRB5_GET_AUTHTIME"
4223                 },
4224         null);
4225     }
4226 }
4227 
4228 class LogPerm extends Perm {
4229     public LogPerm() {
4230     super("LoggingPermission",
4231         "java.util.logging.LoggingPermission",
4232         new String[]    {
4233                 "control"
4234                 },
4235         null);
4236     }
4237 }
4238 
4239 class MgmtPerm extends Perm {
4240     public MgmtPerm() {
4241     super("ManagementPermission",
4242         "java.lang.management.ManagementPermission",
4243         new String[]    {
4244                 "control",
4245                 "monitor"
4246                 },
4247         null);
4248     }
4249 }
4250 
4251 class MBeanPerm extends Perm {
4252     public MBeanPerm() {
4253     super("MBeanPermission",
4254         "javax.management.MBeanPermission",
4255         new String[]    {
4256                 // allow user input
4257                 },
4258         new String[]    {
4259                 "addNotificationListener",
4260                 "getAttribute",
4261                 "getClassLoader",
4262                 "getClassLoaderFor",
4263                 "getClassLoaderRepository",
4264                 "getDomains",
4265                 "getMBeanInfo",
4266                 "getObjectInstance",
4267                 "instantiate",
4268                 "invoke",
4269                 "isInstanceOf",
4270                 "queryMBeans",
4271                 "queryNames",
4272                 "registerMBean",
4273                 "removeNotificationListener",
4274                 "setAttribute",
4275                 "unregisterMBean"
4276                 });
4277     }
4278 }
4279 
4280 class MBeanSvrPerm extends Perm {
4281     public MBeanSvrPerm() {
4282     super("MBeanServerPermission",
4283         "javax.management.MBeanServerPermission",
4284         new String[]    {
4285                 "createMBeanServer",
4286                 "findMBeanServer",
4287                 "newMBeanServer",
4288                 "releaseMBeanServer"
4289                 },
4290         null);
4291     }
4292 }
4293 
4294 class MBeanTrustPerm extends Perm {
4295     public MBeanTrustPerm() {
4296     super("MBeanTrustPermission",
4297         "javax.management.MBeanTrustPermission",
4298         new String[]    {
4299                 "register"
4300                 },
4301         null);
4302     }
4303 }
4304 
4305 class NetPerm extends Perm {
4306     public NetPerm() {
4307     super("NetPermission",
4308         "java.net.NetPermission",
4309         new String[]    {
4310                 "setDefaultAuthenticator",
4311                 "requestPasswordAuthentication",
4312                 "specifyStreamHandler",
4313                 "setProxySelector",
4314                 "getProxySelector",
4315                 "setCookieHandler",
4316                 "getCookieHandler",
4317                 "setResponseCache",
4318                 "getResponseCache"
4319                 },
4320         null);
4321     }
4322 }
4323 
4324 class PrivCredPerm extends Perm {
4325     public PrivCredPerm() {
4326     super("PrivateCredentialPermission",
4327         "javax.security.auth.PrivateCredentialPermission",
4328         new String[]    {
4329                 // allow user input
4330                 },
4331         new String[]    {
4332                 "read"
4333                 });
4334     }
4335 }
4336 
4337 class PropPerm extends Perm {
4338     public PropPerm() {
4339     super("PropertyPermission",
4340         "java.util.PropertyPermission",
4341         new String[]    {
4342                 // allow user input
4343                 },
4344         new String[]    {
4345                 "read",
4346                 "write"
4347                 });
4348     }
4349 }
4350 
4351 class ReflectPerm extends Perm {
4352     public ReflectPerm() {
4353     super("ReflectPermission",
4354         "java.lang.reflect.ReflectPermission",
4355         new String[]    {
4356                 "suppressAccessChecks"
4357                 },
4358         null);
4359     }
4360 }
4361 
4362 class RuntimePerm extends Perm {
4363     public RuntimePerm() {
4364     super("RuntimePermission",
4365         "java.lang.RuntimePermission",
4366         new String[]    {
4367                 "createClassLoader",
4368                 "getClassLoader",
4369                 "setContextClassLoader",
4370                 "enableContextClassLoaderOverride",
4371                 "setSecurityManager",
4372                 "createSecurityManager",
4373                 "getenv.<" +
4374                     PolicyTool.getMessage("environment.variable.name") + ">",
4375                 "exitVM",
4376                 "shutdownHooks",
4377                 "setFactory",
4378                 "setIO",
4379                 "modifyThread",
4380                 "stopThread",
4381                 "modifyThreadGroup",
4382                 "getProtectionDomain",
4383                 "readFileDescriptor",
4384                 "writeFileDescriptor",
4385                 "loadLibrary.<" +
4386                     PolicyTool.getMessage("library.name") + ">",
4387                 "accessClassInPackage.<" +
4388                     PolicyTool.getMessage("package.name")+">",
4389                 "defineClassInPackage.<" +
4390                     PolicyTool.getMessage("package.name")+">",
4391                 "accessDeclaredMembers",
4392                 "queuePrintJob",
4393                 "getStackTrace",
4394                 "setDefaultUncaughtExceptionHandler",
4395                 "preferences",
4396                 "usePolicy",
4397                 // "inheritedChannel"
4398                 },
4399         null);
4400     }
4401 }
4402 
4403 class SecurityPerm extends Perm {
4404     public SecurityPerm() {
4405     super("SecurityPermission",
4406         "java.security.SecurityPermission",
4407         new String[]    {
4408                 "createAccessControlContext",
4409                 "getDomainCombiner",
4410                 "getPolicy",
4411                 "setPolicy",
4412                 "createPolicy.<" +
4413                     PolicyTool.getMessage("policy.type") + ">",
4414                 "getProperty.<" +
4415                     PolicyTool.getMessage("property.name") + ">",
4416                 "setProperty.<" +
4417                     PolicyTool.getMessage("property.name") + ">",
4418                 "insertProvider.<" +
4419                     PolicyTool.getMessage("provider.name") + ">",
4420                 "removeProvider.<" +
4421                     PolicyTool.getMessage("provider.name") + ">",
4422                 //"setSystemScope",
4423                 //"setIdentityPublicKey",
4424                 //"setIdentityInfo",
4425                 //"addIdentityCertificate",
4426                 //"removeIdentityCertificate",
4427                 //"printIdentity",
4428                 "clearProviderProperties.<" +
4429                     PolicyTool.getMessage("provider.name") + ">",
4430                 "putProviderProperty.<" +
4431                     PolicyTool.getMessage("provider.name") + ">",
4432                 "removeProviderProperty.<" +
4433                     PolicyTool.getMessage("provider.name") + ">",
4434                 //"getSignerPrivateKey",
4435                 //"setSignerKeyPair"
4436                 },
4437         null);
4438     }
4439 }
4440 
4441 class SerialPerm extends Perm {
4442     public SerialPerm() {
4443     super("SerializablePermission",
4444         "java.io.SerializablePermission",
4445         new String[]    {
4446                 "enableSubclassImplementation",
4447                 "enableSubstitution"
4448                 },
4449         null);
4450     }
4451 }
4452 
4453 class ServicePerm extends Perm {
4454     public ServicePerm() {
4455     super("ServicePermission",
4456         "javax.security.auth.kerberos.ServicePermission",
4457         new String[]    {
4458                 // allow user input
4459                 },
4460         new String[]    {
4461                 "initiate",
4462                 "accept"
4463                 });
4464     }
4465 }
4466 
4467 class SocketPerm extends Perm {
4468     public SocketPerm() {
4469     super("SocketPermission",
4470         "java.net.SocketPermission",
4471         new String[]    {
4472                 // allow user input
4473                 },
4474         new String[]    {
4475                 "accept",
4476                 "connect",
4477                 "listen",
4478                 "resolve"
4479                 });
4480     }
4481 }
4482 
4483 class SQLPerm extends Perm {
4484     public SQLPerm() {
4485     super("SQLPermission",
4486         "java.sql.SQLPermission",
4487         new String[]    {
4488                 "setLog",
4489                 "callAbort",
4490                 "setSyncFactory",
4491                 "setNetworkTimeout",
4492                 },
4493         null);
4494     }
4495 }
4496 
4497 class SSLPerm extends Perm {
4498     public SSLPerm() {
4499     super("SSLPermission",
4500         "javax.net.ssl.SSLPermission",
4501         new String[]    {
4502                 "setHostnameVerifier",
4503                 "getSSLSessionContext"
4504                 },
4505         null);
4506     }
4507 }
4508 
4509 class SubjDelegPerm extends Perm {
4510     public SubjDelegPerm() {
4511     super("SubjectDelegationPermission",
4512         "javax.management.remote.SubjectDelegationPermission",
4513         new String[]    {
4514                 // allow user input
4515                 },
4516         null);
4517     }
4518 }