JNDI Hello World..

..not much to say really. Just another Hello World!

So first I downloaded fscontext.jar file from here, This jar is required because it contains the class: com.sun.jndi.fscontext.RefFSContextFactory.

And here is my code:
import javax.naming.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Hashtable;
import java.util.Scanner;
 
/**
 * User: Koray Tugay (koray@tugay.biz)
 * Date: 7/30/2016
 * Time: 9:49 AM
 */
@SuppressWarnings("unchecked")
public class JNDIHello {
 
    final static String URL =
            "file:///Users/Koray Tugay/Development/studentform/src/main/java/biz/tugay";
 
    public static void main(String[] args) throws NamingException, FileNotFoundException {
        final Hashtable hashtableEnvironment = new Hashtable();
        hashtableEnvironment.put(Context.INITIAL_CONTEXT_FACTORY,
                "com.sun.jndi.fscontext.RefFSContextFactory");
        hashtableEnvironment.put(Context.PROVIDER_URL, URL);
        final InitialContext context = new InitialContext(hashtableEnvironment);
        final NamingEnumeration namingenumeration = context.listBindings("");
        System.out.println("============================");
        System.out.println("Printing all bindings.");
        System.out.println("============================");
        while (namingenumeration.hasMore()) {
            final Binding binding = (Binding) namingenumeration.next();
            System.out.println("Binding Name: " + binding.getName()
                    + " Binding object: " + binding.getObject());
        }
        System.out.println("============================");
        System.out.println("Finished printing all bindings.");
        System.out.println("============================");
 
        System.out.println("");
        System.out.println("");
        System.out.println("");
 
        final File file = (File) context.lookup("TestClass.java");
 
        System.out.println("============================");
        System.out.println("Printing 'TestClass.java'");
        System.out.println("============================");
 
        final Scanner fileScanner = new Scanner(file, "utf-8");
        while (fileScanner.hasNext()) {
            System.out.println(fileScanner.nextLine());
        }
    }
 
}

and the output will be:
============================
Printing all bindings.
============================
Binding Name: backingbeans Binding object: com.sun.jndi.fscontext.RefFSContext@7d8aecf1
Binding Name: db Binding object: com.sun.jndi.fscontext.RefFSContext@2bca029b
Binding Name: filter Binding object: com.sun.jndi.fscontext.RefFSContext@5b712492
Binding Name: TestClass.java Binding object: C:\Users\Koray Tugay\Development\studentform\src\main\java\biz\tugay\TestClass.java
============================
Finished printing all bindings.
============================
 
 
 
============================
Printing 'TestClass.java'
============================
package biz.tugay;
 
import org.apache.commons.dbcp2.BasicDataSource;
 
import java.sql.*;
 
/**
 * User: Koray Tugay (koray@tugay.biz)
 * Date: 7/29/2016
 * Time: 10:28 PM
 */
public class TestClass {
 
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
 
        final BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setDriverClassName(DRIVER_CLASS_NAME);
        basicDataSource.setUrl(URL);
        basicDataSource.setUsername(USERNAME);
        basicDataSource.setPassword(PASSWORD);
 
        Connection sa = basicDataSource.getConnection();
 
        final Statement statement = sa.createStatement();
        final ResultSet resultSet = statement.executeQuery("SELECT * FROM STUDENT");
        while (resultSet.next()) {
            System.out.println(resultSet.getString("FULLNAME"));
        }
        sa.close();
    }
 
    final static String DRIVER_CLASS_NAME = "org.h2.Driver";
    final static String URL = "jdbc:h2:tcp://localhost:9092/~/h2dbs/studentform";
    final static String USERNAME = "sa";
    final static String PASSWORD = "";
}

Ok, what is so interesting about this? Note, how we did not require to create a File object. The RefFSContextFactory took care of it, for us!