KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > apps > svgbrowser > JAuthenticator


1 /*
2
3    Copyright 2002-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.apps.svgbrowser;
19
20 import java.awt.BorderLayout JavaDoc;
21 import java.awt.Container JavaDoc;
22 import java.awt.EventQueue JavaDoc;
23 import java.awt.Frame JavaDoc;
24 import java.awt.GridBagConstraints JavaDoc;
25 import java.awt.GridBagLayout JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.awt.event.ActionListener JavaDoc;
28 import java.awt.event.WindowAdapter JavaDoc;
29 import java.awt.event.WindowEvent JavaDoc;
30 import java.net.Authenticator JavaDoc;
31 import java.net.PasswordAuthentication JavaDoc;
32
33 import javax.swing.JButton JavaDoc;
34 import javax.swing.JComponent JavaDoc;
35 import javax.swing.JDialog JavaDoc;
36 import javax.swing.JLabel JavaDoc;
37 import javax.swing.JPanel JavaDoc;
38 import javax.swing.JPasswordField JavaDoc;
39 import javax.swing.JTextField JavaDoc;
40 import javax.swing.SwingConstants JavaDoc;
41
42 /**
43  * This class is resposible for providing authentication information
44  * when needed by network protocols. It does this by poping up a small
45  * window that asks for User ID and password for the system.
46  */

