Created
July 7, 2014 17:58
-
-
Save gissuebot/6705d79db7d07a28c4a6 to your computer and use it in GitHub Desktop.
Migrated attachment for Guice issue 94, comment 24
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Index: src/META-INF/services/com.google.inject.ClassLoaderFactory | |
=================================================================== | |
--- src/META-INF/services/com.google.inject.ClassLoaderFactory (revision 0) | |
+++ src/META-INF/services/com.google.inject.ClassLoaderFactory (revision 0) | |
@@ -0,0 +1 @@ | |
+com.google.inject.internal.DefaultClassLoaderFactory | |
Index: src/META-INF/MANIFEST.MF | |
=================================================================== | |
--- src/META-INF/MANIFEST.MF (revision 0) | |
+++ src/META-INF/MANIFEST.MF (revision 0) | |
@@ -0,0 +1,31 @@ | |
+Bundle-ManifestVersion: 2 | |
+Bundle-Name: Google-Guice | |
+Bundle-Description: Guice is a lightweight dependency injection framework for Java 5 | |
+Bundle-License: http://www.apache.org/licenses/LICENSE-2.0 | |
+Bundle-DocURL: http://code.google.com/p/google-guice | |
+Bundle-SymbolicName: com.google.inject | |
+Bundle-Vendor: Google Inc. | |
+Import-Package: | |
+ javax.management;resolution:=optional, | |
+ javax.naming;resolution:=optional, | |
+ org.aopalliance.aop, | |
+ org.aopalliance.intercept, | |
+ com.google.inject, | |
+ com.google.inject.spi, | |
+ com.google.inject.matcher, | |
+ com.google.inject.introspect, | |
+ com.google.inject.tools.jmx, | |
+ com.google.inject.util, | |
+ com.google.inject.binder, | |
+ com.google.inject.name, | |
+ com.google.inject.jndi | |
+Export-Package: | |
+ com.google.inject, | |
+ com.google.inject.spi, | |
+ com.google.inject.matcher, | |
+ com.google.inject.introspect, | |
+ com.google.inject.tools.jmx, | |
+ com.google.inject.util, | |
+ com.google.inject.binder, | |
+ com.google.inject.name, | |
+ com.google.inject.jndi | |
Index: src/com/google/inject/internal/GuiceFastClass.java | |
=================================================================== | |
--- src/com/google/inject/internal/GuiceFastClass.java (revision 345) | |
+++ src/com/google/inject/internal/GuiceFastClass.java (working copy) | |
@@ -16,6 +16,7 @@ | |
package com.google.inject.internal; | |
+import com.google.inject.ClassLoaderFactory; | |
import net.sf.cglib.reflect.FastClass; | |
/** | |
@@ -25,11 +26,11 @@ | |
*/ | |
public class GuiceFastClass { | |
- public static FastClass create(Class type) { | |
- return create(type.getClassLoader(), type); | |
+ public static FastClass create(Class<?> type) { | |
+ return create(ClassLoaderFactory.getClassLoader(type), type); | |
} | |
- public static FastClass create(ClassLoader loader, Class type) { | |
+ public static FastClass create(ClassLoader loader, Class<?> type) { | |
FastClass.Generator generator = new FastClass.Generator(); | |
generator.setType(type); | |
generator.setClassLoader(loader); | |
Index: src/com/google/inject/internal/GuiceEnhancer.java | |
=================================================================== | |
--- src/com/google/inject/internal/GuiceEnhancer.java (revision 0) | |
+++ src/com/google/inject/internal/GuiceEnhancer.java (revision 0) | |
@@ -0,0 +1,33 @@ | |
+/** | |
+ * Copyright (C) 2007 Stuart McCulloch. | |
+ * | |
+ * Licensed 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 com.google.inject.internal; | |
+ | |
+import com.google.inject.ClassLoaderFactory; | |
+import net.sf.cglib.proxy.Enhancer; | |
+ | |
+/** | |
+ * Adapt CGLIB enhancer to allow custom classloading in Guice. | |
+ * | |
+ * @author stuart.mcculloch@jayway.net (Stuart McCulloch) | |
+ */ | |
+public class GuiceEnhancer extends Enhancer | |
+{ | |
+ @Override | |
+ protected ClassLoader getDefaultClassLoader() { | |
+ return ClassLoaderFactory.getClassLoader(super.getDefaultClassLoader()); | |
+ } | |
+} | |
Property changes on: src/com/google/inject/internal/GuiceEnhancer.java | |
___________________________________________________________________ | |
Name: svn:eol-style | |
+ native | |
Index: src/com/google/inject/internal/ContainerClassLoaderFactory.java | |
=================================================================== | |
--- src/com/google/inject/internal/ContainerClassLoaderFactory.java (revision 0) | |
+++ src/com/google/inject/internal/ContainerClassLoaderFactory.java (revision 0) | |
@@ -0,0 +1,65 @@ | |
+/** | |
+ * Copyright (C) 2007 Stuart McCulloch. | |
+ * | |
+ * Licensed 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 com.google.inject.internal; | |
+ | |
+import com.google.inject.ClassLoaderFactory; | |
+import java.util.WeakHashMap; | |
+import java.util.Map; | |
+ | |
+/** | |
+ * Delegating implementation of Guice classloading for use in containers. | |
+ * | |
+ * @author stuart.mcculloch@jayway.net (Stuart McCulloch) | |
+ */ | |
+public class ContainerClassLoaderFactory extends ClassLoaderFactory | |
+{ | |
+ private Map<ClassLoader,ClassLoader> delegateMap; | |
+ | |
+ public ContainerClassLoaderFactory() { | |
+ delegateMap = new WeakHashMap<ClassLoader,ClassLoader>(); | |
+ } | |
+ | |
+ @Override | |
+ public ClassLoader getClassLoader(final ClassLoader guiceLoader, ClassLoader typeLoader) { | |
+ | |
+ if (null == guiceLoader || typeLoader == guiceLoader) { | |
+ return typeLoader; | |
+ } | |
+ | |
+ ClassLoader loader = delegateMap.get(typeLoader); | |
+ if (null == loader) { | |
+ loader = new ClassLoader(typeLoader) { | |
+ | |
+ @Override | |
+ protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { | |
+ if (name.startsWith("com.google.inject")) { | |
+ Class<?> clazz = guiceLoader.loadClass(name); | |
+ if (resolve) { | |
+ super.resolveClass(clazz); | |
+ } | |
+ return clazz; | |
+ } | |
+ return super.loadClass(name, resolve); | |
+ } | |
+ }; | |
+ | |
+ delegateMap.put(typeLoader, loader); | |
+ } | |
+ | |
+ return loader; | |
+ } | |
+} | |
Property changes on: src/com/google/inject/internal/ContainerClassLoaderFactory.java | |
___________________________________________________________________ | |
Name: svn:eol-style | |
+ native | |
Index: src/com/google/inject/internal/DefaultClassLoaderFactory.java | |
=================================================================== | |
--- src/com/google/inject/internal/DefaultClassLoaderFactory.java (revision 0) | |
+++ src/com/google/inject/internal/DefaultClassLoaderFactory.java (revision 0) | |
@@ -0,0 +1,32 @@ | |
+/** | |
+ * Copyright (C) 2007 Stuart McCulloch. | |
+ * | |
+ * Licensed 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 com.google.inject.internal; | |
+ | |
+import com.google.inject.ClassLoaderFactory; | |
+ | |
+/** | |
+ * Default implementation of Guice classloading for use in normal Java applications. | |
+ * | |
+ * @author stuart.mcculloch@jayway.net (Stuart McCulloch) | |
+ */ | |
+public class DefaultClassLoaderFactory extends ClassLoaderFactory | |
+{ | |
+ @Override | |
+ public ClassLoader getClassLoader(ClassLoader guiceLoader, ClassLoader typeLoader) { | |
+ return typeLoader; | |
+ } | |
+} | |
Property changes on: src/com/google/inject/internal/DefaultClassLoaderFactory.java | |
___________________________________________________________________ | |
Name: svn:eol-style | |
+ native | |
Index: src/com/google/inject/ProxyFactory.java | |
=================================================================== | |
--- src/com/google/inject/ProxyFactory.java (revision 345) | |
+++ src/com/google/inject/ProxyFactory.java (working copy) | |
@@ -16,6 +16,7 @@ | |
package com.google.inject; | |
+import com.google.inject.internal.GuiceEnhancer; | |
import com.google.inject.internal.GuiceFastClass; | |
import com.google.inject.internal.GuiceNamingPolicy; | |
import com.google.inject.internal.Objects; | |
@@ -140,7 +141,7 @@ | |
} | |
// Create the proxied class. | |
- Enhancer enhancer = new Enhancer(); | |
+ Enhancer enhancer = new GuiceEnhancer(); | |
enhancer.setSuperclass(declaringClass); | |
enhancer.setUseCache(false); // We do enough caching. | |
enhancer.setCallbackFilter(new CallbackFilter() { | |
@@ -164,7 +165,7 @@ | |
* Creates a construction proxy given a class and parameter types. | |
*/ | |
<T> ConstructionProxy<T> createConstructionProxy(Class<?> clazz, | |
- Class[] parameterTypes) { | |
+ Class<?>[] parameterTypes) { | |
FastClass fastClass = GuiceFastClass.create(clazz); | |
final FastConstructor fastConstructor | |
= fastClass.getConstructor(parameterTypes); | |
Index: src/com/google/inject/ConstructionContext.java | |
=================================================================== | |
--- src/com/google/inject/ConstructionContext.java (revision 345) | |
+++ src/com/google/inject/ConstructionContext.java (working copy) | |
@@ -80,8 +80,9 @@ | |
= new DelegatingInvocationHandler<T>(); | |
invocationHandlers.add(invocationHandler); | |
- Object object = Proxy.newProxyInstance(expectedType.getClassLoader(), | |
- new Class[] { expectedType }, invocationHandler); | |
+ Object object = Proxy.newProxyInstance( | |
+ ClassLoaderFactory.getClassLoader(expectedType), | |
+ new Class[] { expectedType }, invocationHandler); | |
return expectedType.cast(object); | |
} | |
Index: src/com/google/inject/ClassLoaderFactory.java | |
=================================================================== | |
--- src/com/google/inject/ClassLoaderFactory.java (revision 0) | |
+++ src/com/google/inject/ClassLoaderFactory.java (revision 0) | |
@@ -0,0 +1,144 @@ | |
+/** | |
+ * Copyright (C) 2007 Stuart McCulloch. | |
+ * | |
+ * Licensed 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 com.google.inject; | |
+ | |
+import java.io.BufferedReader; | |
+import java.io.IOException; | |
+import java.io.InputStream; | |
+import java.io.InputStreamReader; | |
+import java.security.AccessController; | |
+import java.security.PrivilegedAction; | |
+ | |
+import com.google.inject.internal.GuiceFastClass; | |
+ | |
+/** | |
+ * Use factory pattern to allow custom classloading in Guice. | |
+ * | |
+ * @author stuart.mcculloch@jayway.net (Stuart McCulloch) | |
+ */ | |
+public abstract class ClassLoaderFactory | |
+{ | |
+ public static final String FACTORY_PROPERTY = ClassLoaderFactory.class.getName(); | |
+ public static final String FACTORY_DEFAULT = "com.google.inject.internal.DefaultClassLoaderFactory"; | |
+ | |
+ public static final String SERVICE_ID = "META-INF/services/" + FACTORY_PROPERTY; | |
+ | |
+ public abstract ClassLoader getClassLoader(ClassLoader guiceLoader, ClassLoader typeLoader); | |
+ | |
+ public static ClassLoader getClassLoader(ClassLoader typeLoader) { | |
+ return getInstance().getClassLoader(GuiceFastClass.class.getClassLoader(), typeLoader); | |
+ } | |
+ | |
+ public static ClassLoader getClassLoader(Class<?> type) { | |
+ return getInstance().getClassLoader(GuiceFastClass.class.getClassLoader(), type.getClassLoader()); | |
+ } | |
+ | |
+ private static ClassLoaderFactory loaderFactory; | |
+ | |
+ public static ClassLoaderFactory getInstance() { | |
+ if (null == loaderFactory) { | |
+ ClassLoader loader = getContextClassLoader(); | |
+ | |
+ String className = System.getProperty(FACTORY_PROPERTY); | |
+ if (null != className) { | |
+ loaderFactory = newFactory(className, loader); | |
+ } | |
+ if (null == loaderFactory) { | |
+ className = findService(SERVICE_ID, loader); | |
+ if (null != className) { | |
+ loaderFactory = newFactory(className, loader); | |
+ } | |
+ } | |
+ if (null == loaderFactory) { | |
+ loaderFactory = newFactory(FACTORY_DEFAULT, loader); | |
+ } | |
+ } | |
+ return loaderFactory; | |
+ } | |
+ | |
+ protected static ClassLoader getContextClassLoader() { | |
+ return AccessController.doPrivileged( | |
+ new PrivilegedAction<ClassLoader>() { | |
+ public ClassLoader run() { | |
+ return Thread.currentThread().getContextClassLoader(); | |
+ } | |
+ }); | |
+ } | |
+ | |
+ protected static ClassLoaderFactory newFactory(final String className, final ClassLoader loader) { | |
+ Object result = AccessController.doPrivileged( | |
+ new PrivilegedAction<Object>() { | |
+ public Object run() { | |
+ return createFactory(className, loader); | |
+ } | |
+ }); | |
+ | |
+ if (result instanceof ConfigurationException) { | |
+ throw (ConfigurationException)result; | |
+ } else { | |
+ return (ClassLoaderFactory)result; | |
+ } | |
+ } | |
+ | |
+ protected static Object createFactory(String className, ClassLoader loader) { | |
+ try { | |
+ Class<?> clazz = null; | |
+ if (null != loader) { | |
+ try { | |
+ clazz = loader.loadClass(className); | |
+ } catch (ClassNotFoundException e) {} | |
+ } | |
+ if (null == clazz) { | |
+ clazz = Class.forName(className); | |
+ } | |
+ return (ClassLoaderFactory)clazz.newInstance(); | |
+ } catch( Throwable e ) { | |
+ return new ConfigurationException(e); | |
+ } | |
+ } | |
+ | |
+ protected static String findService(final String serviceName, final ClassLoader loader) { | |
+ Object result = AccessController.doPrivileged( | |
+ new PrivilegedAction<Object>() { | |
+ public Object run() { | |
+ InputStream is = loader.getResourceAsStream(serviceName); | |
+ if (null != is) { | |
+ BufferedReader br = new BufferedReader(new InputStreamReader(is)); | |
+ try | |
+ { | |
+ return br.readLine(); | |
+ } | |
+ catch( IOException e ) | |
+ { | |
+ return new ConfigurationException(e); | |
+ } | |
+ finally | |
+ { | |
+ try {br.close();} catch (Exception e) {} | |
+ } | |
+ } | |
+ return null; | |
+ } | |
+ }); | |
+ | |
+ if (result instanceof ConfigurationException) { | |
+ throw (ConfigurationException)result; | |
+ } else { | |
+ return (String)result; | |
+ } | |
+ } | |
+} | |
Property changes on: src/com/google/inject/ClassLoaderFactory.java | |
___________________________________________________________________ | |
Name: svn:eol-style | |
+ native | |
Index: build.xml | |
=================================================================== | |
--- build.xml (revision 345) | |
+++ build.xml (working copy) | |
@@ -14,7 +14,8 @@ | |
<taskdef name="jarjar" classname="com.tonicsystems.jarjar.JarJarTask" | |
classpath="lib/build/jarjar-1.0rc3.jar"/> | |
<mkdir dir="${build.dir}/dist"/> | |
- <jarjar jarfile="${build.dir}/dist/guice-${version}.jar"> | |
+ <jarjar jarfile="${build.dir}/dist/guice-${version}.jar" | |
+ manifest="${src.dir}/META-INF/MANIFEST.MF"> | |
<fileset dir="${build.dir}/classes"/> | |
<zipfileset src="lib/build/cglib-2.2_beta1.jar"/> | |
<zipfileset src="lib/build/asm-2.2.3.jar"/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment