You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by db...@apache.org on 2007/01/13 04:44:27 UTC
svn commit: r495832 - in
/incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/alt/config:
ConfigurationFactory.java DeploymentLoader.java DeploymentsResolver.java
JarExtractor.java
Author: dblevins
Date: Fri Jan 12 19:44:26 2007
New Revision: 495832
URL: http://svn.apache.org/viewvc?view=rev&rev=495832
Log:
More hammering on ConfigFactory and co. Trying to sort the code out into more focused buckets to be later inlined.
Added:
incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/alt/config/DeploymentsResolver.java
incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/alt/config/JarExtractor.java
Modified:
incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/alt/config/ConfigurationFactory.java
incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/alt/config/DeploymentLoader.java
Modified: incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/alt/config/ConfigurationFactory.java
URL: http://svn.apache.org/viewvc/incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/alt/config/ConfigurationFactory.java?view=diff&rev=495832&r1=495831&r2=495832
==============================================================================
--- incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/alt/config/ConfigurationFactory.java (original)
+++ incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/alt/config/ConfigurationFactory.java Fri Jan 12 19:44:26 2007
@@ -21,7 +21,6 @@
import org.apache.openejb.alt.config.sys.ConnectionManager;
import org.apache.openejb.alt.config.sys.Connector;
import org.apache.openejb.alt.config.sys.Container;
-import org.apache.openejb.alt.config.sys.Deployments;
import org.apache.openejb.alt.config.sys.JndiProvider;
import org.apache.openejb.alt.config.sys.Openejb;
import org.apache.openejb.alt.config.sys.ProxyFactory;
@@ -115,28 +114,32 @@
DynamicDeployer deployer = getDeployer(openejb);
- List<Deployments> deployments = new ArrayList<Deployments>(Arrays.asList(openejb.getDeployments()));
+ DeploymentLoader deploymentLoader = new DeploymentLoader(props);
- //// getOption ///////////////////////////////// BEGIN ////////////////////
- String flag = props.getProperty("openejb.deployments.classpath", "true").toLowerCase();
- boolean searchClassPath = flag.equals("true");
- //// getOption ///////////////////////////////// END ////////////////////
-
- if (searchClassPath) {
- Deployments deployment = new Deployments();
- deployment.setClasspath(this.getClass().getClassLoader());
- deployments.add(deployment);
- }
+ List<String> jarList = DeploymentsResolver.resolveAppLocations(openejb.getDeployments());
+
+ List<DeploymentModule> appModules = new ArrayList();
+
+ for (String pathname : jarList) {
+
+ File jarFile = new File(pathname);
+ DeploymentLoader.logger.debug("Beginning load: " + jarFile.getAbsolutePath());
+
+ try {
+
+ AppModule appModule = deploymentLoader.load(jarFile, deployer);
+ appModules.add(appModule);
- DeploymentLoader deploymentLoader = new DeploymentLoader();
+ DeploymentLoader.logger.info("Loaded Module: " + appModule.getJarLocation());
- List<DeploymentModule> deployedJars = deploymentLoader.loadModules(deployments, deployer);
+ } catch (OpenEJBException e) {
+ e.printStackTrace();
+ ConfigUtils.logger.i18n.warning("conf.0004", jarFile.getAbsolutePath(), e.getMessage());
+ }
- for (DeploymentModule ejbModule : deployedJars) {
- DeploymentLoader.logger.info("Loaded Module: " + ejbModule.getJarLocation());
}
- DeploymentModule[] jars = deployedJars.toArray(new DeploymentModule[]{});
+ DeploymentModule[] jars = appModules.toArray(new DeploymentModule[]{});
sys = new OpenEjbConfiguration();
sys.containerSystem = new ContainerSystemInfo();
Modified: incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/alt/config/DeploymentLoader.java
URL: http://svn.apache.org/viewvc/incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/alt/config/DeploymentLoader.java?view=diff&rev=495832&r1=495831&r2=495832
==============================================================================
--- incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/alt/config/DeploymentLoader.java (original)
+++ incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/alt/config/DeploymentLoader.java Fri Jan 12 19:44:26 2007
@@ -21,38 +21,26 @@
import org.apache.openejb.OpenEJBException;
import org.apache.openejb.core.TemporaryClassLoader;
import org.apache.openejb.alt.config.ejb.OpenejbJar;
-import org.apache.openejb.alt.config.sys.Deployments;
import org.apache.openejb.jee.Application;
import org.apache.openejb.jee.ApplicationClient;
import org.apache.openejb.jee.EjbJar;
import org.apache.openejb.jee.Module;
-import org.apache.openejb.loader.FileUtils;
-import org.apache.openejb.loader.SystemInstance;
import org.apache.openejb.util.Logger;
import org.apache.xbean.finder.ResourceFinder;
import org.apache.xbean.finder.ClassFinder;
-import org.apache.xbean.finder.UrlSet;
import javax.ejb.Stateless;
import javax.ejb.Stateful;
-import java.io.BufferedOutputStream;
import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
-import java.nio.channels.FileChannel;
import java.util.ArrayList;
-import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import java.util.Collections;
-import java.util.jar.JarEntry;
-import java.util.jar.JarFile;
+import java.util.Properties;
import java.util.jar.Manifest;
import java.util.jar.Attributes;
@@ -63,344 +51,228 @@
private static final Map<Class<?>, JaxbUnmarshaller> unmarshallers = new HashMap();
- private static final String CLASSPATH_INCLUDE = "openejb.deployments.classpath.include";
- private static final String CLASSPATH_EXCLUDE = "openejb.deployments.classpath.exclude";
-
public static Logger logger = Logger.getInstance("OpenEJB.startup", "org.apache.openejb.util.resources");
- public DeploymentLoader() {
+ public DeploymentLoader(Properties props) {
// For some reason intellij won't log to the one we configured statically
logger = EjbJarUtils.logger;
}
- private void loadFrom(Deployments dep, FileUtils path, List jarList) {
-
- ////////////////////////////////
- //
- // Expand the path of a jar
- //
- ////////////////////////////////
- if (dep.getDir() == null && dep.getJar() != null) {
- try {
- File jar = path.getFile(dep.getJar(), false);
- if (!jarList.contains(jar.getAbsolutePath())) {
- jarList.add(jar.getAbsolutePath());
- }
- } catch (Exception ignored) {
- }
- return;
- }
-
- File dir = null;
- try {
- dir = path.getFile(dep.getDir(), false);
- } catch (Exception ignored) {
- }
-
- if (dir == null || !dir.isDirectory()) return;
-
- ////////////////////////////////
- //
- // Unpacked "Jar" directory
- //
- ////////////////////////////////
- File ejbJarXml = new File(dir, "META-INF" + File.separator + "ejb-jar.xml");
- if (ejbJarXml.exists()) {
- if (!jarList.contains(dir.getAbsolutePath())) {
- jarList.add(dir.getAbsolutePath());
- }
- return;
- }
-
- File appXml = new File(dir, "META-INF" + File.separator + "application.xml");
- if (appXml.exists()) {
- if (!jarList.contains(dir.getAbsolutePath())) {
- jarList.add(dir.getAbsolutePath());
- }
- return;
- }
-
- HashMap<String, URL> files = new HashMap<String, URL>();
- scanDir(dir, files,"");
- for (String fileName : files.keySet()) {
- if (fileName.endsWith(".class")){
- if (!jarList.contains(dir.getAbsolutePath())) {
- jarList.add(dir.getAbsolutePath());
- }
- return;
- }
- }
-
- ////////////////////////////////
- //
- // Directory container Jar files
- //
- ////////////////////////////////
- for (String fileName : files.keySet()) {
- if (fileName.endsWith(".jar") || fileName.endsWith(".ear")){
- File jar = new File(dir, fileName);
-
- if (jarList.contains(jar.getAbsolutePath())) continue;
- jarList.add(jar.getAbsolutePath());
- }
- }
- }
-
-
- public List<DeploymentModule> loadModules(List<Deployments> deployments, DynamicDeployer deployer) {
- List<DeploymentModule> deployedJars = new ArrayList();
- // resolve jar locations ////////////////////////////////////// BEGIN ///////
+ public AppModule load(File jarFile, DynamicDeployer deployer) throws OpenEJBException {
+ ClassLoader classLoader = getClassLoader(jarFile);
- FileUtils base = SystemInstance.get().getBase();
+ URL baseUrl = getFileUrl(jarFile);
- List<String> jarList = new ArrayList(deployments.size());
+ Class moduleClass = null;
try {
- for (Deployments deployment : deployments) {
- if (deployment.getClasspath() != null) {
- loadFromClasspath(base, jarList, deployment.getClasspath());
- } else {
- loadFrom(deployment, base, jarList);
- }
- }
- } catch (SecurityException se) {
-
+ moduleClass = discoverModuleType(baseUrl, classLoader);
+ } catch (Exception e) {
+ throw new OpenEJBException("Unable to determine module type for jar: " + baseUrl.toExternalForm(), e);
}
- String[] jarsToLoad = (String[]) jarList.toArray(new String[]{});
+ if (AppModule.class.equals(moduleClass)) {
- /*[1] Put all EjbJar & OpenejbJar objects in a vector ***************/
- for (int i = 0; i < jarsToLoad.length; i++) {
+ File appDir = unpack(jarFile);
- String pathname = jarsToLoad[i];
+ URL appUrl = getFileUrl(appDir);
- logger.debug("Beginning load: " + pathname);
+ ClassLoader tmpClassLoader = new TemporaryClassLoader(new URL[]{appUrl}, OpenEJB.class.getClassLoader());
- File jarFile = new File(pathname);
+ ResourceFinder finder = new ResourceFinder("", tmpClassLoader, appUrl);
+ Map<String, URL> descriptors = null;
try {
+ descriptors = finder.getResourcesMap("META-INF");
+ } catch (IOException e) {
+ throw new OpenEJBException("Unable to determine descriptors in jar: " + appUrl.toExternalForm(), e);
+ }
- ClassLoader classLoader = getClassLoader(jarFile);
+ try {
+ List<URL> extraLibs = new ArrayList();
- URL baseUrl = getFileUrl(jarFile);
+ try {
+ Map<String, URL> libs = finder.getResourcesMap("lib/");
+ extraLibs.addAll(libs.values());
+ } catch (IOException e) {
+ logger.warning("Cannot load libs from 'lib/' : " + e.getMessage(), e);
+ }
+ try {
+ Map<String, URL> libs = finder.getResourcesMap("APP-INF/lib/");
+ extraLibs.addAll(libs.values());
+ } catch (IOException e) {
+ logger.warning("Cannot load libs from 'APP-INF/lib/' : " + e.getMessage(), e);
+ }
- Class moduleClass = null;
try {
- moduleClass = discoverModuleType(baseUrl, classLoader);
- } catch (Exception e) {
- throw new OpenEJBException("Unable to determine module type for jar: " + baseUrl.toExternalForm(), e);
+ Map<String, URL> libs = finder.getResourcesMap("META-INF/lib/");
+ extraLibs.addAll(libs.values());
+ } catch (IOException e) {
+ logger.warning("Cannot load libs from 'META-INF/lib/' : " + e.getMessage(), e);
}
- if (AppModule.class.equals(moduleClass)) {
+ Map<String,URL> ejbModules = new HashMap();
+ Map<String,URL> clientModules = new HashMap();
- loadAppModule(jarFile, deployer, deployedJars);
+ URL applicationXmlUrl = descriptors.get("application.xml");
+ Application application;
+ if (applicationXmlUrl != null) {
+ application = unmarshal(Application.class, "META-INF/application.xml", applicationXmlUrl);
+ for (Module module : application.getModule()) {
+ try {
+ if (module.getEjb() != null) {
+ URL url = finder.find(module.getEjb().trim());
+ ejbModules.put(module.getEjb(), url);
+ } else if (module.getJava() != null) {
+ URL url = finder.find(module.getJava().trim());
+ clientModules.put(module.getConnector(), url);
+ }
+ } catch (IOException e) {
+ throw new OpenEJBException("Invalid path to module " + e.getMessage(), e);
+ }
+ }
} else {
-
- EjbJarUtils ejbJarUtils = new EjbJarUtils(jarFile.getAbsolutePath());
- EjbModule undeployedModule = new EjbModule(classLoader, jarFile.getAbsolutePath(), ejbJarUtils.getEjbJar(), ejbJarUtils.getOpenejbJar());
-
-
- EjbModule ejbModule = deployer.deploy(undeployedModule);
-
- AppModule appModule = new AppModule(classLoader, null);
- appModule.getEjbModules().add(ejbModule);
- deployedJars.add(appModule);
- }
-
- } catch (OpenEJBException e) {
- e.printStackTrace();
- ConfigUtils.logger.i18n.warning("conf.0004", jarFile.getAbsolutePath(), e.getMessage());
- }
- }
- return deployedJars;
- }
-
- private void scanDir(File dir, Map<String,URL> files, String path) {
- for (File file : dir.listFiles()) {
- if (file.isDirectory()) {
- scanDir(file, files, path + file.getName() + "/");
- } else {
- String name = file.getName();
- try {
- files.put(path + name, file.toURL());
- } catch (MalformedURLException e) {
- logger.warning("EAR path bad: "+path + name, e);
+ application = new Application();
+ HashMap<String,URL> files = new HashMap();
+ scanDir(appDir, files, "");
+ files.remove("META-INF/MANIFEST.MF");
+ for (Map.Entry<String, URL> entry : files.entrySet()) {
+ if (entry.getKey().startsWith("lib/")) continue;
+ if (!entry.getKey().matches(".*\\.(jar|war|rar|ear)")) continue;
+
+ try {
+ Class moduleType = discoverModuleType(entry.getValue(), tmpClassLoader);
+ if (EjbModule.class.equals(moduleType)){
+ ejbModules.put(entry.getKey(), entry.getValue());
+ } else if (ClientModule.class.equals(moduleType)){
+ clientModules.put(entry.getKey(), entry.getValue());
+ }
+ } catch (UnsupportedOperationException e) {
+ // Ignore it as per the javaee spec EE.8.4.2 section 1.d.iiilogger.info("Ignoring unknown module type: "+entry.getKey());
+ } catch (Exception e) {
+ throw new OpenEJBException("Unable to determine the module type of "+ entry.getKey()+": Exception: "+ e.getMessage(), e);
+ }
+ }
}
- }
- }
- }
- private void loadAppModule(File jarFile, DynamicDeployer deployer, List<DeploymentModule> deployedJars) throws OpenEJBException {
- File appDir = unpack(jarFile);
+ List<URL> classPath = new ArrayList();
+ classPath.addAll(ejbModules.values());
+ classPath.addAll(clientModules.values());
+ classPath.addAll(extraLibs);
+ ClassLoader appClassLoader = new TemporaryClassLoader(classPath.toArray(new URL[]{}), OpenEJB.class.getClassLoader());
- URL appUrl = getFileUrl(appDir);
- ClassLoader tmpClassLoader = new TemporaryClassLoader(new URL[]{appUrl}, OpenEJB.class.getClassLoader());
-
- ResourceFinder finder = new ResourceFinder("", tmpClassLoader, appUrl);
+ AppModule appModule = new AppModule(appClassLoader, appDir.getAbsolutePath());
+ appModule.getAdditionalLibraries().addAll(extraLibs);
+
+ for (String moduleName : ejbModules.keySet()) {
+ URL ejbUrl = ejbModules.get(moduleName);
+ File ejbFile = new File(ejbUrl.getPath());
+ try {
+ ResourceFinder ejbFinder = new ResourceFinder(ejbUrl);
- Map<String, URL> descriptors = null;
- try {
- descriptors = finder.getResourcesMap("META-INF");
- } catch (IOException e) {
- throw new OpenEJBException("Unable to determine descriptors in jar: " + appUrl.toExternalForm(), e);
- }
+ Map<String, URL> ejbDescriptors = null;
+ try {
+ ejbDescriptors = ejbFinder.getResourcesMap("META-INF/");
+ } catch (IOException e) {
+ throw new OpenEJBException("Unable to determine descriptors in jar.", e);
+ }
- try {
- List<URL> extraLibs = new ArrayList();
+ EjbJar ejbJar;
+ if (ejbDescriptors.containsKey("ejb-jar.xml")) {
+ ejbJar = unmarshal(EjbJar.class, "META-INF/ejb-jar.xml", ejbDescriptors.get("ejb-jar.xml"));
+ } else {
+ logger.warning("No ejb-jar.xml found assuming annotated beans present: " + appDir.getAbsolutePath() + ", module: " + moduleName);
+ ejbJar = new EjbJar();
+ }
- try {
- Map<String, URL> libs = finder.getResourcesMap("lib/");
- extraLibs.addAll(libs.values());
- } catch (IOException e) {
- logger.warning("Cannot load libs from 'lib/' : " + e.getMessage(), e);
- }
- try {
- Map<String, URL> libs = finder.getResourcesMap("APP-INF/lib/");
- extraLibs.addAll(libs.values());
- } catch (IOException e) {
- logger.warning("Cannot load libs from 'APP-INF/lib/' : " + e.getMessage(), e);
- }
+ OpenejbJar openejbJar = null;
- try {
- Map<String, URL> libs = finder.getResourcesMap("META-INF/lib/");
- extraLibs.addAll(libs.values());
- } catch (IOException e) {
- logger.warning("Cannot load libs from 'META-INF/lib/' : " + e.getMessage(), e);
- }
+ if (ejbDescriptors.containsKey("openejb-jar.xml")) {
+ openejbJar = unmarshal(OpenejbJar.class, "META-INF/openejb-jar.xml", ejbDescriptors.get("openejb-jar.xml"));
+ }
- Map<String,URL> ejbModules = new HashMap();
- Map<String,URL> clientModules = new HashMap();
+ EjbModule ejbModule = new EjbModule(appClassLoader, ejbFile.getAbsolutePath(), ejbJar, openejbJar);
- URL applicationXmlUrl = descriptors.get("application.xml");
+ ejbModule = deployer.deploy(ejbModule);
- Application application;
- if (applicationXmlUrl != null) {
- application = unmarshal(Application.class, "META-INF/application.xml", applicationXmlUrl);
- for (Module module : application.getModule()) {
- try {
- if (module.getEjb() != null) {
- URL url = finder.find(module.getEjb().trim());
- ejbModules.put(module.getEjb(), url);
- } else if (module.getJava() != null) {
- URL url = finder.find(module.getJava().trim());
- clientModules.put(module.getConnector(), url);
- }
- } catch (IOException e) {
- throw new OpenEJBException("Invalid path to module " + e.getMessage(), e);
+ appModule.getEjbModules().add(ejbModule);
+ } catch (OpenEJBException e) {
+ logger.error("Unable to load EJBs from EAR: " + appDir.getAbsolutePath() + ", module: " + moduleName + ". Exception: " + e.getMessage(), e);
}
}
- } else {
- application = new Application();
- HashMap<String,URL> files = new HashMap();
- scanDir(appDir, files, "");
- files.remove("META-INF/MANIFEST.MF");
- for (Map.Entry<String, URL> entry : files.entrySet()) {
- if (entry.getKey().startsWith("lib/")) continue;
- if (!entry.getKey().matches(".*\\.(jar|war|rar|ear)")) continue;
-
+ for (String moduleName : clientModules.keySet()) {
+ URL clientUrl = clientModules.get(moduleName);
+ File clientFile = new File(clientUrl.getPath());
try {
- Class moduleType = discoverModuleType(entry.getValue(), tmpClassLoader);
- if (EjbModule.class.equals(moduleType)){
- ejbModules.put(entry.getKey(), entry.getValue());
- } else if (ClientModule.class.equals(moduleType)){
- clientModules.put(entry.getKey(), entry.getValue());
+ ResourceFinder clientFinder = new ResourceFinder(clientUrl);
+
+ ApplicationClient applicationClient;
+ try {
+ URL appClientXmlUrl = clientFinder.find("META-INF/application-client.xml");
+ applicationClient = unmarshal(ApplicationClient.class, "META-INF/application-client.xml", appClientXmlUrl);
+ } catch (IOException e) {
+ logger.warning("No application-client.xml found assuming annotations present: " + appDir.getAbsolutePath() + ", module: " + moduleName);
+ applicationClient = new ApplicationClient();
}
- } catch (UnsupportedOperationException e) {
- // Ignore it as per the javaee spec EE.8.4.2 section 1.d.iiilogger.info("Ignoring unknown module type: "+entry.getKey());
- } catch (Exception e) {
- throw new OpenEJBException("Unable to determine the module type of "+ entry.getKey()+": Exception: "+ e.getMessage(), e);
- }
- }
- }
- List<URL> classPath = new ArrayList();
- classPath.addAll(ejbModules.values());
- classPath.addAll(clientModules.values());
- classPath.addAll(extraLibs);
- ClassLoader appClassLoader = new TemporaryClassLoader(classPath.toArray(new URL[]{}), OpenEJB.class.getClassLoader());
+ URL manifestUrl = clientFinder.find("META-INF/MANIFEST.MF");
+ InputStream is = manifestUrl.openStream();
+ Manifest manifest = new Manifest(is);
+ String mainClass = manifest.getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);
+ ClientModule clientModule = new ClientModule(applicationClient, appClassLoader, clientFile.getAbsolutePath(), mainClass);
- AppModule appModule = new AppModule(appClassLoader, appDir.getAbsolutePath());
- appModule.getAdditionalLibraries().addAll(extraLibs);
-
- for (String moduleName : ejbModules.keySet()) {
- URL ejbUrl = ejbModules.get(moduleName);
- File ejbFile = new File(ejbUrl.getPath());
- try {
- ResourceFinder ejbFinder = new ResourceFinder(ejbUrl);
+ clientModule = deployer.deploy(clientModule);
- Map<String, URL> ejbDescriptors = null;
- try {
- ejbDescriptors = ejbFinder.getResourcesMap("META-INF/");
- } catch (IOException e) {
- throw new OpenEJBException("Unable to determine descriptors in jar.", e);
+ appModule.getClientModules().add(clientModule);
+ } catch (Exception e) {
+ logger.error("Unable to load App Client from EAR: " + appDir.getAbsolutePath() + ", module: " + moduleName + ". Exception: " + e.getMessage(), e);
}
+ }
- EjbJar ejbJar;
- if (ejbDescriptors.containsKey("ejb-jar.xml")) {
- ejbJar = unmarshal(EjbJar.class, "META-INF/ejb-jar.xml", ejbDescriptors.get("ejb-jar.xml"));
- } else {
- logger.warning("No ejb-jar.xml found assuming annotated beans present: " + appDir.getAbsolutePath() + ", module: " + moduleName);
- ejbJar = new EjbJar();
- }
+ return appModule;
+ } catch (OpenEJBException e) {
+ logger.error("Unable to load EAR: " + jarFile.getAbsolutePath(), e);
+ throw e;
+ }
- OpenejbJar openejbJar = null;
+ } else {
- if (ejbDescriptors.containsKey("openejb-jar.xml")) {
- openejbJar = unmarshal(OpenejbJar.class, "META-INF/openejb-jar.xml", ejbDescriptors.get("openejb-jar.xml"));
- }
+ EjbJarUtils ejbJarUtils = new EjbJarUtils(jarFile.getAbsolutePath());
+ EjbModule undeployedModule = new EjbModule(classLoader, jarFile.getAbsolutePath(), ejbJarUtils.getEjbJar(), ejbJarUtils.getOpenejbJar());
- EjbModule ejbModule = new EjbModule(appClassLoader, ejbFile.getAbsolutePath(), ejbJar, openejbJar);
- ejbModule = deployer.deploy(ejbModule);
+ EjbModule ejbModule = deployer.deploy(undeployedModule);
- appModule.getEjbModules().add(ejbModule);
- } catch (OpenEJBException e) {
- logger.error("Unable to load EJBs from EAR: " + appDir.getAbsolutePath() + ", module: " + moduleName + ". Exception: " + e.getMessage(), e);
- }
- }
- for (String moduleName : clientModules.keySet()) {
- URL clientUrl = clientModules.get(moduleName);
- File clientFile = new File(clientUrl.getPath());
- try {
- ResourceFinder clientFinder = new ResourceFinder(clientUrl);
+ AppModule appModule = new AppModule(classLoader, null);
+ appModule.getEjbModules().add(ejbModule);
- ApplicationClient applicationClient;
- try {
- URL appClientXmlUrl = clientFinder.find("META-INF/application-client.xml");
- applicationClient = unmarshal(ApplicationClient.class, "META-INF/application-client.xml", appClientXmlUrl);
- } catch (IOException e) {
- logger.warning("No application-client.xml found assuming annotations present: " + appDir.getAbsolutePath() + ", module: " + moduleName);
- applicationClient = new ApplicationClient();
- }
+ return appModule;
+ }
+ }
- URL manifestUrl = clientFinder.find("META-INF/MANIFEST.MF");
- InputStream is = manifestUrl.openStream();
- Manifest manifest = new Manifest(is);
- String mainClass = manifest.getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);
-
- ClientModule clientModule = new ClientModule(applicationClient, appClassLoader, clientFile.getAbsolutePath(), mainClass);
-
- clientModule = deployer.deploy(clientModule);
-
- appModule.getClientModules().add(clientModule);
- } catch (Exception e) {
- logger.error("Unable to load App Client from EAR: " + appDir.getAbsolutePath() + ", module: " + moduleName + ". Exception: " + e.getMessage(), e);
+ public static void scanDir(File dir, Map<String,URL> files, String path) {
+ for (File file : dir.listFiles()) {
+ if (file.isDirectory()) {
+ scanDir(file, files, path + file.getName() + "/");
+ } else {
+ String name = file.getName();
+ try {
+ files.put(path + name, file.toURL());
+ } catch (MalformedURLException e) {
+ logger.warning("EAR path bad: "+path + name, e);
}
}
- deployedJars.add(appModule);
- } catch (OpenEJBException e) {
- logger.error("Unable to load EAR: " + jarFile.getAbsolutePath(), e);
}
}
- private Class<? extends DeploymentModule> discoverModuleType(URL baseUrl, ClassLoader classLoader) throws IOException, UnsupportedOperationException {
+ public static Class<? extends DeploymentModule> discoverModuleType(URL baseUrl, ClassLoader classLoader) throws IOException, UnsupportedOperationException {
ResourceFinder finder = new ResourceFinder("", classLoader, baseUrl);
Map<String, URL> descriptors = finder.getResourcesMap("META-INF");
@@ -491,349 +363,4 @@
}
}
- /*
- * The algorithm of OpenEJB deployments class-path inclusion and exclusion is implemented as follows:
- * 1- If the string value of the resource URL matches the include class-path pattern
- * Then load this resource
- * 2- If the string value of the resource URL matches the exclude class-path pattern
- * Then ignore this resource
- * 3- If the include and exclude class-path patterns are not defined
- * Then load this resource
- *
- * The previous steps are based on the following points:
- * 1- Include class-path pattern has the highst priority
- * This helps in case both patterns are defined using the same values.
- * This appears in step 1 and 2 of the above algorithm.
- * 2- Loading the resource is the default behaviour in case of not defining a value for any class-path pattern
- * This appears in step 3 of the above algorithm.
- */
- private void loadFromClasspath(FileUtils base, List<String> jarList, ClassLoader classLoader) {
-
- Deployments deployment = null;
- String include = null;
- String exclude = null;
- String path = null;
-
- include = SystemInstance.get().getProperty(CLASSPATH_INCLUDE, "");
- exclude = SystemInstance.get().getProperty(CLASSPATH_EXCLUDE, ".*");
- try {
- UrlSet urlSet = new UrlSet(classLoader);
- UrlSet includes = urlSet.matching(include);
- urlSet = urlSet.exclude(ClassLoader.getSystemClassLoader().getParent());
- urlSet = urlSet.excludeJavaExtDirs();
- urlSet = urlSet.excludeJavaEndorsedDirs();
- urlSet = urlSet.excludeJavaHome();
- urlSet = urlSet.excludePaths(System.getProperty("sun.boot.class.path",""));
- urlSet = urlSet.exclude(".*/JavaVM.framework/.*");
- urlSet = urlSet.exclude(exclude);
- urlSet = urlSet.include(includes);
-
- List<URL> urls = urlSet.getUrls();
- int size = urls.size();
- if (size == 0 && include.length() > 0){
- logger.warning("No classpath URLs matched. Current settings: "+CLASSPATH_EXCLUDE +"='"+exclude+"', "+CLASSPATH_INCLUDE+"='"+include+"'");
- return;
- } else if (size == 0) {
- return;
- } else if (size < 10) {
- logger.debug("Inspecting classpath for applications: "+urls.size()+" urls.");
- } else if (size < 50) {
- logger.info("Inspecting classpath for applications: "+urls.size()+" urls. Consider adjusting your exclude/include. Current settings: "+CLASSPATH_EXCLUDE +"='"+exclude+"', "+CLASSPATH_INCLUDE+"='"+include+"'");
- } else {
- logger.warning("Inspecting classpath for applications: "+urls.size()+" urls.");
- logger.warning("ADJUST THE EXCLUDE/INCLUDE!!!. Current settings: "+CLASSPATH_EXCLUDE +"='"+exclude+"', "+CLASSPATH_INCLUDE+"='"+include+"'");
- }
-
- long begin = System.currentTimeMillis();
- for (URL url : urls) {
- try {
- Class moduleType = discoverModuleType(url, classLoader);
- if (AppModule.class.isAssignableFrom(moduleType) || EjbModule.class.isAssignableFrom(moduleType)){
- deployment = new Deployments();
- if (url.getProtocol().equals("jar")) {
- url = new URL(url.getFile().replaceFirst("!.*$", ""));
- File file = new File(url.getFile());
- path = file.getAbsolutePath();
- deployment.setJar(path);
- } else if (url.getProtocol().equals("file")) {
- File file = new File(url.getFile());
- path = file.getAbsolutePath();
- deployment.setDir(path);
- } else {
- logger.warning("Not loading " + moduleType.getSimpleName() + ". Unknown protocol " + url.getProtocol());
- continue;
- }
- logger.info("Found " + moduleType.getSimpleName() + " in classpath: " + path);
- loadFrom(deployment, base, jarList);
- }
- } catch (IOException e) {
- logger.warning("Unable to determine the module type of "+ url.toExternalForm()+": Exception: "+ e.getMessage(), e);
- } catch (UnsupportedOperationException ignore) {
- }
- }
- long end = System.currentTimeMillis();
- long time = end - begin;
-
- if (time < 1000) {
- logger.debug("Searched "+urls.size()+" classpath urls in "+time+" milliseconds. Average "+(time/urls.size())+" milliseconds per url.");
- } else if (time < 4000 || urls.size() < 3) {
- logger.info("Searched "+urls.size()+" classpath urls in "+time+" milliseconds. Average "+(time/urls.size())+" milliseconds per url.");
- } else if (time < 10000){
- logger.warning("Searched "+urls.size()+" classpath urls in "+time+" milliseconds. Average "+(time/urls.size())+" milliseconds per url.");
- logger.warning("Consider adjusting your "+CLASSPATH_EXCLUDE +" and "+CLASSPATH_INCLUDE +" settings. Current settings: exclude='"+exclude+"', include='"+include+"'");
- } else {
- logger.fatal("Searched "+urls.size()+" classpath urls in "+time+" milliseconds. Average "+(time/urls.size())+" milliseconds per url. TOO LONG!");
- logger.fatal("ADJUST THE EXCLUDE/INCLUDE!!!. Current settings: "+CLASSPATH_EXCLUDE +"='"+exclude+"', "+CLASSPATH_INCLUDE+"='"+include+"'");
- List<String> list = new ArrayList<String>();
- for (URL url : urls) {
- list.add(url.toExternalForm());
- }
- Collections.sort(list);
- for (String url : list) {
- logger.info("Matched: "+url);
- }
- }
- } catch (IOException e1) {
- e1.printStackTrace();
- logger.warning("Unable to search classpath for modules: Received Exception: " + e1.getClass().getName() + " " + e1.getMessage(), e1);
- }
-
- }
-
- public static class JarExtractor {
-
- /**
- * Extract the WAR file found at the specified URL into an unpacked
- * directory structure, and return the absolute pathname to the extracted
- * directory.
- *
- * @param jar URL of the web application archive to be extracted
- * (must start with "jar:")
- * @param pathname Context path name for web application
- * @param file
- * @throws IllegalArgumentException if this is not a "jar:" URL
- * @throws IOException if an input/output error was encountered
- * during expansion
- */
- public static File extract(URL jar, String pathname, File file)
- throws IOException {
-
- // Make sure that there is no such directory already existing
- FileUtils base = SystemInstance.get().getBase();
- File appBase = base.getDirectory("apps", true);
- if (!appBase.exists() || !appBase.isDirectory()) {
- throw new IOException("" + appBase.getAbsolutePath());
- }
-
- File docBase = new File(appBase, pathname);
- if (docBase.exists()) {
- // Ear file is already installed
- return docBase;
- }
-
- logger.info("Extracting jar: " + file.getAbsolutePath());
-
- // Create the new document base directory
- docBase.mkdir();
-
- // Extract the JAR into the new directory
- JarURLConnection jarURLConnection = (JarURLConnection) jar.openConnection();
- jarURLConnection.setUseCaches(false);
- JarFile jarFile = null;
- InputStream input = null;
- try {
- jarFile = jarURLConnection.getJarFile();
- Enumeration jarEntries = jarFile.entries();
- while (jarEntries.hasMoreElements()) {
- JarEntry jarEntry = (JarEntry) jarEntries.nextElement();
- String name = jarEntry.getName();
- int last = name.lastIndexOf('/');
- if (last >= 0) {
- File parent = new File(docBase,
- name.substring(0, last));
- parent.mkdirs();
- }
- if (name.endsWith("/")) {
- continue;
- }
- input = jarFile.getInputStream(jarEntry);
-
- File extractedFile = extract(input, docBase, name);
- long lastModified = jarEntry.getTime();
- if ((lastModified != -1) && (lastModified != 0) && (extractedFile != null)) {
- extractedFile.setLastModified(lastModified);
- }
-
- input.close();
- input = null;
- }
- } catch (IOException e) {
- // If something went wrong, delete extracted dir to keep things
- // clean
- deleteDir(docBase);
- throw e;
- } finally {
- if (input != null) {
- try {
- input.close();
- } catch (Throwable t) {
- ;
- }
- input = null;
- }
- if (jarFile != null) {
- try {
- jarFile.close();
- } catch (Throwable t) {
- ;
- }
- jarFile = null;
- }
- }
-
- // Return the absolute path to our new document base directory
- logger.info("Extracted path: " + docBase.getAbsolutePath());
-
- return docBase;
-
- }
-
-
- /**
- * Copy the specified file or directory to the destination.
- *
- * @param src File object representing the source
- * @param dest File object representing the destination
- */
- public static boolean copy(File src, File dest) {
-
- boolean result = true;
-
- String files[] = null;
- if (src.isDirectory()) {
- files = src.list();
- result = dest.mkdir();
- } else {
- files = new String[1];
- files[0] = "";
- }
- if (files == null) {
- files = new String[0];
- }
- for (int i = 0; (i < files.length) && result; i++) {
- File fileSrc = new File(src, files[i]);
- File fileDest = new File(dest, files[i]);
- if (fileSrc.isDirectory()) {
- result = copy(fileSrc, fileDest);
- } else {
- FileChannel ic = null;
- FileChannel oc = null;
- try {
- ic = (new FileInputStream(fileSrc)).getChannel();
- oc = (new FileOutputStream(fileDest)).getChannel();
- ic.transferTo(0, ic.size(), oc);
- } catch (IOException e) {
- logger.error("Copy failed: src: " + fileSrc + ", dest: " + fileDest, e);
- result = false;
- } finally {
- if (ic != null) {
- try {
- ic.close();
- } catch (IOException e) {
- }
- }
- if (oc != null) {
- try {
- oc.close();
- } catch (IOException e) {
- }
- }
- }
- }
- }
- return result;
-
- }
-
-
- /**
- * Delete the specified directory, including all of its contents and
- * subdirectories recursively.
- *
- * @param dir File object representing the directory to be deleted
- */
- public static boolean delete(File dir) {
- if (dir.isDirectory()) {
- return deleteDir(dir);
- } else {
- return dir.delete();
- }
- }
-
-
- /**
- * Delete the specified directory, including all of its contents and
- * subdirectories recursively.
- *
- * @param dir File object representing the directory to be deleted
- */
- public static boolean deleteDir(File dir) {
-
- String files[] = dir.list();
- if (files == null) {
- files = new String[0];
- }
- for (int i = 0; i < files.length; i++) {
- File file = new File(dir, files[i]);
- if (file.isDirectory()) {
- deleteDir(file);
- } else {
- file.delete();
- }
- }
- return dir.delete();
-
- }
-
-
- /**
- * Extract the specified input stream into the specified directory, creating
- * a file named from the specified relative path.
- *
- * @param input InputStream to be copied
- * @param docBase Document base directory into which we are extracting
- * @param name Relative pathname of the file to be created
- * @return A handle to the extracted File
- * @throws IOException if an input/output error occurs
- */
- protected static File extract(InputStream input, File docBase, String name)
- throws IOException {
-
- File file = new File(docBase, name);
- BufferedOutputStream output = null;
- try {
- output =
- new BufferedOutputStream(new FileOutputStream(file));
- byte buffer[] = new byte[2048];
- while (true) {
- int n = input.read(buffer);
- if (n <= 0)
- break;
- output.write(buffer, 0, n);
- }
- } finally {
- if (output != null) {
- try {
- output.close();
- } catch (IOException e) {
- // Ignore
- }
- }
- }
-
- return file;
- }
-
-
- }
}
Added: incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/alt/config/DeploymentsResolver.java
URL: http://svn.apache.org/viewvc/incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/alt/config/DeploymentsResolver.java?view=auto&rev=495832
==============================================================================
--- incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/alt/config/DeploymentsResolver.java (added)
+++ incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/alt/config/DeploymentsResolver.java Fri Jan 12 19:44:26 2007
@@ -0,0 +1,254 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.alt.config;
+
+import org.apache.openejb.alt.config.sys.Deployments;
+import org.apache.openejb.loader.FileUtils;
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.xbean.finder.UrlSet;
+
+import java.util.List;
+import java.util.HashMap;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class DeploymentsResolver {
+
+ private static final String CLASSPATH_INCLUDE = "openejb.deployments.classpath.include";
+ private static final String CLASSPATH_EXCLUDE = "openejb.deployments.classpath.exclude";
+
+ private static void loadFrom(Deployments dep, FileUtils path, List jarList) {
+
+ ////////////////////////////////
+ //
+ // Expand the path of a jar
+ //
+ ////////////////////////////////
+ if (dep.getDir() == null && dep.getJar() != null) {
+ try {
+ File jar = path.getFile(dep.getJar(), false);
+ if (!jarList.contains(jar.getAbsolutePath())) {
+ jarList.add(jar.getAbsolutePath());
+ }
+ } catch (Exception ignored) {
+ }
+ return;
+ }
+
+ File dir = null;
+ try {
+ dir = path.getFile(dep.getDir(), false);
+ } catch (Exception ignored) {
+ }
+
+ if (dir == null || !dir.isDirectory()) return;
+
+ ////////////////////////////////
+ //
+ // Unpacked "Jar" directory
+ //
+ ////////////////////////////////
+ File ejbJarXml = new File(dir, "META-INF" + File.separator + "ejb-jar.xml");
+ if (ejbJarXml.exists()) {
+ if (!jarList.contains(dir.getAbsolutePath())) {
+ jarList.add(dir.getAbsolutePath());
+ }
+ return;
+ }
+
+ File appXml = new File(dir, "META-INF" + File.separator + "application.xml");
+ if (appXml.exists()) {
+ if (!jarList.contains(dir.getAbsolutePath())) {
+ jarList.add(dir.getAbsolutePath());
+ }
+ return;
+ }
+
+ HashMap<String, URL> files = new HashMap<String, URL>();
+ DeploymentLoader.scanDir(dir, files,"");
+ for (String fileName : files.keySet()) {
+ if (fileName.endsWith(".class")){
+ if (!jarList.contains(dir.getAbsolutePath())) {
+ jarList.add(dir.getAbsolutePath());
+ }
+ return;
+ }
+ }
+
+ ////////////////////////////////
+ //
+ // Directory container Jar files
+ //
+ ////////////////////////////////
+ for (String fileName : files.keySet()) {
+ if (fileName.endsWith(".jar") || fileName.endsWith(".ear")){
+ File jar = new File(dir, fileName);
+
+ if (jarList.contains(jar.getAbsolutePath())) continue;
+ jarList.add(jar.getAbsolutePath());
+ }
+ }
+ }
+
+ public static List<String> resolveAppLocations(Deployments... deploymentsArray) {
+ List<Deployments> deployments = new ArrayList<Deployments>(Arrays.asList(deploymentsArray));
+
+ //// getOption ///////////////////////////////// BEGIN ////////////////////
+ String flag = SystemInstance.get().getProperty("openejb.deployments.classpath", "true").toLowerCase();
+ boolean searchClassPath = flag.equals("true");
+ //// getOption ///////////////////////////////// END ////////////////////
+
+ if (searchClassPath) {
+ Deployments deployment = new Deployments();
+ deployment.setClasspath(Thread.currentThread().getContextClassLoader());
+ deployments.add(deployment);
+ }
+ // resolve jar locations ////////////////////////////////////// BEGIN ///////
+
+ FileUtils base = SystemInstance.get().getBase();
+
+ List<String> jarList = new ArrayList(deployments.size());
+ try {
+ for (Deployments deployment : deployments) {
+ if (deployment.getClasspath() != null) {
+ loadFromClasspath(base, jarList, deployment.getClasspath());
+ } else {
+ loadFrom(deployment, base, jarList);
+ }
+ }
+ } catch (SecurityException se) {
+
+ }
+ return jarList;
+ }
+
+ /*
+ * The algorithm of OpenEJB deployments class-path inclusion and exclusion is implemented as follows:
+ * 1- If the string value of the resource URL matches the include class-path pattern
+ * Then load this resource
+ * 2- If the string value of the resource URL matches the exclude class-path pattern
+ * Then ignore this resource
+ * 3- If the include and exclude class-path patterns are not defined
+ * Then load this resource
+ *
+ * The previous steps are based on the following points:
+ * 1- Include class-path pattern has the highst priority
+ * This helps in case both patterns are defined using the same values.
+ * This appears in step 1 and 2 of the above algorithm.
+ * 2- Loading the resource is the default behaviour in case of not defining a value for any class-path pattern
+ * This appears in step 3 of the above algorithm.
+ */
+ private static void loadFromClasspath(FileUtils base, List<String> jarList, ClassLoader classLoader) {
+
+ Deployments deployment = null;
+ String include = null;
+ String exclude = null;
+ String path = null;
+
+ include = SystemInstance.get().getProperty(CLASSPATH_INCLUDE, "");
+ exclude = SystemInstance.get().getProperty(CLASSPATH_EXCLUDE, ".*");
+ try {
+ UrlSet urlSet = new UrlSet(classLoader);
+ UrlSet includes = urlSet.matching(include);
+ urlSet = urlSet.exclude(ClassLoader.getSystemClassLoader().getParent());
+ urlSet = urlSet.excludeJavaExtDirs();
+ urlSet = urlSet.excludeJavaEndorsedDirs();
+ urlSet = urlSet.excludeJavaHome();
+ urlSet = urlSet.excludePaths(System.getProperty("sun.boot.class.path",""));
+ urlSet = urlSet.exclude(".*/JavaVM.framework/.*");
+ urlSet = urlSet.exclude(exclude);
+ urlSet = urlSet.include(includes);
+
+ List<URL> urls = urlSet.getUrls();
+ int size = urls.size();
+ if (size == 0 && include.length() > 0){
+ DeploymentLoader.logger.warning("No classpath URLs matched. Current settings: "+CLASSPATH_EXCLUDE +"='"+exclude+"', "+CLASSPATH_INCLUDE+"='"+include+"'");
+ return;
+ } else if (size == 0) {
+ return;
+ } else if (size < 10) {
+ DeploymentLoader.logger.debug("Inspecting classpath for applications: "+urls.size()+" urls.");
+ } else if (size < 50) {
+ DeploymentLoader.logger.info("Inspecting classpath for applications: "+urls.size()+" urls. Consider adjusting your exclude/include. Current settings: "+CLASSPATH_EXCLUDE +"='"+exclude+"', "+CLASSPATH_INCLUDE+"='"+include+"'");
+ } else {
+ DeploymentLoader.logger.warning("Inspecting classpath for applications: "+urls.size()+" urls.");
+ DeploymentLoader.logger.warning("ADJUST THE EXCLUDE/INCLUDE!!!. Current settings: "+CLASSPATH_EXCLUDE +"='"+exclude+"', "+CLASSPATH_INCLUDE+"='"+include+"'");
+ }
+
+ long begin = System.currentTimeMillis();
+ for (URL url : urls) {
+ try {
+ Class moduleType = DeploymentLoader.discoverModuleType(url, classLoader);
+ if (AppModule.class.isAssignableFrom(moduleType) || EjbModule.class.isAssignableFrom(moduleType)){
+ deployment = new Deployments();
+ if (url.getProtocol().equals("jar")) {
+ url = new URL(url.getFile().replaceFirst("!.*$", ""));
+ File file = new File(url.getFile());
+ path = file.getAbsolutePath();
+ deployment.setJar(path);
+ } else if (url.getProtocol().equals("file")) {
+ File file = new File(url.getFile());
+ path = file.getAbsolutePath();
+ deployment.setDir(path);
+ } else {
+ DeploymentLoader.logger.warning("Not loading " + moduleType.getSimpleName() + ". Unknown protocol " + url.getProtocol());
+ continue;
+ }
+ DeploymentLoader.logger.info("Found " + moduleType.getSimpleName() + " in classpath: " + path);
+ loadFrom(deployment, base, jarList);
+ }
+ } catch (IOException e) {
+ DeploymentLoader.logger.warning("Unable to determine the module type of "+ url.toExternalForm()+": Exception: "+ e.getMessage(), e);
+ } catch (UnsupportedOperationException ignore) {
+ }
+ }
+ long end = System.currentTimeMillis();
+ long time = end - begin;
+
+ if (time < 1000) {
+ DeploymentLoader.logger.debug("Searched "+urls.size()+" classpath urls in "+time+" milliseconds. Average "+(time/urls.size())+" milliseconds per url.");
+ } else if (time < 4000 || urls.size() < 3) {
+ DeploymentLoader.logger.info("Searched "+urls.size()+" classpath urls in "+time+" milliseconds. Average "+(time/urls.size())+" milliseconds per url.");
+ } else if (time < 10000){
+ DeploymentLoader.logger.warning("Searched "+urls.size()+" classpath urls in "+time+" milliseconds. Average "+(time/urls.size())+" milliseconds per url.");
+ DeploymentLoader.logger.warning("Consider adjusting your "+CLASSPATH_EXCLUDE +" and "+CLASSPATH_INCLUDE +" settings. Current settings: exclude='"+exclude+"', include='"+include+"'");
+ } else {
+ DeploymentLoader.logger.fatal("Searched "+urls.size()+" classpath urls in "+time+" milliseconds. Average "+(time/urls.size())+" milliseconds per url. TOO LONG!");
+ DeploymentLoader.logger.fatal("ADJUST THE EXCLUDE/INCLUDE!!!. Current settings: "+CLASSPATH_EXCLUDE +"='"+exclude+"', "+CLASSPATH_INCLUDE+"='"+include+"'");
+ List<String> list = new ArrayList<String>();
+ for (URL url : urls) {
+ list.add(url.toExternalForm());
+ }
+ Collections.sort(list);
+ for (String url : list) {
+ DeploymentLoader.logger.info("Matched: "+url);
+ }
+ }
+ } catch (IOException e1) {
+ e1.printStackTrace();
+ DeploymentLoader.logger.warning("Unable to search classpath for modules: Received Exception: " + e1.getClass().getName() + " " + e1.getMessage(), e1);
+ }
+
+ }
+}
Added: incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/alt/config/JarExtractor.java
URL: http://svn.apache.org/viewvc/incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/alt/config/JarExtractor.java?view=auto&rev=495832
==============================================================================
--- incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/alt/config/JarExtractor.java (added)
+++ incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/alt/config/JarExtractor.java Fri Jan 12 19:44:26 2007
@@ -0,0 +1,273 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.alt.config;
+
+import org.apache.openejb.loader.FileUtils;
+import org.apache.openejb.loader.SystemInstance;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.BufferedOutputStream;
+import java.net.URL;
+import java.net.JarURLConnection;
+import java.util.jar.JarFile;
+import java.util.jar.JarEntry;
+import java.util.Enumeration;
+import java.nio.channels.FileChannel;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class JarExtractor {
+
+ /**
+ * Extract the WAR file found at the specified URL into an unpacked
+ * directory structure, and return the absolute pathname to the extracted
+ * directory.
+ *
+ * @param jar URL of the web application archive to be extracted
+ * (must start with "jar:")
+ * @param pathname Context path name for web application
+ * @param file
+ * @throws IllegalArgumentException if this is not a "jar:" URL
+ * @throws java.io.IOException if an input/output error was encountered
+ * during expansion
+ */
+ public static File extract(URL jar, String pathname, File file)
+ throws IOException {
+
+ // Make sure that there is no such directory already existing
+ FileUtils base = SystemInstance.get().getBase();
+ File appBase = base.getDirectory("apps", true);
+ if (!appBase.exists() || !appBase.isDirectory()) {
+ throw new IOException("" + appBase.getAbsolutePath());
+ }
+
+ File docBase = new File(appBase, pathname);
+ if (docBase.exists()) {
+ // Ear file is already installed
+ return docBase;
+ }
+
+ DeploymentLoader.logger.info("Extracting jar: " + file.getAbsolutePath());
+
+ // Create the new document base directory
+ docBase.mkdir();
+
+ // Extract the JAR into the new directory
+ JarURLConnection jarURLConnection = (JarURLConnection) jar.openConnection();
+ jarURLConnection.setUseCaches(false);
+ JarFile jarFile = null;
+ InputStream input = null;
+ try {
+ jarFile = jarURLConnection.getJarFile();
+ Enumeration jarEntries = jarFile.entries();
+ while (jarEntries.hasMoreElements()) {
+ JarEntry jarEntry = (JarEntry) jarEntries.nextElement();
+ String name = jarEntry.getName();
+ int last = name.lastIndexOf('/');
+ if (last >= 0) {
+ File parent = new File(docBase,
+ name.substring(0, last));
+ parent.mkdirs();
+ }
+ if (name.endsWith("/")) {
+ continue;
+ }
+ input = jarFile.getInputStream(jarEntry);
+
+ File extractedFile = extract(input, docBase, name);
+ long lastModified = jarEntry.getTime();
+ if ((lastModified != -1) && (lastModified != 0) && (extractedFile != null)) {
+ extractedFile.setLastModified(lastModified);
+ }
+
+ input.close();
+ input = null;
+ }
+ } catch (IOException e) {
+ // If something went wrong, delete extracted dir to keep things
+ // clean
+ deleteDir(docBase);
+ throw e;
+ } finally {
+ if (input != null) {
+ try {
+ input.close();
+ } catch (Throwable t) {
+ ;
+ }
+ input = null;
+ }
+ if (jarFile != null) {
+ try {
+ jarFile.close();
+ } catch (Throwable t) {
+ ;
+ }
+ jarFile = null;
+ }
+ }
+
+ // Return the absolute path to our new document base directory
+ DeploymentLoader.logger.info("Extracted path: " + docBase.getAbsolutePath());
+
+ return docBase;
+
+ }
+
+
+ /**
+ * Copy the specified file or directory to the destination.
+ *
+ * @param src File object representing the source
+ * @param dest File object representing the destination
+ */
+ public static boolean copy(File src, File dest) {
+
+ boolean result = true;
+
+ String files[] = null;
+ if (src.isDirectory()) {
+ files = src.list();
+ result = dest.mkdir();
+ } else {
+ files = new String[1];
+ files[0] = "";
+ }
+ if (files == null) {
+ files = new String[0];
+ }
+ for (int i = 0; (i < files.length) && result; i++) {
+ File fileSrc = new File(src, files[i]);
+ File fileDest = new File(dest, files[i]);
+ if (fileSrc.isDirectory()) {
+ result = copy(fileSrc, fileDest);
+ } else {
+ FileChannel ic = null;
+ FileChannel oc = null;
+ try {
+ ic = (new FileInputStream(fileSrc)).getChannel();
+ oc = (new FileOutputStream(fileDest)).getChannel();
+ ic.transferTo(0, ic.size(), oc);
+ } catch (IOException e) {
+ DeploymentLoader.logger.error("Copy failed: src: " + fileSrc + ", dest: " + fileDest, e);
+ result = false;
+ } finally {
+ if (ic != null) {
+ try {
+ ic.close();
+ } catch (IOException e) {
+ }
+ }
+ if (oc != null) {
+ try {
+ oc.close();
+ } catch (IOException e) {
+ }
+ }
+ }
+ }
+ }
+ return result;
+
+ }
+
+
+ /**
+ * Delete the specified directory, including all of its contents and
+ * subdirectories recursively.
+ *
+ * @param dir File object representing the directory to be deleted
+ */
+ public static boolean delete(File dir) {
+ if (dir.isDirectory()) {
+ return deleteDir(dir);
+ } else {
+ return dir.delete();
+ }
+ }
+
+
+ /**
+ * Delete the specified directory, including all of its contents and
+ * subdirectories recursively.
+ *
+ * @param dir File object representing the directory to be deleted
+ */
+ public static boolean deleteDir(File dir) {
+
+ String files[] = dir.list();
+ if (files == null) {
+ files = new String[0];
+ }
+ for (int i = 0; i < files.length; i++) {
+ File file = new File(dir, files[i]);
+ if (file.isDirectory()) {
+ deleteDir(file);
+ } else {
+ file.delete();
+ }
+ }
+ return dir.delete();
+
+ }
+
+
+ /**
+ * Extract the specified input stream into the specified directory, creating
+ * a file named from the specified relative path.
+ *
+ * @param input InputStream to be copied
+ * @param docBase Document base directory into which we are extracting
+ * @param name Relative pathname of the file to be created
+ * @return A handle to the extracted File
+ * @throws java.io.IOException if an input/output error occurs
+ */
+ protected static File extract(InputStream input, File docBase, String name)
+ throws IOException {
+
+ File file = new File(docBase, name);
+ BufferedOutputStream output = null;
+ try {
+ output =
+ new BufferedOutputStream(new FileOutputStream(file));
+ byte buffer[] = new byte[2048];
+ while (true) {
+ int n = input.read(buffer);
+ if (n <= 0)
+ break;
+ output.write(buffer, 0, n);
+ }
+ } finally {
+ if (output != null) {
+ try {
+ output.close();
+ } catch (IOException e) {
+ // Ignore
+ }
+ }
+ }
+
+ return file;
+ }
+
+
+}