All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
net.e6tech.elements.common.resources.Provision Maven / Gradle / Ivy
/*
Copyright 2015-2019 Futeh Kao
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 net.e6tech.elements.common.resources;
import net.e6tech.elements.common.federation.Cluster;
import net.e6tech.elements.common.federation.Registry;
import net.e6tech.elements.common.inject.Inject;
import net.e6tech.elements.common.logging.LogLevel;
import net.e6tech.elements.common.logging.Logger;
import net.e6tech.elements.common.reflection.ObjectConverter;
import net.e6tech.elements.common.resources.plugin.Plugin;
import net.e6tech.elements.common.resources.plugin.PluginManager;
import net.e6tech.elements.common.resources.plugin.PluginPath;
import net.e6tech.elements.common.util.SystemException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
/**
* Created by futeh.
*/
@SuppressWarnings({"squid:S1444", "squid:ClassVariableVisibilityCheck"})
public class Provision {
public static final int JVM_VERSION;
public static Integer cacheBuilderConcurrencyLevel = 32;
private ResourceManager resourceManager;
private boolean suppressLogging = false;
public void suppressLogging() {
suppressLogging = true;
}
public void suppressLogging(Runnable runnable) {
try {
suppressLogging = true;
runnable.run();
} finally {
resumeLogging();
}
}
public void resumeLogging() {
suppressLogging = false;
}
public boolean isSuppressLogging() {
return suppressLogging;
}
static {
String version = System.getProperty("java.version");
int firstIdx = version.indexOf('.');
int verNumber = Integer.parseInt(version.substring(0, version.indexOf('.')));
if (verNumber == 1) {
int secondIdx = version.indexOf('.', firstIdx + 1);
verNumber = Integer.parseInt(version.substring(firstIdx + 1, secondIdx));
}
JVM_VERSION = verNumber;
}
public Provision load(Map map) {
Class cls = getClass();
while (Provision.class.isAssignableFrom(cls)) {
Field[] fields = cls.getDeclaredFields();
for (Field f : fields) {
setField(f, map);
}
cls = cls.getSuperclass();
}
return this;
}
private void setField(Field f, Map map) {
ObjectConverter converter = new ObjectConverter();
if (Modifier.isPublic(f.getModifiers())
&& !Modifier.isStatic(f.getModifiers())
&& map.get(f.getName()) != null) {
Object from = map.get(f.getName());
if (from != null) {
try {
f.setAccessible(true);
f.set(this, converter.convert(from, f, null, null));
f.setAccessible(false);
} catch (Exception e) {
throw new SystemException(e);
}
}
}
}
public void log(Logger logger, LogLevel level, String message, Throwable thIn) {
if (suppressLogging)
return;
Throwable th = thIn;
while (th instanceof RuntimeException) {
if (th.getCause() == null || th == th.getCause())
break;
th = th.getCause();
}
doLog(logger, level, message, th);
}
protected void doLog(Logger logger, LogLevel level, String message, Throwable th) {
logger.log(level, message, th);
}
public ResourceManager getResourceManager() {
return resourceManager;
}
@Inject
public void setResourceManager(ResourceManager resourceManager) {
this.resourceManager = resourceManager;
}
public Executor getExecutor() {
return resourceManager.getExecutor();
}
public T getComponentResource(String componentName, String resourceName) {
return getResourceManager().getAtomResource(componentName, resourceName);
}
public String getName() {
return resourceManager.getName();
}
public T nullableVar(String key) {
return resourceManager.nullableVar(key);
}
public Optional getVariable(String key) {
return resourceManager.getVariable(key);
}
public Properties getProperties() {
return resourceManager.getProperties();
}
public Map> getKnownEnvironments() {
return resourceManager.getKnownEnvironments();
}
public T getBean(String name) {
return resourceManager.getBean(name);
}
public T getBean(Class cls) {
return resourceManager.getBean(cls);
}
public Map getBeans() {
return resourceManager.getBeans();
}
public Map getBeans(Class cls) {
return resourceManager.getBeans(cls);
}
public List listBeans() {
return resourceManager.listBeans();
}
public T getInstance(Class cls) {
return resourceManager.getInstance(cls);
}
public T newInstance(Class cls) {
return resourceManager.newInstance(cls);
}
public Optional findInstance(Class cls) {
T t = resourceManager.getInstance(cls);
return Optional.ofNullable(t);
}
public T inject(T obj) {
return resourceManager.inject(obj);
}
public PluginManager getPluginManager() {
return resourceManager.getPluginManager();
}
public ClassLoader getPluginClassLoader() {
return resourceManager.getPluginClassLoader();
}
public Class extends Resources> getResourcesClass() {
return Resources.class;
}
public Optional getPlugin(Class c1, String n1, Class c2, Object ... args) {
return getPlugin(PluginPath.of(c1, n1).and(c2), args);
}
public Optional getPlugin(Class c1, String n1, Class c2, String n2, Class c3, Object ... args) {
return getPlugin(PluginPath.of(c1, n1).and(c2, n2).and(c3), args);
}
public Optional getPlugin(PluginPath path, Object ... args) {
return getInstance(PluginManager.class).get(path, args);
}
public UnitOfWork open() {
UnitOfWork unitOfWork = new UnitOfWork(resourceManager);
return unitOfWork.preOpen(null);
}
// used for configuring resourcesManager's resourceProviders before Resources is open
public UnitOfWork preOpen(Consumer consumer) {
UnitOfWork unitOfWork = new UnitOfWork(resourceManager);
return unitOfWork.preOpen(consumer);
}
public UnitOfWork onOpen(OnOpen onOpen) {
UnitOfWork unitOfWork = new UnitOfWork(resourceManager);
return unitOfWork.onOpen(onOpen);
}
public ResourcesFactory resourcesFactory() {
ResourcesFactory factory = new ResourcesFactory();
inject(factory);
return factory;
}
public void registerInCluster(String qualifier, Class interfaceClass, T implementation) {
if (getResourceManager().hasInstance(Cluster.class)) {
getResourceManager().getInstance(Cluster.class)
.getServiceProvider(Registry.class)
.register(qualifier, interfaceClass, implementation);
};
}
}