KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  *
3  * Copyright 2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.openejb.loader;
18
19 import org.openejb.util.FileUtils;
20
21 import java.io.File JavaDoc;
22
23 /**
24  * @version $Revision$ $Date$
25  */

26 public class Embedder {
27
28     private final String JavaDoc className;
29
30     public Embedder(String JavaDoc className) {
31         this.className = className;
32     }
33
34     public Class JavaDoc load() throws Exception JavaDoc {
35         ClassPath classPath = SystemInstance.get().getClassPath();
36         ClassLoader JavaDoc classLoader = classPath.getClassLoader();
37         try {
38             return classLoader.loadClass(className);
39         } catch (Exception JavaDoc e) {
40             return forcefulLoad(classPath, classLoader);
41         }
42     }
43
44     private Class JavaDoc forcefulLoad(ClassPath classPath, ClassLoader JavaDoc classLoader) throws Exception JavaDoc {
45         try {
46             checkOpenEjbHome(SystemInstance.get().getHome().getDirectory());
47             FileUtils home = SystemInstance.get().getHome();
48             classPath.addJarsToPath(home.getDirectory("lib"));
49         } catch (Exception JavaDoc e2) {
50             throw new Exception JavaDoc("Could not load OpenEJB libraries. Exception: " + e2.getClass().getName() + " " + e2.getMessage());
51         }
52         try {
53             return classLoader.loadClass(className);
54         } catch (Exception JavaDoc e2) {
55             throw new Exception JavaDoc("Could not load class '"+className+"' after embedding libraries. Exception: " + e2.getClass().getName() + " " + e2.getMessage());
56         }
57     }
58
59     private String JavaDoc NO_HOME = "The openejb.home is not set.";
60
61     private String JavaDoc BAD_HOME = "Invalid openejb.home: ";
62
63     private String JavaDoc NOT_THERE = "The path specified does not exist.";
64
65     private String JavaDoc NOT_DIRECTORY = "The path specified is not a directory.";
66
67     private String JavaDoc NO_LIBS = "The path specified is not correct, it does not contain any OpenEJB libraries.";
68
69     // TODO: move this part back into the LoaderServlet
70
private 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.";
71
72     private void checkOpenEjbHome(File JavaDoc openejbHome) throws Exception JavaDoc {
73         try {
74
75             String JavaDoc homePath = openejbHome.getAbsolutePath();
76
77             // The openejb.home must exist
78
if (!openejbHome.exists())
79                 handleError(BAD_HOME + homePath, NOT_THERE, INSTRUCTIONS);
80
81             // The openejb.home must be a directory
82
if (!openejbHome.isDirectory())
83                 handleError(BAD_HOME + homePath, NOT_DIRECTORY, INSTRUCTIONS);
84
85             // The openejb.home must contain a 'lib' directory
86
File JavaDoc openejbHomeLibs = new File JavaDoc(openejbHome, "lib");
87             if (!openejbHomeLibs.exists())
88                 handleError(BAD_HOME + homePath, NO_LIBS, INSTRUCTIONS);
89
90             // The openejb.home there must be openejb*.jar files in the 'dist'
91
// directory
92
String JavaDoc[] 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 JavaDoc e) {
101             e.printStackTrace();
102         }
103     }
104
105     private void handleError(String JavaDoc m1, String JavaDoc m2, String JavaDoc m3) throws Exception JavaDoc {
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 JavaDoc(m1 + " " + m2 + " " + m3);
112     }
113
114     private void handleError(String JavaDoc m1, String JavaDoc m2) throws Exception JavaDoc {
115         System.err.println("--[PLEASE FIX]-------------------------------------");
116         System.err.println(m1);
117         System.err.println(m2);
118         System.err.println("---------------------------------------------------");
119         throw new Exception JavaDoc(m1 + " " + m2);
120     }
121
122 }
123
Popular Tags