Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package org.kles.m3; import com.intentia.mak.core.m3.classpath.M3ClassPathEntry; import com.intentia.mak.util.MAKException; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Set; import java.util.logging.Level; import java.util.logging.LogManager; import java.util.logging.Logger; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipInputStream; import mak.db.MetaDataDef; import org.apache.commons.io.IOUtils; import org.kles.MainTEST; import org.kles.m3.comparator.DBComparator; import org.kles.m3.comparator.Diff; import org.kles.m3.task.process.analyze.AnalyzeDBTask; public class M3ClassLoader extends ClassLoader { public String[] listSpe = { "Y:/M3BE/env/PREP/Fix/ALS/VFix/bindbg/", "Y:/M3BE/ALS/1310/SP1310C61469/bindbg/", "Y:/M3BE/ALS/1310/base/bindbg/", "Y:/M3BE/CERG/1310/base/bindbg/", "Y:/M3BE/env/PREP/Properties/", "Y:/M3BE/env/PREP/Fix/MVX/HFix/bindbg/", "Y:/M3BE/env/PREP/Fix/MVX/TFix/bindbg/", "Y:/M3BE/env/PREP/Fix/MVX/VFix/bindbg/", "Y:/M3BE/MVX/1310/SP1310S51031/bindbg/", "Y:/M3BE/MVX/1310/base/bindbg/", "Y:/M3BE/env/PREP/Fix/FND/TFix/Foundation/", "Y:/M3BE/FND/133/Foundation.jar", "Y:/M3BE/FND/133/common/activation.jar", "Y:/M3BE/FND/133/common/adonix.jar", "Y:/M3BE/FND/133/common/asm.jar", "Y:/M3BE/FND/133/common/axis-ant.jar", "Y:/M3BE/FND/133/common/axis.jar", "Y:/M3BE/FND/133/common/backport-util-concurrent-3.1-osgi.jar", "Y:/M3BE/FND/133/common/commons-codec-1.3.jar", "Y:/M3BE/FND/133/common/commons-discovery-0.2.jar", "Y:/M3BE/FND/133/common/commons-lang-2.1.jar", "Y:/M3BE/FND/133/common/commons-logging-1.0.4.jar", "Y:/M3BE/FND/133/common/commons-net-2.0.jar", "Y:/M3BE/FND/133/common/commons-net-ftp-2.0.jar", "Y:/M3BE/FND/133/common/commonsCollections.jar", "Y:/M3BE/FND/133/common/commonsLogging.jar", "Y:/M3BE/FND/133/common/commonsPool.jar", "Y:/M3BE/FND/133/common/cryptixJceProvider.jar", "Y:/M3BE/FND/133/common/derby.jar", "Y:/M3BE/FND/133/common/freemarker.jar", "Y:/M3BE/FND/133/common/FndAPI.jar", "Y:/M3BE/FND/133/common/javamail.jar", "Y:/M3BE/FND/133/common/jaxrpc.jar", "Y:/M3BE/FND/133/common/jcifs.jar", "Y:/M3BE/FND/133/common/json.jar", "Y:/M3BE/FND/133/common/jsr173_1.0_api.jar", "Y:/M3BE/FND/133/common/jt400.jar", "Y:/M3BE/FND/133/common/jtds.jar", "Y:/M3BE/FND/133/common/junit.jar", "Y:/M3BE/FND/133/common/log4j.jar", "Y:/M3BE/FND/133/common/mneMetadataUtil.jar", "Y:/M3BE/FND/133/common/mprolog.jar", "Y:/M3BE/FND/133/common/MvxMpdCommon.jar", "Y:/M3BE/FND/133/common/org.apache.commons.httpclient_3.1.0.v20080605-1935.jar", "Y:/M3BE/FND/133/common/org.jdom_1.0.0.v201005080400.jar", "Y:/M3BE/FND/133/common/PDFBox-0.7.3.jar", "Y:/M3BE/FND/133/common/saaj.jar", "Y:/M3BE/FND/133/common/servitrax.jar", "Y:/M3BE/FND/133/common/servlet.jar", "Y:/M3BE/FND/133/common/snmp.jar", "Y:/M3BE/FND/133/common/soap.jar", "Y:/M3BE/FND/133/common/sqljdbc4.jar", "Y:/M3BE/FND/133/common/stax-1.2.0.jar", "Y:/M3BE/FND/133/common/SysInfo.jar", "Y:/M3BE/FND/133/common/wsdl4j-1.5.1.jar", "Y:/M3BE/FND/133/common/xercesXercesImpl.jar", "Y:/M3BE/FND/133/common/xercesXmlApis.jar", "Y:/M3BE/FND/133/common/xmlenc.jar", "Y:/M3BE/env/PREP/Drivers/jdbc/axis2-jaxws-api-1.2.jar", "Y:/M3BE/env/PREP/Drivers/jdbc/Chronopost_2.0.jar", "Y:/M3BE/env/PREP/Drivers/jdbc/InterfaceM3OptiTime.V2_R4.jar", "Y:/M3BE/env/PREP/Drivers/jdbc/jaxb-api.jar", "Y:/M3BE/env/PREP/Drivers/jdbc/jaxb-impl-2.2.6.jar", "Y:/M3BE/env/PREP/Drivers/jdbc/jaxb-xjc-2.2.jar", "Y:/M3BE/env/PREP/Drivers/jdbc/jms.jar", "Y:/M3BE/env/PREP/Drivers/jdbc/neocaseClient.jar", "Y:/M3BE/env/PREP/Drivers/jdbc/opt-ffa-api-external-jdk5-8.0.2.4.jar", "Y:/M3BE/env/PREP/Drivers/jdbc/OAGI_JAXB.jar", "Y:/M3BE/env/PREP/Drivers/jdbc/tibcrypt.jar", "Y:/M3BE/env/PREP/Drivers/jdbc/tibjms.jar", "Y:/M3BE/env/PREP/Drivers/jdbc/tibjmsadmin.jar", "Y:/M3BE/env/PREP/Drivers/jdbc/tibjmsapps.jar", "Y:/M3BE/env/PREP/Drivers/jdbc/tibjmsufo.jar", "Y:/M3BE/env/PREP/Drivers/jdbc/tibrvjms.jar" }; public String[] listStd = { "Y:/M3BE/env/DVLP/Properties", "Y:/M3BE/env/DVLP/Fix/MVX/HFix/bindbg", "Y:/M3BE/env/DVLP/Fix/MVX/TFix/bindbg", "Y:/M3BE/env/DVLP/Fix/MVX/VFix/bindbg", "Y:/M3BE/MVX/1310/SP1310S51031/bindbg", "Y:/M3BE/MVX/1310/base/bindbg", "Y:/M3BE/env/DVLP/Fix/FND/TFix/Foundation", "Y:/M3BE/FND/133/Foundation.jar", "Y:/M3BE/FND/133/common/activation.jar", "Y:/M3BE/FND/133/common/adonix.jar", "Y:/M3BE/FND/133/common/asm.jar", "Y:/M3BE/FND/133/common/axis-ant.jar", "Y:/M3BE/FND/133/common/axis.jar", "Y:/M3BE/FND/133/common/backport-util-concurrent-3.1-osgi.jar", "Y:/M3BE/FND/133/common/commons-codec-1.3.jar", "Y:/M3BE/FND/133/common/commons-discovery-0.2.jar", "Y:/M3BE/FND/133/common/commons-lang-2.1.jar", "Y:/M3BE/FND/133/common/commons-logging-1.0.4.jar", "Y:/M3BE/FND/133/common/commons-net-2.0.jar", "Y:/M3BE/FND/133/common/commons-net-ftp-2.0.jar", "Y:/M3BE/FND/133/common/commonsCollections.jar", "Y:/M3BE/FND/133/common/commonsLogging.jar", "Y:/M3BE/FND/133/common/commonsPool.jar", "Y:/M3BE/FND/133/common/cryptixJceProvider.jar", "Y:/M3BE/FND/133/common/derby.jar", "Y:/M3BE/FND/133/common/freemarker.jar", "Y:/M3BE/FND/133/common/FndAPI.jar", "Y:/M3BE/FND/133/common/javamail.jar", "Y:/M3BE/FND/133/common/jaxrpc.jar", "Y:/M3BE/FND/133/common/jcifs.jar", "Y:/M3BE/FND/133/common/json.jar", "Y:/M3BE/FND/133/common/jsr173_1.0_api.jar", "Y:/M3BE/FND/133/common/jt400.jar", "Y:/M3BE/FND/133/common/jtds.jar", "Y:/M3BE/FND/133/common/junit.jar", "Y:/M3BE/FND/133/common/log4j.jar", "Y:/M3BE/FND/133/common/mneMetadataUtil.jar", "Y:/M3BE/FND/133/common/mprolog.jar", "Y:/M3BE/FND/133/common/MvxMpdCommon.jar", "Y:/M3BE/FND/133/common/org.apache.commons.httpclient_3.1.0.v20080605-1935.jar", "Y:/M3BE/FND/133/common/org.jdom_1.0.0.v201005080400.jar", "Y:/M3BE/FND/133/common/PDFBox-0.7.3.jar", "Y:/M3BE/FND/133/common/saaj.jar", "Y:/M3BE/FND/133/common/servitrax.jar", "Y:/M3BE/FND/133/common/servlet.jar", "Y:/M3BE/FND/133/common/snmp.jar", "Y:/M3BE/FND/133/common/soap.jar", "Y:/M3BE/FND/133/common/sqljdbc4.jar", "Y:/M3BE/FND/133/common/stax-1.2.0.jar", "Y:/M3BE/FND/133/common/SysInfo.jar", "Y:/M3BE/FND/133/common/wsdl4j-1.5.1.jar", "Y:/M3BE/FND/133/common/xercesXercesImpl.jar", "Y:/M3BE/FND/133/common/xercesXmlApis.jar", "Y:/M3BE/FND/133/common/xmlenc.jar", "Y:/M3BE/env/DVLP/Drivers/jdbc/axis2-jaxws-api-1.2.jar", "Y:/M3BE/env/DVLP/Drivers/jdbc/Chronopost_2.0.jar", "Y:/M3BE/env/DVLP/Drivers/jdbc/InterfaceM3OptiTime.V2_R4.jar", "Y:/M3BE/env/DVLP/Drivers/jdbc/jaxb-api.jar", "Y:/M3BE/env/DVLP/Drivers/jdbc/jaxb-impl-2.2.6.jar", "Y:/M3BE/env/DVLP/Drivers/jdbc/jaxb-xjc-2.2.jar", "Y:/M3BE/env/DVLP/Drivers/jdbc/jms.jar", "Y:/M3BE/env/DVLP/Drivers/jdbc/neocaseClient.jar", "Y:/M3BE/env/DVLP/Drivers/jdbc/opt-ffa-api-external-jdk5-8.0.2.4.jar", "Y:/M3BE/env/DVLP/Drivers/jdbc/OAGI_JAXB.jar", "Y:/M3BE/env/DVLP/Drivers/jdbc/tibcrypt.jar", "Y:/M3BE/env/DVLP/Drivers/jdbc/tibjms.jar", "Y:/M3BE/env/DVLP/Drivers/jdbc/tibjmsadmin.jar", "Y:/M3BE/env/DVLP/Drivers/jdbc/tibjmsapps.jar", "Y:/M3BE/env/DVLP/Drivers/jdbc/tibjmsufo.jar", "Y:/M3BE/env/DVLP/Drivers/jdbc/tibrvjms.jar" }; // private static final Logger logger. = Logger.getLogger(MovexClassLoader.class); private static final String FILE_EXTENSION_CLASS = "class"; private static final String FILE_EXTENSION_JAR = "jar"; private static final String FILE_EXTENSION_ZIP = "zip"; private static final String FILE_EXTENSION_PROPERTIES = "properties"; private List<File> m_classPaths; private Set<File> m_zipProblems; private Set<File> m_pathProblems; private ClassLoader urlclassLoader; public static Logger logger = LogManager.getLogManager().getLogger("M3ClassLoader"); public M3ClassLoader(M3ClassPathEntry[] classPath) { super(); this.m_classPaths = new ArrayList(); for (M3ClassPathEntry entry : classPath) { this.m_classPaths.add(entry.getPath().toFile()); } this.m_zipProblems = new HashSet(); this.m_pathProblems = new HashSet(); urlclassLoader = new URLClassLoader(createURLList(classPath)); MetaDataDef defSrc = getMetaDataDef(urlclassLoader, "OCUSMA"); } private void DBTest() throws MalformedURLException { ClassLoader loaderSpe = new URLClassLoader(createURLList(listSpe)); ClassLoader loaderStd = new URLClassLoader(createURLList(listStd)); MetaDataDef defSrc = getMetaDataDef(loaderSpe, "OCUSMA"); MetaDataDef defStd = getMetaDataDef(loaderStd, "OCUSMA"); } public URL[] createURLList(String[] list) { ArrayList<URL> ar = new ArrayList<>(); for (String s : list) { try { ar.add(new URL("file:/" + s)); } catch (MalformedURLException ex) { Logger.getLogger(M3ClassLoader.class.getName()).log(Level.SEVERE, null, ex); } } return ar.toArray(new URL[ar.size()]); } protected MetaDataDef getMetaDataDef(ClassLoader loader, String name) { try { return new MetaDataDef(loader, name); } catch (InstantiationException | IOException | IllegalAccessException | ClassNotFoundException ex) { System.err.println(ex); } return null; } public static URL[] createURLList(M3ClassPathEntry[] list) { File[] lst = new File[list.length]; for (int i = 0; i < list.length; i++) { lst[i] = list[i].getPath().toFile(); } return createURLList(lst); } public static URL[] createURLList(File[] list) { ArrayList<URL> ar = new ArrayList<>(); for (File s : list) { try { if (s.isFile()) { ar.add(new URL("file:/" + s.getAbsolutePath().replaceAll("\\\\", "/"))); System.out.println("file:/" + s.getAbsolutePath().replaceAll("\\\\", "/")); } else { ar.add(new URL("file:/" + s.getAbsolutePath().replaceAll("\\\\", "/") + "/")); System.out.println("file:/" + s.getAbsolutePath().replaceAll("\\\\", "/") + "/"); } } catch (MalformedURLException ex) { Logger.getLogger(AnalyzeDBTask.class.getName()).log(Level.SEVERE, null, ex); } } return ar.toArray(new URL[ar.size()]); } @Override public final URL getResource(String name) { return super.getResource(name); } @Override public final InputStream getResourceAsStream(String name) { return super.getResourceAsStream(name); } @Override public final Class findClass(String name) throws ClassNotFoundException { byte[] b = loadClassData(name); if (b == null) { throw new ClassNotFoundException(name); } return defineClass(name, b, 0, b.length); } private byte[] loadClassData(String name) { boolean classNotFound = true; byte[] bytes = (byte[]) null; int i = 0; label176: while ((classNotFound) && (i < this.m_classPaths.size())) { DataInputStream dis = null; try { File path = (File) this.m_classPaths.get(i++); if (this.m_pathProblems.contains(path)) { IOUtils.closeQuietly(dis); } else if (path.exists()) { dis = getStreamFromPath(name, path); if (dis != null) { classNotFound = false; bytes = getBytesFromStream(dis); break label176; } } else { logger.log(Level.WARNING, path + " could not be found/accessed on the M3 classpath"); this.m_pathProblems.add(path); } } catch (MAKException e) { logger.log(Level.SEVERE, "Failed to load class data from " + name, e); } finally { IOUtils.closeQuietly(dis); } } return bytes; } private DataInputStream getStreamFromPath(String name, File path) throws MAKException { Exception exception; try { DataInputStream dis = null; if (path.isDirectory()) { dis = getStreamFromFolder(name, path); } else if ((!path.getAbsolutePath().endsWith("jar")) && (!path.getAbsolutePath().endsWith("zip"))) { dis = getStreamFromJar(name, path); } return dis; } catch (IOException e) { exception = e; } catch (MAKException e) { exception = e; } throw new MAKException("Failed to get stream for " + name + " from " + path, exception); } private byte[] getBytesFromStream(DataInputStream dis) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; byte[] bytes = (byte[]) null; int len = 0; try { while ((len = dis.read(buf)) > 0) { bos.write(buf, 0, len); } bytes = bos.toByteArray(); } catch (IOException e) { logger.log(Level.SEVERE, "Failed to get bytes from stream", e); } finally { IOUtils.closeQuietly(bos); } return bytes; } private DataInputStream getStreamFromJar(String name, File path) throws MAKException { if (!this.m_zipProblems.contains(path)) { try { DataInputStream dis = null; ZipFile file = new ZipFile(path); ZipEntry entry = file.getEntry(name.replace('.', '/') + '.' + "class"); if (entry == null) { entry = file.getEntry(name.replace('.', '/') + '.' + "properties"); } if (entry != null) ; return new DataInputStream(file.getInputStream(entry)); } catch (IOException e) { logger.log(Level.SEVERE, "Failed to get " + name + " from jar " + path + " - MAK will try again using another method", e); this.m_zipProblems.add(path); } } return getStreamFromJar2(name, path); } private DataInputStream getStreamFromJar2(String name, File path) throws MAKException { Exception exception; try { DataInputStream dis = null; InputStream is = null; is = new FileInputStream(path); ZipInputStream zis = new ZipInputStream(is); ZipEntry entry = findZipEntry(name, zis); if (entry != null) ; return new DataInputStream(zis); } catch (FileNotFoundException e) { exception = e; } catch (MAKException e) { exception = e; } throw new MAKException("Failed to get " + name + " from jar " + path, exception); } private ZipEntry findZipEntry(String name, ZipInputStream zis) throws MAKException { try { ZipEntry entry = null; String tmp = name.replace('.', '/') + "."; String match1 = tmp + "class"; String match2 = tmp + "properties"; while ((entry = zis.getNextEntry()) != null) { if ((entry.getName().equals(match1)) || (entry.getName().equals(match2))) { return entry; } } return null; } catch (IOException e) { throw new MAKException("Failed to find entry " + name, e); } } private DataInputStream getStreamFromFolder(String name, File path) throws FileNotFoundException { DataInputStream dis = null; File file = new File( path.toString() + File.separator + name.replace('.', File.separatorChar) + '.' + "class"); if (!file.exists()) { file = new File( path.toString() + File.separator + name.replace('.', File.separatorChar) + '.' + "properties"); } if (file.exists()) { dis = new DataInputStream(new FileInputStream(file)); } return dis; } public ClassLoader getUrlclassLoader() { return urlclassLoader; } public void setUrlclassLoader(URLClassLoader urlclassLoader) { this.urlclassLoader = urlclassLoader; } }