private void discoverCommands()

in runner/src/main/java/org/jclouds/cli/runner/Main.java [332:377]


    private void discoverCommands(CommandProcessorImpl commandProcessor, ClassLoader cl) throws IOException, ClassNotFoundException {
        Enumeration<URL> urls = cl.getResources(getDiscoveryResource());
        while (urls.hasMoreElements()) {
            URL url = urls.nextElement();
            BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
            try {
                while (true) {
                    String line = reader.readLine();
                    if (line == null) {
                        break;
                    }
                    line = line.trim();
                    if (line.isEmpty() || line.charAt(0) == '#') {
                        continue;
                    }
                    final Class<Action> actionClass = (Class<Action>) cl.loadClass(line);
                    Command cmd = actionClass.getAnnotation(Command.class);
                    Function function = new AbstractCommand() {
                        @Override
                        public Action createNewAction() {
                            try {
                               Action action = ((Class< ? extends Action>) actionClass).newInstance();
                               if (action instanceof ComputeCommandBase) {
                                   ShellTableFactory shellTableFactory = ((ComputeCommandBase) action).getShellTableFactory();
                                   if (shellTableFactory instanceof BasicShellTableFactory) {
                                       BasicShellTableFactory factory = (BasicShellTableFactory) shellTableFactory;
                                       if (factory.getScriptEngineManager() == null) {
                                           factory.setScriptEngineManager(new ScriptEngineManager());
                                       }
                                   }
                               }
                               return action;
                            } catch (InstantiationException e) {
                                throw new RuntimeException(e);
                            } catch (IllegalAccessException e) {
                                throw new RuntimeException(e);
                            }
                        }
                    };
                    addCommand(cmd, function, commandProcessor);
                }
            } finally {
                reader.close();
            }
        }
    }