View Javadoc
1   package org.codehaus.plexus.archiver;
2   
3   import java.io.BufferedReader;
4   import java.io.File;
5   import java.io.FileReader;
6   import java.io.IOException;
7   import java.util.Collections;
8   import java.util.List;
9   import org.codehaus.plexus.util.FileUtils;
10  import org.codehaus.plexus.util.IOUtil;
11  import org.codehaus.plexus.util.StringUtils;
12  
13  /**
14   * An {@link ArchiveFinalizer} that process dot files with archiver directives
15   * contained within. This basically means you can communicate archive creation
16   * instructions between processes using dot files.
17   *
18   * @author Jason van Zyl
19   */
20  public class DotDirectiveArchiveFinalizer
21      extends AbstractArchiveFinalizer
22  {
23  
24      private static final String DEFAULT_DOT_FILE_PREFIX = ".plxarc";
25  
26      private final File dotFileDirectory;
27  
28      private final String dotFilePrefix;
29  
30      public DotDirectiveArchiveFinalizer( File dotFileDirectory )
31      {
32          this( dotFileDirectory, DEFAULT_DOT_FILE_PREFIX );
33      }
34  
35      public DotDirectiveArchiveFinalizer( File dotFileDirectory, String dotFilePrefix )
36      {
37          this.dotFileDirectory = dotFileDirectory;
38  
39          this.dotFilePrefix = dotFilePrefix;
40      }
41  
42      @Override
43      public void finalizeArchiveCreation( Archiver archiver )
44          throws ArchiverException
45      {
46          BufferedReader in = null;
47          try
48          {
49              List dotFiles = FileUtils.getFiles( dotFileDirectory, dotFilePrefix + "*", null );
50  
51              for ( Object dotFile1 : dotFiles )
52              {
53                  File dotFile = (File) dotFile1;
54  
55                  in = new BufferedReader( new FileReader( dotFile ) );
56  
57                  for ( String line = in.readLine(); line != null; line = in.readLine() )
58                  {
59                      String[] s = StringUtils.split( line, ":" );
60  
61                      if ( s.length == 1 )
62                      {
63                          File directory = new File( dotFileDirectory, s[0] );
64  
65                          System.out.println( "adding directory = " + directory );
66  
67                          archiver.addDirectory( directory );
68                      }
69                      else
70                      {
71                          File directory = new File( dotFileDirectory, s[0] );
72  
73                          System.out.println( "adding directory = " + directory + " to: " + s[1] );
74  
75                          if ( s[1].endsWith( "/" ) )
76                          {
77                              archiver.addDirectory( directory, s[1] );
78                          }
79                          else
80                          {
81                              archiver.addDirectory( directory, s[1] + "/" );
82                          }
83                      }
84                  }
85  
86                  in.close();
87                  in = null;
88              }
89          }
90          catch ( IOException e )
91          {
92              throw new ArchiverException( "Error processing dot files.", e );
93          }
94          finally
95          {
96              IOUtil.close( in );
97          }
98      }
99  
100     @Override
101     public List getVirtualFiles()
102     {
103         return Collections.EMPTY_LIST;
104     }
105 
106 }