public class CustomDataSource extends BasicDataSourceFactory {
public Object getObjectInstance(Object obj, Name name, Context nameCtx,
@SuppressWarnings("rawtypes") Hashtable environment) throws Exception
{
if(obj instanceof Reference)
{
setUsername((Reference)obj);
setPassword((Reference)obj);
}
return super.getObjectInstance(obj, name, nameCtx, environment);
}
private void setUsername(Reference ref)
throws Exception
{
findDecryptAndReplace("username", ref);
}
private void setPassword(Reference ref)
throws Exception
{
findDecryptAndReplace("password", ref);
}
private void findDecryptAndReplace(String refType, Reference ref)
throws Exception
{
int idx = find(refType, ref);
String decrypted = decrypt(refType);
replace(idx, refType, decrypted, ref);
}
private void replace(int idx, String refType, String newValue, Reference ref)
throws Exception
{
ref.remove(idx);
ref.add(idx, new StringRefAddr(refType, newValue));
}
private String decrypt(String key)throws Exception
{
return getDecViaSeed(getProperty(key));
}
private int find(String addrType, Reference ref)
{
try{
@SuppressWarnings("rawtypes")
Enumeration enu = ref.getAll();
for(int i = 0; enu.hasMoreElements(); i++)
{
RefAddr addr = (RefAddr)enu.nextElement();
if(addr.getType().compareTo(addrType) == 0)
return i;
}
}catch(Exception e){
e.printStackTrace();
}
return 0;
}
public String getDecViaSeed(String str) {
String strDecrypt = "";
try
{
// strDecrypt = 복호화 처리
// 실제 테스트를 위해서는 간단히 BASE64 정도로 테스트 해봐도 된다.
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
return strDecrypt;
}
public String getProperty(String key) {
String rtnStr = "";
try {
Properties prop = new Properties();
prop.load(new FileInputStream("../conf/dbinfo.properties"));
rtnStr = prop.getProperty(key);
} catch ( IOException ioe ) {
ioe.printStackTrace();
return null;
} catch ( Exception e ) {
e.printStackTrace();
}
return rtnStr;
}
} |