Permalink
Browse files

MODULES-135 - ensure that TCCL is set in time for class init to happe…

…n on main methods
  • Loading branch information...
1 parent 6b1a1df commit 70e82924ee2d810d46e38f8a8112489a750396a4 @dmlloyd dmlloyd committed May 22, 2012
@@ -33,7 +33,6 @@
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.security.AccessController;
-import java.security.PrivilegedAction;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.ServiceLoader;
@@ -44,6 +43,8 @@
import java.util.jar.Manifest;
import org.jboss.modules.log.JDKModuleLogger;
+import static org.jboss.modules.SecurityActions.setContextClassLoader;
+
/**
* The main entry point of JBoss Modules when run as a JAR on the command line.
*
@@ -357,26 +358,6 @@ private static String getServiceName(ClassLoader classLoader, String className)
return null;
}
- private static ClassLoader setContextClassLoader(final ClassLoader classLoader) {
- final SecurityManager sm = System.getSecurityManager();
- if (sm != null) {
- return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
- public ClassLoader run() {
- return doSetContextClassLoader(classLoader);
- }
- });
- }
- return doSetContextClassLoader(classLoader);
- }
-
- private static ClassLoader doSetContextClassLoader(final ClassLoader classLoader) {
- try {
- return Thread.currentThread().getContextClassLoader();
- } finally {
- Thread.currentThread().setContextClassLoader(classLoader);
- }
- }
-
private static final String JAR_NAME;
private static final String VERSION_STRING;
@@ -245,24 +245,23 @@ public void run(final String[] args) throws NoSuchMethodException, InvocationTar
if (mainClassName == null) {
throw new NoSuchMethodException("No main class defined for " + this);
}
- final Class<?> mainClass = Class.forName(mainClassName, false, moduleClassLoader);
+ final ClassLoader oldClassLoader = SecurityActions.setContextClassLoader(moduleClassLoader);
try {
- Class.forName(mainClassName, true, moduleClassLoader);
- } catch (Throwable t) {
- throw new InvocationTargetException(t, "Failed to initialize main class '" + mainClassName + "'");
- }
- final Method mainMethod = mainClass.getMethod("main", String[].class);
- final int modifiers = mainMethod.getModifiers();
- if (! Modifier.isStatic(modifiers)) {
- throw new NoSuchMethodException("Main method is not static for " + this);
- }
- final ClassLoader previousClassLoader = Thread.currentThread().getContextClassLoader();
- try {
- Thread.currentThread().setContextClassLoader(moduleClassLoader);
+ final Class<?> mainClass = Class.forName(mainClassName, false, moduleClassLoader);
+ try {
+ Class.forName(mainClassName, true, moduleClassLoader);
+ } catch (Throwable t) {
+ throw new InvocationTargetException(t, "Failed to initialize main class '" + mainClassName + "'");
+ }
+ final Method mainMethod = mainClass.getMethod("main", String[].class);
+ final int modifiers = mainMethod.getModifiers();
+ if (! Modifier.isStatic(modifiers)) {
+ throw new NoSuchMethodException("Main method is not static for " + this);
+ }
// ignore the return value
mainMethod.invoke(null, new Object[] {args});
} finally {
- Thread.currentThread().setContextClassLoader(previousClassLoader);
+ SecurityActions.setContextClassLoader(oldClassLoader);
}
} catch (IllegalAccessException e) {
// unexpected; should be public
@@ -0,0 +1,73 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2012, Red Hat, Inc., and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of 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.jboss.modules;
+
+import java.security.PrivilegedAction;
+
+import static java.lang.System.getSecurityManager;
+import static java.lang.Thread.currentThread;
+import static java.security.AccessController.doPrivileged;
+
+/**
+ * This class must not be public.
+ *
+ * @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
+ */
+final class SecurityActions {
+
+ private static final PrivilegedAction<ClassLoader> GET_LOADER_ACTION = new PrivilegedAction<ClassLoader>() {
+ public ClassLoader run() {
+ return currentThread().getContextClassLoader();
+ }
+ };
+
+ static ClassLoader setContextClassLoader(final ClassLoader classLoader) {
+ final SecurityManager sm = getSecurityManager();
+ if (sm != null) {
+ return doPrivileged(new PrivilegedAction<ClassLoader>() {
+ public ClassLoader run() {
+ try {
+ return currentThread().getContextClassLoader();
+ } finally {
+ currentThread().setContextClassLoader(classLoader);
+ }
+ }
+ });
+ } else {
+ try {
+ return currentThread().getContextClassLoader();
+ } finally {
+ currentThread().setContextClassLoader(classLoader);
+ }
+ }
+ }
+
+ static ClassLoader getContextClassLoader() {
+ final SecurityManager sm = getSecurityManager();
+ if (sm != null) {
+ return doPrivileged(GET_LOADER_ACTION);
+ } else {
+ return currentThread().getContextClassLoader();
+ }
+ }
+}

0 comments on commit 70e8292

Please sign in to comment.