sonarqube-6.7.6源码浅析

原创
2019/01/09 11:32
阅读数 454

通过sonar-application模块中的org.sonar.application.App.main()方法启动
main() [org.sonar.application.App]
    |- start(String[] cliArguments) [org.sonar.application.App]
        |-new AppSettingsLoaderImpl(cliArguments) [org.sonar.application.config.AppSettingsLoaderImpl]
        |-new AppSettingsLoaderImpl(String[] cliArguments, File homeDir, Consumer<Props>... consumers) [org.sonar.application.config.AppSettingsLoaderImpl]  初始化参数:cliArguments,homeDir,FileSystemSettings,JdbcSettings,ClusterSettings.
        |-AppSettings settings = settingsLoader.load(); [org.sonar.application.AppSettingsLoaderImpl]
            |-loadPropertiesFile(homeDir) 加载conf/sonar.properties配置
            |-p.putAll(CommandLineParser.parseArguments(cliArguments)); 加载命令行配置和系统环境变量配置
            |-p.setProperty(ProcessProperties.PATH_HOME, homeDir.getAbsolutePath()); 设置sonar.path.home绝对路径
            |-Arrays.stream(consumers).forEach(c -> c.accept(props)); 调用FileSystemSettings,JdbcSettings,ClusterSettings三个Consumer接口实现类的accept方法,进行校验。
            |-return new AppSettingsImpl(props) 返回一个AppSettings实例,该实例类具备所有配置信息
        |-AppLogging logging = new AppLogging(settings);
        |-logging.configure(); 设置日志组件信息
        |-AppFileSystem fileSystem = new AppFileSystem(settings); 根据Settings中环境变量,处理有关文件系统操作的类
        |-new AppStateFactory(settings).create()
            |-ClusterSettings.isClusterEnabled(settings) 配置sonar.cluster.enabled是否为true/false
               //如果为true,设置网络、端口、节点等相关信息,然后返回ClusterAppState的实例
                |-HazelcastMember hzMember = createHzMember(settings.getProps());
                |-return new ClusterAppStateImpl(settings, hzMember);
                //如果为false,则直接返回AppState的实例
                |-return new AppStateImpl();
        |-appState.registerSonarQubeVersion(getSonarqubeVersion()); 注册Sonar版本号
        |-appState.registerClusterName(settings.getProps().nonNullValue(CLUSTER_NAME)); 注册cluster版本号
        |-AppReloader appReloader = new AppReloaderImpl(settingsLoader, fileSystem, appState, logging); 将上述组件注册进AppReloader中
        |-fileSystem.reset(); 创建各种目录sonar.path.data,sonar.path.web,sonar.path.logs,sonar.path.temp
        |-CommandFactory commandFactory = new CommandFactoryImpl(settings.getProps(), fileSystem.getTempDir(), System2.INSTANCE); 创建CommandFactory接口实例,用于createEsCommand,createCeCommand,createWebCommand
        |-ProcessLauncher processLauncher = new ProcessLauncherImpl(fileSystem.getTempDir()); 创建ProcessLauncher实例,用于启动进程
        |-Scheduler scheduler = new SchedulerImpl(settings, appReloader, commandFactory, processLauncher, appState); 创建Scheduler实例,用于确认进程状态
        |-Runtime.getRuntime().addShutdownHook(new ShutdownHook(scheduler)); 增加优雅退出
        |-scheduler.schedule(); 启动es,web,cs几个Server的进程
        |-StopRequestWatcher stopRequestWatcher = StopRequestWatcherImpl.create(settings, scheduler, fileSystem); 生成观察者线程,用于检查是否发送stop请求
        |-stopRequestWatcher.startWatching();
            |-settings.getProps().valueAsBoolean(ProcessProperties.ENABLE_STOP_COMMAND); 判断sonar.enableStopCommand为true或者false
                |- start(); 如果sonar.enableStopCommand配置为true,则启动观察者线程
        |-scheduler.awaitTermination(); 等待中断请求
        |-stopRequestWatcher.stopWatching();
            |-interrupt(); 中断观察者线程

public static void main(String... args) throws IOException {
    new App().start(args);
}

public void start(String[] cliArguments) throws IOException {
    AppSettingsLoader settingsLoader = new AppSettingsLoaderImpl(cliArguments);
    AppSettings settings = settingsLoader.load();
    // order is important - logging must be configured before any other components (AppFileSystem, ...)
    AppLogging logging = new AppLogging(settings);
    logging.configure();
    AppFileSystem fileSystem = new AppFileSystem(settings);

    try (AppState appState = new AppStateFactory(settings).create()) {
      appState.registerSonarQubeVersion(getSonarqubeVersion());
      appState.registerClusterName(settings.getProps().nonNullValue(CLUSTER_NAME));
      AppReloader appReloader = new AppReloaderImpl(settingsLoader, fileSystem, appState, logging);
      fileSystem.reset();
      CommandFactory commandFactory = new CommandFactoryImpl(settings.getProps(), fileSystem.getTempDir(), System2.INSTANCE);

      try (ProcessLauncher processLauncher = new ProcessLauncherImpl(fileSystem.getTempDir())) {
        Scheduler scheduler = new SchedulerImpl(settings, appReloader, commandFactory, processLauncher, appState);

        // intercepts CTRL-C
        Runtime.getRuntime().addShutdownHook(new ShutdownHook(scheduler));

        scheduler.schedule();

        stopRequestWatcher = StopRequestWatcherImpl.create(settings, scheduler, fileSystem);
        stopRequestWatcher.startWatching();

        scheduler.awaitTermination();
        stopRequestWatcher.stopWatching();
      }
    }

    systemExit.exit(0);
  }

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部