📄 configurator.java
字号:
/*
* Copyright (c) 2001-2002 Sun Microsystems, Inc. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Sun Microsystems, Inc. for Project JXTA."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact Project JXTA at http://www.jxta.org.
*
* 5. Products derived from this software may not be called "JXTA",
* nor may "JXTA" appear in their name, without prior written
* permission of Sun.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of Project JXTA. For more
* information on Project JXTA, please see
* <http://www.jxta.org/>.
*
* This license is based on the BSD license adopted by the Apache Foundation.
*
* $Id: Configurator.java,v 1.73 2002/04/09 00:07:37 hamada Exp $
*/
package net.jxta.impl.peergroup;
import java.net.*;
import java.util.*;
import java.io.*;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import net.jxta.protocol.PeerAdvertisement;
import net.jxta.protocol.TransportAdvertisement;
import net.jxta.impl.protocol.HTTPAdv;
import net.jxta.impl.protocol.TCPAdv;
import net.jxta.impl.endpoint.tls.TlsConfig;
import net.jxta.impl.endpoint.tls.JTlsUtil;
import net.jxta.peer.*;
import net.jxta.peergroup.*;
import net.jxta.resolver.*;
import net.jxta.id.*;
import net.jxta.exception.*;
import net.jxta.credential.*;
import net.jxta.document.*;
import net.jxta.impl.config.Config;
import org.apache.log4j.Category;
import org.apache.log4j.Priority;
/**
*Description of the Class
*/
public class Configurator {
static String configName = Config.JXTA_HOME + "PlatformConfig";
private final static Category LOG =
Category.getInstance(Configurator.class.getName());
PeerAdvertisement advertisement = null;
/**
* Constructor for the Configurator object
*
* @since 1.0
*/
public Configurator() {
// start with error as the default
//LOG.setPriority(Priority.toPriority("debug"));
load();
// Make sure it does not need a reconf
if (fixAdvertisement() == false) {
adjustLog4JPriority();
// if there's a password set, or a pass
// pop a dialog to get the password
if (checkPassword()) {
// Set Tls password, and check for
// changed password.
configureTls();
routerRdvMatch();
} else {
platformCanceled = true;
}
return;
}
try {
ConfigDialog ignore = new ConfigDialog(advertisement);
ignore.untilDone();
} catch (Throwable t) {
if (t instanceof JxtaError) {
throw (JxtaError) t;
}
if (LOG.isEnabledFor(Priority.ERROR))
LOG.error( "Could not initialize graphical config dialog", t );
BufferedReader in
= new BufferedReader(new InputStreamReader(System.in));
try {
while (in.ready()) {
in.readLine();
}
} catch (Exception e) {
}
System.out.println("The window-based configurator does not seem"
+ " to be usable.");
System.out.print("Do you want to stop and edit the current "
+ "configuration ? [no]: ");
String answer = "no";
try {
answer = in.readLine();
} catch (Exception e) {
}
// this will cover all the cases of the answer yes, while
// allowing non-gui/batch type scripts to load the platform
// see platform issue #45
if ("yes".equalsIgnoreCase(answer)) {
save();
System.out.println("Exiting; edit the file \"" + configName
+ "\", remove the file \"reconf\", and then"
+ ", launch JXTA again.");
throw new JxtaError("Manual Configuration Requested");
} else {
// Save that config.
save();
// save is done before RouterRdvMatch so that no original
// data is removed from the saved config.
routerRdvMatch();
System.out.println("Using the current configuration.");
}
}
// Set the global debug level.
adjustLog4JPriority();
// If password exists and is NOT
// the default password, then
// pop a dialog to get the password
// Otherwise, we will pass the default
// password as the password.
if (checkPassword()) {
// Configure fully TLS
// Set the password as a system property
configureTls();
// Add root certificate to the config adv
addRootCert();
// Save that config.
save();
// save is done before RouterRdvMatch so that no original data is
// removed from the saved config.
routerRdvMatch();
} else {
platformCanceled = true;
}
}
private boolean checkPassword() {
// If the passwd exists, then validate it.
//
// We are called for three cases:
// 1) Initial configuration of everything
// passwd file does not exist
// 2) REconfiguration: passwd file exists
// 3) REstarting the platfrom: passwd file exists
String password = System.getProperty("net.jxta.tls.password");
String principal = System.getProperty("net.jxta.tls.principal");
if (password != null && principal != null)
return true;
if (JTlsUtil.passwdExists()) {
try {
PasswordDialog p = new PasswordDialog();
p.showDialog();
if (p.wasCanceled()) {
return false;
}
} catch (Throwable t) {
System.out.println("The window-based authenticator does not "
+ "seem to be usable.");
System.out.println("You must authenticate through the "
+ "command-line.");
throw new JxtaError("Could not authenticate.");
}
}
return true;
}
private void configureTls() {
// We configure TLS after the configurator is done.
// Still waiting for the password GUI
String peerName = advertisement.getName();
String password = System.getProperty("net.jxta.tls.password");
String principal = System.getProperty("net.jxta.tls.principal");
// we done with it, rid of it
System.setProperty("net.jxta.tls.password", "");
// Both the principal and the password are always
// set either by the configuration panel or
// security login.
TlsConfig.init(principal, password);
}
// add root certificate to the config Advertisement
public void addRootCert() {
StructuredTextDocument paramDoc =
(StructuredTextDocument)StructuredDocumentFactory.newStructuredDocument(
new MimeMediaType("text", "xml"),
"Parm");
String rootCert = null;
try {
rootCert = TlsConfig.getRootCert();
} catch (IOException e) {
// This should never happen unless the file system is trashed.
if (LOG.isEnabledFor(Priority.ERROR)) LOG.error("Cannot get root cert, Exception = ", e);
return;
}
Element e = paramDoc.createElement("RootCert", rootCert);
paramDoc.appendChild(e);
advertisement.putServiceParam(PeerGroup.peerGroupClassID, paramDoc);
}
class PasswordDialog extends javax.swing.JDialog implements ActionListener {
JTextField principalField = new JTextField(10);
JPasswordField passwordField = new JPasswordField(10);
private boolean principalDone = false;
private boolean passwordDone = false;
/**
* Dialog to prompt for a password
*/
public PasswordDialog() {
super(JOptionPane.getRootFrame(), "JXTA Secure Login", /*modal*/true);
JPanel contentPane = new JPanel(new java.awt.GridBagLayout());
GridBagConstraints c = new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
GridBagConstraints.NONE, new Insets(4, 4, 4, 4), 0, 0);
contentPane.add(new JLabel("Secure Username:"), c);
c.gridy = 1;
contentPane.add(new JLabel("Password:"), c);
c.gridx = 1; c.gridy = 0; c.weightx = 1.0; c.fill = GridBagConstraints.HORIZONTAL;
contentPane.add(principalField, c);
c.gridy = 1;
contentPane.add(passwordField, c);
c.gridx = 0; c.gridy = 2; c.anchor = GridBagConstraints.EAST; c.gridwidth = 2; c.fill = GridBagConstraints.NONE;
JPanel buttonPanel = new JPanel(new java.awt.GridLayout(/*rows*/1, /*cols*/0));
JButton okButton = new JButton("OK");
getRootPane().setDefaultButton(okButton);
JButton cancelButton = new JButton("Cancel");
buttonPanel.add(okButton);
buttonPanel.add(cancelButton);
contentPane.add(buttonPanel, c);
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
okButton.addActionListener(this);
setContentPane(contentPane);
}
public void showDialog() {
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public boolean wasCanceled() {
return canceled;
}
public void actionPerformed(ActionEvent e) {
String password = new String(passwordField.getPassword());
String principal = principalField.getText();
if (principal.length() == 0 || !JTlsUtil.principalIsIssuer(principal) ){
javax.swing.JOptionPane.showMessageDialog(this,
"Invalid Secure Username. Try again.",
"Error Message",
javax.swing.JOptionPane.ERROR_MESSAGE);
return;
}
if (password.length() == 0 || ! JTlsUtil.validPasswd(password) ){
javax.swing.JOptionPane.showMessageDialog(this,
"Invalid Password. Try again.",
"Error Message",
javax.swing.JOptionPane.ERROR_MESSAGE);
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -