KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > wsitconf > ui > service > subpanels > KeystorePanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.websvc.wsitconf.ui.service.subpanels;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import org.netbeans.modules.websvc.wsitconf.ui.StoreFileFilter;
25 import org.netbeans.modules.websvc.wsitconf.util.Util;
26 import org.netbeans.modules.websvc.wsitconf.wsdlmodelext.ProprietarySecurityPolicyModelHelper;
27 import org.netbeans.modules.xml.multiview.ui.SectionVisualTheme;
28 import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
29 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
30 import org.openide.DialogDisplayer;
31 import org.openide.NotifyDescriptor;
32 import org.openide.filesystems.FileUtil;
33 import org.openide.util.NbBundle;
34 import javax.swing.*;
35 import java.io.File JavaDoc;
36 import org.netbeans.api.project.Project;
37 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
38 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
39 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
40
41 /**
42  *
43  * @author Martin Grebac
44  */

45 public class KeystorePanel extends JPanel {
46
47     private static final String JavaDoc PKCS12 = "PKCS12"; //NOI18N
48
private static final String JavaDoc JKS = "JKS"; //NOI18N
49

50     private static final String JavaDoc DEFAULT_PASSWORD="changeit"; //NOI18N
51

52     private WSDLModel model;
53     private WSDLComponent comp;
54
55     private boolean jsr109 = false;
56     private Project project = null;
57     
58     private String JavaDoc keystoreType = JKS;
59     
60     private boolean inSync = false;
61     
62     public KeystorePanel(WSDLComponent comp, Project p, boolean jsr109) {
63         super();
64         this.model = comp.getModel();
65         this.comp = comp;
66         this.jsr109 = jsr109;
67         this.project = p;
68         
69         initComponents();
70
71         keyAliasCombo.setBackground(SectionVisualTheme.getDocumentBackgroundColor());
72         keyAliasLabel.setBackground(SectionVisualTheme.getDocumentBackgroundColor());
73         keyPasswordLabel.setBackground(SectionVisualTheme.getDocumentBackgroundColor());
74         keyPasswordField.setBackground(SectionVisualTheme.getDocumentBackgroundColor());
75         keystoreLocationLabel.setBackground(SectionVisualTheme.getDocumentBackgroundColor());
76         keystoreLocationTextField.setBackground(SectionVisualTheme.getDocumentBackgroundColor());
77         keystorePasswordLabel.setBackground(SectionVisualTheme.getDocumentBackgroundColor());
78         keystorePasswordField.setBackground(SectionVisualTheme.getDocumentBackgroundColor());
79
80         sync();
81     }
82
83     private String JavaDoc getKeystoreAlias() {
84         return (String JavaDoc) this.keyAliasCombo.getSelectedItem();
85     }
86
87     private void setKeystoreAlias(String JavaDoc alias) {
88         this.keyAliasCombo.setSelectedItem(alias);
89     }
90
91     private String JavaDoc getKeyPassword() {
92         return String.valueOf(this.keyPasswordField.getPassword());
93     }
94
95     private void setKeyPassword(String JavaDoc password) {
96         this.keyPasswordField.setText(password);
97     }
98     
99     private char[] getCharKeystorePassword() {
100         return keystorePasswordField.getPassword();
101     }
102     
103     private String JavaDoc getKeystorePassword() {
104         return String.valueOf(this.keystorePasswordField.getPassword());
105     }
106
107     private void setKeystorePassword(String JavaDoc password) {
108         this.keystorePasswordField.setText(password);
109     }
110
111     private void setKeystorePath(String JavaDoc path) {
112         this.keystoreLocationTextField.setText(path);
113     }
114     
115     private String JavaDoc getKeystorePath() {
116         String JavaDoc path = this.keystoreLocationTextField.getText();
117         if ("".equals(path) || (path == null)) { //NOI18N
118
return null;
119         }
120         return path;
121     }
122
123     private void setKeystoreType(String JavaDoc type) {
124         this.keystoreType = type;
125     }
126     
127     private String JavaDoc getKeystoreType() {
128         String JavaDoc type = this.keystoreType;
129         if ("".equals(type) || (type == null)) { //NOI18N
130
return JKS;
131         }
132         return type;
133     }
134     
135     public void sync() {
136         inSync = true;
137
138         String JavaDoc keystoreLocation = ProprietarySecurityPolicyModelHelper.getStoreLocation(comp, false);
139         if (keystoreLocation != null) {
140             setKeystorePath(keystoreLocation);
141         } else if (jsr109) {
142             setKeystorePath(getServerStoreLocation());
143         }
144
145         String JavaDoc keystoreType = ProprietarySecurityPolicyModelHelper.getStoreType(comp, false);
146         if (keystoreType != null) {
147             setKeystoreType(keystoreType);
148         }
149         
150         String JavaDoc keyStorePassword = ProprietarySecurityPolicyModelHelper.getStorePassword(comp, false);
151         if (keyStorePassword != null) {
152             setKeystorePassword(keyStorePassword);
153             reloadAliases();
154         } else if (jsr109) {
155             setKeystorePassword(DEFAULT_PASSWORD);
156         }
157
158         String JavaDoc keyStoreAlias = ProprietarySecurityPolicyModelHelper.getStoreAlias(comp, false);
159         setKeystoreAlias(keyStoreAlias);
160
161         String JavaDoc keyPassword = ProprietarySecurityPolicyModelHelper.getKeyPassword(comp);
162         if (keyPassword != null) {
163             setKeyPassword(keyPassword);
164         }
165         
166         enableDisable();
167
168         inSync = false;
169     }
170
171     private void enableDisable() {
172         //these depend on jsr109 state
173
keystoreLocationButton.setEnabled(!jsr109);
174         keystoreLocationLabel.setEnabled(!jsr109);
175         keystoreLocationTextField.setEnabled(!jsr109);
176     }
177     
178     private String JavaDoc getServerStoreLocation() {
179         String JavaDoc keystoreLocation = null;
180         J2eeModuleProvider mp = (J2eeModuleProvider)project.getLookup().lookup(J2eeModuleProvider.class);
181         if (mp != null) {
182             String JavaDoc sID = mp.getServerInstanceID();
183             J2eePlatform j2eePlatform = Deployment.getDefault().getJ2eePlatform(sID);
184             File JavaDoc[] keyLocs = null;
185             keyLocs = j2eePlatform.getToolClasspathEntries(J2eePlatform.TOOL_KEYSTORE);
186             if ((keyLocs != null) && (keyLocs.length > 0)) {
187                 keystoreLocation = keyLocs[0].getAbsolutePath();
188             }
189         }
190         return keystoreLocation;
191     }
192     
193     public void storeState() {
194         String JavaDoc keystoreAlias = getKeystoreAlias();
195         if ((keystoreAlias == null) || (keystoreAlias.length() == 0)) {
196             ProprietarySecurityPolicyModelHelper.setKeyStoreAlias(comp, null, false);
197         } else {
198             ProprietarySecurityPolicyModelHelper.setKeyStoreAlias(comp, keystoreAlias, false);
199         }
200         String JavaDoc keyPasswd = getKeyPassword();
201         if ((keyPasswd == null) || (keyPasswd.length() == 0)) {
202             ProprietarySecurityPolicyModelHelper.setKeyPassword(comp, null, false);
203         } else {
204             ProprietarySecurityPolicyModelHelper.setKeyPassword(comp, keyPasswd, false);
205         }
206
207         String JavaDoc keyStorePasswd = getKeystorePassword();
208         if ((keyStorePasswd == null) || (keyStorePasswd.length() == 0)) {
209             ProprietarySecurityPolicyModelHelper.setStorePassword(comp, null, false, false);
210         } else {
211             ProprietarySecurityPolicyModelHelper.setStorePassword(comp, keyStorePasswd, false, false);
212         }
213         
214         ProprietarySecurityPolicyModelHelper.setStoreType(comp, keystoreType, false, false);
215         
216         ProprietarySecurityPolicyModelHelper.setStoreLocation(comp, getKeystorePath(), false, false);
217     }
218     
219     /** This method is called from within the constructor to
220      * initialize the form.
221      * WARNING: Do NOT modify this code. The content of this method is
222      * always regenerated by the Form Editor.
223      */

224     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
225
private void initComponents() {
226
227         keystoreLocationLabel = new javax.swing.JLabel JavaDoc();
228         keystorePasswordLabel = new javax.swing.JLabel JavaDoc();
229         keystoreLocationTextField = new javax.swing.JTextField JavaDoc();
230         keystoreLocationButton = new javax.swing.JButton JavaDoc();
231         keyAliasLabel = new javax.swing.JLabel JavaDoc();
232         keyPasswordLabel = new javax.swing.JLabel JavaDoc();
233         keyAliasCombo = new javax.swing.JComboBox JavaDoc();
234         keystorePasswordField = new javax.swing.JPasswordField JavaDoc();
235         keyPasswordField = new javax.swing.JPasswordField JavaDoc();
236         loadkeysButton = new javax.swing.JButton JavaDoc();
237
238         keystoreLocationLabel.setText(org.openide.util.NbBundle.getMessage(KeystorePanel.class, "LBL_KeyStorePanel_LocationLabel")); // NOI18N
239

240         keystorePasswordLabel.setText(org.openide.util.NbBundle.getMessage(KeystorePanel.class, "LBL_StorePanel_StorePassword")); // NOI18N
241

242         keystoreLocationButton.setText("Browse...");
243         keystoreLocationButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
244             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
245                 keystoreLocationButtonActionPerformed(evt);
246             }
247         });
248
249         keyAliasLabel.setText(org.openide.util.NbBundle.getMessage(KeystorePanel.class, "LBL_KeyStorePanel_KeyAliasLabel")); // NOI18N
250

