Permalink
Browse files

[AS7-5652]; set TCCL for any POJO service invocation.

  • Loading branch information...
alesj authored and ctomc committed Sep 28, 2012
1 parent ed2bc55 commit 1e054b80a4c7df6261ff5c039f69f16f7c1c6c49
@@ -22,6 +22,12 @@
package org.jboss.as.pojo.service;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+
import org.jboss.as.pojo.BeanState;
import org.jboss.as.pojo.PojoLogger;
import org.jboss.as.pojo.PojoMessages;
@@ -45,12 +51,6 @@
import org.jboss.msc.value.ImmediateValue;
import org.jboss.msc.value.Value;
-import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.Set;
-
/**
* Abstract pojo phase; it handles install/uninstall
*
@@ -70,6 +70,20 @@
protected abstract AbstractPojoPhase createNextPhase();
public void start(StartContext context) throws StartException {
+ if (module != null) {
+ final ClassLoader previous = SecurityActions.getContextClassLoader();
+ SecurityActions.setContextClassLoader(module.getClassLoader());
+ try {
+ startInternal(context);
+ } finally {
+ SecurityActions.setContextClassLoader(previous);
+ }
+ } else {
+ startInternal(context);
+ }
+ }
+
+ protected void startInternal(StartContext context) throws StartException {
try {
executeInstalls();
@@ -121,6 +135,20 @@ public Object getValue() throws IllegalStateException, IllegalArgumentException
}
public void stop(StopContext context) {
+ if (module != null) {
+ final ClassLoader previous = SecurityActions.getContextClassLoader();
+ SecurityActions.setContextClassLoader(module.getClassLoader());
+ try {
+ stopInternal(context);
+ } finally {
+ SecurityActions.setContextClassLoader(previous);
+ }
+ } else {
+ stopInternal(context);
+ }
+ }
+
+ protected void stopInternal(StopContext context) {
if (getLifecycleState().isAfter(BeanState.DESCRIBED)) {
InstancesService.removeInstance(context.getController().getServiceContainer(), getLifecycleState(), getBean());
@@ -48,20 +48,20 @@ protected void configure(boolean nullify) throws Throwable {
}
@Override
- public void start(StartContext context) throws StartException {
+ protected void startInternal(StartContext context) throws StartException {
try {
configure(false);
} catch (StartException t) {
throw t;
} catch (Throwable t) {
throw new StartException(t);
}
- super.start(context);
+ super.startInternal(context);
}
@Override
- public void stop(StopContext context) {
- super.stop(context);
+ protected void stopInternal(StopContext context) {
+ super.stopInternal(context);
try {
configure(true);
} catch (Throwable ignored) {
@@ -61,7 +61,7 @@ protected AbstractPojoPhase createNextPhase() {
}
@SuppressWarnings("unchecked")
- public void start(StartContext context) throws StartException {
+ protected void startInternal(StartContext context) throws StartException {
try {
setModule(getBeanConfig().getModule().getInjectedModule().getValue());
String beanClass = getBeanConfig().getBeanClass();
@@ -72,7 +72,7 @@ public void start(StartContext context) throws StartException {
} catch (Exception e) {
throw new StartException(e);
}
- super.start(context);
+ super.startInternal(context);
}
public BeanInfo getValue() throws IllegalStateException, IllegalArgumentException {
@@ -48,7 +48,8 @@ protected AbstractPojoPhase createNextPhase() {
return new ConfiguredPojoPhase();
}
- public void start(StartContext context) throws StartException {
+ @Override
+ protected void startInternal(StartContext context) throws StartException {
try {
BeanInfo beanInfo = getBeanInfo();
setBean(BeanUtils.instantiateBean(getBeanConfig(), beanInfo, getIndex(), getModule()));
@@ -64,6 +65,6 @@ public void start(StartContext context) throws StartException {
} catch (Throwable t) {
throw new StartException(t);
}
- super.start(context);
+ super.startInternal(context);
}
}
@@ -43,18 +43,18 @@ protected void dispatchJoinpoint(LifecycleConfig config, String defaultMethod) t
}
@Override
- public void start(StartContext context) throws StartException {
+ protected void startInternal(StartContext context) throws StartException {
try {
dispatchJoinpoint(getUpConfig(), defaultUp());
} catch (Throwable t) {
throw new StartException(t);
}
- super.start(context);
+ super.startInternal(context);
}
@Override
- public void stop(StopContext context) {
- super.stop(context);
+ protected void stopInternal(StopContext context) {
+ super.stopInternal(context);
try {
dispatchJoinpoint(getDownConfig(), defaultDown());
} catch (Throwable t) {
@@ -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.as.pojo.service;
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
+/**
+ * SecurityActions.
+ *
+ * @author <a href="mailto:ales.justin@jboss.org">Ales Justin</a>
+ */
+final class SecurityActions {
+ private SecurityActions() {
+ // forbidden inheritance
+ }
+
+ /**
+ * Gets context classloader.
+ *
+ * @return the current context classloader
+ */
+ static ClassLoader getContextClassLoader() {
+ if (System.getSecurityManager() == null) {
+ return Thread.currentThread().getContextClassLoader();
+ } else {
+ return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
+ public ClassLoader run() {
+ return Thread.currentThread().getContextClassLoader();
+ }
+ });
+ }
+ }
+
+ /**
+ * Sets context classloader.
+ *
+ * @param classLoader
+ * the classloader
+ */
+ static void setContextClassLoader(final ClassLoader classLoader) {
+ if (System.getSecurityManager() == null) {
+ Thread.currentThread().setContextClassLoader(classLoader);
+ } else {
+ AccessController.doPrivileged(new PrivilegedAction<Object>() {
+ public Object run() {
+ Thread.currentThread().setContextClassLoader(classLoader);
+ return null;
+ }
+ });
+ }
+ }
+}
@@ -0,0 +1,36 @@
+/*
+ * 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.as.test.integration.pojo.support;
+
+import java.net.URL;
+
+/**
+ * @author <a href="mailto:ales.justin@jboss.org">Ales Justin</a>
+ */
+public class TcclChecker {
+ public void start() {
+ URL url = Thread.currentThread().getContextClassLoader().getResource("tccl.txt");
+ if (url == null)
+ throw new IllegalArgumentException("tccl.txt should not be null!");
+ }
+}
@@ -0,0 +1,54 @@
+/*
+ * 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.as.test.integration.pojo.test;
+
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.container.test.api.OperateOnDeployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.as.test.integration.pojo.support.TFactory;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.StringAsset;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * @author <a href="mailto:ales.justin@jboss.org">Ales Justin</a>
+ */
+@RunWith(Arquillian.class)
+public class TcclBeansTestCase {
+ @Deployment(name = "tccl-beans")
+ public static JavaArchive getTcclBeansJar() {
+ JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "tccl-beans.jar");
+ archive.addPackage(TFactory.class.getPackage());
+ archive.addAsManifestResource(TcclBeansTestCase.class.getPackage(), "tccl-jboss-beans.xml", "tccl-jboss-beans.xml");
+ archive.addAsResource(new StringAsset("tccl"), "tccl.txt");
+ return archive;
+ }
+
+ @Test
+ @OperateOnDeployment("tccl-beans")
+ public void testTcclBeans() throws Exception {
+ // TODO -- try to get beans?
+ }
+}
@@ -37,7 +37,7 @@
@RunWith(Arquillian.class)
public class TypeBeansTestCase {
@Deployment(name = "type-beans")
- public static JavaArchive getCycleBeansJar() {
+ public static JavaArchive getTypeBeansJar() {
JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "type-beans.jar");
archive.addPackage(TFactory.class.getPackage());
archive.addAsManifestResource(TypeBeansTestCase.class.getPackage(), "type-jboss-beans.xml", "type-jboss-beans.xml");
@@ -0,0 +1,28 @@
+<!--
+ ~ 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.
+ -->
+<deployment xmlns="urn:jboss:pojo:7.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="urn:jboss:pojo:7.0 jboss-mc_7_0.xsd">
+
+ <bean name="tccl" class="org.jboss.as.test.integration.pojo.support.TcclChecker"/>
+
+</deployment>

0 comments on commit 1e054b8

Please sign in to comment.