Version 1.7.4
[gae.git] / java / src / main / com / google / appengine / tools / util / ApiVersionFinder.java
blob3491af67f3a7fc361e9c6645b46a6c204999a8ba
1 // Copyright 2009 Google Inc. All Rights Reserved.
3 package com.google.appengine.tools.util;
5 import java.io.File;
6 import java.io.IOException;
7 import java.util.jar.JarFile;
8 import java.util.jar.Manifest;
9 import java.util.jar.Attributes;
11 /**
12 * {@code ApiVersionFinder} extracts the {@code Specification-Version}
13 * from the Jar manifest of the specified jar file.
16 public class ApiVersionFinder {
17 private static final String DESIRED_VENDOR_ID = "com.google";
18 private static final String API_PACKAGE_NAME = "com/google/appengine/api/";
20 public static void main(String[] args) throws IOException {
21 ApiVersionFinder finder = new ApiVersionFinder();
22 String apiVersion = finder.findApiVersion(args[0]);
23 if (apiVersion == null) {
24 System.exit(1);
27 System.out.println(apiVersion);
28 System.exit(0);
31 public String findApiVersion(String fileName) throws IOException {
32 return findApiVersion(new File(fileName));
35 public String findApiVersion(File inputJar) throws IOException {
36 JarFile jarFile = new JarFile(inputJar);
37 try {
38 Manifest manifest = jarFile.getManifest();
40 if (manifest != null) {
41 Attributes attr = manifest.getAttributes(API_PACKAGE_NAME);
42 if (attr != null) {
43 String vendorId = attr.getValue(Attributes.Name.IMPLEMENTATION_VENDOR_ID);
44 if (!DESIRED_VENDOR_ID.equals(vendorId)) {
45 return null;
48 return attr.getValue(Attributes.Name.SPECIFICATION_VERSION);
51 return null;
52 } finally {
53 try {
54 jarFile.close();
55 } catch (IOException ex) {