Skip to content

TOMEE-4456 add <trim/> support for embedded use case #1785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: tomee-8.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class BeansInfo extends InfoObject {

public String version = "1.1";
public String discoveryMode;
public boolean trim;
public final List<ExclusionEntryInfo> excludes = new LinkedList<>();
public final List<BDAInfo> bdas = new LinkedList<>();
public final List<BDAInfo> noDescriptorBdas = new LinkedList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public class CompositeBeans extends Beans {
@XmlTransient
private final Map<URL, String> discoveryByUrl = new HashMap<>();

@XmlTransient
private final Map<URL, Boolean> trimByUrl = new HashMap<>();

@XmlTransient
private final Map<URL, Collection<String>> interceptorsByUrl = new HashMap<>();

Expand All @@ -43,10 +46,6 @@ public class CompositeBeans extends Beans {
@XmlTransient
private final Map<URL, Collection<String>> alternativeStereotypesByUrl = new HashMap<>();

public Map<URL, String> getDiscoveryByUrl() {
return discoveryByUrl;
}

public void mergeClasses(final URL url, final Beans beans) {
// just for jaxb tree
getAlternativeClasses().addAll(beans.getAlternativeClasses());
Expand All @@ -59,6 +58,15 @@ public void mergeClasses(final URL url, final Beans beans) {
decoratorsByUrl.put(url, beans.getDecorators());
alternativesByUrl.put(url, beans.getAlternativeClasses());
alternativeStereotypesByUrl.put(url, beans.getAlternativeStereotypes());
trimByUrl.put(url, beans.isTrim());
}

public Map<URL, String> getDiscoveryByUrl() {
return discoveryByUrl;
}

public Map<URL, Boolean> getTrimByUrl() {
return trimByUrl;
}

public Map<URL, Collection<String>> getInterceptorsByUrl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ public EjbJarInfo buildInfo(final EjbModule jar) throws OpenEJBException {
ejbJar.beans = new BeansInfo();
ejbJar.beans.version = beans.getVersion();
ejbJar.beans.discoveryMode = beans.getBeanDiscoveryMode();
ejbJar.beans.trim = beans.isTrim();
if (beans.getScan() != null) {
for (final Beans.Scan.Exclude exclude : beans.getScan().getExclude()) {
final ExclusionInfo exclusionInfo = new ExclusionInfo();
Expand Down Expand Up @@ -271,11 +272,13 @@ public EjbJarInfo buildInfo(final EjbModule jar) throws OpenEJBException {
ejbJar.beans.startupClasses.addAll(beans.getStartupBeans());

final Map<URL, String> discoveryModeByUrl = new HashMap<>();
final Map<URL, Boolean> trimByUrl = new HashMap<>();
final CompositeBeans composite;
final boolean isComposite = CompositeBeans.class.isInstance(beans);
if (isComposite) {
composite = CompositeBeans.class.cast(beans);
discoveryModeByUrl.putAll(composite.getDiscoveryByUrl());
trimByUrl.putAll(composite.getTrimByUrl());
} else {
composite = null;
URL key = DEFAULT_BEANS_XML_KEY;
Expand All @@ -287,12 +290,14 @@ public EjbJarInfo buildInfo(final EjbModule jar) throws OpenEJBException {
}
}
discoveryModeByUrl.put(key, beans.getBeanDiscoveryMode());
trimByUrl.put(key, beans.isTrim());
}
for (final Map.Entry<URL, List<String>> next : beans.getManagedClasses().entrySet()) {
final URL key = next.getKey();

final BeansInfo.BDAInfo bdaInfo = new BeansInfo.BDAInfo();
bdaInfo.discoveryMode = discoveryModeByUrl.get(key);
bdaInfo.trim = Boolean.TRUE.equals(trimByUrl.get(key));
merge(composite, key == null ? DEFAULT_BEANS_XML_KEY : key, bdaInfo, next.getValue());
ejbJar.beans.bdas.add(bdaInfo);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tomee.itests.trimmedbeans;

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class BeanA {

public int getMeaningOfLife() {
return 42;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tomee.itests.trimmedbeans;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

/**
* A bean without a bean defining annotation -> should not be found
*/
@ApplicationScoped
public class BeanB {
private @Inject BeanA beanA;

public int getMeaningOfLife() {
return beanA.getMeaningOfLife();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tomee.itests.trimmedbeans;

/**
* A bean without a bean defining annotation -> should not be found
*/
public class NotPickedUpBean {
public String getName() {
return "dummy";
}
}
24 changes: 24 additions & 0 deletions itests/trimmedBeans/src/main/resources/META-INF/beans.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
version="2.0" bean-discovery-mode="all">
<trim/>
</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tomee.itests.trimmedbeans;

import java.util.Properties;

import javax.enterprise.inject.spi.BeanManager;
import javax.naming.Context;
import javax.naming.InitialContext;

import org.apache.openejb.OpenEjbContainer;
import org.apache.openejb.core.LocalInitialContext;
import org.apache.openejb.core.LocalInitialContextFactory;
import org.apache.webbeans.config.WebBeansContext;
import org.junit.Test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;


/**
* This test checks that CDI does not pick up non annotated beans in
* a trimmed bean archive.
*/
public class TrimmedBeansTest {

@Test
public void startupBeanWithAmbiguousResolutionException() throws Exception {
Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, LocalInitialContextFactory.class.getName());
props.setProperty(LocalInitialContext.ON_CLOSE, LocalInitialContext.Close.DESTROY.name());
this.getClass().getClassLoader().loadClass("org.apache.openejb.server.ServiceManager");
props.setProperty(OpenEjbContainer.OPENEJB_EMBEDDED_REMOTABLE, "true");

Context context = new InitialContext(props);
BeanManager bm = WebBeansContext.currentInstance().getBeanManagerImpl();

assertFalse(bm.getBeans(BeanA.class).isEmpty());
assertFalse(bm.getBeans(BeanB.class).isEmpty());
assertTrue(bm.getBeans(NotPickedUpBean.class).isEmpty());
}
}
Loading