Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 1e054b80a4c7df6261ff5c039f69f16f7c1c6c49 1 parent ed2bc55
Aleš Justin authored September 28, 2012 ctomc committed November 20, 2012
40  pojo/src/main/java/org/jboss/as/pojo/service/AbstractPojoPhase.java
@@ -22,6 +22,12 @@
22 22
 
23 23
 package org.jboss.as.pojo.service;
24 24
 
  25
+import java.lang.reflect.Method;
  26
+import java.util.ArrayList;
  27
+import java.util.Collections;
  28
+import java.util.List;
  29
+import java.util.Set;
  30
+
25 31
 import org.jboss.as.pojo.BeanState;
26 32
 import org.jboss.as.pojo.PojoLogger;
27 33
 import org.jboss.as.pojo.PojoMessages;
@@ -45,12 +51,6 @@
45 51
 import org.jboss.msc.value.ImmediateValue;
46 52
 import org.jboss.msc.value.Value;
47 53
 
48  
-import java.lang.reflect.Method;
49  
-import java.util.ArrayList;
50  
-import java.util.Collections;
51  
-import java.util.List;
52  
-import java.util.Set;
53  
-
54 54
 /**
55 55
  * Abstract pojo phase; it handles install/uninstall
56 56
  *
@@ -70,6 +70,20 @@
70 70
     protected abstract AbstractPojoPhase createNextPhase();
71 71
 
72 72
     public void start(StartContext context) throws StartException {
  73
+        if (module != null) {
  74
+            final ClassLoader previous = SecurityActions.getContextClassLoader();
  75
+            SecurityActions.setContextClassLoader(module.getClassLoader());
  76
+            try {
  77
+                startInternal(context);
  78
+            } finally {
  79
+                SecurityActions.setContextClassLoader(previous);
  80
+            }
  81
+        } else {
  82
+            startInternal(context);
  83
+        }
  84
+    }
  85
+
  86
+    protected void startInternal(StartContext context) throws StartException {
73 87
         try {
74 88
             executeInstalls();
75 89
 
@@ -121,6 +135,20 @@ public Object getValue() throws IllegalStateException, IllegalArgumentException
121 135
     }
122 136
 
123 137
     public void stop(StopContext context) {
  138
+        if (module != null) {
  139
+            final ClassLoader previous = SecurityActions.getContextClassLoader();
  140
+            SecurityActions.setContextClassLoader(module.getClassLoader());
  141
+            try {
  142
+                stopInternal(context);
  143
+            } finally {
  144
+                SecurityActions.setContextClassLoader(previous);
  145
+            }
  146
+        } else {
  147
+            stopInternal(context);
  148
+        }
  149
+    }
  150
+
  151
+    protected void stopInternal(StopContext context) {
124 152
         if (getLifecycleState().isAfter(BeanState.DESCRIBED)) {
125 153
             InstancesService.removeInstance(context.getController().getServiceContainer(), getLifecycleState(), getBean());
126 154
 
8  pojo/src/main/java/org/jboss/as/pojo/service/ConfiguredPojoPhase.java
@@ -48,7 +48,7 @@ protected void configure(boolean nullify) throws Throwable {
48 48
     }
49 49
 
50 50
     @Override
51  
-    public void start(StartContext context) throws StartException {
  51
+    protected void startInternal(StartContext context) throws StartException {
52 52
         try {
53 53
             configure(false);
54 54
         } catch (StartException t) {
@@ -56,12 +56,12 @@ public void start(StartContext context) throws StartException {
56 56
         } catch (Throwable t) {
57 57
             throw new StartException(t);
58 58
         }
59  
-        super.start(context);
  59
+        super.startInternal(context);
60 60
     }
61 61
 
62 62
     @Override
63  
-    public void stop(StopContext context) {
64  
-        super.stop(context);
  63
+    protected void stopInternal(StopContext context) {
  64
+        super.stopInternal(context);
65 65
         try {
66 66
             configure(true);
67 67
         } catch (Throwable ignored) {
4  pojo/src/main/java/org/jboss/as/pojo/service/DescribedPojoPhase.java
@@ -61,7 +61,7 @@ protected AbstractPojoPhase createNextPhase() {
61 61
     }
62 62
 
63 63
     @SuppressWarnings("unchecked")
64  
-    public void start(StartContext context) throws StartException {
  64
+    protected void startInternal(StartContext context) throws StartException {
65 65
         try {
66 66
             setModule(getBeanConfig().getModule().getInjectedModule().getValue());
67 67
             String beanClass = getBeanConfig().getBeanClass();
@@ -72,7 +72,7 @@ public void start(StartContext context) throws StartException {
72 72
         } catch (Exception e) {
73 73
             throw new StartException(e);
74 74
         }
75  
-        super.start(context);
  75
+        super.startInternal(context);
76 76
     }
77 77
 
78 78
     public BeanInfo getValue() throws IllegalStateException, IllegalArgumentException {
5  pojo/src/main/java/org/jboss/as/pojo/service/InstantiatedPojoPhase.java
@@ -48,7 +48,8 @@ protected AbstractPojoPhase createNextPhase() {
48 48
         return new ConfiguredPojoPhase();
49 49
     }
50 50
 
51  
-    public void start(StartContext context) throws StartException {
  51
+    @Override
  52
+    protected void startInternal(StartContext context) throws StartException {
52 53
         try {
53 54
             BeanInfo beanInfo = getBeanInfo();
54 55
             setBean(BeanUtils.instantiateBean(getBeanConfig(), beanInfo, getIndex(), getModule()));
@@ -64,6 +65,6 @@ public void start(StartContext context) throws StartException {
64 65
         } catch (Throwable t) {
65 66
             throw new StartException(t);
66 67
         }
67  
-        super.start(context);
  68
+        super.startInternal(context);
68 69
     }
69 70
 }
8  pojo/src/main/java/org/jboss/as/pojo/service/LifecyclePojoPhase.java
@@ -43,18 +43,18 @@ protected void dispatchJoinpoint(LifecycleConfig config, String defaultMethod) t
43 43
     }
44 44
 
45 45
     @Override
46  
-    public void start(StartContext context) throws StartException {
  46
+    protected void startInternal(StartContext context) throws StartException {
47 47
         try {
48 48
             dispatchJoinpoint(getUpConfig(), defaultUp());
49 49
         } catch (Throwable t) {
50 50
             throw new StartException(t);
51 51
         }
52  
-        super.start(context);
  52
+        super.startInternal(context);
53 53
     }
54 54
 
55 55
     @Override
56  
-    public void stop(StopContext context) {
57  
-        super.stop(context);
  56
+    protected void stopInternal(StopContext context) {
  57
+        super.stopInternal(context);
58 58
         try {
59 59
             dispatchJoinpoint(getDownConfig(), defaultDown());
60 60
         } catch (Throwable t) {
73  pojo/src/main/java/org/jboss/as/pojo/service/SecurityActions.java
... ...
@@ -0,0 +1,73 @@
  1
+/*
  2
+ * JBoss, Home of Professional Open Source.
  3
+ * Copyright 2012, Red Hat, Inc., and individual contributors
  4
+ * as indicated by the @author tags. See the copyright.txt file in the
  5
+ * distribution for a full listing of individual contributors.
  6
+ *
  7
+ * This is free software; you can redistribute it and/or modify it
  8
+ * under the terms of the GNU Lesser General Public License as
  9
+ * published by the Free Software Foundation; either version 2.1 of
  10
+ * the License, or (at your option) any later version.
  11
+ *
  12
+ * This software is distributed in the hope that it will be useful,
  13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15
+ * Lesser General Public License for more details.
  16
+ *
  17
+ * You should have received a copy of the GNU Lesser General Public
  18
+ * License along with this software; if not, write to the Free
  19
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21
+ */
  22
