/alaspatial/src/main/java/org/ala/spatial/web/services/JobsController.java

http://alageospatialportal.googlecode.com/ · Java · 153 lines · 113 code · 22 blank · 18 comment · 13 complexity · 47fbec115507daf9e059986bc96101b2 MD5 · raw file

  1. /**
  2. * ************************************************************************
  3. * Copyright (C) 2010 Atlas of Living Australia All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with the
  7. * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS IS" basis,
  10. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
  11. * the specific language governing rights and limitations under the License.
  12. * *************************************************************************
  13. */
  14. package org.ala.spatial.web.services;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. import javax.servlet.http.HttpServletRequest;
  18. import org.ala.spatial.util.AnalysisQueue;
  19. import org.springframework.stereotype.Controller;
  20. import org.springframework.web.bind.annotation.*;
  21. /**
  22. * Analysis jobs general webservices.
  23. *
  24. * @author Adam
  25. */
  26. @Controller
  27. public class JobsController {
  28. static HashMap<String, Integer> lastLogPos = new HashMap<String, Integer>();
  29. @RequestMapping(value = "/ws/job", method = RequestMethod.GET)
  30. public @ResponseBody
  31. Map job(@RequestParam(value = "pid", required = true, defaultValue = "") String pid, HttpServletRequest req) {
  32. String fullLog = req.getParameter("log");
  33. Map m = new HashMap<String, String>();
  34. m.put("state", "job does not exist");
  35. try {
  36. String state = AnalysisQueue.getState(pid);
  37. if (state != null) {
  38. m.put("state", state);
  39. m.put("message", AnalysisQueue.getMessage(pid));
  40. m.put("progress", AnalysisQueue.getProgress(pid));
  41. m.put("status", AnalysisQueue.getStatus(pid));
  42. if (fullLog == null) {
  43. Integer pos = lastLogPos.containsKey(pid) ? lastLogPos.get(pid) : 0;
  44. String log = AnalysisQueue.getLog(pid);
  45. if (log == null) {
  46. log = "";
  47. }
  48. m.put("log", log.substring(pos));
  49. lastLogPos.put(pid, log.length());
  50. } else {
  51. m.put("log", AnalysisQueue.getLog(pid));
  52. }
  53. }
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. }
  57. return m;
  58. }
  59. @RequestMapping(value = "/ws/jobs/listwaiting", method = RequestMethod.GET)
  60. public @ResponseBody
  61. String listWaiting() {
  62. try {
  63. String s = AnalysisQueue.listWaiting();
  64. if (s != null) {
  65. return s;
  66. }
  67. } catch (Exception e) {
  68. e.printStackTrace();
  69. }
  70. return "job does not exist";
  71. }
  72. @RequestMapping(value = "/ws/jobs/listrunning", method = RequestMethod.GET)
  73. public @ResponseBody
  74. String listRunning() {
  75. try {
  76. String s = AnalysisQueue.listRunning();
  77. if (s != null) {
  78. return s;
  79. }
  80. } catch (Exception e) {
  81. e.printStackTrace();
  82. }
  83. return "job does not exist";
  84. }
  85. @RequestMapping(value = "/ws/jobs/listfinished", method = RequestMethod.GET)
  86. public @ResponseBody
  87. String listFinished() {
  88. try {
  89. String s = AnalysisQueue.listFinished();
  90. if (s != null) {
  91. return s;
  92. }
  93. } catch (Exception e) {
  94. e.printStackTrace();
  95. }
  96. return "job does not exist";
  97. }
  98. @RequestMapping(value = "/ws/jobs/cancel", method = RequestMethod.GET)
  99. public @ResponseBody
  100. String cancel(@RequestParam(value = "pid", required = true, defaultValue = "") String pid) {
  101. try {
  102. AnalysisQueue.cancelJob(pid);
  103. return "";
  104. } catch (Exception e) {
  105. e.printStackTrace();
  106. }
  107. return "job does not exist";
  108. }
  109. @RequestMapping(value = "/ws/jobs/inputs", method = RequestMethod.GET)
  110. public @ResponseBody
  111. String inputs(@RequestParam(value = "pid", required = true, defaultValue = "") String pid) {
  112. try {
  113. String s = AnalysisQueue.getInputs(pid);
  114. return s;
  115. } catch (Exception e) {
  116. e.printStackTrace();
  117. }
  118. return "job does not exist";
  119. }
  120. @RequestMapping(value = "/ws/jobs/image", method = RequestMethod.GET)
  121. public @ResponseBody
  122. String image(@RequestParam(value = "pid", required = true, defaultValue = "") String pid) {
  123. try {
  124. String s = AnalysisQueue.getImage(pid);
  125. return s;
  126. } catch (Exception e) {
  127. e.printStackTrace();
  128. }
  129. return "job does not exist";
  130. }
  131. }