FileDocCategorySizeDatePackage
AEJarBuilder.javaAPI DocAzureus 3.0.3.46169Wed Nov 30 22:28:32 GMT 2005org.gudy.azureus2.core3.util.jar

AEJarBuilder

public class AEJarBuilder extends Object
author
parg

Fields Summary
Constructors Summary
Methods Summary
public static longbuildFromPackages(java.io.OutputStream os, java.lang.ClassLoader class_loader, java.lang.String[] package_names, java.util.Map package_map, java.lang.String sign_alias)

		List	resource_names = new ArrayList();
		
		for (int i=0;i<package_names.length;i++){
			
			List	entries = (List)package_map.get(package_names[i]);
			
			if ( entries == null ){
				
				Debug.out( "package '" + package_names[i] + "' missing" );
				
			}else{
			
				for (int j=0;j<entries.size();j++){
				
					resource_names.add( package_names[i] + "/" + entries.get(j));
				}
			}
		}
		
		String[]	res = new String[resource_names.size()];
		
		resource_names.toArray( res );
		
		return( buildFromResources2( os, class_loader, null, res, sign_alias ));	
	
public static voidbuildFromResources(java.io.OutputStream os, java.lang.ClassLoader class_loader, java.lang.String resource_prefix, java.lang.String[] resource_names, java.lang.String sign_alias)

		buildFromResources2( os, class_loader, resource_prefix, resource_names, sign_alias );	
	
private static longbuildFromResources2(java.io.OutputStream os, java.lang.ClassLoader class_loader, java.lang.String resource_prefix, java.lang.String[] resource_names, java.lang.String sign_alias)

		if ( sign_alias != null ){
			
			ByteArrayOutputStream	baos = new ByteArrayOutputStream(65536);
			
			long tim = buildFromResourcesSupport( new JarOutputStream( baos ),class_loader,resource_prefix,resource_names );
						
			try{
					// leave this check in here as we might as well check for the alias
				
				SEKeyDetails	kd = SESecurityManager.getKeyDetails( sign_alias );
			
				if ( kd == null ){
					Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_ERROR,
							"Certificate alias '" + sign_alias
									+ "' not found, jar signing fails"));
					
					throw( new Exception( "Certificate alias '" + sign_alias + "' not found "));
				}
				
				// WUJarSigner signer = new WUJarSigner(sign_alias, (PrivateKey)kd.getKey(), kd.getCertificateChain());

				AEJarSigner2 signer = 
					new AEJarSigner2(
							sign_alias,
							SESecurityManager.getKeystoreName(),
							SESecurityManager.getKeystorePassword());
							
				signer.signJarStream( new ByteArrayInputStream(baos.toByteArray()), os );
			
				return( tim );
				
			}catch( Throwable e ){
				
				Debug.printStackTrace( e );
				
				throw( new IOException( e.getMessage()));
			}
			
		}else{
						
			JarOutputStream	jos;
			
			if ( os instanceof JarOutputStream ){
				
				jos	= (JarOutputStream)os;
				
			}else{
								
				jos = new JarOutputStream( os );
			}
			
			return( buildFromResourcesSupport( jos,class_loader,resource_prefix,resource_names ));
		}
	
public static longbuildFromResourcesSupport(java.util.jar.JarOutputStream jos, java.lang.ClassLoader class_loader, java.lang.String resource_prefix, java.lang.String[] resource_names)

		
		long	latest_time	= 0;
		long	now			= SystemTime.getCurrentTime();
		
		for (int i=0;i<resource_names.length;i++){
	
			String	resource_name = resource_names[i];
	
			if ( resource_prefix != null ){
				
				resource_name = resource_prefix + "/" + resource_name;
			}
			
			InputStream	is = null;
			
			try{
				is	= class_loader.getResourceAsStream(resource_name);
			
				if ( is == null ){
				
					Debug.out( "WUJarBuilder: failed to find resource '" + resource_name + "'");
	
				}else{
				
					URL	url = class_loader.getResource( resource_name );
					
					try{
						File	file = null;
						
						if ( url != null ){
							
							String	url_str = url.toString();
							
							if ( url_str.startsWith("jar:file:" )){
								
								file	= FileUtil.getJarFileFromURL( url_str );
								
							}else if ( url_str.startsWith( "file:")){
								
								file	= new File( URI.create( url_str ));
							}
						}
						
						if ( file == null ){
							
							latest_time	= now;
							
						}else{
						
							long	time = file.lastModified();
							
							if ( time > latest_time ){
								
								latest_time	= time;
							}
						}
					}catch( Throwable e ){
						
						Debug.printStackTrace( e );
					}
					
					JarEntry entry = new JarEntry(resource_name);
			
					writeEntry( jos, entry, is );
				}
			}finally{
				if ( is != null ){
					
					is.close();
				}
			}
		}
		
		JarEntry entry = new JarEntry("META-INF/MANIFEST.MF");
		
		ByteArrayInputStream bais = new ByteArrayInputStream("Manifest-Version: 1.0\r\n\r\n".getBytes());
		
		writeEntry( jos, entry, bais );
		
		jos.flush();
		
		jos.finish();
		
		return( latest_time );
	
private static voidwriteEntry(java.util.jar.JarOutputStream jos, java.util.jar.JarEntry entry, java.io.InputStream data)

		jos.putNextEntry(entry);

		byte[]	newBytes = new byte[4096];
		
		int size = data.read(newBytes);

		while (size != -1){
			
			jos.write(newBytes, 0, size);
			
			size = data.read(newBytes);
		}