开源b3log代码赏析

package org.b3log.latke;

public class testEVN {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Latkes.initRuntimeEnv();
		System.out.println(Latkes.getRuntimeEnv());

	}

}

输出:

七月 15, 2013 7:55:52 上午 org.b3log.latke.Latkes initRuntimeEnv
INFO: Latke is running on [LOCAL] with mode [DEVELOPMENT]
七月 15, 2013 7:55:53 上午 org.b3log.latke.Latkes initRuntimeEnv
INFO: Runtime database is [MYSQL]
LOCAL

latke.properties:

# Configures the followings before deploy
serverScheme=http
serverHost=localhost
serverPort=8080
staticServerScheme=http
staticServerHost=localhost
staticServerPort=8080
# Note: The context path and static path should be "" if deploy app on ROOT. For other cases, starts with '/'
#       and not ends with '/', for example, /blog 
contextPath=
staticPath=

#### Runtime Environment ####
runtimeEnv=LOCAL

#### Runtime Mode ####
runtimeMode=DEVELOPMENT
#runtimeMode=PRODUCTION

#### Cache Implementation ####
# Note: If the runtime environment is LOCAL, the cache will be 'LOCAL' always
cache=LOCAL

#### User Service Implementation ####
userService=LOCAL

#### Cache ####
cache.maxPageCnt=128
cache.maxDataCnt=128

#### Static resource version ####
staticResourceVersion=201211021410

public final class Latkes {
 /**
     * Latke configurations (latke.properties).
     */
    private static final Properties LATKE_PROPS = new Properties();
try {
            final InputStream resourceAsStream = Latkes.class.getResourceAsStream("/local.properties");

public static void initRuntimeEnv() {
        LOGGER.log(Level.FINEST, "Initializes runtime environment from configuration file");
        final String runtimeEnvValue = LATKE_PROPS.getProperty("runtimeEnv");

        if (null != runtimeEnvValue) {
            runtimeEnv = RuntimeEnv.valueOf(runtimeEnvValue);
        }

        if (null == runtimeEnv) {
            LOGGER.log(Level.FINEST, "Initializes runtime environment by class loading");

            try {
                runtimeEnv = RuntimeEnv.GAE;
                Class.forName("org.b3log.latke.repository.gae.GAERepository");
            } catch (final ClassNotFoundException e) {
                runtimeEnv = RuntimeEnv.LOCAL;
            }
        }

        final String runtimeModeValue = LATKE_PROPS.getProperty("runtimeMode");

        if (null != runtimeModeValue) {
            runtimeMode = RuntimeMode.valueOf(runtimeModeValue);
        } else {
            LOGGER.log(Level.FINEST, "Can't parse runtime mode in latke.properties, default to [DEVELOPMENT]");
            runtimeMode = RuntimeMode.DEVELOPMENT;
        }

        LOGGER.log(Level.INFO, "Latke is running on [{0}] with mode [{1}]", new Object[] {Latkes.getRuntimeEnv(), Latkes.getRuntimeMode()});

        if (RuntimeEnv.LOCAL == runtimeEnv) {
            // Read local database configurations
            final RuntimeDatabase runtimeDatabase = getRuntimeDatabase();

            LOGGER.log(Level.INFO, "Runtime database is [{0}]", runtimeDatabase);

            if (RuntimeDatabase.H2 == runtimeDatabase) {
                final String newTCPServer = Latkes.getLocalProperty("newTCPServer");

                if ("true".equals(newTCPServer)) {
                    LOGGER.log(Level.INFO, "Starting H2 TCP server");

                    final String jdbcURL = Latkes.getLocalProperty("jdbc.URL");

                    if (Strings.isEmptyOrNull(jdbcURL)) {
                        throw new IllegalStateException("The jdbc.URL in local.properties is required");
                    }

                    final String[] parts = jdbcURL.split(":");

                    if (parts.length != Integer.valueOf("5")/* CheckStyle.... */) {
                        throw new IllegalStateException("jdbc.URL should like [jdbc:h2:tcp://localhost:8250/~/] (the port part is required)");
                    }

                    String port = parts[parts.length - 1];

                    port = StringUtils.substringBefore(port, "/");

                    LOGGER.log(Level.FINEST, "H2 TCP port [{0}]", port);

                    try {
                        h2 = Server.createTcpServer(new String[] {"-tcpPort", port, "-tcpAllowOthers"}).start();
                    } catch (final SQLException e) {
                        final String msg = "H2 TCP server create failed";

                        LOGGER.log(Level.SEVERE, msg, e);
                        throw new IllegalStateException(msg);
                    }

                    LOGGER.info("Started H2 TCP server");
                }
            }
        }

        locale = new Locale("en_US");
    }


源代码:http://pan.baidu.com/share/link?shareid=1644033861&uk=3878681452

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值