Adding correction

revert-dabc3590
Nirothipan 8 years ago
parent 6acf7c4225
commit 944a28aae0

@ -41,6 +41,8 @@ import java.util.Enumeration;
import java.util.Scanner; import java.util.Scanner;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
@ -53,9 +55,9 @@ public class TryIt {
private String androidSdkHome; private String androidSdkHome;
private String userHome; private String userHome;
private String workingDirectory; private String workingDirectory;
private File sdkLocationFile; // file in which SDK location is written
private String adbLocation; // location of executable file abd private String adbLocation; // location of executable file abd
private String emulatorLocation; // location of executable file emulator private String emulatorLocation; // location of executable file emulator
private File sdkConfigFile; // file in which SDK location is written
/** /**
* This method gets the system specific variables. * This method gets the system specific variables.
@ -68,7 +70,7 @@ public class TryIt {
if (osSuffix.contains(Constants.WINDOWS_OS)) { if (osSuffix.contains(Constants.WINDOWS_OS)) {
osSuffix = Constants.WINDOWS_OS; osSuffix = Constants.WINDOWS_OS;
} }
if (osSuffix.contains("mac")) { if (osSuffix.contains(Constants.MAC)) {
osSuffix = Constants.MAC_OS; osSuffix = Constants.MAC_OS;
} }
System.out.println("Detected OS " + osSuffix); System.out.println("Detected OS " + osSuffix);
@ -80,52 +82,23 @@ public class TryIt {
* @param args commandline arguments. * @param args commandline arguments.
*/ */
public static void main(String[] args) { public static void main(String[] args) {
TryIt tryIt = new TryIt(); TryIt tryIt = new TryIt();
tryIt.setAndroidSDK(); tryIt.setAndroidSDK();
tryIt.checkBuildTools(); tryIt.checkBuildTools();
tryIt.startAVD();
try { tryIt.checkEmulatorBoot();
tryIt.startAVD(); String[] agents = tryIt.checkForAgent();
} catch (IOException e) {
tryIt.handleException("Unable to start AVD", e);
}
try {
tryIt.checkEmulatorBoot();
} catch (IOException e) {
tryIt.handleException("Emulator boot process failure", e);
}
String[] agents = new String[2];
try {
agents = tryIt.checkForAgent();
} catch (IOException ignored) {
// can continue installing agent again
}
System.out.println("Starting Agent ..."); System.out.println("Starting Agent ...");
try { tryIt.startPackage(agents);
tryIt.startPackage(agents);
} catch (IOException e) {
tryIt.handleException("Unable to start WSO2 package", e);
}
Process startShell = null;
ProcessBuilder startShellProcessBuilder = new ProcessBuilder(tryIt.adbLocation, "shell"); ProcessBuilder startShellProcessBuilder = new ProcessBuilder(tryIt.adbLocation, "shell");
try { try {
startShellProcessBuilder.redirectInput(ProcessBuilder.Redirect.INHERIT); startShellProcessBuilder.redirectInput(ProcessBuilder.Redirect.INHERIT);
startShellProcessBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT); startShellProcessBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
startShell = startShellProcessBuilder.start(); Process startShell = startShellProcessBuilder.start();
} catch (IOException ignored) { System.out.println("Connected to device shell");
//can continue startShell.waitFor();
} } catch (IOException | InterruptedException ignored) {
System.out.println("Connected to device shell"); // script can continue without this process
try {
if (startShell != null) {
startShell.waitFor();
}
} catch (InterruptedException ignored) {
// Interrupted if AVD is closed only.
} }
System.out.println("Good Bye!"); System.out.println("Good Bye!");
} }
@ -137,22 +110,16 @@ public class TryIt {
* @param folderName - the folder location to download the files to. * @param folderName - the folder location to download the files to.
*/ */
private void downloadArtifacts(String path, String folderName) { private void downloadArtifacts(String path, String folderName) {
ReadableByteChannel rbc = null; ReadableByteChannel readableByteChannel = null;
FileOutputStream fos = null; FileOutputStream fileOutputStream = null;
URL url = null;
try { try {
url = new URL(path); URL url = new URL(path);
readableByteChannel = Channels.newChannel(url.openStream());
fileOutputStream = new FileOutputStream(folderName);
fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
handleException("Downloading " + folderName + " failed.", e); System.out.println("Error in download URL of " + folderName);
} System.out.println("URL provided " + path);
try {
if (url != null) {
rbc = Channels.newChannel(url.openStream());
}
fos = new FileOutputStream(folderName);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
} catch (IOException e) { } catch (IOException e) {
if (!new File(folderName).delete()) { if (!new File(folderName).delete()) {
System.out.println("Delete " + folderName + " and try again"); System.out.println("Delete " + folderName + " and try again");
@ -160,103 +127,80 @@ public class TryIt {
handleException("Downloading " + folderName + " failed.", e); handleException("Downloading " + folderName + " failed.", e);
} finally { } finally {
try { try {
if (fos != null) { if (fileOutputStream != null) {
fos.close(); fileOutputStream.close();
} }
if (rbc != null) { if (readableByteChannel != null) {
rbc.close(); readableByteChannel.close();
} }
} catch (IOException ignored) { } catch (IOException ignored) {
// File close exception ignored // Exception in finally block
} }
} }
} }
/** /**
* This method validates the Android SDK location provided by the user and write it to the file * This method validates the Android SDK location provided by the user and write it to the file
* sdkLocationFile. * sdkConfigFile.
*/ */
private void setSDKPath() { private void setSDKPath() {
System.out.println("Please provide android SDK location"); System.out.println("Please provide android SDK location : ");
String response = new Scanner(System.in, "UTF-8").next(); String response = new Scanner(System.in, "UTF-8").next();
String emulatorLocationPath = response + File.separator + "tools" + File.separator + "emulator"; String emulatorLocationPath = response + File.separator + "tools" + File.separator + "emulator";
if (osSuffix.equals(Constants.WINDOWS_OS)) {
if (osSuffix.equals(Constants.WINDOWS_OS)) { // windows emulator location ends with .bat
emulatorLocationPath += Constants.WINDOWS_EXTENSION_BAT; emulatorLocationPath += Constants.WINDOWS_EXTENSION_BAT;
} }
if (new File(emulatorLocationPath).exists()) { if (new File(emulatorLocationPath).exists()) {
Writer writer = null; androidSdkHome = response;
try { writeToSdkConfigFile(response);
writer = new OutputStreamWriter(new FileOutputStream(sdkLocationFile), StandardCharsets.UTF_8);
} catch (FileNotFoundException e) {
System.out.println("Unable to write the sdkLocation to file ");
}
try {
if (writer != null) {
writer.write(response);
}
} catch (IOException e) {
androidSdkHome = response;
System.out.println("Unable to write the sdkLocation to file ");
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException ignored) {
// writer close
}
}
} else { } else {
System.out.println("Invalid SDK location"); System.out.println("Invalid SDK location");
setSDKPath(); setSDKPath();
} }
} }
/** /**
* This method creates a folder named android-sdk and downloads the minimum tools for SDK * This method writes the SDK location to a file sdkConfigFile for future use.
* and write the sdk-location to the file sdkLocationFile. *
* @param string - SDK location.
*/ */
private void getAndroidSDK() { private void writeToSdkConfigFile(String string) {
String androidSdkFolderName = "android-sdk";
//noinspection ResultOfMethodCallIgnored
new File(workingDirectory + File.separator + androidSdkFolderName).mkdir();
androidSdkHome = workingDirectory + File.separator + androidSdkFolderName;
getTools(System.getProperty(Constants.SDK_TOOLS_URL), "_Android-sdk-tools.zip");
getTools(System.getProperty(Constants.PLATFORM_TOOLS_URL), "_Android-platform-tools.zip");
Writer writer = null; Writer writer = null;
try { try {
writer = new OutputStreamWriter(new FileOutputStream(sdkLocationFile), StandardCharsets.UTF_8); writer = new OutputStreamWriter(new FileOutputStream(sdkConfigFile), StandardCharsets.UTF_8);
} catch (FileNotFoundException e) { writer.write(string);
System.out.println("Unable to write the sdkLocation to file ");
}
try {
if (writer != null) {
writer.write(androidSdkHome);
}
} catch (IOException e) { } catch (IOException e) {
System.out.println("Unable to write the sdkLocation to file "); System.out.println("Writing to " + sdkConfigFile.toString() + " failed.");
e.printStackTrace();
} finally { } finally {
try { try {
if (writer != null) { if (writer != null) {
writer.close(); writer.close();
} }
} catch (IOException ignored) { } catch (IOException ignored) {
// writes close //
} }
} }
} }
/**
* This method creates a folder named android-sdk and downloads the minimum tools for SDK
* and write the sdk-location to the file sdkConfigFile.
*/
private void getAndroidSDK() {
String androidSdkFolderName = "android-sdk";
if (!new File(workingDirectory + File.separator + androidSdkFolderName).exists()) {
if (!new File(workingDirectory + File.separator + androidSdkFolderName).mkdir()) {
System.out.println("Unable to make folder named " + androidSdkFolderName + " in " + workingDirectory);
System.exit(1);
}
}
androidSdkHome = workingDirectory + File.separator + androidSdkFolderName;
getTools(System.getProperty(Constants.SDK_TOOLS_URL), "_Android-sdk-tools.zip");
getTools(System.getProperty(Constants.PLATFORM_TOOLS_URL), "_Android-platform-tools.zip");
writeToSdkConfigFile(androidSdkHome);
}
/** /**
* This method downloads and extracts the tools. * This method downloads and extracts the tools.
* *
@ -272,19 +216,17 @@ public class TryIt {
/** /**
* This method starts the AVD specified by the user. * This method starts the AVD specified by the user.
*
* @throws IOException process throws if an I/O error occurs.
*/ */
private void startAVD() throws IOException { private void startAVD() {
String wso2AvdLocation = userHome + File.separator + ".android" + File.separator + "avd" + File.separator String wso2AvdLocation = userHome + File.separator + ".android" + File.separator + "avd" + File.separator
+ "WSO2_AVD.avd"; + Constants.WSO2_AVD_NAME + ".avd";
checkForPlatform(); checkForPlatform();
checkForSystemImages(); checkForSystemImages();
if (!new File(wso2AvdLocation).isDirectory()) { if (!new File(wso2AvdLocation).isDirectory()) {
Scanner read = new Scanner(System.in, "UTF-8"); Scanner read = new Scanner(System.in, "UTF-8");
System.out.println("Do you want to create WSO2_AVD with default configs (Y/n)?: "); System.out.print("Do you want to create WSO2_AVD with default configs (Y/n)?: ");
if (read.next().toLowerCase().matches("y")) { if (read.next().toLowerCase().matches("y")) {
createAVD(); createAVD();
return; return;
@ -296,30 +238,50 @@ public class TryIt {
System.out.println("+----------------------------------------------------------------+"); System.out.println("+----------------------------------------------------------------+");
emulatorLocation = androidSdkHome + File.separator + "tools" + File.separator + "emulator"; emulatorLocation = androidSdkHome + File.separator + "tools" + File.separator + "emulator";
if (osSuffix.equals(Constants.WINDOWS_OS)) { if (osSuffix.equals(Constants.WINDOWS_OS)) {
emulatorLocation += Constants.WINDOWS_EXTENSION_EXE; emulatorLocation += Constants.WINDOWS_EXTENSION_EXE;
} }
setExecutePermission(emulatorLocation); setExecutePermission(emulatorLocation);
ProcessBuilder listAVDsProcessBuilder = new ProcessBuilder(emulatorLocation, "-list-avds"); listAVDs();
Process listAVDsProcess = listAVDsProcessBuilder.start(); }
/**
* This method gets the available AVDs' name from the system.
*/
private void listAVDs() {
ArrayList<String> devices = new ArrayList<>(); ArrayList<String> devices = new ArrayList<>();
BufferedReader reader = null; BufferedReader reader = null;
try { try {
ProcessBuilder listAVDsProcessBuilder = new ProcessBuilder(emulatorLocation, "-list-avds");
Process listAVDsProcess = listAVDsProcessBuilder.start();
reader = new BufferedReader(new InputStreamReader(listAVDsProcess.getInputStream(), reader = new BufferedReader(new InputStreamReader(listAVDsProcess.getInputStream(),
StandardCharsets.UTF_8)); StandardCharsets.UTF_8));
String readLine; String readLine;
while ((readLine = reader.readLine()) != null) { while ((readLine = reader.readLine()) != null) {
devices.add(readLine); devices.add(readLine);
} }
selectAVD(devices);
} catch (IOException e) {
//TODO
} finally { } finally {
if (reader != null) { try {
reader.close(); if (reader != null) {
reader.close();
}
} catch (IOException ignored) {
// exception in finally block
} }
} }
}
/**
* This method enables the user to select an AVD form available AVDs.
*
* @param devices - list of available AVDs.
*/
private void selectAVD(ArrayList<String> devices) {
if (devices.size() == 0) { if (devices.size() == 0) {
System.out.println("No AVDs available in the system "); System.out.println("No AVDs available in the system ");
startAVD(); startAVD();
@ -341,69 +303,66 @@ public class TryIt {
/** /**
* This method creates WSO2_AVD with the specific configurations. * This method creates WSO2_AVD with the specific configurations.
*
* @throws IOException process throws if an I/O error occurs.
*/ */
private void createAVD() throws IOException { private void createAVD() {
String avdManagerPath = androidSdkHome + File.separator + "tools" + File.separator + "bin" String avdManagerPath = androidSdkHome + File.separator + "tools" + File.separator + "bin"
+ File.separator + "avdmanager"; + File.separator + "avdmanager";
String androidPath = androidSdkHome + File.separator + "tools" + File.separator + "android"; String androidPath = androidSdkHome + File.separator + "tools" + File.separator + "android";
String configFileLocation = workingDirectory + File.separator + "resources" + File.separator + "config.ini";
String wso2ConfigFile = userHome + File.separator + ".android" + File.separator + "avd" + File.separator
+ "WSO2_AVD.avd" + File.separator + "config.ini";
if (osSuffix.equals(Constants.WINDOWS_OS)) { if (osSuffix.equals(Constants.WINDOWS_OS)) {
avdManagerPath += Constants.WINDOWS_EXTENSION_BAT; avdManagerPath += Constants.WINDOWS_EXTENSION_BAT;
}
if (osSuffix.equals(Constants.WINDOWS_OS)) {
androidPath += Constants.WINDOWS_EXTENSION_BAT; androidPath += Constants.WINDOWS_EXTENSION_BAT;
} }
setExecutePermission(androidPath); setExecutePermission(androidPath);
System.out.println("Creating a new AVD device"); System.out.println("Creating a new AVD device");
try {
if (new File(avdManagerPath).exists()) { if (new File(avdManagerPath).exists()) {
setExecutePermission(avdManagerPath); setExecutePermission(avdManagerPath);
ProcessBuilder createAvdProcessBuilder = new ProcessBuilder(avdManagerPath, "create", "avd", "-k", ProcessBuilder createAvdProcessBuilder = new ProcessBuilder(avdManagerPath, "create", "avd", "-k",
"system-images;android-23;default;x86", "-n", "WSO2_AVD"); "system-images;android-23;default;x86", "-n", Constants.WSO2_AVD_NAME);
createAvdProcessBuilder.redirectInput(ProcessBuilder.Redirect.INHERIT);
createAvdProcessBuilder.redirectInput(ProcessBuilder.Redirect.INHERIT); createAvdProcessBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
createAvdProcessBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT); Process createAvdProcess = createAvdProcessBuilder.start();
Process createAvdProcess = createAvdProcessBuilder.start();
try {
createAvdProcess.waitFor(); createAvdProcess.waitFor();
} catch (InterruptedException e) {
handleException("Unable to create new AVD", e);
}
} else {
ProcessBuilder createAvd = new ProcessBuilder(androidPath, "create", "avd", "-n", "WSO2_AVD",
"-t", "android-23");
createAvd.redirectInput(ProcessBuilder.Redirect.INHERIT);
createAvd.redirectOutput(ProcessBuilder.Redirect.INHERIT);
Process createAvdProcess = createAvd.start(); } else {
ProcessBuilder createAvd = new ProcessBuilder(androidPath, "create", "avd", "-n",
try { Constants.WSO2_AVD_NAME, "-t", "android-23");
createAvd.redirectInput(ProcessBuilder.Redirect.INHERIT);
createAvd.redirectOutput(ProcessBuilder.Redirect.INHERIT);
Process createAvdProcess = createAvd.start();
createAvdProcess.waitFor(); createAvdProcess.waitFor();
} catch (InterruptedException e) {
handleException("Unable to create new AVD", e);
} }
} catch (IOException e) {
handleException("Unable to create " + Constants.WSO2_AVD_NAME, e);
} catch (InterruptedException ignored) {
// interruption in main thread
} }
Files.copy(Paths.get(configFileLocation), Paths.get(wso2ConfigFile), StandardCopyOption.REPLACE_EXISTING); copyDefaultWSO2Configs();
startAVD(); startAVD();
} }
/**
* This method replaces the default configurations provided in the resources to the WSoO2 AVD created
*/
private void copyDefaultWSO2Configs() {
String configFileLocation = workingDirectory + Constants.WSO2_CONFIG_LOCATION;
String wso2ConfigFile = userHome + File.separator + ".android" + File.separator + "avd" + File.separator
+ Constants.WSO2_AVD_NAME + ".avd" + File.separator + "config.ini";
try {
Files.copy(Paths.get(configFileLocation), Paths.get(wso2ConfigFile), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException ignored) {
System.out.println("Failed to have WSO2 default AVD configurations");
}
}
/** /**
* This method runs the Android Emulator for the name specified by deviceId. * This method runs the Android Emulator for the name specified by deviceId.
* *
* @param deviceId String name of the device. * @param deviceId String name of the device.
* @throws IOException process start throws if an I/O error occurs.
*/ */
private void runEmulator(String deviceId) throws IOException { private void runEmulator(String deviceId) {
// mac os and windows needs hardware_Accelerated_execution_Manager
if (osSuffix.equals(Constants.MAC_OS) || osSuffix.equals(Constants.WINDOWS_OS)) { if (osSuffix.equals(Constants.MAC_OS) || osSuffix.equals(Constants.WINDOWS_OS)) {
installHAXM(); installHAXM();
} }
@ -417,56 +376,57 @@ public class TryIt {
*/ */
private void checkBuildTools() { private void checkBuildTools() {
File buildTools = new File(androidSdkHome + File.separator + "build-tools" File buildTools = new File(androidSdkHome + File.separator + "build-tools"
+ File.separator + "25.0.2"); + File.separator + System.getProperty(Constants.BUILD_TOOLS_VERSION));
if (!buildTools.exists()) { if (!buildTools.exists()) {
getTools(System.getProperty(Constants.BUILD_TOOL_URL), "_Android-build-tool.zip"); getTools(System.getProperty(Constants.BUILD_TOOL_URL), "_Android-build-tool.zip");
File buildTool = new File(androidSdkHome + File.separator + "android-7.1.1"); File buildTool = new File(androidSdkHome + File.separator + System.getProperty(Constants.DOWNLOADED_BUILD_TOOL_NAME));
//noinspection ResultOfMethodCallIgnored //noinspection ResultOfMethodCallIgnored
new File(androidSdkHome + File.separator + "build-tools").mkdir(); new File(androidSdkHome + File.separator + "build-tools").mkdir();
//noinspection ResultOfMethodCallIgnored //noinspection ResultOfMethodCallIgnored
buildTool.renameTo(new File(androidSdkHome + File.separator + "build-tools" buildTool.renameTo(new File(androidSdkHome + File.separator + "build-tools"
+ File.separator + "25.0.2")); + File.separator + System.getProperty(Constants.BUILD_TOOLS_VERSION)));
} }
} }
/** /**
* This method halts the system until the emulator is fully booted * This method halts the system until the emulator is fully booted
* if boot process is not completed successfully, rest of the tasks won't be continued. * if boot process is not completed successfully, rest of the tasks won't be continued.
*
* @throws IOException process throws if an I/O error occurs.
*/ */
private void checkEmulatorBoot() throws IOException { private void checkEmulatorBoot() {
BufferedReader reader; BufferedReader reader = null;
String readLine; String readLine;
Boolean sysBootComplete = false; Boolean sysBootComplete = false;
do { do {
ProcessBuilder systemBoot = new ProcessBuilder(adbLocation, "shell", "getprop", ProcessBuilder systemBoot = new ProcessBuilder(adbLocation, "shell", "getprop",
"sys.boot_completed"); "sys.boot_completed");
Process systemBootProcess = systemBoot.start();
try { try {
Process systemBootProcess = systemBoot.start();
systemBootProcess.waitFor(); systemBootProcess.waitFor();
} catch (InterruptedException e) { reader = new BufferedReader(new InputStreamReader(systemBootProcess.getInputStream(),
handleException("System boot process interuppted", e); StandardCharsets.UTF_8));
} while ((readLine = reader.readLine()) != null) {
reader = new BufferedReader(new InputStreamReader(systemBootProcess.getInputStream(), // if boot process is success the process gives 1 as output
StandardCharsets.UTF_8)); if (readLine.contains("1")) {
while ((readLine = reader.readLine()) != null) { sysBootComplete = true;
// if boot process is success the process gives 1 as output }
if (readLine.contains("1")) {
sysBootComplete = true;
} }
} System.out.print(".");
System.out.print(".");
try {
Thread.sleep(1000); Thread.sleep(1000);
} catch (IOException e) {
System.out.println("Unable to check boot process");
} catch (InterruptedException ignored) { } catch (InterruptedException ignored) {
// ignored //interruption in main thread
} finally { } finally {
reader.close(); try {
if (reader != null) {
reader.close();
}
} catch (IOException ignored) {
}
} }
} while (!sysBootComplete); } while (!sysBootComplete);
System.out.println(); System.out.println();
@ -476,29 +436,28 @@ public class TryIt {
* This method gets the Android SDK location if available and sets the SDK path else downloads the SDK. * This method gets the Android SDK location if available and sets the SDK path else downloads the SDK.
*/ */
private void setAndroidSDK() { private void setAndroidSDK() {
sdkLocationFile = new File("sdkLocation"); sdkConfigFile = new File("sdkLocation");
if (!(sdkLocationFile.exists() && !sdkLocationFile.isDirectory())) { if (!(sdkConfigFile.exists() && !sdkConfigFile.isDirectory())) {
Scanner read = new Scanner(System.in, "UTF-8"); Scanner read = new Scanner(System.in, "UTF-8");
System.out.println("Do you have an Android SDK installed on your computer (y/N)?: "); System.out.print("Do you have an Android SDK installed on your computer (y/N) ? : ");
String response = read.next().toLowerCase(); String response = read.next().toLowerCase();
if (response.matches("y")) { if (response.matches("y")) {
setSDKPath(); setSDKPath();
} else { } else {
getAndroidSDK(); getAndroidSDK();
} }
} } else {
// writes the Android SDK location to sdkLocationFile file Scanner scanner = null;
Scanner scanner = null; try {
try { scanner = new Scanner(sdkConfigFile, "UTF-8");
scanner = new Scanner(sdkLocationFile, "UTF-8"); androidSdkHome = scanner.useDelimiter("\\Z").next();
androidSdkHome = scanner.useDelimiter("\\Z").next(); } catch (FileNotFoundException ignored) {
} catch (FileNotFoundException ignored) { // already checked
System.out.println("sdkLocation file not found"); } finally {
// already written to androidSdkHome if (scanner != null) {
} finally { scanner.close();
if (scanner != null) { }
scanner.close();
} }
} }
@ -518,103 +477,121 @@ public class TryIt {
private void handleException(String message, Exception ex) { private void handleException(String message, Exception ex) {
System.out.println(message); System.out.println(message);
ex.printStackTrace(); ex.printStackTrace();
System.exit(0); System.exit(1);
} }
/** /**
* This method check for the android agent in the specified AVD and installs it if not available. * This method check for the android agent in the specified AVD and installs it if not available.
* *
* @return package name and act name. * @return package name and act name.
* @throws IOException process throws if an I/O error occurs.
*/ */
private String[] checkForAgent() throws IOException { private String[] checkForAgent() {
String apkFileLocation = workingDirectory + File.separator + "resources" + File.separator + "android-agent.apk"; String pkg = null;
String aaptLocation = androidSdkHome + File.separator + "build-tools" + File.separator + "25.0.2" String activity = null;
String readLine;
BufferedReader reader = null;
String apkFileLocation = workingDirectory + Constants.APK_LOCATION;
String aaptLocation = androidSdkHome + File.separator + "build-tools" + File.separator + System.getProperty(Constants.BUILD_TOOLS_VERSION)
+ File.separator + "aapt"; + File.separator + "aapt";
if (osSuffix.equals(Constants.WINDOWS_OS)) { if (osSuffix.equals(Constants.WINDOWS_OS)) {
aaptLocation += Constants.WINDOWS_EXTENSION_EXE; aaptLocation += Constants.WINDOWS_EXTENSION_EXE;
} }
setExecutePermission(aaptLocation); setExecutePermission(aaptLocation);
//process to get the name of package and launchable-activity available in android agent apk file
ProcessBuilder badgingApkFileProcessBuilder = new ProcessBuilder(aaptLocation, "d", "badging", ProcessBuilder badgingApkFileProcessBuilder = new ProcessBuilder(aaptLocation, "d", "badging",
apkFileLocation); apkFileLocation);
Process badgingApkFileProcess = badgingApkFileProcessBuilder.start();
String pkg = null;
String activity = null;
Boolean hasAgent = false;
String readLine;
BufferedReader reader = null;
try { try {
Process badgingApkFileProcess = badgingApkFileProcessBuilder.start();
reader = new BufferedReader(new InputStreamReader(badgingApkFileProcess.getInputStream(), reader = new BufferedReader(new InputStreamReader(badgingApkFileProcess.getInputStream(),
StandardCharsets.UTF_8)); StandardCharsets.UTF_8));
while ((readLine = reader.readLine()) != null) { while ((readLine = reader.readLine()) != null) {
if (readLine.contains("package")) { if (readLine.contains("package")) {
pkg = readLine.substring(readLine.indexOf(Constants.NAME) + 6).substring(0, Pattern pattern = Pattern.compile("'(.*?)'");
readLine.substring(readLine.indexOf(Constants.NAME) Matcher matcher = pattern.matcher(readLine);
+ 6).indexOf("'")); if (matcher.find()) {
pkg = matcher.group(1);
}
} }
if (readLine.contains("launchable-activity")) { if (readLine.contains("launchable-activity")) {
activity = readLine.substring(readLine.indexOf(Constants.NAME) + 6).substring(0, Pattern pattern = Pattern.compile("'(.*?)'");
readLine.substring(readLine.indexOf(Constants.NAME) Matcher matcher = pattern.matcher(readLine);
+ 6).indexOf("'")); if (matcher.find()) {
activity = matcher.group(1);
}
} }
} }
} catch (IOException ignored) {
//
} finally { } finally {
if (reader != null) { if (reader != null) {
reader.close(); try {
reader.close();
} catch (IOException ignored) {
//
}
} }
} }
if (!checkForPackage(pkg)) {
installAgent();
}
return new String[]{pkg, activity};
}
/**
* This method check whether the package is available in the AVD.
*
* @param pkg - name og package to check for.
* @return - available or not.
*/
private boolean checkForPackage(String pkg) {
String readLine;
BufferedReader reader = null;
Boolean hasAgent = false;
ProcessBuilder listPackages = new ProcessBuilder(adbLocation, "shell", "pm", "list", "packages"); ProcessBuilder listPackages = new ProcessBuilder(adbLocation, "shell", "pm", "list", "packages");
Process listPackagesProcess = listPackages.start();
try {
listPackagesProcess.waitFor();
} catch (InterruptedException e) {
System.out.println("Unable to read available packages in the current AVD");
}
try { try {
Process listPackagesProcess = listPackages.start();
listPackagesProcess.waitFor();
reader = new BufferedReader(new InputStreamReader(listPackagesProcess.getInputStream(), reader = new BufferedReader(new InputStreamReader(listPackagesProcess.getInputStream(),
StandardCharsets.UTF_8)); StandardCharsets.UTF_8));
while ((readLine = reader.readLine()) != null) { while ((readLine = reader.readLine()) != null) {
if (readLine.contains("package:" + pkg)) { if (readLine.contains("package:" + pkg)) {
hasAgent = true; hasAgent = true;
} }
} }
} catch (IOException | InterruptedException ignored) {
//TODO
} finally { } finally {
reader.close(); try {
} if (reader != null) {
reader.close();
}
if (!hasAgent) { } catch (IOException ignored) {
installAgent(); //TODO
}
} }
return new String[]{pkg, activity}; return hasAgent;
} }
/** /**
* This method installs the Android Agent ( WSO2 iot agent ). * This method installs the Android Agent ( WSO2 iot agent ).
*
* @throws IOException process start throws if an I/O error occurs.
*/ */
private void installAgent() throws IOException { private void installAgent() {
String androidAgentLocation = workingDirectory + File.separator + "resources" + File.separator String androidAgentLocation = workingDirectory + Constants.APK_LOCATION;
+ "android-agent.apk";
System.out.println("Installing agent ..."); System.out.println("Installing agent ...");
ProcessBuilder installAgentProcessBuilder = new ProcessBuilder(adbLocation, "install", ProcessBuilder installAgentProcessBuilder = new ProcessBuilder(adbLocation, "install",
androidAgentLocation); androidAgentLocation);
Process installAgentProcess = installAgentProcessBuilder.start();
try { try {
Process installAgentProcess = installAgentProcessBuilder.start();
installAgentProcess.waitFor(); installAgentProcess.waitFor();
} catch (InterruptedException e) { } catch (Exception e) {
System.out.println("WSO2 Agent installation failed"); System.out.println("WSO2 Agent installation failed");
installAgent(); Scanner read = new Scanner(System.in, "UTF-8");
System.out.println("Do you want to install agent again (Y/N) ? ");
if (read.next().toLowerCase().matches("y")) {
installAgent();
}
} }
} }
@ -622,20 +599,20 @@ public class TryIt {
* This method starts the package (wso2.iot.agent). * This method starts the package (wso2.iot.agent).
* *
* @param agents package name and launchable activity name. * @param agents package name and launchable activity name.
* @throws IOException process throws if an I/O error occurs.
*/ */
private void startPackage(String[] agents) throws IOException { private void startPackage(String[] agents) {
String pkg = agents[0]; String pkg = agents[0];
String activity = agents[1]; String activity = agents[1];
ProcessBuilder pkgStartProcessBuilder = new ProcessBuilder(adbLocation, "shell", "am", "start", ProcessBuilder pkgStartProcessBuilder = new ProcessBuilder(adbLocation, "shell", "am", "start",
"-n", pkg + "/" + activity); "-n", pkg + "/" + activity);
Process pkgStartProcess = pkgStartProcessBuilder.start();
try { try {
Process pkgStartProcess = pkgStartProcessBuilder.start();
pkgStartProcess.waitFor(); pkgStartProcess.waitFor();
} catch (InterruptedException e) { } catch (InterruptedException ignored) {
handleException("Package start process interuptted", e); // TODO
} catch (IOException e) {
handleException("Unable to start WSO2 package", e);
} }
} }
@ -643,15 +620,17 @@ public class TryIt {
* This method checks for the availability of Android Platform in SDK and if not available downloads it. * This method checks for the availability of Android Platform in SDK and if not available downloads it.
*/ */
private void checkForPlatform() { private void checkForPlatform() {
File platform = new File(androidSdkHome + File.separator + "platforms" + File.separator + "android-23"); File platform = new File(androidSdkHome + File.separator + "platforms" + File.separator
+ System.getProperty(Constants.TARGET_VERSION));
if (!platform.isDirectory()) { if (!platform.isDirectory()) {
getTools(System.getProperty(Constants.PLATFORM_URL), "_Android-platforms.zip"); getTools(System.getProperty(Constants.PLATFORM_URL), "_Android-platforms.zip");
//noinspection ResultOfMethodCallIgnored //noinspection ResultOfMethodCallIgnored
new File(androidSdkHome + File.separator + "platforms").mkdir(); new File(androidSdkHome + File.separator + "platforms").mkdir();
//noinspection ResultOfMethodCallIgnored //noinspection ResultOfMethodCallIgnored
new File(androidSdkHome + File.separator + "android-6.0").renameTo(new File(androidSdkHome new File(androidSdkHome + File.separator + System.getProperty(Constants.DOWNLOADED_PLATFORM_NAME)).
+ File.separator + "platforms" + File.separator + "android-23")); renameTo(new File(androidSdkHome + File.separator + "platforms"
+ File.separator + System.getProperty(Constants.TARGET_VERSION)));
} }
} }
@ -660,17 +639,17 @@ public class TryIt {
*/ */
private void checkForSystemImages() { private void checkForSystemImages() {
File systemImages = new File(androidSdkHome + File.separator + "system-images" File systemImages = new File(androidSdkHome + File.separator + "system-images"
+ File.separator + "android-23" + File.separator + "default"); + File.separator + System.getProperty(Constants.TARGET_VERSION) + File.separator + "default");
if (!systemImages.isDirectory()) { if (!systemImages.isDirectory()) {
getTools(System.getProperty(Constants.SYSTEM_IMAGE_URL), "_sys-images.zip"); getTools(System.getProperty(Constants.SYSTEM_IMAGE_URL), "_sys-images.zip");
//noinspection ResultOfMethodCallIgnored //noinspection ResultOfMethodCallIgnored
new File(androidSdkHome + File.separator + "system-images" + File.separator new File(androidSdkHome + File.separator + "system-images" + File.separator
+ "android-23" + File.separator + "default").mkdirs(); + System.getProperty(Constants.TARGET_VERSION) + File.separator + "default").mkdirs();
//noinspection ResultOfMethodCallIgnored //noinspection ResultOfMethodCallIgnored
new File(androidSdkHome + File.separator + "x86").renameTo(new File(androidSdkHome new File(androidSdkHome + File.separator + System.getProperty(Constants.OS_TARGET)).renameTo(new File(androidSdkHome
+ File.separator + "system-images" + File.separator + "android-23" + File.separator + File.separator + "system-images" + File.separator + System.getProperty(Constants.TARGET_VERSION) + File.separator
+ "default" + File.separator + "x86")); + "default" + File.separator + System.getProperty(Constants.OS_TARGET)));
} }
} }
@ -683,22 +662,21 @@ public class TryIt {
+ File.separator + "Hardware_Accelerated_Execution_Manager"; + File.separator + "Hardware_Accelerated_Execution_Manager";
if (!new File(haxmLocation).isDirectory()) { if (!new File(haxmLocation).isDirectory()) {
System.out.println("Downloading intel HAXM..."); //System.out.println("Downloading intel HAXM...");
new File(haxmLocation).mkdirs(); new File(haxmLocation).mkdirs();
String folderName = "_haxm.zip"; String folderName = "_haxm.zip";
getTools(Constants.HAXM_URL, haxmLocation + File.separator
downloadArtifacts(System.getProperty(Constants.HAXM_URL), haxmLocation + File.separator
+ folderName); + folderName);
System.out.println("Configuring HAXM..."); // downloadArtifacts(System.getProperty(Constants.HAXM_URL), haxmLocation + File.separator
extractFolder(haxmLocation + File.separator + folderName); // + folderName);
// System.out.println("Configuring HAXM...");
// extractFolder(haxmLocation + File.separator + folderName);
String haxmInstaller = haxmLocation + File.separator + "silent_install"; String haxmInstaller = haxmLocation + File.separator + "silent_install";
if (osSuffix.equals(Constants.WINDOWS_OS)) { if (osSuffix.equals(Constants.WINDOWS_OS)) {
haxmInstaller += Constants.WINDOWS_EXTENSION_BAT; haxmInstaller += Constants.WINDOWS_EXTENSION_BAT;
} else { } else {
haxmInstaller += ".sh"; haxmInstaller += Constants.MAC_HAXM_EXTENSION;
} }
setExecutePermission(haxmInstaller); setExecutePermission(haxmInstaller);
@ -707,17 +685,10 @@ public class TryIt {
processBuilder.directory(new File(haxmLocation)); processBuilder.directory(new File(haxmLocation));
processBuilder.redirectInput(ProcessBuilder.Redirect.INHERIT); processBuilder.redirectInput(ProcessBuilder.Redirect.INHERIT);
processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT); processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
Process process = null;
try {
process = processBuilder.start();
} catch (IOException e) {
System.out.println("HAXM installation failed, install HAXM and try again");
}
try { try {
if (process != null) { Process process = processBuilder.start();
process.waitFor(); process.waitFor();
} } catch (IOException | InterruptedException e) {
} catch (InterruptedException e) {
System.out.println("HAXM installation failed, install HAXM and try again"); System.out.println("HAXM installation failed, install HAXM and try again");
} }
System.out.println("Please restart your machine and run again."); System.out.println("Please restart your machine and run again.");
@ -736,7 +707,7 @@ public class TryIt {
switch (osSuffix) { switch (osSuffix) {
case Constants.MAC_OS: case Constants.MAC_OS:
qemuSystemFileLocation += "darwin" + "-x86_64" + File.separator + "qemu-system-i386"; qemuSystemFileLocation += Constants.MAC_DARWIN + "-x86_64" + File.separator + "qemu-system-i386";
break; break;
case Constants.WINDOWS_OS: case Constants.WINDOWS_OS:
qemuSystemFileLocation += osSuffix + "-x86_64" + File.separator + "qemu-system-i386.exe"; qemuSystemFileLocation += osSuffix + "-x86_64" + File.separator + "qemu-system-i386.exe";
@ -745,7 +716,6 @@ public class TryIt {
qemuSystemFileLocation += osSuffix + "-x86_64" + File.separator + "qemu-system-i386"; qemuSystemFileLocation += osSuffix + "-x86_64" + File.separator + "qemu-system-i386";
} }
killServer(); killServer();
setExecutePermission(qemuSystemFileLocation); setExecutePermission(qemuSystemFileLocation);
ExecutorService service = Executors.newSingleThreadExecutor(); ExecutorService service = Executors.newSingleThreadExecutor();
service.execute(new TryItEmulator(deviceId, emulatorLocation)); service.execute(new TryItEmulator(deviceId, emulatorLocation));
@ -757,7 +727,6 @@ public class TryIt {
private void killServer() { private void killServer() {
ProcessBuilder processBuilderKillServer = new ProcessBuilder(adbLocation, "kill-server"); ProcessBuilder processBuilderKillServer = new ProcessBuilder(adbLocation, "kill-server");
Process processKillServer = null; Process processKillServer = null;
try { try {
processKillServer = processBuilderKillServer.start(); processKillServer = processBuilderKillServer.start();
} catch (IOException ignored) { } catch (IOException ignored) {
@ -785,8 +754,8 @@ public class TryIt {
System.out.print("."); System.out.print(".");
try { try {
Thread.sleep(1000); Thread.sleep(1000);
} catch (InterruptedException e) { } catch (InterruptedException ignored) {
handleException("Virtual device not loaded properly, process interuptted", e); //
} }
} }
System.out.println(); System.out.println();

Loading…
Cancel
Save