Java tutorial
/* * Copyright 2014 Electroteque Media * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the Lesser GPL * along with this program. If not, see <http://www.gnu.org/licenses/> */ package org.electroteque.jscrambler; import java.util.Timer; import java.util.TimerTask; import java.util.HashMap; //import java.util.logging.Level; //import java.util.logging.Logger; import com.auditmark.jscrambler.client.JScrambler; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Task; import org.apache.commons.lang3.StringUtils; import org.json.*; /** * Setup a new project for building using the JSrambler API client * * @author Dan Rossi <electroteque@gmail.com> */ public class JScramblerProjectTask extends Task { //private Logger _logger = Logger.getLogger("org.electroteque.jscrambler"); private HashMap params = new HashMap(); private String accessKey; private String secretKey; // private String[] files; protected String projectId; protected JScrambler jscrambler; protected String apiUrl = "api.jscrambler.com"; protected int port = 443; /** * Set a list of files seperated by whitespace * @param value */ public void setFiles(String value) { params.put("files", value.split(" ")); //files[files.length - 1] = value; } /** * Set the access key * @param value */ public void setAccessKey(String value) { accessKey = value; } /** * Set the secret key * @param value */ public void setSecretKey(String value) { secretKey = value; } public void setDicCompression(Boolean value) { if (value) params.put("dictionary_compression", "%DEFAULT%"); } public void setSelfDefending(Boolean value) { if (value) params.put("self_defending", "%DEFAULT%"); } public void setConstantFolding(Boolean value) { if (value) params.put("constant_folding", "%DEFAULT%"); } public void setDeadCode(Boolean value) { if (value) params.put("dead_code", "%DEFAULT%"); } public void setDeadCodeElimination(Boolean value) { if (value) params.put("dead_code_elimination", "%DEFAULT%"); } public void setDomains(String value) { if (!value.equals("")) { value = "*." + value.replaceAll(" ", " *.") + " localhost"; String[] domains = value.split(" "); params.put("domain_lock", StringUtils.join(domains, ";")); } } public void setDotNotation(Boolean value) { if (value) params.put("dot_notation_elimination", "%DEFAULT%"); } public void setEncodeAll(Boolean value) { if (value) params.put("encode_all", "%DEFAULT%"); } public void setExceptionsList(String value) { if (!value.equals("")) params.put("exceptions_list", value); } public void setExpiry(String value) { if (!value.equals("")) params.put("expiration_date", value); } public void setFunctionOutlining(Boolean value) { if (value) params.put("function_outlining", "%DEFAULT%"); } public void setFunctionReorder(Boolean value) { if (value) params.put("function_reorder", "%DEFAULT%"); } public void setLiteralHooking(String value) { if (!value.equals("")) params.put("literal_hooking", value); } public void setLiteralDuplicates(Boolean value) { if (value) params.put("literal_duplicates", "%DEFAULT%"); } public void setMemberEnumeration(Boolean value) { if (value) params.put("member_enumeration", "%DEFAULT%"); } public void setMode(String value) { params.put("mode", value); } public void setNamePrefix(String value) { params.put("name_prefix", value); } public void setRenameAll(Boolean value) { if (value) params.put("rename_all", "%DEFAULT%"); } public void setRenameLocal(Boolean value) { if (value) params.put("rename_local", "%DEFAULT%"); } public void setStringSplitting(String value) { params.put("string_splitting", value); } public void setWhitespace(Boolean value) { if (value) params.put("whitespace", "%DEFAULT%"); } /** * run the api comment * @throws BuildException */ public void execute() throws BuildException { try { //request the new build from the client given by the selected params jscrambler = new JScrambler(accessKey, secretKey, apiUrl, port); String result = jscrambler.post("/code.json", params); System.out.println(result); System.out.println(params); JSONObject json; try { json = new JSONObject(result); } catch (org.json.JSONException ignore) { throw new BuildException("Failed to decode json GET response."); } if (!json.has("id") || json.has("error")) { throw new BuildException("Something went wrong.\n" + result); } projectId = json.getString("id"); Timer timer = new Timer("ProjectStatus"); ProjectCheckTask t = new ProjectCheckTask(); //store the project id into an ant property for later use this.getProject().setNewProperty("jscrambler.projectId", projectId); //scheduler to check if the build has finished timer.schedule(t, 0, 2000); } catch (Exception e) { throw new BuildException(e); } } class ProjectCheckTask extends TimerTask { public void run() { String projectResult = (String) jscrambler.get("/code/" + projectId + ".json"); System.out.println("RESULT " + projectResult); JSONObject project; try { project = new JSONObject(projectResult); } catch (org.json.JSONException ignore) { throw new BuildException("Failed to decode json GET response."); } try { if (project.has("error_id") && project.get("error_id").toString().compareTo("null") != 0 && project.getInt("error_id") != 0) { this.cancel(); System.out.println(project.getString("error_message")); } } catch (org.json.JSONException ignore) { this.cancel(); System.out.println("Failed to check project status."); } //check if the project has finished building try { if (project.has("finished_at") && project.get("finished_at").toString().compareTo("null") != 0) { System.out.println("FINISHED"); getProject().setNewProperty("jscrambler.finished", "true"); this.cancel(); } else { System.out.println("NOT FINISHED"); } } catch (Exception e) { this.cancel(); System.out.println("ERROR" + e.getMessage()); } } } }