tomcat6 jndi 三
4.DBUserInfo.java如下:
package com.huawei.support.dao;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.huawei.it.common.encryption.exception.AppException;
import com.huawei.it.common.encryption.util.DESEncryptionFactory;
import com.huawei.it.common.encryption.util.DESEncryptor;
import com.huawei.support.exception.ApplicationException;
public class DBUserInfo
{
private static Log sLog = LogFactory.getLog(DBUserInfo.class);
private String mUserName;
private String mPassword;
public String getmUserName()
{
return mUserName;
}
public void setmUserName(String aUserName)
{
this.mUserName = aUserName;
}
public void setmPassword(String aPassword)
{
this.mPassword = aPassword;
}
public String getmPassword() throws ApplicationException
{
String pwd = null;
try
{
Context initCtx = new InitialContext();
if (initCtx == null)
{
throw new ApplicationException("Context初始化失败! ");
}
// 在JNDI中查找
Context ctx = (Context) initCtx.lookup("java:comp/env");
if (ctx != null)
{
DBUserInfo dbUserInfo = (DBUserInfo) ctx.lookup("dbUserInfo");
if (dbUserInfo != null)
{
// DES解密
pwd = decrypt(dbUserInfo.mPassword);
}
}
}
catch (NamingException e)
{
sLog.error(e);
// TODO:JUnit测试用,正式环境需要删除.
pwd = "tbz19078";
// throw new ApplicationException(e);
}
return pwd;
}
/** DES解密.
* @param aDataSource 加密过的密文
* @return 解密后的明文
* @throws ApplicationException ApplicationException
*/
private static String decrypt(String aDataSource)
throws ApplicationException
{
DESEncryptionFactory factory = null;
try
{
factory = DESEncryptionFactory.getInstance();
DESEncryptor encryptor =
factory.getEncryptor(DESEncryptionFactory.DES);
return encryptor.decrypt(aDataSource);
}
catch (AppException e)
{
sLog.error("DES解密出错!");
sLog.error(e);
throw new ApplicationException(e);
}
}
}