streamlet-examples/JarInfo.java
branchv_0
changeset 73 1a067a217454
parent 72 f7b9db6fc32b
child 74 a2aa84f310a5
equal deleted inserted replaced
72:f7b9db6fc32b 73:1a067a217454
    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 public class JarInfo extends Streamlet {
    24 public class JarInfo extends Streamlet {
    25 
    25 
       
    26 	public static final String ATTRIBUTE_COMMENT = "comment";
       
    27 	public static final String ATTRIBUTE_ENTRIES = "entries";
       
    28 	public static final String ATTRIBUTE_MAIN_CLASS = "main_class";
       
    29 
       
    30 	private List<String> jarAttributes = new LinkedList<>();
       
    31 
    26 	public static void main(String[] args) throws IOException {
    32 	public static void main(String[] args) throws IOException {
    27 		JarInfo s = new JarInfo();
    33 		JarInfo s = new JarInfo();
    28 		int status = s.run();
    34 		int status = s.run();
    29 		System.exit(status);
    35 		System.exit(status);
    30 
    36 
    36 		System.out.println("Entries:    " + jar.stream().count());
    42 		System.out.println("Entries:    " + jar.stream().count());
    37 		System.out.println("Main class: " + mainClass);
    43 		System.out.println("Main class: " + mainClass);
    38 	}
    44 	}
    39 
    45 
    40 	protected List<Streamlet.AttributeMetadata> getOutputAttributesMetadata() {
    46 	protected List<Streamlet.AttributeMetadata> getOutputAttributesMetadata() {
       
    47 
       
    48 		List<Option> attributeOptions = getOptions("attribute");
       
    49 		if (attributeOptions.isEmpty()) {
       
    50 			attributeOptions.add(new Option("attribute", ATTRIBUTE_MAIN_CLASS));
       
    51 			attributeOptions.add(new Option("attribute", ATTRIBUTE_ENTRIES));
       
    52 		}
       
    53 
    41 		List<Streamlet.AttributeMetadata> result = new LinkedList<>();
    54 		List<Streamlet.AttributeMetadata> result = new LinkedList<>();
    42 		result.add(new Streamlet.AttributeMetadata("main_class", Streamlet.Type.STRING));
    55 
       
    56 		for (int i = 0; i < attributeOptions.size(); i++) {
       
    57 			Streamlet.Option o = attributeOptions.get(i);
       
    58 			Streamlet.Type type;
       
    59 			switch (o.value) {
       
    60 				case ATTRIBUTE_COMMENT:
       
    61 				case ATTRIBUTE_MAIN_CLASS:
       
    62 					type = Streamlet.Type.STRING;
       
    63 					break;
       
    64 				case ATTRIBUTE_ENTRIES:
       
    65 					type = Streamlet.Type.INTEGER;
       
    66 					break;
       
    67 				default:
       
    68 					throw new IllegalArgumentException("Unsupported attribute: " + o.value);
       
    69 			}
       
    70 			result.add(new Streamlet.AttributeMetadata(getAlias(i, o.value), type));
       
    71 			jarAttributes.add(o.value);
       
    72 		}
       
    73 
    43 		return result;
    74 		return result;
    44 	}
    75 	}
    45 
    76 
       
    77 	@Override
    46 	protected List<Object> getOutputAttributes() {
    78 	protected List<Object> getOutputAttributes() {
    47 		List<Object> result = new LinkedList<>();
    79 		List<Object> result = new LinkedList<>();
    48 		result.add("TODO: main class");
    80 
       
    81 		try {
       
    82 			JarFile jar = new JarFile(new File(currentFile));
       
    83 
       
    84 			for (String attributeName : jarAttributes) {
       
    85 				switch (attributeName) {
       
    86 					case ATTRIBUTE_COMMENT:
       
    87 						result.add(jar.getComment());
       
    88 						break;
       
    89 					case ATTRIBUTE_MAIN_CLASS:
       
    90 						String mainClass = jar.getManifest() == null ? null : jar.getManifest().getMainAttributes().getValue("Main-Class");
       
    91 						result.add(mainClass);
       
    92 						break;
       
    93 					case ATTRIBUTE_ENTRIES:
       
    94 						result.add(jar.size());
       
    95 						break;
       
    96 				}
       
    97 			}
       
    98 
       
    99 		} catch (Exception e) {
       
   100 			for (String attributeName : jarAttributes) {
       
   101 				result.add(null);
       
   102 			}
       
   103 		}
    49 		return result;
   104 		return result;
    50 	}
   105 	}
    51 }
   106 }