1
20 package org.crosswire.common.util;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.zip.ZipEntry;
25 import java.util.zip.ZipFile;
26
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30
36 public final class ClassUtil {
37
40 private ClassUtil() {
41 }
42
43
52 public static Class<?> forName(String className) throws ClassNotFoundException {
53 return Thread.currentThread().getContextClassLoader().loadClass(className);
54 }
55
56
65 public static String findClasspathEntry(String className, String classPath) {
66 String full = null;
67
68 String[] paths = StringUtil.split(classPath, File.pathSeparator);
69 for (int i = 0; i < paths.length; i++) {
70 if (paths[i].endsWith(EXTENSION_ZIP) || paths[i].endsWith(EXTENSION_JAR)) {
72 ZipFile zip = null;
73 try {
74 String fileName = className.replace(',', '/') + EXTENSION_CLASS;
75 zip = new ZipFile(paths[i]);
76 ZipEntry entry = zip.getEntry(fileName);
77
78 if (entry != null && !entry.isDirectory()) {
79 if (full != null && !full.equals(fileName)) {
80 LOGGER.warn("Warning duplicate {} found: {} and {}", className, full, paths[i]);
81 } else {
82 full = paths[i];
83 }
84 }
85 } catch (IOException ex) {
86 LOGGER.warn("Missing zip file for {} and {}", className, paths[i]);
88 } finally {
89 if (null != zip) {
90 try {
91 zip.close();
92 } catch (IOException ex) {
93 LOGGER.error("close", ex);
94 }
95 }
96 }
97 } else {
98 StringBuilder path = new StringBuilder(256);
99
100 String extra = className.replace('.', File.separatorChar);
102
103 path.append(paths[i]);
104 if (paths[i].charAt(paths[i].length() - 1) != File.separatorChar) {
105 path.append(File.separatorChar);
106 }
107
108 path.append(extra);
109 path.append(EXTENSION_CLASS);
110 String fileName = path.toString();
111
112 if (new File(fileName).isFile()) {
113 if (full != null && !full.equals(fileName)) {
114 LOGGER.warn("Warning duplicate {} found: {} and {}", className, full, paths[i]);
115 } else {
116 full = paths[i];
117 }
118 }
119 }
120 }
121
122 return full;
123 }
124
125
133 public static String findClasspathEntry(String className) {
134 String classpath = System.getProperty("java.class.path", "");
135 return findClasspathEntry(className, classpath);
136 }
137
138
148 public static String getShortClassName(Object object, String valueIfNull) {
149 if (object == null) {
150 return valueIfNull;
151 }
152 return getShortClassName(object.getClass().getName());
153 }
154
155
165 public static String getShortClassName(Class<?> cls) {
166 if (cls == null) {
167 throw new IllegalArgumentException("The class must not be null");
168 }
169 return getShortClassName(cls.getName());
170 }
171
172
186 public static String getShortClassName(String className) {
187 if (className == null || className.length() == 0) {
188 throw new IllegalArgumentException("The class name must not be empty");
189 }
190 char[] chars = className.toCharArray();
191 int lastDot = 0;
192 for (int i = 0; i < chars.length; i++) {
193 if (chars[i] == PACKAGE_SEPARATOR_CHAR) {
194 lastDot = i + 1;
195 } else if (chars[i] == INNER_CLASS_SEPARATOR_CHAR) {
196 chars[i] = PACKAGE_SEPARATOR_CHAR;
197 }
198 }
199 return new String(chars, lastDot, chars.length - lastDot);
200 }
201
202
205 private static final char PACKAGE_SEPARATOR_CHAR = '.';
206
207
210 private static final char INNER_CLASS_SEPARATOR_CHAR = '$';
211
212 private static final String EXTENSION_CLASS = ".class";
213 private static final String EXTENSION_JAR = ".jar";
214 private static final String EXTENSION_ZIP = ".zip";
215
216
219 private static final Logger LOGGER = LoggerFactory.getLogger(ClassUtil.class);
220 }
221