|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package io.microsphere.lang; |
| 18 | + |
| 19 | +import io.microsphere.collection.CollectionUtils; |
| 20 | +import io.microsphere.util.ClassPathUtils; |
| 21 | + |
| 22 | +import javax.annotation.Nonnull; |
| 23 | +import javax.annotation.Nullable; |
| 24 | +import java.io.File; |
| 25 | +import java.net.MalformedURLException; |
| 26 | +import java.net.URL; |
| 27 | +import java.security.CodeSource; |
| 28 | +import java.security.ProtectionDomain; |
| 29 | +import java.util.LinkedHashMap; |
| 30 | +import java.util.LinkedHashSet; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.Set; |
| 33 | +import java.util.jar.JarFile; |
| 34 | + |
| 35 | +import static io.microsphere.util.ClassPathUtils.getBootstrapClassPaths; |
| 36 | +import static io.microsphere.util.ClassPathUtils.getClassPaths; |
| 37 | +import static io.microsphere.util.ClassUtils.findClassNamesInClassPath; |
| 38 | +import static io.microsphere.util.ClassUtils.resolvePackageName; |
| 39 | +import static io.microsphere.util.StringUtils.isNotBlank; |
| 40 | +import static java.util.Collections.emptySet; |
| 41 | +import static java.util.Collections.unmodifiableMap; |
| 42 | +import static java.util.Collections.unmodifiableSet; |
| 43 | + |
| 44 | +/** |
| 45 | + * The Class Data Repository |
| 46 | + * |
| 47 | + * @author <a href="mailto:mercyblitz@gmail.com">Mercy<a/> |
| 48 | + * @see ClassPathUtils |
| 49 | + * @since 1.0.0 |
| 50 | + */ |
| 51 | +public class ClassDataRepository { |
| 52 | + |
| 53 | + /** |
| 54 | + * Singleton instance of {@link ClassDataRepository} |
| 55 | + */ |
| 56 | + public static final ClassDataRepository INSTANCE = new ClassDataRepository(); |
| 57 | + |
| 58 | + private final Map<String, Set<String>> classPathToClassNamesMap = initClassPathToClassNamesMap(); |
| 59 | + |
| 60 | + private final Map<String, String> classNameToClassPathsMap = initClassNameToClassPathsMap(); |
| 61 | + |
| 62 | + private final Map<String, Set<String>> packageNameToClassNamesMap = initPackageNameToClassNamesMap(); |
| 63 | + |
| 64 | + private ClassDataRepository() { |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Get all package names in {@link ClassPathUtils#getClassPaths() class paths} |
| 69 | + * |
| 70 | + * @return all package names in class paths |
| 71 | + */ |
| 72 | + @Nonnull |
| 73 | + public Set<String> getAllPackageNamesInClassPaths() { |
| 74 | + return packageNameToClassNamesMap.keySet(); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Find class path under specified class name |
| 79 | + * |
| 80 | + * @param type class |
| 81 | + * @return class path |
| 82 | + */ |
| 83 | + @Nullable |
| 84 | + public String findClassPath(Class<?> type) { |
| 85 | + return findClassPath(type.getName()); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Find class path under specified class name |
| 90 | + * |
| 91 | + * @param className class name |
| 92 | + * @return class path |
| 93 | + */ |
| 94 | + @Nullable |
| 95 | + public String findClassPath(String className) { |
| 96 | + return classNameToClassPathsMap.get(className); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Gets class name {@link Set} under specified class path |
| 101 | + * |
| 102 | + * @param classPath class path |
| 103 | + * @param recursive is recursive on sub directories |
| 104 | + * @return non-null {@link Set} |
| 105 | + */ |
| 106 | + @Nonnull |
| 107 | + public Set<String> getClassNamesInClassPath(String classPath, boolean recursive) { |
| 108 | + Set<String> classNames = classPathToClassNamesMap.get(classPath); |
| 109 | + if (CollectionUtils.isEmpty(classNames)) { |
| 110 | + classNames = findClassNamesInClassPath(classPath, recursive); |
| 111 | + } |
| 112 | + return classNames; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Gets class name {@link Set} under specified package |
| 117 | + * |
| 118 | + * @param onePackage one package |
| 119 | + * @return non-null {@link Set} |
| 120 | + */ |
| 121 | + @Nonnull |
| 122 | + public Set<String> getClassNamesInPackage(Package onePackage) { |
| 123 | + return getClassNamesInPackage(onePackage.getName()); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Gets class name {@link Set} under specified package name |
| 128 | + * |
| 129 | + * @param packageName package name |
| 130 | + * @return non-null {@link Set} |
| 131 | + */ |
| 132 | + @Nonnull |
| 133 | + public Set<String> getClassNamesInPackage(String packageName) { |
| 134 | + Set<String> classNames = packageNameToClassNamesMap.get(packageName); |
| 135 | + return classNames == null ? emptySet() : classNames; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * The map of all class names in {@link ClassPathUtils#getClassPaths() class path} , the class path for one {@link |
| 140 | + * JarFile} or classes directory as key , the class names set as value |
| 141 | + * |
| 142 | + * @return Read-only |
| 143 | + */ |
| 144 | + @Nonnull |
| 145 | + public Map<String, Set<String>> getClassPathToClassNamesMap() { |
| 146 | + return classPathToClassNamesMap; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * The set of all class names in {@link ClassPathUtils#getClassPaths() class path} |
| 151 | + * |
| 152 | + * @return Read-only |
| 153 | + */ |
| 154 | + @Nonnull |
| 155 | + public Set<String> getAllClassNamesInClassPaths() { |
| 156 | + Set<String> allClassNames = new LinkedHashSet(); |
| 157 | + for (Set<String> classNames : classPathToClassNamesMap.values()) { |
| 158 | + allClassNames.addAll(classNames); |
| 159 | + } |
| 160 | + return unmodifiableSet(allClassNames); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Get {@link Class}'s code source location URL |
| 165 | + * |
| 166 | + * @param type |
| 167 | + * @return If , return <code>null</code>. |
| 168 | + * @throws NullPointerException If <code>type</code> is <code>null</code> , {@link NullPointerException} will be thrown. |
| 169 | + */ |
| 170 | + public URL getCodeSourceLocation(Class<?> type) throws NullPointerException { |
| 171 | + |
| 172 | + URL codeSourceLocation = null; |
| 173 | + ClassLoader classLoader = type.getClassLoader(); |
| 174 | + |
| 175 | + if (classLoader == null) { // Bootstrap ClassLoader or type is primitive or void |
| 176 | + String path = findClassPath(type); |
| 177 | + if (isNotBlank(path)) { |
| 178 | + try { |
| 179 | + codeSourceLocation = new File(path).toURI().toURL(); |
| 180 | + } catch (MalformedURLException ignored) { |
| 181 | + codeSourceLocation = null; |
| 182 | + } |
| 183 | + } |
| 184 | + } else { |
| 185 | + ProtectionDomain protectionDomain = type.getProtectionDomain(); |
| 186 | + CodeSource codeSource = protectionDomain == null ? null : protectionDomain.getCodeSource(); |
| 187 | + codeSourceLocation = codeSource == null ? null : codeSource.getLocation(); |
| 188 | + } |
| 189 | + return codeSourceLocation; |
| 190 | + } |
| 191 | + |
| 192 | + private Map<String, Set<String>> initClassPathToClassNamesMap() { |
| 193 | + Map<String, Set<String>> classPathToClassNamesMap = new LinkedHashMap<>(); |
| 194 | + Set<String> classPaths = new LinkedHashSet<>(); |
| 195 | + classPaths.addAll(getBootstrapClassPaths()); |
| 196 | + classPaths.addAll(getClassPaths()); |
| 197 | + for (String classPath : classPaths) { |
| 198 | + Set<String> classNames = findClassNamesInClassPath(classPath, true); |
| 199 | + classPathToClassNamesMap.put(classPath, classNames); |
| 200 | + } |
| 201 | + return unmodifiableMap(classPathToClassNamesMap); |
| 202 | + } |
| 203 | + |
| 204 | + private Map<String, String> initClassNameToClassPathsMap() { |
| 205 | + Map<String, String> classNameToClassPathsMap = new LinkedHashMap<>(); |
| 206 | + |
| 207 | + for (Map.Entry<String, Set<String>> entry : classPathToClassNamesMap.entrySet()) { |
| 208 | + String classPath = entry.getKey(); |
| 209 | + Set<String> classNames = entry.getValue(); |
| 210 | + for (String className : classNames) { |
| 211 | + classNameToClassPathsMap.put(className, classPath); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + return unmodifiableMap(classNameToClassPathsMap); |
| 216 | + } |
| 217 | + |
| 218 | + private Map<String, Set<String>> initPackageNameToClassNamesMap() { |
| 219 | + Map<String, Set<String>> packageNameToClassNamesMap = new LinkedHashMap(); |
| 220 | + for (Map.Entry<String, String> entry : classNameToClassPathsMap.entrySet()) { |
| 221 | + String className = entry.getKey(); |
| 222 | + String packageName = resolvePackageName(className); |
| 223 | + Set<String> classNamesInPackage = packageNameToClassNamesMap.get(packageName); |
| 224 | + if (classNamesInPackage == null) { |
| 225 | + classNamesInPackage = new LinkedHashSet(); |
| 226 | + packageNameToClassNamesMap.put(packageName, classNamesInPackage); |
| 227 | + } |
| 228 | + classNamesInPackage.add(className); |
| 229 | + } |
| 230 | + |
| 231 | + return unmodifiableMap(packageNameToClassNamesMap); |
| 232 | + } |
| 233 | +} |
0 commit comments