Search in sources :

Example 6 with WindowsException

use of com.sun.enterprise.util.cluster.windows.process.WindowsException in project Payara by payara.

the class WindowsRemoteFile method copyFrom.

public final void copyFrom(File from, RemoteFileCopyProgress progress) throws WindowsException {
    try {
        if (from == null || !from.isFile())
            throw new IllegalArgumentException("copyFrom file arg is bad: " + from);
        long filesize = from.length();
        BufferedInputStream sin = new BufferedInputStream(new FileInputStream(from));
        copyFrom(sin, progress, filesize);
    } catch (WindowsException e) {
        throw e;
    } catch (Exception e) {
        throw new WindowsException(e);
    }
}
Also used : WindowsException(com.sun.enterprise.util.cluster.windows.process.WindowsException) WindowsException(com.sun.enterprise.util.cluster.windows.process.WindowsException)

Example 7 with WindowsException

use of com.sun.enterprise.util.cluster.windows.process.WindowsException in project Payara by payara.

the class WindowsRemoteFile method copyFrom.

/*
     * Use this for tiny files -- like scripts that are created on-the-fly from a String
     */
public final void copyFrom(String from) throws WindowsException {
    try {
        if (from == null || from.isEmpty())
            throw new IllegalArgumentException("copyFrom String arg is empty");
        if (!exists())
            createNewFile();
        PrintWriter pw = new PrintWriter(new BufferedOutputStream(smbFile.getOutputStream()));
        pw.print(from);
        try {
            pw.close();
        } catch (Exception e) {
        // nothing can be done!
        }
    } catch (Exception e) {
        throw new WindowsException(e);
    }
}
Also used : WindowsException(com.sun.enterprise.util.cluster.windows.process.WindowsException) WindowsException(com.sun.enterprise.util.cluster.windows.process.WindowsException)

Example 8 with WindowsException

use of com.sun.enterprise.util.cluster.windows.process.WindowsException in project Payara by payara.

the class LogFilterForInstance method downloadGivenInstanceLogFile.

