
com.lithium.flow.util.Main Maven / Gradle / Ivy
/*
* Copyright 2015 Lithium Technologies, Inc.
*
* 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.lithium.flow.util;
import static com.google.common.base.Preconditions.checkNotNull;
import com.lithium.flow.config.Config;
import com.lithium.flow.config.ConfigLoader;
import com.lithium.flow.config.Configs;
import com.lithium.flow.config.loaders.ClasspathConfigLoader;
import com.lithium.flow.config.loaders.FileConfigLoader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.slf4j.Logger;
/**
* @author Matt Ayres
*/
public interface Main {
Logger log = Logs.getLogger(3);
void main(@Nonnull Config config) throws Exception;
static void run() {
run(Thread.currentThread().getStackTrace()[2].getClassName());
}
static void run(@Nonnull String className) {
try {
run(Class.forName(className));
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
@Nonnull
static T run(@Nonnull Class clazz) {
checkNotNull(clazz);
AtomicReference ref = new AtomicReference<>();
run(clazz, config -> ref.set(run(clazz, config)));
return ref.get();
}
@Nonnull
static T run(@Nonnull Class clazz, @Nonnull Config config) throws Exception {
T instance;
try {
instance = clazz.getDeclaredConstructor(Config.class).newInstance(config);
} catch (NoSuchMethodException e) {
instance = clazz.getDeclaredConstructor().newInstance();
}
if (instance instanceof Main) {
((Main) instance).main(config);
}
return instance;
}
static void run(@Nonnull CheckedConsumer callback) {
run(null, callback);
}
static void run(@Nullable Class> clazz, @Nonnull CheckedConsumer callback) {
checkNotNull(callback);
try {
Config config = config(clazz);
Map map = new HashMap<>();
for (Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy