|
27 | 27 | import java.io.IOException;
|
28 | 28 | import java.lang.reflect.InvocationTargetException;
|
29 | 29 | import java.util.ArrayList;
|
| 30 | +import java.util.HashMap; |
30 | 31 | import java.util.List;
|
| 32 | +import java.util.Locale; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.Set; |
31 | 35 | import java.util.logging.Level;
|
32 | 36 | import java.util.logging.Logger;
|
33 | 37 |
|
| 38 | +import org.opengrok.indexer.configuration.Configuration; |
34 | 39 | import org.opengrok.indexer.configuration.RuntimeEnvironment;
|
| 40 | +import org.opengrok.indexer.index.IgnoredNames; |
35 | 41 | import org.opengrok.indexer.logger.LoggerFactory;
|
36 | 42 | import org.opengrok.indexer.util.ForbiddenSymlinkException;
|
37 | 43 |
|
@@ -62,20 +68,43 @@ public final class RepositoryFactory {
|
62 | 68 | new SSCMRepository()
|
63 | 69 | };
|
64 | 70 |
|
| 71 | + private static final Map<String, Class<? extends Repository>> byName = new HashMap<>(); |
| 72 | + |
| 73 | + static { |
| 74 | + final String REPOSITORY = "Repository"; |
| 75 | + for (Repository repository : repositories) { |
| 76 | + Class<? extends Repository> clazz = repository.getClass(); |
| 77 | + String repoName = clazz.getSimpleName(); |
| 78 | + byName.put(repoName, clazz); |
| 79 | + byName.put(repoName.toLowerCase(Locale.ROOT), clazz); |
| 80 | + if (repoName.endsWith(REPOSITORY)) { |
| 81 | + String shortName = repoName.substring(0, repoName.length() - REPOSITORY.length()); |
| 82 | + if (shortName.length() > 0) { |
| 83 | + byName.put(shortName, clazz); |
| 84 | + byName.put(shortName.toLowerCase(Locale.ROOT), clazz); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** Private to enforce static. */ |
65 | 91 | private RepositoryFactory() {
|
66 |
| - // Factory class, should not be constructed |
67 | 92 | }
|
68 | 93 |
|
69 | 94 | /**
|
70 | 95 | * Get a list of all available repository handlers.
|
71 | 96 | *
|
72 |
| - * @return a list which contains none-{@code null} values, only. |
| 97 | + * @return a list that contains non-{@code null} values only |
73 | 98 | */
|
74 | 99 | public static List<Class<? extends Repository>> getRepositoryClasses() {
|
75 |
| - ArrayList<Class<? extends Repository>> list |
76 |
| - = new ArrayList<>(repositories.length); |
| 100 | + RuntimeEnvironment env = RuntimeEnvironment.getInstance(); |
| 101 | + |
| 102 | + ArrayList<Class<? extends Repository>> list = new ArrayList<>(repositories.length); |
77 | 103 | for (int i = repositories.length - 1; i >= 0; i--) {
|
78 |
| - list.add(repositories[i].getClass()); |
| 104 | + Class<? extends Repository> clazz = repositories[i].getClass(); |
| 105 | + if (isEnabled(clazz, env)) { |
| 106 | + list.add(clazz); |
| 107 | + } |
79 | 108 | }
|
80 | 109 |
|
81 | 110 | return list;
|
@@ -128,15 +157,18 @@ public static Repository getRepository(File file, boolean interactive, boolean i
|
128 | 157 | String relFile = env.getPathRelativeToSourceRoot(file);
|
129 | 158 |
|
130 | 159 | Repository repo = null;
|
131 |
| - for (Repository rep : repositories) { |
132 |
| - if ((!isNested || rep.isNestable()) && rep.isRepositoryFor(file, interactive)) { |
133 |
| - repo = rep.getClass().getDeclaredConstructor().newInstance(); |
| 160 | + for (Repository referenceRepo : repositories) { |
| 161 | + Class<? extends Repository> clazz = referenceRepo.getClass(); |
| 162 | + |
| 163 | + if ((!isNested || referenceRepo.isNestable()) && isEnabled(clazz, env) && |
| 164 | + referenceRepo.isRepositoryFor(file, interactive)) { |
| 165 | + repo = clazz.getDeclaredConstructor().newInstance(); |
134 | 166 |
|
135 | 167 | if (env.isProjectsEnabled() && relFile.equals(File.separator)) {
|
136 | 168 | LOGGER.log(Level.WARNING, "{0} was detected as {1} repository however with directory " +
|
137 | 169 | "matching source root. This is invalid because projects are enabled. Ignoring this " +
|
138 | 170 | "repository.",
|
139 |
| - new Object[]{file, rep.getType()}); |
| 171 | + new Object[]{file, repo.getType()}); |
140 | 172 | return null;
|
141 | 173 | }
|
142 | 174 | repo.setDirectoryName(file);
|
@@ -220,22 +252,46 @@ public static Repository getRepository(RepositoryInfo info, boolean interactive)
|
220 | 252 | }
|
221 | 253 |
|
222 | 254 | /**
|
223 |
| - * Go through all repository types and add items to lists of ignored |
224 |
| - * files/directories. This way per-repository ignored entries are set |
225 |
| - * inside repository classes rather than globally in IgnoredFiles/Dirs. |
226 |
| - * Should be called after {@code setConfiguration()}. |
227 |
| - * |
228 |
| - * @param env runtime environment |
| 255 | + * Go through all supported repository types, and add their ignored items to |
| 256 | + * the environment's lists of ignored files/directories -- but skip any |
| 257 | + * repository types which are named in |
| 258 | + * {@link RuntimeEnvironment#getDisabledRepositories()} ()}. This way |
| 259 | + * per-repository ignored entries are set inside repository classes rather |
| 260 | + * than globally in IgnoredFiles/Dirs. |
| 261 | + * <p> |
| 262 | + * (Should be called after |
| 263 | + * {@link RuntimeEnvironment#setConfiguration(Configuration)}.) |
229 | 264 | */
|
230 | 265 | public static void initializeIgnoredNames(RuntimeEnvironment env) {
|
| 266 | + IgnoredNames ignoredNames = env.getIgnoredNames(); |
231 | 267 | for (Repository repo : repositories) {
|
232 |
| - for (String file : repo.getIgnoredFiles()) { |
233 |
| - env.getIgnoredNames().add("f:" + file); |
| 268 | + if (isEnabled(repo.getClass(), env)) { |
| 269 | + for (String file : repo.getIgnoredFiles()) { |
| 270 | + ignoredNames.add("f:" + file); |
| 271 | + } |
| 272 | + for (String dir : repo.getIgnoredDirs()) { |
| 273 | + ignoredNames.add("d:" + dir); |
| 274 | + } |
234 | 275 | }
|
| 276 | + } |
| 277 | + } |
235 | 278 |
|
236 |
| - for (String dir : repo.getIgnoredDirs()) { |
237 |
| - env.getIgnoredNames().add("d:" + dir); |
238 |
| - } |
| 279 | + /** |
| 280 | + * Tries to match a supported repositories by name or nickname -- e.g. |
| 281 | + * {@code "CVSRepository"} or {@code "CVS"} or {@code "cvs"}. |
| 282 | + * @return a defined, class simple name (e.g. {@code "CVSRepository"} when |
| 283 | + * {@code "cvs"} is passed); or {@code null} if no match found |
| 284 | + */ |
| 285 | + public static String matchRepositoryByName(String name) { |
| 286 | + Class<? extends Repository> clazz = byName.get(name); |
| 287 | + if (clazz != null) { |
| 288 | + return clazz.getSimpleName(); |
239 | 289 | }
|
| 290 | + return null; |
| 291 | + } |
| 292 | + |
| 293 | + private static boolean isEnabled(Class<? extends Repository> clazz, RuntimeEnvironment env) { |
| 294 | + Set<String> disabledRepos = env.getDisabledRepositories(); |
| 295 | + return disabledRepos == null || !disabledRepos.contains(clazz.getSimpleName()); |
240 | 296 | }
|
241 | 297 | }
|
0 commit comments