Launcher/LaunchServer/src/main/java/ru/gravit/launchserver/components/Component.java

42 lines
1.5 KiB
Java
Raw Normal View History

2019-03-13 12:13:21 +03:00
package ru.gravit.launchserver.components;
import ru.gravit.launchserver.LaunchServer;
2019-03-13 12:13:21 +03:00
import ru.gravit.utils.helper.VerifyHelper;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
public abstract class Component {
2019-03-13 12:13:21 +03:00
private static final Map<String, Class<? extends Component>> COMPONENTS = new ConcurrentHashMap<>(4);
private static boolean registredComp = false;
public static void registerComponent(String name, Class<? extends Component> adapter) {
2019-03-13 12:13:21 +03:00
VerifyHelper.verifyIDName(name);
VerifyHelper.putIfAbsent(COMPONENTS, name, Objects.requireNonNull(adapter, "adapter"),
String.format("Auth handler has been already registered: '%s'", name));
}
public static Class<? extends Component> getComponentClass(String name) {
2019-03-13 12:13:21 +03:00
return COMPONENTS.get(name);
}
public static String getComponentName(Class<Component> clazz) {
2019-03-13 12:13:21 +03:00
for (Map.Entry<String, Class<? extends Component>> e : COMPONENTS.entrySet()) {
if (e.getValue().equals(clazz)) return e.getKey();
}
return null;
}
public static void registerComponents() {
if (!registredComp) {
registerComponent("authLimiter", AuthLimiterComponent.class);
2019-03-28 13:26:47 +03:00
registerComponent("commandRemover", CommandRemoverComponent.class);
2019-03-13 12:13:21 +03:00
registredComp = true;
}
}
public abstract void preInit(LaunchServer launchServer);
public abstract void init(LaunchServer launchServer);
public abstract void postInit(LaunchServer launchServer);
2019-03-13 12:13:21 +03:00
}