當前位置: 首頁>>代碼示例>>Java>>正文


Java Channel.isConnected方法代碼示例

本文整理匯總了Java中com.jcraft.jsch.Channel.isConnected方法的典型用法代碼示例。如果您正苦於以下問題:Java Channel.isConnected方法的具體用法?Java Channel.isConnected怎麽用?Java Channel.isConnected使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.jcraft.jsch.Channel的用法示例。


在下文中一共展示了Channel.isConnected方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: rename

import com.jcraft.jsch.Channel; //導入方法依賴的package包/類
@Override
public boolean rename(String newName){
    Channel channel = null;
    try {
         channel = SFTPSession.getInstance().getSFTPChannel(mUri);
        ((ChannelSftp)channel).rename(mUri.getPath(), new File(new File(mUri.getPath()).getParentFile(), newName).getAbsolutePath());
        channel.disconnect();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        if(channel!=null&&channel.isConnected())
            channel.disconnect();
    }
    return false;
}
 
開發者ID:archos-sa,項目名稱:aos-FileCoreLibrary,代碼行數:17,代碼來源:SftpFileEditor.java

示例2: move

import com.jcraft.jsch.Channel; //導入方法依賴的package包/類
@Override
public boolean move(Uri uri) {
    if(!mUri.getScheme().equals(uri.getScheme())|| !mUri.getHost().equals(uri.getHost())||mUri.getPort()!=uri.getPort())
        return false;
    Channel channel = null;
    try {
        channel = SFTPSession.getInstance().getSFTPChannel(mUri);
        ((ChannelSftp)channel).rename(mUri.getPath(),uri.getPath());
        channel.disconnect();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        if(channel!=null&&channel.isConnected())
            channel.disconnect();
    }
    return false;
}
 
開發者ID:archos-sa,項目名稱:aos-FileCoreLibrary,代碼行數:19,代碼來源:SftpFileEditor.java

示例3: exists

import com.jcraft.jsch.Channel; //導入方法依賴的package包/類
@Override
public boolean exists() {
    Channel channel = null;
    try {
        channel = SFTPSession.getInstance().getSFTPChannel(mUri);
        SftpATTRS attrs =  ((ChannelSftp)channel).stat(mUri.getPath());
        channel.disconnect();
        return attrs !=null;
    } catch (Exception e) {
        if(channel!=null&&channel.isConnected())
            channel.disconnect();
        e.printStackTrace();
    }finally {
        if(channel!=null&&channel.isConnected())
            channel.disconnect();
    }
    return false;
}
 
開發者ID:archos-sa,項目名稱:aos-FileCoreLibrary,代碼行數:19,代碼來源:SftpFileEditor.java

示例4: getChannelShell

import com.jcraft.jsch.Channel; //導入方法依賴的package包/類
/**
 * Get a {@link ChannelShell}
 *
 * @param channelSessionKey the session key that identifies the channel
 * @return {@link ChannelShell}
 * @throws Exception thrown by borrowObject and invalidateObject
 */
private ChannelShell getChannelShell(final ChannelSessionKey channelSessionKey) throws Exception {
    final long startTime = System.currentTimeMillis();
    Channel channel;
    do {
        LOGGER.debug("borrowing a channel...");
        channel = channelPool.borrowObject(channelSessionKey);
        if (channel != null) {
            LOGGER.debug("channel {} borrowed", channel.getId());
            if (!channel.isConnected()) {
                try {
                    LOGGER.debug("channel {} connecting...", channel.getId());
                    channel.connect(CHANNEL_CONNECT_TIMEOUT);
                    LOGGER.debug("channel {} connected!", channel.getId());
                } catch (final JSchException jsche) {
                    LOGGER.error("Borrowed channel {} connection failed! Invalidating the channel...",
                            channel.getId(), jsche);
                    channelPool.invalidateObject(channelSessionKey, channel);
                }
            } else {
                LOGGER.debug("Channel {} already connected!", channel.getId());
            }
        }

        if ((channel == null || !channel.isConnected()) && (System.currentTimeMillis() - startTime) > CHANNEL_BORROW_LOOP_WAIT_TIME) {
            final String errMsg = MessageFormat.format("Failed to get a channel within {0} ms! Aborting channel acquisition!",
                    CHANNEL_BORROW_LOOP_WAIT_TIME);
            LOGGER.error(errMsg);
            throw new JschServiceException(errMsg);
        }
    } while (channel == null || !channel.isConnected());
    return (ChannelShell) channel;
}
 
