1 45 46 package org.openejb.loader; 47 48 import org.openejb.loader.ClassPath; 49 import org.openejb.loader.SystemInstance; 50 import org.openejb.util.FileUtils; 51 52 import javax.servlet.ServletException ; 53 import java.lang.reflect.Method ; 54 import java.lang.reflect.InvocationTargetException ; 55 import java.util.Properties ; 56 import java.io.File ; 57 58 63 public class OpenEJBInstance { 64 private final Class openejb; 65 private final Method init; 66 private final Method isInitialized; 67 68 public OpenEJBInstance() throws Exception { 69 this.openejb = loadOpenEJBClass(); 70 this.init = openejb.getMethod("init", new Class []{Properties .class}); 71 this.isInitialized = openejb.getMethod("isInitialized", new Class []{}); 72 } 73 74 public void init(Properties props) throws Exception { 75 try { 76 init.invoke(null, new Object []{props}); 77 } catch (InvocationTargetException e) { 78 throw (Exception ) e.getCause(); 79 } catch (Exception e) { 80 throw new RuntimeException ("OpenEJB.init: ", e); 81 } 82 } 83 84 public boolean isInitialized() { 85 try { 86 Boolean b = (Boolean ) isInitialized.invoke(null, new Object []{}); 87 return b.booleanValue(); 88 } catch (InvocationTargetException e) { 89 throw new RuntimeException ("OpenEJB.isInitialized: ", e.getCause()); 90 } catch (Exception e) { 91 throw new RuntimeException ("OpenEJB.isInitialized: ", e); 92 } 93 } 94 95 private Class loadOpenEJBClass() throws Exception { 96 ClassPath classPath = SystemInstance.get().getClassPath(); 97 ClassLoader classLoader = classPath.getClassLoader(); 98 try { 99 return classLoader.loadClass("org.openejb.OpenEJB"); 100 } catch (Exception e) { 101 try { 102 checkOpenEjbHome(SystemInstance.get().getHome().getDirectory()); 103 FileUtils home = SystemInstance.get().getHome(); 104 classPath.addJarsToPath(home.getDirectory("lib")); 105 } catch (Exception e2) { 106 throw new Exception ("Could not load OpenEJB libraries. Exception: " + e2.getClass().getName() + " " + e2.getMessage()); 107 } 108 try { 109 return classLoader.loadClass("org.openejb.OpenEJB"); 110 } catch (Exception e2) { 111 throw new Exception ("Could not load OpenEJB class after embedding libraries. Exception: " + e2.getClass().getName() + " " + e2.getMessage()); 112 } 113 } 114 } 115 String NO_HOME = "The openejb.home is not set."; 116 117 String BAD_HOME = "Invalid openejb.home: "; 118 119 String NOT_THERE = "The path specified does not exist."; 120 121 String NOT_DIRECTORY = "The path specified is not a directory."; 122 123 String NO_DIST = "The path specified is not correct, it does not contain a 'dist' directory."; 124 125 String NO_LIBS = "The path specified is not correct, it does not contain any OpenEJB libraries."; 126 127 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."; 129 130 private void checkOpenEjbHome(File openejbHome) throws Exception { 131 try { 132 133 String homePath = openejbHome.getAbsolutePath(); 134 135 if (!openejbHome.exists()) 137 handleError(BAD_HOME + homePath, NOT_THERE, INSTRUCTIONS); 138 139 if (!openejbHome.isDirectory()) 141 handleError(BAD_HOME + homePath, NOT_DIRECTORY, INSTRUCTIONS); 142 143 File openejbHomeLibs = new File (openejbHome, "lib"); 145 if (!openejbHomeLibs.exists()) 146 handleError(BAD_HOME + homePath, NO_DIST, INSTRUCTIONS); 147 148 String [] libs = openejbHomeLibs.list(); 151 boolean found = false; 152 for (int i = 0; i < libs.length && !found; i++) { 153 found = (libs[i].startsWith("openejb-") && libs[i].endsWith(".jar")); 154 } 155 if (!found) 156 handleError(BAD_HOME + homePath, NO_LIBS, INSTRUCTIONS); 157 158 } catch (Exception e) { 159 e.printStackTrace(); 160 } 161 } 162 163 private void handleError(String m1, String m2, String m3) throws Exception { 164 System.err.println("--[PLEASE FIX]-------------------------------------"); 165 System.err.println(m1); 166 System.err.println(m2); 167 System.err.println(m3); 168 System.err.println("---------------------------------------------------"); 169 throw new Exception (m1 + " " + m2 + " " + m3); 170 } 171 172 private void handleError(String m1, String m2) throws Exception { 173 System.err.println("--[PLEASE FIX]-------------------------------------"); 174 System.err.println(m1); 175 System.err.println(m2); 176 System.err.println("---------------------------------------------------"); 177 throw new Exception (m1 + " " + m2); 178 } 179 180 } 181 | Popular Tags |