10 Replies Latest reply on Feb 14, 2013 12:09 AM by abettich

    Connect to JMX tools from java

    crestenst

      Hello everyone, hope you’re all having a wonderful day,

       

      I’m currently trying to connect to the JMX console’s LDAP functionality through a custom login module. I’m currently trying to use the code located on this web page:

       

      http://dev.day.com/content/kb/home/Crx/CrxFAQ/how-to-synchronize-user-with-ldap.html

       

      The problem I am running into is this:

       

      I run the code, and I am greeted with a null pointer error originating from the line: MBeanServerConnection server = (MBeanServerConnection) JMXConnectorFactory.connect(new JMXServiceURL(serverUrl), attributes).getMBeanServerConnection();

       

      What I would like to know is this:

      What are the possible causes? I believe that the problem stems from the fact that I cannot connect to the JMX console for some unknown reason, but I could be wrong. I’ve had LDAP working for a while now, so I know that I am able to successfully connect to the server.

       

      Additionally: How do I close my connection to the LDAP server after I have finished? I’ve pasted the code below so you guys can try and figure what the problem is. Thanks!

       

                  public static void syncUserToCrx(String userName){

                              String userid = "admin";

                              String password = "admin";

                              String serverUrl = "service:jmx:rmi:///jndi/rmi://localhost:9000/jmxrmi";

                              String OBJECT_NAME = "com.adobe.granite.ldap:host=(host),port=389,type=Tools";

                              String[] buffer = new String[] { userid, password };

                              Hashtable<String, String[]> attributes = new Hashtable<String, String[]>();

                              attributes.put("jmx.remote.credentials", (String[]) buffer);       

                              MBeanServerConnection server;

                              ObjectName name;

                              try {

                                          server = (MBeanServerConnection) JMXConnectorFactory.connect(new JMXServiceURL(serverUrl), attributes).getMBeanServerConnection();

                                          name = new ObjectName(OBJECT_NAME);

                                          LDAPUserMBean ldap = (LDAPUserMBean) MBeanServerInvocationHandler.newProxyInstance(server, name, LDAPUserMBean.class,false);

                                          ldap.syncUser(userName);

                                         //This is my trying to close the connection

                                          server.unregisterMBean(name);

                              } catch (MalformedObjectNameException e) {

       

                                          LOG.error("Line 79: "+e.getMessage());

                                          e.printStackTrace();

                              } catch (NullPointerException e) {

       

                                          LOG.error("Line 83: "+e.getMessage());

                                          e.printStackTrace();

                              }

                              catch (MalformedURLException e) {

                                          LOG.error("Line 88: "+e.getMessage());

                                          e.printStackTrace();

                              } catch (IOException e) {

                                          LOG.error("Line 90: "+e.getMessage());

                                          e.printStackTrace();

                              } catch (InstanceNotFoundException e) {

                                          LOG.error("Line 94: "+e.getMessage());

                                          e.printStackTrace();

                              } catch (MBeanRegistrationException e) {

                                          LOG.error("Line 97: "+e.getMessage());

                                          e.printStackTrace();

                              }

       

       

                  }

        • 1. Re: Connect to JMX tools from java
          justin_at_adobe Adobe Employee

          The most likely cause of this is that you haven't actually enabled remote JMX access. To use remote JMX access, you need to pass several system properties:

          -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=9000

           

          See http://docs.oracle.com/javase/1.5.0/docs/guide/management/agent.html#properties

           

          Before using the code above, I'd suggest ensuring that you can connect to the URL using jconsole.

           

          Regards,

          Justin

          • 2. Re: Connect to JMX tools from java
            Sham HC Level 7

            Along with justin suggestion please ensure

             

            *  First of all you must have CQ5.5 SP1 installed for the ldap bean to work.

            *  Next, you will need to set up LDAP before the MBean will even show up in the /system/console/jmx web ui

            *  Go to http://host:port/system/console/jmx and ensure the LDAP MBean exist

            • 3. Re: Connect to JMX tools from java
              crestenst Level 1

              Thanks for the reply. I'm able to connect to the instance through jconsole after putting those parameters into my start script, but I am facing the same issues as before.

               

              I get to the point where it is creating the connection object, and a Null Pointer exception is thrown.

               

              I definetely have the mBean currently, and it is working. I'm able to sync users through the JMX console. I can even pull the MBean I want up on jConsole!

              But, the string I am passing the MBean object is still not giving me the results I want.

              • 4. Re: Connect to JMX tools from java
                justin_at_adobe Adobe Employee

                To be clear, in JConsole you use the URL service:jmx:rmi:///jndi/rmi://localhost:9000/jmxrmi ?

                • 5. Re: Connect to JMX tools from java
                  crestenst Level 1

                  Yep, into the "remote process" text box, i paste the string. And it allows me to connect.

                  • 6. Re: Connect to JMX tools from java
                    justin_at_adobe Adobe Employee

                    Hmmm... I can't reproduce that. If I can connect using JConsole, the same URL consistenly works in code. One thing which you might want to look at is to break down that line of code in order to identify exactly which bit of code is producing the NPE. There's two or three opportunities there.

                    • 7. Re: Connect to JMX tools from java
                      crestenst Level 1

                      Sounds like a plan. Thanks for your help!

                      • 8. Re: Connect to JMX tools from java
                        gvad000

                        Hi,

                         

                        im having the same problem.im creating the job which will retrive the users from ladp and insert into CRX. i could run the program through eclipse IDE as java file but when i run the same creating bundle as job, im not able to connect to JMX, getting null pointer exception on MBeanServerConnction.

                         

                        below is my code.... this is working fine in IDE but activator.java file getting null pointer error.

                         

                        Could you please help me?

                         

                        Executing UserSyncJob : Fri Oct 26 09:28:00 IST 2012

                        java.lang.NullPointerException

                                at javax.management.remote.rmi.RMIConnector.getConnection(Unknown Source)

                                at javax.management.remote.rmi.RMIConnector.connect(Unknown Source)

                                at javax.management.remote.JMXConnectorFactory.connect(Unknown Source)

                         

                        String userid = "admin";

                                String password = "admin";

                                String serverUrl = "service:jmx:rmi:///jndi/rmi://:9999/jmxrmi";

                                String OBJECT_NAME = "com.adobe.granite.ldap:host={host},port=9389,type=Tools";

                                String[] buffer = new String[] { userid, password };

                                Hashtable<String, String[]> attributes = new Hashtable<String, String[]>();

                                attributes.put("jmx.remote.credentials", (String[]) buffer);

                                try{

                                     MBeanServerConnection server = (MBeanServerConnection) JMXConnectorFactory.connect(new JMXServiceURL(serverUrl), null).getMBeanServerConnection();

                                     System.out.println("Server Domain"+server.getMBeanCount());

                                     /*ObjectName name = new ObjectName(OBJECT_NAME);

                                     LDAPUserMBean ldap = (LDAPUserMBean) MBeanServerInvocationHandler.newProxyInstance(server, name, LDAPUserMBean.class,false);

                                     String[] users={

                        user id from ldap

                        };

                                     for(String user : ldap.syncUserList(users)) {

                                         System.out.println(user);

                                     }*/

                                    // ldap.syncUser(users);

                                }catch (Exception e) {

                                    e.printStackTrace();

                                }

                            }

                             public static interface LDAPUserMBean extends DynamicMBean {

                                    public String[] listOrphanedUsers();

                                    public String[] syncUsers(String[] users);

                                    public String[] syncUserList(String[] users);

                                    public void syncAllUsers();

                                    //public void syncUser(String user);

                                    public void syncOrphanedUsers();

                                    public void purgeOrphanedUsers();

                                }

                        • 9. Re: Connect to JMX tools from java
                          rakesh2k78

                          Hi,

                           

                          There is no need to look through remote way. Below is the way to look for remote MBeans.

                          MBeanServerConnection server = (MBeanServerConnection) JMXConnectorFactory.connect(new JMXServiceURL(serverUrl), null).getMBeanServerConnection();

                           

                          // Below code will suffice your need. This code is working for me in CQ


                          String OBJECT_NAME = "com.adobe.granite.ldap:host=<your LDAP host>,port=<Port>,type=Tools";

                          MBeanServerConnection server = ManagementFactory.getPlatformMBeanServer(); //returns a reference to the existing MBean server within the JVM.

                          ObjectName name = new ObjectName(OBJECT_NAME);

                          LDAPUserMBean ldap = JMX.newMBeanProxy(server, name, LDAPUserMBean.class);

                          ldap.syncUser("userDN");

                           

                          Thanks,

                          Rakesh

                          • 10. Re: Connect to JMX tools from java
                            abettich

                            Hi,

                             

                            It seems that I had the same problem some time ago.

                            http://stackoverflow.com/questions/13628759/why-does-adobe-cq-5-5-remote-jmx-from-a-servle t-fail

                            I didn't get an answer from stackoverflow neither from the google group.

                            Also the response from a Daycare ticket wasn't very helpfull.

                             

                            I understand, but this is not a product feature, it's your own code, so we cannot support on your on custom code which is not a product existing feature as we don't have any documentation or any expertise on that specific topic as a base for verifying that this is supposed to work.