/alaspatial/src/main/java/org/ala/spatial/analysis/maxent/MaxentServiceImpl.java

http://alageospatialportal.googlecode.com/ · Java · 208 lines · 120 code · 40 blank · 48 comment · 12 complexity · 79ac760aded179c40682ecfc1776a9b8 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.analysis.maxent;
  15. import org.ala.spatial.util.StreamGobbler;
  16. import java.io.BufferedReader;
  17. import java.io.File;
  18. import java.io.InputStreamReader;
  19. import org.ala.spatial.util.AnalysisJob;
  20. import org.ala.spatial.util.AnalysisJobMaxent;
  21. /**
  22. * Gets the submitted parameters and runs a maxent model
  23. *
  24. * @author ajayr
  25. */
  26. public class MaxentServiceImpl implements MaxentService {
  27. MaxentSettings cmdMaxent = null;
  28. public MaxentServiceImpl() {
  29. cmdMaxent = new MaxentSettings();
  30. }
  31. public MaxentSettings getMaxentSettings() {
  32. return cmdMaxent;
  33. }
  34. public void setMaxentSettings(MaxentSettings cmdMaxent) {
  35. this.cmdMaxent = cmdMaxent;
  36. }
  37. /**
  38. * The generateSessionDirectory allows creating a session directory
  39. *
  40. * @param thePath
  41. * @return
  42. */
  43. private File generateSessionDirectory(String thePath) {
  44. File fDir = null;
  45. try {
  46. //fDir = new File(cmdPath + sessionId);
  47. fDir = new File(thePath);
  48. fDir.mkdir();
  49. } catch (Exception e) {
  50. }
  51. return fDir;
  52. }
  53. /**
  54. * The process method sets up the parameters and runs the maxent process
  55. *
  56. * @return success int value if the process was successful
  57. */
  58. @Override
  59. public int process(AnalysisJob job) {
  60. return runCommand(cmdMaxent.toString(), job);
  61. }
  62. /**
  63. * The runCommand method does the fork'ing
  64. *
  65. * @param command The command to be run
  66. * @return success int value if the process was successful
  67. */
  68. private int runCommand(String command, AnalysisJob job) {
  69. Runtime runtime = Runtime.getRuntime();
  70. try {
  71. String[] acmd = new String[3];
  72. acmd[0] = "cmd.exe";
  73. acmd[1] = "/C";
  74. acmd[2] = command;
  75. System.out.println("Exec'ing " + command);
  76. Process proc = runtime.exec(command);
  77. // any error message?
  78. StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR", job);
  79. // any output?
  80. StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT", job);
  81. // kick them off
  82. errorGobbler.start();
  83. outputGobbler.start();
  84. System.out.printf("Output of running %s is:", command);
  85. int exitVal = proc.waitFor();
  86. errorGobbler.interrupt();
  87. outputGobbler.interrupt();
  88. // any error???
  89. return exitVal;
  90. } catch (Exception e) {
  91. e.printStackTrace(System.out);
  92. }
  93. return 1;
  94. }
  95. public int process(AnalysisJobMaxent job) {
  96. MaxentThread mt = new MaxentThread(cmdMaxent.toString(), job);
  97. mt.start();
  98. while (mt.isAlive() && (job == null || !job.isCancelled())) {
  99. try {
  100. Thread.sleep(100);
  101. } catch (InterruptedException ex) {
  102. //wake up
  103. }
  104. }
  105. try {
  106. mt.kill(); //in case it is still running, MaxentThread will end now
  107. } catch (Exception e) {
  108. }
  109. return mt.exitValue;
  110. }
  111. }
  112. class MaxentThread extends Thread {
  113. public int exitValue = -1;
  114. String command;
  115. Process proc;
  116. AnalysisJob job;
  117. public MaxentThread(String command_, AnalysisJob job) {
  118. command = command_;
  119. this.job = job;
  120. setPriority(Thread.MIN_PRIORITY);
  121. }
  122. public void kill() {
  123. proc.destroy();
  124. }
  125. /**
  126. * The runCommand method does the fork'ing
  127. *
  128. * @param command The command to be run
  129. * @return success int value if the process was successful
  130. */
  131. public void run() {
  132. Runtime runtime = Runtime.getRuntime();
  133. try {
  134. String[] acmd = new String[3];
  135. acmd[0] = "cmd.exe";
  136. acmd[1] = "/C";
  137. acmd[2] = command;
  138. System.out.println("Exec'ing " + command);
  139. proc = runtime.exec(command);
  140. System.out.println("Setting up output stream readers");
  141. InputStreamReader isre = new InputStreamReader(proc.getErrorStream());
  142. BufferedReader bre = new BufferedReader(isre);
  143. InputStreamReader isr = new InputStreamReader(proc.getInputStream());
  144. BufferedReader br = new BufferedReader(isr);
  145. String line;
  146. System.out.printf("Output of running %s is:", command);
  147. while ((line = bre.readLine()) != null) {
  148. if (job != null) {
  149. job.log(line);
  150. }
  151. System.out.println(line);
  152. }
  153. while ((line = br.readLine()) != null) {
  154. if (job != null) {
  155. job.log(line);
  156. }
  157. System.out.println(line);
  158. }
  159. int exitVal = proc.waitFor();
  160. // any error???
  161. exitValue = exitVal;
  162. return;
  163. } catch (Exception e) {
  164. e.printStackTrace(System.out);
  165. }
  166. exitValue = 1;
  167. }
  168. }