View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.tools;
14  
15  import java.io.File;
16  import java.io.FileFilter;
17  import java.io.FileInputStream;
18  import java.io.FileOutputStream;
19  import java.io.IOException;
20  import java.util.jar.JarEntry;
21  import java.util.jar.JarOutputStream;
22  import java.util.jar.Manifest;
23  
24  import org.apache.log4j.Logger;
25  
26  public class JarPackager
27  {
28  	public static int BUFFER_SIZE = 10240;
29  	static Logger log = Logger.getLogger( JarPackager.class );
30  
31  	public static void copyFileToDir( File fromFile, File toDir )
32  	{
33  		File toFile = new File( toDir, fromFile.getName() );
34  		try
35  		{
36  			copyFile( fromFile, toFile );
37  		}
38  		catch( IOException e )
39  		{
40  			log.error( e.getMessage(), e );
41  		}
42  
43  	}
44  
45  	public static void copyAllFromTo( File fromDir, File toDir, FileFilter filter )
46  	{
47  		if( fromDir.isDirectory() & toDir.isDirectory() )
48  		{
49  			log.info( "Coping files from " + fromDir.getAbsolutePath() + " to " + toDir.getAbsolutePath() );
50  			File[] fromFiles = filter == null ? fromDir.listFiles() : fromDir.listFiles( filter );
51  			for( File file : fromFiles )
52  			{
53  				File toFile = new File( toDir, file.getName() );
54  				try
55  				{
56  					copyFile( file, toFile );
57  				}
58  				catch( IOException e )
59  				{
60  					log.error( e.getMessage(), e );
61  				}
62  			}
63  		}
64  		else
65  		{
66  			log.error( fromDir.getAbsolutePath() + " or " + toDir.getAbsolutePath() + " is not directory!" );
67  		}
68  	}
69  
70  	private static void copyFile( File fromFile, File toFile ) throws IOException
71  	{
72  		FileInputStream from = null;
73  		FileOutputStream to = null;
74  		try
75  		{
76  			from = new FileInputStream( fromFile );
77  			to = new FileOutputStream( toFile );
78  			byte[] buffer = new byte[4096];
79  			int bytesRead;
80  
81  			while( ( bytesRead = from.read( buffer ) ) != -1 )
82  				to.write( buffer, 0, bytesRead ); // write
83  		}
84  		catch( Exception e )
85  		{
86  			log.error( e );
87  		}
88  		finally
89  		{
90  			if( from != null )
91  				try
92  				{
93  					from.close();
94  				}
95  				catch( IOException e )
96  				{
97  					throw e;
98  				}
99  			if( to != null )
100 				try
101 				{
102 					to.close();
103 				}
104 				catch( IOException e )
105 				{
106 					throw e;
107 				}
108 		}
109 
110 	}
111 
112 	public static void createJarArchive( File archiveFile, File root, File... tobeJared )
113 	{
114 		try
115 		{
116 			byte buffer[] = new byte[BUFFER_SIZE];
117 			// Open archive file
118 			FileOutputStream stream = new FileOutputStream( archiveFile );
119 			JarOutputStream out = new JarOutputStream( stream, new Manifest() );
120 
121 			for( int i = 0; i < tobeJared.length; i++ )
122 			{
123 				if( tobeJared[i] == null || !tobeJared[i].exists() )
124 					continue; // Just in case...
125 
126 				// Add archive entry
127 				String jarName = tobeJared[i].isDirectory() ? tobeJared[i].getAbsolutePath() + "/" : tobeJared[i]
128 						.getAbsolutePath();
129 				jarName = jarName.replace( root.getAbsolutePath(), "" ).substring( 1 );
130 				JarEntry jarAdd = new JarEntry( jarName );
131 				log.info( "Adding " + jarName );
132 				jarAdd.setTime( tobeJared[i].lastModified() );
133 				out.putNextEntry( jarAdd );
134 
135 				if( jarAdd.isDirectory() )
136 					continue;
137 
138 				// Write file to archive
139 				FileInputStream in = new FileInputStream( tobeJared[i] );
140 				while( true )
141 				{
142 					int nRead = in.read( buffer, 0, buffer.length );
143 					if( nRead <= 0 )
144 						break;
145 					out.write( buffer, 0, nRead );
146 				}
147 				in.close();
148 			}
149 
150 			out.close();
151 			stream.close();
152 			log.info( "Adding completed OK" );
153 		}
154 		catch( Exception ex )
155 		{
156 			log.error( ex.getMessage(), ex );
157 		}
158 	}
159 
160 }