Search in sources :

Example 46 with NameClassPair

use of javax.naming.NameClassPair in project tomee by apache.

the class JNDIContext method listBindings.

@SuppressWarnings("unchecked")
@Override
public NamingEnumeration<Binding> listBindings(final String name) throws NamingException {
    final Object o = lookup(name);
    if (o instanceof Context) {
        final Context context = (Context) o;
        final NamingEnumeration<NameClassPair> enumeration = context.list("");
        final List<NameClassPair> bindings = new ArrayList<NameClassPair>();
        while (enumeration.hasMoreElements()) {
            final NameClassPair pair = enumeration.nextElement();
            bindings.add(new LazyBinding(pair.getName(), pair.getClassName(), context));
        }
        return new NameClassPairEnumeration(bindings);
    } else {
        return null;
    }
}
Also used : Context(javax.naming.Context) NameClassPair(javax.naming.NameClassPair) ArrayList(java.util.ArrayList)

Example 47 with NameClassPair

use of javax.naming.NameClassPair in project tomee by apache.

the class NameClassPairEnumeration method readExternal.

@Override
public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
    // future use
    final byte version = in.readByte();
    int size = in.readInt();
    list = new ArrayList<NameClassPair>(size);
    for (; size > 0; size--) {
        final String name = (String) in.readObject();
        final String className = (String) in.readObject();
        list.add(new NameClassPair(name, className));
    }
    iterator = list.iterator();
}
Also used : NameClassPair(javax.naming.NameClassPair)

Example 48 with NameClassPair

use of javax.naming.NameClassPair in project tomee by apache.

the class JndiRequestHandler method doList.

private void doList(final JNDIRequest req, final JNDIResponse res, final String prefix) {
    final String name = req.getRequestString();
    try {
        final NamingEnumeration<NameClassPair> namingEnumeration = rootContext.list(prefix + name);
        if (namingEnumeration == null) {
            res.setResponseCode(ResponseCodes.JNDI_OK);
            res.setResult(null);
        } else {
            res.setResponseCode(ResponseCodes.JNDI_ENUMERATION);
            final ArrayList<NameClassPair> list = Collections.list(namingEnumeration);
            for (final NameClassPair pair : list) {
                if (pair.getClassName().equals(IvmContext.class.getName())) {
                    pair.setClassName(javax.naming.Context.class.getName());
                }
            }
            res.setResult(new NameClassPairEnumeration(list));
        }
    } catch (NameNotFoundException e) {
        res.setResponseCode(ResponseCodes.JNDI_NOT_FOUND);
    } catch (NamingException e) {
        res.setResponseCode(ResponseCodes.JNDI_NAMING_EXCEPTION);
        res.setResult(new ThrowableArtifact(e));
    }
}
Also used : IvmContext(org.apache.openejb.core.ivm.naming.IvmContext) BeanContext(org.apache.openejb.BeanContext) Context(javax.naming.Context) IvmContext(org.apache.openejb.core.ivm.naming.IvmContext) NameNotFoundException(javax.naming.NameNotFoundException) NameClassPair(javax.naming.NameClassPair) ThrowableArtifact(org.apache.openejb.client.ThrowableArtifact) NamingException(javax.naming.NamingException) NameClassPairEnumeration(org.apache.openejb.client.NameClassPairEnumeration)

Example 49 with NameClassPair

use of javax.naming.NameClassPair in project tomee by apache.

the class JndiServlet method addBindings.

private void addBindings(final String path, final Map<String, Object> bindings, final Context context) {
    try {
        for (final NameClassPair pair : Collections.list(context.list(""))) {
            final String name = pair.getName();
            final String className = pair.getClassName();
            if ("org.apache.naming.resources.FileDirContext$FileResource".equals(className)) {
                bindings.put(path + name, "<file>");
            } else {
                try {
                    final Object value = context.lookup(name);
                    if (value instanceof Context) {
                        final Context nextedContext = (Context) value;
                        bindings.put(path + name, "");
                        addBindings(path + name + "/", bindings, nextedContext);
                    } else {
                        bindings.put(path + name, value);
                    }
                } catch (final NamingException e) {
                    // lookup failed
                    bindings.put(path + name, "ERROR: " + e.getMessage());
                }
            }
        }
    } catch (final NamingException e) {
        bindings.put(path, "ERROR: list bindings threw an exception: " + e.getMessage());
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) NameClassPair(javax.naming.NameClassPair) NamingException(javax.naming.NamingException)

Example 50 with NameClassPair

use of javax.naming.NameClassPair in project deltaspike by apache.

the class JndiUtils method list.

/**
 * Resolves an instances for the given naming context.
 *
 * @param name       context name
 * @param type       target type
 * @param <T>        type
 * @return the found instances, null otherwise
 */
public static <T> Map<String, T> list(String name, Class<T> type) {
    Map<String, T> result = new HashMap<String, T>();
    try {
        NameParser nameParser = initialContext.getNameParser(name);
        NamingEnumeration<NameClassPair> enumeration = initialContext.list(name);
        while (enumeration.hasMoreElements()) {
            try {
                NameClassPair binding = enumeration.nextElement();
                Name bindingName = nameParser.parse(name).add(binding.getName());
                result.put(binding.getName(), lookup(bindingName, type));
            } catch (NamingException e) {
                if (LOG.isLoggable(Level.FINEST)) {
                    // this is expected if there is no entry in JNDI for the requested name or type
                    // so finest level is ok, if devs want to see it they can enable this logger level.
                    LOG.log(Level.FINEST, "InitialContext#list failed!", e);
                }
            }
        }
    } catch (NamingException e) {
        // this is fine at this point, since the individual lines will be caught currently.
        LOG.log(Level.WARNING, "Problem reading the name of the JNDI location " + name + " or failuring listing pairs.", e);
    }
    return result;
}
Also used : HashMap(java.util.HashMap) NameClassPair(javax.naming.NameClassPair) NamingException(javax.naming.NamingException) NameParser(javax.naming.NameParser) Name(javax.naming.Name)

Aggregations

NameClassPair (javax.naming.NameClassPair)60 NamingException (javax.naming.NamingException)28 DirContext (javax.naming.directory.DirContext)19 Test (org.junit.Test)17 HashSet (java.util.HashSet)13 Context (javax.naming.Context)11 InitialContext (javax.naming.InitialContext)10 Hashtable (java.util.Hashtable)9 NamingEnumeration (javax.naming.NamingEnumeration)8 InitialDirContext (javax.naming.directory.InitialDirContext)8 IOException (java.io.IOException)7 FileNotFoundException (java.io.FileNotFoundException)5 HashMap (java.util.HashMap)5 Vector (java.util.Vector)5 Resource (org.apache.naming.resources.Resource)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 OutputStreamWriter (java.io.OutputStreamWriter)4 CompositeName (javax.naming.CompositeName)4 File (java.io.File)3