protected List getApplicationContextResource()

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


    protected List<URL> getApplicationContextResource(URL url)
        throws ContributionReadException {
        File manifestFile = null;
        File appXmlFile;
        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);
	            			if (appXmlFile.exists()) {
	            				appCtxResources.add(appXmlFile.toURI().toURL());
	            			}
	            		}
	            		return appCtxResources;
                    }
                }
                // no manifest-specified Spring context, use default
                appXmlFile = new File(locationFile, "META-INF" + File.separator + "spring"
                                                        + File.separator + SpringImplementationConstants.APPLICATION_CONTEXT);
                if (appXmlFile.exists()) {
                	appCtxResources.add(appXmlFile.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);
    	            			if (je != null) {
    	            				appCtxResources.add(new URL("jar:" + locationFile.toURI().toURL() + "!/" + appCtxPath));
    	            			}
    	            		}
    	            		return appCtxResources;
                        }
                    }
                    // Look for the default applicaiton-context.xml file, when MANIFEST.MF is absent.
                    je = jf.getJarEntry("META-INF" + "/" + "spring" + "/" + SpringImplementationConstants.APPLICATION_CONTEXT);
                    if (je != null) {
                    	appCtxResources.add(new URL("jar:" + locationFile.toURI().toURL() + "!/" +
                        		"META-INF" + "/" + "spring" + "/" + SpringImplementationConstants.APPLICATION_CONTEXT));
                    	return appCtxResources;                        
                    }
                } catch (IOException e) {
                    // TODO: create a more appropriate exception type
                    throw new ContributionReadException("SpringXMLLoader 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);
		                                if (je != null) {
		                                	appCtxResources.add(new URL("jar:" + url.getPath() + "/" + path));
		    	                        	return appCtxResources;
		                                }
	        	            		}
	                            }
	        				}
	        			    // Look for the default applicaiton-context.xml file, when MANIFEST.MF is absent.
        			    	je = jf.getJarEntry(url.getPath().substring(url.getPath().indexOf("!/")+2) + "/" + 
                        			"META-INF" + "/" + "spring" + "/" + SpringImplementationConstants.APPLICATION_CONTEXT);
                            if (je != null) {
                            	appCtxResources.add(new URL("jar:" + url.getPath() + "/" + 
                                    	"META-INF" + "/" + "spring" + "/" + SpringImplementationConstants.APPLICATION_CONTEXT));
	                        	return appCtxResources;
                            }
	        			}
            		} catch (IOException e) {
                        throw new ContributionReadException("Error reading manifest " + manifestFile);
                    }
        		}
        	}
        }

        throw new ContributionReadException("SpringXMLComponentTypeLoader getApplicationContextResource: " 
        		                            + "unable to read resource file " + url);
    } // end method getApplicationContextResource