開發者ID:cerner,項目名稱:jwala,代碼行數:40,代碼來源:JschServiceImpl.java

示例5: executeCommandNoResponse

import com.jcraft.jsch.Channel; //導入方法依賴的package包/類
public static void executeCommandNoResponse( Session sessionObj, String command )
    throws JSchException, IOException
{
    logger.debug( "Starting to execute command [" + maskedPasswordString( command ) + "]" );

    if ( sessionObj != null && command != null && !"".equals( command ) )
    {
        Channel channel = null;

        try
        {
            channel = sessionObj.openChannel( "exec" );
            ( (ChannelExec) channel ).setCommand( command );
            channel.setInputStream( null );
            ( (ChannelExec) channel ).setErrStream( System.err );
            channel.getInputStream();
            channel.connect();
            /* We do not care about whether the command succeeds or not */

            if ( channel.isClosed() && channel.getExitStatus() != 0 )
            {
                logger.debug( "Command exited with error code " + channel.getExitStatus() );
            }
        }
        catch ( Exception e )
        {
            logger.error( "Received exception during command execution", e );
        }
        finally
        {
            if ( channel != null && channel.isConnected() )
            {
                channel.disconnect();
            }
            logger.debug( "End of execution of command [" + maskedPasswordString( command ) + "]" );
        }
    }
}
 
開發者ID:vmware,項目名稱:OHMS,代碼行數:39,代碼來源:SshUtil.java

示例6: executeCommandNoResponse

import com.jcraft.jsch.Channel; //導入方法依賴的package包/類
public static void executeCommandNoResponse( Session sessionObj, String command )
    throws JSchException, IOException
{
    logger.debug( "Starting to execute command [" + command + "]" );

    if ( sessionObj != null && command != null && !"".equals( command ) )
    {
        Channel channel = null;

        try
        {
            channel = sessionObj.openChannel( "exec" );
            ( (ChannelExec) channel ).setCommand( command );
            channel.setInputStream( null );
            ( (ChannelExec) channel ).setErrStream( System.err );
            channel.getInputStream();
            channel.connect();
            /* We do not care about whether the command succeeds or not */

            if ( channel.isClosed() && channel.getExitStatus() != 0 )
            {
                logger.debug( "Command exited with error code " + channel.getExitStatus() );
            }
        }
        catch ( Exception e )
        {
            logger.error( "Received exception during command execution", e );
        }
        finally
        {
            if ( channel != null && channel.isConnected() )
            {
                channel.disconnect();
            }
            logger.debug( "End of execution of command [" + command + "]" );
        }
    }
}
 
開發者ID:vmware,項目名稱:OHMS,代碼行數:39,代碼來源:EsxiSshUtil.java

示例7: executeCommand

import com.jcraft.jsch.Channel; //導入方法依賴的package包/類
/**
 * It executes the command and returns the response back to the calling function. This function will expect Session
 * object and command string as parameters .
 *
 * @param sessionObj
 * @param command
 * @return
 * @throws JSchException
 * @throws IOException
 */
