Changeset 3568


Ignore:
Timestamp:
Jul 18, 2007, 3:46:04 PM (14 years ago)
Author:
Martin Svensson
Message:

References #451 Packer plugin for zip, tar.gz and tar.bz2 format
Should be tested more.

Location:
trunk/src
Files:
4 added
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/plugins/core/net/sf/basedb/plugins/PackedFileExporter.java

    r3555 r3568  
    2525package net.sf.basedb.plugins;
    2626
     27import net.sf.basedb.core.AbsoluteProgressReporter;
    2728import net.sf.basedb.core.BaseException;
    2829import net.sf.basedb.core.BooleanParameterType;
     
    3031import net.sf.basedb.core.Directory;
    3132import net.sf.basedb.core.File;
    32 import net.sf.basedb.core.IntegerParameterType;
    3333import net.sf.basedb.core.Item;
    34 import net.sf.basedb.core.ItemResultList;
     34import net.sf.basedb.core.ParameterType;
    3535import net.sf.basedb.core.Path;
    3636import net.sf.basedb.core.PathParameterType;
     
    4040import net.sf.basedb.core.ProgressReporter;
    4141import net.sf.basedb.core.RequestInformation;
     42import net.sf.basedb.core.SimpleAbsoluteProgressReporter;
    4243import net.sf.basedb.core.StringParameterType;
    4344import net.sf.basedb.core.Job.ExecutionTime;
     
    5253import net.sf.basedb.core.plugin.Response;
    5354import net.sf.basedb.util.Enumeration;
    54 import net.sf.basedb.util.FileUtil;
     55import net.sf.basedb.util.zip.FilePacker;
    5556
    5657import java.io.IOException;
    57 import java.io.InputStream;
    5858import java.util.ArrayList;
    5959import java.util.Arrays;
    6060import java.util.Collection;
    6161import java.util.EnumSet;
     62import java.util.HashMap;
    6263import java.util.HashSet;
    6364import java.util.List;
     65import java.util.Map;
    6466import java.util.Set;
    65 import java.util.zip.ZipEntry;
    66 import java.util.zip.ZipOutputStream;
    6767
    6868/**
    69   Plugin that compress selected files and directories into one of following selected format:
     69  Plugin that packs selected files and directories.
     70  Following packing formats are supported:
    7071  <ul>
    7172    <li>ZIP-file(.zip)
    72     <li>TAR-file(.tar)
     73    <li>Gzipped TAR-file(.tar.gz)
     74    <li>BZipped-file(tar.bz2)
    7375  </ul>
    7476  The compressed file can the either be downloaded immediately to a local computer or
    7577  stored into BASE's file system.
    7678 
    77   @author martin
     79  @author Martin
    7880  @verison 2.4
    7981  @base.modified $Date: 2007-07-11 15:45:01 +0200 (Mon, 02 Jul 2007) $
    8082*/
    81 public class FilePacker
     83public class PackedFileExporter
    8284  extends AbstractExporterPlugin
    8385  implements InteractivePlugin
     
    8688  private static final About about = new AboutImpl
    8789    (
    88         "File packer exporter",
    89         "This plugin is used to compress one or more files or directories" +
    90         "into a selected format. The compressed file can be downloaded immediatley" +
    91         "or stored in BASE's file system",
     90        "Packed file exporter",
     91        "This plugin is used to packed one or more files/directories" +
     92        "into a selected format.Items to pack can be selected before starting the plugin and " +
     93        "during configuration(or both). The compressed file can be downloaded immediatley" +
     94        "or stored in file system of BASE. The user can choose to have the selected files and " +
     95        "directories removed after they've been packed.",
    9296        "2.4",
    9397        "2007, Base 2 development team",
     
    101105  private static final Set<GuiContext> guiContexts = 
    102106    new HashSet<GuiContext>(Arrays.asList(fileGui, directoryGui));
     107 
    103108  private static final String FORMAT_ZIP = "zip";
    104   private static final String FORMAT_TAR = "tar";
     109  private static final String FORMAT_TAR_GZ = "tar.gz";
     110  private static final String FORMAT_BZ = "tar.bz2";
     111  private Map<String, FilePacker> packers = new HashMap<String, FilePacker>(); 
     112 
    105113  private static final String COMMAND_CONFIGURE_JOB_STEP2 = "command_configure_job_step2";
    106114 
    107115  private static final Set<Permissions> permissions = new HashSet<Permissions>();
    108116 
     117  // Parameters
    109118  private RequestInformation configureJob;
    110119  private RequestInformation configurePacking;
     
    114123  private PluginParameter<String> directoriesParameter;
    115124 
    116   private int compressedFiles;
    117   private int compressedFailed;
    118 
     125  // Counter
     126  private int compressedItems;
     127
     128  public PackedFileExporter()
     129  {
     130    packers.put(FORMAT_ZIP, new ZipFilePacker());
     131    packers.put(FORMAT_TAR_GZ, new TarFilePacker(false));
     132    packers.put(FORMAT_BZ, new TarFilePacker(true));
     133  }
     134 
    119135  private DbControl dc;
     136  /*
     137      @see net.sf.basedb.core.plugin.AbstractExporterPlugin#begin(net.sf.basedb.core.DbControl)
     138  */
    120139  protected void begin(DbControl dc)
    121140  {
    122141    this.dc = dc;
    123     compressedFiles = 0;
    124     compressedFailed = 0;
    125   }
    126   /*
    127     Pack/compress the files and directories and then store the finished file.   
     142    compressedItems = 0;   
     143  }
     144 
     145  /*
     146    Find the right packer tool to use and then pack the files and directories.     
    128147      @see net.sf.basedb.core.plugin.AbstractExporterPlugin#performExport(net.sf.basedb.core.plugin.ExportOutputStream,
    129148        net.sf.basedb.core.ProgressReporter)
     
    131150  @Override
    132151  @SuppressWarnings("unchecked")
    133   protected void performExport(ExportOutputStream out,
    134       ProgressReporter progress) throws IOException
     152  protected void performExport(ExportOutputStream out, ProgressReporter progress)
     153    throws IOException
    135154  {
    136155    List<String> filePaths = (List<String>)job.getValues("files");       
     
    138157    String compressionFormat = (String)job.getValue("fileFormat");
    139158    boolean removeItems = (Boolean)job.getValue("removeItems");
    140    
    141     //The fileformat is .zip
    142     if (compressionFormat.equals(FORMAT_ZIP))
    143     {
    144       ZipOutputStream zipOut = new ZipOutputStream(out);
    145      
    146       //Zip all the selected files to file
    147       for (String pathString : filePaths)
    148       {
    149         File file = File.getByPath(dc, new Path(pathString, Path.Type.FILE), false);
    150         zipFile(zipOut, File.getByPath(dc, file.getPath(), false), null);
    151         // Set the removed flag to true if this has been defined in the job configuration
    152         file.setRemoved(removeItems ? true : file.isRemoved());
    153       }
    154      
    155       //Zip all the selected directories to file
    156       for (String pathString : directoryPaths)
    157       {
    158         Directory dir = Directory.getByPath(dc, new Path (pathString, Path.Type.DIRECTORY));
    159         zipDirectory(zipOut, Directory.getByPath(dc, dir.getPath()));
    160         //Set the removed flag to true if this has been defined in the job configuration
    161         dir.setRemoved(removeItems ? true : dir.isRemoved());
     159    FilePacker packer = packers.get(compressionFormat);
    162160       
    163       }
    164       zipOut.flush();
    165     }
    166     //The files should be compressed in tar-format
    167     else if (compressionFormat.equals(FORMAT_TAR))
    168     {
    169       //TODO
    170     }
    171   }
     161    if (packer == null)
     162    {
     163      throw new BaseException("Packing tool could not be found");
     164    }
     165    // Progressreporter showing how many of the seleced items have been processed.
     166    AbsoluteProgressReporter absoluteProgress = progress == null ?
     167        null : new SimpleAbsoluteProgressReporter(progress, (filePaths.size() + directoryPaths.size()));
     168    packer.setRemoveAfter(removeItems);
     169    out.setMimeType(packer.getMimeType());
     170    compressedItems = packer.pack(dc, out, filePaths, directoryPaths, absoluteProgress);
     171  }
     172 
     173  /*
     174      @see net.sf.basedb.core.plugin.AbstractExporterPlugin#end(boolean)
     175  */
    172176  protected void end(boolean success)
    173177  {
     
    175179  }
    176180 
     181  /*
     182      @see net.sf.basedb.core.plugin.AbstractExporterPlugin#getSuccessMessage()
     183  */
    177184  protected String getSuccessMessage()
    178185  {
    179     return compressedFiles + " file(s) compressed successfully";
     186    return compressedItems + " of the selected directories/files were compressed successfully";
    180187  }
    181188
     
    184191        net.sf.basedb.core.plugin.Request, net.sf.basedb.core.plugin.Response)
    185192  */
     193  @SuppressWarnings("unchecked")
    186194  public void configure(GuiContext context, Request request, Response response)
    187195  {
     
    198206          return;
    199207        }
    200 
    201         // Store the parameter values for the job
     208        if (request.getParameterValues("files").isEmpty() && request.getParameterValues("directories").isEmpty())
     209        {
     210          response.setError("Select at least one file or directory to pack", null);
     211          return;
     212        }
     213
     214        // Store first part of the parameter values for the job
    202215        storeValue(job, request, ri.getParameter("fileFormat"));
    203216        storeValue(job, request, ri.getParameter("files"));
     
    215228        }
    216229       
    217         String fileExtension = null;
    218         String fileFormat = (String)job.getValue(formatParameter.getName());
    219         if (fileFormat.equals(FORMAT_ZIP))
    220         {
    221           fileExtension = ".zip";
    222         }
    223         else if (fileFormat.equals(FORMAT_TAR))
    224         {
    225           fileExtension = ".tar";
    226         }
     230        String fileExtension = packers.get((String)job.getValue(formatParameter.getName())).getFileExtension();         
    227231        if (request.getParameterValue(SAVE_AS) == null)
    228232        {
     
    236240        else
    237241        {
     242          //The finished archive file cannot be saved inside one of the selected directories.
     243          if (isFileInDirectories((String)request.getParameterValue(SAVE_AS), (List<String>)job.getValues("directories")))
     244          {
     245            response.setError("The compressed file cannot be saved inside a directory that are going to be archived", null);
     246            return;
     247          }
    238248          if (!pathCanBeUsed((String)request.getParameterValue(ri.getParameter(SAVE_AS).getName()),
    239249              (Boolean)request.getParameterValue(ri.getParameter(OVERWRITE).getName())))
     
    242252                " already exists in specified format", null);
    243253            return;
    244           }         
    245           storeValue(job, request, ri.getParameter(SAVE_AS));
     254          }
     255          // The finished file needs to have right file extension otherwise it won't be
     256          //readable for other packing tools.
     257          if (!((String)request.getParameterValue(SAVE_AS)).endsWith(fileExtension))
     258          {
     259            job.setValue(SAVE_AS,
     260                (ParameterType<String>)ri.getParameter(SAVE_AS).getParameterType(),
     261                ((String)request.getParameterValue(SAVE_AS) + "." + fileExtension));
     262          }
     263          else
     264          {
     265            storeValue(job, request, ri.getParameter(SAVE_AS));
     266          }
    246267          storeValue(job, request, ri.getParameter(OVERWRITE));
    247           storeValue(job, request, ri.getParameter("removeItems"));
    248          
     268          storeValue(job, request, ri.getParameter("removeItems"));         
    249269          response.setDone("Job configuration is done", ExecutionTime.SHORT);
    250270        }
     
    255275      response.setError(th.getMessage(), Arrays.asList(th));
    256276    }
    257 
    258277  }
    259278
     
    330349 
    331350  /**
    332     Configures the job parameters for this plugin
    333     @param requireFile If the job must have file path-the plugin
    334       is not allowed to download immediately
     351    Configures the first part of the job parameters for this plugin   
    335352      @return {@link #configureJob}
    336353   */
     
    347364        // Format parameter   
    348365        Enumeration<String, String> formats = new Enumeration<String, String>();
    349         formats.add(FORMAT_ZIP, "Zip-format (.zip)");
    350         formats.add(FORMAT_TAR, "Tar-format (.tar)");
     366        formats.add(FORMAT_ZIP, "Zip-archive (.zip)");
     367        formats.add(FORMAT_TAR_GZ, "GZipped Tar-archive (.tar.gz)");
     368        formats.add(FORMAT_BZ, "BZipped Tar-archive (.tar.bz)");
    351369        formatParameter = new PluginParameter<String>
    352370          (
     
    402420            "Files, directories and format",
    403421            "Set which files and diretories that should be compressed" +
    404             "and the compression format to use when packing the files.",
     422            " and the compression format to use.",
    405423            parameters
    406424          );
     
    417435  }
    418436 
     437  /**
     438    Configures the second part of the job parameters for this plugin
     439      @param requireFile True if this plugin cannot download immediately, FALSE otherwise
     440      @return RequestInformation with configured parameters
     441  */
    419442  private RequestInformation getConfigurePackingParameters(Boolean requireFile)
    420443  {
     
    434457      parameters.add(removeFilesParameter);
    435458     
    436       String fileExtension = null;
    437       String fileFormat = (String)job.getValue(formatParameter.getName());
    438       if (fileFormat.equals(FORMAT_ZIP))
    439       {
    440         fileExtension = ".zip";
    441       }
    442       else if (fileFormat.equals(FORMAT_TAR))
    443       {
    444         fileExtension = ".tar";
    445       }
     459      String fileExtension = packers.get((String)job.getValue(formatParameter.getName())).getFileExtension();
    446460     
    447461      // Save as and overwrite parameter
     
    451465        requireFile = !pd.getAllowImmediateExecution();
    452466      }
    453       String defaultPath = "~/compressedFile" + fileExtension;     
     467      String defaultPath = "~/compressedFile." + fileExtension;     
    454468      parameters.add(getSaveAsParameter(null, null, defaultPath, requireFile));
    455469      parameters.add(getOverwriteParameter(null, null));
     
    467481 
    468482  /**
    469     Compress a file to a zip outputstream
    470       @param zipOut The ZipOutputStream to compress the file to
    471       @param file File to compress
    472       @param relatviePath relative path to file from the root of the zip file
    473   */
    474   private void zipFile(ZipOutputStream zipOut, File file, String relativePath)
    475   {
    476     InputStream in = file.getDownloadStream(0);   
    477     ZipEntry entry = new ZipEntry((relativePath == null ? "" : relativePath) + file.getName());
    478     try
    479     {
    480       zipOut.putNextEntry(entry);
    481      
    482       FileUtil.copy(in, zipOut);
    483       zipOut.closeEntry();
    484       in.close();
    485       compressedFiles++;
    486     }
    487     catch (IOException e)
    488     {
    489       compressedFailed++;
    490     }
    491   }
    492   /**
    493     Compress a directory to a zip outputstream.
    494     This method will call itself for each subfolder
    495     and add it recursively.
    496       @param zipOut The ZipOutPutStream to compress the directory to
    497       @param dir The directory that should be compressed
    498   */
    499   private void zipDirectory(ZipOutputStream zipOut, Directory dir)
    500   {
    501     ItemResultList<File> files = dir.getFiles().list(dc);
    502     ItemResultList<Directory> subDirectories = dir.getSubDirectories().list(dc);
    503        
    504     String parentPath = dir.getParent().getPath().toString();
    505     for (File file : files)
    506     {
    507       String filePath = file.getPath().toString();
    508       String relFilePath = filePath.substring(parentPath.length());
    509       System.out.println(relFilePath);
    510       zipFile(zipOut, file, relFilePath);
    511     }
    512     for (Directory subDir : subDirectories)
    513     {
    514       zipDirectory(zipOut, subDir);
    515     }   
     483    Checks if filepath is inside any of listed directories
     484      @param filePath the file path to check
     485      @param directories a list with directories.
     486      @return FALSE if the file isn't inside any of the directories, TRUE otherwise.
     487   */
     488  private boolean isFileInDirectories(String filePath, List<String> directories)
     489  {
     490    boolean isInDirectory = false;
     491    String fileDir = filePath.substring(0, filePath.lastIndexOf("/"));
     492    if ( (directories.size() > 0) && (fileDir.length() > 0))
     493    {           
     494      for (String dir : directories)
     495      {
     496        if (fileDir.contains(dir))
     497        {
     498          isInDirectory = true;
     499        }   
     500      }
     501    }
     502    return isInDirectory;
    516503  }
    517504}
Note: See TracChangeset for help on using the changeset viewer.