Code Search for Developers
 
 
  

MyProxyGUI.java from GridBlocks at Krugle


Show MyProxyGUI.java syntax highlighted

/*
 * Copyright (c) 2007 
 * Helsinki Institute of Physics
 * see LICENSE file for details
 *
 * MyProxyGUI.java
 */

package fi.hip.gb.client.ui.myproxy;

import java.awt.Container;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.Timer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.globus.myproxy.MyProxy;

import fi.hip.gb.client.ConfigUI;
import fi.hip.gb.client.Main;
import fi.hip.gb.core.Config;
import fi.hip.gb.utils.FileUtils;
import fi.hip.gb.utils.MyProxyInit;

/**
 * GUI for using myproxy server. Allowes changing of properties,
 * downloading proxies and uploading proxies to myproxy server. 
 * 
 * @author Juho Karppinen
 */
public class MyProxyGUI extends JDialog {
    /**
     * 
     */
    private static final long serialVersionUID = 6850632355852536651L;
    private static Log log = LogFactory.getLog(MyProxyGUI.class);
    
	/**
	 * Construct new dialog for intializing the grid proxy from myproxy server
	 * @param owner owner dialog, needed if parent dialog is modal
	 * @param modal true if modal dialog
	 */
	public MyProxyGUI(Frame owner, boolean modal) {
		super(owner, modal);
		init();
	}

