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.FilterInputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.util.jar.JarEntry;
26  import java.util.jar.JarOutputStream;
27  
28  import org.apache.commons.io.IOUtils;
29  import org.vafer.dependency.resources.ResourceHandler;
30  import org.vafer.dependency.resources.Version;
31  import org.vafer.dependency.utils.Jar;
32  
33  public final class LicenseHandler implements ResourceHandler
34  {
35  	private File licensesFile;
36  	private FileOutputStream licensesOutputStream;
37  	
38  	public void onStartProcessing( JarOutputStream pOutput )
39  		throws IOException
40  	{
41  	}
42  	
43  	public void onStartJar( Jar pJar, JarOutputStream pOutput )
44  		throws IOException
45  	{		
46  	}
47  	
48  	public InputStream onResource(Jar jar, String oldName, String newName, Version[] versions, InputStream inputStream )
49  		throws IOException
50  	{
51  		final String s = oldName.toLowerCase();
52  
53  		if ( "meta-inf/license.txt".equals(s) || "meta-inf/license".equals(s) || "meta-inf/notice.txt".equals(s) || "meta-inf/notice".equals(s))
54  		{
55  			System.out.println(this + " found resource " + oldName);
56  
57  			if (licensesFile == null)
58  			{
59  				licensesFile = File.createTempFile("minijar", "license");
60  				licensesFile.deleteOnExit();
61  			}
62  			
63  			if (licensesOutputStream == null)
64  			{
65  				licensesOutputStream = new FileOutputStream( licensesFile );				
66  			}
67  			
68  			return new FilterInputStream( inputStream ) {
69  
70  				public int read()
71  					throws IOException
72  				{
73  					int r = super.read();
74  					if ( r > 0 )
75  					{
76  						licensesOutputStream.write(r);
77  					}
78  					return r;
79  				}
80  
81  				public int read( byte[] b, int off, int len )
82  					throws IOException
83  				{
84  					int r = super.read(b, off, len);
85  					if ( r > 0 )
86  					{
87  						licensesOutputStream.write(b, off, r);						
88  					}
89  					return r;
90  				}
91  
92  				public int read( byte[] b ) throws IOException
93  				{
94  					int r = super.read(b);
95  					if ( r > 0 )
96  					{
97  						licensesOutputStream.write( b, 0, r );
98  					}
99  					return r;
100 				}				
101 			};
102 		}
103 
104 		return inputStream;
105 	}
106 
107 	public void onStopJar( Jar pJar, JarOutputStream pOutput )
108 		throws IOException
109 	{		
110 	}
111 
112 	public void onStopProcessing( JarOutputStream pOutput )
113 		throws IOException
114 	{
115 		if ( licensesOutputStream == null )
116 		{
117 			// no license information aggregated
118 			return;
119 		}
120 
121 		IOUtils.closeQuietly( licensesOutputStream );
122 		
123 		
124 		// insert aggregated license information into new jar
125 		
126 		final FileInputStream licensesInputStream = new FileInputStream( licensesFile );
127 		
128 		pOutput.putNextEntry( new JarEntry("LICENSE.txt") );
129 		
130 		IOUtils.copy( licensesInputStream, pOutput );
131 		
132 		IOUtils.closeQuietly( licensesInputStream );
133 		
134 	}
135 
136 }