001 /* 002 * Copyright (C) 2012 eXo Platform SAS. 003 * 004 * This is free software; you can redistribute it and/or modify it 005 * under the terms of the GNU Lesser General Public License as 006 * published by the Free Software Foundation; either version 2.1 of 007 * the License, or (at your option) any later version. 008 * 009 * This software is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * You should have received a copy of the GNU Lesser General Public 015 * License along with this software; if not, write to the Free 016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 018 */ 019 020 package org.crsh.telnet.term; 021 022 import net.wimpi.telnetd.io.terminal.TerminalManager; 023 import net.wimpi.telnetd.net.Connection; 024 import net.wimpi.telnetd.net.ConnectionManager; 025 import net.wimpi.telnetd.net.PortListener; 026 import net.wimpi.telnetd.shell.ShellManager; 027 import net.wimpi.telnetd.util.StringUtil; 028 import org.crsh.plugin.PluginContext; 029 import org.crsh.vfs.Resource; 030 031 import java.io.ByteArrayInputStream; 032 import java.util.ArrayList; 033 import java.util.List; 034 import java.util.Properties; 035 import java.util.concurrent.ConcurrentHashMap; 036 import java.util.logging.Level; 037 import java.util.logging.Logger; 038 039 public class TelnetLifeCycle extends TermLifeCycle { 040 041 /** . */ 042 private final Logger log = Logger.getLogger(TelnetLifeCycle.class.getName()); 043 044 /** . */ 045 private Integer port; 046 047 /** . */ 048 private List<PortListener> listeners; 049 050 /** . */ 051 private static final ConcurrentHashMap<ConnectionManager, TelnetLifeCycle> map = new ConcurrentHashMap<ConnectionManager, TelnetLifeCycle>(); 052 053 /** . */ 054 private Resource config; 055 056 static TelnetLifeCycle getLifeCycle(Connection conn) { 057 return map.get(conn.getConnectionData().getManager()); 058 } 059 060 public TelnetLifeCycle(PluginContext context) { 061 super(context); 062 } 063 064 public Integer getPort() { 065 return port; 066 } 067 068 public void setPort(Integer port) { 069 this.port = port; 070 } 071 072 public Resource getConfig() { 073 return config; 074 } 075 076 public void setConfig(Resource config) { 077 this.config = config; 078 } 079 080 @Override 081 protected synchronized void doInit() throws Exception { 082 Properties props = new Properties(); 083 props.load(new ByteArrayInputStream(config.getContent())); 084 085 // 086 if (port != null) { 087 log.log(Level.FINE, "Explicit telnet port configuration with value " + port); 088 props.put("std.port", port.toString()); 089 } else { 090 log.log(Level.FINE, "Use default telnet port configuration " + props.getProperty("std.port")); 091 } 092 093 // 094 ShellManager.createShellManager(props); 095 096 // 097 TerminalManager.createTerminalManager(props); 098 099 // 100 ArrayList<PortListener> listeners = new ArrayList<PortListener>(); 101 String[] listnames = StringUtil.split(props.getProperty("listeners"), ","); 102 for (String listname : listnames) { 103 PortListener listener = PortListener.createPortListener(listname, props); 104 listeners.add(listener); 105 } 106 107 // 108 this.listeners = listeners; 109 110 // Start listeners 111 for (PortListener listener : this.listeners) { 112 listener.start(); 113 map.put(listener.getConnectionManager(), this); 114 } 115 } 116 117 @Override 118 protected synchronized void doDestroy() { 119 log.log(Level.INFO, "Destroying telnet life cycle"); 120 if (listeners != null) { 121 List<PortListener> listeners = this.listeners; 122 this.listeners = null; 123 for (PortListener listener : listeners) { 124 try { 125 listener.stop(); 126 } catch (Exception ignore) { 127 } finally { 128 map.remove(listener.getConnectionManager()); 129 } 130 } 131 } 132 } 133 }