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