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

📄 agentmanager.java

📁 《精通RMI:Java与EJB企业级应用开发》书籍的源代码. 本书是讲述RMI技术的经典著作
💻 JAVA
字号:
/*
 * Copyright 1999 by dreamBean Software,
 * All rights reserved.
 */
package masteringrmi.agent.manager;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Properties;
import java.util.Collection;
import java.util.Iterator;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.DefaultListModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;

import masteringrmi.agent.interfaces.AgentHost;
import masteringrmi.agent.interfaces.Agent;

/**
 *   This is a manager client that can be used to access, view, and call
 *   agents running on an agent host.
 *      
 *   @author Rickard 謆erg
 *   @version $Revision:$
 */
public class AgentManager
   extends JFrame
{
   // Constants -----------------------------------------------------
    
   // Attributes ----------------------------------------------------
   AgentHost agentHost;
   
   JLabel agentLabel;
   JList agentList;
   JTextField agentHostField;
   JPanel agentPanel;
   JButton agentRemoveButton;
   
   Agent currentAgent;
   Component currentGUI;
   
    
   // Static --------------------------------------------------------
   public static void main(String[] args)
      throws Exception
   {
      // Set policy to use
      System.setProperty("java.security.policy", AgentManager.class.getResource("/client.policy").toString());
      
      // Set a security manager
      System.setSecurityManager(new SecurityManager());
      
      new AgentManager();
   }
   
   // Constructors --------------------------------------------------
   public AgentManager()
      throws Exception
   {
      // Create GUI
      super("Agent manager");
      
      Container content = getContentPane();
      content.setLayout(new BorderLayout());
      
      agentLabel = new JLabel("Agent manager", JLabel.CENTER);
      agentList = new JList(new DefaultListModel());
      agentList.setBorder(BorderFactory.createTitledBorder("Agents"));
      agentList.setPreferredSize(new Dimension(150,150));
      agentList.setFixedCellWidth(150);
      agentHostField = new JTextField("rmi://localhost/");
      agentHostField.setBorder(BorderFactory.createTitledBorder("Agent host"));
      currentGUI = new JLabel("Select agent host", JLabel.CENTER);
      
      agentHostField.addActionListener(new ActionListener()
      {
         public void actionPerformed(ActionEvent evt)
         {
            try
            {
               // Get host
               findHost(agentHostField.getText());
         
               // Get list of agents
               DefaultListModel list = (DefaultListModel)agentList.getModel();
               list.clear();
               Collection agents = agentHost.getAgents();
               Iterator enum = agents.iterator();
               while (enum.hasNext())
               {
                  list.addElement(enum.next());
               }
               
               setGUI(new JLabel("Select agent", JLabel.CENTER));
               
               agentLabel.setText("Agents at "+agentHostField.getText());
               
               // Clear host field
               agentHostField.setText("");   
            } catch (Exception e)
            {
               e.printStackTrace();
            }
         }
      });
      
      agentList.addListSelectionListener(new ListSelectionListener()
      {
         public void valueChanged(ListSelectionEvent evt)
         {
            if (evt.getValueIsAdjusting())
               return; // Wait until the selection has stabilized
            
            // Get the selected agent and show its GUI
            try
            {
               currentAgent = (Agent)agentList.getSelectedValue();
               
               setGUI(currentAgent.getComponent());
            } catch (Exception e)
            {
               e.printStackTrace();
            }
         }
      });
      
      agentList.setCellRenderer(new DefaultListCellRenderer()
      {
         public Component getListCellRendererComponent(JList list,
                                              Object value,
                                              int index,
                                              boolean isSelected,
                                              boolean cellHasFocus)
         {
            String name = "Error";
            try
            {
               name = ((Agent)value).getName();
            } catch (RemoteException e)
            {
               // Ignore - name will be "Error"
            }
         
            return super.getListCellRendererComponent(list, 
                                                      name,
                                                      index,
                                                      isSelected,
                                                      cellHasFocus);
         }
      });
      
      agentRemoveButton = new JButton("Remove agent");
      agentRemoveButton.addActionListener(new ActionListener()
      {
         public void actionPerformed(ActionEvent evt)
         {
            try
            {
               if (currentAgent == null)
                  return;
            
               // Remove agent GUI
               setGUI(new JLabel("Select agent", JLabel.CENTER));

               // Remove agent from host
               agentHost.removeAgent(currentAgent);
               
               // Update list of agents
               DefaultListModel list = (DefaultListModel)agentList.getModel();
               list.clear();
               Collection agents = agentHost.getAgents();
               Iterator enum = agents.iterator();
               while (enum.hasNext())
               {
                  list.addElement(enum.next());
               }
               
               currentAgent = null;
               
               JOptionPane.showMessageDialog(SwingUtilities.getRoot(AgentManager.this), "The agent has been successfully removed!", "Agent removed", JOptionPane.PLAIN_MESSAGE); 
            } catch (Exception e)
            {
               e.printStackTrace();
            }
         }
      });
      
      agentPanel = new JPanel(new BorderLayout());
      agentPanel.add("Center", agentList);
      agentPanel.add("South", agentRemoveButton);
      
      content.add("North", agentLabel);
      content.add("West", agentPanel);
      content.add("Center", currentGUI);
      content.add("South", agentHostField);
      
      agentRemoveButton = new JButton("Remove");
      
      setSize(500,300);
      show();
   }

   // Public --------------------------------------------------------

   // Package protected ---------------------------------------------
   void setGUI(Component gui)
   {
      if (currentGUI != null)
         getContentPane().remove(currentGUI);
  
      getContentPane().add("Center", gui);
      getContentPane().doLayout();
      getContentPane().repaint();
      
      currentGUI = gui;
   }
   
   void findHost(String host)
      throws NamingException
   {
      agentHost = (AgentHost)new InitialContext().lookup(host+"agenthost");
   }
    
   // Protected -----------------------------------------------------
    
   // Private -------------------------------------------------------

   // Inner classes -------------------------------------------------
}

⌨️ 快捷键说明

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