Search in sources :

Example 21 with Binding

use of javax.naming.Binding in project tomcat70 by apache.

the class ManagerServlet method printResources.

/**
 * List the resources of the given context.
 */
protected void printResources(PrintWriter writer, String prefix, javax.naming.Context namingContext, String type, Class<?> clazz, StringManager smClient) {
    try {
        NamingEnumeration<Binding> items = namingContext.listBindings("");
        while (items.hasMore()) {
            Binding item = items.next();
            if (item.getObject() instanceof javax.naming.Context) {
                printResources(writer, prefix + item.getName() + "/", (javax.naming.Context) item.getObject(), type, clazz, smClient);
            } else {
                if ((clazz != null) && (!(clazz.isInstance(item.getObject())))) {
                    continue;
                }
                writer.print(prefix + item.getName());
                writer.print(':');
                writer.print(item.getClassName());
                // Do we want a description if available?
                writer.println();
            }
        }
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        log("ManagerServlet.resources[" + type + "]", t);
        writer.println(smClient.getString("managerServlet.exception", t.toString()));
    }
}
Also used : Binding(javax.naming.Binding) Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext)

Example 22 with Binding

use of javax.naming.Binding in project tomcat70 by apache.

the class GlobalResourcesLifecycleListener method createMBeans.

/**
 * Create the MBeans for the interesting global JNDI resources in
 * the specified naming context.
 *
 * @param prefix Prefix for complete object name paths
 * @param context Context to be scanned
 *
 * @exception NamingException if a JNDI exception occurs
 */
protected void createMBeans(String prefix, Context context) throws NamingException {
    if (log.isDebugEnabled()) {
        log.debug("Creating MBeans for Global JNDI Resources in Context '" + prefix + "'");
    }
    try {
        NamingEnumeration<Binding> bindings = context.listBindings("");
        while (bindings.hasMore()) {
            Binding binding = bindings.next();
            String name = prefix + binding.getName();
            Object value = context.lookup(binding.getName());
            if (log.isDebugEnabled()) {
                log.debug("Checking resource " + name);
            }
            if (value instanceof Context) {
                createMBeans(name + "/", (Context) value);
            } else if (value instanceof UserDatabase) {
                try {
                    createMBeans(name, (UserDatabase) value);
                } catch (Exception e) {
                    log.error("Exception creating UserDatabase MBeans for " + name, e);
                }
            }
        }
    } catch (RuntimeException ex) {
        log.error("RuntimeException " + ex);
    } catch (OperationNotSupportedException ex) {
        log.error("Operation not supported " + ex);
    }
}
Also used : Binding(javax.naming.Binding) InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) OperationNotSupportedException(javax.naming.OperationNotSupportedException) UserDatabase(org.apache.catalina.UserDatabase) NamingException(javax.naming.NamingException) OperationNotSupportedException(javax.naming.OperationNotSupportedException)

Example 23 with Binding

use of javax.naming.Binding in project tomcat70 by apache.

the class ApplicationContext method listCollectionPaths.

/**
 * List resource paths (recursively), and store all of them in the given
 * Set.
 */
private static void listCollectionPaths(Set<String> set, DirContext resources, String path) throws NamingException {
    Enumeration<Binding> childPaths = resources.listBindings(path);
    while (childPaths.hasMoreElements()) {
        Binding binding = childPaths.nextElement();
        String name = binding.getName();
        StringBuilder childPath = new StringBuilder(path);
        if (!"/".equals(path) && !path.endsWith("/"))
            childPath.append("/");
        childPath.append(name);
        Object object = binding.getObject();
        if (object instanceof DirContext) {
            childPath.append("/");
        }
        set.add(childPath.toString());
    }
}
Also used : Binding(javax.naming.Binding) DirContext(javax.naming.directory.DirContext)

Example 24 with Binding

use of javax.naming.Binding in project tomcat70 by apache.

the class NamingContextBindingsEnumeration method nextElementInternal.

private Binding nextElementInternal() throws NamingException {
    NamingEntry entry = iterator.next();
    Object value;
    // If the entry is a reference, resolve it
    if (entry.type == NamingEntry.REFERENCE || entry.type == NamingEntry.LINK_REF) {
        try {
            value = ctx.lookup(new CompositeName(entry.name));
        } catch (NamingException e) {
            throw e;
        } catch (Exception e) {
            NamingException ne = new NamingException(e.getMessage());
            ne.initCause(e);
            throw ne;
        }
    } else {
        value = entry.value;
    }
    return new Binding(entry.name, value.getClass().getName(), value, true);
}
Also used : Binding(javax.naming.Binding) CompositeName(javax.naming.CompositeName) NamingException(javax.naming.NamingException) NamingException(javax.naming.NamingException)

