-
Exception Handling
Greetings,
Im working on a small project to connect to an ldap server and fetch the attributes of a given dn. Now I have been able to make it work with no user input as its not that difficult and the entire bit of code was in a try/catch. But now i want to add in a basic menu and capture some user input.
Logically (to me) the program can be broken into a few pieces. The first bit i want to handle exceptions for is the initial construction of the DirContext object.. like so:
Code:
try {
DirContext dirCtx = new InitialDirContext(environment);
System.out.printf("Connected to: %s\n", connectionString);
} catch (NamingException e) {
System.out.printf("Connection Error: %s\n", e.toString());
}
now if i do it this way, when i try to use dirCtx later in the program NetBeans tells me "variable dirCtx might not have been initialized", and there are compile errors
i have also tried something like this:
Code:
DirContext dirCtx;
try {
dirCtx = new InitialDirContext(environment);
System.out.printf("Connected to: %s\n", connectionString);
} catch (NamingException e) {
System.out.printf("Connection Error: %s\n", e.toString());
}
NetBeans tells me the same error, and i get compile errors again.
I'm very new to java, but have extensive experience with php, and even a little c# experience. I know this is probably a very simple problem that im just not seeing the answer to, but i am unable to locate the answer in the java book i have, also ive read the java tutorial on sun's site and still not sure why the way im doing it is wrong.
i guess im looking for a quick explanation why the way im doing it is wrong, and what the proper way to do it is.
full code:
Code:
import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;
/**
*
* @author Jerry
*/
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String connectionString = "ldap://192.168.137.8:389/CN=Portal,O=Jenzabar,C=US";
String userFQDN = "CN=173,OU=PortalUsers,CN=Portal,O=Jenzabar,C=US";
String userPassword = "password";
Hashtable environment = new Hashtable();
//DirContext dirCtx;
try {
environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
environment.put(Context.PROVIDER_URL, connectionString);
environment.put(Context.SECURITY_PRINCIPAL,userFQDN);
environment.put(Context.SECURITY_CREDENTIALS,userPassword);
DirContext dirCtx = new InitialDirContext(environment);
System.out.printf("Connected to: %s\n", connectionString);
} catch(NamingException e) {
System.out.printf("Connection Error: %s\n", e.toString());
}
System.out.println("What would you like to do? (enter a number)");
System.out.println("1 - Print Attributes of DN");
String command = input.next();
if(command.equals("1")) {
System.out.print("Enter DN:");
String dn = input.next();
printAttributes(dirCtx, dn);
}
}
public static void printAttributes(DirContext dirCtx, String dn) {
try {
Attributes userAttributes = dirCtx.getAttributes(dn);
int i = 0;
for(NamingEnumeration allAttribs = userAttributes.getAll(); allAttribs.hasMore();) {
Attribute thisAttrib = (Attribute)allAttribs.next();
System.out.printf("%s: ", thisAttrib.getID());
for(NamingEnumeration value = thisAttrib.getAll(); value.hasMore();) {
if(i == 0) {
System.out.printf("%s", value.next());
} else {
System.out.printf(", %s", value.next());
}
i++;
}
System.out.println();
i = 0;
}
} catch (NamingException e) {
System.out.printf("Error Fetching Attributes for '%s': %s\n", dn, e.toString());
}
}
}
Thanks!
-
Quote:
DirContext dirCtx;
This defines the variable but does NOT assign it any value. Because you need to define the variable outside the scope of the try{} block, you can do this to give it a value:
DirContext dirCtx = null;
-
norm,
i was hoping it was something easy like that!
Thank you so much!
-
Code:
DirContext dirCtx = null; // I filled this in based on Norms post.
try {
// Get the dirCtxt.
} catch(NamingException e) {
System.out.printf("Connection Error: %s\n", e.toString());
}
System.out.println("What would you like to do? (enter a number)");
System.out.println("1 - Print Attributes of DN");
String command = input.next();
if(command.equals("1")) {
System.out.print("Enter DN:");
String dn = input.next();
printAttributes(dirCtx, dn);
}
}
You've got yourself a bug here. If the lookup for the context fails you still merrily continue on, albeit with a small message on the screen. This will result in a null pointer exception (since dirCtx is null) in printAttributes.
Since you cannot do anything in this program without a valid dirCtx this is one of those situations you want to crash. That is throw the NamingException.
-
Tolls,
Thank you for pointing that out. I should be able to change the flow of the program to take into account a null context.
Thanks!