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