Example 25 with Binding

use of javax.naming.Binding in project tomcat70 by apache.

the class ExtensionValidator method validateApplication.

// --------------------------------------------------------- Public Methods
/**
 * Runtime validation of a Web Application.
 *
 * This method uses JNDI to look up the resources located under a
 * <code>DirContext</code>. It locates Web Application MANIFEST.MF
 * file in the /META-INF/ directory of the application and all
 * MANIFEST.MF files in each JAR file located in the WEB-INF/lib
 * directory and creates an <code>ArrayList</code> of
 * <code>ManifestResource<code> objects. These objects are then passed
 * to the validateManifestResources method for validation.
 *
 * @param dirContext The JNDI root of the Web Application
 * @param context The context from which the Logger and path to the
 *                application
 *
 * @return true if all required extensions satisfied
 */
public static synchronized boolean validateApplication(DirContext dirContext, Context context) throws IOException {
    String appName = context.getName();
    ArrayList<ManifestResource> appManifestResources = new ArrayList<ManifestResource>();
    // therefore is not valid
    if (dirContext == null)
        return false;
    // Find the Manifest for the Web Application
    InputStream inputStream = null;
    try {
        NamingEnumeration<Binding> wne = dirContext.listBindings("/META-INF/");
        Binding binding = wne.nextElement();
        if (binding.getName().toUpperCase(Locale.ENGLISH).equals("MANIFEST.MF")) {
            Resource resource = (Resource) dirContext.lookup("/META-INF/" + binding.getName());
            inputStream = resource.streamContent();
            Manifest manifest = new Manifest(inputStream);
            inputStream.close();
            inputStream = null;
            ManifestResource mre = new ManifestResource(sm.getString("extensionValidator.web-application-manifest"), manifest, ManifestResource.WAR);
            appManifestResources.add(mre);
        }
    } catch (NamingException nex) {
    // Application does not contain a MANIFEST.MF file
    } catch (NoSuchElementException nse) {
    // Application does not contain a MANIFEST.MF file
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
            }
        }
    }
    // Locate the Manifests for all bundled JARs
    NamingEnumeration<Binding> ne = null;
    // Primarily used for error reporting
    String jarName = null;
    try {
        ne = dirContext.listBindings("WEB-INF/lib/");
        while ((ne != null) && ne.hasMoreElements()) {
            Binding binding = ne.nextElement();
            jarName = binding.getName();
            if (!jarName.toLowerCase(Locale.ENGLISH).endsWith(".jar")) {
                continue;
            }
            Object obj = dirContext.lookup("/WEB-INF/lib/" + jarName);
            if (!(obj instanceof Resource)) {
                // Probably a directory named xxx.jar - ignore it
                continue;
            }
            Resource resource = (Resource) obj;
            inputStream = resource.streamContent();
            Manifest jmanifest = getManifest(inputStream);
            if (jmanifest != null) {
                ManifestResource mre = new ManifestResource(jarName, jmanifest, ManifestResource.APPLICATION);
                appManifestResources.add(mre);
            }
        }
    } catch (NamingException nex) {
    // Jump out of the check for this application because it
    // has no resources
    } catch (IOException ioe) {
        throw new IOException("Jar: " + jarName, ioe);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
            }
        }
    }
    return validateManifestResources(appName, appManifestResources);
}
Also used : Binding(javax.naming.Binding) FileInputStream(java.io.FileInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Resource(org.apache.naming.resources.Resource) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) NamingException(javax.naming.NamingException) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

Binding (javax.naming.Binding)69 NamingException (javax.naming.NamingException)36 Context (javax.naming.Context)31 InitialContext (javax.naming.InitialContext)29 NameNotFoundException (javax.naming.NameNotFoundException)22 Name (javax.naming.Name)19 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)19 NotContextException (javax.naming.NotContextException)18 OperationNotSupportedException (javax.naming.OperationNotSupportedException)18 Reference (javax.naming.Reference)17 CompoundName (javax.naming.CompoundName)16 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)10 NamingContext (org.eclipse.jetty.jndi.NamingContext)9 CompositeName (javax.naming.CompositeName)8 Test (org.junit.Test)8 HashMap (java.util.HashMap)7 NamingEnumeration (javax.naming.NamingEnumeration)5 ServletContext (javax.servlet.ServletContext)5 MalformedURLException (java.net.MalformedURLException)4