Search in sources :

Example 1 with SystemOperationException

use of com.axway.ats.common.system.SystemOperationException in project ats-framework by Axway.

the class LocalSystemOperations method isListening.

/**
     *
     * @param host host address
     * @param port port number
     * @param timeout timeout value in milliseconds
     * @return <code>true</code> if the address is listening on this port
     * @throws SystemOperationException when the target host is unknown
     */
public boolean isListening(String host, int port, int timeout) {
    Socket socket = null;
    long endTime = System.currentTimeMillis() + timeout;
    try {
        do {
            try {
                socket = new Socket();
                socket.connect(new InetSocketAddress(host, port), timeout);
                return true;
            } catch (UnknownHostException uhe) {
                throw new SystemOperationException("Unknown host '" + host + "'", uhe);
            } catch (Exception e) {
                // Connection refused
                try {
                    Thread.sleep(300);
                } catch (InterruptedException ie) {
                }
            }
        } while (endTime > System.currentTimeMillis());
    } finally {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
            }
        }
    }
    return false;
}
Also used : UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) SystemOperationException(com.axway.ats.common.system.SystemOperationException) IOException(java.io.IOException) Socket(java.net.Socket) ParseException(java.text.ParseException) SystemOperationException(com.axway.ats.common.system.SystemOperationException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) AWTException(java.awt.AWTException)

Example 2 with SystemOperationException

use of com.axway.ats.common.system.SystemOperationException in project ats-framework by Axway.

the class LocalSystemOperations method setTime.

/**
     * Set the system time
     *
     * @param timestamp the timestamp
     * @param inMilliseconds whether the timestamp is in milliseconds or a formatted date string
     */
public void setTime(String timestamp, boolean inMilliseconds) {
    Calendar calendar = Calendar.getInstance();
    // parse the date
    if (inMilliseconds) {
        try {
            calendar.setTimeInMillis(Long.parseLong(timestamp));
        } catch (NumberFormatException e) {
            throw new SystemOperationException("Could not convert timestamp '" + timestamp + "' to long");
        }
    } else {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
        try {
            Date date = dateFormat.parse(timestamp);
            calendar.setTime(date);
        } catch (ParseException e) {
            throw new SystemOperationException("Could not parse timestamp '" + timestamp + "' to format " + DATE_FORMAT);
        }
    }
    try {
        OperatingSystemType osType = getOperatingSystemType();
        if (osType == OperatingSystemType.WINDOWS) {
            setWindowsTime(calendar);
        } else {
            setUnixTime(calendar);
        }
    } catch (Exception e) {
        throw new SystemOperationException("Could not set time", e);
    }
}
Also used : SystemOperationException(com.axway.ats.common.system.SystemOperationException) Calendar(java.util.Calendar) OperatingSystemType(com.axway.ats.common.system.OperatingSystemType) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) ParseException(java.text.ParseException) SystemOperationException(com.axway.ats.common.system.SystemOperationException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) AWTException(java.awt.AWTException)

Example 3 with SystemOperationException

use of com.axway.ats.common.system.SystemOperationException in project ats-framework by Axway.

the class LocalSystemOperations method createScreenshot.

/**
     * Creates display screenshot and save it in an image file.<br/>
     * The currently supported image formats/types are PNG, JPG, JPEG, GIF and BMP<br/>
     * <br/>
     * <b>NOTE:</b> For remote usage, the filePath value must be only the image file extension eg. ".PNG"
     *
     * @param filePath the screenshot image file path. If the file extension is not specified, the default format PNG will be used
     * @return the full path to the locally saved screenshot image file
     */
public String createScreenshot(String filePath) {
    try {
        if (filePath == null) {
            filePath = "." + DEFAULT_SCREENSHOT_FILE_EXT;
        }
        // extract and validate the image file type
        String fileType = null;
        int extensionIndex = filePath.lastIndexOf('.');
        if (extensionIndex >= 0 && filePath.length() > (extensionIndex + 1)) {
            fileType = filePath.substring(extensionIndex + 1);
            try {
                ImageType.valueOf(fileType.toUpperCase());
            } catch (IllegalArgumentException iae) {
                throw new SystemOperationException("Unsupported image file type \"" + fileType + "\". Use one of these: " + Arrays.toString(ImageType.values()));
            }
        } else {
            log.info("The screenshot file extension was not specified, the default one '.png' will be used instead");
            fileType = DEFAULT_SCREENSHOT_FILE_EXT;
            filePath = filePath + "." + fileType;
        }
        File imageFile = null;
        if (filePath.indexOf('.') == 0) {
            imageFile = File.createTempFile("ats_", "." + fileType);
            imageFile.deleteOnExit();
            filePath = imageFile.getCanonicalPath();
        } else {
            imageFile = new File(filePath);
        }
        Rectangle area = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        RenderedImage renderedImage = new Robot().createScreenCapture(area);
        ImageIO.write(renderedImage, fileType, imageFile);
    } catch (Exception e) {
        throw new SystemOperationException("Could not write Screenshot image to file '" + filePath + "'", e);
    }
    return filePath;
}
Also used : SystemOperationException(com.axway.ats.common.system.SystemOperationException) Rectangle(java.awt.Rectangle) RenderedImage(java.awt.image.RenderedImage) File(java.io.File) Robot(java.awt.Robot) ParseException(java.text.ParseException) SystemOperationException(com.axway.ats.common.system.SystemOperationException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) AWTException(java.awt.AWTException)

