View Javadoc

1   /*
2    * $Id: CommandLineApplicationEnvironment.java,v 1.2 2004/07/01 11:07:59 nw Exp $
3    *
4    * Created on 24 November 2003 by Paul Harrison
5    * Copyright 2003 AstroGrid. All rights reserved.
6    *
7    * This software is published under the terms of the AstroGrid
8    * Software License version 1.2, a copy of which has been included
9    * with this distribution in the LICENSE.txt file.
10   */
11  
12  package org.astrogrid.applications.commandline;
13  
14  import org.astrogrid.applications.commandline.exceptions.CannotCreateWorkingDirectoryException;
15  import org.astrogrid.applications.manager.idgen.IdGen;
16  
17  import java.io.File;
18  import java.net.URLEncoder;
19  /***
20   * Encapsulates all that is needed to run a command line application in its own workspace.
21   * @author Paul Harrison (pah@jb.man.ac.uk)
22   * @version $Name:  $
23   * @since iteration4
24   */
25  public class CommandLineApplicationEnvironment {
26      public interface WorkingDir {
27          File getDir();
28      }
29     /***
30      * Trivial class to create temporary files for use by the environment. Does this by creating files with numbers as their names.
31      * @author Paul Harrison (pah@jb.man.ac.uk)
32      * @version $Name:  $
33      * @since iteration4
34      * */
35      
36     private class TempFileFactory {
37        private int filenum = 0;
38  
39        public File createFile() {
40           StringBuffer filename = new StringBuffer();
41           filename.append(filenum++);
42           filename.append(".tmpgen");
43           return new File(executionDirectory, filename.toString());
44        }
45  
46     }
47     
48     static private org.apache.commons.logging.Log logger =
49        org.apache.commons.logging.LogFactory.getLog(
50           CommandLineApplicationEnvironment.class);
51     private final File errorLog;
52     private final String executionId;
53     private final File outputLog;
54     private final File executionDirectory;
55     private final TempFileFactory tempFileFactory;
56  
57  
58     public CommandLineApplicationEnvironment(final IdGen idgen, final WorkingDir workingDirectory)
59        throws CannotCreateWorkingDirectoryException {
60  
61        
62        executionId = idgen.getNewID();
63        logger.info("new execution id="+executionId);
64        executionDirectory =
65           new File(workingDirectory.getDir(),URLEncoder.encode(executionId));
66        if (!executionDirectory.exists()) {
67  
68           if (!executionDirectory.mkdir()) {
69              logger.error(
70                 " cannot create working directory "
71                    + executionDirectory.getAbsolutePath());
72              throw new CannotCreateWorkingDirectoryException(executionDirectory);
73           }
74        }
75        else {
76           logger.warn(
77              "working directory "
78                 + executionDirectory.getAbsolutePath()
79                 + " already exists");
80           //TODO need to decide if this is a failure condition...
81        }
82        tempFileFactory = new TempFileFactory();
83        // lets give these meaningful names.
84        errorLog = new File(executionDirectory,"cea-error.log"); //tempFileFactory.createFile();
85        outputLog = new File(executionDirectory,"cea-output.log"); //tempFileFactory.createFile();
86     }
87  
88     /***
89      * @return
90      */
91     public File getErrorLog() {
92        return errorLog;
93     }
94  
95     /***
96      * @return
97      */
98     public File getExecutionDirectory() {
99        return executionDirectory;
100    }
101    
102    public File getTempFile() {
103        return tempFileFactory.createFile(); 
104    }
105 
106    /***
107     * @return
108     */
109    public String getExecutionId() {
110       return executionId;
111    }
112 
113    /***
114     * @return
115     */
116    public File getOutputLog() {
117       return outputLog;
118    }
119    
120 
121 
122 }