1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package inc.che.common.jar;
20
21
22 import inc.che.common.resource.ResourceManager;
23 import inc.che.common.resource.StringResources;
24 import inc.che.common.string.StringUtil;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.lang.reflect.Field;
29 import java.util.ArrayList;
30 import java.util.Enumeration;
31 import java.util.Iterator;
32 import java.util.Map;
33 import java.util.StringTokenizer;
34 import java.util.jar.Attributes;
35 import java.util.jar.JarEntry;
36 import java.util.jar.JarFile;
37 import java.util.jar.Manifest;
38
39 import org.apache.log4j.Logger;
40
41 /***
42 * <b>Utilitymethoden für jar-dateien</b>
43 * @version $Id: JarUtil.java,v 1.1 2005/03/06 12:56:59 stevemcmee Exp $
44 * @author <address> Steve McMee <stevemcmee@users.sourceforge.net></address>
45 */
46
47 public final class JarUtil {
48
49 /*** CVS ID of this file */
50 public static final String CVS_ID = "$Id:";
51
52 /***
53 * logger instance for this class
54 */
55
56 private static Logger log = Logger.getLogger(JarUtil.class);
57 /***
58 * The ResourceManager
59 */
60 private static ResourceManager resourceManager =
61 ResourceManager.getResourceManager(StringResources.TEXT_RESOURCES);
62
63 /***
64 * forbidden constructor
65 */
66 private JarUtil() {
67 }
68 /***
69 * gets all attributes in a Manifest
70 * @param manifest the manifestfile
71 * @return String containing all attributes and associating value in the manifest
72 */
73 public static String getAllManifestAttributes(Manifest manifest) {
74 if (log.isDebugEnabled()) {
75 log.debug("getAllManifestAttributes(" + manifest + ")");
76 }
77 Attributes manifestAttributes = manifest.getMainAttributes();
78 if (manifestAttributes != null) {
79 StringBuffer buffer = new StringBuffer();
80 String key;
81 for (Iterator it = manifestAttributes.entrySet().iterator();
82 it.hasNext();
83 ) {
84 Map.Entry entry = (Map.Entry) it.next();
85 key = entry.getKey().toString();
86 buffer
87 .append(key)
88 .append(":")
89 .append(StringUtil.createBlankString(20 - key.length()))
90 .append(entry.getValue())
91 .append("\n");
92 }
93
94 return buffer.toString();
95 } else {
96 return null;
97 }
98 }
99
100 /***
101 * gets CVSIds for all java classes in a jar file
102 * therefore a class must contain the member:
103 * <code>
104 * public static final String CVS_ID="...";
105 * </code>
106 * @param jarFile the jarFile
107 * @return String containing all classnames with their associating CVSIds
108 */
109
110 public static String getAllClassCvsId(JarFile jarFile) {
111 if (log.isDebugEnabled()) {
112 log.debug("getAllClassCvsId(" + jarFile.getName() + ")");
113 }
114 StringBuffer erg = new StringBuffer();
115
116 Enumeration enum = jarFile.entries();
117 while (enum.hasMoreElements()) {
118 String entryName = ((JarEntry) enum.nextElement()).getName();
119
120 if (entryName.endsWith(".class") && entryName.indexOf("$") == -1) {
121 String cvsInfo;
122 entryName = entryName.substring(0, entryName.lastIndexOf("."));
123 entryName = StringUtil.replace(entryName, "/", ".");
124
125 try {
126 Class aClass = Class.forName(entryName);
127 try {
128 Field cvsInfoField = aClass.getField("CVS_ID");
129 cvsInfo = (String) cvsInfoField.get(null);
130 } catch (NoSuchFieldException ex) {
131 log.info(
132 resourceManager.getText(
133 "no_cvs_id",
134 new String[] {String.valueOf(aClass)}));
135 cvsInfo = "unknown";
136 }
137 String className = aClass.getName();
138 if (cvsInfo.equals("unknown")) {
139 erg
140 .append(className)
141 .append(":")
142 .append(
143 StringUtil.createBlankString(
144 60 - className.length()))
145 .append(cvsInfo)
146 .append("\n");
147 } else {
148 erg
149 .append(className)
150 .append(":")
151 .append(
152 StringUtil.createBlankString(
153 60 - className.length()))
154 .append(cvsInfo)
155 .append("\n");
156 }
157
158 } catch (Throwable ex) {
159 erg
160 .append(entryName)
161 .append(":")
162 .append(
163 StringUtil.createBlankString(
164 60 - entryName.length()))
165 .append(ex.getClass())
166 .append(" ")
167 .append(ex.getMessage())
168 .append("\n");
169 }
170 }
171
172 }
173
174 return erg.toString();
175 }
176 /***
177 * gets all JarFiles in the classpath
178 * @return Array containing all JarFiles in the classpath
179 */
180 public static JarFile[] getAllJarFiles() throws IOException {
181 if (log.isDebugEnabled()) {
182 log.debug("getAllJarFiles()");
183 }
184 ArrayList list = new ArrayList();
185 System.out.println(System.getProperty("java.class.path"));
186 StringTokenizer tokenizer =
187 new StringTokenizer(
188 System.getProperty("java.class.path"),
189 System.getProperty("path.separator"));
190 while (tokenizer.hasMoreElements()) {
191 String token = tokenizer.nextToken();
192 if (token.endsWith(".jar")) {
193 if (log.isDebugEnabled()) {
194 log.debug("add jarfile: " + token);
195 }
196 if (new File(token).exists()) {
197 list.add(new JarFile(token));
198 }
199 }
200 }
201
202 return (JarFile[]) list.toArray(new JarFile[] {
203 });
204 }
205 }