Skip to content

Commit c3b1642

Browse files
author
jdcasey
committed
Adding archive filters and archive finalizers (both need testing).
1 parent 869770c commit c3b1642

15 files changed

+472
-88
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.codehaus.plexus.archiver;
2+
3+
public abstract class AbstractArchiveFinalizer
4+
implements ArchiveFinalizer
5+
{
6+
7+
protected AbstractArchiveFinalizer()
8+
{
9+
}
10+
11+
public void finalizeArchiveCreation( Archiver archiver )
12+
throws ArchiverException
13+
{
14+
}
15+
16+
public void finalizeArchiveExtraction( UnArchiver unarchiver )
17+
throws ArchiverException
18+
{
19+
}
20+
21+
}

src/main/java/org/codehaus/plexus/archiver/AbstractArchiver.java

Lines changed: 104 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,31 @@
1717
* limitations under the License.
1818
*/
1919

20+
import java.io.File;
21+
import java.io.IOException;
22+
import java.util.LinkedHashMap;
23+
import java.util.Map;
24+
25+
import org.codehaus.plexus.PlexusConstants;
26+
import org.codehaus.plexus.PlexusContainer;
27+
import org.codehaus.plexus.archiver.manager.ArchiverManager;
28+
import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
29+
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
30+
import org.codehaus.plexus.context.Context;
31+
import org.codehaus.plexus.context.ContextException;
2032
import org.codehaus.plexus.logging.AbstractLogEnabled;
2133
import org.codehaus.plexus.logging.Logger;
2234
import org.codehaus.plexus.logging.console.ConsoleLogger;
35+
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
2336
import org.codehaus.plexus.util.DirectoryScanner;
24-
25-
import java.io.File;
26-
import java.util.LinkedHashMap;
27-
import java.util.Map;
37+
import org.codehaus.plexus.util.FileUtils;
2838

2939
/**
3040
* @version $Id$
3141
*/
3242
public abstract class AbstractArchiver
3343
extends AbstractLogEnabled
34-
implements Archiver
44+
implements Archiver, Contextualizable
3545
{
3646
/**
3747
* Default value for the dirmode attribute.
@@ -57,6 +67,9 @@ public abstract class AbstractArchiver
5767

5868
private int defaultDirectoryMode = DEFAULT_DIR_MODE;
5969

70+
// contextualized.
71+
private ArchiverManager archiverManager;
72+
6073
public void setDefaultFileMode( int mode )
6174
{
6275
defaultFileMode = ( mode & UnixStat.PERM_MASK ) | UnixStat.FILE_FLAG;
@@ -131,16 +144,18 @@ public void addDirectory( File directory, String prefix, String[] includes, Stri
131144

132145
if ( includeEmptyDirs )
133146
{
134-
String [] dirs = scanner.getIncludedDirectories();
147+
String[] dirs = scanner.getIncludedDirectories();
135148

136149
for ( int i = 0; i < dirs.length; i++ )
137150
{
138151
String sourceDir = dirs[i].replace( '\\', '/' );
139152

140153
String targetDir = ( prefix == null ? "" : prefix ) + sourceDir;
141154

142-
getDirs().put( targetDir, ArchiveEntry.createEntry( targetDir, new File( basedir, sourceDir ),
143-
getDefaultFileMode(), getDefaultDirectoryMode() ) );
155+
getDirs().put(
156+
targetDir,
157+
ArchiveEntry.createEntry( targetDir, new File( basedir, sourceDir ),
158+
getDefaultFileMode(), getDefaultDirectoryMode() ) );
144159
}
145160
}
146161

@@ -232,4 +247,85 @@ public Map getDirs()
232247
{
233248
return dirsMap;
234249
}
250+
251+
/**
252+
* @since 1.0-alpha-7
253+
*/
254+
public void addArchivedFileSet( File archiveFile, String prefix, String[] includes, String[] excludes )
255+
throws ArchiverException
256+
{
257+
UnArchiver unArchiver;
258+
try
259+
{
260+
unArchiver = archiverManager.getUnArchiver( archiveFile );
261+
}
262+
catch ( NoSuchArchiverException e )
263+
{
264+
throw new ArchiverException( "Error adding archived file-set. UnArchiver not found for: " + archiveFile, e );
265+
}
266+
267+
File tempDir = FileUtils.createTempFile( "archived-file-set.", ".tmp", null );
268+
269+
tempDir.mkdirs();
270+
271+
unArchiver.setSourceFile( archiveFile );
272+
unArchiver.setDestDirectory( tempDir );
273+
274+
try
275+
{
276+
unArchiver.extract();
277+
}
278+
catch ( IOException e )
279+
{
280+
throw new ArchiverException( "Error adding archived file-set. Failed to extract: " + archiveFile, e );
281+
}
282+
283+
addDirectory( tempDir, prefix, includes, excludes );
284+
}
285+
286+
/**
287+
* @since 1.0-alpha-7
288+
*/
289+
public void addArchivedFileSet( File archiveFile, String prefix )
290+
throws ArchiverException
291+
{
292+
addArchivedFileSet( archiveFile, prefix, null, null );
293+
}
294+
295+
/**
296+
* @since 1.0-alpha-7
297+
*/
298+
public void addArchivedFileSet( File archiveFile, String[] includes, String[] excludes )
299+
throws ArchiverException
300+
{
301+
addArchivedFileSet( archiveFile, null, includes, excludes );
302+
}
303+
304+
/**
305+
* @since 1.0-alpha-7
306+
*/
307+
public void addArchivedFileSet( File archiveFile )
308+
throws ArchiverException
309+
{
310+
addArchivedFileSet( archiveFile, null, null, null );
311+
}
312+
313+
/**
314+
* Allows us to pull the ArchiverManager instance out of the container without
315+
* causing a chicken-and-egg instantiation/composition problem.
316+
*/
317+
public void contextualize( Context context )
318+
throws ContextException
319+
{
320+
PlexusContainer container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
321+
322+
try
323+
{
324+
archiverManager = (ArchiverManager) container.lookup( ArchiverManager.ROLE );
325+
}
326+
catch ( ComponentLookupException e )
327+
{
328+
throw new ContextException( "Error retrieving ArchiverManager instance: " + e.getMessage(), e );
329+
}
330+
}
235331
}

