/** * 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: **/ // This form handles the user management tasks: // Adding a new user, modifying an exsisting account, and deleting a user account // There are two types of users // user: is able to use the simulation service without managing the user accounts // admin: is able to use the simulation service and managing the user accounts package ca.gc.nrc.iit.pds.appgui.simulation; import SimulationService.stubs.CDppPortType; import SimulationService.pstubs.PCDppPortType; import java.awt.BorderLayout; import javax.swing.JFrame; import java.awt.Dimension; import javax.swing.JPanel; import javax.swing.BorderFactory; import com.borland.jbcl.layout.XYLayout; import com.borland.jbcl.layout.*; import java.awt.Color; import javax.swing.JTextField; import javax.swing.JLabel; import java.awt.Font; import javax.swing.JFormattedTextField; import javax.swing.JPasswordField; import javax.swing.JButton; import javax.swing.JComboBox; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import sun.misc.BASE64Encoder; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.rmi.RemoteException; import java.io.UnsupportedEncodingException; //import com.borland.dx.sql.dataset.ProcedureResolver; import javax.swing.JOptionPane; public class UserFrame extends JFrame { private final int MODE_DISTRIBUTED = 1; private final int MODE_PARALLEL = 2; private String serviceAddress = null; private MainFrame parentFrame = null; // DCD++ / PCD++ stub classes private CDppPortType cdppPortType = null; private PCDppPortType pcdppPortType = null; private int sessionID = -1; private String userRole = "user"; MessageDigest messageDigest = null; BorderLayout borderLayout1 = new BorderLayout(); JPanel jPanel1 = new JPanel(); XYLayout xYLayout1 = new XYLayout(); JLabel jLabelUsername = new JLabel(); JLabel jLabelPassword = new JLabel(); JFormattedTextField jFTUsername = new JFormattedTextField(); JPasswordField jPasswordField = new JPasswordField(); JButton jButtonUpdate = new JButton(); JButton jButtonAdd = new JButton(); JButton jButtonDelete = new JButton(); JComboBox jComboBoxUserTypes = new JComboBox(); JLabel jLabelRole = new JLabel(); JButton jButtonCancel = new JButton(); //ProcedureResolver procedureResolver1 = new ProcedureResolver(); JLabel statusBar = new JLabel(); public UserFrame(MainFrame parentFrame, String serviceAddress, int sessionID) { // Sets the sessionID, parentFrame , and the serviceAddress variables this.sessionID = sessionID; this.parentFrame = parentFrame; this.serviceAddress = serviceAddress; // Retrieving the operation mode from the main form and assigining it locally switch( ((MainFrame)this.parentFrame).getMode()) { case MODE_DISTRIBUTED: cdppPortType = (this.parentFrame).getCDppPortType(); break; case MODE_PARALLEL: pcdppPortType = (this.parentFrame).getPCDppPortType(); break; } try { jbInit(); } catch (Exception exception) { exception.printStackTrace(); } } private void jbInit() throws Exception { this.getContentPane().setLayout(borderLayout1); jPanel1.setLayout(xYLayout1); jPanel1.setBorder(BorderFactory.createEtchedBorder()); this.setResizable(false); this.setTitle("Users"); jLabelUsername.setFont(new java.awt.Font("Dialog", Font.BOLD, 12)); jLabelUsername.setForeground(Color.blue); jLabelUsername.setText("Username"); jLabelPassword.setFont(new java.awt.Font("Dialog", Font.BOLD, 12)); jLabelPassword.setForeground(Color.blue); jLabelPassword.setText("Password"); jButtonUpdate.setText("Update"); jButtonUpdate.addActionListener(new userFrame_jButtonUpdate_actionAdapter(this)); jButtonAdd.setToolTipText(""); jButtonAdd.setText("Add"); jButtonAdd.addActionListener(new UserFrame_jButtonAdd_actionAdapter(this)); jButtonDelete.setText("Delete"); jButtonDelete.addActionListener(new userFrame_jButtonDelete_actionAdapter(this)); jLabelRole.setFont(new java.awt.Font("Dialog", Font.BOLD, 12)); jLabelRole.setForeground(Color.blue); jLabelRole.setText("Role"); jButtonCancel.setText("Cancel"); jButtonCancel.addActionListener(new UserFrame_jButtonCancel_actionAdapter(this)); statusBar.setFont(new java.awt.Font("Dialog", Font.BOLD, 12)); statusBar.setBorder(BorderFactory.createEtchedBorder()); statusBar.setText(" "); this.getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER); jPanel1.add(jLabelUsername, new XYConstraints(8, 10, 85, 27)); jPanel1.add(jLabelPassword, new XYConstraints(8, 44, -1, -1)); jPanel1.add(jButtonAdd, new XYConstraints(8, 74, 81, 27)); jPanel1.add(jFTUsername, new XYConstraints(72, 14, 105, 22)); jPanel1.add(jPasswordField, new XYConstraints(72, 43, 105, 22)); jPanel1.add(jLabelRole, new XYConstraints(197, 16, 53, 20)); jPanel1.add(jButtonUpdate, new XYConstraints(95, 74, 81, 27)); jPanel1.add(jButtonDelete, new XYConstraints(183, 74, 81, 27)); jPanel1.add(jButtonCancel, new XYConstraints(270, 74, 81, 27)); jPanel1.add(jComboBoxUserTypes, new XYConstraints(240, 15, 111, 21)); this.getContentPane().add(statusBar, java.awt.BorderLayout.SOUTH); this.setSize(new Dimension(375, 162)); String[] roles= new String[] { "User", "Administrator"}; for (int i = 0 ; i < roles.length; i++ ){ jComboBoxUserTypes.addItem(roles[i]); } this.setLocationRelativeTo(null); this.setVisible(true); } public void jButtonCancel_actionPerformed(ActionEvent e) { jFTUsername.setText(""); jPasswordField.setText(""); jComboBoxUserTypes.setSelectedIndex(0); this.setVisible(false); this.dispose(); } // Adding new user account, the information needed is username, password, role: user/admin public void jButtonAdd_actionPerformed(ActionEvent e) { String password = ""; for (int i = 0; i < jPasswordField.getPassword().length; i++) password = password + jPasswordField.getPassword()[i]; try{ messageDigest = MessageDigest.getInstance("SHA"); messageDigest.update(password.getBytes("UTF-8")); byte[] raw = messageDigest.digest(); String hashPassword = (new BASE64Encoder()).encode(raw); if(jComboBoxUserTypes.getSelectedItem().toString().equals("User")){ userRole = "user"; } else if ( jComboBoxUserTypes.getSelectedItem().toString().equals("Administrator")) { userRole = "admin"; } switch(parentFrame.getMode()) { case MODE_DISTRIBUTED: statusBar.setText(cdppPortType.addUser(sessionID, jFTUsername.getText(), hashPassword, userRole)); break; case MODE_PARALLEL: statusBar.setText(pcdppPortType.addUser(sessionID, jFTUsername.getText(), hashPassword, userRole)); break; } } catch (RemoteException exp) { JOptionPane.showMessageDialog(this, exp.getMessage(),"Error",JOptionPane.ERROR_MESSAGE); } catch ( NoSuchAlgorithmException exp) { exp.printStackTrace(); } catch ( UnsupportedEncodingException exception) { exception.printStackTrace(); } } // Updates an exsisting user account public void jButtonUpdate_actionPerformed(ActionEvent e) { String password = ""; for (int i = 0; i < jPasswordField.getPassword().length; i++) password = password + jPasswordField.getPassword()[i]; try{ messageDigest = MessageDigest.getInstance("SHA"); messageDigest.update(password.getBytes("UTF-8")); byte[] raw = messageDigest.digest(); String hashPassword = (new BASE64Encoder()).encode(raw); if(jComboBoxUserTypes.getSelectedItem().toString().equals("User")){ userRole = "user"; } else if ( jComboBoxUserTypes.getSelectedItem().toString().equals("Administrator")) { userRole = "admin"; } switch(parentFrame.getMode()) { case MODE_DISTRIBUTED: statusBar.setText(cdppPortType.updateUserInfo(sessionID, jFTUsername.getText(), hashPassword, userRole)); break; case MODE_PARALLEL: statusBar.setText(pcdppPortType.updateUserInfo(sessionID, jFTUsername.getText(), hashPassword, userRole)); break; } } catch (RemoteException exp) { JOptionPane.showMessageDialog(this, exp.getMessage(),"Error",JOptionPane.ERROR_MESSAGE); } catch ( NoSuchAlgorithmException exp) { exp.printStackTrace(); } catch ( UnsupportedEncodingException exception) { exception.printStackTrace(); } } // Deletes a user account public void jButtonDelete_actionPerformed(ActionEvent e) { try { switch(parentFrame.getMode()) { case MODE_DISTRIBUTED : statusBar.setText(cdppPortType.delUser(sessionID, jFTUsername.getText())); break; case MODE_PARALLEL: statusBar.setText(pcdppPortType.delUser(sessionID, jFTUsername.getText())); break; } }catch (RemoteException exp) { JOptionPane.showMessageDialog(this, exp.getMessage(),"Error",JOptionPane.ERROR_MESSAGE); } } } class userFrame_jButtonDelete_actionAdapter implements ActionListener { private UserFrame adaptee; userFrame_jButtonDelete_actionAdapter(UserFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButtonDelete_actionPerformed(e); } } class userFrame_jButtonUpdate_actionAdapter implements ActionListener { private UserFrame adaptee; userFrame_jButtonUpdate_actionAdapter(UserFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButtonUpdate_actionPerformed(e); } } class UserFrame_jButtonAdd_actionAdapter implements ActionListener { private UserFrame adaptee; UserFrame_jButtonAdd_actionAdapter(UserFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButtonAdd_actionPerformed(e); } } class UserFrame_jButtonCancel_actionAdapter implements ActionListener { private UserFrame adaptee; UserFrame_jButtonCancel_actionAdapter(UserFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButtonCancel_actionPerformed(e); } }