Skip to content
Browse files

This makes a future version of bultitude.core/namespaces-on-classpath…

… work [IMMUTANT-115]

It requires a new release of bultitude with this PR: Raynes/bultitude#12

Once that happens, I'll write a test for this.
  • Loading branch information...
1 parent 898258a commit f9d9efdee7cc45c8b5b1a3f50e4880b4de7c8e2b @tobias tobias committed Sep 17, 2012
View
2 integration-tests/src/test/clojure/immutant/integs/integ_helper.clj
@@ -18,4 +18,4 @@
(ns immutant.integs.integ-helper)
(def deployment-class-loader-regex
- #"VFSStrippingClassLoader.*deployment\..*\.clj")
+ #"ImmutantClassLoader.*deployment\..*\.clj")
View
12 modules/core/src/main/clojure/immutant/runtime.clj
@@ -19,6 +19,7 @@
"This namespace is solely for use during the application runtime
bootstrapping process. Applications shouldn't use anything here."
(:require [clojure.string :as str]
+ [clojure.java.io :as io]
[clojure.tools.logging :as log]
[immutant.repl :as repl]
[immutant.utilities :as util]
@@ -31,8 +32,17 @@ bootstrapping process. Applications shouldn't use anything here."
(require namespace)
(apply (intern namespace function) args)))
+(defn ^{:private true} bultitudinize-class-loader []
+ (when-let [p (util/try-resolve 'bultitude.core/ProvidesClasspath)]
+ (log/info
+ "bultitude.core/ProvidesClasspath is available, implementing for ImmutantClassLoader")
+ (extend org.immutant.core.ImmutantClassLoader
+ @p
+ {:get-classpath #(map io/as-file (.getResourcePaths %))})))
+
(defn ^{:private true} post-initialize
[config]
+ (bultitudinize-class-loader)
(repl/init-repl config))
(defn ^{:internal true} initialize
@@ -56,3 +66,5 @@ to finalize initialization."
"Takes the full application config as a data string and makes it available as data under the :config key in the registry."
[config]
(registry/put :config (read-string config)))
+
+
View
2 modules/core/src/main/java/org/immutant/core/ClassLoaderUtils.java
@@ -31,7 +31,7 @@ public static ClassLoader getModuleLoader(DeploymentUnit unit) {
Module module = unit.getAttachment( Attachments.MODULE );
if (module != null) {
- return new VFSStrippingClassLoader( module.getClassLoader(),
+ return new ImmutantClassLoader( module.getClassLoader(),
unit.getAttachment( TmpResourceMountMap.ATTACHMENT_KEY ) );
} else {
// this won't happen in production, but helps testing
View
148 modules/core/src/main/java/org/immutant/core/ImmutantClassLoader.java
@@ -0,0 +1,148 @@
+/*
+ * Copyright 2008-2012 Red Hat, Inc, and individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.immutant.core;
+
+import java.io.File;
+import java.lang.reflect.InvocationTargetException;
+import java.net.MalformedURLException;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.jboss.modules.ModuleClassLoader;
+import org.jboss.modules.Resource;
+import org.jboss.modules.ResourceLoader;
+import org.projectodd.polyglot.core.util.ResourceLoaderUtil;
+
+
+/**
+ * A ClassLoader wrapper that lets us intercept various parts of the class loading process.
+ * Currently, this just turns vfs: urls into file: urls for files that exist outside of an archive and
+ * provides a mechanism for accessing all of the paths used by the parent ModuleClassLoader.
+ *
+ * @author tcrawley@redhat.com
+ *
+ */
+public class ImmutantClassLoader extends ClassLoader {
+
+ public ImmutantClassLoader(ClassLoader parent, TmpResourceMountMap mountMap) {
+ super( parent );
+ this.mountMap = mountMap;
+ }
+
+ @Override
+ public URL getResource(String name) {
+ URL url = getParent().getResource( name );
+
+ return stripVFS( url );
+ }
+
+ public List<URL> getResourcePaths() {
+ ArrayList<URL> urls = new ArrayList<URL>();
+ try {
+ ResourceLoader[] loaders = ResourceLoaderUtil.getExistingResourceLoaders( (ModuleClassLoader)getParent() );
+
+ for (ResourceLoader loader : loaders) {
+ Resource resource = loader.getResource( "/" );
+ if (resource != null) {
+ URL url = resource.getURL();
+ File file = getActualFile( url );
+
+ if (file != null) {
+ url = file.toURI().toURL();
+ } else {
+ url = stripVFSFromNonJar( url );
+ }
+
+ if (url != null) {
+ urls.add( url );
+ }
+ }
+ }
+
+ } catch (IllegalAccessException e) {
+ e.printStackTrace();
+ } catch (InvocationTargetException e) {
+ e.printStackTrace();
+ } catch (NoSuchMethodException e) {
+ e.printStackTrace();
+ } catch (MalformedURLException e) {
+ e.printStackTrace();
+ }
+
+ return urls;
+ }
+
+ private File getActualFile(URL url) {
+ String urlAsString = url.toExternalForm();
+ int splitPoint = urlAsString.indexOf( ".jar/" ) + 5;
+ String jarPrefix = urlAsString.substring( 0, splitPoint );
+
+ return this.mountMap.getActualFile( jarPrefix );
+ }
+
+ private String getFileNameFromURL(URL url) {
+ String urlAsString = url.toExternalForm();
+ int splitPoint = urlAsString.indexOf( ".jar/" ) + 5;
+
+ return urlAsString.substring( splitPoint );
+ }
+
+ private URL stripVFSFromNonJar(URL url) {
+ try {
+ URL nonVFSUrl = new URL( "file" + url.toExternalForm().substring( 3 ) );
+ if ((new File( nonVFSUrl.toURI() )).exists()) {
+ return nonVFSUrl;
+ }
+ } catch (URISyntaxException e) {
+ e.printStackTrace();
+ } catch (MalformedURLException e) {
+ e.printStackTrace();
+ }
+
+ return null;
+ }
+
+ protected URL stripVFS(URL url) {
+ if (url != null && "vfs".equals( url.getProtocol() )) {
+ try {
+ File actualFile = getActualFile( url );
+ if (actualFile != null) {
+ //translate resources within tmp mounted jars to the actual path. see IMMUTANT-70
+ url = new URL( "jar:" + actualFile.toURI().toURL().toExternalForm() + "!/" + getFileNameFromURL( url ) );
+ } else {
+ return stripVFSFromNonJar( url );
+ }
+ } catch (MalformedURLException e) {
+ e.printStackTrace();
+ }
+ }
+
+ return url;
+ }
+
+ public String toString() {
+ return ImmutantClassLoader.class.getName() + "[" + getParent().toString() + "]";
+ }
+
+ private TmpResourceMountMap mountMap;
+
+}
View
76 modules/core/src/main/java/org/immutant/core/VFSStrippingClassLoader.java
@@ -1,76 +0,0 @@
-/*
- * Copyright 2008-2012 Red Hat, Inc, and individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-
-package org.immutant.core;
-
-import java.io.File;
-import java.net.MalformedURLException;
-import java.net.URISyntaxException;
-import java.net.URL;
-
-
-/**
- * Turns vfs: urls into file: urls for files that exist outside of an archive.
- *
- * @author tcrawley@redhat.com
- *
- */
-public class VFSStrippingClassLoader extends ClassLoader {
-
- public VFSStrippingClassLoader(ClassLoader parent, TmpResourceMountMap mountMap) {
- super( parent );
- this.mountMap = mountMap;
- }
-
- @Override
- public URL getResource(String name) {
- URL url = getParent().getResource( name );
- if (url != null && "vfs".equals( url.getProtocol() )) {
- try {
- String urlAsString = url.toExternalForm();
- int splitPoint = urlAsString.indexOf( ".jar/" ) + 5;
- String jarPrefix = urlAsString.substring( 0, splitPoint );
- String fileName = urlAsString.substring( splitPoint );
- File actualFile = this.mountMap.getActualFile( jarPrefix );
- if (actualFile != null) {
- //translate resources within tmp mounted jars to the actual path. see IMMUTANT-70
- url = new URL( "jar:" + actualFile.toURI().toURL().toExternalForm() + "!/" + fileName );
- } else {
- URL nonVFSUrl = new URL( "file" + url.toExternalForm().substring( 3 ) );
- if ((new File( nonVFSUrl.toURI() )).exists()) {
- url = nonVFSUrl;
- }
- }
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (URISyntaxException e) {
- e.printStackTrace();
- }
- }
-
- return url;
- }
-
- public String toString() {
- return VFSStrippingClassLoader.class.getName() + "[" + getParent().toString() + "]";
- }
-
- private TmpResourceMountMap mountMap;
-
-}
View
2 pom.xml
@@ -301,7 +301,7 @@
</dependencyManagement>
<properties>
- <version.polyglot>1.7.2</version.polyglot>
+ <version.polyglot>1.7.3-SNAPSHOT</version.polyglot>
<version.junit>4.7</version.junit>
<version.jmock>2.5.1</version.jmock>
<version.mockito>1.8.4</version.mockito>

0 comments on commit f9d9efd

Please sign in to comment.
Something went wrong with that request. Please try again.