KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > util > CredentialsUtil


1 package fr.jayasoft.ivy.util;
2
3 import java.awt.GridBagConstraints JavaDoc;
4 import java.awt.GridBagLayout JavaDoc;
5 import java.awt.Insets JavaDoc;
6 import java.io.File JavaDoc;
7 import java.io.FileInputStream JavaDoc;
8 import java.io.FileOutputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.util.Properties JavaDoc;
11
12 import javax.swing.ImageIcon JavaDoc;
13 import javax.swing.JCheckBox JavaDoc;
14 import javax.swing.JLabel JavaDoc;
15 import javax.swing.JOptionPane JavaDoc;
16 import javax.swing.JPanel JavaDoc;
17 import javax.swing.JPasswordField JavaDoc;
18 import javax.swing.JTextField JavaDoc;
19
20 import fr.jayasoft.ivy.Ivy;
21
22 public class CredentialsUtil {
23     
24     
25
26     private static final class CredentialPanel extends JPanel JavaDoc {
27         JTextField JavaDoc userNameField = new JTextField JavaDoc(20);
28
29         JTextField JavaDoc passwordField = new JPasswordField JavaDoc(20);
30
31         JCheckBox JavaDoc rememberDataCB = new JCheckBox JavaDoc("remember my information");
32         
33         CredentialPanel(Credentials credentials, File JavaDoc passfile) {
34             GridBagLayout JavaDoc layout = new GridBagLayout JavaDoc();
35             setLayout(layout);
36             GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
37             c.insets = new Insets JavaDoc(2,2,2,2);
38             
39             c.gridx = 1;
40             c.gridheight = 1;
41             c.gridwidth = 2;
42             String JavaDoc prompt = credentials.getRealm() != null ?
43                     "Enter username and password for \""+credentials.getRealm()+"\" at "+credentials.getHost() :
44                         "Enter username and password for "+credentials.getHost();
45             add(new JLabel JavaDoc(prompt), c);
46
47             c.gridy = 1;
48             c.gridwidth = 1;
49
50             add(new JLabel JavaDoc("username: "), c);
51             c.gridx = 2;
52             add(userNameField, c);
53             c.gridx = 1;
54             c.gridy++;
55
56             if (credentials.getUserName() != null) {
57                 userNameField.setText(credentials.getUserName());
58             }
59             
60             if (credentials.getPasswd() == null) {
61                 add(new JLabel JavaDoc("passwd: "), c);
62                 c.gridx = 2;
63                 add(passwordField, c);
64                 c.gridx = 1;
65                 c.gridy++;
66             } else {
67                 passwordField.setText(credentials.getPasswd());
68             }
69             
70             if (passfile != null) {
71                 c.gridwidth = 2;
72                 add(rememberDataCB, c);
73                 c.gridy++;
74             }
75             c.gridwidth = 2;
76             add(new JLabel JavaDoc(), c); // spacer
77

78         }
79     }
80
81
82     public static Credentials promptCredentials(Credentials c, File JavaDoc passfile) {
83         c = loadPassfile(c, passfile);
84         if (c.getUserName() != null && c.getPasswd() != null) {
85             return c;
86         }
87         CredentialPanel credentialPanel = new CredentialPanel(c, passfile);
88         if (JOptionPane.showOptionDialog(null, credentialPanel, c.getHost()+" credentials", JOptionPane.OK_CANCEL_OPTION, 0, new ImageIcon JavaDoc(Ivy.class.getResource("logo.png")), null, new Integer JavaDoc(JOptionPane.OK_OPTION)) == JOptionPane.OK_OPTION) {
89             String JavaDoc username=credentialPanel.userNameField.getText();
90             String JavaDoc passwd=credentialPanel.passwordField.getText();
91             if (credentialPanel.rememberDataCB.isSelected()) {
92                 Properties JavaDoc props = new EncrytedProperties();
93                 props.setProperty("username", username);
94                 props.setProperty("passwd", passwd);
95                 FileOutputStream JavaDoc fos = null;
96                 try {
97                     fos = new FileOutputStream JavaDoc(passfile);
98                     props.store(fos, "");
99                 } catch (Exception JavaDoc e) {
100                     Message.warn("error occured while saving password file "+passfile+": "+ e);
101                 } finally {
102                     if (fos != null) {try {fos.close();} catch (Exception JavaDoc e) {}}
103                 }
104             }
105             c = new Credentials(c.getRealm(), c.getHost(), username, passwd);
106         }
107         return c;
108     }
109
110
111     public static Credentials loadPassfile(Credentials c, File JavaDoc passfile) {
112         if (passfile != null && passfile.exists()) {
113             Properties JavaDoc props = new EncrytedProperties();
114             FileInputStream JavaDoc fis = null;
115             try {
116                 fis = new FileInputStream JavaDoc(passfile);
117                 props.load(fis);
118                 String JavaDoc username = c.getUserName();
119                 String JavaDoc passwd = c.getPasswd();
120                 if (username == null) {
121                     username = props.getProperty("username");
122                 }
123                 if (passwd == null) {
124                     passwd = props.getProperty("passwd");
125                 }
126                 return new Credentials(c.getRealm(), c.getHost(), username, passwd);
127             } catch (IOException JavaDoc e) {
128                 Message.warn("error occured while loading password file "+passfile+": "+ e);
129             } finally {
130                 if (fis != null) {
131                     try {
132                         fis.close();
133                     } catch (IOException JavaDoc e) {
134                     }
135                 }
136             }
137         }
138         return c;
139     }
140
141
142 }
143
Popular Tags