001 /* 002 * Copyright (C) 2003-2009 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.term.TermLifeCycle; 030 import org.slf4j.Logger; 031 import org.slf4j.LoggerFactory; 032 033 import java.net.URL; 034 import java.util.ArrayList; 035 import java.util.List; 036 import java.util.Properties; 037 import java.util.concurrent.ConcurrentHashMap; 038 039 /** 040 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a> 041 * @version $Revision$ 042 */ 043 public class TelnetLifeCycle extends TermLifeCycle { 044 045 /** . */ 046 private final Logger log = LoggerFactory.getLogger(TelnetLifeCycle.class); 047 048 /** . */ 049 private Integer port; 050 051 /** . */ 052 private List<PortListener> listeners; 053 054 /** . */ 055 private static final ConcurrentHashMap<ConnectionManager, TelnetLifeCycle> map = new ConcurrentHashMap<ConnectionManager, TelnetLifeCycle>(); 056 057 /** . */ 058 private URL configURL; 059 060 static TelnetLifeCycle getLifeCycle(Connection conn) { 061 return map.get(conn.getConnectionData().getManager()); 062 } 063 064 public TelnetLifeCycle(PluginContext context) { 065 super(context); 066 } 067 068 public Integer getPort() { 069 return port; 070 } 071 072 public void setPort(Integer port) { 073 this.port = port; 074 } 075 076 public URL getConfigURL() { 077 return configURL; 078 } 079 080 public void setConfigURL(URL configURL) { 081 this.configURL = configURL; 082 } 083 084 @Override 085 protected synchronized void doInit() throws Exception { 086 Properties props = new Properties(); 087 props.load(configURL.openStream()); 088 089 // 090 if (port != null) { 091 log.debug("Explicit telnet port configuration with value " + port); 092 props.put("std.port", port.toString()); 093 } else { 094 log.debug("Use default telnet port configuration " + props.getProperty("std.port")); 095 } 096 097 // 098 ShellManager.createShellManager(props); 099 100 // 101 TerminalManager.createTerminalManager(props); 102 103 // 104 ArrayList<PortListener> listeners = new ArrayList<PortListener>(); 105 String[] listnames = StringUtil.split(props.getProperty("listeners"), ","); 106 for (String listname : listnames) { 107 PortListener listener = PortListener.createPortListener(listname, props); 108 listeners.add(listener); 109 } 110 111 // 112 this.listeners = listeners; 113 114 // Start listeners 115 for (PortListener listener : this.listeners) { 116 listener.start(); 117 map.put(listener.getConnectionManager(), this); 118 } 119 } 120 121 @Override 122 protected synchronized void doDestroy() { 123 log.info("Destroying telnet life cycle"); 124 if (listeners != null) { 125 List<PortListener> listeners = this.listeners; 126 this.listeners = null; 127 for (PortListener listener : listeners) { 128 try { 129 listener.stop(); 130 } catch (Exception ignore) { 131 } finally { 132 map.remove(listener.getConnectionManager()); 133 } 134 } 135 } 136 } 137 }