package com.svcm.util;
import java.lang.reflect.Field;
import javax.naming.NamingException;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.jndi.JndiObjectLocator;
import org.springframework.jndi.JndiObjectTargetSource;
import com.svcm.cyber.pidmsDemo.PasswordFactory;
public class MyJndiObjectFactoryBean extends JndiObjectLocator implements FactoryBean {
private Class proxyInterface;
private boolean lookupOnStartup = true;
private boolean cache = true;
private Object jndiObject;
public void setProxyInterface(Class proxyInterface) {
if (!proxyInterface.isInterface()) {
throw new IllegalArgumentException("[" + proxyInterface.getName() + "] is not an interface");
}
this.proxyInterface = proxyInterface;
}
public void setLookupOnStartup(boolean lookupOnStartup) {
this.lookupOnStartup = lookupOnStartup;
}
public void setCache(boolean cache) {
this.cache = cache;
}
public void afterPropertiesSet() throws NamingException {
super.afterPropertiesSet();
if (this.proxyInterface != null) {
// We need a proxy and a JndiObjectTargetSource.
this.jndiObject = JndiObjectProxyFactory.createJndiObjectProxy(this);
}
else {
if (!this.lookupOnStartup || !this.cache) {
throw new IllegalArgumentException(
"Cannot deactivate 'lookupOnStartup' or 'cache' without specifying a 'proxyInterface'");
}
// Locate specified JNDI object.
this.jndiObject = lookup();
Object mcf = null;
try {
mcf = getPropertyValue(this.jndiObject,"mcf");
System.out.println("password1:"+ getPropertyValue(mcf,"password"));
Field f;
try {
PasswordFactory passwordFactory = new PasswordFactory();
setPropertyValue(mcf,"password",passwordFactory.getObject());
System.out.println("password2:"+ getPropertyValue(mcf,"password"));
} catch (SecurityException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
public Object getObject() {
return this.jndiObject;
}
public Class getObjectType() {
if (this.jndiObject != null) {
return this.jndiObject.getClass();
}
else if (this.proxyInterface != null) {
return this.proxyInterface;
}
else {
return null;
}
}
public boolean isSingleton() {
return true;
}
private static class JndiObjectProxyFactory {
private static Object createJndiObjectProxy(MyJndiObjectFactoryBean jof) throws NamingException {
// Create a JndiObjectTargetSource that mirrors the JndiObjectFactoryBean's configuration.
JndiObjectTargetSource targetSource = new JndiObjectTargetSource();
targetSource.setJndiTemplate(jof.getJndiTemplate());
targetSource.setJndiName(jof.getJndiName());
targetSource.setExpectedType(jof.getExpectedType());
targetSource.setResourceRef(jof.isResourceRef());
targetSource.setLookupOnStartup(jof.lookupOnStartup);
targetSource.setCache(jof.cache);
targetSource.afterPropertiesSet();
// Create a proxy with JndiObjectFactoryBean's proxy interface and the JndiObjectTargetSource.
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.addInterface(jof.proxyInterface);
proxyFactory.setTargetSource(targetSource);
return proxyFactory.getProxy();
}
}
public static Object getPropertyValue(Object obj, String propertyName) throws IllegalAccessException {
Class<?> Clazz = obj.getClass();
Field field;
if ((field = getField(Clazz, propertyName)) == null)
return null;
field.setAccessible(true);
return field.get(obj);
}
public static Field getField(Class<?> clazz, String propertyName) {
if (clazz == null)
return null;
try {
return clazz.getDeclaredField(propertyName);
} catch (NoSuchFieldException e) {
return getField(clazz.getSuperclass(), propertyName);
}
}
public static void setPropertyValue(Object obj, String propertyName,String value) throws IllegalAccessException {
Class<?> Clazz = obj.getClass();
Field field;
if ((field = getField(Clazz, propertyName)) == null){
System.out.println(propertyName+"不存在!!!");
}
field.setAccessible(true);
field.set(obj,value);
}
}