Search in sources :

Example 1 with JndiException

use of org.hibernate.engine.jndi.JndiException in project hibernate-orm by hibernate.

the class JndiServiceImpl method addListener.

@Override
public void addListener(String jndiName, NamespaceChangeListener listener) {
    final InitialContext initialContext = buildInitialContext();
    final Name name = parseName(jndiName, initialContext);
    try {
        ((EventContext) initialContext).addNamingListener(name, EventContext.OBJECT_SCOPE, listener);
    } catch (Exception e) {
        throw new JndiException("Unable to bind listener to namespace [" + name + "]", e);
    } finally {
        cleanUp(initialContext);
    }
}
Also used : EventContext(javax.naming.event.EventContext) JndiException(org.hibernate.engine.jndi.JndiException) InitialContext(javax.naming.InitialContext) NamingException(javax.naming.NamingException) JndiException(org.hibernate.engine.jndi.JndiException) JndiNameException(org.hibernate.engine.jndi.JndiNameException) InvalidNameException(javax.naming.InvalidNameException) NameNotFoundException(javax.naming.NameNotFoundException) Name(javax.naming.Name)

Example 2 with JndiException

use of org.hibernate.engine.jndi.JndiException in project hibernate-orm by hibernate.

the class JndiServiceImpl method bind.

private void bind(Name name, Object value, Context context) {
    try {
        LOG.tracef("Binding : %s", name);
        context.rebind(name, value);
    } catch (Exception initialException) {
        // We had problems doing a simple bind operation.
        if (name.size() == 1) {
            // if the jndi name had only 1 component there is nothing more we can do...
            throw new JndiException("Error performing bind [" + name + "]", initialException);
        }
        // Otherwise, there is a good chance this may have been caused by missing intermediate contexts.  So we
        // attempt to create those missing intermediate contexts and bind again
        Context intermediateContextBase = context;
        while (name.size() > 1) {
            final String intermediateContextName = name.get(0);
            Context intermediateContext = null;
            try {
                LOG.tracev("Intermediate lookup: {0}", intermediateContextName);
                intermediateContext = (Context) intermediateContextBase.lookup(intermediateContextName);
            } catch (NameNotFoundException handledBelow) {
            // ok as we will create it below if not found
            } catch (NamingException e) {
                throw new JndiException("Unanticipated error doing intermediate lookup", e);
            }
            if (intermediateContext != null) {
                LOG.tracev("Found intermediate context: {0}", intermediateContextName);
            } else {
                LOG.tracev("Creating sub-context: {0}", intermediateContextName);
                try {
                    intermediateContext = intermediateContextBase.createSubcontext(intermediateContextName);
                } catch (NamingException e) {
                    throw new JndiException("Error creating intermediate context [" + intermediateContextName + "]", e);
                }
            }
            intermediateContextBase = intermediateContext;
            name = name.getSuffix(1);
        }
        LOG.tracev("Binding : {0}", name);
        try {
            intermediateContextBase.rebind(name, value);
        } catch (NamingException e) {
            throw new JndiException("Error performing intermediate bind [" + name + "]", e);
        }
    }
    LOG.debugf("Bound name: %s", name);
}
Also used : InitialContext(javax.naming.InitialContext) EventContext(javax.naming.event.EventContext) Context(javax.naming.Context) NameNotFoundException(javax.naming.NameNotFoundException) JndiException(org.hibernate.engine.jndi.JndiException) NamingException(javax.naming.NamingException) NamingException(javax.naming.NamingException) JndiException(org.hibernate.engine.jndi.JndiException) JndiNameException(org.hibernate.engine.jndi.JndiNameException) InvalidNameException(javax.naming.InvalidNameException) NameNotFoundException(javax.naming.NameNotFoundException)

Example 3 with JndiException

use of org.hibernate.engine.jndi.JndiException in project hibernate-orm by hibernate.

the class JndiServiceImpl method locate.

@Override
public Object locate(String jndiName) {
    final InitialContext initialContext = buildInitialContext();
    final Name name = parseName(jndiName, initialContext);
    try {
        return initialContext.lookup(name);
    } catch (NamingException e) {
        throw new JndiException("Unable to lookup JNDI name [" + jndiName + "]", e);
    } finally {
        cleanUp(initialContext);
    }
}
Also used : NamingException(javax.naming.NamingException) JndiException(org.hibernate.engine.jndi.JndiException) InitialContext(javax.naming.InitialContext) Name(javax.naming.Name)

Example 4 with JndiException

use of org.hibernate.engine.jndi.JndiException in project hibernate-orm by hibernate.

the class JndiServiceImpl method unbind.

@Override
public void unbind(String jndiName) {
    final InitialContext initialContext = buildInitialContext();
    final Name name = parseName(jndiName, initialContext);
    try {
        initialContext.unbind(name);
    } catch (Exception e) {
        throw new JndiException("Error performing unbind [" + name + "]", e);
    } finally {
        cleanUp(initialContext);
    }
}
Also used : JndiException(org.hibernate.engine.jndi.JndiException) InitialContext(javax.naming.InitialContext) NamingException(javax.naming.NamingException) JndiException(org.hibernate.engine.jndi.JndiException) JndiNameException(org.hibernate.engine.jndi.JndiNameException) InvalidNameException(javax.naming.InvalidNameException) NameNotFoundException(javax.naming.NameNotFoundException) Name(javax.naming.Name)

Aggregations

InitialContext (javax.naming.InitialContext)4 NamingException (javax.naming.NamingException)4 JndiException (org.hibernate.engine.jndi.JndiException)4 InvalidNameException (javax.naming.InvalidNameException)3 Name (javax.naming.Name)3 NameNotFoundException (javax.naming.NameNotFoundException)3 JndiNameException (org.hibernate.engine.jndi.JndiNameException)3 EventContext (javax.naming.event.EventContext)2 Context (javax.naming.Context)1