package hiro.yoshioka.classmanager;

import javax.naming.Context;
import javax.naming.NamingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LookUper {
	private static Log log = LogFactory.getLog(LookUper.class);
	Context ctx;
	private String _factory = "weblogic.jndi.WLInitialContextFactory";
	private String _url = "t3://localhost:7001";
	private String _user = "weblogic";
	private String _pass = "weblogic";

	public LookUper() throws NamingException {
		// createContext();
	}

	public LookUper(String factory, String url, String user, String pass)
			throws NamingException {
		this();
		_factory = factory;
		_url = url;
		_user = user;
		_pass = pass;
		// createContext();
	}

	// private void createContext() throws NamingException {
	// Environment env = new Environment();
	// env.setInitialContextFactory(
	// weblogic.jndi.Environment.DEFAULT_INITIAL_CONTEXT_FACTORY);
	// env.setProviderUrl(_url);
	// env.setSecurityPrincipal(_user);
	// env.setSecurityCredentials(_pass);
	// logger.info("URL" + _url);
	// ctx = env.getInitialContext();
	// }
	//
	// public EJBHome lookup(String jndiName) throws NamingException {
	// logger.info("LookUp : " + jndiName);
	// return (EJBHome) ctx.lookup(jndiName);
	// }
	//
	// public EJBObject createRemote(String jndi) throws NamingException {
	// return createRemote(lookup(jndi));
	// }
	//
	// public EJBObject createRemote(EJBHome home) {
	// try {
	// logger.info("createRemote ! " + home);
	// Method homeMethod =
	// home.getClass().getMethod("create", new Class[] {
	// });
	//
	// return (EJBObject) homeMethod.invoke(home, new Object[] {
	// });
	// } catch (Exception e) {
	// logger.log(Level.SEVERE, "ara------", e);
	// }
	// return null;
	// }

	public void closeContext() throws NamingException {
		if (ctx != null) {
			log.warn("Context close!");
			ctx.close();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#finalize()
	 */
	protected void finalize() throws Throwable {
		if (ctx != null) {
			log.warn("Context close! in finalize");
			ctx.close();
		}
		super.finalize();
	}

}
