001: package salomeTMF_plug.simpleJunit;
002:
003: import java.io.File;
004: import java.lang.reflect.Constructor;
005: import java.net.URL;
006: import java.util.Enumeration;
007: import java.util.Hashtable;
008:
009: import org.objectweb.salome_tmf.api.Util;
010: import org.objectweb.salome_tmf.api.data.SalomeFileWrapper;
011: import org.objectweb.salome_tmf.data.Attachment;
012: import org.objectweb.salome_tmf.data.AutomaticTest;
013: import org.objectweb.salome_tmf.data.FileAttachment;
014: import org.objectweb.salome_tmf.ihm.main.SalomeTMFContext;
015: import org.objectweb.salome_tmf.ihm.main.datawrapper.DataModel;
016:
017: public class JarUtil {
018:
019: SimpleJunitPlugin pSimpleJunitPlugin;
020:
021: JarUtil(SimpleJunitPlugin pSimpleJunitPlugin) {
022: this .pSimpleJunitPlugin = pSimpleJunitPlugin;
023: }
024:
025: /**
026: * Load the referenced in the attachement in the class loader
027: * @param pJarAttach
028: * @return
029: * @throws Exception
030: */
031: ClassLoader loadJar(Attachment pJarAttach) throws Exception {
032: URL UrlLib = null;
033: if (pJarAttach instanceof FileAttachment) {
034: Util
035: .log("[SimpleJunitPlugin:loadJar] Chargement du fichier dans tmp");
036: File pFile = ((FileAttachment) pJarAttach).getFileFromDB();
037: if (pFile == null) {
038: Util
039: .log("[SimpleJunitPlugin:loadJar] File attachment no found in DB"
040: + pJarAttach.getNameFromModel());
041: throw new Exception("File attachment no found in DB");
042: }
043: //UrlLib = new URL(pFile.getProtocol(), pFile.getHost(), pFile.getPort(), pFile.getFileName());
044: UrlLib = pFile.toURL();
045: } else {
046: UrlLib = new URL(pJarAttach.getNameFromModel());
047: }
048: Util.log("[SimpleJunitPlugin:loadJar] URL TO LOAD = " + UrlLib);
049: SalomeTMFContext.getInstance().getSalomeClassLoader().AddJar(
050: UrlLib);
051: return SalomeTMFContext.getInstance().getSalomeClassLoader();
052: }
053:
054: ClassLoader loadJar(String strID, String strAttachment,
055: String testScript, AutomaticTest pTest) throws Exception {
056:
057: Hashtable tabAttach = new Hashtable(DataModel
058: .getCurrentProject().getAttachmentMapFromModel());
059: Enumeration e = tabAttach.elements();
060: Attachment pJarAttach = null;
061: boolean c = true;
062: while (e.hasMoreElements() && c) {
063: Attachment pAttach = (Attachment) e.nextElement();
064: String id = "" + pAttach.getIdBdd();
065: if (id.trim().equals(strID.trim())) {
066: pJarAttach = pAttach;
067: c = false;
068: }
069: }
070: if (pJarAttach == null) {
071: Util
072: .log("[SimpleJunitPlugin:loadJar] File attachment no found in DB"
073: + strAttachment);
074: strID = pSimpleJunitPlugin.updateTestScript(testScript,
075: pTest);
076: tabAttach = new Hashtable(DataModel.getCurrentProject()
077: .getAttachmentMapFromModel());
078: e = tabAttach.elements();
079: pJarAttach = null;
080: c = true;
081: while (e.hasMoreElements() && c) {
082: Attachment pAttach = (Attachment) e.nextElement();
083: String id = "" + pAttach.getIdBdd();
084: if (id.trim().equals(strID.trim())) {
085: pJarAttach = pAttach;
086: c = false;
087: }
088: }
089: if (pJarAttach == null) {
090: throw new Exception("File attachment no found in DB");
091: }
092: }
093: URL UrlLib = null;
094:
095: if (pJarAttach instanceof FileAttachment) {
096: Util
097: .log("[SimpleJunitPlugin:loadJar] Chargement du fichier dans tmp");
098: File pFile = ((FileAttachment) pJarAttach).getFileFromDB();
099: if (pFile == null) {
100: Util
101: .log("[SimpleJunitPlugin:loadJar] File attachment no found in DB"
102: + strAttachment);
103: throw new Exception("File attachment no found in DB");
104: }
105: //UrlLib = new URL(pFile.getProtocol(), pFile.getHost(), pFile.getPort(), pFile.getFileName());
106: UrlLib = pFile.toURL();
107: } else {
108: UrlLib = new URL(pJarAttach.getNameFromModel());
109: }
110: Util.log("[SimpleJunitPlugin:loadJar] URL TO LOAD = " + UrlLib);
111:
112: SalomeTMFContext.getInstance().getSalomeClassLoader().AddJar(
113: UrlLib);
114: return SalomeTMFContext.getInstance().getSalomeClassLoader();
115: }
116:
117: Constructor loadClass(String strClass) throws Exception {
118: Constructor pConstructor = null;
119: Class pTestClass = Class.forName(strClass);
120: Class[] parameterTypes = new Class[1];
121: parameterTypes[0] = Hashtable.class;
122: pConstructor = pTestClass.getConstructor(parameterTypes);
123: return pConstructor;
124: }
125:
126: }
|