protected List getApplicationContextResource()

in modules/implementation-spring/src/main/java/org/apache/tuscany/sca/implementation/spring/introspect/SpringXMLComponentTypeLoader.java [824:963]


    protected List<URL> getApplicationContextResource(URL url) throws ContributionReadException {
        File manifestFile = null;
        File appXmlFile;
        File appXmlFolder;
        File locationFile = null;
        List<URL> appCtxResources = new ArrayList<URL>();

        if (url != null) {
            String path = url.getPath();
            locationFile = new File(path);
        } else {
            throw new ContributionReadException(
                                                "SpringXMLComponentTypeLoader getApplicationContextResource: " + "unable to find resource file "
                                                    + url);
        }

        if (locationFile.isDirectory()) {
            try {
                manifestFile = new File(locationFile, "META-INF" + File.separator + "MANIFEST.MF");
                if (manifestFile.exists()) {
                    Manifest mf = new Manifest(new FileInputStream(manifestFile));
                    Attributes mainAttrs = mf.getMainAttributes();
                    String appCtxPath = mainAttrs.getValue("Spring-Context");
                    if (appCtxPath != null) {
                        String[] cxtPaths = appCtxPath.split(";");
                        for (String path : cxtPaths) {
                            appXmlFile = new File(locationFile, path.trim());
                            if (appXmlFile.exists()) {
                                appCtxResources.add(appXmlFile.toURI().toURL());
                            }
                        }
                        return appCtxResources;
                    }
                }
                // No MANIFEST.MF file OR no manifest-specified Spring context , then read all the 
                // xml files available in the META-INF/spring folder.
                appXmlFolder = new File(locationFile, "META-INF" + File.separator + "spring");
                if (appXmlFolder.exists()) {
                    File[] files = appXmlFolder.listFiles();
                    for (File appFile : files) {
                        if (appFile.getName().endsWith(".xml")) {
                            appCtxResources.add(appFile.toURI().toURL());
                        }
                    }
                    return appCtxResources;
                }
            } catch (IOException e) {
                throw new ContributionReadException("Error reading manifest " + manifestFile);
            }
        } else {
            if (locationFile.isFile() && locationFile.getName().endsWith(".jar")) {
                try {
                    JarFile jf = new JarFile(locationFile);
                    JarEntry je;
                    Manifest mf = jf.getManifest();
                    if (mf != null) {
                        Attributes mainAttrs = mf.getMainAttributes();
                        String appCtxPath = mainAttrs.getValue("Spring-Context");
                        if (appCtxPath != null) {
                            String[] cxtPaths = appCtxPath.split(";");
                            for (String path : cxtPaths) {
                                je = jf.getJarEntry(path.trim());
                                if (je != null)
                                    appCtxResources.add(new URL("jar:" + locationFile.toURI().toURL()
                                        + "!/"
                                        + appCtxPath));
                            }
                            return appCtxResources;
                        }
                    }
                    // No MANIFEST.MF file OR no manifest-specified Spring context , then read all the 
                    // .xml files available in the META-INF/spring folder.
                    Enumeration<JarEntry> entries = jf.entries();
                    while (entries.hasMoreElements()) {
                        je = entries.nextElement();
                        if (je.getName().startsWith("META-INF/spring/") && je.getName().endsWith(".xml")) {
                            appCtxResources.add(new URL("jar:" + locationFile.toURI().toURL() + "!/" + je.getName()));
                        }
                    }
                    return appCtxResources;
                } catch (IOException e) {
                    // TODO: create a more appropriate exception type
                    throw new ContributionReadException(
                                                        "SpringXMLComponentTypeLoader getApplicationContextResource: " + " IO exception reading context file.",
                                                        e);
                }
            } else {
                if (locationFile.getName().endsWith(".xml")) {
                    appCtxResources.add(url);
                    return appCtxResources;
                } else {
                    // Deal with the directory inside a jar file, in case the contribution itself is a JAR file.
                    try {
                        if (locationFile.getPath().indexOf(".jar") > 0) {
                            String jarPath = url.getPath().substring(5, url.getPath().indexOf("!"));
                            JarFile jf = new JarFile(jarPath);
                            JarEntry je =
                                jf.getJarEntry(url.getPath().substring(url.getPath().indexOf("!/") + 2) + "/"
                                    + "META-INF"
                                    + "/"
                                    + "MANIFEST.MF");
                            if (je != null) {
                                Manifest mf = new Manifest(jf.getInputStream(je));
                                Attributes mainAttrs = mf.getMainAttributes();
                                String appCtxPath = mainAttrs.getValue("Spring-Context");
                                if (appCtxPath != null) {
                                    String[] cxtPaths = appCtxPath.split(";");
                                    for (String path : cxtPaths) {
                                        je =
                                            jf.getJarEntry(url.getPath().substring(url.getPath().indexOf("!/") + 2) + "/"
                                                + path.trim());
                                        if (je != null) {
                                            appCtxResources.add(new URL("jar:" + url.getPath() + "/" + path.trim()));
                                        }
                                    }
                                    return appCtxResources;
                                }
                            }
                            // No MANIFEST.MF file OR no manifest-specified Spring context , then read all the 
                            // .xml files available in the META-INF/spring folder.
                            Enumeration<JarEntry> entries = jf.entries();
                            while (entries.hasMoreElements()) {
                                je = entries.nextElement();
                                if (je.getName().startsWith("META-INF/spring/") && je.getName().endsWith(".xml")) {
                                    appCtxResources.add(new URL("jar:" + url.getPath() + "/" + je.getName()));
                                }
                            }
                            return appCtxResources;
                        }
                    } catch (IOException e) {
                        throw new ContributionReadException("Error reading manifest " + manifestFile);
                    }
                }
            }
        }

        throw new ContributionReadException(
                                            "SpringXMLComponentTypeLoader getApplicationContextResource: " + "unable to process unknown resource type: "
                                                + url);
    } // end method getApplicationContextResource