001: /*
002: * <copyright>
003: *
004: * Copyright 2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026: package org.cougaar.tools.csmart.util;
027:
028: import org.cougaar.tools.csmart.experiment.Trial;
029: import org.cougaar.tools.csmart.ui.viewer.CSMART;
030: import org.cougaar.util.log.Logger;
031:
032: import java.io.File;
033: import java.util.Date;
034: import java.text.DateFormat;
035: import java.text.SimpleDateFormat;
036:
037: /**
038: * org.cougaar.tools.csmart.util
039: *
040: */
041: public class FileUtilities {
042:
043: private static Logger log = CSMART.createLogger("FileUtilities");
044: private static DateFormat fileDateFormat = new SimpleDateFormat(
045: "yyyyMMddHHmmss");
046:
047: /**
048: * Create a log file name which is of the form:
049: * node name + date + .log
050: * Create it in the results directory if possible.
051: */
052: public static String getLogFileName(String nodeName, Date runStart) {
053: String filename = nodeName + fileDateFormat.format(runStart)
054: + ".log";
055: String dirname = makeResultDirectory(runStart);
056: if (dirname != null)
057: filename = dirname + File.separatorChar + filename;
058: return filename;
059: }
060:
061: /**
062: * Create a directory for the results of this run.
063: * Results file structure is:
064: * <ExperimentName>
065: * <TrialName>
066: * Results-<Timestamp>.results
067: */
068: private static String makeResultDirectory(Date runStart) {
069: // defaults, if we don't have an experiment
070: File resultDir = CSMART.getResultDir();
071: String experimentName = "Experiment";
072: String trialName = "Trial 1";
073:
074: // if user didn't specify results directory, save in local directory
075: if (resultDir == null) {
076: if (log.isInfoEnabled())
077: log
078: .info("No result directory specified. Should use a local dir. Returning null (in makeResultDirectory).");
079: return null;
080: }
081: String dirname = resultDir.getAbsolutePath()
082: + File.separatorChar + experimentName
083: + File.separatorChar + trialName + File.separatorChar
084: + "Results-" + fileDateFormat.format(runStart);
085: try {
086: File f = new File(dirname);
087: // guarantee that directories exist
088: if (!f.exists() && !f.mkdirs() && !f.exists()) {
089: if (log.isWarnEnabled())
090: log
091: .warn("Unabled to create directory "
092: + dirname
093: + ". Should default to local directory - returning null (in makeResultDirectory)");
094: return null;
095: }
096: } catch (Exception e) {
097: if (log.isErrorEnabled()) {
098: log.error("Couldn't create results directory "
099: + dirname + ": ", e);
100: }
101: return null;
102: }
103: return dirname;
104: }
105:
106: }
|