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    package org.crsh.ssh.term;
020    
021    import org.apache.sshd.server.Environment;
022    
023    import java.io.IOException;
024    import java.security.Principal;
025    
026    /**
027     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
028     * @version $Revision$
029     */
030    public class CRaSHCommand extends AbstractCommand implements Runnable {
031    
032      /** . */
033      private final CRaSHCommandFactory factory;
034    
035      /** . */
036      private Thread thread;
037    
038      public CRaSHCommand(CRaSHCommandFactory factory) {
039        this.factory = factory;
040      }
041    
042      /** . */
043      private SSHContext context;
044    
045      /** . */
046      private SSHIO io;
047    
048      public void start(Environment env) throws IOException {
049    
050        //
051        context = new SSHContext(env);
052        io = new SSHIO(this);
053    
054        //
055        thread = new Thread(this, "CRaSH");
056        thread.start();
057      }
058    
059      public SSHContext getContext() {
060        return context;
061      }
062    
063      public void destroy() {
064        io.closed.set(true);
065        thread.interrupt();
066      }
067    
068      public void run() {
069        try {
070          final String userName = session.getAttribute(SSHLifeCycle.USERNAME);
071          Principal user = new Principal() {
072            public String getName() {
073              return userName;
074            }
075          };
076          factory.handler.handle(io, user);
077        } finally {
078          callback.onExit(0);
079        }
080      }
081    }
082    
083