Skip to content

Commit d8239d5

Browse files
committed
PAXCDI-186 Implicit bean archive support
Using XBean AnnotationFinder to scan for bean-defining annotation. New interfaces to abstract from provider specific XML parser and XML models.
1 parent 46aa96b commit d8239d5

File tree

23 files changed

+1191
-249
lines changed

23 files changed

+1191
-249
lines changed

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*/
1818
package org.ops4j.pax.cdi.test;
1919

20-
import static org.hamcrest.CoreMatchers.hasItem;
2120
import static org.hamcrest.CoreMatchers.is;
2221
import static org.hamcrest.CoreMatchers.notNullValue;
2322
import static org.junit.Assert.assertThat;
@@ -26,13 +25,18 @@
2625
import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
2726
import static org.ops4j.pax.exam.CoreOptions.options;
2827

28+
import java.net.URL;
2929
import java.util.Set;
3030

3131
import javax.inject.Inject;
3232

3333
import org.junit.Test;
3434
import org.junit.runner.RunWith;
35+
import org.ops4j.pax.cdi.spi.scan.BeanDescriptor;
36+
import org.ops4j.pax.cdi.spi.scan.BeanDescriptorParser;
37+
import org.ops4j.pax.cdi.spi.scan.BeanDiscoveryMode;
3538
import org.ops4j.pax.cdi.spi.scan.BeanScanner;
39+
import org.ops4j.pax.cdi.spi.scan.DefaultBeanDescriptor;
3640
import org.ops4j.pax.exam.Configuration;
3741
import org.ops4j.pax.exam.Option;
3842
import org.ops4j.pax.exam.junit.PaxExam;
@@ -61,22 +65,29 @@ public Option[] config() {
6165
workspaceBundle("org.ops4j.pax.cdi", "pax-cdi-spi"),
6266

6367
mavenBundle("org.apache.xbean", "xbean-bundleutils").versionAsInProject(),
68+
mavenBundle("org.apache.xbean", "xbean-finder-shaded").versionAsInProject(),
69+
mavenBundle("org.apache.xbean", "xbean-asm5-shaded").versionAsInProject(),
6470
mavenBundle("javax.interceptor", "javax.interceptor-api", "1.2"),
6571
mavenBundle("javax.el", "javax.el-api", "3.0.0"),
6672
mavenBundle("javax.enterprise", "cdi-api").versionAsInProject());
6773
}
6874

