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

📄 telnetgateway.java

📁 java开源的企业总线.xmlBlaster
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                cmdType.equalsIgnoreCase("CONNECT")) {               lastCommand = cmd;               return getErrorText("Ignoring your empty command '" + cmd + "'");            }            if (cmdType.equalsIgnoreCase("echo")) {               lastCommand = cmd;               return null;            }         }         String query = cmd.substring(cmdType.length()).trim();         if (cmd.equalsIgnoreCase("maxMemStr")) {            lastCommand = cmd;            long usedMem = (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory());            String freeMem = Global.byteString(Global.heapMemoryUsage-usedMem);                        String ret;            if (Global.totalPhysicalMemorySize > 0) // JDK >= 1.5               ret = "Physical RAM size is " + Global.byteString(Global.totalPhysicalMemorySize) + "," +                  " this JVM has currently " + freeMem + " free available and may use max " + Global.byteString(Global.heapMemoryUsage) +                  " and max " + Global.maxFileDescriptorCount + " file descriptors";            else               ret = "This JVM has currently " + freeMem + " free available and may use max " + Global.byteString(Global.heapMemoryUsage);            return ret + "\r\n";         }         if (cmdType.equalsIgnoreCase("CONNECT")) {            if (!st.hasMoreTokens()) {               lastCommand = cmd;               return getErrorText("Please give me a login name and password to connect: '" + cmd + " <name> <passwd>'");            }            String loginName = st.nextToken();            if (!st.hasMoreTokens()) {               lastCommand = cmd;               return getErrorText("Please give me a password to connect: '" + cmd + " <passwd>'");            }            String passwd = st.nextToken();            connect(loginName, passwd); // throws Exception or sets isLogin=true              log.info("Successful login for telnet client '" + loginName + "', session timeout is " +                     Timestamp.millisToNice(sessionTimeout));            lastCommand = cmd;            return "Successful login for user " + loginName + ", session timeout is " +                     Timestamp.millisToNice(sessionTimeout) + CRLF;         }         if (!isLogin) {            lastCommand = cmd;            return getErrorText("Please login first with 'connect <loginName> <password>'");         }         // Commands with login only:         if (cmd.equalsIgnoreCase("gc")) {            lastCommand = cmd;            System.gc();            return "OK\r\n";         }         if (cmd.toUpperCase().startsWith("EXIT")) {            lastCommand = cmd;            return              "\r\nYou are going to shutdown remote JVM!\r\n"+              "Are you sure to do this and stop xmlBlaster? (yes/no): ";         }         if (cmd.equalsIgnoreCase("yes")) {            if (lastCommand.trim().startsWith("exit")) {               System.exit(0);            }         }         if (cmd.equalsIgnoreCase("no")) {            if (lastCommand.trim().toUpperCase().startsWith("EXIT")) {               lastCommand = "";               return CRLF;            }         }         lastCommand = "";         if (log.isLoggable(Level.FINE)) log.fine("Invoking cmdType=" + cmdType + " query=" + query + " from '" + cmd + "'");         if (cmdType.equalsIgnoreCase("GET")) {            QueryKeyData keyData = new QueryKeyData(this.glob);            keyData.setOid("__cmd:" + query);            MsgUnit[] msgs = commandManager.get(this.addressServer, sessionId, keyData, null);            if (msgs.length == 0) return "NO ENTRY FOUND: " + cmd + CRLF;            StringBuffer sb = new StringBuffer(msgs.length * 40);            for (int ii=0; ii<msgs.length; ii++) {               MsgUnit msg = msgs[ii];               if ("text/plain".equalsIgnoreCase(msg.getKeyData().getContentMime()))                  sb./*append(msg.getKey()).append("=").*/append(msg.getContentStr()).append(CRLF);               else                  sb.append(msg.toXml());            }            return sb.toString() + CRLF;         }         else if (cmdType.equalsIgnoreCase("SET")) {            SetReturn ret = commandManager.set(this.addressServer, sessionId, query);            if (ret == null) return "NO ENTRY SET: " + ret.commandWrapper.getCommand() + CRLF;            return ((ret.commandWrapper==null) ? "" : ret.commandWrapper.getCommandStripAssign() + "=") + ret.returnString + CRLF;         }         else {            return null;            //return getErrorText("Ignoring unknown command '" + cmdType + "' of '" + cmd + "'" + CRLF);         }      }      catch (XmlBlasterException e) {         if (log.isLoggable(Level.FINE)) log.fine(e.toString());         return CRLF + e.toString() + CRLF + CRLF;      }   }   private final String getErrorText(String error) {      String text = "ERROR-XmlBlaster telnet server: " + error + CRLF;      if (isLogin) {         text += "Try a 'get sysprop/?user.home' or 'set logging/?org.xmlBlaster=FINE' or just 'help'" + CRLF + CRLF;      }      else {         text += "Try 'help'" + CRLF + CRLF;      }      log.info(error);      return text;   }   /**    * Enforced by "remotecons.CommandHandlerIfc"    */   public String help() {      return CRLF +             "  XmlBlaster telnet administration" + CRLF +             "   connect [name] [passwd]  Login with your login name and password" + CRLF +             "   get [query]              Get property or xmlBlaster state" + CRLF +             "   set [query]              Set a property or change xmlBlaster setting" + CRLF +             "   time                     Display current time on server" + CRLF +             "   gc                       Run System.gc() command on remote system" + CRLF +             "   mem [total|free]         Display amount of memory on remote system" + CRLF +             "   maxMemStr                Display amount of memory on remote system" + CRLF +             "   exit                     Call System.exit(0) on remote system" + CRLF +             "  For query syntax see" + CRLF +             "  http://www.xmlblaster.org/xmlBlaster/doc/requirements/admin.telnet.html" + CRLF +             "  http://www.xmlblaster.org/xmlBlaster/doc/requirements/admin.commands.html" + CRLF + CRLF;   }   /**    * Enforced by "remotecons.CommandHandlerIfc"    */   public String help(String cmd) {      return "";   }   public CommandHandlerIfc getInstance() {      //log.error(ME, "DEBUG ONLY: Entering getInstance(isShutdown="+isShutdown+", port="+this.port+")");      if (isShutdown) return this; // Called on shutdown, we need to investigate and redesign the whole baby      if (this.port <= 1000) {         return null;      }      //!!!! register to CommandManager as it needs to destroy the timer?? what in cluster env?      TelnetGateway telnetGateway = new TelnetGateway();      telnetGateway.initializeVariables(glob, commandManager, false);      this.telnetInstancesSet.add(telnetGateway);       return telnetGateway;   }   public String getName() {      return "TelnetGateway";   }   /**    * Login to xmlBlaster server.     */   public void connect(String loginName, String passwd) throws XmlBlasterException {      if (log.isLoggable(Level.FINER)) log.finer("Entering login(loginName=" + loginName/* + ", qos=" + qos_literal */ + ")");      if (loginName==null || passwd==null) {         log.severe("login failed: please use no null arguments for login()");         throw new XmlBlasterException(this.glob, ErrorCode.USER_ILLEGALARGUMENT, ME + ".connect", "login failed: please use 'connect loginName password'");      }      org.xmlBlaster.client.qos.ConnectQos clientConnectQos = new org.xmlBlaster.client.qos.ConnectQos(glob, loginName, passwd);      clientConnectQos.setSessionTimeout(sessionTimeout);      ConnectQosServer connectQos = new ConnectQosServer(glob, clientConnectQos.getData());      this.addressServer = new AddressServer(glob, "NATIVE", glob.getId(), (java.util.Properties)null);      connectQos.setAddressServer(this.addressServer);      this.connectRetQos = glob.getAuthenticate().connect(this.addressServer, connectQos);      this.loginName = loginName;      this.sessionId = connectRetQos.getSecretSessionId();      isLogin = true;      if (connectQos.getSessionTimeout() > 0L) {         stopTimer();         if (log.isLoggable(Level.FINE)) log.fine("Setting expiry timer for " + loginName + " to " + connectQos.getSessionTimeout() + " msec");         timerKey = this.glob.getTelnetSessionTimer().addTimeoutListener(this, connectQos.getSessionTimeout(), null);      }      else         log.info("Session for " + loginName + " lasts forever, requested expiry timer was 0");   }   public void shutdown() {      //Thread.currentThread().dumpStack();      if (log.isLoggable(Level.FINER)) log.finer("Invoking shutdown()");      isLogin = false;      if (this.glob.hasTelnetSessionTimer()) {         stopTimer();         this.glob.removeTelnetSessionTimer();      }      disconnect();      if (this.telnetInstancesSet != null) {         Iterator it = telnetInstancesSet.iterator();         while(it.hasNext()) {            TelnetGateway gw = (TelnetGateway)it.next();            gw.shutdown();         }         this.telnetInstancesSet.clear();         //telnetInstancesSet = null;      }      if (rs != null) {         rs.disable();         rs = null;         if (log.isLoggable(Level.FINE)) log.fine("Shutdown done, telnet disabled.");      }      isShutdown = true;   }   /**    * Dump state of this object into a XML ASCII string.    */   public final String toXml() {      return toXml((String)null);   }   /**    * Dump state of this object into a XML ASCII string.    * @param extraOffset indenting of tags for nice output    */   public synchronized final String toXml(String extraOffset) {      StringBuffer sb = new StringBuffer(1024);      String offset = "\n ";      if (extraOffset == null) extraOffset = "";      offset += extraOffset;      sb.append(offset).append("<telnetGateway");      sb.append(" port='").append(this.port).append("'");      sb.append(" loginName='").append(this.loginName).append("'");      sb.append(" numInstances='").append((this.telnetInstancesSet != null)?this.telnetInstancesSet.size():0).append("'");      sb.append(">");      if (this.glob.hasTelnetSessionTimer()) {         sb.append(offset).append(" <hasTimer/>");      }      sb.append(offset).append("</telnetGateway>");      return sb.toString();   }}

⌨️ 快捷键说明

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