251         keyPasswordLabel.setText(org.openide.util.NbBundle.getMessage(KeystorePanel.class, "LBL_Keystore_KeyPasswordLabel")); // NOI18N
252

253         keyAliasCombo.setEditable(true);
254
255         loadkeysButton.setText(org.openide.util.NbBundle.getMessage(KeystorePanel.class, "LBL_LoadKeys")); // NOI18N
256
loadkeysButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
257             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
258                 loadkeysButtonActionPerformed(evt);
259             }
260         });
261
262         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
263         this.setLayout(layout);
264         layout.setHorizontalGroup(
265             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
266             .add(layout.createSequentialGroup()
267                 .addContainerGap()
268                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
269                     .add(keystoreLocationLabel)
270                     .add(keystorePasswordLabel)
271                     .add(keyAliasLabel)
272                     .add(keyPasswordLabel))
273                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
274                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
275                     .add(layout.createSequentialGroup()
276                         .add(keystoreLocationTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 239, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
277                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
278                         .add(keystoreLocationButton))
279                     .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
280                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
281                             .add(org.jdesktop.layout.GroupLayout.LEADING, keyAliasCombo, 0, 159, Short.MAX_VALUE)
282                             .add(keystorePasswordField)
283                             .add(org.jdesktop.layout.GroupLayout.LEADING, keyPasswordField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE))
284                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
285                         .add(loadkeysButton)
286                         .add(68, 68, 68)))
287                 .addContainerGap())
288         );
289         layout.setVerticalGroup(
290             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
291             .add(layout.createSequentialGroup()
292                 .addContainerGap()
293                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
294                     .add(keystoreLocationLabel)
295                     .add(keystoreLocationTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
296                     .add(keystoreLocationButton))
297                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
298                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
299                     .add(keystorePasswordLabel)
300                     .add(keystorePasswordField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
301                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
302                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
303                     .add(keyAliasLabel)
304                     .add(keyAliasCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
305                     .add(loadkeysButton))
306                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
307                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
308                     .add(keyPasswordLabel)
309                     .add(keyPasswordField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
310                 .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
311         );
312
313         layout.linkSize(new java.awt.Component JavaDoc[] {keyAliasCombo, keyPasswordField, keystoreLocationTextField, keystorePasswordField}, org.jdesktop.layout.GroupLayout.VERTICAL);
314
315     }// </editor-fold>//GEN-END:initComponents
316

317     private void loadkeysButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_loadkeysButtonActionPerformed
318
boolean success = reloadAliases();
319         if (!success) {
320             DialogDisplayer.getDefault().notify(
321                 new NotifyDescriptor.Message(NbBundle.getMessage(KeystorePanel.class, "MSG_WrongPassword" //NOI18N
322
)));
323         }
324     }//GEN-LAST:event_loadkeysButtonActionPerformed
325

326     private void keystoreLocationButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_keystoreLocationButtonActionPerformed
327
JFileChooser chooser = new JFileChooser();
328         FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
329         chooser.setDialogTitle(NbBundle.getMessage(KeystorePanel.class, "LBL_KeystoreBrowse_Title")); //NOI18N
330
chooser.setFileSelectionMode (JFileChooser.FILES_ONLY);
331         chooser.setMultiSelectionEnabled(false);
332         chooser.setFileFilter(new StoreFileFilter());
333         File JavaDoc f = new File JavaDoc(keystoreLocationTextField.getText());
334         File JavaDoc dir = null;
335         if ((f != null) && (f.exists())) {
336             if (f.isDirectory()) {
337                 chooser.setCurrentDirectory(f);
338             } else {
339                 chooser.setCurrentDirectory(f.getParentFile());
340             }
341         }
342         if (chooser.showOpenDialog(this)== JFileChooser.APPROVE_OPTION) {
343             File JavaDoc file = chooser.getSelectedFile();
344             if (file != null) {
345                 setKeystorePath(file.getPath());
346                 String JavaDoc extension = FileUtil.getExtension(file.getName());
347                 keystoreType = StoreFileFilter.JKS_EXT.equals(extension) ? JKS : PKCS12;
348             }
349         }
350     }//GEN-LAST:event_keystoreLocationButtonActionPerformed
351

