⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 explorerthreadgui.java

📁 一个agent 工具包,可以开发移动设备应用,考虑了安全措施
💻 JAVA
字号:
package SOMA.explorer;

import SOMA.security.auth.*;

import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;

import java.io.*;

import java.security.*;
import javax.security.auth.*;
import javax.security.auth.login.*;
import javax.security.auth.callback.*;

/**
 *
 * @author  Mario Zambrini
 * @version
 */
public class ExplorerThreadGUI extends javax.swing.JFrame implements Runnable {

  // Struttura ad albero

  private DirExplorerItem root;
  private DefaultTreeModel treeModel;
  private ExplorerTreeSelectionListener explorerTreeSelectionListener;

  // Pipe per intercettare l'output e mandarlo sulla TextArea
  // Per ora l'output lo mando a System.out

  /**
  private PipedOutputStream pos=new PipedOutputStream();
  private PrintStream out=new PrintStream(pos,true);
  private PipedInputStream pis;
  private InputStreamReader isr;
  private BufferedReader br;
  */

  // Autenticazione

  private LoginContext lc;
  private Subject somaUser;
  private SomaUserCallbackHandler somaUserCallbackHandler;
  private AccessControlContext userContext;

  /** Initializes the Form */
  public ExplorerThreadGUI(DirExplorerItem root) {
    initComponents ();
    pack ();

    this.root=root;
    initUserContext();

  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the FormEditor.
   */
  private void initComponents () {//GEN-BEGIN:initComponents
    setTitle ("Soma");
    addWindowListener (new java.awt.event.WindowAdapter () {
        public void windowClosing (java.awt.event.WindowEvent evt) {
          exitForm (evt);
        }
      }
    );
    getContentPane ().setLayout (new java.awt.BorderLayout ());

    somaMenuBar = new javax.swing.JMenuBar ();
      jMenu1 = new javax.swing.JMenu ();
      jMenu1.setText ("Soma");
        logonSomaMenu = new javax.swing.JMenuItem ();
        logonSomaMenu.setText ("Logon");
        logonSomaMenu.addActionListener (new java.awt.event.ActionListener () {
            public void actionPerformed (java.awt.event.ActionEvent evt) {
              logonSomaMenuActionPerformed (evt);
            }
          }
        );
        jMenu1.add(logonSomaMenu);

        logoutSomaMenu = new javax.swing.JMenuItem ();
        logoutSomaMenu.setEnabled (false);
        logoutSomaMenu.setText ("Logout");
        logoutSomaMenu.addActionListener (new java.awt.event.ActionListener () {
            public void actionPerformed (java.awt.event.ActionEvent evt) {
              logoutSomaMenuActionPerformed (evt);
            }
          }
        );
        jMenu1.add(logoutSomaMenu);

      somaMenuBar.add(jMenu1);


    setJMenuBar(somaMenuBar);
    explorerScrollPane = new javax.swing.JScrollPane ();

      explorerTree = new javax.swing.JTree ();

    explorerScrollPane.setViewportView (explorerTree);
    getContentPane ().add (explorerScrollPane, "North");

    outputScrollPane = new javax.swing.JScrollPane ();

      outputTextArea = new javax.swing.JTextArea ();

    outputScrollPane.setViewportView (outputTextArea);
    getContentPane ().add (outputScrollPane, "Center");

    syntaxPanel = new javax.swing.JPanel ();
    syntaxPanel.setLayout (new java.awt.BorderLayout ());

      syntaxLabel = new javax.swing.JLabel ();
      syntaxLabel.setText ("Description:");
      syntaxPanel.add (syntaxLabel, "Center");

      executeButton = new javax.swing.JButton ();
      executeButton.setText ("Execute");
      executeButton.addActionListener (new java.awt.event.ActionListener () {
          public void actionPerformed (java.awt.event.ActionEvent evt) {
            executeButtonActionPerformed (evt);
          }
        }
      );
      syntaxPanel.add (executeButton, "East");

    getContentPane ().add (syntaxPanel, "South");

  }//GEN-END:initComponents

    /** Quando l'utente clicca execute, verifico la voce selezionata,
        chiedo i parametri e lancio l'esecuzione del comando legando al
        codice il soggetto autenticato.
        Al termine ricostruisco l'abero con le eventuali nuove voci sempre
        legando la chiamata al soggetto autenticato, in modo da mostrare solo
        le voci di menu' relativo al ruolo assunto dal soggetto.
    */

  private void executeButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_executeButtonActionPerformed

    final String parameters;

    TreePath pathToNode=explorerTree.getSelectionPath();
    final ExplorerTreeNode node=(ExplorerTreeNode)pathToNode.getLastPathComponent();
    parameters=JOptionPane.showInputDialog(this,node.getKey()+" "+node.getSyntax(),"Enter Parameters for Command:",JOptionPane.QUESTION_MESSAGE);
    Subject.doAs(somaUser, new ExecutePrivilegedAction(parameters,node));
    Subject.doAs(somaUser, new TreePrivilegedAction());

  }//GEN-LAST:event_executeButtonActionPerformed

    /** Quando l'utente esce dal sistema, effettuo il logout,
        elimino l'albero e riattivo le voci di menu corrette.
    */

  private void logoutSomaMenuActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logoutSomaMenuActionPerformed

    try {

      lc.logout();

    } catch (LoginException ex) {

      ex.printStackTrace();

    }


    treeModel.setRoot(new DefaultMutableTreeNode("/"));
    treeModel.reload();
    explorerTree.removeTreeSelectionListener(explorerTreeSelectionListener);

    syntaxLabel.setText("Description:");

    logonSomaMenu.setEnabled(true);
    logoutSomaMenu.setEnabled(false);
    executeButton.setEnabled(false);

  }//GEN-LAST:event_logoutSomaMenuActionPerformed

    /** Quando l'utente tenta di loggarsi eseguo l'autenticazione
      e poi costruisco l'albero in modo condizionato al soggetto
      autenticato.
    */

  private void logonSomaMenuActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logonSomaMenuActionPerformed

    // Logon  (JAAS)

    try {
      somaUser=new Subject();
      somaUserCallbackHandler=new SomaUserCallbackHandler();
      lc = new LoginContext("Soma",somaUser,somaUserCallbackHandler);
    } catch (LoginException le) {
      le.printStackTrace();
      System.exit(-1);
    }

    // the user has 3 attempts to authenticate successfully
    int i;
    for (i = 0; i < 3; i++) {
      try {
        // attempt authentication
        lc.login();

        // if we return with no exception, authentication succeeded
        break;

      } catch (AccountExpiredException aee) {

        JOptionPane.showMessageDialog(this,"Your account has expired. Please notify your administrator.","Login Failed",JOptionPane.WARNING_MESSAGE);
        System.exit(-1);

      } catch (CredentialExpiredException cee) {

        JOptionPane.showMessageDialog(this,"Your credentials have expired.","Login Failed",JOptionPane.WARNING_MESSAGE);
        System.exit(-1);

      } catch (FailedLoginException fle) {

        JOptionPane.showMessageDialog(this,"Authentication Failed","Login Failed",JOptionPane.WARNING_MESSAGE);

      } catch (Exception e) {

        JOptionPane.showMessageDialog(this,"Unexpected Exception - unable to continue","Login Failed",JOptionPane.WARNING_MESSAGE);
        e.printStackTrace();
        System.exit(-1);
      }

    }

    // did they fail three times?
    if (i == 3) {
      JOptionPane.showMessageDialog(this,"Sorry","Login Failed",JOptionPane.WARNING_MESSAGE);
      System.exit(-1);
    }

    // Construisco l'albero con le voci di men

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -