任意一个JAVA程序都是从main()开始启动的,OFBIZ也不例外。OFBIZ的main()位于framework/start/src/org/ofbiz/base/start/Start.java:

public final class Start {

    private static final Start instance = new Start();

    public static void main(String[] args) throws StartupException {
... ... instance.init(args, command == Command.COMMAND);
try {
if (command == Command.STATUS) {
System.out.println("Current Status : " + instance.status());
} else if (command == Command.SHUTDOWN) {
System.out.println("Shutting down server : " + instance.shutdown());
} else {
// general start
instance.start();
}
} catch (Exception e) {
e.printStackTrace();
System.exit(99);
}
} void init(String[] args, boolean fullInit) throws StartupException {
... ... // initialize the startup loaders
initStartLoaders();
} private void initStartLoaders() throws StartupException {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
synchronized (this.loaders) {
// initialize the loaders
for (Map<String, String> loaderMap : config.loaders) {
if (this.serverState.get() == ServerState.STOPPING) {
return;
}
try {
String loaderClassName = loaderMap.get("class");
Class<?> loaderClass = classloader.loadClass(loaderClassName);
StartupLoader loader = (StartupLoader) loaderClass.newInstance();
loader.load(config, loaderArgs.toArray(new String[loaderArgs.size()]));
loaders.add(loader);
} catch (ClassNotFoundException e) {
throw (StartupException) new StartupException(e.getMessage()).initCause(e);
} catch (InstantiationException e) {
throw (StartupException) new StartupException(e.getMessage()).initCause(e);
} catch (IllegalAccessException e) {
throw (StartupException) new StartupException(e.getMessage()).initCause(e);
}
}
this.loaders.trimToSize();
}
return;
} void start() throws Exception {
if (!startStartLoaders()) {
if (this.serverState.get() == ServerState.STOPPING) {
return;
} else {
throw new Exception("Error during start.");
}
}
if (config.shutdownAfterLoad) {
stopServer();
}
} boolean startStartLoaders() {
synchronized (this.loaders) {
// start the loaders
for (StartupLoader loader : this.loaders) {
if (this.serverState.get() == ServerState.STOPPING) {
return false;
}
try {
loader.start();
} catch (StartupException e) {
e.printStackTrace();
return false;
}
}
}
return this.serverState.compareAndSet(ServerState.STARTING, ServerState.RUNNING);
}
}

OFBIZ的main()过程可以简单理解为两个步骤:init()和start()。init()初始化StartupLoader,通过initStartLoaders()实现。start()启动StartupLoader,通过startStartLoaders()实现。
StartupLoader是一个接口,实现载入器的整个生命周期。

/**
* An object that loads server startup classes.
* <p>
* When OFBiz starts, the main thread will create the <code>StartupLoader</code> instance and
* then call the loader's <code>load</code> method. If the method returns without
* throwing an exception the loader will be added to a list of initialized loaders.
* After all instances have been created and initialized, the main thread will call the
* <code>start</code> method of each loader in the list. When OFBiz shuts down, a
* separate shutdown thread will call the <code>unload</code> method of each loader.
* Implementations should anticipate asynchronous calls to the methods by different
* threads.
* </p>
*
*/
public interface StartupLoader { /**
* Load a startup class.
*
* @param config Startup config.
* @param args Command-line arguments.
* @throws StartupException If an error was encountered. Throwing this exception
* will halt loader loading, so it should be thrown only when OFBiz can't
* operate without it.
*/
public void load(Config config, String args[]) throws StartupException; /**
* Start the startup class. This method must not block - implementations
* that require thread blocking must create a separate thread and then return.
*
* @throws StartupException If an error was encountered.
*/
public void start() throws StartupException; /**
* Stop the startup class. This method must not block.
*
* @throws StartupException If an error was encountered.
*/
public void unload() throws StartupException;
}

initStartLoaders()从config.loaders获取可用的载入器信息,然后创建StartupLoader实例,调用load()方法。

private void initStartLoaders() throws StartupException {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
synchronized (this.loaders) {
// initialize the loaders
for (Map<String, String> loaderMap : config.loaders) {
if (this.serverState.get() == ServerState.STOPPING) {
return;
}
try {
String loaderClassName = loaderMap.get("class");
Class<?> loaderClass = classloader.loadClass(loaderClassName);
StartupLoader loader = (StartupLoader) loaderClass.newInstance();
loader.load(config, loaderArgs.toArray(new String[loaderArgs.size()]));
loaders.add(loader);
} catch (ClassNotFoundException e) {
throw (StartupException) new StartupException(e.getMessage()).initCause(e);
} catch (InstantiationException e) {
throw (StartupException) new StartupException(e.getMessage()).initCause(e);
} catch (IllegalAccessException e) {
throw (StartupException) new StartupException(e.getMessage()).initCause(e);
}
}
this.loaders.trimToSize();
}
return;
}

config.loaders来源于配置文件framework/start/src/org/ofbiz/base/start/start.properties。

# --- StartupLoader implementations to load (in order)
ofbiz.start.loader1=org.ofbiz.base.container.ContainerLoader
ofbiz.start.loader1.loaders=main,rmi

config是framework/start/src/org/ofbiz/base/start/Config.java的一个实例。Config类调用readConfig()过程读取start.properties中的配置信息。

public class Config {

    public List<Map<String, String>> loaders;

    public static Config getInstance(String[] args) throws IOException {
String firstArg = args.length > 0 ? args[0] : "";
// Needed when portoffset is used with these commands, start.properties fits for all of them
if ("start-batch".equalsIgnoreCase(firstArg)
|| "start-debug".equalsIgnoreCase(firstArg)
|| "stop".equalsIgnoreCase(firstArg)
|| "-shutdown".equalsIgnoreCase(firstArg)
|| "-status".equalsIgnoreCase(firstArg)) {
firstArg = "start";
}
String configFileName = getConfigFileName(firstArg);
Config result = new Config();
result.readConfig(configFileName, args);
return result;
} private static String getConfigFileName(String command) {
// default command is "start"
if (command == null || command.trim().length() == 0) {
command = "start";
}
return "org/ofbiz/base/start/" + command + ".properties";
} public void readConfig(String config, String[] args) throws IOException {
... ... // loader classes
loaders = new ArrayList<Map<String, String>>();
int currentPosition = 1;
Map<String, String> loader = null;
while (true) {
loader = new HashMap<String, String>();
String loaderClass = props.getProperty("ofbiz.start.loader" + currentPosition);
if (loaderClass == null || loaderClass.length() == 0) {
break;
} else {
loader.put("class", loaderClass);
loader.put("profiles", props.getProperty("ofbiz.start.loader" + currentPosition + ".loaders"));
loaders.add(loader);
currentPosition++;
}
}
}
}

OFBIZ:启动之StartupLoader的更多相关文章

  1. 【转】Ofbiz学习经验谈

    不可否认,OFBiz这个开源的系统功能是非常强大的,涉及到的东西太多了,其实对我们现在而言,最有用的只有这么几个:实体引擎.服务引擎.WebTools.用户权限管理.最先要提醒各位的是,在配置一个OF ...

  2. ofbiz安装优化

    一. 1.安装jdk 2.安装数据库 3.安装ant yum install ant 4.编译启动ofbiz cd /ofbiz目录下 ant run-install ./startofbiz.sh ...

  3. OFBiz:添加实体栏位

    如何添加实体栏位?这里演示为PostalAddress添加planet栏位.打开applications/party/entitydef/entitymodel.xml,找到PostalAddress ...

  4. gradle ofbiz 16 开发环境搭建

    原 gradle ofbiz 16 开发环境搭建 2017年02月13日 10:59:19 阅读数:2702 1.安装jdk 2.配置jdk环境变量 3.eclipse 安装svn 插件 4.svn下 ...

  5. ApacheOFBiz的相关介绍以及使用总结(一)

    由于最近一段时间在给一个创业的公司做客户关系管理CRM系统,限于人力要求(其实是没有多少人力),只能看能否有稳定,开源的半成品进行改造,而且最好不需要前端(js)相关开发人员的支援就可以把事情做成,经 ...

  6. OFBIZ:启动之ContainerLoader

    ContainerLoader类实现StartupLoader接口,目的是装入各种Container容器. /** * An OFBiz container. A container can be t ...

  7. ofbiz idea 启动

    1.下载gradle并安装到本地 2.idea引入gradle 3.gradle右键选择refresh,项目会重新编译并加载gradle的task 4.可以再编译一下 5.没问题的话打开,jar ap ...

  8. [OFBiz]开发 三

    1. Debug不要在Eclipse中使用Ant来启动ofbiz, 因为在Eclipse中无法kill掉Ant的进程,而ofbiz又没有提供stop的方法.(有一个hook shutdown的方法,但 ...

  9. OFBiz:配置过程

    OFBiz使用了大量的配置文件,整个过程有点复杂.这里将配置过程大略整理了一下,方便后面查阅. 第一层:org.ofbiz.base.start.Start启动类.该类载入org/ofbiz/base ...

随机推荐

  1. .htaccess设置自定义出错页面

    404错误可以这么写 ErrorDocument code error.php 如果是404错误,跳到文件error.php 其他常用错误页面写法(其中404错误有2种写法,上面一种,下面是通用错误定 ...

  2. MSSQL CharIndex()用法

    CHARINDEX函数返回字符或者字符串在另一个字符串中的起始位置. CHARINDEX ( expression1 , expression2 [ , start_location ] ) expr ...

  3. sql like in 语句获取以逗号分割的字段内的数据

    From:http://www.cnblogs.com/goody9807/archive/2011/07/27/2118107.html sql中的某个字段用“,”分隔数据,需要获取数据的时候直接把 ...

  4. Rhel6-pacemaker+drbd配置文档

    系统环境: rhel6 x86_64 iptables and selinux disabled 主机: 192.168.122.119 server19.example.com 192.168.12 ...

  5. H5实现俄罗斯方块(三)

    最高分的面板: (function (window) { 'use strict'; function HighScore() { this.canvas = new Canvas('highscor ...

  6. 公测后,微信小程序应用可能被拒原因.

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 34.0px "PingFang SC Semibold"; color: #23232 ...

  7. Warning: Attempt to dismiss from view controller <UIViewController: 0x17d71c10> while a presentation or dismiss is in progress!

    昨天 调试程序 已经快要上线了 突然有个BUG 找了半天 才找到是因为这个警告 但是 解决这个警告又花了一天的时间 试了各种消除控制器的方法 都不可用 其中 并且 有这个bug  手机真机测试完全没问 ...

  8. Android 学习第5课,配置android

    昨天与今天都在配置安卓的开发环境, 搞的头都大了,步骤比较多 1. 先下载安装 java 的 jdk ,这个是最基础的组件 2. 再下载 android SDK,  http://developer. ...

  9. 【转载】分享一些Qt学习资源,欢迎下载

    资源来源:http://bbs.csdn.net/topics/390358737 经过我一翻整理,把一些我收集到的Qt学习资源分享给大家,主要适合新手,老鸟可以直接忽略我.要说明一下,很多资源都是在 ...

  10. Erlang 101 Erlang环境和顺序编程

    笔记系列 Erlang环境和顺序编程 Erlang并发编程 Erlang分布式编程 Yaws Erlang/OTP 日期              变更说明2014-10-12 A outline, ...