Skip to content

Commit

Permalink
Expose bean-discovery-mode and version attributes through the BeansXm…
Browse files Browse the repository at this point in the history
…l SPI
  • Loading branch information
jharting committed Mar 8, 2013
1 parent a94d154 commit 32580b3
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 8 deletions.
21 changes: 20 additions & 1 deletion impl/src/main/java/org/jboss/weld/metadata/BeansXmlImpl.java
Expand Up @@ -3,6 +3,7 @@
import java.net.URL;
import java.util.List;

import org.jboss.weld.bootstrap.spi.BeanDiscoveryMode;
import org.jboss.weld.bootstrap.spi.BeansXml;
import org.jboss.weld.bootstrap.spi.Metadata;
import org.jboss.weld.bootstrap.spi.Scanning;
Expand All @@ -15,32 +16,41 @@ public class BeansXmlImpl implements BeansXml {
private final List<Metadata<String>> enabledInterceptors;
private final Scanning scanning;
private final URL url;
private final BeanDiscoveryMode discoveryMode;
private final String version;

public BeansXmlImpl(List<Metadata<String>> enabledAlternatives, List<Metadata<String>> enabledAlternativeStereotypes, List<Metadata<String>> enabledDecorators, List<Metadata<String>> enabledInterceptors, Scanning scanning, URL url) {
public BeansXmlImpl(List<Metadata<String>> enabledAlternatives, List<Metadata<String>> enabledAlternativeStereotypes, List<Metadata<String>> enabledDecorators, List<Metadata<String>> enabledInterceptors, Scanning scanning, URL url, BeanDiscoveryMode discoveryMode, String version) {
this.enabledAlternatives = enabledAlternatives;
this.enabledAlternativeStereotypes = enabledAlternativeStereotypes;
this.enabledDecorators = enabledDecorators;
this.enabledInterceptors = enabledInterceptors;
this.scanning = scanning;
this.url = url;
this.discoveryMode = discoveryMode;
this.version = version;
}

@Override
public List<Metadata<String>> getEnabledAlternativeClasses() {
return enabledAlternatives;
}

@Override
public List<Metadata<String>> getEnabledAlternativeStereotypes() {
return enabledAlternativeStereotypes;
}

@Override
public List<Metadata<String>> getEnabledDecorators() {
return enabledDecorators;
}

@Override
public List<Metadata<String>> getEnabledInterceptors() {
return enabledInterceptors;
}

@Override
public Scanning getScanning() {
return scanning;
}
Expand All @@ -50,4 +60,13 @@ public URL getUrl() {
return url;
}

@Override
public BeanDiscoveryMode getBeanDiscoveryMode() {
return discoveryMode;
}

@Override
public String getVersion() {
return version;
}
}
37 changes: 31 additions & 6 deletions impl/src/main/java/org/jboss/weld/xml/BeansXmlHandler.java
Expand Up @@ -13,10 +13,10 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.jboss.weld.bootstrap.spi.BeanDiscoveryMode;
import org.jboss.weld.bootstrap.spi.BeansXml;
import org.jboss.weld.bootstrap.spi.ClassAvailableActivation;
import org.jboss.weld.bootstrap.spi.Filter;
Expand All @@ -35,6 +35,8 @@
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

import com.google.common.collect.ImmutableSet;

/**
* An implementation of the beans.xml parser written using SAX
* <p/>
Expand All @@ -60,8 +62,8 @@ public abstract class Container {
private final String localName;
private final Collection<String> nestedElements;

public Container(String[] uris, String localName, String... nestedElements) {
this.uris = new HashSet<String>(asList(uris));
public Container(Set<String> uris, String localName, String... nestedElements) {
this.uris = uris;
this.localName = localName;
this.nestedElements = asList(nestedElements);
}
Expand Down Expand Up @@ -115,11 +117,15 @@ public SpecContainer(String localName, String... nestedElements) {
}

public static final String WELD_URI = "http://jboss.org/schema/weld/beans";
public static final String[] WELD_URIS = new String[] { WELD_URI };
public static final Set<String> WELD_URIS = ImmutableSet.of(WELD_URI);

public static final String JAVAEE_LEGACY_URI = "http://java.sun.com/xml/ns/javaee";
public static final String JAVAEE_URI = "http://xmlns.jcp.org/xml/ns/javaee";
public static final String[] JAVAEE_URIS = new String[] { JAVAEE_URI, JAVAEE_LEGACY_URI };
public static final Set<String> JAVAEE_URIS = ImmutableSet.of(JAVAEE_LEGACY_URI, JAVAEE_URI);

private static final String VERSION_ATTRIBUTE_NAME = "version";
private static final String BEAN_DISCOVERY_MODE_ATTRIBUTE_NAME = "bean-discovery-mode";
private static final String ROOT_ELEMENT_NAME = "beans";

/*
* The containers we are parsing
Expand All @@ -136,6 +142,8 @@ public SpecContainer(String localName, String... nestedElements) {
private final List<Metadata<Filter>> includes;
private final List<Metadata<Filter>> excludes;
protected final URL file;
private BeanDiscoveryMode discoveryMode;
private String version;

/*
* Parser State
Expand All @@ -155,6 +163,7 @@ public BeansXmlHandler(final URL file) {
this.excludes = new ArrayList<Metadata<Filter>>();
this.seenContainers = new ArrayList<Container>();
this.containers = new ArrayList<Container>();
this.discoveryMode = BeanDiscoveryMode.ALL; // this is the default value for a beans.xml file
containers.add(new SpecContainer("interceptors", "class") {

@Override
Expand Down Expand Up @@ -256,6 +265,11 @@ public void handleMultiple() {

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (localName.equals(ROOT_ELEMENT_NAME) && (uri == "" || JAVAEE_URIS.contains(uri))) {
processRootElement(attributes);
return;
}

if (currentContainer == null) {
Container container = getContainer(uri, localName);
if (container != null) {
Expand All @@ -273,6 +287,17 @@ public void startElement(String uri, String localName, String qName, Attributes
}
}

private void processRootElement(Attributes attributes) {
String discoveryMode = attributes.getValue(BEAN_DISCOVERY_MODE_ATTRIBUTE_NAME);
if (discoveryMode != null) {
this.discoveryMode = BeanDiscoveryMode.valueOf(discoveryMode.toUpperCase());
}
String version = attributes.getValue(VERSION_ATTRIBUTE_NAME);
if (version != null) {
this.version = version;
}
}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (currentContainer != null) {
Expand Down Expand Up @@ -312,7 +337,7 @@ private static Container getContainer(String uri, String localName, Collection<C
}

public BeansXml createBeansXml() {
return new BeansXmlImpl(alternativesClasses, alternativeStereotypes, decorators, interceptors, new ScanningImpl(includes, excludes), file);
return new BeansXmlImpl(alternativesClasses, alternativeStereotypes, decorators, interceptors, new ScanningImpl(includes, excludes), file, discoveryMode, version);
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion impl/src/main/java/org/jboss/weld/xml/BeansXmlParser.java
Expand Up @@ -33,6 +33,7 @@
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.jboss.weld.bootstrap.spi.BeanDiscoveryMode;
import org.jboss.weld.bootstrap.spi.BeansXml;
import org.jboss.weld.bootstrap.spi.Filter;
import org.jboss.weld.bootstrap.spi.Metadata;
Expand Down Expand Up @@ -148,7 +149,7 @@ public BeansXml parse(Iterable<URL> urls, boolean removeDuplicates) {
*/
beansXmlUrl = url;
}
return new BeansXmlImpl(alternatives, alternativeStereotypes, decorators, interceptors, new ScanningImpl(includes, excludes), beansXmlUrl);
return new BeansXmlImpl(alternatives, alternativeStereotypes, decorators, interceptors, new ScanningImpl(includes, excludes), beansXmlUrl, BeanDiscoveryMode.ALL, null);
}

private void addTo(List<Metadata<String>> list, List<Metadata<String>> listToAdd, boolean removeDuplicates) {
Expand Down
@@ -0,0 +1,76 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2013, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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.jboss.weld.tests.unit.bootstrap.xml.cdi11;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;

import org.jboss.weld.bootstrap.WeldBootstrap;
import org.jboss.weld.bootstrap.spi.BeanDiscoveryMode;
import org.jboss.weld.bootstrap.spi.BeansXml;
import org.testng.annotations.Test;

public class BeansXmlParsingTest {

private final WeldBootstrap bootstrap = new WeldBootstrap();

private BeansXml getBeansXml(String filename) {
return bootstrap.parse(BeansXmlParsingTest.class.getResource(filename));
}

@Test
public void testNoSchemaNoAttributes() {
BeansXml xml = getBeansXml("cdi11-beans1.xml");
assertEquals(xml.getBeanDiscoveryMode(), BeanDiscoveryMode.ALL);
assertNull(xml.getVersion());
}

@Test
public void testExplicitVersionAndMode1() {
BeansXml xml = getBeansXml("cdi11-beans2.xml");
assertEquals(xml.getBeanDiscoveryMode(), BeanDiscoveryMode.NONE);
assertEquals(xml.getVersion(), "1.1");
}

@Test
public void testExplicitVersionAndMode2() {
BeansXml xml = getBeansXml("cdi11-beans3.xml");
assertEquals(xml.getBeanDiscoveryMode(), BeanDiscoveryMode.ANNOTATED);
assertEquals(xml.getVersion(), "1.1");
}

@Test
public void testExplicitVersionAndMode3() {
BeansXml xml = getBeansXml("cdi11-beans3b.xml");
assertEquals(xml.getBeanDiscoveryMode(), BeanDiscoveryMode.ANNOTATED);
assertEquals(xml.getVersion(), "1.1");
}

@Test
public void testExplicitVersionAndMode4() {
BeansXml xml = getBeansXml("cdi11-beans4.xml");
assertEquals(xml.getBeanDiscoveryMode(), BeanDiscoveryMode.ALL);
assertEquals(xml.getVersion(), "1.1");
}

@Test
public void testEmptyFile() {
BeansXml xml = getBeansXml("cdi11-beans5.xml");
assertEquals(xml.getBeanDiscoveryMode(), BeanDiscoveryMode.ALL);
assertNull(xml.getVersion());
}
}
@@ -1,6 +1,8 @@
package org.jboss.weld.tests.unit.deployment.structure.nonTransitiveResolution;

import com.google.common.base.Function;

import org.jboss.weld.bootstrap.spi.BeanDiscoveryMode;
import org.jboss.weld.bootstrap.spi.BeansXml;
import org.jboss.weld.bootstrap.spi.Metadata;
import org.jboss.weld.bootstrap.spi.Scanning;
Expand Down Expand Up @@ -79,4 +81,14 @@ public URL getUrl() {
return null;
}

@Override
public BeanDiscoveryMode getBeanDiscoveryMode() {
return BeanDiscoveryMode.ALL;
}

@Override
public String getVersion() {
return null;
}

}
@@ -0,0 +1,12 @@
<!-- ~ JBoss, Home of Professional Open Source ~ Copyright 2012, Red Hat, Inc., and
individual contributors ~ by the @authors tag. See the copyright.txt in the distribution
for a ~ full listing of individual contributors. ~ ~ Licensed 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>
</beans>
@@ -0,0 +1,14 @@
<!-- ~ JBoss, Home of Professional Open Source ~ Copyright 2012, Red Hat, Inc., and
individual contributors ~ by the @authors tag. See the copyright.txt in the distribution
for a ~ full listing of individual contributors. ~ ~ Licensed 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_1_1.xsd" version="1.1" bean-discovery-mode="none">
</beans>
@@ -0,0 +1,14 @@
<!-- ~ JBoss, Home of Professional Open Source ~ Copyright 2012, Red Hat, Inc., and
individual contributors ~ by the @authors tag. See the copyright.txt in the distribution
for a ~ full listing of individual contributors. ~ ~ Licensed 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_1_1.xsd" version="1.1" bean-discovery-mode="annotated">
</beans>
@@ -0,0 +1,12 @@
<!-- ~ JBoss, Home of Professional Open Source ~ Copyright 2012, Red Hat, Inc., and
individual contributors ~ by the @authors tag. See the copyright.txt in the distribution
for a ~ full listing of individual contributors. ~ ~ Licensed 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 version="1.1" bean-discovery-mode="annotated">
</beans>
@@ -0,0 +1,14 @@
<!-- ~ JBoss, Home of Professional Open Source ~ Copyright 2012, Red Hat, Inc., and
individual contributors ~ by the @authors tag. See the copyright.txt in the distribution
for a ~ full listing of individual contributors. ~ ~ Licensed 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_1_1.xsd" version="1.1" bean-discovery-mode="all">
</beans>
Empty file.

0 comments on commit 32580b3

Please sign in to comment.