dblevins 2005/08/16 00:18:56
Modified: modules/core/src/java/org/openejb/alt/config
AutoDeployer.java ConfigurationFactory.java
Deploy.java EjbSet.java EjbValidator.java
Added: modules/core/src/java/org/openejb/alt/config
TempCodebase.java
Log:
Killed the temp codebase functionality from SafeToolkit. Added new class
TempCodebase which is not static. Refactored nearly all code using the
SafeToolkit.loadTempClass to use just a classloader instead. Code for
creating
"temp" jars for validation and other deployment related classloading is now in
one spot (instead of everywhere a class is loaded) and can now be safely
avoided
for unpacked ejb jars--we don't want to copy entire directories when we
validate
an unpacked ejb jar.
Revision Changes Path
1.7 +19 -11
openejb1/modules/core/src/java/org/openejb/alt/config/AutoDeployer.java
Index: AutoDeployer.java
===================================================================
RCS file:
/home/projects/openejb/scm/openejb1/modules/core/src/java/org/openejb/alt/config/AutoDeployer.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- AutoDeployer.java 16 Aug 2005 02:03:54 -0000 1.6
+++ AutoDeployer.java 16 Aug 2005 04:18:56 -0000 1.7
@@ -88,14 +88,12 @@
private String configFile;
private Container[] containers;
private Connector[] resources;
+ private ClassLoader classLoader;
+ private String jarLocation;
- public AutoDeployer() throws OpenEJBException {
- this(ConfigUtils.readConfig());
- }
-
public AutoDeployer(Openejb config) {
this.config = config;
-
+
/* Load container list */
this.containers = config.getContainer();
@@ -107,7 +105,9 @@
public void init() throws OpenEJBException {
}
- public OpenejbJar deploy(EjbJarUtils ejbJarUtils, String jarLocation)
throws OpenEJBException {
+ public OpenejbJar deploy(EjbJarUtils ejbJarUtils, String jarLocation,
ClassLoader classLoader) throws OpenEJBException {
+ this.jarLocation = jarLocation;
+ this.classLoader = classLoader;
OpenejbJar openejbJar = new OpenejbJar();
Bean[] beans = ejbJarUtils.getBeans();
@@ -139,14 +139,14 @@
if (bean.getType().equals("CMP_ENTITY")){
if (bean.getHome() != null){
- Class tempBean = SafeToolkit.loadTempClass(bean.getHome(),
jarLocation);
- if (hasFinderMethods(tempBean)){
+ Class tempBean = loadClass(bean.getHome());
+ if (hasFinderMethods(tempBean)){
throw new OpenEJBException("CMP 1.1 Beans with finder
methods cannot be autodeployed; finder methods require OQL Select statements
which cannot be generated accurately.");
}
}
if (bean.getLocalHome() != null){
- Class tempBean =
SafeToolkit.loadTempClass(bean.getLocalHome(), jarLocation);
- if (hasFinderMethods(tempBean)){
+ Class tempBean = loadClass(bean.getLocalHome());
+ if (hasFinderMethods(tempBean)){
throw new OpenEJBException("CMP 1.1 Beans with finder
methods cannot be autodeployed; finder methods require OQL Select statements
which cannot be generated accurately.");
}
}
@@ -155,7 +155,15 @@
return deployment;
}
- private boolean hasFinderMethods(Class bean)
+ private Class loadClass(String className) throws OpenEJBException {
+ try {
+ return classLoader.loadClass(className);
+ } catch (ClassNotFoundException cnfe) {
+ throw new OpenEJBException(SafeToolkit.messages.format("cl0007",
className, this.jarLocation));
+ }
+ }
+
+ private boolean hasFinderMethods(Class bean)
throws OpenEJBException {
Method[] methods = bean.getMethods();
1.19 +5 -3
openejb1/modules/core/src/java/org/openejb/alt/config/ConfigurationFactory.java
Index: ConfigurationFactory.java
===================================================================
RCS file:
/home/projects/openejb/scm/openejb1/modules/core/src/java/org/openejb/alt/config/ConfigurationFactory.java,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- ConfigurationFactory.java 16 Aug 2005 01:05:03 -0000 1.18
+++ ConfigurationFactory.java 16 Aug 2005 04:18:56 -0000 1.19
@@ -1245,15 +1245,17 @@
try {
EjbJarUtils ejbJarUtils = new EjbJarUtils(jarLocation);
EjbJar ejbJar = ejbJarUtils.getEjbJar();
+ TempCodebase tempCodebase = new TempCodebase(jarLocation);
+ ClassLoader classLoader = tempCodebase.getClassLoader();
/* If there is no openejb-jar.xml attempt to auto deploy it.
*/
OpenejbJar openejbJar = ejbJarUtils.getOpenejbJar();
if (openejbJar == null) {
- openejbJar = deployer.deploy(ejbJarUtils, jarLocation);
+ openejbJar = deployer.deploy(ejbJarUtils, jarLocation,
classLoader);
}
- EjbSet set = validator.validateJar( ejbJarUtils );
+ EjbSet set = validator.validateJar( ejbJarUtils,
classLoader);
if (set.hasErrors() || set.hasFailures()) {
//System.out.println("[] INVALID "+ jarLocation);
throw new OpenEJBException("Jar failed validation. Use
the validation tool for more details");
1.12 +32 -6
openejb1/modules/core/src/java/org/openejb/alt/config/Deploy.java
Index: Deploy.java
===================================================================
RCS file:
/home/projects/openejb/scm/openejb1/modules/core/src/java/org/openejb/alt/config/Deploy.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- Deploy.java 16 Aug 2005 02:03:54 -0000 1.11
+++ Deploy.java 16 Aug 2005 04:18:56 -0000 1.12
@@ -15,7 +15,7 @@
* 3. The name "OpenEJB" must not be used to endorse or promote
* products derived from this Software without prior written
* permission of The OpenEJB Group. For written permission,
- * please contact [EMAIL PROTECTED]
+ * please contact [EMAIL PROTECTED]
*
* 4. Products derived from this Software may not be called "OpenEJB"
* nor may "OpenEJB" appear in their names without prior written
@@ -23,7 +23,7 @@
* trademark of The OpenEJB Group.
*
* 5. Due credit should be given to the OpenEJB Project
- * (http://www.openejb.org/).
+ * (http://openejb.org/).
*
* THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
@@ -50,8 +50,11 @@
import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
+import java.net.URLClassLoader;
+import java.net.MalformedURLException;
import java.util.Properties;
import java.util.StringTokenizer;
+import java.util.HashMap;
import org.openejb.OpenEJBException;
import org.openejb.loader.SystemInstance;
@@ -69,6 +72,7 @@
import org.openejb.util.Messages;
import org.openejb.util.SafeToolkit;
import org.openejb.util.Logger;
+import org.openejb.util.FileUtils;
/**
* This class represents a command line tool for deploying beans.
@@ -221,6 +225,8 @@
private boolean autoAssign;
private Container[] containers;
private Connector[] resources;
+ private ClassLoader classLoader;
+ private String jarLocation;
/*------------------------------------------------------*/
/* Constructors */
@@ -271,10 +277,22 @@
/*------------------------------------------------------*/
private void deploy(String jarLocation) throws OpenEJBException {
+
+ this.jarLocation = jarLocation;
EjbJarUtils ejbJarUtils = new EjbJarUtils(jarLocation);
EjbValidator validator = new EjbValidator();
- EjbSet set = validator.validateJar(ejbJarUtils);
+
+ classLoader = null;
+ try {
+ File jarFile = new File(ejbJarUtils.getJarLocation());
+ URL[] classpath = new URL[]{jarFile.toURL()};
+ classLoader = new URLClassLoader(classpath,
this.getClass().getClassLoader());
+ } catch (MalformedURLException e) {
+ throw new OpenEJBException("Unable to create a classloader to
load classes from '"+jarLocation+"'", e);
+ }
+
+ EjbSet set = validator.validateJar(ejbJarUtils, classLoader);
if (set.hasErrors() || set.hasFailures()) {
validator.printResults(set);
@@ -358,13 +376,13 @@
//check for OQL statement
if (bean.getType().equals("CMP_ENTITY")){
if (bean.getHome() != null){
- Class tempBean = SafeToolkit.loadTempClass(bean.getHome(),
jarLocation);
+ Class tempBean = loadClass(bean.getHome());
if (hasFinderMethods(tempBean)){
promptForOQLForEntityBeans(tempBean, deployment);
}
}
if (bean.getLocalHome() != null){
- Class tempBean =
SafeToolkit.loadTempClass(bean.getLocalHome(), jarLocation);
+ Class tempBean = loadClass(bean.getLocalHome());
if (hasFinderMethods(tempBean)){
promptForOQLForEntityBeans(tempBean, deployment);
}
@@ -372,6 +390,14 @@
}
return deployment;
+ }
+
+ private Class loadClass(String className) throws OpenEJBException {
+ try {
+ return classLoader.loadClass(className);
+ } catch (ClassNotFoundException cnfe) {
+ throw new OpenEJBException(SafeToolkit.messages.format("cl0007",
className, this.jarLocation));
+ }
}
private boolean hasFinderMethods(Class bean)
1.5 +15 -2
openejb1/modules/core/src/java/org/openejb/alt/config/EjbSet.java
Index: EjbSet.java
===================================================================
RCS file:
/home/projects/openejb/scm/openejb1/modules/core/src/java/org/openejb/alt/config/EjbSet.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- EjbSet.java 4 Aug 2005 01:20:06 -0000 1.4
+++ EjbSet.java 16 Aug 2005 04:18:56 -0000 1.5
@@ -47,6 +47,7 @@
import java.util.Vector;
import org.openejb.alt.config.ejb11.EjbJar;
+import org.openejb.OpenEJBException;
/**
* @author <a href="mailto:[EMAIL PROTECTED]">David Blevins</a>
@@ -61,11 +62,23 @@
private final EjbJar jar;
private final Bean[] beans;
- public EjbSet(String jarPath, EjbJar jar, Bean[] beans) {
+ private final ClassLoader classLoader;
+
+ public EjbSet(String jarPath, EjbJar jar, Bean[] beans, ClassLoader
classLoader) {
this.jarPath = jarPath;
this.jar = jar;
this.beans = beans;
+ this.classLoader = classLoader;
+ }
+
+ public ClassLoader getClassLoader() {
+ return classLoader;
}
+
+ public EjbJar getJar() {
+ return jar;
+ }
+
public void addWarning(ValidationWarning warning) {
warnings.addElement(warning);
1.11 +15 -4
openejb1/modules/core/src/java/org/openejb/alt/config/EjbValidator.java
Index: EjbValidator.java
===================================================================
RCS file:
/home/projects/openejb/scm/openejb1/modules/core/src/java/org/openejb/alt/config/EjbValidator.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- EjbValidator.java 4 Aug 2005 01:20:06 -0000 1.10
+++ EjbValidator.java 16 Aug 2005 04:18:56 -0000 1.11
@@ -47,6 +47,8 @@
import java.io.InputStream;
import java.io.File;
import java.net.URL;
+import java.net.URLClassLoader;
+import java.net.MalformedURLException;
import java.util.Properties;
import java.util.Vector;
@@ -91,11 +93,11 @@
return ejbSets;
}
- public EjbSet validateJar(EjbJarUtils ejbJarUtils){
+ public EjbSet validateJar(EjbJarUtils ejbJarUtils, ClassLoader
classLoader){
EjbSet set = null;
try {
- set = new EjbSet(ejbJarUtils.getJarLocation(),
ejbJarUtils.getEjbJar(), ejbJarUtils.getBeans());
+ set = new EjbSet(ejbJarUtils.getJarLocation(),
ejbJarUtils.getEjbJar(), ejbJarUtils.getBeans(), classLoader);
ValidationRule[] rules = getValidationRules();
for (int i=0; i < rules.length; i++){
rules[i].validate( set );
@@ -342,7 +344,16 @@
for (; i < args.length; i++){
try{
EjbJarUtils ejbJarUtils = new
EjbJarUtils(args[i]);
- EjbSet set = v.validateJar( ejbJarUtils );
+ String jarLocation =
ejbJarUtils.getJarLocation();
+ ClassLoader classLoader = null;
+ try {
+ File jarFile = new File(jarLocation);
+ URL[] classpath = new URL[]{jarFile.toURL()};
+ classLoader = new URLClassLoader(classpath,
EjbValidator.class.getClassLoader());
+ } catch (MalformedURLException e) {
+ throw new OpenEJBException("Unable to create
a classloader to load classes from '"+jarLocation+"'", e);
+ }
+ EjbSet set = v.validateJar( ejbJarUtils,
classLoader);
v.addEjbSet( set );
} catch (Exception e){
e.printStackTrace();
1.1
openejb1/modules/core/src/java/org/openejb/alt/config/TempCodebase.java
Index: TempCodebase.java
===================================================================
/**
* Redistribution and use of this software and associated documentation
* ("Software"), with or without modification, are permitted provided
* that the following conditions are met:
*
* 1. Redistributions of source code must retain copyright
* statements and notices. Redistributions must also contain a
* copy of this document.
*
* 2. Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. The name "OpenEJB" must not be used to endorse or promote
* products derived from this Software without prior written
* permission of The OpenEJB Group. For written permission,
* please contact [EMAIL PROTECTED]
*
* 4. Products derived from this Software may not be called "OpenEJB"
* nor may "OpenEJB" appear in their names without prior written
* permission of The OpenEJB Group. OpenEJB is a registered
* trademark of The OpenEJB Group.
*
* 5. Due credit should be given to the OpenEJB Project
* (http://openejb.org/).
*
* THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Copyright 2005 (C) The OpenEJB Group. All Rights Reserved.
*
* $Id: TempCodebase.java,v 1.1 2005/08/16 04:18:56 dblevins Exp $
*/
package org.openejb.alt.config;
import org.openejb.OpenEJBException;
import org.openejb.util.SafeToolkit;
import org.openejb.util.FileUtils;
import java.util.HashMap;
import java.net.URL;
import java.io.File;
/**
* @version $Revision: 1.1 $ $Date: 2005/08/16 04:18:56 $
*/
public class TempCodebase {
protected static final HashMap tempCodebases = new HashMap();
private final String codebase;
private final ClassLoader classLoader;
public TempCodebase(String codebase) throws OpenEJBException {
this.codebase = codebase;
ClassLoader cl = null;
try {
URL[] urlCodebase = new URL[1];
urlCodebase[0] = createTempCopy(codebase).toURL();
cl = new java.net.URLClassLoader(urlCodebase,
TempCodebase.class.getClassLoader());
} catch (java.net.MalformedURLException mue) {
throw new OpenEJBException(SafeToolkit.messages.format("cl0001",
codebase, mue.getMessage()));
} catch (SecurityException se) {
throw new OpenEJBException(SafeToolkit.messages.format("cl0002",
codebase, se.getMessage()));
}
this.classLoader = cl;
}
public String getCodebase() {
return codebase;
}
public ClassLoader getClassLoader() {
return classLoader;
}
public static TempCodebase getTempCodebase(String codebase) throws
OpenEJBException {
if (codebase == null) {
codebase = "CLASSPATH";
}
TempCodebase tempCodebase = (TempCodebase)tempCodebases.get(codebase);
if (tempCodebase == null){
tempCodebase = new TempCodebase(codebase);
tempCodebases.put(codebase, tempCodebase);
}
return tempCodebase;
}
public Class loadClass(String className) throws OpenEJBException {
ClassLoader cl = getClassLoader();
Class clazz = null;
try {
clazz = cl.loadClass(className);
} catch (ClassNotFoundException cnfe) {
throw new OpenEJBException(SafeToolkit.messages.format("cl0007",
className, codebase));
}
return clazz;
}
public static void unloadTempCodebase(String codebase) {
//TODO Delete temp jar
tempCodebases.remove(codebase);
}
/**
* Ensures that a class loader for each code base used in the
* system is created at most one time. The default bootsrap
* classloader is used if codebase is null.
*
* @param codebase
* @return ClassLoader
* @throws org.openejb.OpenEJBException
*/
protected static ClassLoader getCodebaseTempClassLoader(String codebase)
throws OpenEJBException {
if (codebase == null) codebase = "CLASSPATH";
ClassLoader cl = (ClassLoader) tempCodebases.get(codebase);
if (cl == null) {
synchronized (SafeToolkit.codebases) {
cl = (ClassLoader) SafeToolkit.codebases.get(codebase);
if (cl == null) {
try {
URL[] urlCodebase = new URL[1];
urlCodebase[0] = createTempCopy(codebase).toURL();
// make sure everything works if we were not loaded by the system class loader
cl = new java.net.URLClassLoader(urlCodebase,
SafeToolkit.class.getClassLoader());
tempCodebases.put(codebase, cl);
} catch (java.net.MalformedURLException mue) {
throw new
OpenEJBException(SafeToolkit.messages.format("cl0001", codebase,
mue.getMessage()));
} catch (SecurityException se) {
throw new
OpenEJBException(SafeToolkit.messages.format("cl0002", codebase,
se.getMessage()));
}
}
}
}
return cl;
}
/**
* Ensures that a class loader for each code base used in the
* system is created at most one time. The default bootsrap
* classloader is used if codebase is null.
*
* @param codebase
* @return ClassLoader
* @throws org.openejb.OpenEJBException
*/
protected static ClassLoader getTempClassLoader(String codebase) throws
OpenEJBException {
ClassLoader cl = null;
try {
URL[] urlCodebase = new URL[1];
urlCodebase[0] = createTempCopy(codebase).toURL();
// make sure everything works if we were not loaded by the system
class loader
cl = new java.net.URLClassLoader(urlCodebase,
SafeToolkit.class.getClassLoader());
} catch (java.net.MalformedURLException mue) {
throw new OpenEJBException(SafeToolkit.messages.format("cl0001",
codebase, mue.getMessage()));
} catch (SecurityException se) {
throw new OpenEJBException(SafeToolkit.messages.format("cl0002",
codebase, se.getMessage()));
}
return cl;
}
protected static File createTempCopy(String codebase) throws
OpenEJBException {
File file = null;
try {
File codebaseFile = new File(codebase);
// if (codebaseFile.isDirectory()) return codebaseFile;
file = File.createTempFile("openejb_validate", ".jar", null);
file.deleteOnExit();
FileUtils.copyFile(file, codebaseFile);
} catch (Exception e) {
throw new OpenEJBException(SafeToolkit.messages.format("cl0002",
codebase, e.getMessage()));
}
return file;
}
}