+
  23
+package org.jboss.as.pojo.service;
  24
+
  25
+import java.security.AccessController;
  26
+import java.security.PrivilegedAction;
  27
+
  28
+/**
  29
+ * SecurityActions.
  30
+ *
  31
+ * @author <a href="mailto:ales.justin@jboss.org">Ales Justin</a>
  32
+ */
  33
+final class SecurityActions {
  34
+    private SecurityActions() {
  35
+        // forbidden inheritance
  36
+    }
  37
+
  38
+    /**
  39
+     * Gets context classloader.
  40
+     *
  41
+     * @return the current context classloader
  42
+     */
  43
+    static ClassLoader getContextClassLoader() {
  44
+        if (System.getSecurityManager() == null) {
  45
+            return Thread.currentThread().getContextClassLoader();
  46
+        } else {
  47
+            return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
  48
+                public ClassLoader run() {
  49
+                    return Thread.currentThread().getContextClassLoader();
  50
+                }
  51
+            });
  52
+        }
  53
+    }
  54
+
  55
+    /**
  56
+     * Sets context classloader.
  57
+     *
  58
+     * @param classLoader
  59
+     *            the classloader
  60
+     */
  61
+    static void setContextClassLoader(final ClassLoader classLoader) {
  62
+        if (System.getSecurityManager() == null) {
  63
+            Thread.currentThread().setContextClassLoader(classLoader);
  64
+        } else {
  65
+            AccessController.doPrivileged(new PrivilegedAction<Object>() {
  66
+                public Object run() {
  67
+                    Thread.currentThread().setContextClassLoader(classLoader);
  68
+                    return null;
  69
+                }
  70
+            });
  71
+        }
  72
+    }
  73
