List of usage examples for org.springframework.context.annotation ClassPathScanningCandidateComponentProvider ClassPathScanningCandidateComponentProvider
public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters)
From source file:ubic.gemma.core.apps.GemmaCLI.java
public static void main(String[] args) { /*/*from ww w . j av a 2 s .c o m*/ * Build a map from command names to classes. */ Map<CommandGroup, Map<String, String>> commands = new HashMap<>(); Map<String, Class<? extends AbstractCLI>> commandClasses = new HashMap<>(); try { final ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider( false); provider.addIncludeFilter(new RegexPatternTypeFilter(Pattern.compile(".*"))); // searching entire hierarchy is 1) slow and 2) generates annoying logging from static initialization code. final Set<BeanDefinition> classes = provider.findCandidateComponents("ubic.gemma.core.apps"); classes.addAll(provider.findCandidateComponents("ubic.gemma.core.loader.association.phenotype")); for (BeanDefinition bean : classes) { try { @SuppressWarnings("unchecked") Class<? extends AbstractCLI> aClazz = (Class<? extends AbstractCLI>) Class .forName(bean.getBeanClassName()); Object cliInstance = aClazz.newInstance(); Method method = aClazz.getMethod("getCommandName"); String commandName = (String) method.invoke(cliInstance, new Object[] {}); if (commandName == null || StringUtils.isBlank(commandName)) { // keep null to avoid printing some commands... continue; } Method method2 = aClazz.getMethod("getShortDesc"); String desc = (String) method2.invoke(cliInstance, new Object[] {}); Method method3 = aClazz.getMethod("getCommandGroup"); CommandGroup g = (CommandGroup) method3.invoke(cliInstance, new Object[] {}); if (!commands.containsKey(g)) { commands.put(g, new TreeMap<String, String>()); } commands.get(g).put(commandName, desc + " (" + bean.getBeanClassName() + ")"); commandClasses.put(commandName, aClazz); } catch (Exception e) { // OK, this can happen if we hit a non useful class. } } } catch (Exception e1) { System.err.println("ERROR! Report to developers: " + e1.getMessage()); System.exit(1); } if (args.length == 0 || args[0].equalsIgnoreCase("--help") || args[0].equalsIgnoreCase("-help") || args[0].equalsIgnoreCase("help")) { GemmaCLI.printHelp(commands); } else { LinkedList<String> f = new LinkedList<>(Arrays.asList(args)); String commandRequested = f.remove(0); Object[] argsToPass = f.toArray(new String[] {}); if (!commandClasses.containsKey(commandRequested)) { System.err.println("Unrecognized command: " + commandRequested); GemmaCLI.printHelp(commands); System.err.println("Unrecognized command: " + commandRequested); System.exit(1); } else { try { Class<?> c = commandClasses.get(commandRequested); Method method = c.getMethod("main", String[].class); System.err.println("========= Gemma CLI invocation of " + commandRequested + " ============"); System.err.println("Options: " + GemmaCLI.getOptStringForLogging(argsToPass)); //noinspection JavaReflectionInvocation // It works method.invoke(null, (Object) argsToPass); } catch (Exception e) { System.err.println("Gemma CLI error: " + e.getClass().getName() + " - " + e.getMessage()); System.err.println(ExceptionUtils.getStackTrace(e)); throw new RuntimeException(e); } finally { System.err.println("========= Gemma CLI run of " + commandRequested + " complete ============"); System.exit(0); } } } }