Glassfish 3.1:我在开发或生产环境中运行吗?

时间:2012-02-17 17:20:16

标签: java-ee deployment glassfish environment-variables

JavaEE应用程序确定它是在开发或生产Glassfish服务器上运行的最佳方法是什么?

2 个答案:

答案 0 :(得分:2)

您可以使用JNDI。执行以下操作:

创建资源文件:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE resources PUBLIC "-//Sun Microsystems Inc.//DTD Application Server 9.0 Domain//EN" "*<install directory>/lib/dtds/sun-resources_1_3.dtd*">

<resources>
    <custom-resource 
        res-type="java.lang.Boolean" 
        jndi-name="production" 
        factory-class="org.glassfish.resources.custom.factory.PrimitivesAndStringFactory">
            <description>Determines, wether the Frontend is running in Production or not. AFTER CHANGING, FRONTEND NEEDS TO BE REDEPLOYED</description>
            <property name="value" value="true"/> <!-- Change the value as needed -->
    </custom-resource>

</resources>

然后通过asadmin

将其添加到您的域中

asadmin add-resources resources-file.xml

有关详细信息,请参阅http://docs.oracle.com/cd/E18930_01/html/821-2416/gixps.html

查询实际资源值:

javax.naming.InitialContext ic = new javax.naming.InitialContext();

if ((Boolean) ic.lookup("production")){
     // PRODUCTION
} else {
     // DEVELOPMENT
}

如果您需要区分更多环境,请使用String而不是布尔值。

HTH

答案 1 :(得分:0)

如果您使用的是jsf,请尝试以下方法:

FacesContext fc = FacesContext.getCurrentInstance(); 
Application a = fc.getApplication();
if (a.getProjectStage() == ProjectStage.Development) { }