Skip to content

Commit f731019

Browse files
committed
WELD-2832 Trimming non-exlicit archive should be treated as a deployment problem
1 parent 2a5edff commit f731019

File tree

7 files changed

+83
-7
lines changed

7 files changed

+83
-7
lines changed

impl/src/main/java/org/jboss/weld/bootstrap/BeanDeployment.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
import org.jboss.weld.bootstrap.enablement.GlobalEnablementBuilder;
5353
import org.jboss.weld.bootstrap.enablement.ModuleEnablement;
5454
import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
55+
import org.jboss.weld.bootstrap.spi.BeanDiscoveryMode;
56+
import org.jboss.weld.bootstrap.spi.BeansXml;
5557
import org.jboss.weld.bootstrap.spi.Filter;
5658
import org.jboss.weld.bootstrap.spi.Metadata;
5759
import org.jboss.weld.config.WeldConfiguration;
@@ -263,7 +265,11 @@ public void createBeans(Environment environment) {
263265
}
264266
beanDeployer.addBuiltInBean(new RequestContextControllerBean(beanManager));
265267

266-
if (beanDeploymentArchive.getBeansXml() != null && beanDeploymentArchive.getBeansXml().isTrimmed()) {
268+
BeansXml beansXml = beanDeploymentArchive.getBeansXml();
269+
if (beansXml != null && beansXml.isTrimmed()) {
270+
if (beansXml.getBeanDiscoveryMode() != BeanDiscoveryMode.ALL) {
271+
throw BootstrapLogger.LOG.trimmingNonExplicitBeanArchive(beansXml.getUrl(), beansXml.getBeanDiscoveryMode());
272+
}
267273
beanDeployer.getEnvironment().trim();
268274
}
269275
beanDeployer.createClassBeans();

impl/src/main/java/org/jboss/weld/logging/BootstrapLogger.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,7 @@ public interface BootstrapLogger extends WeldLogger {
349349
@Message(id = 184, value = "BeforeBeanDiscovery.addInvokable() called by {0} for {1}", format = Format.MESSAGE_FORMAT)
350350
void addInvokableCalled(Object extensionName, Object type);
351351

352+
@Message(id = 185, value = "Archive trimming is permitted only for explicit bean archives. Found trimmed bean archive with discovery mode {1} and with URL {0}", format = Format.MESSAGE_FORMAT)
353+
DeploymentException trimmingNonExplicitBeanArchive(Object beansXmlUrl, Object beansXmlDiscoveryMode);
354+
352355
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.jboss.weld.tests.xml.broken.trimming;
2+
3+
import jakarta.enterprise.context.Dependent;
4+
5+
@Dependent
6+
public class DummyBean {
7+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.jboss.weld.tests.xml.broken.trimming;
2+
3+
import jakarta.enterprise.inject.spi.DeploymentException;
4+
5+
import org.jboss.arquillian.container.test.api.Deployment;
6+
import org.jboss.arquillian.container.test.api.ShouldThrowException;
7+
import org.jboss.arquillian.junit.Arquillian;
8+
import org.jboss.shrinkwrap.api.Archive;
9+
import org.jboss.shrinkwrap.api.BeanArchive;
10+
import org.jboss.shrinkwrap.api.BeanDiscoveryMode;
11+
import org.jboss.shrinkwrap.api.ShrinkWrap;
12+
import org.jboss.weld.test.util.Utils;
13+
import org.junit.Test;
14+
import org.junit.runner.RunWith;
15+
16+
@RunWith(Arquillian.class)
17+
public class ImplicitArchiveTrimmingTest {
18+
19+
@Deployment
20+
@ShouldThrowException(DeploymentException.class)
21+
public static Archive<?> deploy() {
22+
return ShrinkWrap.create(BeanArchive.class, Utils.getDeploymentNameAsHash(ImplicitArchiveTrimmingTest.class))
23+
.trim()
24+
.beanDiscoveryMode(BeanDiscoveryMode.ANNOTATED)
25+
.addPackage(ImplicitArchiveTrimmingTest.class.getPackage());
26+
}
27+
28+
@Test
29+
public void testExceptionThrown() {
30+
}
31+
}

tests-common/src/main/java/org/jboss/shrinkwrap/api/BeanArchive.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,9 @@ public interface BeanArchive extends JavaArchive {
5454
* Sets bean-discovery-mode to annotated
5555
*/
5656
BeanArchive annotated();
57+
58+
/**
59+
* Adds the trim element to beans.xml
60+
*/
61+
BeanArchive trim();
5762
}

tests-common/src/main/java/org/jboss/shrinkwrap/impl/BeanArchiveImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,10 @@ public BeanArchive beanDiscoveryMode(BeanDiscoveryMode mode) {
9898
public BeanArchive annotated() {
9999
return beanDiscoveryMode(BeanDiscoveryMode.ANNOTATED);
100100
}
101+
102+
@Override
103+
public BeanArchive trim() {
104+
descriptor.trim();
105+
return covarientReturn();
106+
}
101107
}

tests-common/src/main/java/org/jboss/shrinkwrap/impl/BeansXml.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ public BeanDiscoveryMode getBeanDiscoveryMode() {
2222
};
2323

2424
private static final String CLOSING_TAG_PREFIX = "</";
25+
private static final String CLOSING_TAG_EMPTY = "/>";
2526
private static final String OPENING_TAG_PREFIX = "<";
2627
private static final String TAG_SUFFIX = ">";
2728
private static final String TAG_SUFFIX_NEW_LINE = ">\n";
2829
private static final String TAG_SUFFIX_SELF_CLOSE_NEW_LINE = " />\n";
2930
private static final String ALTERNATIVES_ELEMENT_NAME = "alternatives";
31+
private static final String DECORATORS_ELEMENT_NAME = "decorators";
32+
private static final String INTERCEPTORS_ELEMENT_NAME = "interceptors";
3033
private static final String CLASS = "class";
3134

3235
private static final String SCAN_ELEMENT_NAME = "scan";
@@ -37,11 +40,14 @@ public BeanDiscoveryMode getBeanDiscoveryMode() {
3740
private static final String NAME_ATTRIBUTE_NAME = "name";
3841
private static final String VALUE_ATTRIBUTE_NAME = "value";
3942

43+
private static final String TRIM_ELEMENT_NAME = "trim";
44+
4045
private final List<Class<?>> alternatives;
4146
private final List<Class<?>> interceptors;
4247
private final List<Class<?>> decorators;
4348
private final List<Class<?>> stereotypes;
4449
private final List<Exclude> excludeFilters;
50+
private boolean trimArchive;
4551

4652
private BeanDiscoveryMode mode = BeanDiscoveryMode.ANNOTATED;
4753

@@ -215,6 +221,11 @@ public BeansXml excludeFilters(Exclude... filters) {
215221
return this;
216222
}
217223

224+
public BeansXml trim() {
225+
this.trimArchive = true;
226+
return this;
227+
}
228+
218229
public BeanDiscoveryMode getBeanDiscoveryMode() {
219230
return mode;
220231
}
@@ -226,20 +237,21 @@ public void setBeanDiscoveryMode(BeanDiscoveryMode mode) {
226237
@Override
227238
public InputStream openStream() {
228239
StringBuilder xml = new StringBuilder();
229-
xml.append("<beans version=\"1.1\" bean-discovery-mode=\"");
240+
xml.append("<beans version=\"4.1\" bean-discovery-mode=\"");
230241
xml.append(getBeanDiscoveryMode().getValue());
231242
xml.append("\">\n");
232243
appendExcludeFilters(excludeFilters, xml);
233244
appendAlternatives(alternatives, stereotypes, xml);
234-
appendSection("interceptors", CLASS, interceptors, xml);
235-
appendSection("decorators", CLASS, decorators, xml);
245+
appendSection(INTERCEPTORS_ELEMENT_NAME, CLASS, interceptors, xml);
246+
appendSection(DECORATORS_ELEMENT_NAME, CLASS, decorators, xml);
247+
appendTrimming(trimArchive, xml);
236248
xml.append("</beans>");
237249

238250
return new ByteArrayInputStream(xml.toString().getBytes());
239251
}
240252

241253
private void appendExcludeFilters(List<Exclude> filters, StringBuilder xml) {
242-
if (filters.size() > 0) {
254+
if (!filters.isEmpty()) {
243255
xml.append(OPENING_TAG_PREFIX).append(SCAN_ELEMENT_NAME).append(TAG_SUFFIX_NEW_LINE);
244256
for (Exclude ex : filters) {
245257
xml.append(OPENING_TAG_PREFIX).append(EXCLUDE_ELEMENT_NAME);
@@ -264,7 +276,7 @@ private static void appendAttribute(String name, String value, StringBuilder xml
264276
}
265277

266278
private static void appendAlternatives(List<Class<?>> alternatives, List<Class<?>> stereotypes, StringBuilder xml) {
267-
if (alternatives.size() > 0 || stereotypes.size() > 0) {
279+
if (!alternatives.isEmpty() || !stereotypes.isEmpty()) {
268280
xml.append(OPENING_TAG_PREFIX).append(ALTERNATIVES_ELEMENT_NAME).append(TAG_SUFFIX_NEW_LINE);
269281
appendClasses(CLASS, alternatives, xml);
270282
appendClasses("stereotype", stereotypes, xml);
@@ -273,13 +285,19 @@ private static void appendAlternatives(List<Class<?>> alternatives, List<Class<?
273285
}
274286

275287
private static void appendSection(String name, String subName, List<Class<?>> classes, StringBuilder xml) {
276-
if (classes.size() > 0) {
288+
if (!classes.isEmpty()) {
277289
xml.append(OPENING_TAG_PREFIX).append(name).append(TAG_SUFFIX_NEW_LINE);
278290
appendClasses(subName, classes, xml);
279291
xml.append(CLOSING_TAG_PREFIX).append(name).append(TAG_SUFFIX_NEW_LINE);
280292
}
281293
}
282294

295+
private static void appendTrimming(boolean trimArchive, StringBuilder xml) {
296+
if (trimArchive) {
297+
xml.append(OPENING_TAG_PREFIX).append(TRIM_ELEMENT_NAME).append(CLOSING_TAG_EMPTY);
298+
}
299+
}
300+
283301
private static void appendClasses(String name, List<Class<?>> classes, StringBuilder xml) {
284302
for (Class<?> clazz : classes) {
285303
xml.append(OPENING_TAG_PREFIX).append(name).append(TAG_SUFFIX).append(clazz.getName()).append(CLOSING_TAG_PREFIX)

0 commit comments

Comments
 (0)