package com.emc.mongoose.api.common.env;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.logging.Logger;

import static com.emc.mongoose.api.common.env.PathUtil.BASE_DIR;

public abstract class Extensions {

	public static final String DIR_EXT = BASE_DIR + File.separator + "ext";
	public static final URLClassLoader CLS_LOADER;

	private static final Logger LOG = Logger.getLogger(Extensions.class.getSimpleName());

	static {

		final File dirExt = new File(DIR_EXT);

		if(!dirExt.exists() || !dirExt.isDirectory()) {

			CLS_LOADER = new URLClassLoader(new URL[] {});
			LOG.warning("No \"" + dirExt.getAbsolutePath() + "\" directory, loaded no extensions");

		} else {

			final File[] extFiles = dirExt.listFiles();

			if(extFiles == null) {
				CLS_LOADER = new URLClassLoader(new URL[] {});
				LOG.warning(
					"Failed to load the contents of the \"" + dirExt.getAbsolutePath()
						+ "\" directory, loaded no extensions"
				);
			} else {

				final URL[] extFileUrls = new URL[extFiles.length];
				final JarFile[] extFileJars = new JarFile[extFiles.length];

				for(int i = 0; i < extFiles.length; i ++) {
					try {
						extFileUrls[i] = extFiles[i].toURI().toURL();
						LOG.config("Loading the extension from the file: \"" + extFiles[i] + "\"");
						try {
							extFileJars[i] = new JarFile(extFiles[i]);
						} catch(final IOException e) {
							LOG.warning(
								"Failed to load the file \"" + extFiles[i]
									+ "\", expected a valid JAR/ZIP file"
							);
						}
					} catch(final MalformedURLException e) {
						throw new AssertionError(e);
					}
				}

				CLS_LOADER = new URLClassLoader(extFileUrls);

				Enumeration jarEntries;
				JarEntry jarEntry;
				String jarEntryName;
				String clsName;
				int clsCounter = 0;
				for(int i = 0; i < extFiles.length; i ++) {
					if(null != extFileJars[i]) {
						jarEntries = extFileJars[i].entries();
						while(jarEntries.hasMoreElements()) {
							jarEntry = (JarEntry) jarEntries.nextElement();
							jarEntryName = jarEntry.getName();
							if(!jarEntry.isDirectory() && jarEntryName.endsWith(".class")) {
								clsName = jarEntryName
									.substring(0, jarEntryName.length() - 6)
									.replace('/', '.');
								try {
									CLS_LOADER.loadClass(clsName);
									clsCounter ++;
								} catch(final Throwable cause) {
									LOG.fine(
										"Failed to load class \"" + clsName
											+ "\" from the jar file \"" + extFiles[i] + "\""
									);
								}
							}
						}
					}
				}
				LOG.info("Loaded " + clsCounter + " extension classes");
			}
		}
	}
}

@@ -180,69 +180,8 @@
 er;%0A
-import java.util.Enumeration;%0Aimport java.util.jar.JarEntry;%0A
 impo
@@ -1784,936 +1784,44 @@
 Urls
-);%0A%0A%09%09%09%09Enumeration jarEntries;%0A%09%09%09%09JarEntry jarEntry;%0A%09%09%09%09String jarEntryName;%0A%09%09%09%09String clsName;%0A%09%09%09%09int clsCounter = 0;%0A%09%09%09%09for(int i = 0; i %3C extFiles.length; i ++) %7B%0A%09%09%09%09%09if(null != extFileJars%5Bi%5D) %7B%0A%09%09%09%09%09%09jarEntries = extFileJars%5Bi%5D.entries();%0A%09%09%09%09%09%09while(jarEntries.hasMoreElements()) %7B%0A%09%09%09%09%09%09%09jarEntry = (JarEntry) jarEntries.nextElement();%0A%09%09%09%09%09%09%09jarEntryName = jarEntry.getName();%0A%09%09%09%09%09%09%09if(!jarEntry.isDirectory() && jarEntryName.endsWith(%22.class%22)) %7B%0A%09%09%09%09%09%09%09%09clsName = jarEntryName%0A%09%09%09%09%09%09%09%09%09.substring(0, jarEntryName.length() - 6)%0A%09%09%09%09%09%09%09%09%09.replace('/', '.');%0A%09%09%09%09%09%09%09%09try %7B%0A%09%09%09%09%09%09%09%09%09CLS_LOADER.loadClass(clsName);%0A%09%09%09%09%09%09%09%09%09clsCounter ++;%0A%09%09%09%09%09%09%09%09%7D catch(final Throwable cause) %7B%0A%09%09%09%09%09%09%09%09%09LOG.fine(%0A%09%09%09%09%09%09%09%09%09%09%22Failed to load class %5C%22%22 + clsName%0A%09%09%09%09%09%09%09%09%09%09%09+ %22%5C%22 from the jar file %5C%22%22 + extFiles%5Bi%5D + %22%5C%22%22%0A%09%09%09%09%09%09%09%09%09);%0A%09%09%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%09LOG.info(%22Loaded %22 + clsCounter + %22 extension classes%22
+, ClassLoader.getSystemClassLoader()
 );%0A%09

save [skip ci]