1 17 package org.openejb.loader; 18 19 import org.openejb.util.FileUtils; 20 21 import java.io.File ; 22 23 26 public class Embedder { 27 28 private final String className; 29 30 public Embedder(String className) { 31 this.className = className; 32 } 33 34 public Class load() throws Exception { 35 ClassPath classPath = SystemInstance.get().getClassPath(); 36 ClassLoader classLoader = classPath.getClassLoader(); 37 try { 38 return classLoader.loadClass(className); 39 } catch (Exception e) { 40 return forcefulLoad(classPath, classLoader); 41 } 42 } 43 44 private Class forcefulLoad(ClassPath classPath, ClassLoader classLoader) throws Exception { 45 try { 46 checkOpenEjbHome(SystemInstance.get().getHome().getDirectory()); 47 FileUtils home = SystemInstance.get().getHome(); 48 classPath.addJarsToPath(home.getDirectory("lib")); 49 } catch (Exception e2) { 50 throw new Exception ("Could not load OpenEJB libraries. Exception: " + e2.getClass().getName() + " " + e2.getMessage()); 51 } 52 try { 53 return classLoader.loadClass(className); 54 } catch (Exception e2) { 55 throw new Exception ("Could not load class '"+className+"' after embedding libraries. Exception: " + e2.getClass().getName() + " " + e2.getMessage()); 56 } 57 } 58 59 private String NO_HOME = "The openejb.home is not set."; 60 61 private String BAD_HOME = "Invalid openejb.home: "; 62 63 private String NOT_THERE = "The path specified does not exist."; 64 65 private String NOT_DIRECTORY = "The path specified is not a directory."; 66 67 private String NO_LIBS = "The path specified is not correct, it does not contain any OpenEJB libraries."; 68 69 private String INSTRUCTIONS = "Please edit the web.xml of the openejb_loader webapp and set the openejb.home init-param to the full path where OpenEJB is installed."; 71 72 private void checkOpenEjbHome(File openejbHome) throws Exception { 73 try { 74 75 String homePath = openejbHome.getAbsolutePath(); 76 77 if (!openejbHome.exists()) 79 handleError(BAD_HOME + homePath, NOT_THERE, INSTRUCTIONS); 80 81 if (!openejbHome.isDirectory()) 83 handleError(BAD_HOME + homePath, NOT_DIRECTORY, INSTRUCTIONS); 84 85 File openejbHomeLibs = new File (openejbHome, "lib"); 87 if (!openejbHomeLibs.exists()) 88 handleError(BAD_HOME + homePath, NO_LIBS, INSTRUCTIONS); 89 90 String [] libs = openejbHomeLibs.list(); 93 boolean found = false; 94 for (int i = 0; i < libs.length && !found; i++) { 95 found = (libs[i].startsWith("openejb-") && libs[i].endsWith(".jar")); 96 } 97 if (!found) 98 handleError(BAD_HOME + homePath, NO_LIBS, INSTRUCTIONS); 99 100 } catch (Exception e) { 101 e.printStackTrace(); 102 } 103 } 104 105 private void handleError(String m1, String m2, String m3) throws Exception { 106 System.err.println("--[PLEASE FIX]-------------------------------------"); 107 System.err.println(m1); 108 System.err.println(m2); 109 System.err.println(m3); 110 System.err.println("---------------------------------------------------"); 111 throw new Exception (m1 + " " + m2 + " " + m3); 112 } 113 114 private void handleError(String m1, String m2) throws Exception { 115 System.err.println("--[PLEASE FIX]-------------------------------------"); 116 System.err.println(m1); 117 System.err.println(m2); 118 System.err.println("---------------------------------------------------"); 119 throw new Exception (m1 + " " + m2); 120 } 121 122 } 123 | Popular Tags |