src/main/java/org/codehaus/plexus/archiver/AbstractUnArchiver.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
* limitations under the License.
1818
*/
1919

20-
import org.codehaus.plexus.logging.AbstractLogEnabled;
21-
2220
import java.io.File;
2321
import java.io.IOException;
2422

23+
import org.codehaus.plexus.logging.AbstractLogEnabled;
24+
2525
/**
2626
* @author <a href="mailto:[email protected]">Emmanuel Venisse</a>
2727
* @version $Revision$ $Date$
@@ -98,7 +98,7 @@ protected void validate()
9898
throw new ArchiverException( "The source must not be a directory." );
9999
}
100100

101-
if ( ! sourceFile.exists() )
101+
if ( !sourceFile.exists() )
102102
{
103103
throw new ArchiverException( "The source doesn't exists." );
104104
}
@@ -113,7 +113,7 @@ protected void validate()
113113
throw new ArchiverException( "You must choose between a destination directory and a destination file." );
114114
}
115115

116-
if ( destDirectory != null && ! destDirectory.isDirectory() )
116+
if ( destDirectory != null && !destDirectory.isDirectory() )
117117
{
118118
destFile = destDirectory;
119119
destDirectory = null;
@@ -128,4 +128,5 @@ protected void validate()
128128

129129
protected abstract void execute()
130130
throws ArchiverException, IOException;
131+
131132
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.codehaus.plexus.archiver;
2+
3+
import java.io.InputStream;
4+
5+
public interface ArchiveFileFilter
6+
{
7+
8+
boolean include( InputStream dataStream, String entryName )
9+
throws ArchiveFilterException;
10+
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.codehaus.plexus.archiver;
2+
3+
public class ArchiveFilterException
4+
extends Exception
5+
{
6+
7+
public ArchiveFilterException( String message, Throwable cause )
8+
{
9+
super( message, cause );
10+
}
11+
12+
public ArchiveFilterException( String message )
13+
{
14+
super( message );
15+
}
16+
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.codehaus.plexus.archiver;
2+
3+
public interface ArchiveFinalizer
4+
{
5+
6+
void finalizeArchiveCreation( Archiver archiver )
7+
throws ArchiverException;
8+
9+
void finalizeArchiveExtraction( UnArchiver unarchiver )
10+
throws ArchiverException;
11+
12+
}

src/main/java/org/codehaus/plexus/archiver/Archiver.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ void addFile( File inputFile, String destFileName )
4949
void addFile( File inputFile, String destFileName, int permissions )
5050
throws ArchiverException;
5151

52+
void addArchivedFileSet( File archiveFile )
53+
throws ArchiverException;
54+
55+
void addArchivedFileSet( File archiveFile, String prefix )
56+
throws ArchiverException;
57+
58+
void addArchivedFileSet( File archiveFile, String[] includes, String[] excludes )
59+
throws ArchiverException;
60+
61+
void addArchivedFileSet( File archiveFile, String prefix, String[] includes, String[] excludes )
62+
throws ArchiverException;
63+
5264
File getDestFile();
5365

5466
void setDestFile( File destFile );
@@ -64,6 +76,6 @@ void addFile( File inputFile, String destFileName, int permissions )
6476
boolean getIncludeEmptyDirs();
6577

6678
void setIncludeEmptyDirs( boolean includeEmptyDirs );
67-
79+
6880
Map getFiles();
6981
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.codehaus.plexus.archiver;
2+
3+
import java.util.List;
4+
5+
public interface FilterEnabled
6+
{
7+
8+
void setArchiveFilters( List filters );
9+
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.codehaus.plexus.archiver;
2+
3+
import java.util.List;
4+
5+
public interface FinalizerEnabled
6+
{
7+
8+
void setArchiveFinalizers( List archiveFinalizers );
9+
10+
}

src/main/java/org/codehaus/plexus/archiver/UnArchiver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public interface UnArchiver
2929

3030
void extract()
3131
throws ArchiverException, IOException;
32-
32+
3333
File getDestDirectory();
3434

3535
void setDestDirectory( File destDirectory );
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.codehaus.plexus.archiver.filters;
2+
3+
import java.io.InputStream;
4+
5+
import org.codehaus.plexus.archiver.ArchiveFileFilter;
6+
import org.codehaus.plexus.archiver.ArchiveFilterException;
7+
import org.codehaus.plexus.util.SelectorUtils;
8+
9+
public class JarSecurityFileFilter
10+
implements ArchiveFileFilter
11+
{
12+
13+
public static final String[] SECURITY_FILE_PATTERNS = {
14+
"/META-INF/*.RSA",
15+
"/META-INF/*.DSA",
16+
"/META-INF/*.SF",
17+
"/META-INF/*.rsa",
18+
"/META-INF/*.dsa",
19+
"/META-INF/*.sf"
20+
};
21+
22+
public boolean include( InputStream dataStream, String entryName )
23+
throws ArchiveFilterException
24+
{
25+
for ( int i = 0; i < SECURITY_FILE_PATTERNS.length; i++ )
26+
{
27+
String pattern = SECURITY_FILE_PATTERNS[i];
28+
29+
if ( SelectorUtils.match( pattern, entryName ) )
30+
{
31+
return false;
32+
}
33+
}
34+
35+
return true;
36+
}
37+
}

src/main/java/org/codehaus/plexus/archiver/jar/JarArchiver.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@
1717
* limitations under the License.
1818
*/
1919

