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

📄 admincontroller.java

📁 一个类似于openJMS分布在ObjectWeb之下的JMS消息中间件。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * JORAM: Java(TM) Open Reliable Asynchronous Messaging * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or any later version. *  * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * Lesser General Public License for more details. *  * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 * USA. * * Initial developer(s): Alexander Fedorowicz * Contributor(s): */package org.objectweb.joram.client.tools.admin;import java.util.Hashtable;import java.util.Iterator;import java.util.List;import java.util.Enumeration;import java.net.ConnectException;import javax.naming.*;import javax.swing.*;import java.awt.event.*;import javax.swing.tree.*;import javax.jms.*;import org.objectweb.joram.client.jms.admin.*;import org.objectweb.joram.client.jms.tcp.*;import org.objectweb.joram.client.jms.Queue;import org.objectweb.joram.client.jms.Topic;import org.objectweb.joram.client.jms.Destination;import org.objectweb.util.monolog.api.*;public class AdminController{  private boolean adminConnected = false;  private String adminConnectionStr = "Not connected";  private ControllerEventListener gui;  private DefaultTreeModel adminTreeModel;  private PlatformTreeNode adminRoot;    private DefaultTreeModel jndiTreeModel;  private MutableTreeNode jndiRoot;  private String namedCtx = "";  private Context ctx = null;  private boolean jndiConnected = false;  public static final String DEFAULT_ADMIN_HOST = "localhost";  public static final String DEFAULT_ADMIN_PORT = "16010";    public static final String PROP_JNDI_FACTORY = "java.naming.factory.initial";  public static final String PROP_JNDI_HOST = "java.naming.factory.host";  public static final String PROP_JNDI_PORT = "java.naming.factory.port";    public static final String DEFAULT_JNDI_FACTORY = "fr.dyade.aaa.jndi2.client.NamingContextFactory";  public static final String DEFAULT_JNDI_HOST = "localhost";  public static final String DEFAULT_JNDI_PORT = "16400";  private static final String STR_ADMIN_DISCONNECTED = "Disconnected";  private static final String STR_JNDI_DISCONNECTED = "Disconnected";  public AdminController()  {    adminRoot = new PlatformTreeNode(this, STR_ADMIN_DISCONNECTED);    adminTreeModel = new DefaultTreeModel(adminRoot, true);    jndiRoot = new DefaultMutableTreeNode(STR_JNDI_DISCONNECTED);    jndiTreeModel = new DefaultTreeModel(jndiRoot, true);  }  public void setControllerEventListener(ControllerEventListener l)  {  	this.gui = l;  }  protected DefaultTreeModel getAdminTreeModel() { return adminTreeModel; }    protected DefaultTreeModel getJndiTreeModel() { return jndiTreeModel; }  public void connectJndi(String host, int port, String ctxName) throws NamingException  {    namedCtx = ctxName;        Hashtable env = new Hashtable();    env.put(PROP_JNDI_FACTORY,            System.getProperty(PROP_JNDI_FACTORY) != null ? System.getProperty(PROP_JNDI_FACTORY) : DEFAULT_JNDI_FACTORY);    env.put(PROP_JNDI_HOST, host);    env.put(PROP_JNDI_PORT, Integer.toString(port));        ctx = new InitialContext(env);    jndiConnected = true;    if (ctxName != null) {      ctx = (Context)ctx.lookup(ctxName);    }    jndiRoot.setUserObject((ctxName == null || ctxName.length() == 0) ? "Root Context" : ctxName);    jndiTreeModel.nodeChanged(jndiRoot);    refreshJndiData();  }  public void refreshJndiData() throws NamingException  {    cleanupJndiTree();     for (NamingEnumeration e = ctx.list(""); e.hasMore();) {      NameClassPair pair = (NameClassPair) e.next();      JndiTreeNode node = new JndiTreeNode(this, ctx, pair.getName());      insertJndiNode(node);    }  }  public void disconnectJndi() throws NamingException  {    ctx.close();    jndiRoot.setUserObject(STR_JNDI_DISCONNECTED);    jndiTreeModel.nodeChanged(jndiRoot);    cleanupJndiTree();    ctx = null;    jndiConnected = false;  }  public JndiTreeNode findJndiNodeByName(String name)  {    int i;    for (i = 0; i < jndiRoot.getChildCount(); i++) {      JndiTreeNode curr = (JndiTreeNode) jndiRoot.getChildAt(i);      if (name.equals(curr.getName()))        return curr;    }    return null;  }  public void connectAdmin(final String host,                            final int port,                            final String user,                            final String passwd)     throws Exception {    try {      disconnectAdmin();    }    catch (Exception exc) {}    AdminModule.connect(host, port, user, passwd, 4);    adminConnected = true;    adminConnectionStr = "//" + host + ":" + port;    adminRoot.setUserObject("Active Config");    adminTreeModel.nodeChanged(adminRoot);    AdminTool.invokeLater(new CommandWorker() {        public void run() throws Exception {          refreshAdminData();          gui.adminControllerEvent(            new ControllerEvent(ControllerEvent.ADMIN_CONNECTED));        }      });  }  /**   * First refreshing step. Doesn't block.   */  private void refreshAdminData1(ServerTreeNode serverTreeNode)     throws ConnectException, AdminException {    if (Log.logger.isLoggable(BasicLevel.DEBUG))      Log.logger.log(BasicLevel.DEBUG,                      "AdminController.refreshAdminData(" +                      serverTreeNode + ')');    String[] domainNames = AdminModule.getDomainNames(      serverTreeNode.getServerId());    TreeNode parentTreeNode = serverTreeNode.getParent();    String parentDomainName = null;    if (parentTreeNode instanceof DomainTreeNode) {      DomainTreeNode dtn = (DomainTreeNode)parentTreeNode;      parentDomainName = dtn.getDomainName();    }    for (int i = 0; i < domainNames.length; i++) {      if (! domainNames[i].equals(parentDomainName)) {        DomainTreeNode dtn =           new DomainTreeNode(this, domainNames[i]);        adminTreeModel.insertNodeInto(          dtn,           serverTreeNode.getDomainRoot(),          serverTreeNode.getDomainRoot().getChildCount());                Server[] servers = AdminModule.getServers(domainNames[i]);        for (int j = 0; j < servers.length; j++) {          if (servers[j].getId() !=               serverTreeNode.getServerId()) {            ServerTreeNode stn =               new ServerTreeNode(this, servers[j]);            adminTreeModel.insertNodeInto(              stn, dtn,               dtn.getChildCount());            refreshAdminData1(stn);          }        }      }    }  }  void updateDestinations(int serverId,                           MutableTreeNode destinationRoot)     throws ConnectException, AdminException {    List destList = AdminModule.getDestinations(serverId);    for (Iterator i = destList.iterator(); i.hasNext();) {      Destination dest = (Destination) i.next();      DestinationTreeNode destNode;      if (dest instanceof Topic) {        destNode = new TopicTreeNode(this, (Topic)dest);      } else if (dest instanceof Queue) {        destNode = new QueueTreeNode(this, (Queue)dest);      } else if (dest instanceof TemporaryQueue) {        destNode = new TopicTreeNode(this, (Topic)dest);      } else if (dest instanceof TemporaryTopic) {        destNode = new QueueTreeNode(this, (Queue)dest);      } else {        destNode = new DestinationTreeNode(this, dest);      }      adminTreeModel.insertNodeInto(        destNode,         destinationRoot,        destinationRoot.getChildCount());    }  }  void updateUsers(int serverId,                    MutableTreeNode userRoot)     throws ConnectException, AdminException {    List userList = AdminModule.getUsers(serverId);    for (Iterator i = userList.iterator(); i.hasNext();) {      User user = (User) i.next();      UserTreeNode userNode = new UserTreeNode(this, user);      adminTreeModel.insertNodeInto(        userNode,         userRoot,        userRoot.getChildCount());    }  }  /**   * Second refreshing step. May block.   */  private void refreshAdminData2(ServerTreeNode serverTreeNode)     throws ConnectException, AdminException {    if (Log.logger.isLoggable(BasicLevel.DEBUG))      Log.logger.log(BasicLevel.DEBUG,                      "AdminController.refreshAdminData(" +                      serverTreeNode + ')');    List destList;    List userList;    try {      updateDestinations(        serverTreeNode.getServerId(),        serverTreeNode.getDestinationRoot());      updateUsers(        serverTreeNode.getServerId(),        serverTreeNode.getUserRoot());    } catch (AdminException exc) {      if (Log.logger.isLoggable(BasicLevel.WARN))        Log.logger.log(BasicLevel.WARN, "", exc);      return;    } catch (ConnectException ce) {      if (Log.logger.isLoggable(BasicLevel.WARN))        Log.logger.log(BasicLevel.WARN, "", ce);      return;    }    Enumeration e = serverTreeNode.getDomainRoot().children();    while (e.hasMoreElements()) {      DomainTreeNode dtn = (DomainTreeNode)e.nextElement();      Enumeration e2 = dtn.children();      while (e2.hasMoreElements()) {        ServerTreeNode stn = (ServerTreeNode)e2.nextElement();        refreshAdminData2(stn);      }    }  }  public void refreshAdminData()     throws ConnectException, AdminException {    if (Log.logger.isLoggable(BasicLevel.DEBUG))      Log.logger.log(BasicLevel.DEBUG,                      "AdminController.refreshAdminData()");    cleanupAdminTree();    // Get the local server id    Server localServer = AdminModule.getLocalServer();    ServerTreeNode localServerNode =       new ServerTreeNode(        this,         localServer);    adminTreeModel.insertNodeInto(      localServerNode, adminRoot,       adminRoot.getChildCount());        // Recursively browse the servers configuration    refreshAdminData1(localServerNode);    refreshAdminData2(localServerNode);  }  public void disconnectAdmin() throws Exception {    if (adminConnected) {      AdminModule.disconnect();    }    adminRoot.setUserObject(STR_ADMIN_DISCONNECTED);    adminTreeModel.nodeChanged(adminRoot);    cleanupAdminTree();    adminConnected = false;    adminConnectionStr = "Not connected";  	gui.adminControllerEvent(new ControllerEvent(ControllerEvent.ADMIN_DISCONNECTED));  }    public void stopServer(ServerTreeNode stn) throws Exception {    AdminModule.stopServer(stn.getServerId());  }  public void deleteServer(ServerTreeNode stn) throws Exception {    AdminModule.removeServer(stn.getServerId());    adminTreeModel.removeNodeFromParent(stn);  }  public void deleteDomain(DomainTreeNode dtn) throws Exception {    AdminModule.removeDomain(dtn.getDomainName());    adminTreeModel.removeNodeFromParent(dtn);  }  public void createConnectionFactory(String host, int port,    String name, String type) throws Exception  {    try    {      if (ctx.lookup(name) != null)        throw new Exception("Name already bound in JNDI context");    }    catch (NameNotFoundException exc) {}  	Object factory = null;  	if ("CF".equals(type))  	  factory = TcpConnectionFactory.create(host, port);  	if ("QCF".equals(type))  	  factory = QueueTcpConnectionFactory.create(host, port);  	if ("TCF".equals(type))  	  factory = TopicTcpConnectionFactory.create(host, port);  	if ("XCF".equals(type))  	  factory = XATcpConnectionFactory.create(host, port);  	if ("XQCF".equals(type))  	  factory = XAQueueTcpConnectionFactory.create(host, port);  	if ("XTCF".equals(type))  	  factory = XATopicTcpConnectionFactory.create(host, port);    ctx.bind(name, factory);

⌨️ 快捷键说明

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