
com.steenhq.application.Module Maven / Gradle / Ivy
/*
* Copyright 2018 Frederick Steen
*
* 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.steenhq.application;
import java.io.IOException;
import java.util.Collection;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import com.steenhq.application.module.AbstractModule;
import com.steenhq.application.util.ReflectionUtil;
import com.steenhq.application.annotation.DontRegister;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class Module {
private static final Module INSTANCE = new Module();
private final ConcurrentHashMap, AbstractModule> modules = new ConcurrentHashMap<>();
@SuppressWarnings("unchecked")
public static T getInstance(Class clazz) {
return (T) INSTANCE.modules.get(clazz);
}
public static Collection extends AbstractModule> getModules() {
return INSTANCE.modules.values();
}
@SuppressWarnings("unchecked")
public static Optional getInstance(String name, Class def) {
return getModules().stream().filter(m -> m.getName().equalsIgnoreCase(name)).map(o -> {
try {
return Optional.ofNullable((T) o);
} catch (Exception e) {
}
return Optional.empty();
}).findAny().filter(o -> o.isPresent()).map(o -> (T) o.get());
}
protected static void shutdown() {
INSTANCE.modules.values().forEach(a -> {
fireDisable(a);
});
}
private static void fireDisable(AbstractModule module) {
try {
module.onDisable();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void fireEnable(AbstractModule module) {
try {
module.getLogger().info("Loading Module..");
module.onEnable();
module.getLogger().info("Module was enabled!");
} catch (Exception e) {
e.printStackTrace();
}
}
protected static void offerForRegistration(Class extends AbstractModule> module, ApplicationLoader loader)
throws InstantiationException, IllegalAccessException, ClassNotFoundException, IOException {
INSTANCE.register(module.newInstance(), loader);
}
protected void register(AbstractModule module, ApplicationLoader loader)
throws ClassNotFoundException, IOException {
Set> classes = ReflectionUtil
.getClassesRecursively(module.getClass().getPackage().getName(), loader.getLoader()).stream()
.filter(clazz -> !clazz.isAnnotationPresent(DontRegister.class)).collect(Collectors.toSet());
loader.getLoaders().forEach(m -> {
try {
m.load(classes, module);
} catch (Exception e) {
e.printStackTrace();
}
});
modules.put(module.getClass(), module);
fireEnable(module);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy