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