프로그램/java

JAVA JNDI GlobalNamingResources String Type define Example ( 운영과 개발을 쉽게 하고 deploy를 쉽게 하기)

mulderu 2011. 12. 10. 12:16
Java 프로그램을 하다보면, 개발환경변수와 운영환경변수가 다른경우 deploy 가 매우 까다로와 진다.
WEB-INF 및에 변수를 가지고 있다면 항상 골치거리가 된다.

이러한 문제를 해결하기 위한 방안 하나를 제안 한다....

j2ee 의 jndi name 변수를 이용하는거다,  
(정확히는 GlobalNamingResource 에 변수를 저장하고 이용하는 방법)

아래는 가장 많이 사용하는 tomcat 을 가지고 처리하는 예제 임.
server.xml 에 변수값을 저장하므로, context 이하에서는 개발과 운영이 서로 다른 변수를 가지고 있을 수가 없다.

예제소스: 



아래는 각 부분별 일부 소스입니다.. 도움이 되기 위해 전체소스를 첨부 합니다.

$CATALINA_HOME/server.xml

<GlobalNamingResources>
   <Environment name="maxExemptions" value="10"
        type="java.lang.Integer" override="false"/>
        
   <Environment name="service.host.aaa" value="www.naver.com"
        type="java.lang.String" override="false"/>
        
   <Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
 </GlobalNamingResources>
 



context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>  
<!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    
    <ResourceLink 
            name="maxExemptions"
            global="maxExemptions"
            type="java.lang.Integer"
          />
    <ResourceLink 
            name="service.host.aaa"
            global="service.host.aaa"
            type="java.lang.String"
          />
          
   

</Context>


 

jndi name guery

public class UserGlobalNameUseExample {
private static Context envCtx;
static {
Context initCtx;
try {
initCtx = new InitialContext();
envCtx = (Context) initCtx.lookup("java:comp/env");
catch (NamingException e) {
e.printStackTrace();
}
}
public Object getGlobalJNDIResource(String name) throws Exception {
return envCtx.lookup(name);
}
}





name print example


<%
UserGlobalNameUseExample gname = new UserGlobalNameUseExample();
%>
                        <li>
<h2>maxExemtions: <%= gname.getGlobalJNDIResource("maxExemptions") %> </h2>
</li>
<li>
<h2>service.host.aaa: <%= gname.getGlobalJNDIResource("service.host.aaa") %> </h2>
</li>