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;
}
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);
}
}
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;
}
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!");
}
}
}
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;
}
Aggregations