62,635
社区成员




package weblogic;
import java.util.Properties;
import javax.management.InstanceNotFoundException;
import javax.management.InvalidAttributeValueException;
import javax.naming.Context;
import javax.naming.NamingException;
import weblogic.jndi.Environment;
import weblogic.management.MBeanHome;
import weblogic.management.configuration.JDBCConnectionPoolMBean;
/**
* 测试调用weblogic api直接修改config.xml
*
*
*/
public class ConfigOperator {
/**
* @param args
*/
public static void main(String[] args) {
//modifyAdmin("user", "pass");
modifyConfig("user", "pass");
System.out.println("test finished");
}
public static void modifyConfig(String userName, String password) {
try {
String poolName = "MyJDBC Connection Pool";
MBeanHome home = getMBeanHome();
JDBCConnectionPoolMBean mConfigBean = (JDBCConnectionPoolMBean) home
.getConfigurationMBean(poolName, "JDBCConnectionPoolConfig");
Properties nameProperty = new Properties();
nameProperty.setProperty("name", userName);
mConfigBean.setProperties(nameProperty);
mConfigBean.setPassword(password);
System.out.println(mConfigBean.getPassword());
//mConfigBean.setDriverName("helloworld1");
} catch (InvalidAttributeValueException iave) {
System.out.println("Invalid attribute while configuring tracing "
+ iave);
} catch (InstanceNotFoundException infe) {
System.out.println("Instance not found while configuring tracing "
+ infe);
} catch (Exception e) {
System.out.println(e);
}
}
public static void modifyAdmin(String userName, String password) {
try {
String poolName = "MyJDBC Connection Pool";
MBeanHome home = getMBeanHome();
JDBCConnectionPoolMBean mConfigBean = (JDBCConnectionPoolMBean) home
.getAdminMBean(poolName, "JDBCConnectionPool");
Properties nameProperty = new Properties();
nameProperty.setProperty("name", userName);
mConfigBean.setProperties(nameProperty);
mConfigBean.setPassword(password);
//mConfigBean.setDriverName("helloworld1");
} catch (InvalidAttributeValueException iave) {
System.out.println("Invalid attribute while configuring tracing "
+ iave);
} catch (InstanceNotFoundException infe) {
System.out.println("Instance not found while configuring tracing "
+ infe);
} catch (Exception e) {
System.out.println(e);
}
}
private static MBeanHome getMBeanHome() {
String url = "t3://localhost:7001";
String username = "weblogic";
String password = "weblogic";
MBeanHome home = null;
try {
Environment env = new Environment();
env.setProviderUrl(url);
env.setSecurityPrincipal(username);
env.setSecurityCredentials(password);
Context ctx = env.getInitialContext();
home = (MBeanHome) ctx.lookup(MBeanHome.LOCAL_JNDI_NAME);
} catch (NamingException ne) {
System.out.println("Error getting MBeanHome " + ne);
}
return home;
}
}