All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nu.art.modular.core.ModuleManagerBuilder Maven / Gradle / Ivy

There is a newer version: 1.2.59
Show newest version
/*
 * The module-manager project, is THE infrastructure that all my frameworks
 *  are based on, it allows encapsulation of logic where needed, and allow
 *  modules to converse without other design patterns limitations.
 *
 * Copyright (C) 2018  Adam van der Kruk aka TacB0sS
 *
 * Licensed 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 com.nu.art.modular.core;

import com.nu.art.belog.Logger;
import com.nu.art.modular.core.ModuleManager.ModuleCreatedListener;
import com.nu.art.modular.core.ModuleManager.ModuleInitializedListener;
import com.nu.art.reflection.tools.ReflectiveTools;

import java.util.ArrayList;

public class ModuleManagerBuilder
	extends Logger {

	private ArrayList modulePacks = new ArrayList<>();
	private ModuleInitializedListener moduleInitializedListener = (this instanceof ModuleInitializedListener ? (ModuleInitializedListener) this : null);
	private ModuleCreatedListener moduleCreatedListener = (this instanceof ModuleCreatedListener ? (ModuleCreatedListener) this : null);

	public ModuleManagerBuilder() {
	}

	public void setModuleInitializedListener(ModuleInitializedListener moduleInitializedListener) {
		this.moduleInitializedListener = moduleInitializedListener;
	}

	public void setModuleCreatedListener(ModuleCreatedListener moduleCreatedListener) {
		this.moduleCreatedListener = moduleCreatedListener;
	}

	@SuppressWarnings("unchecked")
	public final ModuleManagerBuilder addModulePacks(Class... modulePacks) {
		for (Class packType : modulePacks) {
			ModulesPack pack = ReflectiveTools.newInstance(packType);
			this.modulePacks.add(pack);
		}
		return this;
	}

	@SuppressWarnings("unchecked")
	public final ModuleManagerBuilder addModules(Class... modules) {
		this.modulePacks.add(new ModulesPack(modules));
		return this;
	}

	public final ModuleManager build() {
		return build(new ModuleManager());
	}

	public final ModuleManager build(ModuleManager moduleManager) {
		moduleManager.setModuleCreatedListener(this.moduleCreatedListener);
		moduleManager.setModuleInitializedListener(this.moduleInitializedListener);

		ArrayList> modulesTypes = new ArrayList<>();

		for (ModulesPack pack : modulePacks) {
			pack.setManager(moduleManager);
			for (Class moduleType : pack.getModuleTypes()) {
				if (modulesTypes.contains(moduleType))
					continue;

				modulesTypes.add(moduleType);
				moduleManager.registerModule(moduleType);
			}
		}

		for (ModulesPack pack : modulePacks) {
			pack.init();
		}

		Module[] registeredModules = moduleManager.getOrderedModules();
		validateModules(registeredModules);

		for (Module registeredModule : registeredModules) {
			moduleManager.getInjector().injectToInstance(registeredModule);
		}

		moduleManager.onBuildPrepared();
		moduleManager.init();
		for (Module module : registeredModules) {
			logInfo("----------- " + module.getClass().getSimpleName() + " ------------");
			module.printDetails();
			logInfo("-------- End of " + module.getClass().getSimpleName() + " --------");
		}

		moduleManager.onBuildCompleted();
		return moduleManager;
	}

	@SuppressWarnings("unused")
	private void validateModules(Module[] allRegisteredModuleInstances) {
		ValidationResult result = new ValidationResult();

		for (Module module : allRegisteredModuleInstances)
			module.validateModule(result);

		if (!result.isEmpty())
			throw new com.nu.art.modular.exceptions.ModuleNotSupportedException("\n" + result.getErrorData());
	}

	@SuppressWarnings("unused")
	protected void postInit(Module[] allRegisteredModuleInstances) {
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy