Interview Questions

ServiceLocator - To Lookup Datasource, EJB Beans

This code sample consist of ServiceLocator class which is a singleton class used for looking up DataSource, EJBLocalHome and EJBRemoteHome objects from JNDI context. This class can be used as a single point of contact to look up EJBHome, DataSource in enterprise applications. It also cache's those objects which are accessed for the first time, to prevent further lookup's in future.

ServiceLocator.java

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */


import java.sql.Connection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import javax.ejb.EJBHome;
import javax.ejb.EJBLocalHome;
import javax.rmi.PortableRemoteObject;

public class ServiceLocator
{
        private Context                                 ic;
        private Map<String, DataSource> cache;
        private static final String             SERVER_HOST     = " ";

        private ServiceLocator()
        {
                try
                {
                        ic = getInitialContext();
                }
                catch (NamingException e)
                {
                        e.printStackTrace();
                }
                cache = Collections.synchronizedMap(new HashMap<String, DataSource>());
        }

        public static ServiceLocator getInstacne()
        {
                return new ServiceLocator();
        }

        private Context getInitialContext() throws NamingException
        {
                try
                {
                        Hashtable<String, String> h = new Hashtable<String, String>();
                        h.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
                        h.put(Context.PROVIDER_URL, SERVER_HOST);
                        return new InitialContext(h);
                }
                catch (NamingException ne)
                {
                        ne.printStackTrace();
                        throw ne;
                }
        }

        public java.sql.Connection getConnection(String dataSourceName) throws Exception
        {
                DataSource dataSource = null;
                Connection con = null;
                try
                {
                        if (cache.containsKey(dataSourceName))
                        {
                                dataSource = (DataSource) cache.get(dataSourceName);
                                con = dataSource.getConnection();
                        }
                        else
                        {
                                dataSource = (DataSource) ic.lookup(dataSourceName);
                                cache.put(dataSourceName, dataSource);
                                con = dataSource.getConnection();
                        }
                }
                catch (NamingException ne)
                {
                        new Exception("[Unable to get DataSource]", ne);
                }
                catch (Exception e)
                {
                        new Exception("[Unable to get connection from DataSource]", e);
                }
                return con;
        }

        /**
         * Get the ejb Local home factory. If this ejb home factory has already been clients need to cast to the type of
         * EJBHome they desire
         */

        public EJBLocalHome getLocalHome(String jndiHomeName) throws Exception
        {
                EJBLocalHome home = null;
                try
                {
                        if (cache.containsKey(jndiHomeName))
                        {
                                home = (EJBLocalHome) cache.get(jndiHomeName);
                        }
                        else
                        {
                                home = (EJBLocalHome) ic.lookup(jndiHomeName);
                                cache.put(jndiHomeName, home);
                        }
                }
                catch (NamingException ne)
                {

                        throw new Exception("Naming error, Unable to lookup the local bean");
                }
                catch (Exception e)
                {

                        throw new Exception("Unknow error, Unable to lookup the local bean");
                }
                return home;
        }

        /**
         * Get the ejb Remote home factory.
         */

        public EJBHome getRemoteHome(String jndiHomeName, Class className) throws Exception
        {
                EJBHome home = null;
                try
                {
                        if (cache.containsKey(jndiHomeName))
                        {
                                home = (EJBHome) cache.get(jndiHomeName);
                        }
                        else
                        {
                                home = (EJBHome) PortableRemoteObject.narrow(ic.lookup(jndiHomeName), className);
                                cache.put(jndiHomeName, home);
                        }
                }
                catch (NamingException ne)
                {
                        throw new Exception("Naming error, Unable to lookup the remote bean");
                }
                catch (Exception e)
                {
                        throw new Exception("Unknow error, Unable to lookup the remote bean");
                }

                return home;
        }
}