KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > directory > jxplorer > KeystoreOptions


1 package com.ca.directory.jxplorer;
2
3
4 import com.ca.commons.cbutil.*;
5 import java.awt.Frame JavaDoc;
6 import java.util.*;
7 import javax.swing.*;
8 import java.awt.Dimension JavaDoc;
9 import javax.swing.border.TitledBorder JavaDoc;
10
11 import java.security.Provider JavaDoc;
12 import java.security.Security JavaDoc;
13
14 /**
15  * The Keystore Options dialog allows the user to set which keystore files
16  * are being used, and what type they are. This is then used to set the
17  * value of the JX properties (kept in the dxconfig.txt file)
18  *<PRE>
19  * keystoreType.clientcerts - client cert keystore type (e.g. "JKS")
20  * keystoreType.cacerts - trusted server keystore type (e.g. "JKS")
21  * option.ssl.clientcerts - client cert location, e.g. D\:\\JavaProjects\\MyBrowser\\security\\clientcerts.jks
22  * option.ssl.cacerts - trusted server keystore location, e.g. D\:\\JavaProjects\\MyBrowser\\security\\cacerts.jks
23  *</PRE>
24  */

25
26 public class KeystoreOptions extends CBDialog
27 {
28      
29     /**
30      * Keystore file locations (e.g. D\:\\JavaProjects\\MyBrowser\\security\\clientcerts.jks)
31      */

32       
33     private String JavaDoc caStore, clientStore;
34     
35     /**
36      * The file choosers used to select the keystore paths.
37      */

38      
39     CBFileChooserButton caChooser, clientChooser;
40
41     /**
42      * The text fields used to enter the keystore paths (through the CBFileChooserButton).
43      */

44     
45     JTextField caKeystoreLocationText, clientKeystoreLocationText;
46
47
48     /**
49      * The text fields used to enter the keystore types. (e.g. "PKCS12" or "JKS")
50      */

51     
52     CBJComboBox caTypeCombo, clientTypeCombo;
53     
54     private JTextField clientKeystore, trustedServerKeystore;
55     
56     
57     private boolean debug = false;
58     
59     /**
60      * A pointer to the properties file to modify with the user's changes
61      */

62     
63     private Properties properties;
64     
65     /**
66      * A vector of (String) key types ('PKCS12' or 'JKS' etc.)
67      */

68     
69     private Vector keyTypes;
70     
71     /**
72      * Initialise the dialog with a parent graphics frame and a list of
73      * properties to modify.
74      * @param owner the parent GUI frame to center on
75      * @param properties the property list to modify with the values described above.
76      */

77      
78     KeystoreOptions(Frame JavaDoc owner, Properties properties)
79     {
80         super(owner, CBIntText.get("Keystore Options"), HelpIDs.SSL_CHANGE_KEYSTORE);
81
82         this.properties = properties;
83
84         keyTypes = new Vector(10);
85
86         /*
87          * Read the existing values from the properties list
88          */

89             
90         String JavaDoc caType = (String JavaDoc) properties.get(JXplorer.CA_TYPE_PROPERTY);
91         String JavaDoc clientType = (String JavaDoc) properties.get(JXplorer.CLIENT_TYPE_PROPERTY);
92         caStore = (String JavaDoc) properties.get(JXplorer.CA_PATH_PROPERTY);
93         clientStore = (String JavaDoc) properties.get(JXplorer.CLIENT_PATH_PROPERTY);
94
95         keyTypes.add(caType);
96         if (keyTypes.contains(clientType) == false)
97             keyTypes.add(clientType);
98         
99         keyTypes = setupKeyList(keyTypes);
100         
101         caTypeCombo = new CBJComboBox(keyTypes);
102         caTypeCombo.setSelectedItem(caType);
103         
104         clientTypeCombo = new CBJComboBox(keyTypes);
105         clientTypeCombo.setSelectedItem(clientType);
106         
107         /*
108          * Setup the gui using the current values.
109          */

110
111         makeWide();
112
113         // --- first line ---
114

115         CBPanel caPanel = new CBPanel();
116         caPanel.setBorder(new TitledBorder JavaDoc(CBIntText.get("Setup the Trusted CA / Server Keystore ")));
117         
118         // --- second line ---
119

120         caPanel.add(new JLabel(CBIntText.get("CA/Server Keystore:")));
121         
122         caKeystoreLocationText = new JTextField(caStore);
123         caPanel.addGreedyWide(caKeystoreLocationText, 2);
124         caPanel.makeLight();
125         
126         caChooser = new CBFileChooserButton(caKeystoreLocationText, this, CBIntText.get("Load"), CBIntText.get("Open the file chooser."));
127         caChooser.setPreferredSize(new Dimension JavaDoc(65,21));
128         caChooser.setStartingDirectory(caStore);
129         caPanel.addln(caChooser);
130         
131         // --- third line ---
132

133         caPanel.add(new JLabel(CBIntText.get("Set CA/Server Keystore Type:")));
134         caTypeCombo.setPreferredSize(new Dimension JavaDoc(100,21));
135         caPanel.add(caTypeCombo);
136         caPanel.add(new JLabel(" "));
137         caPanel.addln(new JLabel(" "));
138             
139         display.addln(new JLabel(" "));
140         display.addln(caPanel);
141         
142         // --- fourth ---
143

144         display.addln(new JLabel(" "));
145         CBPanel clientPanel = new CBPanel();
146         clientPanel.setBorder(new TitledBorder JavaDoc(CBIntText.get("Setup the Client's Private Keystore ")));
147         
148         // --- fifth line ---
149

150         clientPanel.add(new JLabel(CBIntText.get("Client Keystore:")));
151         
152         clientKeystoreLocationText = new JTextField(clientStore);
153         clientPanel.addGreedyWide(clientKeystoreLocationText, 2);
154         clientPanel.makeLight();
155         
156         clientChooser = new CBFileChooserButton(clientKeystoreLocationText, this, CBIntText.get("Load"), CBIntText.get("Open the file chooser."));
157         clientChooser.setPreferredSize(new Dimension JavaDoc(65,21));
158         clientChooser.setStartingDirectory(caStore);
159         clientPanel.addln(clientChooser);
160         
161         // --- sixth line ---
162

163         clientPanel.add(new JLabel(CBIntText.get("Set Client Keystore Type: ")));
164         clientTypeCombo.setPreferredSize(new Dimension JavaDoc(100,21));
165         clientPanel.add(clientTypeCombo);
166         clientPanel.add(new JLabel(" "));
167         clientPanel.addln(new JLabel(" "));
168         display.addln(clientPanel);
169     }
170
171     public Vector setupKeyList(Vector keyList)
172     {
173         if (debug)
174         {
175             Provider JavaDoc[] providers = Security.getProviders();
176             for (int i=0; i<providers.length; i++)
177             {
178                 Set keys = providers[i].keySet();
179                 Iterator iterator = keys.iterator();
180                 while (iterator.hasNext())
181                 {
182                     String JavaDoc key = (String JavaDoc) iterator.next();
183                 }
184             }
185         }
186         
187         Provider JavaDoc[] providers = Security.getProviders();
188         for (int i=0; i<providers.length; i++)
189         {
190             Set keys = providers[i].keySet();
191             Iterator iterator = keys.iterator();
192             while (iterator.hasNext())
193             {
194                 String JavaDoc key = (String JavaDoc) iterator.next();
195         
196                 if (key.startsWith("KeyStore"))
197                 {
198                     if (key.endsWith("ImplementedIn") == false)
199                     {
200                         String JavaDoc keyStoreName = key.substring(9);
201                            
202                         if (keyList.contains(keyStoreName) == false)
203                             keyList.add(keyStoreName);
204                     }
205                 }
206             }
207         }
208
209         return keyList;
210     }
211     
212     /**
213      * Called by the parent class when the user presses the 'OK' button.
214      * loads the properties object up with the new user entered values,
215      * as read from the text components.
216      */

217      
218     public void doOK()
219     {
220         properties.setProperty(JXplorer.CA_TYPE_PROPERTY, (String JavaDoc)caTypeCombo.getSelectedItem());
221         properties.setProperty(JXplorer.CLIENT_TYPE_PROPERTY, (String JavaDoc)clientTypeCombo.getSelectedItem());
222         properties.setProperty(JXplorer.CA_PATH_PROPERTY, caKeystoreLocationText.getText());
223         properties.setProperty(JXplorer.CLIENT_PATH_PROPERTY, clientKeystoreLocationText.getText());
224     
225         super.doOK();
226     }
227     
228 }
Popular Tags