add: 继续搭骨架

This commit is contained in:
wendal chen 2017-11-02 23:27:34 +08:00
parent b1406a9ecf
commit 3d4e96891e
6 changed files with 228 additions and 1 deletions

View File

@ -7,4 +7,29 @@
<version>2.0-SNAPSHOT</version>
</parent>
<artifactId>nutzboot-starter</artifactId>
<dependencies>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutz</artifactId>
<version>1.r.63-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.19</version>
</dependency>
</dependencies>
<distributionManagement>
<snapshotRepository>
<id>nutzcn-snapshots</id>
<name>NutzCN snapshot repository</name>
<url>https://jfrog.nutz.cn/artifactory/snapshots</url>
</snapshotRepository>
<repository>
<id>sonatype-release-staging</id>
<name>Sonatype Nexus release repository</name>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
</repository>
</distributionManagement>
</project>

View File

@ -0,0 +1,59 @@
package org.nutz.boot;
import org.nutz.boot.config.ConfigureLoader;
import org.nutz.ioc.Ioc;
import org.nutz.lang.util.LifeCycle;
public class AppContext implements LifeCycle {
protected static AppContext _default = new AppContext();
protected Ioc ioc;
protected ClassLoader classLoader;
protected ConfigureLoader configure;
public Ioc ioc() {
return ioc;
}
public Ioc getIoc() {
return ioc;
}
public void setIoc(Ioc ioc) {
this.ioc = ioc;
}
public void setClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}
public ClassLoader getClassLoader() {
return classLoader;
}
public ConfigureLoader getConfigure() {
return configure;
}
public void setConfigure(ConfigureLoader configure) {
this.configure = configure;
}
public static AppContext getDefault() {
return _default;
}
public static void setDefault(AppContext ctx) {
_default = ctx;
}
public void init() throws Exception {
}
public void fetch() throws Exception {
}
public void depose() throws Exception {
}
}

View File

@ -1,9 +1,56 @@
package org.nutz.boot;
import org.nutz.boot.config.ConfigureLoader;
import org.nutz.boot.config.YamlConfigureLoader;
import org.nutz.boot.ioc.NbIocLoader;
import org.nutz.ioc.impl.NutIoc;
import org.nutz.lang.Strings;
import org.nutz.log.LogAdapter;
import org.nutz.log.Logs;
public class NbApp {
public static void main(String[] args) {
public static void main(String[] args) throws Exception {
// 就是NB
// 初始化上下文
AppContext ctx = AppContext.getDefault();
// 检查ClassLoader的情况
if (ctx.getClassLoader() == null)
ctx.setClassLoader(NbApp.class.getClassLoader());
// 看看日志应该用哪个
String logAdapter = System.getProperty("nutz.boot.base.LogAdapter");
if (Strings.isBlank(logAdapter)) {
Logs.get();
} else {
Logs.setAdapter((LogAdapter) ctx.getClassLoader().loadClass(logAdapter).newInstance());
}
// 配置信息要准备好
if (ctx.getConfigure() == null) {
String cnfLoader = System.getProperty("nutz.boot.base.ConfigureLoader");
ConfigureLoader configure;
if (Strings.isBlank(cnfLoader)) {
configure = new YamlConfigureLoader();
} else {
configure = (ConfigureLoader) ctx.getClassLoader().loadClass(cnfLoader).newInstance();
}
ctx.setConfigure(configure);
}
// 创建Ioc容器
if (ctx.getIoc() == null) {
ctx.setIoc(new NutIoc(new NbIocLoader()));
}
// 加载各种Starter
// 排序各种starter
// 依次启动
// 等待关闭
// 收尾
}
}

View File

@ -0,0 +1,5 @@
package org.nutz.boot.config;
public interface ConfigureLoader {
}

View File

@ -0,0 +1,68 @@
package org.nutz.boot.config;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import org.nutz.lang.util.LifeCycle;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
public class YamlConfigureLoader extends NutMap implements ConfigureLoader, LifeCycle {
private static final Log log = Logs.get();
private static final long serialVersionUID = 1L;
public void init() throws Exception {
// 首先,尝试读取application.yml
String fromEnv = System.getProperty("nutz.configure.yaml_path", "application.yml");
InputStream ins = null;
File f = new File(fromEnv);
if (f.exists()) {
// 看来文件是存在的,读取之
log.debug("Loading Configure from " + f.getAbsolutePath());
readYaml(new FileInputStream(f));
return;
}
else {
// ClassPath中可以读到
ins = getClass().getClassLoader().getResourceAsStream(fromEnv);
if (ins != null) {
readYaml(ins);
return;
}
}
// 再试试application.properties
fromEnv = System.getProperty("nutz.configure.properties_path", "application.properties");
f = new File(fromEnv);
if (f.exists()) {
readProperties(new FileInputStream(f));
}
else {
ins = getClass().getClassLoader().getResourceAsStream(fromEnv);
if (ins != null) {
readProperties(ins);
return;
}
}
throw new RuntimeException("eithe application.yml or application.properties found");
}
protected void readYaml(InputStream ins ) {
}
protected void readProperties(InputStream ins) {
}
public void fetch() throws Exception {
}
public void depose() throws Exception {
}
}

View File

@ -0,0 +1,23 @@
package org.nutz.boot.ioc;
import org.nutz.ioc.IocLoader;
import org.nutz.ioc.IocLoading;
import org.nutz.ioc.ObjectLoadException;
import org.nutz.ioc.meta.IocObject;
public class NbIocLoader implements IocLoader {
public String[] getName() {
return null;
}
public IocObject load(IocLoading loading, String name) throws ObjectLoadException {
return null;
}
public boolean has(String name) {
return false;
}
}