streamlet-examples/JarInfo.java
branchv_0
changeset 74 a2aa84f310a5
parent 73 1a067a217454
child 75 ecbf6504915c
equal deleted inserted replaced
73:1a067a217454 74:a2aa84f310a5
    19 import java.io.IOException;
    19 import java.io.IOException;
    20 import java.util.LinkedList;
    20 import java.util.LinkedList;
    21 import java.util.List;
    21 import java.util.List;
    22 import java.util.jar.JarFile;
    22 import java.util.jar.JarFile;
    23 
    23 
       
    24 /**
       
    25  * <p>
       
    26  * This streamlet provides metadata from JAR (or ZIP) files.</p>
       
    27  *
       
    28  * <p>
       
    29  * With no options it returns the main class (if any) and number of entries (files and directories) in the archive.</p>
       
    30  *
       
    31  * <p>
       
    32  * Specific attributes can be selected using options – e.g. --option 'attribute' '…' Supported attributes are:</p>
       
    33  *
       
    34  * <ul>
       
    35  * <li>comment</li>
       
    36  * <li>entries</li>
       
    37  * <li>main_class</li>
       
    38  * </ul>
       
    39  */
    24 public class JarInfo extends Streamlet {
    40 public class JarInfo extends Streamlet {
    25 
    41 
       
    42 	// TODO: total size 
       
    43 	// TODO: OSGi metadata etc.
       
    44 	// TODO: more OOP, move to separate repository, proper Maven project, clean-up, stabilize API
    26 	public static final String ATTRIBUTE_COMMENT = "comment";
    45 	public static final String ATTRIBUTE_COMMENT = "comment";
    27 	public static final String ATTRIBUTE_ENTRIES = "entries";
    46 	public static final String ATTRIBUTE_ENTRIES = "entries";
    28 	public static final String ATTRIBUTE_MAIN_CLASS = "main_class";
    47 	public static final String ATTRIBUTE_MAIN_CLASS = "main_class";
    29 
    48 
    30 	private List<String> jarAttributes = new LinkedList<>();
    49 	private List<String> jarAttributes = new LinkedList<>();
    31 
    50 
    32 	public static void main(String[] args) throws IOException {
    51 	public static void main(String[] args) throws IOException {
    33 		JarInfo s = new JarInfo();
    52 		JarInfo s = new JarInfo();
    34 		int status = s.run();
    53 		int status = s.run();
    35 		System.exit(status);
    54 		System.exit(status);
    36 
       
    37 		// TODO: return real values:
       
    38 		JarFile jar = new JarFile(new File(args[0]));
       
    39 		String mainClass = jar.getManifest() == null ? null : jar.getManifest().getMainAttributes().getValue("Main-Class");
       
    40 		System.out.println("Name:       " + jar.getName());
       
    41 		System.out.println("Comment:    " + jar.getComment());
       
    42 		System.out.println("Entries:    " + jar.stream().count());
       
    43 		System.out.println("Main class: " + mainClass);
       
    44 	}
    55 	}
    45 
    56 
    46 	protected List<Streamlet.AttributeMetadata> getOutputAttributesMetadata() {
    57 	protected List<Streamlet.AttributeMetadata> getOutputAttributesMetadata() {
    47 
    58 
    48 		List<Option> attributeOptions = getOptions("attribute");
    59 		List<Option> attributeOptions = getOptions("attribute");