6975
@Test
70-
public void checkBeanCandidateFromEmbeddedJar() {
76+
public void archiveWithoutExtenderShouldBeEmpty() {
7177
Bundle bundle = BundleUtils.getBundle(bc, "org.ops4j.pax.tinybundles");
7278
assertThat(bundle, is(notNullValue()));
7379

74-
BeanScanner scanner = new BeanScanner(bundle);
80+
BeanDescriptorParser parser = new BeanDescriptorParser() {
81+
82+
@Override
83+
public BeanDescriptor parse(URL beansXml) {
84+
return new DefaultBeanDescriptor(null, BeanDiscoveryMode.ALL, "1.0");
85+
}
86+
};
87+
88+
BeanScanner scanner = new BeanScanner(bundle, parser);
7589
scanner.scan();
7690
Set<String> beanClasses = scanner.getBeanClasses();
77-
78-
// check we can see a class from an embedded JAR
79-
assertThat(beanClasses, hasItem("org.ops4j.store.StoreFactory"));
91+
assertThat(beanClasses.size(), is(0));
8092
}
81-
8293
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2015 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.test;
19+
20+
import static org.hamcrest.CoreMatchers.is;
21+
import static org.hamcrest.CoreMatchers.notNullValue;
22+
import static org.junit.Assert.assertThat;
23+
import static org.ops4j.pax.cdi.test.support.TestConfiguration.regressionDefaults;
24+
import static org.ops4j.pax.cdi.test.support.TestConfiguration.workspaceBundle;
25+
import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
26+
import static org.ops4j.pax.exam.CoreOptions.options;
27+
28+
import java.io.IOException;
29+
import java.util.Iterator;
30+
import java.util.List;
31+
32+
import javax.inject.Inject;
33+
34+
import org.apache.xbean.finder.AnnotationFinder;
35+
import org.apache.xbean.finder.archive.Archive.Entry;
36+
import org.junit.Test;
37+
import org.junit.runner.RunWith;
38+
import org.ops4j.pax.cdi.api.OsgiServiceProvider;
39+
import org.ops4j.pax.cdi.spi.scan.BundleArchive;
40+
import org.ops4j.pax.exam.Configuration;
41+
import org.ops4j.pax.exam.Option;
42+
import org.ops4j.pax.exam.junit.PaxExam;
43+
import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
44+
import org.ops4j.pax.exam.spi.reactors.PerClass;
45+
import org.ops4j.pax.swissbox.core.BundleUtils;
46+
import org.osgi.framework.Bundle;
47+
import org.osgi.framework.BundleContext;
48+
49+
@RunWith(PaxExam.class)
50+
@ExamReactorStrategy(PerClass.class)
51+
public class BundleArchiveTest {
52+
53+
@Inject
54+
private BundleContext bc;
55+
56+
@Configuration
57+
public Option[] config() {
58+
return options(
59+
regressionDefaults(),
60+
61+
// This is a bundle with embedded JARs on the bundle classpath
62+
mavenBundle("org.ops4j.pax.tinybundles", "tinybundles", "1.0.0"),
63+
64+
workspaceBundle("org.ops4j.pax.cdi", "pax-cdi-api"),
65+
workspaceBundle("org.ops4j.pax.cdi", "pax-cdi-spi"),
66+
workspaceBundle("org.ops4j.pax.cdi", "pax-cdi-extender"),
67+
workspaceBundle("org.ops4j.pax.cdi", "pax-cdi-extension"),
68+
workspaceBundle("org.ops4j.pax.cdi.samples", "pax-cdi-sample6"),
69+
workspaceBundle("org.ops4j.pax.cdi.samples", "pax-cdi-sample1"),
70+
workspaceBundle("org.ops4j.pax.cdi.samples", "pax-cdi-sample1-client"),
71+
72+
mavenBundle("org.apache.xbean", "xbean-bundleutils").versionAsInProject(),
73+
mavenBundle("org.apache.xbean", "xbean-finder-shaded").versionAsInProject(),
74+
mavenBundle("org.apache.xbean", "xbean-asm5-shaded").versionAsInProject(),
75+
mavenBundle("javax.annotation", "javax.annotation-api", "1.2"),
76+
mavenBundle("javax.interceptor", "javax.interceptor-api", "1.2"),
77+
mavenBundle("javax.el", "javax.el-api", "3.0.0"),
78+
mavenBundle("javax.enterprise", "cdi-api").versionAsInProject());
79+
}
80+
81+
@Test
82+
public void shouldFindClassFromEmbeddedJar() throws ClassNotFoundException, IOException {
83+
Bundle bundle = BundleUtils.getBundle(bc, "org.ops4j.pax.tinybundles");
84+
assertThat(bundle, is(notNullValue()));
85+
86+
BundleArchive archive = new BundleArchive(bundle);
87+
Iterator<Entry> it = archive.iterator();
88+
while (it.hasNext()) {
89+
String className = it.next().getName();
90+
System.out.println(className);
91+
assertThat(archive.getBytecode(className), is(notNullValue()));
92+
}
93+
}
94+
95+
@Test
96+
public void shouldFindClassFromImportedPackage() throws ClassNotFoundException, IOException {
97+
Bundle bundle = BundleUtils.getBundle(bc, "org.ops4j.pax.cdi.spi");
98+
assertThat(bundle, is(notNullValue()));
99+
100+
BundleArchive archive = new BundleArchive(bundle);
101+
Iterator<Entry> it = archive.iterator();
102+
while (it.hasNext()) {
103+
System.out.println(it.next().getName());
104+
}
105+
}
106+
107+
@Test
108+
public void shouldFindAnnotations() {
109+
Bundle bundle = BundleUtils.getBundle(bc, "org.ops4j.pax.cdi.sample1.client");
110+
assertThat(bundle, is(notNullValue()));
111+
112+
BundleArchive archive = new BundleArchive(bundle);
113+
AnnotationFinder finder = new AnnotationFinder(archive);
114+
List<Class<?>> classes = finder.findAnnotatedClasses(OsgiServiceProvider.class);
115+
System.out.println(classes);
116+
assertThat(classes.size(), is(3));
117+
}
118+
}

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import static org.ops4j.pax.cdi.test.support.TestConfiguration.workspaceBundle;
2424
import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
2525
import static org.ops4j.pax.exam.CoreOptions.options;
26-
import static org.ops4j.pax.exam.CoreOptions.wrappedBundle;
2726

2827
import javax.inject.Inject;
2928

@@ -62,22 +61,21 @@ public Option[] config() {
6261
mavenBundle("org.apache.deltaspike.core", "deltaspike-core-api").versionAsInProject(),
6362
mavenBundle("org.apache.deltaspike.core", "deltaspike-core-impl").versionAsInProject(),
6463

65-
// Uncomment this section and delete the next after upgrading to DS 1.4.2
66-
// mavenBundle("org.apache.deltaspike.modules", "deltaspike-security-module-api").versionAsInProject(),
67-
// mavenBundle("org.apache.deltaspike.modules", "deltaspike-security-module-impl").versionAsInProject(),
64+
mavenBundle("org.apache.deltaspike.modules", "deltaspike-security-module-api").versionAsInProject(),
65+
mavenBundle("org.apache.deltaspike.modules", "deltaspike-security-module-impl").versionAsInProject(),
6866

69-
wrappedBundle(mavenBundle("org.apache.deltaspike.modules", "deltaspike-security-module-api")
70-
.versionAsInProject())
71-
.instructions(
72-
"overwrite=merge",
73-
"Bundle-SymbolicName=org.apache.deltaspike.modules.deltaspike-security-module-api"),
74-
wrappedBundle(mavenBundle("org.apache.deltaspike.modules", "deltaspike-security-module-impl").versionAsInProject())
75-
.instructions(
76-
"overwrite=merge",
77-
"Bundle-SymbolicName=org.apache.deltaspike.modules.deltaspike-security-module-impl",
78-
"Provide-Capability=org.ops4j.pax.cdi.extension;extension=\"deltaspike-security-module-impl\"",
79-
"Require-Capability=org.ops4j.pax.cdi.extension; filter:=\"(extension=pax-cdi-extension)\", "
80-
+ "osgi.extender; filter:=\"(osgi.extender=pax.cdi)\""),
67+
// wrappedBundle(mavenBundle("org.apache.deltaspike.modules", "deltaspike-security-module-api")
68+
// .versionAsInProject())
69+
// .instructions(
70+
// "overwrite=merge",
71+
// "Bundle-SymbolicName=org.apache.deltaspike.modules.deltaspike-security-module-api"),
72+
// wrappedBundle(mavenBundle("org.apache.deltaspike.modules", "deltaspike-security-module-impl").versionAsInProject())
73+
// .instructions(
74+
// "overwrite=merge",
75+
// "Bundle-SymbolicName=org.apache.deltaspike.modules.deltaspike-security-module-impl",
76+
// "Provide-Capability=org.ops4j.pax.cdi.extension;extension=\"deltaspike-security-module-impl\"",
77+
// "Require-Capability=org.ops4j.pax.cdi.extension; filter:=\"(extension=pax-cdi-extension)\", "
78+
// + "osgi.extender; filter:=\"(osgi.extender=pax.cdi)\""),
8179

8280
// Sample bundles
8381
workspaceBundle("org.ops4j.pax.cdi.samples", "pax-cdi-sample8"));

pax-cdi-openwebbeans/src/main/java/org/ops4j/pax/cdi/openwebbeans/impl/BundleScannerService.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
package org.ops4j.pax.cdi.openwebbeans.impl;
1919

2020
import java.net.URL;
21-
import java.util.HashMap;
2221
import java.util.HashSet;
23-
import java.util.Map;
2422
import java.util.Set;
2523

24+
import org.apache.webbeans.config.WebBeansContext;
25+
import org.apache.webbeans.spi.BeanArchiveService;
2626
import org.apache.webbeans.spi.ScannerService;
2727
import org.apache.xbean.osgi.bundle.util.BundleUtils;
2828
import org.ops4j.pax.cdi.spi.scan.BeanScanner;
@@ -43,14 +43,6 @@ public class BundleScannerService implements ScannerService {
4343
private BeanScanner scanner;
4444
private Bundle bundle;
4545
private Set<Class<?>> beanClasses;
46-
private Map<String, Set<String>> classAnnotations;
47-
48-
/**
49-
* Creates a new scanner service.
50-
*/
51-
public BundleScannerService() {
52-
classAnnotations = new HashMap<String, Set<String>>();
53-
}
5446

5547
@Override
5648
public void init(Object object) {
@@ -59,8 +51,10 @@ public void init(Object object) {
5951

6052
@Override
6153
public void scan() {
54+
BeanArchiveService archiveService = WebBeansContext.getInstance().getBeanArchiveService();
55+
OpenWebBeansParser parser = new OpenWebBeansParser(archiveService);
6256
bundle = BundleUtils.getContextBundle(true);
63-
scanner = new BeanScanner(bundle);
57+
scanner = new BeanScanner(bundle, parser);
6458
scanner.scan();
6559
}
6660

@@ -69,7 +63,6 @@ public void release() {
6963
scanner = null;
7064
bundle = null;
7165
beanClasses = null;
72-
classAnnotations.clear();
7366
}
7467

7568
@Override
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright 2015 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.openwebbeans.impl;
19+
20+
import java.net.URL;
21+
import java.util.List;
22+
23+
import org.apache.webbeans.spi.BeanArchiveService;
24+
import org.apache.webbeans.spi.BeanArchiveService.BeanArchiveInformation;
25+
import org.ops4j.pax.cdi.spi.scan.BeanDescriptor;
26+
import org.ops4j.pax.cdi.spi.scan.BeanDescriptorParser;
27+
import org.ops4j.pax.cdi.spi.scan.BeanDiscoveryMode;
28+
29+
30+
public class OpenWebBeansParser implements BeanDescriptorParser {
31+
32+
33+
private BeanArchiveService archiveService;
34+
private BeanArchiveInformation archiveInfo;
35+
36+
private class BeanDescriptorImpl implements BeanDescriptor {
37+
38+
39+
private URL url;
40+
41+
public BeanDescriptorImpl(URL url) {
42+
this.url = url;
43+
}
44+
45+
@Override
46+
public String getVersion() {
47+
return archiveInfo.getVersion();
48+
}
49+
50+
@Override
51+
public BeanDiscoveryMode getBeanDiscoveryMode() {
52+
return BeanDiscoveryMode.valueOf(archiveInfo.getBeanDiscoveryMode().toString());
53+
}
54+
55+
@Override
56+
public boolean isClassExcluded(String clazz) {
57+
return archiveInfo.isClassExcluded(clazz);
58+
}
59+
60+
@Override
61+
public boolean isPackageExcluded(String packageName) {
62+
return archiveInfo.isPackageExcluded(packageName);
63+
}
64+
65+
@Override
66+
public List<String> getInterceptors() {
67+
return archiveInfo.getInterceptors();
68+
}
69+
70+
@Override
71+
public List<String> getDecorators() {
72+
return archiveInfo.getDecorators();
73+
}
74+
75+
@Override
76+
public List<String> getAlternativeClasses() {
77+
return archiveInfo.getAlternativeClasses();
78+
}
79+
80+
@Override
81+
public List<String> getAlternativeStereotypes() {
82+
return archiveInfo.getAlternativeStereotypes();
83+
}
84+
85+
@Override
86+
public List<String> getExcludedClasses() {
87+
return archiveInfo.getExcludedClasses();
88+
}
89+
90+
@Override
91+
public List<String> getExcludedPackages() {
92+
return archiveInfo.getExcludedPackages();
93+
}
94+
95+
@Override
96+
public URL getUrl() {
97+
return url;
98+
}
99+
}
100+
101+
public OpenWebBeansParser(BeanArchiveService archiveService) {
102+
this.archiveService = archiveService;
103+
}
104+
105+
@Override
106+
public BeanDescriptor parse(URL beansXml) {
107+
archiveInfo = archiveService.getBeanArchiveInformation(beansXml);
108+
return new BeanDescriptorImpl(beansXml);
109+
}
110+
}

0 commit comments

Comments
 (0)