public File downloadGivenInstanceLogFile(ServiceLocator habitat, Server targetServer, Domain domain, Logger logger, String instanceName, String domainRoot, String logFileName, String instanceLogFileName) throws IOException {
    File instanceLogFile = null;
    // method is used from logviewer back end code logfilter.
    // for Instance it's going through this loop. This will use ssh utility to get file from instance machine(remote machine) and
    // store in domains/domain1/logs/<instance name> which is used to get LogFile object.
    // Right now user needs to go through this URL to setup and configure ssh http://wikis.sun.com/display/GlassFish/3.1SSHSetup
    SSHLauncher sshL = getSSHL(habitat);
    String sNode = targetServer.getNodeRef();
    Nodes nodes = domain.getNodes();
    Node node = nodes.getNode(sNode);
    if (node.getType().equals("SSH")) {
        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 {
            List 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) {
            try (BufferedInputStream in = new BufferedInputStream(sftpClient.read(loggingFile));
                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(instanceLogFile))) {
                int i;
                while ((i = in.read()) != -1) {
                    out.write(i);
                }
                out.flush();
            }
        }
        sftpClient.close();
    } else if (node.getType().equals("DCOM")) {
        File logFileDirectoryOnServer = makingDirectory(domainRoot + File.separator + "logs" + File.separator + instanceName);
        String loggingDir = getLoggingDirectoryForNode(instanceLogFileName, node, sNode, instanceName);
        try {
            DcomInfo info = new DcomInfo(node);
            WindowsRemoteFileSystem wrfs = new WindowsRemoteFileSystem(info.getHost(), info.getUser(), info.getPassword());
            if (logFileName == null || logFileName.equals("")) {
                logFileName = "server.log";
            }
            WindowsRemoteFile wrf = new WindowsRemoteFile(wrfs, loggingDir + File.separator + logFileName);
            instanceLogFile = new File(logFileDirectoryOnServer + File.separator + logFileName);
            wrf.copyTo(instanceLogFile);
        } catch (WindowsException ex) {
            throw new IOException("Unable to download instance log file from DCOM Instance Node");
        }
    }
    return instanceLogFile;
}
Also used : SSHLauncher(org.glassfish.cluster.ssh.launcher.SSHLauncher) Node(com.sun.enterprise.config.serverbeans.Node) SFTPClient(org.glassfish.cluster.ssh.sftp.SFTPClient) IOException(java.io.IOException) WindowsException(com.sun.enterprise.util.cluster.windows.process.WindowsException) Nodes(com.sun.enterprise.config.serverbeans.Nodes) IOException(java.io.IOException) WindowsException(com.sun.enterprise.util.cluster.windows.process.WindowsException) DcomInfo(org.glassfish.cluster.ssh.util.DcomInfo) SFTPv3DirectoryEntry(com.trilead.ssh2.SFTPv3DirectoryEntry) WindowsRemoteFile(com.sun.enterprise.util.cluster.windows.io.WindowsRemoteFile) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) WindowsRemoteFileSystem(com.sun.enterprise.util.cluster.windows.io.WindowsRemoteFileSystem) ArrayList(java.util.ArrayList) List(java.util.List) SFTPv3FileAttributes(com.trilead.ssh2.SFTPv3FileAttributes) WindowsRemoteFile(com.sun.enterprise.util.cluster.windows.io.WindowsRemoteFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 9 with WindowsException

use of com.sun.enterprise.util.cluster.windows.process.WindowsException in project Payara by payara.

the class InstallNodeDcomCommand method precopy.

/**
 * bnevins: This is exclusively a "user-performance" enhancement.
 * We are forcing the failure
 * to happen before the very very slow zipfile creation.
 * FAIL FAST principle
 * This adds a bit of extra overhead to the command...
 * Note that allowing multiple hosts makes things MUCH more complicated.
 * @throws WindowsException
 */
@Override
final void precopy() throws CommandException {
    remoteInstallDirString = getInstallDir().replace('/', '\\');
    try {
        for (String host : hosts) {
            String remotePassword = getWindowsPassword(host);
            passwords.add(new HostAndPassword(host, remotePassword));
            if (!getForce()) {
                WindowsRemoteFileSystem wrfs = new WindowsRemoteFileSystem(host, getRemoteUser(), remotePassword);
                WindowsRemoteFile remoteInstallDir = new WindowsRemoteFile(wrfs, remoteInstallDirString);
                if (remoteInstallDir.exists())
                    throw new CommandException(Strings.get("install.dir.exists", remoteInstallDir));
            }
        }
    } catch (WindowsException ex) {
        throw new CommandException(ex);
    }
}
Also used : WindowsRemoteFile(com.sun.enterprise.util.cluster.windows.io.WindowsRemoteFile) WindowsRemoteFileSystem(com.sun.enterprise.util.cluster.windows.io.WindowsRemoteFileSystem) CommandException(org.glassfish.api.admin.CommandException) WindowsException(com.sun.enterprise.util.cluster.windows.process.WindowsException)

Example 10 with WindowsException

use of com.sun.enterprise.util.cluster.windows.process.WindowsException in project Payara by payara.

the class WindowsRemoteFile method copyTo.

/**
 * Copy the remote Windows file to the given File
 *
 * @param f The File that will be created or overwritten with the contents of
 * this Windows Remote File.
 * @param progress The optional callback object that gets called for each
 * chunk of data that gets copied over the wire.  Setting it to null is OK.
 * @throws WindowsException
 * @since 3.1.2
 */
public final void copyTo(final File file, final RemoteFileCopyProgress progress) throws WindowsException {
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {
        final long filelength = smbFile.length();
        bis = new BufferedInputStream(smbFile.getInputStream());
        bos = new BufferedOutputStream(new FileOutputStream(file));
        byte[] buf = new byte[getChunkSize(progress, filelength)];
        int numBytes = 0;
        long totalBytesCopied = 0;
        while ((numBytes = bis.read(buf)) >= 0) {
            bos.write(buf, 0, numBytes);
            totalBytesCopied += numBytes;
            // It's OK to send in a null Progress object
            if (progress != null)
                progress.callback(totalBytesCopied, filelength);
        }
    } catch (Exception se) {
        throw new WindowsException(se);
    } finally {
        if (bis != null)
            try {
                bis.close();
            } catch (Exception e) {
            // this is SO messy!
            }
        if (bos != null)
            try {
                bos.close();
            } catch (Exception e) {
            // this is SO messy!
            }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) WindowsException(com.sun.enterprise.util.cluster.windows.process.WindowsException) WindowsException(com.sun.enterprise.util.cluster.windows.process.WindowsException)

Aggregations

WindowsException (com.sun.enterprise.util.cluster.windows.process.WindowsException)16 WindowsRemoteFile (com.sun.enterprise.util.cluster.windows.io.WindowsRemoteFile)8 WindowsRemoteFileSystem (com.sun.enterprise.util.cluster.windows.io.WindowsRemoteFileSystem)5 DcomInfo (org.glassfish.cluster.ssh.util.DcomInfo)5 File (java.io.File)4 IOException (java.io.IOException)4 SSHLauncher (org.glassfish.cluster.ssh.launcher.SSHLauncher)4 Node (com.sun.enterprise.config.serverbeans.Node)3 SFTPv3DirectoryEntry (com.trilead.ssh2.SFTPv3DirectoryEntry)3 FileOutputStream (java.io.FileOutputStream)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 SFTPClient (org.glassfish.cluster.ssh.sftp.SFTPClient)3 Nodes (com.sun.enterprise.config.serverbeans.Nodes)2 WindowsRemoteScripter (com.sun.enterprise.util.cluster.windows.process.WindowsRemoteScripter)2 WindowsCredentials (com.sun.enterprise.util.cluster.windows.process.WindowsCredentials)1 WindowsRemoteAsadmin (com.sun.enterprise.util.cluster.windows.process.WindowsRemoteAsadmin)1 WindowsWmi (com.sun.enterprise.util.cluster.windows.process.WindowsWmi)1 SCPClient (com.trilead.ssh2.SCPClient)1 SFTPv3FileAttributes (com.trilead.ssh2.SFTPv3FileAttributes)1