Permalink
Browse files

git-svn-id: http://svn.lsd.ufcg.edu.br/repos/ourgrid/projects/ourgrid…

…/trunk@3788 92191d1a-1055-4c69-b98d-3dc93dc5eb86
  • Loading branch information...
1 parent ef93413 commit 542b909c675ec28c16269f3db34a3b637cd91bc3 patriciaam committed May 9, 2012
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
+ <display-name>Jersey</display-name>
+ <servlet>
+ <servlet-name>OurGridRESTAPI</servlet-name>
+ <servlet-class>
+ com.sun.jersey.spi.container.servlet.ServletContainer
+ </servlet-class>
+ <init-param>
+ <param-name>com.sun.jersey.config.property.packages</param-name>
+ <param-value>org.ourgrid.broker.api</param-value>
+ </init-param>
+ <load-on-startup>1</load-on-startup>
+ </servlet>
+ <servlet-mapping>
+ <servlet-name>OurGridRESTAPI</servlet-name>
+ <url-pattern>/*</url-pattern>
+ </servlet-mapping>
+</web-app>
@@ -0,0 +1,112 @@
+package client;
+
+import javax.ws.rs.core.MediaType;
+
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.api.client.WebResource;
+
+public class RESTClient {
+
+ public static void main(String[] args) {
+
+ Client c = Client.create();
+ WebResource r = c
+ .resource("http://localhost:8080/OurGridRESTAPI/broker");
+ System.out.println("===== Add Job =====");
+ addJob(r);
+ System.out.println("===== Get All Job Status =====");
+ getStatus(r);
+ System.out.println("===== Get Status by JobId =====");
+ getStatusById(r, "190");
+ System.out.println("===== Job Cancel =====");
+ cancel(r, "190");
+ System.out.println("===== Get All Job Status =====");
+ getStatus(r);
+ System.out.println("===== Clean by Id =====");
+ cleanById(r, "190");
+ System.out.println("===== Get All Job Status =====");
+ getStatus(r);
+ }
+
+ public static void getStatus(WebResource r) {
+
+ String jsonRes = r.path("/status/listall")
+ .accept(MediaType.APPLICATION_JSON).get(String.class);
+ System.out.println("All Job Status: " + jsonRes);
+
+ }
+
+ public static void getStatusById(WebResource r, String id) {
+
+ String response = r.path("/status/job_id/" + id).get(String.class);
+ System.out.println("Status by Id Response: " + response);
+ }
+
+ public static void cancel(WebResource r, String id) {
+
+ try {
+ ClientResponse response = r.path("/cancel/" + id).delete(
+ ClientResponse.class);
+ if (response.getStatus() != 200) {
+ throw new RuntimeException("Failed: HTTP error code: "
+ + response.getStatus());
+
+ } else {
+ String output = response.getEntity(String.class);
+ System.out.println("Server response: " + output);
+ }
+
+ } catch (Exception e) {
+
+ e.printStackTrace();
+
+ }
+
+ }
+
+ public static void cleanById(WebResource r, String id) {
+
+ try {
+ ClientResponse response = r.path("/clean/" + id).delete(
+ ClientResponse.class);
+ if (response.getStatus() != 200) {
+ throw new RuntimeException("Failed: HTTP error code: "
+ + response.getStatus());
+ } else {
+ String output = response.getEntity(String.class);
+ System.out.println("Server response: " + output);
+ }
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ public static void addJob(WebResource r) {
+
+ String input = "job:\nlabel: JobTest\n\ntask:\ninit:\nremote: /bin/echo Hello, World!\nfinal:";
+
+ try {
+
+ ClientResponse response = r.path("/addjob").type("text/plain")
+ .post(ClientResponse.class, input);
+ if (response.getStatus() != 200) {
+ throw new RuntimeException("Failed: HTTP error code: "
+ + response.getStatus());
+ } else {
+ response.getClass();
+ System.out.println("Job added with success....");
+ String output = response.getEntity(String.class);
+ System.out.println("JobId: " + output);
+ }
+
+ } catch (Exception e) {
+
+ e.printStackTrace();
+
+ }
+
+ }
+}
@@ -0,0 +1,193 @@
+package org.ourgrid.broker.api;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+
+import org.ourgrid.broker.job.Commands;
+import org.ourgrid.broker.job.JobUsage;
+import org.ourgrid.broker.job.Output;
+import org.ourgrid.broker.job.OutputCapturer;
+
+import com.google.gson.Gson;
+import com.sun.jersey.api.NotFoundException;
+
+/**
+ * The Resources class makes available the broker resources
+ *
+ * @author Patricia Alanis
+ * @since March, 2012.
+ **/
+@Path("/broker")
+public class Resources {
+
+ OutputCapturer outputCapturer = new OutputCapturer();
+ JobUsage jobTreatment = new JobUsage();
+ Gson gson = new Gson();
+
+ /**
+ * Adds a job to run it in the broker, job submission
+ *
+ * @param nativeJobDesc
+ * job description in the language supported by the targeted grid
+ * @return Returns the job identifier in the grid
+ * */
+ @Path("/addjob")
+ @POST
+ @Consumes("text/plain")
+ @Produces({ MediaType.APPLICATION_JSON })
+ public String addJob(String nativeJobDesc) {
+
+ Output output = null;
+ String jobDesc = jobTreatment.getJobDesc(nativeJobDesc);
+ output = outputCapturer.getTerminalOutputWithJobFilePath(
+ Commands.ADD_JOB, jobDesc);
+
+ return jobTreatment.getParameter(output, "ID");
+
+ }
+
+ /**
+ * Returns the job status via <job-id>
+ *
+ * @param nativeJobId
+ * Identifier of the job
+ *
+ * @return Returns the job status
+ * @throws NotFoundJobException
+ * if a job with the nativeId could not be found
+ */
+ @Path("/status/job_id/{nativeId}")
+ @GET
+ @Produces({ MediaType.APPLICATION_JSON })
+ public String getStatusbyJobId(@PathParam("nativeId") String nativeJobId) {
+
+ Output output = outputCapturer.getTerminalOutputWithoutspaceJobId(
+ Commands.STATUS_JOBID, nativeJobId);
+ if (output == null)
+ throw new NotFoundException("No such Job...");
+ else
+ return jobTreatment.getParameter(output, "STATUS");
+
+ }
+
+ /**
+ * Cancels a job execution filtered by <job-id>
+ *
+ * @param nativeJobId
+ * Identifier of the job
+ * @return The message the job was canceled with success
+ * @throws NotFoundJobException
+ * if a job with the nativeId could not be found
+ * */
+ @Path("/cancel/{nativeId}")
+ @DELETE
+ @Produces("text/plain")
+ public String cancelJob(@PathParam("nativeId") String nativeJobId) {
+
+ Output output = outputCapturer.getTerminalOutputWithJobId(
+ Commands.CANCEL_JOB, nativeJobId);
+ System.out.println(output);
+ if (output == null)
+ throw new NotFoundException("No such Job.");
+ else
+ return "Job canceled with success..";
+
+ }
+
+ /**
+ * Cleans a finished job filtered by <job-id>
+ *
+ * @param nativeJobId
+ * Identifier of the job
+ * @return The message the job was removed with success
+ * @throws NotFoundJobException
+ * if a job with the nativeId could not be found
+ */
+ @Path("/clean/{nativeId}")
+ @DELETE
+ @Produces("text/plain")
+ public String cleanJobId(@PathParam("nativeId") String nativeJobId) {
+
+ Output output = outputCapturer.getTerminalOutputWithJobId(
+ Commands.CLEAN_JOBID, nativeJobId);
+ if (output == null)
+ throw new NotFoundException("No such Job...");
+ else
+ return "Job was removed with success...";
+ }
+
+ /**
+ * Shows the current broker status
+ *
+ * @return Returns a status list
+ * */
+ @Path("/status/listall")
+ @GET
+ @Produces({ MediaType.APPLICATION_JSON })
+ public String getStatus() {
+
+ Output output = outputCapturer.getOutput(Commands.STATUS);
+ String status = gson.toJson(jobTreatment.getAllStatus(output));
+ return status;
+ }
+
+ /**
+ * Returns all states of the jobs that matching to certain label
+ *
+ * @param label
+ * Label is a tag placed into the job file, which describes the
+ * job
+ * @return Returns a list of status
+ *
+ */
+
+ public String getStatusbyLabel(@PathParam("label") String label) {
+
+ Output output = outputCapturer.getTerminalOutputWithLabel(
+ Commands.STATUS_LABEL, label);
+ String status = gson.toJson(jobTreatment.getAllStatus(output));
+ return status;
+ }
+
+ /**
+ * Returns all states of the jobs that matching to certain status
+ *
+ * @param state
+ * Describes the status of the job: UNSTARTED, RUNNING, FINISHED,
+ * CANCELLED
+ * @return Returns a list of status
+ */
+
+ public String getStatusbyState(@PathParam("state") String state) {
+
+ Output output = outputCapturer.getTerminalOutputWithState(
+ Commands.STATUS_STATE, state);
+ String status = gson.toJson(jobTreatment.getAllStatus(output));
+ return status;
+ }
+
+ /**
+ * Cleans all the finished and cancelled jobs
+ *
+ * @return The message all finished and cancelled jobs were removed
+ * @throws NotFoundJobException
+ * if a job with the nativeId could not be found
+ */
+
+ public String clean() {
+
+ Output output = outputCapturer.getOutput(Commands.CLEAN);
+ System.out.println("auqui" + output);
+ if (output == null)
+ throw new NotFoundException("No such Job...");
+ else
+ return "All finished and cancelled jobs were removed...";
+ }
+
+}
@@ -0,0 +1,24 @@
+package org.ourgrid.broker.job;
+
+/**
+ * Class with the OurGrid broker commands
+ **/
+public final class Commands {
+
+ public static final String START = "broker start";
+ public static final String STOP = "broker stop";
+ public static final String STOP_F = "broker stop -f";
+ public static final String RESTART = "broker restart";
+ public static final String STATUS = "broker status";
+ public static final String STATUS_JOBID = "broker status -i=";
+ public static final String STATUS_STATE = "broker status -s";
+ public static final String STATUS_LABEL = "broker status -l";
+ public static final String ADD_JOB = "broker addjob";
+ public static final String CANCEL_JOB = "broker canceljob";
+ public static final String WAIT_FOR_JOB = "broker waitforjob";
+ public static final String SETGRID = "broker setgrid";
+ public static final String CLEAN = "broker clean";
+ public static final String CLEAN_JOBID = "broker clean";
+ public static final String GUI = "broker gui";
+
+}
Oops, something went wrong.

0 comments on commit 542b909

Please sign in to comment.