Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

JetWebFile.java

Go to the documentation of this file.
00001 package cedar.jetweb.util;
00002 import org.apache.commons.io.FileUtils;
00003 import java.util.Vector;
00004 import java.io.*;
00005 
00006 import cedar.jetweb.*;
00008 
00022 
00023 
00024 public class JetWebFile extends File{
00025 
00026     private boolean zipped_ = false;
00027     private boolean archived_ = false;
00028     private boolean jobIn_ = false;
00029     private boolean jobOut_ = false;
00030     private String dir_ = "";
00031     private String fileName_ = "";
00032     private String extension_ = "";
00033     private static String archiveDir_ = JetWebConfig.storeDirName;
00034     private static String inputDir_ = JetWebConfig.jobInDirName;
00035     private static String outputDir_ = JetWebConfig.jobOutDirName;
00036     private static boolean pathsChecked_ = false;
00037 
00038     private boolean isXML_ = false;
00039     private boolean isAida_ = false;
00040     private boolean isOut_ = false;
00041 
00042     public static String XML = ".xml";
00043     private static String GZ_ = ".gz";
00044     public  static String AIDA = ".aida";
00045     public static String AIDML = ".aidml";
00046     public static String OUT = ".out";
00047 
00048     private static Integer xmlLength_ = XML.length();
00049     private static Integer outLength_ = OUT.length();
00050     private static Integer aidaLength_ =  AIDA.length();
00051     private static Integer gzLength_ =  GZ_.length();
00052     private static Integer aidmlLength_ = AIDML.length();
00053 
00055 
00064     public JetWebFile(String fName){
00065 
00066     super(findFileName(fName));
00067     parseFileName(this.getAbsolutePath());
00068     }
00070 
00080     public JetWebFile(String fName, String extension){
00081     super(findFileName(fName + extension));
00082     parseFileName(this.getAbsolutePath());
00083     }
00085 
00089     public String toString(){
00090 
00091     return fileName_;
00092     }
00094 
00099     public JetWebFile Zip()
00100     throws JetWebException{
00101     
00102     if(zipped_) return this;
00103 
00104     if(!this.exists()){
00105         throw new JetWebException("JetWebFile.Zip()", 
00106                       "Error, file " + 
00107                       this + extension_ + 
00108                       "does not exist for zipping");
00109     }
00110 
00111     execCommand("gzip -f");
00112     zipped_ = true;
00113 
00114     JetWebFile newFile = null;
00115 
00116     if(isAida()){//then we must re-name the file from .aidml.gz to .aida
00117         reName(dir_ + fileName_ + AIDML + GZ_,
00118            dir_ + fileName_ + AIDA);
00119         extension_ = AIDA;
00120         newFile = new JetWebFile(dir_ + fileName_ + AIDA);
00121     }else{
00122         newFile = new JetWebFile(dir_ + fileName_ + extension_ + GZ_);
00123     }
00124     
00125     return newFile;
00126     }
00128 
00133     public JetWebFile UnZip()
00134     throws JetWebException{
00135 
00136     if(!zipped_) return this;
00137     
00138     if(!this.exists()){
00139         throw new JetWebException("JetWebFile.UnZip()", 
00140                       "Error, file " + 
00141                       this + extension_ + GZ_ +  
00142                       "does not exist for unzipping");
00143     }
00144 
00145     JetWebFile newFile = null;
00146 
00147     if(isAida()){
00148         execCommand("gunzip -f -S '" + AIDA + "'");
00149         //we now need to tag on the .aidml extension
00150         reName(dir_ + fileName_, dir_ + fileName_ + AIDML);
00151         extension_ = AIDML;
00152         newFile = new JetWebFile(dir_ + fileName_ + AIDML);
00153     }else{
00154         execCommand("gunzip -f");
00155         newFile = new JetWebFile(dir_ + fileName_ + extension_);
00156     }
00157     zipped_ = false;
00158     return newFile;
00159     }
00161 
00164     public boolean isZipped(){
00165     return zipped_;
00166     }
00168 
00173     public JetWebFile Archive()
00174     throws JetWebException{
00175 
00176     if(archived_) return this;
00177     if(!this.exists()){
00178         throw new JetWebException("JetWebFile.Archive()", 
00179                       "Error, file " + 
00180                       this + extension_ + 
00181                       "does not exist for archiving");
00182     }
00183 
00184     archived_ = true;
00185 
00186     return moveTo(archiveDir_);
00187     }
00189 
00195     public JetWebFile UnArchive()
00196     throws JetWebException{
00197 
00198     if(!archived_) return this;
00199     if(!this.exists()){
00200         throw new JetWebException("JetWebFile.UnArchive()", 
00201                       "Error, file " + 
00202                       this + extension_ + 
00203                       "does not exist in the archive directory"
00204                       );
00205     }
00206 
00207     archived_ = false;
00208 
00209     return moveTo(outputDir_);
00210     }
00212 
00215     public boolean isArchived(){
00216     return archived_;
00217     }
00219 
00223     public String extension(){
00224     return extension_;
00225     }
00227 
00230     public String directory(){
00231     return dir_;
00232     }
00234 
00237     public boolean isAida(){
00238     return(isAida_);
00239     }
00241 
00244     public boolean isXML(){
00245     return(isXML_);
00246     }
00248 
00252     public boolean isOut(){
00253     return(isOut_);
00254     }
00256 
00259     public String fullPath(){
00260 
00261     String fp = dir_ + fileName_ + extension_;
00262     if(zipped_ && !isAida_) fp = fp + GZ_;
00263 
00264     return fp;
00265     }
00267     private static String findFileName(String fName){
00268     
00269     checkPaths();
00270 
00271     File tmp = new File(fName);
00272     if(tmp.exists()){
00273         return fName;
00274     }
00275         
00276     Vector<String> parts = decompose(fName);
00277 
00278     boolean triedZipped = parts.get(2).equals(AIDA) || 
00279         parts.get(3).equals(GZ_);
00280     boolean triedArchive = parts.get(0).equals(archiveDir_);
00281     boolean triedInput = parts.get(0).equals(inputDir_);
00282     boolean isAida = parts.get(2).equals(AIDA) ||
00283         parts.get(2).equals(AIDML);
00284 
00285     if( !(triedArchive && triedZipped) ){
00286         if(isAida){
00287         fName = archiveDir_ + parts.get(1) + AIDA;
00288         }else{
00289         fName = archiveDir_ + parts.get(1) + parts.get(2) + GZ_;
00290         }
00291 
00292         tmp = new File(fName);
00293         if(tmp.exists())return fName;
00294     }
00295 
00296     if( !(triedArchive && !triedZipped) ){
00297         if(isAida){
00298         fName = archiveDir_ + parts.get(1) + AIDML;
00299         }else{
00300         fName = archiveDir_ + parts.get(1) + parts.get(2);
00301         }
00302 
00303         tmp = new File(fName);
00304         if(tmp.exists()) return fName;
00305     }
00306 
00307     if( !(triedInput && triedZipped)){
00308         if(isAida){
00309         fName = inputDir_ + parts.get(1) + AIDA;
00310         }else{
00311         fName = inputDir_ + parts.get(1) + parts.get(2) + GZ_;
00312         }
00313 
00314         tmp = new File(fName);
00315         if(tmp.exists()) return fName;
00316         }
00317     
00318     if( !(triedInput && !triedZipped)){
00319         if(isAida){
00320         fName = inputDir_ + parts.get(1) + AIDML;
00321         }else{
00322         fName = inputDir_ + parts.get(1) + parts.get(2);
00323         }
00324 
00325         tmp = new File(fName);
00326         if(tmp.exists()) return fName;
00327     }
00328 
00329     fName = outputDir_ + parts.get(1) + parts.get(2);
00330     
00331     return fName;
00332     }
00334     private static Vector<String> decompose(String fName){
00335     Vector<String> vec = new Vector(4);
00336 
00337     int lastSlash = fName .lastIndexOf("/");
00338     if(lastSlash != -1){
00339         String dir = fName.substring(0, lastSlash);
00340         if(!dir.endsWith("/")) dir = dir + "/";
00341         vec.add(dir);
00342         fName = fName.substring(lastSlash+1);
00343     }else{
00344         vec.add("");
00345     }
00346     boolean zipped = false;
00347     boolean aida;
00348     String gz = "";
00349     String ext = "";
00350     if(fName.endsWith(GZ_)){
00351         fName = fName.substring(0, fName.length() - gzLength_);
00352         gz = GZ_;
00353     }
00354 
00355     if(fName.endsWith(AIDA)){
00356         fName = fName.substring(0, fName.length() - aidaLength_);
00357         ext = AIDA;
00358     }
00359 
00360     if(fName.endsWith(AIDML)){
00361         fName = fName.substring(0, fName.length() - aidmlLength_);
00362         ext = AIDML;
00363     }
00364 
00365     if(fName.endsWith(XML)){
00366         fName = fName.substring(0, fName.length() - xmlLength_);
00367         ext = XML;
00368     }
00369     
00370     if(fName.endsWith(OUT)){
00371         fName = fName.substring(0, fName.length() - outLength_);
00372         ext = OUT;
00373     }
00374 
00375     vec.add(fName);
00376     vec.add(ext);
00377     vec.add(gz);
00378     return vec;
00379     }
00381 
00386     private void parseFileName(String fName){
00387     
00388     Vector<String> parts = decompose(fName);
00389     dir_ = parts.get(0);
00390     fileName_ = parts.get(1);
00391     String ext = parts.get(2);
00392 
00393     if(parts.get(3).equals(GZ_) ||
00394        ext.equals(AIDA)){
00395         zipped_ = true;
00396     }else{
00397         zipped_ = false;
00398     }
00399 
00400     if(ext.equals(AIDA)){
00401         extension_ = AIDA;
00402         isAida_ = true;
00403     }else if(ext.equals(AIDML)){
00404         extension_ = AIDML;
00405         isAida_ = true;
00406     }else if(ext.equals(XML)){
00407         extension_ = XML;
00408         isXML_ = true;
00409     }else if(ext.equals(OUT)){
00410         extension_ = OUT;
00411         isOut_ = true;
00412     }
00413 
00414     if(dir_.equals(archiveDir_)){
00415         archived_ = true;
00416     }else if(dir_.equals(inputDir_)){
00417         jobIn_ = true;
00418     }else if(dir_.equals(outputDir_)){
00419         jobOut_ = true;
00420     }
00421 
00422     return; 
00423     }
00425 
00429     private static void checkPaths(){
00430       
00431     if(pathsChecked_) return;
00432     pathsChecked_ = true;
00433 
00434     if(!archiveDir_.endsWith("/")) archiveDir_ = archiveDir_ + "/";
00435     if(!inputDir_.endsWith("/")) inputDir_ = inputDir_ + "/";
00436     if(!outputDir_.endsWith("/")) outputDir_ = outputDir_ + "/";
00437 
00438     return;
00439     }
00441     private JetWebFile moveTo(String dir)
00442     throws JetWebException{
00443 
00444     if(!dir.endsWith("/")) dir = dir + "/";
00445     JetWebFile newFile = null;
00446     try{
00447         File fileDir = new File(dir);
00448         FileUtils.copyFileToDirectory(this, fileDir);
00449         
00450         String fName = fileName_ + extension_;
00451         if(!isAida() && isZipped()){
00452         fName = fName + GZ_;
00453         }
00454 
00455         newFile = new JetWebFile(dir + fName);
00456         File oldFile  = new File(dir_ + fName);
00457         
00458         //this.renameTo(newFile);
00459         FileUtils.forceDelete(oldFile);
00460     }catch (IOException err){
00461         throw new JetWebException(err);
00462     }
00463     return newFile;
00464     }
00466     private void reName(String oldName, String newName)
00467     throws JetWebException{
00468 
00469     File newFile = new File(newName);
00470     File oldFile = new File(oldName);
00471     try{
00472         FileUtils.copyFile(oldFile, newFile);
00473         FileUtils.forceDelete(oldFile);
00474     }catch(IOException err){
00475         throw new JetWebException(err);
00476     }
00477     //this.renameTo(newFile);
00478     //oldFile.delete();
00479     return;
00480     }
00482     private void execCommand(String cmd)
00483     throws JetWebException{
00484 
00485     try{
00486         Process prc = 
00487         Runtime.getRuntime().exec
00488         (cmd + " " + dir_ + fileName_ + extension_);
00489         try {
00490         prc.waitFor();
00491         } catch (InterruptedException e) {
00492         System.out.println("JetWebFile: Problem waiting for file " + 
00493                    dir_ + fileName_ + 
00494                    " with command " + cmd);
00495         }
00496     }catch (IOException e1) {
00497         throw new JetWebException
00498         (e1, "JetWebFile: Could not execute command " + 
00499          cmd +"on file " + dir_ + fileName_);
00500     }
00501     
00502     return;
00503     }
00504 }

Generated Wed Jan 17 09:14:27 GMT 2007