⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keystoregui.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    public static class CertItem
    {
        public String alias;
        
        public X509Certificate x509Cert = null;
        
        public boolean hasPrivateKey = false;
        
        /**
         *    Initialises a certitem with the alias name of a 
         *    certificate only (the actual cert can be added
         *    seperately)
         *    @param certAlias the alias of the certificate, the arbitrary user assigned
         *           name the certificate is labelled by in the keystore.
         */
        
        public CertItem(String certAlias)
        {
            this(certAlias, null);
        }
        
        /**
         *    Initialises a certItem with the alias name of a 
         *    certificate and the actual certificate data.
         *    @param certAlias the alias of the certificate, the arbitrary user assigned
         *           name the certificate is labelled by in the keystore.
         *    @param cert the actual X509 Certificate data.
         */
        
        public CertItem(String certAlias, X509Certificate cert)
        {
            alias = certAlias;
            x509Cert = cert;
        }
        
        
        /**
         *    Adds (or Replaces) the X509Cert data.
         *    @param x the actual X509 Certificate data.
         */
        
        public void addX509Cert(X509Certificate x)
        {
            x509Cert = x;
        }
        
        /**
         *    Returns a formatted string identifying the cert by the alias.
         *    @return the alias assigned to this cert.
         */
        
        public String toString()
        {
            if (hasPrivateKey)
                return "<html><b><font color=black>" + alias + "</font><br><font color=blue>(has private key)</font></b></html>";
            else
                return alias;
        }
        
        /**
         *    Returns a formatted string identifying the cert by the alias.
         *    @return the alias assigned to this cert.
         */
        
        public String getSelectedText()
        {
            if (hasPrivateKey)
                return "<html><b><font color=white>" + alias + "</font><br><font color=white>(has private key)</font></b></html>";
            else
                return alias;
        }
        
        /**
         *    returns the raw alias for this cert.
         *    @return the alias assigned to this cert.
         */
        
        public String getAlias()
        {
            return alias;
        }
        
        /**
         *    Returns an image representing this CertItem.
         */
        
        public ImageIcon getIcon()
        {
            if (hasPrivateKey)
                return smallKeyCert;
            else
                return smallCert;
        }
        
        /**
         *    Returns the X509 certificate data (may be null if this hasn't
         *    been set).
         *    @return the X509 certificate stored in this CertItem
         */
        
        public X509Certificate getX509Cert()
        {
            return x509Cert;
        }
        
        public void setHasPrivateKey(boolean state)
        {
            hasPrivateKey = state;
        }
        
        public boolean getHasPrivateKey()
        {
            return hasPrivateKey;
        }
    }
    
    
    /**
     *    A quicky cell renderer to allow us to display active and pending
     *    queries in different colours, and to display the text of a
     *    QueryBroker object. 
     */
    
    class CertificateListRenderer extends JLabel implements ListCellRenderer
    {
        Color highlight = new Color(0,0,128);  // Colour of 'selected' text
        
        CertificateListRenderer()
        {
            setOpaque(true);
        }
        
        public Component getListCellRendererComponent(JList list, Object value, int index,
                boolean isSelected, boolean cellHasFocus)
        {
            if (value instanceof CertItem == false)  // should never happen :-)
            {
                System.err.println("Rendering error in KeystoreGUI");
                setText(ERRORCERT);
                return this;
            }
            
            if (index == -1)
            {
                index = list.getSelectedIndex();
                if (index == -1)
                {
                    setText("<error>");
                    return this;
                }
            }
            
            if (value == null)    // shouldn't happen
            {
                setBackground(Color.white);
                setForeground(Color.gray);
                setText("<deleted>");
                return this;
            }
            
            CertItem item = (CertItem)value;
            
            setIcon(item.getIcon());
            
            if (isSelected)
            {
                setText(item.getSelectedText());
                setBackground(highlight);
                setForeground(Color.white);
            }
            else
            {
                setText(item.toString());
                setBackground(Color.white);
                setForeground(Color.black);
            }
            return this;
        }
    }
    
    public ImageIcon getImageIcon(String name)
    {
        ImageIcon newIcon = new ImageIcon(properties.getProperty("dir.images") + name);
        return newIcon;
    }
 


    
    
    private static void printUsageAndExit()
    {
        System.out.println("USAGE: java KeystoreGUI [keystore file|path] [keystore password] [keystore type] [provider]\n" +
                          "(defaults are 'security/clientcerts' and 'jks'");
        System.exit(0);                              
    }
    
    /**
     *    Main method for stand alone usage and provider testing.
     */
    
    public static void main(String[] argsv)
    {
        String keystoreType = "jks";
        
        String provider = null;
        
        String password = null;
        
        Frame rootFrame = new Frame();
        
        CBUtility.initDefaultDisplay(rootFrame);
        
        // stand alone demo...
        
        System.out.println("running KeystoreGUI 1.0 stand alone demo - Chris Betts 2002\n");
        
        
                           
                           
        String localDir = System.getProperty("user.dir") + File.separator;
        
        Properties props = new Properties();
        
        /*
         *    This sets the directory that the file browsers start in.
         *    This can be saved/read from file to allow the user to start
         *    loading/saving from the same place.
         */
        
        props.setProperty("cert.homeDir", localDir + "certs" + File.separator);
        
        /*
         *    This simply sets the directory where the GUI will try to load
         *    its button images from.
         */
        
        props.setProperty("dir.images", localDir + "images" + File.separator);
        
        /*
         *   Set the location of the java keystore to manipulate.
         */
        
        String keystoreName = localDir + "security" + File.separator + "clientcerts";
        
        if (argsv.length < 1)
            printUsageAndExit();
        
        if (argsv[0].startsWith("-h"))
            printUsageAndExit();
        
        if (argsv[0].length() < 2)
        {
            keystoreName = argsv[0];
        }
        else if (argsv[0].charAt(1) == ':' || argsv[0].charAt(0) == '/')  // absolute path
        {
            keystoreName = argsv[0];
        }
        else    // assume relative path.
        {
            keystoreName = localDir + argsv[0];
        }
        
        /*
         *    Keystore Password
         */
        
        if (argsv.length > 1)
        {
            password = argsv[1];
        }
        
        /*
         *    Read optional keystore type (e.g. "KSE")
         */
        
        if (argsv.length > 2)
        {
            keystoreType = argsv[2];
        }
        
        /*
         *    Read optional provider (e.g. com.ca.pki.security.provider.KeyStoreEngine )
         */
        
        if (argsv.length > 3)
        {
            provider = argsv[3];
            
            // register the new provider
            try
            {
                Class providerClass = Class.forName(provider);
                Provider providerObject = (Provider)providerClass.newInstance();
                Security.insertProviderAt(providerObject, 1);
                System.out.println("\nPROVIDER: " + providerObject.getName() + " v" + providerObject.getVersion() + " has been registered ");
            }
            catch (Exception e)
            {
                System.err.println("\n*** unable to load new security provider: " + ((provider==null)?"null":provider));
                System.err.println(e + "\n");
                printUsageAndExit();
            }
        }

        Provider[] current = Security.getProviders();
        for (int i=0; i<current.length; i++)
            System.out.println("registered security providers: " + i + " = " + current[i].getName() + " " + current[i].getInfo());
        
        // Extend KeystoreGUI to add 'exit on close' behaviour
        
        class StandaloneKeystore extends KeystoreGUI
        {
            public StandaloneKeystore( Frame owner, Properties props, String keyStoreLocation, char[] pwd, String keystoreType, String title, boolean handlePrivateKeys, String helpTopic)
            {
                super(owner, props, keyStoreLocation, pwd, keystoreType, title, handlePrivateKeys, helpTopic);
            }
            
            public void doOK()
            {
                super.doOK();
                System.exit(0);
            }
            
            public void doCancel()
            {
                super.doCancel();
                System.exit(0);
            }
        }
        
        char[] pwd = null; //((password==null)?null:password.toCharArray());
        
        StandaloneKeystore gui = new StandaloneKeystore(rootFrame, props, keystoreName, pwd, keystoreType, "CB Keystore GUI Demo", true, null);
        gui.setSize(450,440);
        
        CBUtility.center(gui, null);
        
        gui.setVisible(true);
    }
   
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -