Search in sources :

Example 31 with NameClassPair

use of javax.naming.NameClassPair in project Payara by payara.

the class WebappLoader method copyDir.

/**
 * Copy directory.
 */
private boolean copyDir(DirContext srcDir, File destDir) {
    try {
        NamingEnumeration<NameClassPair> enumeration = srcDir.list("");
        while (enumeration.hasMoreElements()) {
            NameClassPair ncPair = enumeration.nextElement();
            String name = ncPair.getName();
            Object object = srcDir.lookup(name);
            File currentFile = new File(destDir, name);
            if (object instanceof Resource) {
                InputStream is = ((Resource) object).streamContent();
                OutputStream os = new FileOutputStream(currentFile);
                if (!copy(is, os))
                    return false;
            } else if (object instanceof InputStream) {
                OutputStream os = new FileOutputStream(currentFile);
                if (!copy((InputStream) object, os))
                    return false;
            } else if (object instanceof DirContext) {
                if (!currentFile.isDirectory() && !currentFile.mkdir())
                    return false;
                if (!copyDir((DirContext) object, currentFile))
                    return false;
            }
        }
    } catch (NamingException | IOException e) {
        return false;
    }
    return true;
}
Also used : Resource(org.apache.naming.resources.Resource) DirContext(javax.naming.directory.DirContext) NameClassPair(javax.naming.NameClassPair) NamingException(javax.naming.NamingException)

Example 32 with NameClassPair

use of javax.naming.NameClassPair in project geronimo-xbean by apache.

the class Lookup method list.

public void list(String name, InputStream in, PrintStream out) throws IOException {
    try {
        NamingEnumeration names = null;
        try {
            names = ctx.list(name);
        } catch (NameNotFoundException e) {
            out.print("lookup: ");
            out.print(name);
            out.println(": No such object or subcontext");
            return;
        } catch (Throwable e) {
            out.print("lookup: error: ");
            e.printStackTrace(new PrintStream(out));
            return;
        }
        if (names == null) {
            return;
        }
        while (names.hasMore()) {
            NameClassPair entry = (NameClassPair) names.next();
            out.println(entry.getName());
        }
    } catch (Exception e) {
        e.printStackTrace(new PrintStream(out));
    }
}
Also used : PrintStream(java.io.PrintStream) NameNotFoundException(javax.naming.NameNotFoundException) NameClassPair(javax.naming.NameClassPair) NamingEnumeration(javax.naming.NamingEnumeration) IOException(java.io.IOException) NameNotFoundException(javax.naming.NameNotFoundException)

Example 33 with NameClassPair

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

the class JndiRegistry method findByType.

public <T> Set<T> findByType(Class<T> type) {
    Set<T> answer = new LinkedHashSet<T>();
    try {
        NamingEnumeration<NameClassPair> list = getContext().list("");
        while (list.hasMore()) {
            NameClassPair pair = list.next();
            Object instance = context.lookup(pair.getName());
            if (type.isInstance(instance)) {
                answer.add(type.cast(instance));
            }
        }
    } catch (NamingException e) {
    // ignore
    }
    return answer;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) NameClassPair(javax.naming.NameClassPair) NamingException(javax.naming.NamingException)

Example 34 with NameClassPair

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

the class JndiRegistry method findByTypeWithName.

public <T> Map<String, T> findByTypeWithName(Class<T> type) {
    Map<String, T> answer = new LinkedHashMap<String, T>();
    try {
        NamingEnumeration<NameClassPair> list = getContext().list("");
        while (list.hasMore()) {
            NameClassPair pair = list.next();
            Object instance = context.lookup(pair.getName());
            if (type.isInstance(instance)) {
                answer.put(pair.getName(), type.cast(instance));
            }
        }
    } catch (NamingException e) {
    // ignore
    }
    return answer;
}
Also used : NameClassPair(javax.naming.NameClassPair) NamingException(javax.naming.NamingException) LinkedHashMap(java.util.LinkedHashMap)

Example 35 with NameClassPair

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

the class WebappClassLoaderBase method modified.

/**
 * Have one or more classes or resources been modified so that a reload
 * is appropriate?
 */
public boolean modified() {
    if (log.isDebugEnabled())
        log.debug("modified()");
    // Checking for modified loaded resources
    int length = paths.length;
    // A rare race condition can occur in the updates of the two arrays
    // It's totally ok if the latest class added is not checked (it will
    // be checked the next time
    int length2 = lastModifiedDates.length;
    if (length > length2)
        length = length2;
    for (int i = 0; i < length; i++) {
        try {
            long lastModified = ((ResourceAttributes) resources.getAttributes(paths[i])).getLastModified();
            if (lastModified != lastModifiedDates[i]) {
                if (log.isDebugEnabled())
                    log.debug("  Resource '" + paths[i] + "' was modified; Date is now: " + new java.util.Date(lastModified) + " Was: " + new java.util.Date(lastModifiedDates[i]));
                return (true);
            }
        } catch (NamingException e) {
            log.error("    Resource '" + paths[i] + "' is missing");
            return (true);
        }
    }
    length = jarNames.length;
    // Check if JARs have been added or removed
    if (getJarPath() != null) {
        try {
            NamingEnumeration<Binding> enumeration = resources.listBindings(getJarPath());
            int i = 0;
            while (enumeration.hasMoreElements() && (i < length)) {
                NameClassPair ncPair = enumeration.nextElement();
                String name = ncPair.getName();
                // Ignore non JARs present in the lib folder
                if (!name.endsWith(".jar"))
                    continue;
                if (!name.equals(jarNames[i])) {
                    // Missing JAR
                    log.info("    Additional JARs have been added : '" + name + "'");
                    return (true);
                }
                i++;
            }
            if (enumeration.hasMoreElements()) {
                while (enumeration.hasMoreElements()) {
                    NameClassPair ncPair = enumeration.nextElement();
                    String name = ncPair.getName();
                    // Additional non-JAR files are allowed
                    if (name.endsWith(".jar")) {
                        // There was more JARs
                        log.info("    Additional JARs have been added");
                        return (true);
                    }
                }
            } else if (i < jarNames.length) {
                // There was less JARs
                log.info("    Additional JARs have been added");
                return (true);
            }
        } catch (NamingException e) {
            if (log.isDebugEnabled())
                log.debug("    Failed tracking modifications of '" + getJarPath() + "'");
        } catch (ClassCastException e) {
            log.error("    Failed tracking modifications of '" + getJarPath() + "' : " + e.getMessage());
        }
    }
    // No classes have been modified
    return (false);
}
Also used : Binding(javax.naming.Binding) NameClassPair(javax.naming.NameClassPair) NamingException(javax.naming.NamingException) ResourceAttributes(org.apache.naming.resources.ResourceAttributes)

Aggregations

NameClassPair (javax.naming.NameClassPair)59 NamingException (javax.naming.NamingException)27 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 Resource (org.apache.naming.resources.Resource)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 OutputStreamWriter (java.io.OutputStreamWriter)4 Vector (java.util.Vector)4 CompositeName (javax.naming.CompositeName)4 File (java.io.File)3