String sNode = targetServer.getNodeRef();
Nodes nodes = domain.getNodes();
Node node = nodes.getNode(sNode);
sshL.init(node, logger);
SFTPClient sftpClient = sshL.getSFTPClient();
File logFileDirectoryOnServer = makingDirectory(domainRoot + File.separator + "logs"
+ File.separator + instanceName);
boolean noFileFound = true;
String loggingDir = getLoggingDirectoryForNode(instanceLogFileName, node, sNode, instanceName);
try {
Vector instanceLogFileNames = sftpClient.ls(loggingDir);
for (int i = 0; i < instanceLogFileNames.size(); i++) {
SFTPv3DirectoryEntry file = (SFTPv3DirectoryEntry) instanceLogFileNames.get(i);
String fileName = file.filename;
// code to remove . and .. file which is return from sftpclient ls method
if (!file.attributes.isDirectory() && !fileName.equals(".") && !fileName.equals("..")
&& fileName.contains(".log") && !fileName.contains(".log.")) {
noFileFound = false;
break;
}
}
} catch (Exception e) {
// if directory doesn't present or missing on remote machine. It happens due to bug 16451
noFileFound = true;
}
if (noFileFound) {
// this loop is used when user has changed value for server.log but not restarted the server.
loggingDir = getLoggingDirectoryForNodeWhenNoFilesFound(instanceLogFileName, node, sNode, instanceName);
}
String loggingFile = loggingDir + File.separator + logFileName;
if (!sftpClient.exists(loggingFile)) {
loggingFile = loggingDir + File.separator + "server.log";
} else if (!sftpClient.exists(loggingFile)) {
loggingFile = instanceLogFileName;
}
// creating local file name on DAS
long instanceLogFileSize = 0;
instanceLogFile = new File(logFileDirectoryOnServer.getAbsolutePath() + File.separator
+ loggingFile.substring(loggingFile.lastIndexOf(File.separator), loggingFile.length()));
// getting size of the file on DAS
if (instanceLogFile.exists())
instanceLogFileSize = instanceLogFile.length();
SFTPv3FileAttributes sftPv3FileAttributes = sftpClient._stat(loggingFile);
// getting size of the file on instance machine
long fileSizeOnNode = sftPv3FileAttributes.size;
// if differ both size then downloading
if (instanceLogFileSize != fileSizeOnNode) {
BufferedInputStream in = null;
FileOutputStream file = null;
BufferedOutputStream out = null;
InputStream inputStream = sftpClient.read(loggingFile);
in = new BufferedInputStream(inputStream);
file = new FileOutputStream(instanceLogFile);
out = new BufferedOutputStream(file);
int i;
while ((i = in.read()) != -1) {
out.write(i);
}
out.flush();
}
sftpClient.close();
return instanceLogFile;
}