+}
36  testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/pojo/support/TcclChecker.java
... ...
@@ -0,0 +1,36 @@
  1
+/*
  2
+ * JBoss, Home of Professional Open Source.
  3
+ * Copyright 2012, Red Hat, Inc., and individual contributors
  4
+ * as indicated by the @author tags. See the copyright.txt file in the
  5
+ * distribution for a full listing of individual contributors.
  6
+ *
  7
+ * This is free software; you can redistribute it and/or modify it
  8
+ * under the terms of the GNU Lesser General Public License as
  9
+ * published by the Free Software Foundation; either version 2.1 of
  10
+ * the License, or (at your option) any later version.
  11
+ *
  12
+ * This software is distributed in the hope that it will be useful,
  13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15
+ * Lesser General Public License for more details.
  16
+ *
  17
+ * You should have received a copy of the GNU Lesser General Public
  18
+ * License along with this software; if not, write to the Free
  19
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21
+ */
  22
+
  23
+package org.jboss.as.test.integration.pojo.support;
  24
+
  25
+import java.net.URL;
  26
+
  27
+/**
  28
+ * @author <a href="mailto:ales.justin@jboss.org">Ales Justin</a>
  29
+ */
  30
+public class TcclChecker {
  31
+    public void start() {
  32
+        URL url = Thread.currentThread().getContextClassLoader().getResource("tccl.txt");
  33
+        if (url == null)
  34
+            throw new IllegalArgumentException("tccl.txt should not be null!");
  35
+    }
  36
+}
54  testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/pojo/test/TcclBeansTestCase.java
... ...
@@ -0,0 +1,54 @@
  1
+/*
  2
+ * JBoss, Home of Professional Open Source.
  3
+ * Copyright 2012, Red Hat, Inc., and individual contributors
  4
+ * as indicated by the @author tags. See the copyright.txt file in the
  5
+ * distribution for a full listing of individual contributors.
  6
+ *
  7
+ * This is free software; you can redistribute it and/or modify it
  8
+ * under the terms of the GNU Lesser General Public License as
  9
+ * published by the Free Software Foundation; either version 2.1 of
  10
+ * the License, or (at your option) any later version.
  11
+ *
  12
+ * This software is distributed in the hope that it will be useful,
  13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15
+ * Lesser General Public License for more details.
  16
+ *
  17
+ * You should have received a copy of the GNU Lesser General Public
  18
+ * License along with this software; if not, write to the Free
  19
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21
+ */
  22