public static String executeCommand( Session sessionObj, String command )
    throws JSchException, IOException
{
    StringBuilder builder = null;
    logger.debug( "Starting to execute command [" + maskedPasswordString( command ) + "]" );

    if ( sessionObj != null && command != null && !"".equals( command ) )
    {
        builder = new StringBuilder();
        Channel channel = null;

        int arrMaxSize = 1024;

        try
        {
            channel = sessionObj.openChannel( "exec" );
            ( (ChannelExec) channel ).setCommand( command );
            channel.setInputStream( null );
            ( (ChannelExec) channel ).setErrStream( System.err );
            InputStream in = channel.getInputStream();
            channel.connect();

            byte[] tmp = new byte[arrMaxSize];

            while ( true )
            {
                while ( in.available() > 0 )
                {
                    int i = in.read( tmp, 0, arrMaxSize );
                    if ( i < 0 )
                        break;
                    builder.append( new String( tmp, 0, i ) );
                }

                if ( channel.isClosed() )
                {
                    break;
                }
                try
                {
                    Thread.sleep( 500 );
                }
                catch ( Exception ee )
                {

                }
            }

            if ( channel.isClosed() && channel.getExitStatus() != 0 )
            {
                logger.debug( "Command exited with error code " + channel.getExitStatus() );
            }
        }
        catch ( Exception e )
        {
            logger.error( "Received exception during command execution", e );
        }
        finally
        {
            if ( channel != null && channel.isConnected() )
            {
                channel.disconnect();
            }
            logger.debug( "End of execution of command [" + maskedPasswordString( command ) + "]" );
        }
        return builder.toString();
    }
    return null;
}
 
開發者ID:vmware,項目名稱:OHMS,代碼行數:80,代碼來源:SshUtil.java

示例8: executeCommand

import com.jcraft.jsch.Channel; //導入方法依賴的package包/類
/**
 * It executes the command and returns the response back to the calling function. This function will expect Session
 * object and command string as parameters .
 * 
 * @param sessionObj
 * @param command
 * @return
 * @throws JSchException
 * @throws IOException
 */
public static String executeCommand( Session sessionObj, String command )
    throws JSchException, IOException
{
    StringBuilder builder = null;
    logger.debug( "Starting to execute command [" + command + "]" );

    if ( sessionObj != null && command != null && !"".equals( command ) )
    {
        builder = new StringBuilder();
        Channel channel = null;

        int arrMaxSize = 1024;

        try
        {
            channel = sessionObj.openChannel( "exec" );
            ( (ChannelExec) channel ).setCommand( command );
            channel.setInputStream( null );
            ( (ChannelExec) channel ).setErrStream( System.err );
            InputStream in = channel.getInputStream();
            channel.connect();

            byte[] tmp = new byte[arrMaxSize];

            while ( true )
            {
                while ( in.available() > 0 )
                {
                    int i = in.read( tmp, 0, arrMaxSize );
                    if ( i < 0 )
                        break;
                    builder.append( new String( tmp, 0, i ) );
                }

                if ( channel.isClosed() )
                {
                    break;
                }
                try
                {
                    Thread.sleep( 500 );
                }
                catch ( Exception ee )
                {

                }
            }

            if ( channel.isClosed() && channel.getExitStatus() != 0 )
            {
                logger.debug( "Command exited with error code " + channel.getExitStatus() );
            }
        }
        catch ( Exception e )
        {
            logger.error( "Received exception during command execution", e );
        }
        finally
        {
            if ( channel != null && channel.isConnected() )
            {
                channel.disconnect();
            }
            logger.debug( "End of execution of command [" + command + "]" );
        }
        return builder.toString();
    }
    return null;
}
 
開發者ID:vmware,項目名稱:OHMS,代碼行數:80,代碼來源:SshUtil.java

示例9: executeCommand

import com.jcraft.jsch.Channel; //導入方法依賴的package包/類
/**
 * It executes the command and returns the response back to the calling function. This function will expect Session
 * object and command string as parameters .
 *
 * @param sessionObj
 * @param command
 * @return
 * @throws JSchException
 * @throws IOException
 */
public static String executeCommand( Session sessionObj, String command )
    throws JSchException, IOException
{
    StringBuilder builder = null;
    logger.debug( "Starting to execute command [" + command + "]" );

    if ( sessionObj != null && command != null && !"".equals( command ) )
    {
        builder = new StringBuilder();
        Channel channel = null;

        int arrMaxSize = 1024;

        try
        {
            channel = sessionObj.openChannel( "exec" );
            ( (ChannelExec) channel ).setCommand( command );
            channel.setInputStream( null );
            ( (ChannelExec) channel ).setErrStream( System.err );
            InputStream in = channel.getInputStream();
            channel.connect();

            byte[] tmp = new byte[arrMaxSize];

            while ( true )
            {
                while ( in.available() > 0 )
                {
                    int i = in.read( tmp, 0, arrMaxSize );
                    if ( i < 0 )
                        break;
                    builder.append( new String( tmp, 0, i ) );
                }

                if ( channel.isClosed() )
                {
                    break;
                }
                try
                {
                    Thread.sleep( 500 );
                }
                catch ( Exception ee )
                {

                }
            }

            if ( channel.isClosed() && channel.getExitStatus() != 0 )
            {
                logger.debug( "Command exited with error code " + channel.getExitStatus() );
            }
        }
        catch ( Exception e )
        {
            logger.error( "Received exception during command execution", e );
        }
        finally
        {
            if ( channel != null && channel.isConnected() )
            {
                channel.disconnect();
            }
            logger.debug( "End of execution of command [" + command + "]" );
        }
        return builder.toString();
    }
    return null;
}
 
