View Javadoc

1   package org.codehaus.mojo.minijar.resource;
2   
3   /*
4    * Copyright 2005 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.File;
20  import java.io.FileInputStream;
21  import java.io.FileOutputStream;
22  import java.io.FileReader;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.OutputStream;
26  import java.io.OutputStreamWriter;
27  import java.util.Iterator;
28  import java.util.LinkedHashMap;
29  import java.util.Map;
30  import java.util.jar.JarEntry;
31  import java.util.jar.JarOutputStream;
32  
33  import org.apache.commons.io.IOUtils;
34  import org.codehaus.plexus.util.xml.Xpp3Dom;
35  import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
36  import org.codehaus.plexus.util.xml.Xpp3DomWriter;
37  import org.vafer.dependency.resources.ResourceHandler;
38  import org.vafer.dependency.resources.Version;
39  import org.vafer.dependency.utils.Jar;
40  
41  public final class ComponentsXmlHandler implements ResourceHandler
42  {
43  	public static final String COMPONENTS_XML_PATH = "META-INF/plexus/components.xml";
44  	
45  	private Map components;
46  
47  	public void onStartProcessing( JarOutputStream pOutput )
48  		throws IOException
49  	{
50  		components = new LinkedHashMap();
51  	}
52  	
53  	public void onStartJar( Jar pJar, JarOutputStream pOutput )
54  		throws IOException
55  	{		
56  	}
57  	
58  	public InputStream onResource( Jar pJar, String oldName, String newName, Version[] versions, InputStream inputStream )
59  		throws IOException
60  	{
61  		if ( COMPONENTS_XML_PATH.equals( oldName ) )
62  		{
63  			// needs to be aggregated
64  			
65  	        final File file = File.createTempFile( "minijar", "tmp" );
66  	        file.deleteOnExit();
67  	        
68  	        final OutputStream os = new FileOutputStream( file );
69  	        IOUtils.copy( inputStream, os );
70  	        os.close();
71  			
72  	        final Xpp3Dom dom;
73  	        
74  	        try
75  	        {
76  	            dom = Xpp3DomBuilder.build( new FileReader( file ) );
77  	        }
78  	        catch ( Exception e )
79  	        {
80  	            throw new IOException( "Error parsing components.xml in " + pJar + " at " + oldName );
81  	        }
82  
83  	        final Xpp3Dom[] children = dom.getChild( "components" ).getChildren( "component" );
84  	        
85  	        for ( int i = 0; i < children.length; i++ )
86  	        {
87  	            final Xpp3Dom component = children[i];
88  
89  	            final String role = component.getChild( "role" ).getValue();
90  
91  	            final Xpp3Dom child = component.getChild( "role-hint" );
92  
93  	            final String roleHint = child != null ? child.getValue() : "";
94  
95  	            components.put( role + roleHint, component );
96  	        }	        
97  
98  	        return new FileInputStream(file);	        
99  		}
100 
101 		return inputStream;
102 	}
103 
104 	public void onStopJar( Jar pJar, JarOutputStream pOutput )
105 		throws IOException
106 	{		
107 	}
108 
109 	public void onStopProcessing( JarOutputStream pOutput )
110 		throws IOException
111 	{
112 		if ( components.size() == 0 )
113 		{
114 			// no components information available
115 			return;
116 		}
117 
118         final Xpp3Dom dom = new Xpp3Dom( "component-set" );
119         final Xpp3Dom componentDom = new Xpp3Dom( "components" );
120 
121         dom.addChild( componentDom );
122 
123         for ( Iterator it = components.values().iterator(); it.hasNext(); )
124         {
125             final Xpp3Dom component = (Xpp3Dom) it.next();
126             componentDom.addChild( component );
127         }
128 
129         
130 		// insert aggregated license information into new jar
131 		        
132 		pOutput.putNextEntry( new JarEntry( COMPONENTS_XML_PATH ) );
133 
134         Xpp3DomWriter.write( new OutputStreamWriter( pOutput ), dom );
135 	}
136 
137 }