Example usage for javax.naming NamingException NamingException

List of usage examples for javax.naming NamingException NamingException

Introduction

In this page you can find the example usage for javax.naming NamingException NamingException.

Prototype

public NamingException(String explanation) 

Source Link

Document

Constructs a new NamingException with an explanation.

Usage

From source file:JNDIUtil.java

/**
 * Get the JNDI Context.//from   w  w w  .j  av  a2  s.co  m
 * <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 jndiProperties) throws 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 ((NamingException) new 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;
}

From source file:org.alfresco.config.JndiObjectFactoryBean.java

@Override
protected Object lookup() throws NamingException {
    Object candidate = super.lookup();
    if (candidate instanceof DataSource) {
        Connection con = null;// ww w .j  ava  2  s  . c  o m
        try {
            con = ((DataSource) candidate).getConnection();
        } catch (Exception e) {
            NamingException e1 = new NamingException("Unable to get connection from " + getJndiName());
            e1.setRootCause(e);
            throw e1;
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
            } catch (Exception e) {
            }
        }
    }
    return candidate;
}

From source file:JNDIUtil.java

/**
 * Create a context path recursively./*from   w  w  w  .  ja va  2  s.com*/
 */
public static Context createContext(Context c, String path) throws NamingException {
    Context crtContext = c;
    for (StringTokenizer st = new StringTokenizer(path, "/"); st.hasMoreTokens();) {
        String tok = st.nextToken();

        try {
            Object o = crtContext.lookup(tok);
            if (!(o instanceof Context)) {
                throw new NamingException("Path " + path + " overwrites and already bound object");
            }
            crtContext = (Context) o;
            continue;
        } catch (NameNotFoundException e) {
            // OK
        }
        crtContext = crtContext.createSubcontext(tok);
    }
    return crtContext;
}

From source file:net.sf.ehcache.distribution.MockContextFactory.java

/**
 * @see javax.naming.spi.InitialContextFactory#getInitialContext(java.util.Hashtable)
 *///from w  w  w  .  ja  v  a2 s  . c o  m
public Context getInitialContext(Hashtable environment) throws NamingException {
    LOG.debug("getInitialContext " + environment);
    String jndiProviderUrl = (String) environment.get(Context.PROVIDER_URL);
    synchronized (JNDI_PROVIDER_URL_TO_CONTEXT_MAP) {
        if (jndiProviderUrl == null) {
            throw new NamingException("getInitialContext: " + Context.PROVIDER_URL + " is null " + environment);
        }
        Context context = null;
        context = (Context) JNDI_PROVIDER_URL_TO_CONTEXT_MAP.get(jndiProviderUrl);
        if (context == null) {
            context = new MockContext(jndiProviderUrl);
            JNDI_PROVIDER_URL_TO_CONTEXT_MAP.put(jndiProviderUrl, context);
        }
        return context;
    }
}

From source file:com.dattack.naming.standalone.StandaloneContextFactory.java

private static Context createInitialContext(final File dir, final Map<?, ?> environment,
        final CompositeConfiguration configuration) throws NamingException {

    LOGGER.debug("Scanning directory '{}' for JNDI resources.", dir);
    try {//  ww  w  .ja  va 2  s.com
        final StandaloneContext ctx = new StandaloneContext(environment);
        final NamingLoader loader = new NamingLoader();
        final Collection<File> extraClasspath = FilesystemUtils
                .locateFiles(configuration.getList(CLASSPATH_DIRECTORY_PROPERTY));
        loader.loadDirectory(dir, ctx, extraClasspath);

        LOGGER.debug("JNDI context is ready");

        return ctx;
    } catch (final IOException e) {
        throw (NamingException) new NamingException(e.getMessage()).initCause(e);
    }
}

From source file:com.jaeksoft.searchlib.util.ActiveDirectory.java

public ActiveDirectory(String username, String password, String domain) throws NamingException {
    if (StringUtils.isEmpty(domain))
        throw new NamingException("The domain is empty");
    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    properties.put(Context.PROVIDER_URL, StringUtils.fastConcat("LDAP://", domain));
    properties.put(Context.SECURITY_PRINCIPAL, StringUtils.fastConcat(username, "@", domain));
    properties.put(Context.SECURITY_CREDENTIALS, password);
    properties.put("java.naming.ldap.attributes.binary", "objectSID");
    properties.put(Context.REFERRAL, "follow");
    dirContext = new InitialDirContext(properties);
    domainSearchName = getDomainSearch(domain);
}

From source file:com.wso2telco.dbUtil.DataBaseConnectUtils.java

private static void initializeConnectDatasource() throws NamingException {
    if (mConnectDatasource != null) {
        return;//  w w  w.j  av a 2  s  . c o m
    }

    String dataSourceName = null;
    try {
        Context ctx = new InitialContext();
        ConfigurationService configurationService = new ConfigurationServiceImpl();
        dataSourceName = configurationService.getDataHolder().getMobileConnectConfig().getDataSourceName();
        mConnectDatasource = (DataSource) ctx.lookup(dataSourceName);
    } catch (NamingException e) {
        throw new NamingException("Error while looking up the data source : " + dataSourceName);
    }
}

From source file:com.enioka.jqm.tools.ResourceParser.java

private static JndiResourceDescriptor fromDatabase(String alias) throws NamingException {
    JndiObjectResource resource = null;//from w  w  w .ja v a  2 s  .c  om
    EntityManager em = null;
    try {
        // Using the horrible CriteriaBuilder API instead of a string query. This avoids classloading issues - Hibernate binds
        // the entities at run time with the thread current classloader...
        em = Helpers.getNewEm();

        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<JndiObjectResource> q = cb.createQuery(JndiObjectResource.class);
        Root<JndiObjectResource> c = q.from(JndiObjectResource.class);
        ParameterExpression<String> p = cb.parameter(String.class);
        q.select(c).where(cb.equal(c.get("name"), p));

        TypedQuery<JndiObjectResource> query = em.createQuery(q);
        query.setParameter(p, alias);
        resource = query.getSingleResult();
    } catch (Exception e) {
        NamingException ex = new NamingException("Could not find a JNDI object resource of name " + alias);
        ex.setRootCause(e);
        throw ex;
    } finally {
        if (em != null) {
            em.close();
        }
    }

    // Create the ResourceDescriptor from the JPA object
    JndiResourceDescriptor d = new JndiResourceDescriptor(resource.getType(), resource.getDescription(), null,
            resource.getAuth(), resource.getFactory(), resource.getSingleton());
    for (JndiObjectResourceParameter prm : resource.getParameters()) {
        d.add(new StringRefAddr(prm.getKey(), prm.getValue()));
    }

    return d;
}

From source file:com.enioka.jqm.tools.JndiContext.java

/**
 * Will create a JNDI Context and register it as the initial context factory builder
 * /*from   www. j  a  v  a2  s . c om*/
 * @return the context
 * @throws NamingException
 *             on any issue during initial context factory builder registration
 */
static JndiContext createJndiContext() throws NamingException {
    try {
        if (!NamingManager.hasInitialContextFactoryBuilder()) {
            JndiContext ctx = new JndiContext();
            NamingManager.setInitialContextFactoryBuilder(ctx);
            return ctx;
        } else {
            return (JndiContext) NamingManager.getInitialContext(null);
        }
    } catch (Exception e) {
        jqmlogger.error("Could not create JNDI context: " + e.getMessage());
        NamingException ex = new NamingException("Could not initialize JNDI Context");
        ex.setRootCause(e);
        throw ex;
    }
}

From source file:com.gemstone.gemfire.security.GemFireSecurityExceptionTest.java

@Before
public void setUp() throws Exception {
    this.message = testName.getMethodName() + " message";
    this.causeMessage = testName.getMethodName() + " cause message";

    this.nonSerializableResolvedObj = new Object();
    this.nonSerializableNamingException = new NamingException(this.causeMessage);
    this.nonSerializableNamingException.setResolvedObj(this.nonSerializableResolvedObj);

    this.serializableResolvedObj = new SerializableObject(this.testName.getMethodName());
    this.serializableNamingException = new NamingException(this.causeMessage);
    this.serializableNamingException.setResolvedObj(this.serializableResolvedObj);

    assertPreConditions();/*w w  w .  ja  v  a  2 s  . c o  m*/
}