/** * Advanced Real-Time Simulation Lab (ARS) - Carleton University * * PROJECT: Web Service-Enabled DCD++ Client * AUTHOR(S): Rami Madhoun * PURPOSE: Euaclyptus * CMD LINE ARG: * OUTPUT: * DATE CREATED: Nov 25, 2006 * LAST MOD: Jan 10, 2007 * LAST MOD BY: Rami Madhoun * COPYRIGHT: ARS-Carleton University * USAGE: * COMMENTS: **/ package ca.gc.nrc.iit.pds.appgui.simulation; import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.*; import com.borland.jbcl.layout.XYLayout; import com.borland.jbcl.layout.*; import javax.swing.JLabel; import javax.swing.JFormattedTextField; import javax.swing.JPasswordField; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import SimulationService.stubs.CDppPortType; import SimulationService.stubs.CDppPortTypeService; import SimulationService.stubs.CDppPortTypeServiceLocator; import SimulationService.pstubs.PCDppPortType; import SimulationService.pstubs.PCDppPortTypeService; import SimulationService.pstubs.PCDppPortTypeServiceLocator; import SimulationService.wrapper.JavaWrapper; import java.rmi.*; import java.security.MessageDigest; import sun.misc.BASE64Encoder; import sun.misc.CharacterEncoder; import java.security.NoSuchAlgorithmException; import java.io.UnsupportedEncodingException; import java.awt.Dimension; import javax.swing.BorderFactory; import java.awt.Font; public class AuthenticateFrame extends JFrame { // Operation mode : DCD++ / PCD++ private final int MODE_DISTRIBUTED = 1; private final int MODE_PARALLEL = 2 ; int sessionID = -1; String serviceAddress = null; // DCD++ stubs classes CDppPortType cdppPortType = null; CDppPortTypeService cdppService = null; // PCD++ stubs classes PCDppPortType pcdppPortType = null; PCDppPortTypeService pcdppService = null; // Reference to the main form MainFrame parentFrame = null; // Used to encode the password before sending it to the simulation service MessageDigest messageDigest; JPanel jPanel1 = new JPanel(); XYLayout xYLayout1 = new XYLayout(); BorderLayout borderLayout1 = new BorderLayout(); JButton jButtonLogin = new JButton(); JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); JFormattedTextField jFTUsername = new JFormattedTextField(); JButton jButtonCancel = new JButton(); JPasswordField jPasswordField = new JPasswordField(); JPanel jPanel2 = new JPanel(); JRadioButton jRBNewSession = new JRadioButton(); XYLayout xYLayout2 = new XYLayout(); JRadioButton jRBExsisting = new JRadioButton(); JTextField jTFSessionID = new JTextField(); ButtonGroup buttonGroup1 = new ButtonGroup(); public AuthenticateFrame(MainFrame frame, String serviceAddress) { try { this.parentFrame = frame; this.serviceAddress = serviceAddress; jbInit(); // Initializes the service stub classes switch(parentFrame.getMode()) { case MODE_DISTRIBUTED: cdppService = new CDppPortTypeServiceLocator(); cdppPortType = cdppService.getCDppPortType(new java.net.URL(serviceAddress)); break; case MODE_PARALLEL: pcdppService = new PCDppPortTypeServiceLocator(); pcdppPortType = pcdppService.getPCDppPortType(new java.net.URL(serviceAddress)); break; } } catch (Exception exception) { exception.printStackTrace(); } } private void jbInit() throws Exception { getContentPane().setLayout(borderLayout1); this.setSize(new Dimension(293, 194)); this.setDefaultLookAndFeelDecorated(true); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setLocationRelativeTo(null); this.setResizable(false); this.setTitle("Login"); jPanel1.setBorder(BorderFactory.createEtchedBorder()); xYLayout1.setWidth(293); xYLayout1.setHeight(120); jPanel1.setLayout(xYLayout1); jButtonLogin.setNextFocusableComponent(jButtonCancel); jButtonLogin.setText("Login"); jButtonLogin.addActionListener(new AuthenticateFrame_jButtonLogin_actionAdapter(this)); jLabel1.setFont(new java.awt.Font("Dialog", Font.BOLD, 12)); jLabel1.setForeground(Color.blue); jLabel1.setToolTipText(""); jLabel1.setText("Username"); jLabel2.setFont(new java.awt.Font("Dialog", Font.BOLD, 12)); jLabel2.setForeground(Color.blue); jLabel2.setText("Password"); jButtonCancel.setText("Cancel"); jButtonCancel.addActionListener(new AuthenticateFrame_jButtonCancel_actionAdapter(this)); jFTUsername.setNextFocusableComponent(jPasswordField); jPasswordField.setNextFocusableComponent(jButtonLogin); jPanel2.setBorder(BorderFactory.createEtchedBorder()); jPanel2.setLayout(xYLayout2); jRBNewSession.setFont(new java.awt.Font("Dialog", Font.BOLD, 12)); jRBNewSession.setForeground(Color.blue); jRBNewSession.setSelected(true); jRBNewSession.setText("New Session"); jRBNewSession.addActionListener(new AuthenticateFrame_jRBNewSession_actionAdapter(this)); jRBExsisting.setFont(new java.awt.Font("Dialog", Font.BOLD, 12)); jRBExsisting.setForeground(Color.blue); jRBExsisting.setText("Exsisting Session"); jRBExsisting.addActionListener(new AuthenticateFrame_jRBExsisting_actionAdapter(this)); jTFSessionID.setEnabled(false); jTFSessionID.setFont(new java.awt.Font("SansSerif", Font.PLAIN, 12)); this.getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER); jPanel1.add(jPasswordField, new XYConstraints(73, 48, 117, 23)); jPanel1.add(jLabel1, new XYConstraints(4, 19, 65, 23)); jPanel1.add(jFTUsername, new XYConstraints(73, 20, 117, 23)); jPanel1.add(jLabel2, new XYConstraints(4, 44, 66, 25)); jPanel1.add(jButtonLogin, new XYConstraints(193, 20, 76, 24)); jPanel1.add(jButtonCancel, new XYConstraints(193, 48, 76, 24)); jPanel1.add(jPanel2, new XYConstraints(5, 82, 265, 72)); jPanel2.add(jRBNewSession, new XYConstraints(6, 7, -1, -1)); jPanel2.add(jRBExsisting, new XYConstraints(6, 31, -1, -1)); jPanel2.add(jTFSessionID, new XYConstraints(142, 35, 112, -1)); this.setVisible(true); buttonGroup1.add(jRBExsisting); buttonGroup1.add(jRBNewSession); } public void jButtonCancel_actionPerformed(ActionEvent e) { this.setVisible(false); this.dispose(); } public void jButtonLogin_actionPerformed(ActionEvent e) { try { // Retrieve the password entered by the user and encodes it using a one-way hashing algorithm String password = ""; for (int i = 0; i < jPasswordField.getPassword().length; i++) password = password + jPasswordField.getPassword()[i]; messageDigest = MessageDigest.getInstance("SHA"); messageDigest.update(password.getBytes("UTF-8")); byte[] raw = messageDigest.digest(); String hashPassword = ( new BASE64Encoder()).encode(raw); if ( jRBNewSession.isSelected()== true) { // If the user chooses to initialize new session switch(parentFrame.getMode()) { case MODE_DISTRIBUTED: sessionID = cdppPortType.authenticate(jFTUsername.getText(), hashPassword, false,-1); break; case MODE_PARALLEL: sessionID = pcdppPortType.authenticate(jFTUsername.getText(), hashPassword, false,-1); break; } parentFrame.isExsistingSession(false); }else if ( jRBExsisting.isSelected() == true ) { // if the user chooses to connect to an exsisting session switch(parentFrame.getMode()) { case MODE_DISTRIBUTED: sessionID = cdppPortType.authenticate(jFTUsername.getText(), hashPassword, true, Integer.parseInt(jTFSessionID.getText()) ); break; case MODE_PARALLEL: sessionID = pcdppPortType.authenticate(jFTUsername.getText(), hashPassword, true, Integer.parseInt(jTFSessionID.getText()) ); } parentFrame.isExsistingSession(true); } // After successful loging, the sessionID is set in the main form parentFrame.setSessionID(sessionID); this.setVisible(false); this.dispose(); } catch ( RemoteException excep) { excep.printStackTrace(); } catch ( NoSuchAlgorithmException exp) { exp.printStackTrace(); } catch ( UnsupportedEncodingException exception) { exception.printStackTrace(); } } public void jRBExsisting_actionPerformed(ActionEvent e) { if(jRBExsisting.isSelected()== true) { jTFSessionID.setEnabled(true); } } public void jRBNewSession_actionPerformed(ActionEvent e) { if(jRBNewSession.isSelected() == true) { jTFSessionID.setEnabled(false); } } } class AuthenticateFrame_jRBNewSession_actionAdapter implements ActionListener { private AuthenticateFrame adaptee; AuthenticateFrame_jRBNewSession_actionAdapter(AuthenticateFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jRBNewSession_actionPerformed(e); } } class AuthenticateFrame_jRBExsisting_actionAdapter implements ActionListener { private AuthenticateFrame adaptee; AuthenticateFrame_jRBExsisting_actionAdapter(AuthenticateFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jRBExsisting_actionPerformed(e); } } class AuthenticateFrame_jButtonLogin_actionAdapter implements ActionListener { private AuthenticateFrame adaptee; AuthenticateFrame_jButtonLogin_actionAdapter(AuthenticateFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButtonLogin_actionPerformed(e); } } class AuthenticateFrame_jButtonCancel_actionAdapter implements ActionListener { private AuthenticateFrame adaptee; AuthenticateFrame_jButtonCancel_actionAdapter(AuthenticateFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButtonCancel_actionPerformed(e); } }