001: /*
002: * This file or a portion of this file is licensed under the terms of
003: * the Globus Toolkit Public License, found in file ../GTPL, or at
004: * http://www.globus.org/toolkit/download/license.html. This notice must
005: * appear in redistributions of this file, with or without modification.
006: *
007: * Redistributions of this Software, with or without modification, must
008: * reproduce the GTPL in: (1) the Software, or (2) the Documentation or
009: * some other similar material which is provided with the Software (if
010: * any).
011: *
012: * Copyright 1999-2004 University of Chicago and The University of
013: * Southern California. All rights reserved.
014: */
015: package org.griphyn.vdl.euryale;
016:
017: import org.griphyn.common.util.Separator;
018: import org.griphyn.common.util.Currently;
019: import java.io.*;
020: import java.util.*;
021:
022: /**
023: * This file factory generates a stream of submit files within the same
024: * toplevel directory. There is no subdirectory structuring.
025: *
026: * @author Kavitha Ranganathan
027: * @author Jens-S. Vöckler
028: * @author Yong Zhao
029: * @version $Revision: 289 $
030: *
031: * @see DAX2DAG
032: */
033: public class FlatFileFactory implements FileFactory {
034: /**
035: * Contains the base directory where to store all files into.
036: */
037: private File m_directory;
038:
039: /**
040: * Counts the number of times the virtual constructor was called.
041: */
042: private int m_count;
043:
044: /**
045: * Helping structure to avoid repeated memory requests. Stores the
046: * path string of the base directory for later perusal.
047: * @see #getBaseDirectory()
048: */
049: private String mh_directory;
050:
051: /**
052: * Resets the helper structures after changing layout parameters. You
053: * will also need to call this function after you invoked the virtual
054: * constructors, but want to change parameter pertaining to the
055: * directory structure. The structured file count will also be reset!
056: */
057: public void reset() {
058: m_count = 0;
059: mh_directory = m_directory.getPath();
060: }
061:
062: /**
063: * Constructor: Creates the directory and employs sanity checks.
064: * @param directory is the place where files should be placed.
065: * @throws IOException if the location is not a writable directory,
066: * or cannot be created as such.
067: */
068: public FlatFileFactory(File directory) throws IOException {
069: sanityCheck(directory);
070: m_directory = directory;
071: m_count = 0;
072: mh_directory = m_directory.getPath();
073: }
074:
075: /**
076: * Constructor: Creates the directory and employs sanity checks.
077: * @param directory is the place where files should be placed.
078: * @throws IOException if the location is not a writable directory,
079: * or cannot be created as such.
080: */
081: public FlatFileFactory(String directory) throws IOException {
082: File base = new File(directory);
083: sanityCheck(base);
084: m_directory = base;
085: m_count = 0;
086: mh_directory = m_directory.getPath();
087: }
088:
089: /**
090: * Virtual constructor: Creates the next file with the given basename.
091: * @param basename is the filename to create. Don't specify dirs here.
092: * @return a File structure which points to the new file. Nothing is
093: * created through this method, and creation may still fail.
094: * @see #getCount()
095: */
096: public File createFile(String basename) throws IOException {
097: return createFlatFile(basename);
098: }
099:
100: /**
101: * Virtual constructor: Creates the next file with the given basename.
102: * @param basename is the filename to create. Don't specify dirs here.
103: * @return a File structure which points to the new file. Nothing is
104: * created through this method, and creation may still fail.
105: * @see #getCount()
106: */
107: public File createFlatFile(String basename) throws IOException {
108: m_count++;
109: return new File(m_directory, basename);
110: }
111:
112: /**
113: * Returns the number of times the virtual constructor for structured
114: * files was called. Since this is a flat file class, it will be 0.
115: * @return the count for createFile invocations.
116: * @see #createFile( String )
117: */
118: public int getCount() {
119: return 0;
120: }
121:
122: /**
123: * Returns the number of times the virtual constructor was called.
124: * @return the count for createFile invocations.
125: * @see #createFile( String )
126: */
127: public int getFlatCount() {
128: return m_count;
129: }
130:
131: /**
132: * Returns base directory. We want to be on the safe side.
133: * @return the directory passed to the constructor.
134: * @see #setBaseDirectory( File )
135: */
136: public File getBaseDirectory() {
137: return m_directory;
138: }
139:
140: /**
141: * Checks the destination location for existence, if it can
142: * be created, if it is writable etc.
143: * @param dir is the new base directory to optionally create
144: */
145: protected void sanityCheck(File dir) throws IOException {
146: if (dir.exists()) {
147: // location exists
148: if (dir.isDirectory()) {
149: // ok, isa directory
150: if (dir.canWrite()) {
151: // can write, all is well
152: return;
153: } else {
154: // all is there, but I cannot write to dir
155: throw new IOException(
156: "Cannot write to existing directory "
157: + dir.getPath());
158: }
159: } else {
160: // exists but not a directory
161: throw new IOException("Destination " + dir.getPath()
162: + " already "
163: + "exists, but is not a directory.");
164: }
165: } else {
166: // does not exist, try to make it
167: if (!dir.mkdirs()) {
168: throw new IOException(
169: "Unable to create directory destination "
170: + dir.getPath());
171: }
172: }
173: }
174:
175: /**
176: * Accessor to set a different base directory. Note that this accessor
177: * may only be called before any of the virtual constructors were
178: * invoked the first time.
179: * @param base is the new directory to set things to
180: * @throws VTorInUseException if the virtual constructor is already in use.
181: * @throws IOException if there is something wrong with the directory
182: * @see #getBaseDirectory()
183: * @see #reset()
184: */
185: public void setBaseDirectory(File base) throws IOException,
186: VTorInUseException {
187: if (m_count != 0)
188: throw new VTorInUseException();
189: sanityCheck(base);
190: m_directory = base;
191: reset(); // will invoke _virtual_ reset :-)
192: }
193:
194: /**
195: * Constructs a virtual basename by removing the directory from a
196: * given file. If the file's directory does not match our stored
197: * directory prefix, nothing will be removed. This method is essential
198: * in order to assemble relative pathnames to the base directory.
199: *
200: * @param file is an arbitrary file, should have been constructed using
201: * the virtual constructor.
202: * @return a relative pathname with the base directory removed. Note
203: * that may will still contain directories. In case of an arbitrary
204: * file, the full filename may be returned.
205: */
206: public String getName(File file) {
207: String path = file.getPath();
208: if (path.startsWith(mh_directory)) {
209: // yes, one of ours
210: return path.substring(mh_directory.length() + 1);
211: } else {
212: // eek, an alien
213: return path;
214: }
215: }
216: }
|