+
  23
+package org.jboss.as.test.integration.pojo.test;
  24
+
  25
+import org.jboss.arquillian.container.test.api.Deployment;
  26
+import org.jboss.arquillian.container.test.api.OperateOnDeployment;
  27
+import org.jboss.arquillian.junit.Arquillian;
  28
+import org.jboss.as.test.integration.pojo.support.TFactory;
  29
+import org.jboss.shrinkwrap.api.ShrinkWrap;
  30
+import org.jboss.shrinkwrap.api.asset.StringAsset;
  31
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
  32
+import org.junit.Test;
  33
+import org.junit.runner.RunWith;
  34
+
  35
+/**
  36
+ * @author <a href="mailto:ales.justin@jboss.org">Ales Justin</a>
  37
+ */
  38
+@RunWith(Arquillian.class)
  39
+public class TcclBeansTestCase {
  40
+    @Deployment(name = "tccl-beans")
  41
+    public static JavaArchive getTcclBeansJar() {
  42
+        JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "tccl-beans.jar");
  43
+        archive.addPackage(TFactory.class.getPackage());
  44
+        archive.addAsManifestResource(TcclBeansTestCase.class.getPackage(), "tccl-jboss-beans.xml", "tccl-jboss-beans.xml");
  45
+        archive.addAsResource(new StringAsset("tccl"), "tccl.txt");
  46
+        return archive;
  47
+    }
  48
+
  49
+    @Test
  50
+    @OperateOnDeployment("tccl-beans")
  51
+    public void testTcclBeans() throws Exception {
  52
+        // TODO -- try to get beans?
  53
+    }
  54
+}
2  testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/pojo/test/TypeBeansTestCase.java
@@ -37,7 +37,7 @@
37 37
 @RunWith(Arquillian.class)
38 38
 public class TypeBeansTestCase {
39 39
     @Deployment(name = "type-beans")
40  
-    public static JavaArchive getCycleBeansJar() {
  40
+    public static JavaArchive getTypeBeansJar() {
41 41
         JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "type-beans.jar");
42 42
         archive.addPackage(TFactory.class.getPackage());
43 43
         archive.addAsManifestResource(TypeBeansTestCase.class.getPackage(), "type-jboss-beans.xml", "type-jboss-beans.xml");
28  testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/pojo/test/tccl-jboss-beans.xml
... ...
@@ -0,0 +1,28 @@
  1
+<!--
  2
+  ~ JBoss, Home of Professional Open Source.
  3
+  ~ Copyright 2012, Red Hat, Inc., and individual contributors
  4
+  ~ as indicated by the @author tags. See the copyright.txt file in the
  5
+  ~ distribution for a full listing of individual contributors.
  6
+  ~
  7
+  ~ This is free software; you can redistribute it and/or modify it
  8
+  ~ under the terms of the GNU Lesser General Public License as
  9
+  ~ published by the Free Software Foundation; either version 2.1 of
  10
+  ~ the License, or (at your option) any later version.
  11
+  ~
  12
+  ~ This software is distributed in the hope that it will be useful,
  13
+  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
  14
+  ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15
+  ~ Lesser General Public License for more details.
  16
+  ~
  17
+  ~ You should have received a copy of the GNU Lesser General Public
  18
+  ~ License along with this software; if not, write to the Free
  19
+  ~ Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20
+  ~ 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21
+  -->
  22
+<deployment xmlns="urn:jboss:pojo:7.0"
  23
+      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24
+      xsi:schemaLocation="urn:jboss:pojo:7.0 jboss-mc_7_0.xsd">
  25
+
  26
+    <bean name="tccl" class="org.jboss.as.test.integration.pojo.support.TcclChecker"/>
  27
+
  28
+</deployment>

0 notes on commit 1e054b8

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