제목이 이상하지만 의도는 Framework를 사용하지 않고 tomcat의 context.xml과 web.xml을 사용하여 경로 설정과 같은 변동이 쉬운 환경 설정 값을 저장하는 방법을 찾아 봄.

우선 JNDI Resource에 대한 문서을 보면 Generic JavaBean Resource에 대한 사항이 나온다. 그리고 이를 이용하여 사용자가 정의한 MyBean을 사용하는 방법이 나오는데 이를 적절하게 이용하면 처음에 의도한 특정 값들을 가져올 수 있을거라 결론을 내림

그래서 나온 ContextValueFactory.java

package kr.pe.egg.misc;

import java.util.Enumeration;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.Name;
import javax.naming.RefAddr;
import javax.naming.spi.ObjectFactory;
import javax.naming.Reference;

public class ContextValueFactory implements ObjectFactory {

	public Object getObjectInstance(Object obj, Name name, Context context,
			Hashtable env) throws Exception {
		// TODO Auto-generated method stub

		// Customize the properties from attributes
		Reference reference = (Reference) obj;
		Enumeration addrs = reference.getAll();
		while (addrs.hasMoreElements()) {
			RefAddr addr = addrs.nextElement();
			String type = addr.getType();

			if (type.equals("value") == true) {
				return (String) addr.getContent();
			}
		}
		return null;
	}
}

value에 설정된 값을 String으로 받아오는 정말 아무것도 고려 안한... 생 코드.

사용법은 context.xml


web.xml

	
	   resource name
	   java.lang.String
	   Container
	

으로 하여 설정하고 실제 code에서는

Context initContext = new InitialContext();
String type = (String) initContext.lookup("java:/comp/env/factory_type");

분명. Framework에서 지원을 하거나 다른 방법이 있을 것 같긴한데... 일단 데모 환경이 열악하니...;;