Skip to content

Commit 5c1c8e2

Browse files
committed
[PAXCDI-193] IllegalAccessError for Weld interceptor proxies
1 parent 90c62c0 commit 5c1c8e2

File tree

7 files changed

+183
-1
lines changed

7 files changed

+183
-1
lines changed

itest/src/it/itest-standalone/src/test/java/org/ops4j/pax/cdi/test/SecurityTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.junit.Test;
3333
import org.junit.rules.ExpectedException;
3434
import org.junit.runner.RunWith;
35+
import org.ops4j.pax.cdi.api.Info;
3536
import org.ops4j.pax.cdi.sample8.service.SecuredClient;
3637
import org.ops4j.pax.exam.Configuration;
3738
import org.ops4j.pax.exam.Option;
@@ -61,6 +62,7 @@ public Option[] config() {
6162
// DeltaSpike bundles
6263
mavenBundle("org.apache.deltaspike.core", "deltaspike-core-api").versionAsInProject(),
6364
mavenBundle("org.apache.deltaspike.core", "deltaspike-core-impl").versionAsInProject(),
65+
mavenBundle("org.ops4j.pax.cdi", "pax-cdi-weld-fragment", Info.getPaxCdiVersion()).noStart(),
6466

6567
// Uncomment this section and delete the next after upgrading to DS 1.4.2
6668
// mavenBundle("org.apache.deltaspike.modules", "deltaspike-security-module-api").versionAsInProject(),

pax-cdi-weld-fragment/pom.xml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.ops4j.pax</groupId>
6+
<artifactId>cdi</artifactId>
7+
<version>1.0.0-SNAPSHOT</version>
8+
</parent>
9+
<groupId>org.ops4j.pax.cdi</groupId>
10+
<artifactId>pax-cdi-weld-fragment</artifactId>
11+
<packaging>bundle</packaging>
12+
13+
14+
<properties>
15+
<bundle.symbolicName>org.ops4j.pax.cdi.weld.fragment</bundle.symbolicName>
16+
<bundle.namespace>org.ops4j.pax.cdi.weld.fragment</bundle.namespace>
17+
</properties>
18+
19+
20+
<build>
21+
<plugins>
22+
<plugin>
23+
<groupId>org.apache.felix</groupId>
24+
<artifactId>maven-bundle-plugin</artifactId>
25+
<configuration>
26+
<instructions>
27+
<Bundle-SymbolicName>${bundle.symbolicName}</Bundle-SymbolicName>
28+
<Fragment-Host>org.jboss.weld.osgi-bundle</Fragment-Host>
29+
<Import-Package>
30+
org.jboss.weld.*, *
31+
</Import-Package>
32+
<Export-Package>
33+
org.jboss.weld.*
34+
</Export-Package>
35+
</instructions>
36+
</configuration>
37+
</plugin>
38+
</plugins>
39+
</build>
40+
41+
42+
</project>

pax-cdi-weld/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
<configuration>
8181
<instructions>
8282
<Bundle-SymbolicName>${bundle.symbolicName}</Bundle-SymbolicName>
83+
<Bundle-Activator>${bundle.namespace}.impl.Activator</Bundle-Activator>
8384
<Import-Package>
8485
org.jboss.weld.*; version="[1,3)",
8586
org.slf4j;version="[1.6,2)";resolution:=optional,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2012 Harald Wellmann.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13+
* implied.
14+
*
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.ops4j.pax.cdi.weld.impl;
19+
20+
import org.osgi.framework.BundleActivator;
21+
import org.osgi.framework.BundleContext;
22+
import org.osgi.framework.hooks.weaving.WeavingHook;
23+
24+
/**
25+
* Activator for Weld CDI Adapter.
26+
*
27+
* @author Harald Wellmann
28+
*
29+
*/
30+
public class Activator implements BundleActivator {
31+
32+
/**
33+
* Starts this bundle and registers a weaving hook for Weld proxies.
34+
*/
35+
@Override
36+
public void start(BundleContext context) throws Exception {
37+
ProxyWeavingHook weavingHook = new ProxyWeavingHook();
38+
context.registerService(WeavingHook.class, weavingHook, null);
39+
}
40+
41+
@Override
42+
public void stop(BundleContext context) throws Exception {
43+
// not used
44+
}
45+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2012 Harald Wellmann.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13+
* implied.
14+
*
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.ops4j.pax.cdi.weld.impl;
19+
20+
import java.util.List;
21+
import java.util.Map;
22+
import java.util.WeakHashMap;
23+
24+
import org.ops4j.pax.cdi.api.Constants;
25+
import org.osgi.framework.Bundle;
26+
import org.osgi.framework.hooks.weaving.WeavingHook;
27+
import org.osgi.framework.hooks.weaving.WovenClass;
28+
import org.osgi.framework.wiring.BundleWire;
29+
import org.osgi.framework.wiring.BundleWiring;
30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
32+
33+
/**
34+
* A weaving hook which adds a dynamic package import for managed bean proxies for all
35+
* classes from bean bundles.
36+
*
37+
* @author Harald Wellmann
38+
*/
39+
public class ProxyWeavingHook implements WeavingHook {
40+
41+
private static Logger log = LoggerFactory.getLogger(ProxyWeavingHook.class);
42+
43+
private Map<Bundle, Boolean> bundleMap = new WeakHashMap<Bundle, Boolean>();
44+
45+
46+
@Override
47+
public void weave(WovenClass wovenClass) {
48+
Bundle bundle = wovenClass.getBundleWiring().getBundle();
49+
Boolean seen = bundleMap.get(bundle);
50+
if (seen != null) {
51+
return;
52+
}
53+
boolean requiresWeaving = false;
54+
if (isBeanBundle(bundle) || isExtension(bundle)) {
55+
log.debug("weaving {}", wovenClass.getClassName());
56+
wovenClass.getDynamicImports().add("org.jboss.weld.*");
57+
requiresWeaving = true;
58+
}
59+
bundleMap.put(bundle, requiresWeaving);
60+
}
61+
62+
/**
63+
* TODO Copied from BeanBundles.isBeanBundle(). Using that method from pax-cdi-spi
64+
* causes a ClassCircularityError. Is there a better way to avoid this?
65+
* @param candidate
66+
* @return
67+
*/
68+
private static boolean isBeanBundle(Bundle candidate) {
69+
List<BundleWire> wires = candidate.adapt(BundleWiring.class).getRequiredWires(
70+
Constants.EXTENDER_CAPABILITY);
71+
for (BundleWire wire : wires) {
72+
Object object = wire.getCapability().getAttributes().get(Constants.EXTENDER_CAPABILITY);
73+
if (object instanceof String) {
74+
String extender = (String) object;
75+
if (extender.equals(Constants.CDI_EXTENDER)) {
76+
return true;
77+
}
78+
}
79+
}
80+
return false;
81+
}
82+
83+
private static boolean isExtension(Bundle candidate) {
84+
if (candidate.getSymbolicName().equals(Constants.CDI_EXTENSION_CAPABILITY)) {
85+
return true;
86+
}
87+
List<BundleWire> wires = candidate.adapt(BundleWiring.class).getRequiredWires(
88+
Constants.CDI_EXTENSION_CAPABILITY);
89+
return !wires.isEmpty();
90+
}
91+
}

pax-cdi-weld/src/main/java/org/ops4j/pax/cdi/weld/impl/util/OsgiProxyService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class OsgiProxyService implements ProxyServices {
3737

3838
@Override
3939
public ClassLoader getClassLoader(Class<?> proxiedBeanType) {
40-
return loader;
40+
return proxiedBeanType.getClassLoader();
4141
}
4242

4343
@Override

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<module>pax-cdi-undertow-openwebbeans</module>
4343
<module>pax-cdi-undertow-weld</module>
4444
<module>pax-cdi-manual</module>
45+
<module>pax-cdi-weld-fragment</module>
4546
</modules>
4647

4748
<profiles>

0 commit comments

Comments
 (0)