	/**
	 * Initialize the GUI
	 */
	private void init() {
		setTitle("MyProxy Init");

		JPanel loginPanel = new JPanel(new GridLayout(2, 2, 2, 2));;
		_usernameTF = new JTextField();
		_usernameTF.setText((Config.getInstance().getMyProxyUser() != null
						&& Config.getInstance().getMyProxyUser().length() > 0)
				? Config.getInstance().getMyProxyUser()
				: System.getProperty("user.name"));
			
		_passwordTF = new JPasswordField(15);
		
		loginPanel.add(new JLabel("MyProxy Username: "));
		loginPanel.add(_usernameTF);
		loginPanel.add(new JLabel("MyProxy Password: "));
		loginPanel.add(_passwordTF);

		JPanel buttonPanel = new JPanel(new GridLayout(1, 3, 2, 2));
		_getButton = new JButton("Get");
		_putButton = new JButton("Put");
		_infoButton = new JButton("Info");
		_cancelButton = new JButton("Cancel");
		_optionsButton = new JButton("Options");
		
		_getButton.setMnemonic(KeyEvent.VK_G);
		_putButton.setMnemonic(KeyEvent.VK_P);
		_cancelButton.setMnemonic(KeyEvent.VK_C);
		_infoButton.setMnemonic(KeyEvent.VK_I);
		_optionsButton.setMnemonic(KeyEvent.VK_O);
		
		_passwordTF.requestFocus();
		getRootPane().setDefaultButton(_getButton);
		
		_getButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (!validateSettings())
					return;
				getProxy();
			}
		});
		
		_putButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (!validateSettings())
					return;
				new PasswordDialog(MyProxyGUI.this);
			}
		});
		_infoButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getInfo();
			}
		});
		_optionsButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new ConfigUI(2);
			}
		});
		_cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});

		buttonPanel.add(_getButton);
		buttonPanel.add(_putButton);
		buttonPanel.add(_infoButton);
		buttonPanel.add(_optionsButton);
		buttonPanel.add(_cancelButton);

		Container contentPane = this.getContentPane();
		BoxLayout bl = new BoxLayout(contentPane, BoxLayout.Y_AXIS);
		contentPane.setLayout(bl);

		contentPane.add(loginPanel);
		contentPane.add(buttonPanel);
	}

	/**
	 * Validate UI fields
	 * @return true if all fields are valid
	 */
	private boolean validateSettings() {
		if (_usernameTF.getText().trim().equals("")) {
			Main.errorMessage("Please enter your username", "Need More Information", this);
			return false;
		}

		char[] pwd = _passwordTF.getPassword();
		if (pwd.length == 0) {
			Main.errorMessage("Please enter your password", "Need More Information", this);
			return false;
		}
		return true;
	}

	/**
	 * Download proxy from the myproxy server
	 */
	protected void getProxy() {
		disableButtons();
		String successMessage = "A proxy has been received for user " 
			+ _usernameTF.getText() + " to file " + Config.getInstance().getProxyFile();
		process(MyProxy.GET_PROXY, "Downloading proxy", successMessage);
		enableButtons();
	}

	/**
	 * Download info from the myproxy server
	 */
	protected void getInfo() {
		disableButtons();
		process(MyProxy.INFO_PROXY, "Receiving information", "");
		/*
		Window info = new Window(MyProxyGUI.this, "Myproxy on " + Config.getMyProxyServer());
		info.addOkButton();
		JTextArea ta = new JTextArea();
		info.setContent(new JScrollPane(ta));
		Utils.center(info);
		info.show();
		*/
		enableButtons();
	}

	/**
	 * Called when user has entered password for certificate private key
	 * @param password certificate private key
	 */ 
	protected void putProxy(String password) {
		disableButtons();
		_privatePassword = password;
		String successMessage = "A proxy valid for " 
            + Config.getInstance().getMyProxyRemoteLifetime() / 60 
            + " hours ("
			+ (Config.getInstance().getMyProxyRemoteLifetime() / (60 * 24))	
            + " days) for user "
			+ _usernameTF.getText()	+ " now exists on " 
            + Config.getInstance().getMyProxyServer() + ".";
		
		if(password != null)
			process(MyProxy.PUT_PROXY, "Uploading proxy", successMessage);
			
		enableButtons();
	}
	
	private void enableButtons() {
		_getButton.setEnabled(true);
		_putButton.setEnabled(true);
		_infoButton.setEnabled(true);
		_optionsButton.setEnabled(true);
		_cancelButton.setEnabled(true);
	}
	
	private void disableButtons() {
		_getButton.setEnabled(false);
		_putButton.setEnabled(false);
		_infoButton.setEnabled(false);
		_optionsButton.setEnabled(false);
		_cancelButton.setEnabled(false);
	}	

	/**
	 * Start the job with progressbar showing some activity
	 * @param operation task number from <code>org.globus.myproxy.MyProxy</code>
	 * @param actionMessage message shown to user while executing
	 * @param completedMessage message shown to user when execution is completed
	 * successfully
	 */
	private void process(int operation, String actionMessage, String completedMessage) {
		final JLabel msgLabel = new JLabel(actionMessage + "...");
		final JProgressBar progressBar = new JProgressBar(0, MAX);

		progressBar.setValue(0);

		Object[] comp = { msgLabel, progressBar };
		Object[] options = { "Cancel" };

		JOptionPane pane =
			new JOptionPane(
				comp,
				JOptionPane.DEFAULT_OPTION,
				JOptionPane.INFORMATION_MESSAGE,
				null,
				options,
				options[0]);

		final JDialog dialog = pane.createDialog(null, actionMessage);

		final Task task = new Task(operation);
		Timer timer = new Timer(250, new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (task.isDone()) {
					dialog.setVisible(false);
				} else {
					progressBar.setValue((progressBar.getValue() + 1) % MAX);
				}
			}
		});

		timer.start();
		task.start();

		try {
			Thread.sleep(500);
		} catch (Exception e) {
		}
		Object selectedValue = null;
		if (task.getException() == null) {
			dialog.setVisible(true);
			selectedValue = pane.getValue();
		} else {
			selectedValue = JOptionPane.UNINITIALIZED_VALUE;
		}
		
		timer.stop();
		if (selectedValue == null || selectedValue == options[0]) {
			// window closed by user or cancel pressed
			dialog.setVisible(false);
		} else if (selectedValue == JOptionPane.UNINITIALIZED_VALUE) {
			// task finished
			Exception e = task.getException();
			if (e == null) {
				// get additional message
				if(task.getMessage() != null)
					completedMessage += "<br>" + task.getMessage();
				JOptionPane.showMessageDialog(
					this,
					"<html>" + completedMessage + "</html>",
					"Succeeded",
					JOptionPane.INFORMATION_MESSAGE);

				Config.getInstance().setMyProxyUser(_usernameTF.getText());
				
				// only close when receiving proxy
				if(operation == MyProxy.GET_PROXY)
					setVisible(false);
			} else {
				Main.errorMessage(e.getMessage(), MyProxyGUI.this);
			}
		}
	}
	
	/**
	 * Does the actual job in own thread, keeps UI interactive
	 */
	class Task extends Thread {
		private int _operation = MyProxy.GET_PROXY;
		private boolean _done;
		private Exception _exception;
		private String _msg;

		/**
		 * Execute the task
		 * @param operation task number from <code>org.globus.myproxy.MyProxy</code>
		 */
		public Task(int operation) {
			_operation = operation;
		}

		public void run() {
			try {
				log.info("Starting myproxy initialization with task " + _operation);
				MyProxyInit proxyinit = new MyProxyInit(
						_usernameTF.getText(),
						new String(_passwordTF.getPassword()),
						Config.getInstance().getMyProxyServer(),
						Config.getInstance().getMyProxyPort(),
						Config.getInstance().getMyProxyDn(),
						Config.getInstance().getMyProxyLocalLifetime() * 60,
						Config.getInstance().getMyProxyRemoteLifetime() * 60);
				if(_operation == MyProxy.PUT_PROXY) {
					if(_privatePassword != null)
						proxyinit.doPut(_privatePassword);
				} else if(_operation == MyProxy.GET_PROXY){
					FileUtils.writeFile(Config.getInstance().getProxyFile(), proxyinit.doGet(true));
				} else {
					_msg = proxyinit.doInfo();
				}
			} catch (Exception e) {
				_exception = e;
				log.error(e.getMessage(), e);
				_done = true;
			}

			_done = true;
		}
		/**
		 * Gets finished status
		 * @return true if operation is finished, false if not
		 */
		public boolean isDone() {
			return _done;
		}

		/**
		 * Gets exception if occured during the myproxy operations
		 * @return exception or null if no exceptions
		 */
		public Exception getException() {
			return _exception;
		}
		/**
		 * Gets additional message show to user
		 * @return message, or null if no message
		 */
		public String getMessage() {
			return _msg;
		}
	}
	
	/** user name field */
	private JTextField _usernameTF;
	/** password field */
	private JPasswordField _passwordTF;
	/** button for options */
	private JButton _optionsButton;
	/** button for closing the dialog */
	private JButton _cancelButton;
	/** button for receiving proxy */
	private JButton _getButton;
	/** button for uploading proxy */
	private JButton _putButton;
	/** button for showing information */
	private JButton _infoButton;
	/** password for certificate password */
	private String _privatePassword;
	/** maximum steps on progress bar */
	private final int MAX = 10;
	/** is DN of the certificate also a username */
	//private boolean _dnAsUsername;
}



See more files for this project here

GridBlocks

GridBlocks builds a grid application framework via easy-to-use building blocks in distributed environment. The framework offers components for Grid security, distributed storage, computing, and Portlet web interfaces.

Project homepage: http://sourceforge.net/projects/gridblocks
Programming language(s): Java,JSP,XML
License: other

  MyProxyGUI.java
  MyProxyOptions.java
  PasswordDialog.java
  package.html