2012-04-28 107 views
3

我試圖以簡單的獨立Java客戶端(一個類 - >兩種方法:createWeblogicUser()& main())以編程方式在WebLogic(10.3.4)中創建用戶。從獨立的Java客戶端以編程方式創建WebLogic用戶

public void createWeblogicUser() { 
try { 
    Hashtable<String, String> env = new Hashtable<String, String>(); 
    env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); 
    env.put(Context.SECURITY_PRINCIPAL, "weblogic"); 
    env.put(Context.SECURITY_CREDENTIALS, "weblogic"); 
    env.put(Context.PROVIDER_URL, "t3://myserver:7001"); 

    InitialContext ctx = new InitialContext(env); 
    MBeanServer wls = (MBeanServer) ctx.lookup("java:comp/env/jmx/runtime"); 

    ObjectName userEditor = null; 
    ObjectName mBeanTypeService = new ObjectName("com.bea:Name=MBeanTypeService, Type=weblogic.management.mbeanservers.MBeanTypeService"); 
    ObjectName rs = new ObjectName("com.bea:Name=RuntimeService, Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean"); 
    ObjectName domainMBean = (ObjectName) wls.getAttribute(rs, "DomainConfiguration"); 
    ObjectName securityConfig = (ObjectName) wls.getAttribute(domainMBean, "SecurityConfiguration"); 
    ObjectName defaultRealm = (ObjectName) wls.getAttribute(securityConfig, "DefaultRealm"); 
    ObjectName[] authProviders = (ObjectName[]) wls.getAttribute(defaultRealm, "AuthenticationProviders"); 

    for(ObjectName providerName : authProviders) { 
     if(userEditor == null) { 
      ModelMBeanInfo info = (ModelMBeanInfo) wls.getMBeanInfo(providerName); 
      String className = (String) info.getMBeanDescriptor().getFieldValue("interfaceClassName"); 
      if(className != null) { 
       String[] mba = (String[]) wls.invoke(mBeanTypeService 
                , "getSubtypes" 
                , new Object[] {"weblogic.management.security.authentication.UserEditorMBean"} 
                , new String[] {"java.lang.String"} 
              ); 
       for(String mb : mba) { 
        if(className.equals(mb)) 
         userEditor = providerName; 
       } 
      } 
     } 

     if(userEditor == null) 
      throw new RuntimeException("Could not retrieve user editor"); 

     try { 
      wls.invoke(userEditor 
         , "createUser" 
         , new Object[] {"wls_user", "password123","User created programmatically."} 
         , new String[] {"java.lang.String", "java.lang.String","java.lang.String"} 
      ); 
     } 
     catch(Exception e){ 
      e.printStackTrace(); 
     } 

     ctx.close(); 
    } 
} 
catch(Exception ex) { 
    ex.printStackTrace(); 
} 

}

上下文查找我應該怎樣做任何想法? 「java:comp」會拋出一個javax.naming.NameNotFoundException;看起來像我只能從容器中使用它。

回答

2

得到它的工作。

private void createWeblogicUser(String username) { 
try { 
    Hashtable<String, String> env = new Hashtable<String, String>(); 
    env.put(Context.SECURITY_PRINCIPAL, "weblogic"); 
    env.put(Context.SECURITY_CREDENTIALS, "weblogic"); 

    String hostname = "myserver"; 
    int port = 7001; 
    String protocol = "rmi"; 
    String url= new String("/jndi/iiop://myserver:7001/weblogic.management.mbeanservers.domainruntime"); 

    JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port, url); 
    JMXConnector connector = JMXConnectorFactory.connect(serviceURL, env); 
    MBeanServerConnection connection = connector.getMBeanServerConnection(); 

    ObjectName userEditor = null; 
    ObjectName mBeanTypeService = new ObjectName("com.bea:Name=MBeanTypeService,Type=weblogic.management.mbeanservers.MBeanTypeService"); 
    ObjectName rs = new ObjectName("com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean"); 
    ObjectName domainMBean = (ObjectName) connection.getAttribute(rs, "DomainConfiguration"); 
    ObjectName securityConfig = (ObjectName) connection.getAttribute(domainMBean, "SecurityConfiguration"); 
    ObjectName defaultRealm = (ObjectName) connection.getAttribute(securityConfig, "DefaultRealm"); 
    ObjectName[] authProviders = (ObjectName[]) connection.getAttribute(defaultRealm, "AuthenticationProviders"); 

    for(ObjectName providerName : authProviders) { 
     System.out.println("Auth provider is: " + providerName) ; 

     if(userEditor == null) { 
      ModelMBeanInfo info = (ModelMBeanInfo) connection.getMBeanInfo(providerName); 
      String className = (String) info.getMBeanDescriptor().getFieldValue("interfaceClassName"); 
      System.out.println("className is: " + className) ; 

      if(className != null) { 
       String[] mba = (String[]) connection.invoke(mBeanTypeService 
             , "getSubtypes" 
             , new Object[] {"weblogic.management.security.authentication.UserEditorMBean"} 
             , new String[] {"java.lang.String"} 
            ); 
       for(String mb : mba) { 
        System.out.println("Model Bean is: " + mb) ; 
        if(className.equals(mb)) { 
         System.out.println("Found a macth for the model bean and class name!") ; 
         userEditor = providerName; 
        } 
       } 
      } 
     } 
    } 

    if(userEditor == null) 
     throw new RuntimeException("Could not retrieve user editor"); 

    try { 
     connection.invoke(userEditor 
        , "createUser" 
        , new Object[] {username, "password123","User created programmatically."} 
        , new String[] {"java.lang.String", "java.lang.String","java.lang.String"} 
     ); 

     System.out.println("User created successfully") ; 
    } 
    catch(Exception e){ 
     e.printStackTrace(); 
    } 

    connector.close(); 

} 
catch(Exception ex) { 
    ex.printStacktrace(); 
} 

}

你只需要在類路徑的weblogic.jar和wljmxclient.jar。我在JDK 1.6.0_29上運行了這個。我必須補充一點,我在安裝了WebLogic的機器上運行這個。所以classpath條目是jar文件的完全限定路徑名。

我遇到了一個「疑難雜症」: 在提供「com.bea:Name = XXXX,Type = XXXX」的同時,不給冒號之間留出空格 - 不是逗號;什麼都沒有 - 我花了一些時間調試它,然後才終於找到它。

+0

很棒的工作!感謝分享這個結果。 – kvatashydze 2016-04-18 18:23:50

相關問題