352     private boolean reloadAliases() {
353         Enumeration JavaDoc<String JavaDoc> aliases;
354         try {
355             aliases = Util.getAliases(getKeystorePath(), getCharKeystorePassword(), keystoreType);
356         } catch (IOException JavaDoc ex) {
357             return false;
358         }
359         keyAliasCombo.removeAllItems();
360         if (aliases != null) {
361             keyAliasCombo.addItem(""); //NOI18N
362
while (aliases.hasMoreElements()){
363                 String JavaDoc alias = aliases.nextElement();
364                 keyAliasCombo.addItem(alias);
365             }
366             if (keyAliasCombo.getItemCount() > 1) {
367                 keyAliasCombo.setSelectedIndex(1);
368             }
369         }
370         return true;
371     }
372         
373     // Variables declaration - do not modify//GEN-BEGIN:variables
374
private javax.swing.JComboBox JavaDoc keyAliasCombo;
375     private javax.swing.JLabel JavaDoc keyAliasLabel;
376     private javax.swing.JPasswordField JavaDoc keyPasswordField;
377     private javax.swing.JLabel JavaDoc keyPasswordLabel;
378     private javax.swing.JButton JavaDoc keystoreLocationButton;
379     private javax.swing.JLabel JavaDoc keystoreLocationLabel;
380     private javax.swing.JTextField JavaDoc keystoreLocationTextField;
381     private javax.swing.JPasswordField JavaDoc keystorePasswordField;
382     private javax.swing.JLabel JavaDoc keystorePasswordLabel;
383     private javax.swing.JButton JavaDoc loadkeysButton;
384     // End of variables declaration//GEN-END:variables
385

386 }
387
Popular Tags