KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > loader > OpenEJBInstance


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact info@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2005 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: OpenEJBInstance.java 2113 2005-08-26 21:50:13Z dblevins $
44  */

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 JavaDoc;
53 import java.lang.reflect.Method JavaDoc;
54 import java.lang.reflect.InvocationTargetException JavaDoc;
55 import java.util.Properties JavaDoc;
56 import java.io.File JavaDoc;
57
58 /**
59  * This class can exist in several child classloaders
60  * Specifically in the tomcat webapp style, this class
61  * is loaded several times.
62  */

63 public class OpenEJBInstance {
64     private final Class JavaDoc openejb;
65     private final Method JavaDoc init;
66     private final Method JavaDoc isInitialized;
67
68     public OpenEJBInstance() throws Exception JavaDoc {
69         this.openejb = loadOpenEJBClass();
70         this.init = openejb.getMethod("init", new Class JavaDoc[]{Properties JavaDoc.class});
71         this.isInitialized = openejb.getMethod("isInitialized", new Class JavaDoc[]{});
72     }
73
74     public void init(Properties JavaDoc props) throws Exception JavaDoc {
75         try {
76             init.invoke(null, new Object JavaDoc[]{props});
77         } catch (InvocationTargetException JavaDoc e) {
78             throw (Exception JavaDoc) e.getCause();
79         } catch (Exception JavaDoc e) {
80             throw new RuntimeException JavaDoc("OpenEJB.init: ", e);
81         }
82     }
83
84     public boolean isInitialized() {
85         try {
86             Boolean JavaDoc b = (Boolean JavaDoc) isInitialized.invoke(null, new Object JavaDoc[]{});
87             return b.booleanValue();
88         } catch (InvocationTargetException JavaDoc e) {
89             throw new RuntimeException JavaDoc("OpenEJB.isInitialized: ", e.getCause());
90         } catch (Exception JavaDoc e) {
91             throw new RuntimeException JavaDoc("OpenEJB.isInitialized: ", e);
92         }
93     }
94
95     private Class JavaDoc loadOpenEJBClass() throws Exception JavaDoc {
96         ClassPath classPath = SystemInstance.get().getClassPath();
97         ClassLoader JavaDoc classLoader = classPath.getClassLoader();
98         try {
99             return classLoader.loadClass("org.openejb.OpenEJB");
100         } catch (Exception JavaDoc e) {
101             try {
102                 checkOpenEjbHome(SystemInstance.get().getHome().getDirectory());
103                 FileUtils home = SystemInstance.get().getHome();
104                 classPath.addJarsToPath(home.getDirectory("lib"));
105             } catch (Exception JavaDoc e2) {
106                 throw new Exception JavaDoc("Could not load OpenEJB libraries. Exception: " + e2.getClass().getName() + " " + e2.getMessage());
107             }
108             try {
109                 return classLoader.loadClass("org.openejb.OpenEJB");
110             } catch (Exception JavaDoc e2) {
111                 throw new Exception JavaDoc("Could not load OpenEJB class after embedding libraries. Exception: " + e2.getClass().getName() + " " + e2.getMessage());
112             }
113         }
114     }
115     String JavaDoc NO_HOME = "The openejb.home is not set.";
116
117     String JavaDoc BAD_HOME = "Invalid openejb.home: ";
118
119     String JavaDoc NOT_THERE = "The path specified does not exist.";
120
121     String JavaDoc NOT_DIRECTORY = "The path specified is not a directory.";
122
123     String JavaDoc NO_DIST = "The path specified is not correct, it does not contain a 'dist' directory.";
124
125     String JavaDoc NO_LIBS = "The path specified is not correct, it does not contain any OpenEJB libraries.";
126
127     // TODO: move this part back into the LoaderServlet
128
String JavaDoc 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 JavaDoc openejbHome) throws Exception JavaDoc {
131         try {
132
133             String JavaDoc homePath = openejbHome.getAbsolutePath();
134             
135             // The openejb.home must exist
136
if (!openejbHome.exists())
137                 handleError(BAD_HOME + homePath, NOT_THERE, INSTRUCTIONS);
138
139             // The openejb.home must be a directory
140
if (!openejbHome.isDirectory())
141                 handleError(BAD_HOME + homePath, NOT_DIRECTORY, INSTRUCTIONS);
142
143             // The openejb.home must contain a 'lib' directory
144
File JavaDoc openejbHomeLibs = new File JavaDoc(openejbHome, "lib");
145             if (!openejbHomeLibs.exists())
146                 handleError(BAD_HOME + homePath, NO_DIST, INSTRUCTIONS);
147
148             // The openejb.home there must be openejb*.jar files in the 'dist'
149
// directory
150
String JavaDoc[] 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 JavaDoc e) {
159             e.printStackTrace();
160         }
161     }
162
163     private void handleError(String JavaDoc m1, String JavaDoc m2, String JavaDoc m3) throws Exception JavaDoc {
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 JavaDoc(m1 + " " + m2 + " " + m3);
170     }
171
172     private void handleError(String JavaDoc m1, String JavaDoc m2) throws Exception JavaDoc {
173         System.err.println("--[PLEASE FIX]-------------------------------------");
174         System.err.println(m1);
175         System.err.println(m2);
176         System.err.println("---------------------------------------------------");
177         throw new Exception JavaDoc(m1 + " " + m2);
178     }
179
180 }
181
Popular Tags