/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.apache.skywalking.oap.server.library.module;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.ServiceLoader;

/**
 * The <code>ModuleManager</code> takes charge of all {@link ModuleDefine}s in collector.
 */
public class ModuleManager implements ModuleDefineHolder {
    private boolean isInPrepareStage = true;
    private final Map<String, ModuleDefine> loadedModules = new HashMap<>();

    /**
     * Init the given modules
     */
    public void init(
        ApplicationConfiguration applicationConfiguration) throws ModuleNotFoundException, ProviderNotFoundException, ServiceNotProvidedException, CycleDependencyException, ModuleConfigException, ModuleStartException {
        String[] moduleNames = applicationConfiguration.moduleList();
        ServiceLoader<ModuleDefine> moduleServiceLoader = ServiceLoader.load(ModuleDefine.class);
        ServiceLoader<ModuleProvider> moduleProviderLoader = ServiceLoader.load(ModuleProvider.class);

        LinkedList<String> moduleList = new LinkedList<>(Arrays.asList(moduleNames));
        for (ModuleDefine module : moduleServiceLoader) {
            for (String moduleName : moduleNames) {
                if (moduleName.equals(module.name())) {
                    module.prepare(this, applicationConfiguration.getModuleConfiguration(moduleName), moduleProviderLoader);
                    loadedModules.put(moduleName, module);
                    moduleList.remove(moduleName);
                }
            }
        }
        // Finish prepare stage
        isInPrepareStage = false;

        if (moduleList.size() > 0) {
            throw new ModuleNotFoundException(moduleList.toString() + " missing.");
        }

        BootstrapFlow bootstrapFlow = new BootstrapFlow(loadedModules);

        bootstrapFlow.start(this);
        bootstrapFlow.notifyAfterCompleted();
    }

    @Override
    public boolean has(String moduleName) {
        return loadedModules.get(moduleName) != null;
    }

    @Override
    public ModuleProviderHolder find(String moduleName) throws ModuleNotFoundRuntimeException {
        assertPreparedStage();
        ModuleDefine module = loadedModules.get(moduleName);
        if (module != null)
            return module;
        throw new ModuleNotFoundRuntimeException(moduleName + " missing.");
    }

    private void assertPreparedStage() {
        if (isInPrepareStage) {
            throw new AssertionError("Still in preparing stage.");
        }
    }
}

@@ -925,25 +925,22 @@
 va.util.
-LinkedLis
+HashSe
 t;%0Aimpor
@@ -1836,25 +1836,22 @@
         
-LinkedLis
+HashSe
 t%3CString
@@ -1862,19 +1862,18 @@
 dule
-Lis
+Se
 t = new 
 Link
@@ -1872,17 +1872,14 @@
 new 
-LinkedLis
+HashSe
 t%3C%3E(
@@ -1981,84 +1981,29 @@
     
-for (String moduleName : moduleNames) %7B%0A                if (moduleName.equal
+if (moduleSet.contain
 s(mo
@@ -2030,28 +2030,24 @@
             
-    
 module.prepa
@@ -2109,20 +2109,23 @@
 n(module
-N
+.n
 ame
+()
 ), modul
@@ -2158,20 +2158,16 @@
         
- 
-   
 loadedMo
@@ -2182,20 +2182,23 @@
 t(module
-N
+.n
 ame
+()
 , module
@@ -2220,21 +2220,16 @@
     
-  
-  
 module
-Lis
+Se
 t.re
@@ -2235,28 +2235,31 @@
 emove(module
-N
+.n
 ame
+()
 );%0A         
@@ -2257,34 +2257,26 @@
             
-    %7D%0A    
+%7D%0A
         %7D%0A  
@@ -2269,34 +2269,24 @@
 %7D%0A        %7D%0A
-        %7D%0A
         // F
@@ -2358,19 +2358,18 @@
  (module
-Lis
+Se
 t.size()
@@ -2428,19 +2428,18 @@
 n(module
-Lis
+Se
 t.toStri

optimize moduleServiceLoader traversal logic (#6537)