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