{"action":"created","comment":{"url":"https://api.github.com/repos/wso2/product-as/pulls/comments/25488131","id":25488131,"diff_hunk":"@@ -0,0 +1,280 @@\n+package org.wso2.appserver.integration.common.utils;\n+\n+import org.apache.commons.lang3.ArrayUtils;\n+import org.apache.commons.logging.Log;\n+import org.apache.commons.logging.LogFactory;\n+import org.wso2.carbon.automation.engine.FrameworkConstants;\n+import org.wso2.carbon.automation.engine.context.AutomationContext;\n+import org.wso2.carbon.automation.extensions.ExtensionConstants;\n+import org.wso2.carbon.automation.extensions.servers.utils.ClientConnectionUtil;\n+import org.wso2.carbon.automation.extensions.servers.utils.InputStreamHandler;\n+import org.wso2.carbon.automation.extensions.servers.utils.ServerLogReader;\n+import org.wso2.carbon.automation.engine.frameworkutils.enums.OperatingSystems;\n+import org.wso2.carbon.integration.common.admin.client.LogViewerClient;\n+import org.wso2.carbon.integration.common.admin.client.ServerAdminClient;\n+import org.wso2.carbon.logging.view.stub.LogViewerLogViewerException;\n+import org.wso2.carbon.logging.view.stub.types.carbon.LogEvent;\n+\n+import javax.xml.xpath.XPathExpressionException;\n+import java.io.BufferedReader;\n+import java.io.File;\n+import java.io.IOException;\n+import java.io.InputStreamReader;\n+import java.lang.Thread;\n+import java.rmi.RemoteException;\n+\n+/**\n+ * This class has the method which using by carbon tools test cases\n+ */\n+\n+public class CarbonCommandToolsUtil {\n+\n+    private static final Log log = LogFactory.getLog(CarbonCommandToolsUtil.class);\n+    private static int TIMEOUT = 180 * 1000; // Max time to wait\n+    private static final long DEFAULT_START_STOP_WAIT_MS = 1000 * 60 * 2;\n+    private static final String SERVER_STARTUP_MESSAGE = \"Mgt Console URL\";\n+    private static ServerLogReader inputStreamHandler;\n+\n+    /**\n+     * This method is for start a as server\n+     *\n+     * @param carbonHome        - carbon home\n+     * @param portOffset        - port offset\n+     * @param automationContext - AutomationContext\n+     * @param parameters        - server startup arguments as an string array\n+     * @return Process of the startup execution\n+     * @throws Exception - Error occurred when starting the the server\n+     */\n+    public static Process startServerUsingCarbonHome(String carbonHome, int portOffset,\n+                                                     AutomationContext automationContext,\n+                                                     String[] parameters) throws Exception {\n+\n+        Process tempProcess;\n+        String scriptName = \"wso2server\";\n+        File commandDir = new File(carbonHome);\n+        String[] cmdArray;\n+        log.info(\"Starting server............. \");\n+        if (CarbonCommandToolsUtil.getCurrentOperatingSystem().\n+                contains(OperatingSystems.WINDOWS.name().toLowerCase())) {\n+            commandDir = new File(carbonHome + File.separator + \"bin\");\n+            cmdArray = new String[]{\"cmd.exe\", \"/c\", scriptName + \".bat\", \"-DportOffset=\" + portOffset};\n+            cmdArray = mergePropertiesToCommandArray(parameters, cmdArray);\n+            tempProcess = Runtime.getRuntime().exec(cmdArray, null, commandDir);\n+        } else {\n+            cmdArray = new String[]{\"sh\", \"bin/\" + scriptName + \".sh\", \"-DportOffset=\" + portOffset};\n+            cmdArray = mergePropertiesToCommandArray(parameters, cmdArray);\n+            tempProcess = Runtime.getRuntime().exec(cmdArray, null, commandDir);\n+        }\n+        InputStreamHandler errorStreamHandler =\n+                new InputStreamHandler(\"errorStream\", tempProcess.getErrorStream());\n+        inputStreamHandler = new ServerLogReader(\"inputStream\", tempProcess.getInputStream());\n+        // start the stream readers\n+        inputStreamHandler.start();\n+        errorStreamHandler.start();\n+        ClientConnectionUtil.waitForPort(Integer.parseInt(FrameworkConstants.SERVER_DEFAULT_HTTPS_PORT) +\n+                                         portOffset, DEFAULT_START_STOP_WAIT_MS, false,\n+                                         automationContext.getInstance().getHosts().get(\"default\"));\n+        //wait until Mgt console url printed.\n+        long time = System.currentTimeMillis() + DEFAULT_START_STOP_WAIT_MS;\n+        while (!inputStreamHandler.getOutput().contains(SERVER_STARTUP_MESSAGE) &&\n+               System.currentTimeMillis() < time) {\n+            // wait until server startup is completed\n+        }\n+        ClientConnectionUtil.waitForLogin(automationContext);\n+        log.info(\"Server started successfully.\");\n+        return tempProcess;\n+    }\n+\n+    /**\n+     * This method is to shutdown a server\n+     *\n+     * @param portOffset        - port offset\n+     * @param automationContext - AutomationContext\n+     * @throws XPathExpressionException - Error when getting data from automation.xml\n+     * @throws RemoteException          - Error when shutdown the server\n+     */\n+    public static void serverShutdown(int portOffset,\n+                                      AutomationContext automationContext)\n+            throws XPathExpressionException, RemoteException, InterruptedException {\n+        long time = System.currentTimeMillis() + DEFAULT_START_STOP_WAIT_MS;\n+        log.info(\"Shutting down server..\");\n+        boolean logOutSuccess = false;\n+        if (ClientConnectionUtil.isPortOpen(Integer.parseInt(ExtensionConstants.\n+                                                                     SERVER_DEFAULT_HTTPS_PORT))) {\n+\n+            int httpsPort = Integer.parseInt(FrameworkConstants.SERVER_DEFAULT_HTTPS_PORT) + portOffset;\n+            String url = automationContext.getContextUrls().getBackEndUrl();\n+            String backendURL = url.replaceAll(\"(:\\\\d+)\", \":\" + httpsPort);\n+\n+            ServerAdminClient serverAdminServiceClient = new ServerAdminClient(backendURL,\n+                              automationContext.getContextTenant().getTenantAdmin().getUserName(),\n+                              automationContext.getContextTenant().getTenantAdmin().getPassword());\n+\n+            serverAdminServiceClient.shutdown();\n+\n+            while (System.currentTimeMillis() < time && !logOutSuccess) {\n+                logOutSuccess = isServerDown(automationContext, portOffset);\n+                // wait until server shutdown is completed\n+            }\n+            log.info(\"Server stopped successfully...\");\n+        }\n+\n+    }\n+\n+    /**\n+     * This method is to merge two arrays together\n+     *\n+     * @param parameters - Server startup arguments\n+     * @param cmdArray   - Server startup command\n+     * @return - merged array\n+     */\n+    private static String[] mergePropertiesToCommandArray(String[] parameters, String[] cmdArray) {\n+        if (parameters != null && cmdArray != null) {\n+            cmdArray = ArrayUtils.addAll(cmdArray, parameters);\n+        }\n+        return cmdArray;\n+    }\n+\n+    /**\n+     * This method is to check whether server is down or not\n+     *\n+     * @param automationContext - AutomationContext\n+     * @param portOffset        - port offset\n+     * @return boolean - if server is down true : else false\n+     * @throws XPathExpressionException\n+     */\n+    public static boolean isServerDown(AutomationContext automationContext, int portOffset)\n+            throws XPathExpressionException, InterruptedException {\n+        boolean isPortOpen = true;\n+        long startTime = System.currentTimeMillis();\n+        // Looping the isPortOpen method, waiting for a while  to check the server is down or not\n+        while (isPortOpen && (System.currentTimeMillis() - startTime) < TIMEOUT) {\n+            isPortOpen = ClientConnectionUtil.isPortOpen(","path":"modules/integration/tests-common/integration-test-utils/src/main/java/org/wso2/appserver/integration/common/utils/CarbonCommandToolsUtil.java","position":152,"original_position":152,"commit_id":"16f8c25e4ccabdd0c740de69b9adcd71703fba75","original_commit_id":"16f8c25e4ccabdd0c740de69b9adcd71703fba75","user":{"login":"krishanthasamaraweera","id":6601948,"avatar_url":"https://avatars.githubusercontent.com/u/6601948?v=3","gravatar_id":"","url":"https://api.github.com/users/krishanthasamaraweera","html_url":"https://github.com/krishanthasamaraweera","followers_url":"https://api.github.com/users/krishanthasamaraweera/followers","following_url":"https://api.github.com/users/krishanthasamaraweera/following{/other_user}","gists_url":"https://api.github.com/users/krishanthasamaraweera/gists{/gist_id}","starred_url":"https://api.github.com/users/krishanthasamaraweera/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/krishanthasamaraweera/subscriptions","organizations_url":"https://api.github.com/users/krishanthasamaraweera/orgs","repos_url":"https://api.github.com/users/krishanthasamaraweera/repos","events_url":"https://api.github.com/users/krishanthasamaraweera/events{/privacy}","received_events_url":"https://api.github.com/users/krishanthasamaraweera/received_events","type":"User","site_admin":false},"body":"reformat the code properly","created_at":"2015-02-27T05:08:37Z","updated_at":"2015-02-27T05:08:37Z","html_url":"https://github.com/wso2/product-as/pull/47#discussion_r25488131","pull_request_url":"https://api.github.com/repos/wso2/product-as/pulls/47","_links":{"self":{"href":"https://api.github.com/repos/wso2/product-as/pulls/comments/25488131"},"html":{"href":"https://github.com/wso2/product-as/pull/47#discussion_r25488131"},"pull_request":{"href":"https://api.github.com/repos/wso2/product-as/pulls/47"}}},"pull_request":{"url":"https://api.github.com/repos/wso2/product-as/pulls/47","id":29367676,"html_url":"https://github.com/wso2/product-as/pull/47","diff_url":"https://github.com/wso2/product-as/pull/47.diff","patch_url":"https://github.com/wso2/product-as/pull/47.patch","issue_url":"https://api.github.com/repos/wso2/product-as/issues/47","number":47,"state":"open","locked":false,"title":"Carbon tools integration tests.","user":{"login":"irham0019","id":8790160,"avatar_url":"https://avatars.githubusercontent.com/u/8790160?v=3","gravatar_id":"","url":"https://api.github.com/users/irham0019","html_url":"https://github.com/irham0019","followers_url":"https://api.github.com/users/irham0019/followers","following_url":"https://api.github.com/users/irham0019/following{/other_user}","gists_url":"https://api.github.com/users/irham0019/gists{/gist_id}","starred_url":"https://api.github.com/users/irham0019/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/irham0019/subscriptions","organizations_url":"https://api.github.com/users/irham0019/orgs","repos_url":"https://api.github.com/users/irham0019/repos","events_url":"https://api.github.com/users/irham0019/events{/privacy}","received_events_url":"https://api.github.com/users/irham0019/received_events","type":"User","site_admin":false},"body":"This pull has carbon tools chpasswd.sh/chpasswd.bat,--cleanRegistry and -Dtenant.idle.time integration tests.Which need to be added to kernel level since they need additional functionalities added to AS.","created_at":"2015-02-16T11:54:42Z","updated_at":"2015-02-27T05:08:37Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f0826bdf8d6634acd7855b75146f965f30792ae6","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/wso2/product-as/pulls/47/commits","review_comments_url":"https://api.github.com/repos/wso2/product-as/pulls/47/comments","review_comment_url":"https://api.github.com/repos/wso2/product-as/pulls/comments{/number}","comments_url":"https://api.github.com/repos/wso2/product-as/issues/47/comments","statuses_url":"https://api.github.com/repos/wso2/product-as/statuses/16f8c25e4ccabdd0c740de69b9adcd71703fba75","head":{"label":"irham0019:master","ref":"master","sha":"16f8c25e4ccabdd0c740de69b9adcd71703fba75","user":{"login":"irham0019","id":8790160,"avatar_url":"https://avatars.githubusercontent.com/u/8790160?v=3","gravatar_id":"","url":"https://api.github.com/users/irham0019","html_url":"https://github.com/irham0019","followers_url":"https://api.github.com/users/irham0019/followers","following_url":"https://api.github.com/users/irham0019/following{/other_user}","gists_url":"https://api.github.com/users/irham0019/gists{/gist_id}","starred_url":"https://api.github.com/users/irham0019/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/irham0019/subscriptions","organizations_url":"https://api.github.com/users/irham0019/orgs","repos_url":"https://api.github.com/users/irham0019/repos","events_url":"https://api.github.com/users/irham0019/events{/privacy}","received_events_url":"https://api.github.com/users/irham0019/received_events","type":"User","site_admin":false},"repo":{"id":30786339,"name":"product-as","full_name":"irham0019/product-as","owner":{"login":"irham0019","id":8790160,"avatar_url":"https://avatars.githubusercontent.com/u/8790160?v=3","gravatar_id":"","url":"https://api.github.com/users/irham0019","html_url":"https://github.com/irham0019","followers_url":"https://api.github.com/users/irham0019/followers","following_url":"https://api.github.com/users/irham0019/following{/other_user}","gists_url":"https://api.github.com/users/irham0019/gists{/gist_id}","starred_url":"https://api.github.com/users/irham0019/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/irham0019/subscriptions","organizations_url":"https://api.github.com/users/irham0019/orgs","repos_url":"https://api.github.com/users/irham0019/repos","events_url":"https://api.github.com/users/irham0019/events{/privacy}","received_events_url":"https://api.github.com/users/irham0019/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/irham0019/product-as","description":"","fork":true,"url":"https://api.github.com/repos/irham0019/product-as","forks_url":"https://api.github.com/repos/irham0019/product-as/forks","keys_url":"https://api.github.com/repos/irham0019/product-as/keys{/key_id}","collaborators_url":"https://api.github.com/repos/irham0019/product-as/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/irham0019/product-as/teams","hooks_url":"https://api.github.com/repos/irham0019/product-as/hooks","issue_events_url":"https://api.github.com/repos/irham0019/product-as/issues/events{/number}","events_url":"https://api.github.com/repos/irham0019/product-as/events","assignees_url":"https://api.github.com/repos/irham0019/product-as/assignees{/user}","branches_url":"https://api.github.com/repos/irham0019/product-as/branches{/branch}","tags_url":"https://api.github.com/repos/irham0019/product-as/tags","blobs_url":"https://api.github.com/repos/irham0019/product-as/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/irham0019/product-as/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/irham0019/product-as/git/refs{/sha}","trees_url":"https://api.github.com/repos/irham0019/product-as/git/trees{/sha}","statuses_url":"https://api.github.com/repos/irham0019/product-as/statuses/{sha}","languages_url":"https://api.github.com/repos/irham0019/product-as/languages","stargazers_url":"https://api.github.com/repos/irham0019/product-as/stargazers","contributors_url":"https://api.github.com/repos/irham0019/product-as/contributors","subscribers_url":"https://api.github.com/repos/irham0019/product-as/subscribers","subscription_url":"https://api.github.com/repos/irham0019/product-as/subscription","commits_url":"https://api.github.com/repos/irham0019/product-as/commits{/sha}","git_commits_url":"https://api.github.com/repos/irham0019/product-as/git/commits{/sha}","comments_url":"https://api.github.com/repos/irham0019/product-as/comments{/number}","issue_comment_url":"https://api.github.com/repos/irham0019/product-as/issues/comments{/number}","contents_url":"https://api.github.com/repos/irham0019/product-as/contents/{+path}","compare_url":"https://api.github.com/repos/irham0019/product-as/compare/{base}...{head}","merges_url":"https://api.github.com/repos/irham0019/product-as/merges","archive_url":"https://api.github.com/repos/irham0019/product-as/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/irham0019/product-as/downloads","issues_url":"https://api.github.com/repos/irham0019/product-as/issues{/number}","pulls_url":"https://api.github.com/repos/irham0019/product-as/pulls{/number}","milestones_url":"https://api.github.com/repos/irham0019/product-as/milestones{/number}","notifications_url":"https://api.github.com/repos/irham0019/product-as/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/irham0019/product-as/labels{/name}","releases_url":"https://api.github.com/repos/irham0019/product-as/releases{/id}","created_at":"2015-02-14T03:50:04Z","updated_at":"2015-02-27T04:34:11Z","pushed_at":"2015-02-27T04:34:11Z","git_url":"git://github.com/irham0019/product-as.git","ssh_url":"git@github.com:irham0019/product-as.git","clone_url":"https://github.com/irham0019/product-as.git","svn_url":"https://github.com/irham0019/product-as","homepage":null,"size":41988,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"wso2:master","ref":"master","sha":"4f8e802fdf563900e8f386afa2123ebd1b754a3d","user":{"login":"wso2","id":533043,"avatar_url":"https://avatars.githubusercontent.com/u/533043?v=3","gravatar_id":"","url":"https://api.github.com/users/wso2","html_url":"https://github.com/wso2","followers_url":"https://api.github.com/users/wso2/followers","following_url":"https://api.github.com/users/wso2/following{/other_user}","gists_url":"https://api.github.com/users/wso2/gists{/gist_id}","starred_url":"https://api.github.com/users/wso2/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wso2/subscriptions","organizations_url":"https://api.github.com/users/wso2/orgs","repos_url":"https://api.github.com/users/wso2/repos","events_url":"https://api.github.com/users/wso2/events{/privacy}","received_events_url":"https://api.github.com/users/wso2/received_events","type":"Organization","site_admin":false},"repo":{"id":16947368,"name":"product-as","full_name":"wso2/product-as","owner":{"login":"wso2","id":533043,"avatar_url":"https://avatars.githubusercontent.com/u/533043?v=3","gravatar_id":"","url":"https://api.github.com/users/wso2","html_url":"https://github.com/wso2","followers_url":"https://api.github.com/users/wso2/followers","following_url":"https://api.github.com/users/wso2/following{/other_user}","gists_url":"https://api.github.com/users/wso2/gists{/gist_id}","starred_url":"https://api.github.com/users/wso2/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wso2/subscriptions","organizations_url":"https://api.github.com/users/wso2/orgs","repos_url":"https://api.github.com/users/wso2/repos","events_url":"https://api.github.com/users/wso2/events{/privacy}","received_events_url":"https://api.github.com/users/wso2/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/wso2/product-as","description":"","fork":false,"url":"https://api.github.com/repos/wso2/product-as","forks_url":"https://api.github.com/repos/wso2/product-as/forks","keys_url":"https://api.github.com/repos/wso2/product-as/keys{/key_id}","collaborators_url":"https://api.github.com/repos/wso2/product-as/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/wso2/product-as/teams","hooks_url":"https://api.github.com/repos/wso2/product-as/hooks","issue_events_url":"https://api.github.com/repos/wso2/product-as/issues/events{/number}","events_url":"https://api.github.com/repos/wso2/product-as/events","assignees_url":"https://api.github.com/repos/wso2/product-as/assignees{/user}","branches_url":"https://api.github.com/repos/wso2/product-as/branches{/branch}","tags_url":"https://api.github.com/repos/wso2/product-as/tags","blobs_url":"https://api.github.com/repos/wso2/product-as/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/wso2/product-as/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/wso2/product-as/git/refs{/sha}","trees_url":"https://api.github.com/repos/wso2/product-as/git/trees{/sha}","statuses_url":"https://api.github.com/repos/wso2/product-as/statuses/{sha}","languages_url":"https://api.github.com/repos/wso2/product-as/languages","stargazers_url":"https://api.github.com/repos/wso2/product-as/stargazers","contributors_url":"https://api.github.com/repos/wso2/product-as/contributors","subscribers_url":"https://api.github.com/repos/wso2/product-as/subscribers","subscription_url":"https://api.github.com/repos/wso2/product-as/subscription","commits_url":"https://api.github.com/repos/wso2/product-as/commits{/sha}","git_commits_url":"https://api.github.com/repos/wso2/product-as/git/commits{/sha}","comments_url":"https://api.github.com/repos/wso2/product-as/comments{/number}","issue_comment_url":"https://api.github.com/repos/wso2/product-as/issues/comments{/number}","contents_url":"https://api.github.com/repos/wso2/product-as/contents/{+path}","compare_url":"https://api.github.com/repos/wso2/product-as/compare/{base}...{head}","merges_url":"https://api.github.com/repos/wso2/product-as/merges","archive_url":"https://api.github.com/repos/wso2/product-as/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/wso2/product-as/downloads","issues_url":"https://api.github.com/repos/wso2/product-as/issues{/number}","pulls_url":"https://api.github.com/repos/wso2/product-as/pulls{/number}","milestones_url":"https://api.github.com/repos/wso2/product-as/milestones{/number}","notifications_url":"https://api.github.com/repos/wso2/product-as/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/wso2/product-as/labels{/name}","releases_url":"https://api.github.com/repos/wso2/product-as/releases{/id}","created_at":"2014-02-18T11:57:09Z","updated_at":"2015-02-10T04:38:50Z","pushed_at":"2015-02-26T14:18:45Z","git_url":"git://github.com/wso2/product-as.git","ssh_url":"git@github.com:wso2/product-as.git","clone_url":"https://github.com/wso2/product-as.git","svn_url":"https://github.com/wso2/product-as","homepage":null,"size":43180,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":34,"mirror_url":null,"open_issues_count":12,"forks":34,"open_issues":12,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/wso2/product-as/pulls/47"},"html":{"href":"https://github.com/wso2/product-as/pull/47"},"issue":{"href":"https://api.github.com/repos/wso2/product-as/issues/47"},"comments":{"href":"https://api.github.com/repos/wso2/product-as/issues/47/comments"},"review_comments":{"href":"https://api.github.com/repos/wso2/product-as/pulls/47/comments"},"review_comment":{"href":"https://api.github.com/repos/wso2/product-as/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/wso2/product-as/pulls/47/commits"},"statuses":{"href":"https://api.github.com/repos/wso2/product-as/statuses/16f8c25e4ccabdd0c740de69b9adcd71703fba75"}}}}