本文最后一次修改日期:2013-05-03
import javax.naming.*;
import weblogic.jndi.Environment;
import weblogic.security.providers.authentication.DefaultAuthenticatorMBean;
import weblogic.management.MBeanHome;
import weblogic.management.security.authentication.AuthenticationProviderMBean;
import weblogic.management.security.authentication.*;
public class WebLogicJMXUtils {
public WebLogicJMXUtils() {
super();
}
public static void main(String[] args) {
MBeanHome home = null;
try {
Environment env = new Environment();
env.setProviderUrl("t3://localhost:7001");
env.setSecurityPrincipal("weblogic");
env.setSecurityCredentials("welcome1");
Context ctx = env.getInitialContext();
home = (MBeanHome)ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
weblogic.management.security.RealmMBean rmBean =
home.getActiveDomain().getSecurityConfiguration().getDefaultRealm();
AuthenticationProviderMBean[] authenticationBeans = rmBean.getAuthenticationProviders();
DefaultAuthenticatorMBean defaultAuthenticationMBean = (DefaultAuthenticatorMBean)authenticationBeans[0];
UserReaderMBean userReaderMBean = (UserReaderMBean)defaultAuthenticationMBean;
GroupReaderMBean groupReaderMBean = (GroupReaderMBean)defaultAuthenticationMBean;
UserRemoverMBean userRemoverMBean = (UserRemoverMBean)defaultAuthenticationMBean;
GroupRemoverMBean groupRemoverMBean = (GroupRemoverMBean)defaultAuthenticationMBean;
// userRemoverMBean.removeUser("");
// groupRemoverMBean.removeGroup("");
String userCurName = userReaderMBean.listUsers("*", 100);
while (userReaderMBean.haveCurrent(userCurName)) {
String user = userReaderMBean.getCurrentName(userCurName);
System.out.println("\n User: " + user);
userReaderMBean.advance(userCurName);
}
String cursorName = groupReaderMBean.listGroups("*", 100);
while (groupReaderMBean.haveCurrent(cursorName)) {
String group = groupReaderMBean.getCurrentName(cursorName);
System.out.println("\n Group: " + group);
groupReaderMBean.advance(cursorName);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行程序,输出如下:
User: OracleSystemUser
User: weblogic
User: john
User: joe
User: ted
User: mary
User: albert
Group: AdminChannelUsers
Group: Administrators
Group: AppTesters
Group: CrossDomainConnectors
Group: Deployers
Group: Monitors
Group: Operators
Group: OracleSystemGroup
Group: employees
Group: managers
说明:MBeanHome已经不建议使用,官方文档的说明如下:
As of 9.0, the MBeanHome interface is deprecated.
Instead of using this API-like programming model, all JMX applications should use the standard JMX programming model, in which clients use the javax.management.MBeanServerConnection interface to discover MBeans, attributes, and attribute types at run time.
In this JMX model, clients interact indirectly with MBeans through the MBeanServerConnection interface.
As of 9.0, the MBeanHome interface is deprecated.
Instead of using this API-like programming model, all JMX applications should use the standard JMX programming model, in which clients use the javax.management.MBeanServerConnection interface to discover MBeans, attributes, and attribute types at run time.
In this JMX model, clients interact indirectly with MBeans through the MBeanServerConnection interface.
参考文献:
1. http://weblogic-wonders.com/weblogic/2010/11/10/list-users-and-groups-in-weblogic-using-jmx/
2. http://dead-knight.iteye.com/blog/853008
3. http://www.360doc.com/content/10/1005/14/39755_58581727.shtml
4. http://www.360doc.com/content/10/1005/14/39755_58582655.shtml
5. http://www.ibm.com/developerworks/cn/java/j-lo-jse63/
6. http://java.e800.com.cn/articles/2007/11/1167663893665537991_1.html
没有评论:
发表评论