13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

490Chapter 21Introducti<strong>on</strong> to Enterprise JavaBeansSo what is going <strong>on</strong> here? This c<strong>on</strong>structor is a bit unusual, isn’t it? TheInitialDirC<strong>on</strong>text is a “c<strong>on</strong>text factory.” It takes an “envir<strong>on</strong>ment,” whichis a Hashtable, that provides the informati<strong>on</strong> needed to make the c<strong>on</strong>text.And what is that informati<strong>on</strong>? At a minimum, the c<strong>on</strong>stant value associatedwith C<strong>on</strong>text.INITIAL_CONTEXT_FACTORY must be associated with the classname of the real c<strong>on</strong>text factory for the directory system—in this case,com.sun.jndi.dns.DnsC<strong>on</strong>textFactory. If you are from a C/C++background, think of this as a functi<strong>on</strong> pointer. 8We now have an initial directory c<strong>on</strong>text, which we can use to search.Going from the initial c<strong>on</strong>text to a DNS entry. Let’s now c<strong>on</strong>sider a usecase for this little program. The program begins at main(), line 21. We createan instance of our class and an instance of NamingEnumerati<strong>on</strong> (which wewill discuss in a moment). We do some very lazy error handling by enclosingthe entire process in a simple try/catch block 9 and treating all excepti<strong>on</strong>s thesame. The first thing we do is c<strong>on</strong>struct an instance of our class, passing in thefirst command-line argument 10 as the domain name to use for setting the initialc<strong>on</strong>text.Next, we get an enumerati<strong>on</strong> of all the names in that c<strong>on</strong>text. This is d<strong>on</strong>ethrough a method in our class that simply wraps the actual JNDI call that obtainsthis enumerati<strong>on</strong>. The real event is <strong>on</strong> line 18. The list() method ofthe directory c<strong>on</strong>text returns a NamingEnumerati<strong>on</strong>, which is an extensi<strong>on</strong> ofthe classic Enumerati<strong>on</strong> Java class. With it, you can iterate over the c<strong>on</strong>tentsof the c<strong>on</strong>text—which we do in lines 29–33. We rely <strong>on</strong> our old Objectmethod, toString(), to make these names readable for us. Of course, each8. Of course, it is not. What really happens here is that the code in InitialDirC<strong>on</strong>textuses the Class class to load the specified class by name. All JNDI c<strong>on</strong>text factory classes implementthe C<strong>on</strong>text interface, so InitialDirC<strong>on</strong>text uses the ability of Class to load theclass by its name as an instance of C<strong>on</strong>text.9. JNDI has a particularly rich collecti<strong>on</strong> of excepti<strong>on</strong>s. When a naming or directory operati<strong>on</strong>fails, it is usually possible to determine exactly how and why it failed from the type ofExcepti<strong>on</strong> thrown. All JNDI Excepti<strong>on</strong>s extend NamingExcepti<strong>on</strong>, so they also make itquite easy to handle them in a lazy manner. In a real producti<strong>on</strong> applicati<strong>on</strong>, you should atleast make some effort to differentiate between failures where the network is not working andfailures where the network is working fine, but the named resource does not exist. Believe us,if you have to support your applicati<strong>on</strong> in producti<strong>on</strong> you will care about the difference.10. Again, very bad producti<strong>on</strong> coding. Note that no attempt is made to check the number ofarguments passed or their c<strong>on</strong>tents.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!