views:

65

answers:

3

When I run following code through Main method, it works fine but when i try to execute it on click of swing button, it hangs.

Please help

import java.util.Hashtable;

import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

public class SimpleLdapAuthentication {
    public static void main(String[] args) {
        String username = "user";
        String password = "password";
        String base = "ou=People,dc=objects,dc=com,dc=au";
        String dn = "uid=" + username + "," + base;
        String ldapURL = "ldap://ldap.example.com:389";

        // Setup environment for authenticating

        Hashtable<String, String> environment = new Hashtable<String, String>();
        environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        environment.put(Context.PROVIDER_URL, ldapURL);
        environment.put(Context.SECURITY_AUTHENTICATION, "simple");
        environment.put(Context.SECURITY_PRINCIPAL, dn);
        environment.put(Context.SECURITY_CREDENTIALS, password);

        try {
            DirContext authContext =
            new InitialDirContext(environment);

            // user is authenticated
        } catch (AuthenticationException ex) {

            // Authentication failed

        } catch (NamingException ex) {
            ex.printStackTrace();
        }
    }
}
A: 

Does it really hang, or just take a long time to come back ?

It's not a good idea to do lots of processing in a Swing event handler, since Swing needs to be responsive to the user. You should delegate long-running actions to another thread.

Brian Agnew
I am already using a thread to perform ldap operations. Do you have any other idea?
rajesh
No. I think you should publish the code that doesn't work, rather than the code that does
Brian Agnew
Here is the code. I have put the above main method code in getT() method in SimpleLdapAuthentication class. private void jButton1MouseClicked(java.awt.event.MouseEvent evt) { // TODO add your handling code here: SimpleLdapAuthentication sla=new SimpleLdapAuthentication(); sla.getT();}
rajesh
Rajes, where is the other thread spawned? If I understand correctly, this is Matis generated code. And the jButton1MouseClicked are called from AWT.
Rastislav Komara
@rajesh - you're just executing this in the event handler, aren't you ?
Brian Agnew
correct this is 1 of the codes that I tried... and this is an AWT event listener. But tell what's the problem in executing it this way. If LDAPConnection works with main method why not it work with event listener ? I have seen similar posts on many forum and unable to get the resolution
rajesh
A: 

correct this is 1 of the codes that I tried... and this is an AWT event listener. But tell what's the problem in executing it this way. If LDAPConnection works with main method why not it work with event listener ? I have seen similar posts on many forum and unable to get the resolution

rajesh
A: 

Use an ActionListener instead of MouseListener

 btnYourLdapButton.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent ev) {
   doLdapRequest(ev);
  }
 });
Scrutator