20-
import org.codehaus.plexus.archiver.ArchiverException;
21-
import org.codehaus.plexus.archiver.util.EnumeratedAttribute;
22-
import org.codehaus.plexus.archiver.zip.ZipArchiver;
23-
import org.codehaus.plexus.archiver.zip.ZipEntry;
24-
import org.codehaus.plexus.archiver.zip.ZipFile;
25-
import org.codehaus.plexus.archiver.zip.ZipOutputStream;
26-
2720
import java.io.ByteArrayInputStream;
2821
import java.io.ByteArrayOutputStream;
2922
import java.io.File;
@@ -47,6 +40,13 @@
4740
import java.util.TreeMap;
4841
import java.util.Vector;
4942

43+
import org.codehaus.plexus.archiver.ArchiverException;
44+
import org.codehaus.plexus.archiver.util.EnumeratedAttribute;
45+
import org.codehaus.plexus.archiver.zip.ZipArchiver;
46+
import org.codehaus.plexus.archiver.zip.ZipEntry;
47+
import org.codehaus.plexus.archiver.zip.ZipFile;
48+
import org.codehaus.plexus.archiver.zip.ZipOutputStream;
49+
5050
/**
5151
* Base class for tasks that build archives in JAR file format.
5252
*
@@ -140,7 +140,7 @@ public class JarArchiver
140140
* Path containing jars that shall be indexed in addition to this archive.
141141
*/
142142
private ArrayList indexJars;
143-
143+
144144
/**
145145
* constructor
146146
*/

0 commit comments

Comments
 (0)