Changeset 3646


Ignore:
Timestamp:
Aug 9, 2007, 9:46:56 AM (14 years ago)
Author:
Nicklas Nordborg
Message:

References #451: Zipped download

Fixed test program and added it to TestAll?. Fixed some bugs resulting from problems with closing
the TAR stream properly.

Location:
trunk
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/doc/src/docbook/developerdoc/plugin_developer.xml

    r3643 r3646  
    39483948            <para>
    39493949            Finish the packing. The packer should release any resources, flush
    3950             all data. It should not close the <varname>out</varname> stream
    3951             set in the <methodname>setOutputStream</methodname> method since
    3952             this is done by the calling code.
     3950            all data and close all output streams, including the <varname>out</varname> stream
     3951            set in the <methodname>setOutputStream</methodname> method.
    39533952            </para>
    39543953          </listitem>
  • trunk/src/core/net/sf/basedb/core/plugin/ExportOutputStream.java

    r2981 r3646  
    8888    out.write(b);
    8989  }
     90  @Override
    9091  public void write(int b)
    9192    throws IOException
  • trunk/src/core/net/sf/basedb/util/zip/Bzip2FilePacker.java

    r3643 r3646  
    9393    throws IOException
    9494  {
    95     bzip.flush();
     95    super.close();
    9696    bzip = null;
    9797  }
  • trunk/src/core/net/sf/basedb/util/zip/FilePacker.java

    r3643 r3646  
    8888 
    8989  /**
    90     End the packing and close. The plug-in should close any open resources and
    91     cleanup temporary objects. This method should not close the output stream
    92     set by the {@link #setOutputStream(OutputStream)} method, since this is
    93     the responsibility of the calling code.
     90    End the packing and close. The plug-in should close any open resources, including
     91    the output stream set by the {@link #setOutputStream(OutputStream)} method, and
     92    cleanup temporary objects.
    9493   
    9594      @throws IOException If there are any errors
  • trunk/src/core/net/sf/basedb/util/zip/GzipFilePacker.java

    r3643 r3646  
    8989    throws IOException
    9090  {
    91     gzip.finish();
     91    super.close();
    9292    gzip = null;
    9393  }
  • trunk/src/core/net/sf/basedb/util/zip/TarFilePacker.java

    r3643 r3646  
    9999    tar.putNextEntry(entry);
    100100    if (!isDirectory) FileUtil.copy(in, tar);
    101     tar.flush();
    102101    tar.closeEntry();
    103102  }
     
    108107    throws IOException
    109108  {
    110     tar.finish();
     109    tar.close();
    111110    tar = null;
    112111  }
  • trunk/src/core/net/sf/basedb/util/zip/ZipFilePacker.java

    r3641 r3646  
    116116    throws IOException
    117117  {
    118     zip.finish();
     118    zip.close();
    119119    zip = null;
    120120  }
  • trunk/src/plugins/core/net/sf/basedb/plugins/PackedFileExporter.java

    r3643 r3646  
    422422   
    423423    packer.close();
     424   
    424425    if (progress != null)
    425426    {
     
    453454        ItemContext fileContext = sc.getCurrentContext(Item.FILE);
    454455        ItemContext dirContext = sc.getCurrentContext(Item.DIRECTORY);
    455         Directory currentDir = Directory.getById(dc, dirContext.getId());
     456        Directory currentDir = null;
     457        try
     458        {
     459          currentDir = Directory.getById(dc, dirContext.getId());
     460        }
     461        catch (Throwable t)
     462        {}
     463       
    456464        selectedFilesAndDirs = new LinkedList<Nameable>();
    457465        loadFilesAndDirectories(dc, selectedFilesAndDirs, fileContext.getSelected(), dirContext.getSelected(), false);
     
    648656  private String generateDefaultPath(FilePacker packer, Directory rootDir, Collection<Nameable> selectedItems)
    649657  {
    650     String defaultPath = rootDir.getPath().toString() + "/" + generateDefaultName(packer, rootDir, selectedItems);
     658    String defaultPath = rootDir == null ? "~/" : rootDir.getPath().toString() + "/";
     659    defaultPath +=  generateDefaultName(packer, rootDir, selectedItems);
    651660    return defaultPath;
    652661  }
     
    656665    @see #generateDefaultPath(FilePacker, Directory, Collection)
    657666  */
    658   private String generateDefaultName(FilePacker packer, Directory currentDir, Collection<Nameable> selectedItems)
     667  private String generateDefaultName(FilePacker packer, Directory rootDir, Collection<Nameable> selectedItems)
    659668  {
    660669    String defaultName = "";
     
    663672      defaultName += selectedItems.iterator().next().getName();
    664673    }
     674    else if (rootDir != null)
     675    {
     676      defaultName += rootDir.getName();
     677    }
    665678    else
    666679    {
    667       defaultName += currentDir.getName();
     680      defaultName += "packed";
    668681    }
    669682    defaultName += "."+packer.getFileExtension();
  • trunk/src/test/TestAll.java

    r3626 r3646  
    6363    results.put("TestFile", TestFile.test_all());
    6464    results.put("TestDiskUsage", TestDiskUsage.test_all());
     65    results.put("TestPackedFileExporter", TestPackedFileExporter.test_all());
    6566
    6667    // Hardware, software, protocols
  • trunk/src/test/TestPackedFileExporter.java

    r3616 r3646  
    2525import net.sf.basedb.core.BaseException;
    2626import net.sf.basedb.core.DbControl;
     27import net.sf.basedb.core.Directory;
    2728import net.sf.basedb.core.File;
    2829import net.sf.basedb.core.Item;
     30import net.sf.basedb.core.ItemContext;
    2931import net.sf.basedb.core.Job;
    3032import net.sf.basedb.core.ParameterType;
    3133import net.sf.basedb.core.Path;
    3234import net.sf.basedb.core.Permission;
     35import net.sf.basedb.core.PluginConfiguration;
    3336import net.sf.basedb.core.PluginConfigurationRequest;
    3437import net.sf.basedb.core.PluginDefinition;
     
    3639import net.sf.basedb.core.PluginResponse;
    3740import net.sf.basedb.core.RequestInformation;
     41import net.sf.basedb.core.SessionControl;
     42import net.sf.basedb.core.plugin.GuiContext;
    3843import net.sf.basedb.core.plugin.Response;
    3944
    40 import java.io.IOException;
     45import java.io.InputStream;
    4146import java.util.ArrayList;
    4247import java.util.List;
     
    4550import java.util.zip.ZipInputStream;
    4651
     52import org.apache.tools.bzip2.CBZip2InputStream;
     53
    4754import com.ice.tar.TarEntry;
    4855import com.ice.tar.TarInputStream;
     
    6774    write("++Testing TestPackedFileExporter");
    6875    String packedDir = "packedFiles";
    69     //Prepare lists, one with filepaths and one with directory paths
     76    // Directory where packed files are created
    7077    int packedDirId = TestDirectory.test_create(true, packedDir);
     78   
     79    // Upload zip file to '/unpackzip'
    7180    int dirId = TestDirectory.test_create(true, "unpackzip");
    7281    TestFile.test_unpack_zip("data/test.unpack.zip", dirId);
     82   
     83    // Files and directories to pack
    7384    List<String> filePaths = new ArrayList<String>();
     85    List<String> directoryPaths = new ArrayList<String>();
    7486    filePaths.add("/unpackzip/index.html");
    75     List<String> directoryPaths = new ArrayList<String>();
    7687    directoryPaths.add("/unpackzip/images");
    77    
    78     String tarGz = "/" + packedDir + "/test.compressed.file.tar.gz";
    79     String zip = "/" + packedDir + "/test.compressed.file.zip";
    80    
     88    set_selected(filePaths, directoryPaths);
     89   
     90    int pluginId = TestPluginDefinition.test_get("net.sf.basedb.plugins.PackedFileExporter");
     91
    8192    // Create job to zip files
    82     Path zipPath = new Path(zip, Path.Type.FILE);
    83     int zipJobId = test_create_zipJob(filePaths, directoryPaths, zipPath.toString());
    84     TestJob.test_execute(zipJobId);
     93    int zipConfig = test_create_config(pluginId, "Zip", "net.sf.basedb.util.zip.ZipFilePacker");
     94    Path zip = new Path("/" + packedDir + "/test.compressed.file.zip", Path.Type.FILE);
     95    int zipJobId = test_create_job(zipConfig, dirId, zip.toString());
     96    ok = TestJob.test_execute(zipJobId) && ok;
     97    test_read_zip(zip);
     98
     99    // Create job to pack files in tar
     100    int tarConfig = test_create_config(pluginId, "Tar", "net.sf.basedb.util.zip.TarFilePacker");
     101    Path tar = new Path("/" + packedDir + "/test.compressed.file.tar", Path.Type.FILE);
     102    int tarJobId = test_create_job(tarConfig, dirId, tar.toString());
     103    ok = TestJob.test_execute(tarJobId) && ok;
     104    test_read_tar(tar, false, false);
    85105   
    86106    // Create job to pack files in tar.gz
    87     Path tarGzPath = new Path(tarGz, Path.Type.FILE);
    88     int tarJobId = test_create_tarJob(filePaths, directoryPaths, tarGzPath.toString());
    89     TestJob.test_execute(tarJobId);
    90      
    91     // Test to read the content of the zip file
    92     test_read_zip(zipPath);
    93     test_read_targz(tarGzPath);
     107    int tarGzConfig = test_create_config(pluginId, "Tar.gz", "net.sf.basedb.util.zip.GzipFilePacker");
     108    Path tarGz = new Path("/" + packedDir + "/test.compressed.file.tar.gz", Path.Type.FILE);
     109    int tarGzJobId = test_create_job(tarGzConfig, dirId, tarGz.toString());
     110    ok = TestJob.test_execute(tarGzJobId) && ok;
     111    test_read_tar(tarGz, true, false);
     112   
     113    // Create job to pack files in tar.bz2
     114    int tarBz2Config = test_create_config(pluginId, "Tar.bz2", "net.sf.basedb.util.zip.Bzip2FilePacker");
     115    Path tarBz2 = new Path("/" + packedDir + "/test.compressed.file.tar.bz2", Path.Type.FILE);
     116    int tarBz2JobId = test_create_job(tarBz2Config, dirId, tarBz2.toString());
     117    ok = TestJob.test_execute(tarBz2JobId) && ok;
     118    test_read_tar(tarBz2, false, true);
    94119   
    95120    // Clean up
    96121    if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter();
    97     TestDirectory.test_delete(packedDirId, true);
    98122    TestJob.test_delete(zipJobId);
    99123    TestJob.test_delete(tarJobId);
     124    TestJob.test_delete(tarGzJobId);
     125    TestJob.test_delete(tarBz2JobId);
     126    TestPluginConfiguration.test_delete(zipConfig);
     127    TestPluginConfiguration.test_delete(tarConfig);
     128    TestPluginConfiguration.test_delete(tarGzConfig);
     129    TestPluginConfiguration.test_delete(tarBz2Config);
     130    TestDirectory.test_delete(packedDirId, true);
    100131    TestDirectory.test_delete(dirId, true);   
    101132       
     
    104135  }
    105136
    106   static int test_create_zipJob(List<String> filePaths, List<String> directoryPaths, String filePath)
    107   {
    108     if (!TestUtil.hasPermission(Permission.CREATE, Item.JOB)) return 0;
     137  static int test_create_config(int pluginId, String name, String packerClass)
     138  {
     139    if (pluginId == 0 || !TestUtil.hasPermission(Permission.CREATE, Item.PLUGINCONFIGURATION)) return 0;
    109140    int id = 0;
    110141    DbControl dc = null;
     
    112143    {
    113144      dc = TestUtil.getDbControl();
     145
     146      PluginDefinition pd = PluginDefinition.getById(dc, pluginId);
     147      PluginConfiguration pc = pd.newPluginConfiguration();
     148      pc.setName(name);
     149     
     150      PluginConfigurationRequest request = pc.configure();
     151      write_request_information(request.getRequestInformation());
     152      request.setParameterValue("packer", packerClass);
     153     
     154      PluginResponse response = request.invoke();
     155      if (response.getStatus() == Response.Status.DONE)
     156      {
     157        response.saveParameters(dc);
     158        dc.saveItem(pc);
     159        dc.commit();
     160      }
     161      else
     162      {
     163        throw new BaseException(response.getMessage(), response.getErrorList().get(0));
     164      }
     165      id = pc.getId();
     166      write("--Create configuration for " + name + " file export OK");
     167    }
     168    catch (Throwable ex)
     169    {
     170      write("--Create configuration for " + name  + " file export FAILED");
     171      ex.printStackTrace();
     172      ok = false;
     173    }
     174    finally
     175    {
     176      if (dc != null) dc.close();
     177    }
     178    return id;
     179  }
     180 
     181  static void set_selected(List<String> filePaths, List<String> dirPaths)
     182  {
     183    DbControl dc = null;
     184    try
     185    {
     186      dc = TestUtil.getDbControl();
     187      SessionControl sc = TestUtil.getSessionControl();
     188      ItemContext fileCC = sc.getCurrentContext(Item.FILE);
     189      ItemContext dirCC = sc.getCurrentContext(Item.DIRECTORY);
     190     
     191      for (String path : filePaths)
     192      {
     193        File f = File.getByPath(dc, new Path(path, Path.Type.FILE), false);
     194        fileCC.getSelected().add(f.getId());
     195      }
     196      for (String path : dirPaths)
     197      {
     198        Directory d = Directory.getByPath(dc, new Path(path, Path.Type.DIRECTORY));
     199        dirCC.getSelected().add(d.getId());
     200      }
     201     
     202      write("--Set selected files and directories OK");
     203    }
     204    catch (Throwable ex)
     205    {
     206      write("--Set selected files and directories FAILED");
     207      ex.printStackTrace();
     208      ok = false;
     209    }
     210    finally
     211    {
     212      if (dc != null) dc.close();
     213    }   
     214  }
     215 
     216  static int test_create_job(int configId, int rootDirId, String saveAs)
     217  {
     218    if (!TestUtil.hasPermission(Permission.CREATE, Item.JOB) || configId == 0) return 0;
     219    int id = 0;
     220    DbControl dc = null;
     221    try
     222    {
     223      dc = TestUtil.getDbControl();
    114224      PluginDefinition pd = PluginDefinition.getByClassName(dc, "net.sf.basedb.plugins.PackedFileExporter");
    115       Job job = Job.getNew(dc, pd, null);
    116      
    117       job.setName("Test_zipping");
     225     
     226      PluginConfiguration config = PluginConfiguration.getById(dc, configId);
     227     
     228      Job job = Job.getNew(dc, pd, config);
     229      job.setName("Test " + config.getName());
    118230      job.setSendMessage(false);
    119       PluginConfigurationRequest request = job.configure(null);
    120       request.setParameterValues("files", filePaths);
    121       request.setParameterValues("directories", directoryPaths);
    122       request.setParameterValue("fileFormat", "zip");
     231      PluginConfigurationRequest request = job.configure(new GuiContext(Item.FILE, GuiContext.Type.LIST));
     232     
     233      request.setParameterValue("root", Directory.getById(dc, rootDirId));
     234      request.setParameterValue("overwrite", true);
     235      request.setParameterValue("saveAs", saveAs);
     236      request.setParameterValue("removeItems", false);
     237   
    123238      write_request_information(request.getRequestInformation());
    124       PluginResponse firstResponse = request.invoke();
    125       if (!(firstResponse.getStatus() == Response.Status.CONTINUE))
    126       {
    127         throw new BaseException(firstResponse.getMessage(), firstResponse.getErrorList().get(0));
    128       }
    129       request = firstResponse.getNextRequest();
    130       request.setParameterValue("overwrite", true);
    131       request.setParameterValue("saveAs", filePath);
    132       request.setParameterValue("removeItems", false);
    133    
    134       write_request_information(request.getRequestInformation());
    135       PluginResponse secondResponse = request.invoke();
    136      
    137       if (secondResponse.getStatus() == Response.Status.DONE)
    138       {
    139         secondResponse.saveParameters(dc);
     239      PluginResponse response = request.invoke();
     240     
     241      if (response.getStatus() == Response.Status.DONE)
     242      {
     243        response.saveParameters(dc);
    140244        dc.saveItem(job);
    141245        dc.commit();
     
    143247      else
    144248      {
    145         throw new BaseException("Status: " + secondResponse.getStatus() +"\n" + secondResponse.getMessage(), secondResponse.getErrorList().get(0));
     249        throw new BaseException("Status: " + response.getStatus() +"\n" + response.getMessage(), response.getErrorList().get(0));
    146250      }
    147251      id = job.getId();
    148       write("--Create zip-job for PackedFileExporter OK");
     252      write("--Create job for PackedFileExporter OK");
    149253    }
    150254    catch (Throwable ex)
    151255    {
    152       write("--Create zip-job for PackedFileExporter FAILED");
     256      write("--Create job for PackedFileExporter FAILED");
    153257      ex.printStackTrace();
    154258      ok = false;
     
    196300      File zipFile = File.getByPath(dc, zipPath, false);
    197301      ZipInputStream zios = new ZipInputStream(zipFile.getDownloadStream(0));
    198       write ("--Contents in: " + zipFile.getName());
     302      if (!TestUtil.getSilent()) write ("--Contents in: " + zipFile.getName());
    199303      while ((entry = zios.getNextEntry()) != null)
    200304      {
     
    204308        }
    205309      }
    206       write ("--Read zip-file OK");
    207     }
    208     catch (IOException ioex)
    209     {
    210       write ("--Read zip-file FAILED");
    211       ioex.printStackTrace();
     310      write ("--Read zip-file OK: " + zipPath.toString());
     311    }
     312    catch (Throwable t)
     313    {
     314      write ("--Read zip-file FAILED: " + zipPath.toString());
     315      t.printStackTrace();
    212316      ok = false;
    213317    }
     
    219323 
    220324  /**
    221     Test to read a gzipped tar-file and write the files to prompt.
     325    Test to read a tar-file and write the files to prompt.
    222326      @param tarPath A Path to the file that should be read. 
    223327  */
    224   static void test_read_targz(Path tarPath)
     328  static void test_read_tar(Path tarPath, boolean isGzip, boolean isBzip)
    225329  {
    226330    if (!TestUtil.hasPermission(Permission.READ, Item.FILE)) return;
     
    230334    {
    231335      dc = TestUtil.getDbControl();
    232       File targzFile = File.getByPath(dc, tarPath, false);
    233      
    234       TarInputStream tos = new TarInputStream(new GZIPInputStream(targzFile.getDownloadStream(0)));
    235       write ("--Contents in: " + targzFile.getName());
     336      File tarFile = File.getByPath(dc, tarPath, false);
     337      InputStream download = tarFile.getDownloadStream(0);
     338      if (isGzip) download = new GZIPInputStream(download);
     339      if (isBzip)
     340      {
     341        download.read(); // 'B'
     342        download.read(); // 'Z'
     343        download = new CBZip2InputStream(download);
     344      }
     345      TarInputStream tos = new TarInputStream(download);
     346      if (!TestUtil.getSilent()) write ("--Contents in: " + tarFile.getName());
    236347      while ((entry = tos.getNextEntry()) != null)
    237348      {
     
    242353      }
    243354      tos.close();
    244       write ("--Read tar.gz-file OK");
     355      write ("--Read tar-file OK: " + tarPath.toString());
    245356    }   
    246     catch (IOException ioex)
    247     {
    248       write ("--Read tar.gz-file FAILED");
    249       ioex.printStackTrace();
    250       ok = false;
    251     }
    252     finally
    253     {
    254       if (dc != null) dc.close();
    255     }
    256   }
    257  
    258   /**
    259     Creates a job that will compress files and directories
    260     into a gzipped tar file.
    261       @param filePaths The paths to files that should be compressed.
    262       @param directoryPaths The paths to directories that should be compressed.
    263       @param filePath The path where the archive-file should be saved.
    264       @return the created job's id in BASE
    265    */
    266   static int test_create_tarJob(List<String> filePaths, List<String> directoryPaths, String filePath)
    267   {
    268     if (!TestUtil.hasPermission(Permission.CREATE, Item.JOB)) return 0;
    269     int id = 0;
    270     DbControl dc = null;
    271     try
    272     {
    273       dc = TestUtil.getDbControl();
    274       PluginDefinition pd = PluginDefinition.getByClassName(dc, "net.sf.basedb.plugins.PackedFileExporter");
    275       Job job = Job.getNew(dc, pd, null);
    276      
    277       job.setName("Test_tar.gz");
    278       job.setSendMessage(false);
    279       PluginConfigurationRequest request = job.configure(null);
    280       request.setParameterValues("files", filePaths);
    281       request.setParameterValues("directories", directoryPaths);
    282       request.setParameterValue("fileFormat", "tar.gz");
    283       write_request_information(request.getRequestInformation());
    284       PluginResponse firstResponse = request.invoke();
    285       if (!(firstResponse.getStatus() == Response.Status.CONTINUE))
    286       {
    287         throw new BaseException(firstResponse.getMessage(), firstResponse.getErrorList().get(0));
    288       }
    289       request = firstResponse.getNextRequest();
    290       request.setParameterValue("overwrite", true);
    291       request.setParameterValue("saveAs", filePath);
    292       request.setParameterValue("removeItems", false);
    293    
    294       write_request_information(request.getRequestInformation());
    295       PluginResponse secondResponse = request.invoke();
    296      
    297       if (secondResponse.getStatus() == Response.Status.DONE)
    298       {
    299         secondResponse.saveParameters(dc);
    300         dc.saveItem(job);
    301         dc.commit();
    302       }
    303       else
    304       {
    305         throw new BaseException("Status: " + secondResponse.getStatus() +"\n" + secondResponse.getMessage(), secondResponse.getErrorList().get(0));
    306       }
    307       id = job.getId();
    308       write("--Create tar.gz job for PackedFileExporter OK");
    309     }
    310     catch (Throwable ex)
    311     {
    312       write("--Create tar.gz job for PackedFileExporter FAILED");
    313       ex.printStackTrace();
    314       ok = false;
    315     }
    316     finally
    317     {
    318       if (dc != null) dc.close();
    319     }
    320      
    321     return id;
    322   }
     357    catch (Throwable t)
     358    {
     359      write ("--Read tar-file FAILED: " + tarPath.toString());
     360      t.printStackTrace();
     361      ok = false;
     362    }
     363    finally
     364    {
     365      if (dc != null) dc.close();
     366    }
     367  }
     368
    323369}
Note: See TracChangeset for help on using the changeset viewer.