streamlet-examples/JarInfo.java
branchv_0
changeset 73 1a067a217454
parent 72 f7b9db6fc32b
child 74 a2aa84f310a5
--- a/streamlet-examples/JarInfo.java	Wed Jan 29 18:05:13 2020 +0100
+++ b/streamlet-examples/JarInfo.java	Wed Jan 29 18:41:42 2020 +0100
@@ -23,6 +23,12 @@
 
 public class JarInfo extends Streamlet {
 
+	public static final String ATTRIBUTE_COMMENT = "comment";
+	public static final String ATTRIBUTE_ENTRIES = "entries";
+	public static final String ATTRIBUTE_MAIN_CLASS = "main_class";
+
+	private List<String> jarAttributes = new LinkedList<>();
+
 	public static void main(String[] args) throws IOException {
 		JarInfo s = new JarInfo();
 		int status = s.run();
@@ -38,14 +44,63 @@
 	}
 
 	protected List<Streamlet.AttributeMetadata> getOutputAttributesMetadata() {
+
+		List<Option> attributeOptions = getOptions("attribute");
+		if (attributeOptions.isEmpty()) {
+			attributeOptions.add(new Option("attribute", ATTRIBUTE_MAIN_CLASS));
+			attributeOptions.add(new Option("attribute", ATTRIBUTE_ENTRIES));
+		}
+
 		List<Streamlet.AttributeMetadata> result = new LinkedList<>();
-		result.add(new Streamlet.AttributeMetadata("main_class", Streamlet.Type.STRING));
+
+		for (int i = 0; i < attributeOptions.size(); i++) {
+			Streamlet.Option o = attributeOptions.get(i);
+			Streamlet.Type type;
+			switch (o.value) {
+				case ATTRIBUTE_COMMENT:
+				case ATTRIBUTE_MAIN_CLASS:
+					type = Streamlet.Type.STRING;
+					break;
+				case ATTRIBUTE_ENTRIES:
+					type = Streamlet.Type.INTEGER;
+					break;
+				default:
+					throw new IllegalArgumentException("Unsupported attribute: " + o.value);
+			}
+			result.add(new Streamlet.AttributeMetadata(getAlias(i, o.value), type));
+			jarAttributes.add(o.value);
+		}
+
 		return result;
 	}
 
+	@Override
 	protected List<Object> getOutputAttributes() {
 		List<Object> result = new LinkedList<>();
-		result.add("TODO: main class");
+
+		try {
+			JarFile jar = new JarFile(new File(currentFile));
+
+			for (String attributeName : jarAttributes) {
+				switch (attributeName) {
+					case ATTRIBUTE_COMMENT:
+						result.add(jar.getComment());
+						break;
+					case ATTRIBUTE_MAIN_CLASS:
+						String mainClass = jar.getManifest() == null ? null : jar.getManifest().getMainAttributes().getValue("Main-Class");
+						result.add(mainClass);
+						break;
+					case ATTRIBUTE_ENTRIES:
+						result.add(jar.size());
+						break;
+				}
+			}
+
+		} catch (Exception e) {
+			for (String attributeName : jarAttributes) {
+				result.add(null);
+			}
+		}
 		return result;
 	}
 }