本文整理汇总了Java中net.schmizz.sshj.common.IOUtils.closeQuietly方法的典型用法代码示例。如果您正苦于以下问题:Java IOUtils.closeQuietly方法的具体用法?Java IOUtils.closeQuietly怎么用?Java IOUtils.closeQuietly使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.schmizz.sshj.common.IOUtils
的用法示例。
在下文中一共展示了IOUtils.closeQuietly方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doEndTag
import net.schmizz.sshj.common.IOUtils; //导入方法依赖的package包/类
@Override
public int doEndTag() throws JspException {
// Get input and ouput variables
Reader rawContent = (Reader) pageContext.getAttribute(Constants.RAW_CONTENT_KEY, PageContext.REQUEST_SCOPE);
JspWriter out = pageContext.getOut();
try {
// Copy input (rawContent) to output (out)
char[] buffer = new char[StreamUtils.BUFFER_SIZE];
int bytesRead = -1;
while ((bytesRead = rawContent.read(buffer)) != -1) {
String stringToWrite = new String(buffer, 0, bytesRead);
stringToWrite = HtmlUtils.htmlEscape(stringToWrite);
out.write(stringToWrite);
}
out.flush();
return EVAL_PAGE;
}
catch (IOException e) {
throw new JspException(e);
}
finally {
IOUtils.closeQuietly(rawContent);
}
}
示例2: executeCommand
import net.schmizz.sshj.common.IOUtils; //导入方法依赖的package包/类
@Override
public InputStream executeCommand(String logAccessConfigId, String shellCommand) throws LogAccessException {
// Get the LogAccessConfig
LogAccessConfig logAccessConfig = configService.getLogAccessConfig(logAccessConfigId);
// Create ssh client and authenticate
SSHClient sshClient = sshClientThreadLocal.get();
boolean closeSshClient = false;
if (sshClient == null) {
sshClient = connectAndAuthenticate(logAccessConfig);
closeSshClient = true;
}
// Define the precommand (if any)
String precommand = "";
if (StringUtils.hasText(logAccessConfig.getPreCommand())) {
precommand = logAccessConfig.getPreCommand() + " && ";
}
// Execute the shell command
Session session = null;
Command resultCommand;
try {
session = sshClient.startSession();
resultCommand = session.exec("cd \"" + logAccessConfig.getDirectory() + "\" && " + precommand + shellCommand);
}
catch (SSHException e) {
IOUtils.closeQuietly(session, sshClient);
throw new LogAccessException("Error when executing command " + shellCommand + " to " + logAccessConfig, e);
}
// Get and return the result stream
InputStream sequenceStream = new SequenceInputStream(resultCommand.getInputStream(), resultCommand.getErrorStream());
InputStream resultStream = new SshCloseFilterInputStream(sequenceStream, resultCommand, session, (closeSshClient ? sshClient : null));
return resultStream;
}
示例3: listFilesUsingNativeSystem
import net.schmizz.sshj.common.IOUtils; //导入方法依赖的package包/类
@Override
protected Set<FileInfo> listFilesUsingNativeSystem(LogAccessConfig logAccessConfig, String subPath) throws LogAccessException {
// Get ssh client
SSHClient sshClient = sshClientThreadLocal.get();
// Define target directory
String targetPath = logAccessConfig.getDirectory();
if (subPath != null) {
targetPath += "/" + subPath;
}
// List files and directories (keep only the 'fileListMaxCount' last modified resources)
SFTPClient sftpClient = null;
Collection<RemoteResourceInfo> remoteResourceInfos;
try {
sftpClient = sshClient.newSFTPClient();
LastUpdatedRemoteResourceFilter remoteResourcefilter = new LastUpdatedRemoteResourceFilter(configService.getFileListMaxCount());
sftpClient.ls(targetPath, remoteResourcefilter);
remoteResourceInfos = remoteResourcefilter.getRemoteResourceInfos();
}
catch (IOException e) {
throw new LogAccessException("Error when listing files and directories on " + logAccessConfig, e);
}
finally {
IOUtils.closeQuietly(sftpClient, sshClient);
}
// Extract meta-informations
Set<FileInfo> fileInfos = new TreeSet<FileInfo>();
for (RemoteResourceInfo remoteResourceInfo : remoteResourceInfos) {
FileInfo fileInfo = new FileInfo();
fileInfo.setFileName(remoteResourceInfo.getName());
fileInfo.setRelativePath(remoteResourceInfo.getPath().substring(logAccessConfig.getDirectory().length() + 1).replace('\\', '/'));
fileInfo.setDirectory(remoteResourceInfo.isDirectory());
fileInfo.setLastModified(new Date(remoteResourceInfo.getAttributes().getMtime() * 1000L));
fileInfo.setFileSize(remoteResourceInfo.isDirectory() ? 0L : remoteResourceInfo.getAttributes().getSize());
fileInfo.setLogAccessType(LogAccessType.SSH);
fileInfos.add(fileInfo);
}
// Return meta-informations about files and folders
return fileInfos;
}
示例4: executeCommand
import net.schmizz.sshj.common.IOUtils; //导入方法依赖的package包/类
@RequestMapping("/logs/{logAccessConfigId}/command")
public String executeCommand(Model model,
HttpServletRequest request,
@PathVariable String logAccessConfigId,
@RequestParam(value="cmd", required=false, defaultValue=DEFAULT_LIST_COMMAND) String cmd,
@RequestParam(value="encoding", required=false) String encoding,
@RequestParam(value="displayType", required=false) DisplayType displayType
) throws AuthorizationException, LogAccessException, IOException {
// Parse command line
CommandLine commandLine = CommandLineParser.parseCommandLine(cmd);
// Is command forbidden ?
checkForbiddenCommand(commandLine);
// Forward to 'list' action, if command is 'ls'
if ((displayType == null || displayType == DisplayType.TABLE) && commandLine.getCommand().equals(DEFAULT_LIST_COMMAND) && !cmd.contains("|")) {
if (commandLine.hasParams()) {
return UrlBasedViewResolver.FORWARD_URL_PREFIX + FOLDER_VIEW_URL_PREFIX + commandLine.getParam(0);
}
else {
return UrlBasedViewResolver.FORWARD_URL_PREFIX + LOGS_LIST_URL;
}
}
// Define default encoding when not given by client
if (encoding == null) {
encoding = configService.getDefaultEncoding(logAccessConfigId);
}
// Define default displayType when not given by client
if (displayType == null) {
if (cmd.startsWith(TAR_GZ_FILE_VIEW_COMMAND_START) || cmd.endsWith(TAR_GZ_FILE_VIEW_COMMAND_END)) {
displayType = DisplayType.TABLE;
}
else {
displayType = DisplayType.RAW;
}
}
// Add options to model
request.setAttribute(SHOW_OPTIONS_KEY, true);
request.setAttribute(ENCODING_KEY, encoding);
request.setAttribute(DISPLAY_TYPE_KEY, displayType);
// Generate Breadcrumbs
generateBreadcrumbs(logAccessConfigId, commandLine, request);
// Execute the command
InputStream resultStream = logAccessService.executeCommand(logAccessConfigId, cmd);
BufferedReader resultReader = new BufferedReader(new InputStreamReader(resultStream, encoding));
// Process the result lines for raw display
if (displayType == DisplayType.RAW) {
model.addAttribute(RAW_CONTENT_KEY, resultReader);
return VIEW_RAW;
}
// Process the result lines for html table display
else {
try {
if (cmd.startsWith(TAR_GZ_FILE_VIEW_COMMAND_START) || cmd.endsWith(TAR_GZ_FILE_VIEW_COMMAND_END)) {
return processTarGzList(resultReader, model, cmd);
}
else {
processOtherCommand(resultReader, model);
}
}
finally {
IOUtils.closeQuietly(resultReader);
}
return VIEW_TABLE;
}
}
示例5: close
import net.schmizz.sshj.common.IOUtils; //导入方法依赖的package包/类
@Override
public void close() throws IOException {
// Close the stream, session and ssh client
IOUtils.closeQuietly(super.in, command, session, sshClient);
}