Register a hub based on property file contents
Register a Hub (based on property file contents)
Grid.properties
HubRole=hub
HubHost=127.0.0.1
HubPort=4444
FileRead.java <== This reads above Grid.properties and returns a Map of it.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class FileRead {
public static FileInputStream readFile(String file) throws FileNotFoundException {
return new FileInputStream(new File(file));
}
public static Map
Properties prop = new Properties();
prop.load(readFile(“src/main/resources/Grid.properties”));
Map
Enumeration
return properties;
}
}
HubUtilities.java <== This class exposes “startHub()” and “stopHub” functions to initialize Hub on local file system
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import org.openqa.grid.internal.utils.configuration.GridHubConfiguration;
import org.openqa.grid.web.Hub;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HubUtilities {
Logger LOG = LoggerFactory.getLogger(HubUtilities.class);
private GridHubConfiguration gridHubConfig;
private Hub myHub;
private Map
public HubUtilities() {
try {
hubProperties = FileRead.readProperties();
} catch (IOException e) {
LOG.error(“Exception occurred reading Hub properties {} {}”, e.getMessage(), HubUtilities.class.getName());
}
}
public void startHub() {
if (!isHubRunning()) {
try {
gridHubConfig = new GridHubConfiguration();
gridHubConfig.role = hubProperties.get(“HubRole”);
gridHubConfig.host = hubProperties.get(“HubHost”);
gridHubConfig.port = Integer.parseInt(hubProperties.get(“HubPort”));
myHub = new Hub(gridHubConfig);
myHub.start();
} catch (Exception e) {
LOG.error(“Exception occurred during Hub initialization {} {}”, e.getMessage(), HubUtilities.class.getName());
}
}
}
public void stopHub() {
String line;
String[] pID;
if (isHubRunning()) {
try {
java.lang.Process p = Runtime.getRuntime().exec(“netstat -ano”);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
if (line.contains(“:4444”)) {
pID = line.split(“\\s+”);
String cmd = “taskkill /PID ” + pID[5] + ” /F”;
p = Runtime.getRuntime().exec(cmd);
}
}
} catch (Exception e) {
LOG.error(“Exception occurred on trying to stop hub {} {}”, e.getMessage(), HubUtilities.class.getName());
}
}
}
public boolean isHubRunning() {
String line;
String[] pID = null;
try {
java.lang.Process p = Runtime.getRuntime().exec(“netstat -ano”);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
if (line.contains(hubProperties.get(“HubPort”))) {
pID = line.split(“\\s+”);
}
}
} catch (Exception e) {
LOG.error(“Exception occurred while checking Hub running status {} {}”, e.getMessage(), HubUtilities.class.getName());
}
if (pID == null) {
return false;
} else {
return true;
}
}
}
Pom.xml <= = This defines following dependency
Output: on running a Hub
C:\>netstat -ano | findstr :4444
TCP 127.0.0.1:4444 0.0.0.0:0 LISTENING 14424
0 comments