開發者ID:vmware,項目名稱:OHMS,代碼行數:80,代碼來源:EsxiSshUtil.java

示例10: createJschChannel

import com.jcraft.jsch.Channel; //導入方法依賴的package包/類
private Connection createJschChannel(Session session, InputStream jsch_in,
		OutputStream out) throws JSchException, IOException {
	final InputStream in;
	Channel channel;
	// open shell channel with vt100 term
	channel = session.openChannel("shell");
	((ChannelShell) channel).setPtyType("vt100");
	((ChannelShell) channel).setPty(true);
	channel.setInputStream(jsch_in);
	in = channel.getInputStream();

	channel.connect(10000);

	// sleep 500s in case we don't have open channel permission and server
	// close the channel.
	try {
		Thread.sleep(500);
	} catch (InterruptedException e) {
	}
	if (!channel.isConnected()) {
		throw new NemoException(
				"channel has been closed by peer. maybe you don't have permission");
	}

	final Session _session = session;
	final InputStream _in = this.createVT100Filter(in);
	final OutputStream _out = out;
	return new Connection() {
		boolean _active = true;

		@Override
		public void close() {
			_active = false;
			_session.disconnect();
		}

		@Override
		public OutputStream getOutputStream() {
			return _out;
		}

		@Override
		public InputStream getInputStream() {
			return _in;
		}

		@Override
		public boolean isActive() {
			return _active && _session.isConnected();
		}

	};

}
 
開發者ID:kira8565,項目名稱:JavaExpect,代碼行數:55,代碼來源:SshDriver.java

示例11: sendFile

import com.jcraft.jsch.Channel; //導入方法依賴的package包/類
private void sendFile(Channel channel, String localFile, String remoteName, String mode)
        throws IOException, RemoteScpException {
    byte[] buffer = new byte[BUFFER_SIZE];

    OutputStream os = new BufferedOutputStream(channel.getOutputStream(),
            SEND_FILE_BUFFER_LENGTH);
    InputStream is = new BufferedInputStream(channel.getInputStream(), SEND_BYTES_BUFFER_LENGTH);

    try {
        if (channel.isConnected()) {
            channel.start();
        } else {
            channel.connect();
        }
    } catch (JSchException jsche) {
        throw new IOException("Channel connection problems", jsche);
    }

    readResponse(is);

    File f = new File(localFile);
    long remain = f.length();

    String cMode = mode;
    if (cMode == null) {
        cMode = "0600";
    }
    String cline = "C" + cMode + " " + remain + " " + remoteName + "\n";

    os.write(cline.getBytes());
    os.flush();

    readResponse(is);

    try (FileInputStream fis = new FileInputStream(f)) {
        while (remain > 0) {
            int trans;
            if (remain > buffer.length) {
                trans = buffer.length;
            } else {
                trans = (int) remain;
            }
            if (fis.read(buffer, 0, trans) != trans) {
                throw new IOException("Cannot read enough from local file " + localFile);
            }

            os.write(buffer, 0, trans);

            remain -= trans;
        }

        fis.close();
    }

    os.write(0);
    os.flush();

    readResponse(is);

    os.write("E\n".getBytes());
    os.flush();
}
 
開發者ID:apache,項目名稱:ant-ivy,代碼行數:63,代碼來源:Scp.java


注:本文中的com.jcraft.jsch.Channel.isConnected方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。