My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.abdennebi.version;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

public class VersionUtil14 {

public static void main(String[] args) {

System.out.println(readVersion());
}

public static String readVersion() {

URL pathToManifest = null;

try {
pathToManifest = new URL(getPathToManifest());

InputStream inputStream = pathToManifest.openStream();

Manifest manifest = new Manifest(inputStream);

String version = manifest.getMainAttributes().getValue(Attributes.Name.IMPLEMENTATION_VERSION);

inputStream.close();

return version;

} catch (MalformedURLException e) {
throw new UnreadableVersionException(e);
} catch (IOException e) {
throw new UnreadableVersionException(e);
}
}

private static String getPathToManifest(){

// 1 - Lire le nom de la classe
String classSimpleName = getSimpleName(VersionUtil14.class);
// classSimpleName = VersionUtil.class

// 2 - Récupérer le chemin physique de la classe
String pathToClass = VersionUtil14.class.getResource(classSimpleName).toString();

// pathToClass = file:/C:/workspace/VersionUtil/bin/com/abdennebi/version/VersionUtil.class
// pathToClass = jar:file:/C:/version.jar!/com/abdennebi/version/VersionUtil.class

// 3 - Récupérer le chemin de la classe à partir de la racine du classpath
String classFullName = VersionUtil14.class.getName().replace('.', '/') + ".class";
// classFullName = com/abdennebi/version/VersionUtil.class

// 4 - Récupérer le chemin complet vers MANIFEST.MF
String pathToManifest = pathToClass.substring( 0, pathToClass.length() - (classFullName.length())) + "META-INF/MANIFEST.MF";
// pathToManifest = file:/C:/workspace/VersionUtil/bin/META-INF/MANIFEST.MF
// pathToManifest = jar:file:/C:/version.jar!/META-INF/MANIFEST.MF

return pathToManifest;
}

public static class UnreadableVersionException extends RuntimeException {

public UnreadableVersionException(Throwable cause) {
super(cause);
}

private static final long serialVersionUID = 1L;
}

private static String getSimpleName(Class c){
String className = c.getName();
String simpleName = c.getName();
if (c.getPackage() != null) {
simpleName = className.substring(c.getPackage().getName().length() + 1);
}

return simpleName + ".class";
}
}

Change log

r16 by abdennebi on Mar 6, 2009   Diff
Partager projet "VersionUtil14" dans
"https://abdennebi.googlecode.com/svn"
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 2512 bytes, 83 lines

File properties

svn:mime-type
text/plain
Powered by Google Project Hosting