You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by st...@apache.org on 2015/04/26 22:50:55 UTC
svn commit: r1676145 - in /openwebbeans/trunk/webbeans-impl/src:
main/java/org/apache/webbeans/xml/DefaultBeanArchiveService.java
test/java/org/apache/webbeans/test/xml/BeanArchiveServiceTest.java
Author: struberg
Date: Sun Apr 26 20:50:54 2015
New Revision: 1676145
URL: http://svn.apache.org/r1676145
Log:
OWB-1059 improve stability by not relying on InputStream.available()
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/xml/DefaultBeanArchiveService.java
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/xml/BeanArchiveServiceTest.java
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/xml/DefaultBeanArchiveService.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/xml/DefaultBeanArchiveService.java?rev=1676145&r1=1676144&r2=1676145&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/xml/DefaultBeanArchiveService.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/xml/DefaultBeanArchiveService.java Sun Apr 26 20:50:54 2015
@@ -22,6 +22,7 @@ import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.IOException;
import java.io.InputStream;
+import java.io.PushbackInputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
@@ -176,45 +177,59 @@ public class DefaultBeanArchiveService i
* Read the information from the given beans.xml and fill it into a
* BeanArchiveInformation instance.
*/
- protected BeanArchiveInformation readBeansXml(InputStream xmlStream) throws IOException
+ protected BeanArchiveInformation readBeansXml(InputStream xmlStreamIn) throws IOException
{
DefaultBeanArchiveInformation bdaInfo = createBeanArchiveInformation();
- if (xmlStream != null && xmlStream.available() == 0)
+ if (xmlStreamIn != null)
{
- // an empty beans.xml will be treated as ALL for backward compat with CDI-1.0
- bdaInfo.setBeanDiscoveryMode(BeanDiscoveryMode.ALL);
- }
- else if (xmlStream != null && xmlStream.available() > 0)
- {
- //Get root element of the XML document
- Element webBeansRoot = getBeansRootElement(xmlStream);
- if (webBeansRoot != null)
+ PushbackInputStream xmlStream = new PushbackInputStream(xmlStreamIn);
+
+ // try to read from the stream
+ int firstVal = xmlStream.read();
+ if (firstVal < 0)
{
- if (!"beans".equalsIgnoreCase(webBeansRoot.getLocalName()))
+ // this means the stream is empty
+ bdaInfo.setBeanDiscoveryMode(BeanDiscoveryMode.ALL);
+ }
+ else
+ {
+ // put the first byte back on the stream so we can properly parse the XML.
+ xmlStream.unread(firstVal);
+
+ //Get root element of the XML document
+ Element webBeansRoot = getBeansRootElement(xmlStream);
+ if (webBeansRoot == null)
{
- throw new WebBeansConfigurationException("beans.xml must have a <beans> root element, but has: " + webBeansRoot.getLocalName());
+ bdaInfo.setBeanDiscoveryMode(BeanDiscoveryMode.ALL);
}
+ else
+ {
+ if (!"beans".equalsIgnoreCase(webBeansRoot.getLocalName()))
+ {
+ throw new WebBeansConfigurationException("beans.xml must have a <beans> root element, but has: " + webBeansRoot.getLocalName());
+ }
- bdaInfo.setVersion(getTrimmedAttribute(webBeansRoot, "version"));
+ bdaInfo.setVersion(getTrimmedAttribute(webBeansRoot, "version"));
- String beanDiscoveryMode = getTrimmedAttribute(webBeansRoot, "bean-discovery-mode");
- bdaInfo.setBeanDiscoveryMode(beanDiscoveryMode != null ? BeanDiscoveryMode.valueOf(beanDiscoveryMode.toUpperCase()) : null);
+ String beanDiscoveryMode = getTrimmedAttribute(webBeansRoot, "bean-discovery-mode");
+ bdaInfo.setBeanDiscoveryMode(beanDiscoveryMode != null ? BeanDiscoveryMode.valueOf(beanDiscoveryMode.toUpperCase()) : null);
- readBeanChildren(bdaInfo, webBeansRoot);
- }
+ readBeanChildren(bdaInfo, webBeansRoot);
+ }
- if (bdaInfo.getVersion() != null && !"1.0".equals(bdaInfo.getVersion()) && bdaInfo.getBeanDiscoveryMode() == null)
- {
- throw new WebBeansConfigurationException("beans.xml with version 1.1 and higher must declare a bean-discovery-mode!");
- }
+ if (bdaInfo.getVersion() != null && !"1.0".equals(bdaInfo.getVersion()) && bdaInfo.getBeanDiscoveryMode() == null)
+ {
+ throw new WebBeansConfigurationException("beans.xml with version 1.1 and higher must declare a bean-discovery-mode!");
+ }
- if (bdaInfo.getBeanDiscoveryMode() == null)
- {
- // an empty beans.xml file lead to backward compat mode with CDI-1.1.
- bdaInfo.setBeanDiscoveryMode(BeanDiscoveryMode.ALL);
+ if (bdaInfo.getBeanDiscoveryMode() == null)
+ {
+ // an empty beans.xml file lead to backward compat mode with CDI-1.1.
+ bdaInfo.setBeanDiscoveryMode(BeanDiscoveryMode.ALL);
+ }
}
}
Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/xml/BeanArchiveServiceTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/xml/BeanArchiveServiceTest.java?rev=1676145&r1=1676144&r2=1676145&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/xml/BeanArchiveServiceTest.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/xml/BeanArchiveServiceTest.java Sun Apr 26 20:50:54 2015
@@ -20,18 +20,25 @@ package org.apache.webbeans.test.xml;
import javax.enterprise.inject.spi.DeploymentException;
+import java.io.File;
import java.net.URL;
import org.apache.webbeans.spi.BeanArchiveService;
import org.apache.webbeans.spi.BeanArchiveService.BeanArchiveInformation;
import org.apache.webbeans.spi.BeanArchiveService.BeanDiscoveryMode;
import org.apache.webbeans.xml.DefaultBeanArchiveService;
+import org.junit.Rule;
import org.junit.Test;
import org.junit.Assert;
+import org.junit.rules.TemporaryFolder;
public class BeanArchiveServiceTest
{
+
+ @Rule
+ public TemporaryFolder tempFolder = new TemporaryFolder();
+
@Test
public void testNotExistingBeansXml() throws Exception
{
@@ -55,6 +62,16 @@ public class BeanArchiveServiceTest
}
@Test
+ public void testEmptyZeroSizeBeansXml() throws Exception
+ {
+ File emptyBeansXml = tempFolder.newFile("beans.xml");
+ BeanArchiveService bas = new DefaultBeanArchiveService();
+ BeanArchiveInformation beanArchiveInformation = bas.getBeanArchiveInformation(emptyBeansXml.toURI().toURL());
+ Assert.assertNotNull(beanArchiveInformation);
+ Assert.assertEquals(BeanDiscoveryMode.ALL, beanArchiveInformation.getBeanDiscoveryMode());
+ }
+
+ @Test
public void testAlternativesBeansXml() throws Exception
{
BeanArchiveInformation bai = scanBeansXml("alternatives_correct.xml");