Example 4 with SystemOperationException

use of com.axway.ats.common.system.SystemOperationException in project ats-framework by Axway.

the class SystemOperations method getJvmMbeans.

/**
     * @param host the address of the host machine
     * @param jmxPort the jmx port
     * 
     * @return all MBeans with their attributes and type
     * @throws SystemOperationException
     */
@PublicAtsApi
public String getJvmMbeans(String host, String jmxPort) {
    JMXConnector jmxCon = null;
    try {
        // Connect to JMXConnector
        JMXServiceURL serviceUrl = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + jmxPort + "/jmxrmi");
        jmxCon = JMXConnectorFactory.newJMXConnector(serviceUrl, null);
        jmxCon.connect();
        // Access the MBean
        MBeanServerConnection con = jmxCon.getMBeanServerConnection();
        Set<ObjectName> queryResults = con.queryNames(null, null);
        StringBuilder results = new StringBuilder();
        for (ObjectName theName : queryResults) {
            results.append("\n---");
            results.append("\nMBean name: " + theName.getCanonicalName());
            MBeanAttributeInfo[] attributes = con.getMBeanInfo(theName).getAttributes();
            for (MBeanAttributeInfo attribute : attributes) {
                if (attribute.getType() != null) {
                    if (!"javax.management.openmbean.CompositeData".equals(attribute.getType())) {
                        if ("java.lang.Long".equals(attribute.getType()) || "java.lang.Integer".equals(attribute.getType()) || "int".equals(attribute.getType()) || "long".equals(attribute.getType()))
                            results.append("\r   " + attribute.getName() + " | " + attribute.getType());
                    } else {
                        results.append("\r   " + attribute.getName() + " | " + attribute.getType());
                        CompositeData comdata = (CompositeData) con.getAttribute(theName, attribute.getName());
                        if (comdata != null) {
                            for (String key : comdata.getCompositeType().keySet()) {
                                Object value = comdata.get(key);
                                if (value instanceof Integer || value instanceof Double || value instanceof Long)
                                    results.append("\r      " + key + " | " + value.getClass());
                            }
                        }
                    }
                }
            }
        }
        return results.toString();
    } catch (Exception e) {
        throw new SystemOperationException("MBeans with their attributes cannot be get.", e);
    } finally {
        if (jmxCon != null)
            try {
                jmxCon.close();
            } catch (IOException e) {
                log.error("JMX connection was not closed!");
            }
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) SystemOperationException(com.axway.ats.common.system.SystemOperationException) CompositeData(javax.management.openmbean.CompositeData) IOException(java.io.IOException) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) IOException(java.io.IOException) SystemOperationException(com.axway.ats.common.system.SystemOperationException) ObjectName(javax.management.ObjectName) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 5 with SystemOperationException

use of com.axway.ats.common.system.SystemOperationException in project ats-framework by Axway.

the class RemoteSystemOperations method createScreenshot.

@Override
public String createScreenshot(String filePath) {
    try {
        String remoteFilePath = null;
        int extIndex = filePath.lastIndexOf('.');
        if (extIndex > 0) {
            remoteFilePath = filePath.substring(extIndex);
        }
        remoteFilePath = this.remoteSystemOperations.createScreenshot(remoteFilePath);
        new RemoteFileSystemOperations(atsAgent).copyFileFrom(remoteFilePath, filePath, true);
    } catch (AgentException e) {
        throw new SystemOperationException("Could not create screenshot on agent: " + atsAgent, e);
    }
    return filePath;
}
Also used : AgentException(com.axway.ats.agent.core.exceptions.AgentException) SystemOperationException(com.axway.ats.common.system.SystemOperationException) RemoteFileSystemOperations(com.axway.ats.action.filesystem.RemoteFileSystemOperations)

Aggregations

SystemOperationException (com.axway.ats.common.system.SystemOperationException)5 IOException (java.io.IOException)4 AWTException (java.awt.AWTException)3 UnknownHostException (java.net.UnknownHostException)3 ParseException (java.text.ParseException)3 RemoteFileSystemOperations (com.axway.ats.action.filesystem.RemoteFileSystemOperations)1 AgentException (com.axway.ats.agent.core.exceptions.AgentException)1 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 OperatingSystemType (com.axway.ats.common.system.OperatingSystemType)1 Rectangle (java.awt.Rectangle)1 Robot (java.awt.Robot)1 RenderedImage (java.awt.image.RenderedImage)1 File (java.io.File)1 InetSocketAddress (java.net.InetSocketAddress)1 Socket (java.net.Socket)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)1 MBeanServerConnection (javax.management.MBeanServerConnection)1