PageRenderTime 27ms CodeModel.GetById 17ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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