JNDI utilities : Context « JNDI LDAP « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » JNDI LDAP » Context 




JNDI utilities
 
/*
  Milyn - Copyright (C) 2006

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License (version 2.1) as published by the Free Software
  Foundation.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  See the GNU Lesser General Public License for more details:
  http://www.gnu.org/licenses/lgpl.txt
*/


import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.InitialContext;
import java.util.Properties;
import java.io.InputStream;

/**
 * JNDI utilities.
 *
 @author <a href="mailto:[email protected]">[email protected]</a>
 */
public class JNDIUtil {


    /**
     * Private default constructor.
     */
    private JNDIUtil() {
    }

    /**
     * Get the JNDI Context.
     * <p/>
     * Don't forget to close it when done!
     *
     @param jndiProperties JNDI properties.
     @return The context.
     @throws javax.naming.NamingException Error getting context.
     */
    public static Context getNamingContext(final Properties jndiPropertiesthrows NamingException {
        Context context;
        try {
            context = jndiProperties.isEmpty() new InitialContext() new InitialContext(jndiProperties);
        }
        catch (NamingException e) {
            System.out.println("NamingException while try to create initialContext. jndiProperties are " + jndiProperties + e);
            throw ((NamingExceptionnew NamingException("Failed to load InitialContext: " + jndiProperties).initCause(e));
        }
        if (context == null) {
            throw new NamingException("Failed to create JNDI context.  Check that '" + Context.PROVIDER_URL + "', '" + Context.INITIAL_CONTEXT_FACTORY + "', '" + Context.URL_PKG_PREFIXES + "' are correctly configured in the supplied JNDI properties.");
        }

        return context;
    }

    /**
     * Lookup an object through the JNDI context.
     *
     @param objectName     The name of the object to be looked up.
     @param jndiProperties JNDI properties.
     @return The object.
     @throws NamingException Error getting object.
     */
    public static Object lookup(final String objectName, final Properties jndiPropertiesthrows NamingException {
        Object object = null;
        Context context;

        context = JNDIUtil.getNamingContext(jndiProperties);
        try {
            object = context.lookup(objectName);
        }
        finally {
            try {
                context.close();
            }
            catch (NamingException ne) {
                System.out.println("Failed to close Naming Context."+ ne);
            }
        }

        return object;
    }

    /**
     * Lookup an object through the JNDI context.
     *
     @param objectName     The name of the object to be looked up.
     @param jndiProperties JNDI properties.
     @param classLoaders   The {@link ClassLoader ClassLoaders) to be used during the lookup.
     @return The object.
     @throws NamingException Error getting object.
     */
    public static Object lookup(final String objectName, final Properties jndiProperties, final ClassLoader[] classLoadersthrows NamingException {
        ClassLoader tcClassLoader = Thread.currentThread().getContextClassLoader();

        try {
            for (ClassLoader classLoader : classLoaders) {
                Thread.currentThread().setContextClassLoader(classLoader);
                try {
                    return JNDIUtil.lookup(objectName, jndiProperties);
                }
                catch (NamingException e) {
                    System.out.println("NamingException while trying to lookup '" + objectName + "' using JNDI Properties '" + jndiProperties + "', classloader used '" + classLoader + "'" + e);
                    // Try the other ClassLoaders...
                }
            }
        }
        finally {
            Thread.currentThread().setContextClassLoader(tcClassLoader);
        }

        throw new NamingException("JNDI lookup of Object [" + objectName + "] failed.");
    }

    public static Properties getDefaultProperties() {
        Properties defaultProperties = new Properties();
        try {
            InitialContext context = new InitialContext();
            defaultProperties.putAll(context.getEnvironment());
        catch (Exception e) {
            System.out.println("Unexpected exception when trying to retrieve default naming context." + e);
        }
        return defaultProperties;
    }
}

   
  














Related examples in the same category
1.how to list the name and class of objects in a context
2.how to create a new subcontext called "ou=NewOu" with some attributes
3.how to destroy a subcontext called "ou=NewOu"
4.Listing a Context in the Naming Service
5.Creating and Destroying a Subcontext in the Naming Service
6.Deleting an entry
7.rebind Context
8.tearDown Context Recursively
9.Create a context path recursively.
10.A JNDI context wrapper implementation that delegates read-only methods to its delegate Context, and throws OperationNotSupportedException for any method with a side-effect
11.A static utility class for common JNDI operations
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.