47 public class JAuthenticator extends Authenticator JavaDoc {
48
49     /**
50      * Internationalization message string
51      */

52     public static final String JavaDoc TITLE
53         = "JAuthenticator.title";
54     public static final String JavaDoc LABEL_SITE
55         = "JAuthenticator.label.site";
56     public static final String JavaDoc LABEL_REQ
57         = "JAuthenticator.label.req";
58     public static final String JavaDoc LABEL_USERID
59         = "JAuthenticator.label.userID";
60     public static final String JavaDoc LABEL_PASSWORD
61         = "JAuthenticator.label.password";
62
63     public static final String JavaDoc LABEL_CANCEL
64         = "JAuthenticator.label.cancel";
65     public static final String JavaDoc LABEL_OK
66         = "JAuthenticator.label.ok";
67
68     protected JDialog JavaDoc window;
69     protected JButton JavaDoc cancelButton;
70     protected JButton JavaDoc okButton;
71
72     protected JLabel JavaDoc label1;
73     protected JLabel JavaDoc label2;
74     protected JTextField JavaDoc JUserID;
75     protected JPasswordField JavaDoc JPassword;
76
77     Object JavaDoc lock = new Object JavaDoc();
78
79     private boolean result;
80     private boolean wasNotified;
81     private String JavaDoc userID;
82     private char [] password;
83
84     public JAuthenticator() {
85         initWindow();
86     }
87
88     protected void initWindow() {
89         String JavaDoc title = Resources.getString(TITLE);
90         window = new JDialog JavaDoc((Frame JavaDoc)null, title, true);
91
92         Container JavaDoc mainPanel = window.getContentPane();
93         mainPanel.setLayout(new BorderLayout JavaDoc());
94         mainPanel.add(buildAuthPanel(), BorderLayout.CENTER);
95         mainPanel.add(buildButtonPanel(), BorderLayout.SOUTH);
96         window.pack();
97
98         window.addWindowListener( new WindowAdapter JavaDoc() {
99                 public void windowClosing(WindowEvent JavaDoc e) {
100                     cancelListener.actionPerformed
101                         (new ActionEvent JavaDoc(e.getWindow(),
102                                          ActionEvent.ACTION_PERFORMED,
103                                          "Close"));
104                 }
105             });
106     }
107
108     protected JComponent JavaDoc buildAuthPanel() {
109         GridBagLayout JavaDoc gridBag = new GridBagLayout JavaDoc();
110         GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
111         JPanel JavaDoc proxyPanel = new JPanel JavaDoc(gridBag);
112         c.fill = GridBagConstraints.BOTH;
113         c.weightx = 1.0;
114
115         c.gridwidth = 1;
116         JLabel JavaDoc labelS = new JLabel JavaDoc(Resources.getString(LABEL_SITE));
117         labelS.setHorizontalAlignment(SwingConstants.LEFT);
118         gridBag.setConstraints(labelS, c);
119         proxyPanel.add(labelS);
120         
121         c.gridwidth = GridBagConstraints.REMAINDER;
122         label1 = new JLabel JavaDoc("");
123         label1.setHorizontalAlignment(SwingConstants.LEFT);
124         gridBag.setConstraints(label1, c);
125         proxyPanel.add(label1);
126         
127         c.gridwidth = 1;
128         JLabel JavaDoc labelR = new JLabel JavaDoc(Resources.getString(LABEL_REQ));
129         labelR.setHorizontalAlignment(SwingConstants.LEFT);
130         gridBag.setConstraints(labelR, c);
131         proxyPanel.add(labelR);
132         
133         c.gridwidth = GridBagConstraints.REMAINDER;
134         label2 = new JLabel JavaDoc("");
135         label2.setHorizontalAlignment(SwingConstants.LEFT);
136         gridBag.setConstraints(label2, c);
137         proxyPanel.add(label2);
138         
139         c.gridwidth = 1;
140         JLabel JavaDoc labelUserID = new JLabel JavaDoc(Resources.getString(LABEL_USERID));
141         labelUserID.setHorizontalAlignment(SwingConstants.LEFT);
142         gridBag.setConstraints(labelUserID, c);
143         proxyPanel.add(labelUserID);
144
145         c.gridwidth = GridBagConstraints.REMAINDER;
146         JUserID = new JTextField JavaDoc(20);
147         gridBag.setConstraints(JUserID, c);
148         proxyPanel.add(JUserID);
149
150         c.gridwidth = 1;
151         JLabel JavaDoc labelPassword = new JLabel JavaDoc(Resources.getString(LABEL_PASSWORD));
152         labelPassword.setHorizontalAlignment(SwingConstants.LEFT);
153         gridBag.setConstraints(labelPassword, c);
154         proxyPanel.add(labelPassword);
155
156         c.gridwidth = GridBagConstraints.REMAINDER;
157         JPassword = new JPasswordField JavaDoc(20);
158         JPassword.setEchoChar('*');
159         JPassword.addActionListener(okListener);
160         gridBag.setConstraints(JPassword, c);
161         proxyPanel.add(JPassword);
162
163         return proxyPanel;
164     }
165
166     
167
168     protected JComponent JavaDoc buildButtonPanel() {
169         JPanel JavaDoc buttonPanel = new JPanel JavaDoc();
170         cancelButton = new JButton JavaDoc(Resources.getString(LABEL_CANCEL));
171         cancelButton.addActionListener(cancelListener);
172         buttonPanel.add(cancelButton);
173
174         okButton = new JButton JavaDoc(Resources.getString(LABEL_OK));
175         okButton.addActionListener( okListener);
176         buttonPanel.add(okButton);
177
178         return buttonPanel;
179     }
180
181     /**
182      * This is called by the protocol stack when authentication is
183      * required. We then show the dialog in the Swing event thread,
184      * and block waiting for the user to select either cancel or ok,
185      * at which point we get notified.
186      */

187     public PasswordAuthentication JavaDoc getPasswordAuthentication() {
188         synchronized (lock) {
189             EventQueue.invokeLater(new Runnable JavaDoc() {
190                     public void run() {
191                         label1.setText(getRequestingSite().getHostName());
192                         label2.setText(getRequestingPrompt());
193                         window.setVisible(true);
194                     }
195                 });
196             wasNotified = false;
197             while (!wasNotified) {
198                 try {
199                     lock.wait();
200                 } catch(InterruptedException JavaDoc ie) { }
201             }
202             if (!result)
203                 return null;
204
205             return new PasswordAuthentication JavaDoc(userID, password);
206         }
207     }
208
209     ActionListener JavaDoc okListener = new ActionListener JavaDoc() {
210             public void actionPerformed(ActionEvent JavaDoc e) {
211                 synchronized (lock) {
212                     window.setVisible(false);
213
214                     userID = JUserID.getText();
215                     password = JPassword.getPassword();
216                     JPassword.setText("");
217                     result = true;
218                     wasNotified = true;
219                     lock.notifyAll();
220                 }
221             }
222         };
223
224     ActionListener JavaDoc cancelListener = new ActionListener JavaDoc() {
225             public void actionPerformed(ActionEvent JavaDoc e) {
226                 synchronized (lock) {
227                     window.setVisible(false);
228
229                     userID = null;
230                     JUserID.setText("");
231                     password = null;
232                     JPassword.setText("");
233                     result = false;
234                     wasNotified = true;
235                     lock.notifyAll();
236                 }
237             }
238         };
239 }
240
Popular Tags