Skip to content

Commit f33eb29

Browse files
committed
o Added some private code to compare JDK manifest fatures to plexus manifest
1 parent d76b88a commit f33eb29

File tree

3 files changed

+283
-0
lines changed

3 files changed

+283
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package org.codehaus.plexus.archiver.jar;
2+
/**
3+
*
4+
* Copyright 2004 The Apache Software Foundation
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.util.Map;
22+
import java.util.jar.Attributes;
23+
import org.codehaus.plexus.archiver.ArchiverException;
24+
import org.codehaus.plexus.util.IOUtil;
25+
26+
/**
27+
* Not part of any public API
28+
* @author Kristian Rosenvold
29+
*/
30+
class JdkManifestFactory
31+
{
32+
public static java.util.jar.Manifest getDefaultManifest()
33+
throws ArchiverException
34+
{
35+
try
36+
{
37+
String defManifest = "/org/codehaus/plexus/archiver/jar/defaultManifest.mf";
38+
InputStream in = JdkManifestFactory.class.getResourceAsStream( defManifest );
39+
if ( in == null )
40+
{
41+
throw new ArchiverException( "Could not find default manifest: " + defManifest );
42+
}
43+
try
44+
{
45+
java.util.jar.Manifest defaultManifest = new java.util.jar.Manifest( in );
46+
47+
defaultManifest.getMainAttributes().putValue( "Created-By",
48+
System.getProperty( "java.vm.version" ) + " ("
49+
+ System.getProperty( "java.vm.vendor" ) + ")" );
50+
return defaultManifest;
51+
}
52+
finally
53+
{
54+
IOUtil.close( in );
55+
}
56+
}
57+
catch ( IOException e )
58+
{
59+
throw new ArchiverException( "Unable to read default manifest", e );
60+
}
61+
}
62+
63+
public static void merge( java.util.jar.Manifest target, java.util.jar.Manifest other, boolean overwriteMain )
64+
{
65+
if ( other != null )
66+
{
67+
final Attributes mainAttributes = target.getMainAttributes();
68+
if ( overwriteMain )
69+
{
70+
mainAttributes.clear();
71+
mainAttributes.putAll( other.getMainAttributes() );
72+
}
73+
else
74+
{
75+
mergeAttributes( mainAttributes, other.getMainAttributes() );
76+
}
77+
78+
for ( Map.Entry<String, Attributes> o : other.getEntries().entrySet() )
79+
{
80+
Attributes ourSection = target.getAttributes( o.getKey() );
81+
Attributes otherSection = o.getValue();
82+
if ( ourSection == null )
83+
{
84+
if ( otherSection != null )
85+
{
86+
target.getEntries().put( o.getKey(), (Attributes) otherSection.clone() );
87+
}
88+
}
89+
else
90+
{
91+
mergeAttributes( ourSection, otherSection );
92+
}
93+
}
94+
}
95+
}
96+
97+
/**
98+
* Merge in another section
99+
*
100+
* @param target The target manifest of the merge
101+
* @param section the section to be merged with this one.
102+
*/
103+
public static void mergeAttributes( java.util.jar.Attributes target, java.util.jar.Attributes section )
104+
{
105+
for ( Object o : section.keySet() )
106+
{
107+
java.util.jar.Attributes.Name key = (Attributes.Name) o;
108+
final Object value = section.get( o );
109+
// the merge file always wins
110+
target.put( key, value );
111+
}
112+
}
113+
114+
115+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.codehaus.plexus.archiver.jar;
2+
3+
/**
4+
*
5+
* Copyright 2004 The Apache Software Foundation
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
/**
21+
* Manifest constants
22+
* Not part of any public API
23+
*/
24+
class ManifestConstants
25+
{
26+
/**
27+
* The standard manifest version header
28+
*/
29+
public static final String ATTRIBUTE_MANIFEST_VERSION = "Manifest-Version";
30+
31+
/**
32+
* The standard Signature Version header
33+
*/
34+
public static final String ATTRIBUTE_SIGNATURE_VERSION = "Signature-Version";
35+
36+
/**
37+
* The Name Attribute is the first in a named section
38+
*/
39+
public static final String ATTRIBUTE_NAME = "Name";
40+
41+
/**
42+
* The From Header is disallowed in a Manifest
43+
*/
44+
public static final String ATTRIBUTE_FROM = "From";
45+
46+
/**
47+
* The Class-Path Header is special - it can be duplicated
48+
*/
49+
public static final String ATTRIBUTE_CLASSPATH = "Class-Path";
50+
51+
/**
52+
* Default Manifest version if one is not specified
53+
*/
54+
public static final String DEFAULT_MANIFEST_VERSION = "1.0";
55+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package org.codehaus.plexus.archiver.jar;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
import java.util.jar.Attributes;
7+
import java.util.jar.Manifest;
8+
import org.codehaus.plexus.PlexusTestCase;
9+
10+
/**
11+
* @author Kristian Rosenvold
12+
*/
13+
public class JdkManifestFactoryTest
14+
extends PlexusTestCase
15+
{
16+
public void testGetDefaultManifest()
17+
throws Exception
18+
{
19+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
20+
Manifest manifest = JdkManifestFactory.getDefaultManifest();
21+
manifest.write( byteArrayOutputStream );
22+
System.out.println( new String( byteArrayOutputStream.toByteArray() ) );
23+
}
24+
25+
public void testGetDefaultManifestString()
26+
throws Exception
27+
{
28+
Manifest manifest = getManifest( "src/test/resources/manifests/manifestWithClassPath.mf" );
29+
Manifest manifestWithout = getManifest( "src/test/resources/manifests/manifest1.mf" );
30+
String value = manifest.getMainAttributes().getValue( ManifestConstants.ATTRIBUTE_CLASSPATH );
31+
System.out.println( "value = " + value );
32+
manifestWithout.getMainAttributes().putValue( ManifestConstants.ATTRIBUTE_CLASSPATH, value );
33+
String value2 = manifestWithout.getMainAttributes().getValue( ManifestConstants.ATTRIBUTE_CLASSPATH );
34+
35+
assertEquals( value, value2 );
36+
}
37+
38+
39+
public void testIllegals()
40+
throws ManifestException, IOException
41+
{
42+
Manifest manifest = getManifest( "src/test/resources/manifests/manifest6.mf" );
43+
assertNotNull( manifest );
44+
45+
try
46+
{
47+
getManifest( "src/test/resources/manifests/manifest5.mf" );
48+
fail( "We expect to fail" );
49+
}
50+
catch ( IOException ignore )
51+
{
52+
53+
}
54+
}
55+
56+
public void testMerge()
57+
throws ManifestException, IOException
58+
{
59+
Manifest manifest1 = getManifest( "src/test/resources/manifests/manifestMerge1.mf" );
60+
Manifest manifest2 = getManifest( "src/test/resources/manifests/manifestMerge2.mf" );
61+
62+
Manifest target = new Manifest();
63+
JdkManifestFactory.merge( target, manifest1, false );
64+
65+
assertEquals( "001", target.getMainAttributes().getValue( "Bar" ) );
66+
Attributes fudz = target.getAttributes( "Fudz" );
67+
assertNotNull( fudz );
68+
assertEquals( "002", fudz.getValue( "Bar" ) );
69+
Attributes redz = target.getAttributes( "Redz" );
70+
assertNotNull( redz );
71+
assertEquals( "002", redz.getValue( "Baz" ) );
72+
73+
JdkManifestFactory.merge( target, manifest2, false );
74+
75+
assertEquals( "001", target.getMainAttributes().getValue( "Bar" ) );
76+
fudz = target.getAttributes( "Fudz" );
77+
assertNotNull( fudz );
78+
assertEquals( "003", fudz.getValue( "Bar" ) );
79+
redz = target.getAttributes( "Redz" );
80+
assertNotNull( redz );
81+
assertEquals( "002", redz.getValue( "Baz" ) );
82+
}
83+
84+
85+
public void testDualClassPath()
86+
throws ManifestException, IOException
87+
{
88+
Manifest manifest = getManifest( "src/test/resources/manifests/manifestWithDualClassPath.mf" );
89+
final Attributes mainAttributes = manifest.getMainAttributes();
90+
final String attribute = mainAttributes.getValue( "Class-Path" );
91+
// assertEquals( "../config/ classes12.jar baz", attribute );
92+
}
93+
94+
95+
/**
96+
* Reads a Manifest file.
97+
*/
98+
private java.util.jar.Manifest getManifest( String filename )
99+
throws IOException, ManifestException
100+
{
101+
FileInputStream r = new FileInputStream( getTestFile( filename ) );
102+
103+
try
104+
{
105+
return new Manifest( r );
106+
}
107+
finally
108+
{
109+
r.close();
110+
}
111+
}
112+
113+
}

0 commit comments

Comments
 (0)