[jboss-cvs] JBossAS SVN: r76956 - in trunk/system-jmx/src: resources/tests/org/jboss/test/system/metadata/value/valuefactory/test and 1 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Aug 11 17:45:13 EDT 2008
Author: bstansberry at jboss.com
Date: 2008-08-11 17:45:13 -0400 (Mon, 11 Aug 2008)
New Revision: 76956
Added:
trunk/system-jmx/src/resources/tests/org/jboss/test/system/metadata/value/valuefactory/test/ElementParameter.xml
trunk/system-jmx/src/resources/tests/org/jboss/test/system/metadata/value/valuefactory/test/TrimAndReplace.xml
trunk/system-jmx/src/resources/tests/org/jboss/test/system/metadata/value/valuefactory/test/TrimAndReplaceWithValue.xml
trunk/system-jmx/src/resources/tests/org/jboss/test/system/metadata/value/valuefactory/test/TrimAndReplaceWithValueAndOverride.xml
Modified:
trunk/system-jmx/src/main/org/jboss/system/metadata/ServiceMetaDataParser.java
trunk/system-jmx/src/tests/org/jboss/test/system/metadata/value/valuefactory/test/ServiceValueFactoryParameterMetaDataUnitTestCase.java
trunk/system-jmx/src/tests/org/jboss/test/system/metadata/value/valuefactory/test/ValueFactoryParsingUnitTestCase.java
Log:
[JBAS-5822] Handle nested CDATA sections, trimming and system property replacement
Modified: trunk/system-jmx/src/main/org/jboss/system/metadata/ServiceMetaDataParser.java
===================================================================
--- trunk/system-jmx/src/main/org/jboss/system/metadata/ServiceMetaDataParser.java 2008-08-11 20:01:03 UTC (rev 76955)
+++ trunk/system-jmx/src/main/org/jboss/system/metadata/ServiceMetaDataParser.java 2008-08-11 21:45:13 UTC (rev 76956)
@@ -21,6 +21,9 @@
*/
package org.jboss.system.metadata;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.security.PrivilegedActionException;
import java.util.ArrayList;
import java.util.List;
@@ -537,7 +540,7 @@
return value;
}
- private ServiceValueMetaData parseValueFactory(Element el) throws DeploymentException
+ private ServiceValueMetaData parseValueFactory(Element el) throws Exception
{
ServiceValueMetaData value;
String dependency = el.getAttribute("bean");
@@ -582,7 +585,7 @@
return value;
}
- private ServiceValueFactoryParameterMetaData parseValueFactoryParameter(Element el) throws DeploymentException
+ private ServiceValueFactoryParameterMetaData parseValueFactoryParameter(Element el) throws Exception
{
String parameterType = null;
Attr attr = el.getAttributeNode("class");
@@ -595,20 +598,26 @@
Node child = el.getFirstChild();
if (child.getNodeType() == Node.ELEMENT_NODE)
{
- if ("value".equals(((Element)child).getTagName()))
- {
+ Element valueEl = (Element) child;
+ if ("value".equals(valueEl.getTagName()))
+ {
valueType = ((Element)child).getAttribute("class");
- textValue = child.getFirstChild().getNodeValue();
+ if (valueType.length() == 0)
+ valueType = null;
+
+ textValue = getElementTextContent(valueEl);
+ // Deal with any trim/replace from the outer element
+ textValue = trimAndReplace(textValue, getTrim(el), getReplace(el));
}
- else if ("null".equals(((Element)child).getTagName()) == false)
+ else if ("null".equals(valueEl.getTagName()) == false)
{
throw new DeploymentException("Element " + ((Element)child).getTagName() + " not supported as a child of value-factory/parameter in a -service.xml");
}
}
- else if (child.getNodeType() == Node.TEXT_NODE)
+ else
{
- textValue = child.getNodeValue();
- }
+ textValue = getElementTextContent(el);
+ }
return new ServiceValueFactoryParameterMetaData(textValue, parameterType, valueType);
}
@@ -658,6 +667,42 @@
}
/**
+ * Get an element's text content, looking for "trim" and "replace" attributes
+ * on the element to determine whether to trim the text and/or perform
+ * system property substitution.
+ *
+ * @param element the element
+ * @return the concatentation of the text nodes
+ * @throws Exception for any error
+ */
+ public static String getElementTextContent(Element element) throws Exception
+ {
+ boolean replace = getReplace(element);
+ boolean trim = getTrim(element);
+
+ String rawText = getRawElementTextContent(element);
+ return trimAndReplace(rawText, trim, replace);
+ }
+
+ public static boolean getTrim(Element element)
+ {
+ boolean trim = true;
+ String trimAttr = element.getAttribute("trim");
+ if (trimAttr.length() > 0)
+ trim = Boolean.valueOf(trimAttr).booleanValue();
+ return trim;
+ }
+
+ public static boolean getReplace(Element element)
+ {
+ boolean replace = true;
+ String replaceAttr = element.getAttribute("replace");
+ if (replaceAttr.length() > 0)
+ replace = Boolean.valueOf(replaceAttr).booleanValue();
+ return replace;
+ }
+
+ /**
* Get an elements text content
*
* @param element the element
@@ -668,20 +713,51 @@
*/
public static String getElementTextContent(Element element, boolean trim, boolean replace) throws Exception
{
+ String rawText = getRawElementTextContent(element);
+
+ return trimAndReplace(rawText, trim, replace);
+ }
+
+ public static String getRawElementTextContent(Element element)
+ {
NodeList nl = element.getChildNodes();
- String attributeText = "";
+ String rawText = "";
for (int i = 0; i < nl.getLength(); i++)
{
Node n = nl.item(i);
if (n instanceof Text)
{
- attributeText += ((Text) n).getData();
+ rawText += ((Text) n).getData();
}
}
+ return rawText;
+ }
+
+ public static String trimAndReplace(String rawText, boolean trim, boolean replace)
+ {
if (trim)
- attributeText = attributeText.trim();
+ rawText = rawText.trim();
if (replace)
- attributeText = StringPropertyReplacer.replaceProperties(attributeText);
- return attributeText;
- }
+ {
+ SecurityManager manager = System.getSecurityManager();
+ if (manager == null)
+ {
+ rawText = StringPropertyReplacer.replaceProperties(rawText);
+ }
+ else
+ {
+ final String input = rawText;
+ rawText = AccessController.doPrivileged(new PrivilegedAction<String>()
+ {
+ public String run()
+ {
+ return StringPropertyReplacer.replaceProperties(input);
+ }
+ });
+ }
+ }
+ return rawText;
+
+ }
+
}
Added: trunk/system-jmx/src/resources/tests/org/jboss/test/system/metadata/value/valuefactory/test/ElementParameter.xml
===================================================================
--- trunk/system-jmx/src/resources/tests/org/jboss/test/system/metadata/value/valuefactory/test/ElementParameter.xml (rev 0)
+++ trunk/system-jmx/src/resources/tests/org/jboss/test/system/metadata/value/valuefactory/test/ElementParameter.xml 2008-08-11 21:45:13 UTC (rev 76956)
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<mbean name="jboss.test:type=BasicMBeanName" code="BasicMBeanCode">
+ <attribute name="Attribute">
+ <value-factory bean="bean" method="method" default="default">
+ <parameter><![CDATA[<element/>]]></parameter>
+ </value-factory>
+ </attribute>
+</mbean>
\ No newline at end of file
Added: trunk/system-jmx/src/resources/tests/org/jboss/test/system/metadata/value/valuefactory/test/TrimAndReplace.xml
===================================================================
--- trunk/system-jmx/src/resources/tests/org/jboss/test/system/metadata/value/valuefactory/test/TrimAndReplace.xml (rev 0)
+++ trunk/system-jmx/src/resources/tests/org/jboss/test/system/metadata/value/valuefactory/test/TrimAndReplace.xml 2008-08-11 21:45:13 UTC (rev 76956)
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<mbean name="jboss.test:type=BasicMBeanName" code="BasicMBeanCode">
+ <attribute name="Attribute">
+ <value-factory bean="bean" method="method" default="default">
+ <parameter> ${valuefactory.test.property:1} </parameter>
+ <parameter trim="false" replace="false"> ${valuefactory.test.property:1} </parameter>
+ </value-factory>
+ </attribute>
+</mbean>
\ No newline at end of file
Added: trunk/system-jmx/src/resources/tests/org/jboss/test/system/metadata/value/valuefactory/test/TrimAndReplaceWithValue.xml
===================================================================
--- trunk/system-jmx/src/resources/tests/org/jboss/test/system/metadata/value/valuefactory/test/TrimAndReplaceWithValue.xml (rev 0)
+++ trunk/system-jmx/src/resources/tests/org/jboss/test/system/metadata/value/valuefactory/test/TrimAndReplaceWithValue.xml 2008-08-11 21:45:13 UTC (rev 76956)
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<mbean name="jboss.test:type=BasicMBeanName" code="BasicMBeanCode">
+ <attribute name="Attribute">
+ <value-factory bean="bean" method="method" default="default">
+ <parameter><value class="java.lang.Integer"> ${valuefactory.test.property:1} </value></parameter>
+ <parameter trim="false" replace="false"><value trim="false" replace="false"> ${valuefactory.test.property:1} </value></parameter>
+ </value-factory>
+ </attribute>
+</mbean>
\ No newline at end of file
Added: trunk/system-jmx/src/resources/tests/org/jboss/test/system/metadata/value/valuefactory/test/TrimAndReplaceWithValueAndOverride.xml
===================================================================
--- trunk/system-jmx/src/resources/tests/org/jboss/test/system/metadata/value/valuefactory/test/TrimAndReplaceWithValueAndOverride.xml (rev 0)
+++ trunk/system-jmx/src/resources/tests/org/jboss/test/system/metadata/value/valuefactory/test/TrimAndReplaceWithValueAndOverride.xml 2008-08-11 21:45:13 UTC (rev 76956)
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<mbean name="jboss.test:type=BasicMBeanName" code="BasicMBeanCode">
+ <attribute name="Attribute">
+ <value-factory bean="bean" method="method" default="default">
+ <parameter><value class="java.lang.Integer"> ${valuefactory.test.property:1} </value></parameter>
+ <parameter><value trim="false" replace="false"> ${valuefactory.test.property:1} </value></parameter>
+ </value-factory>
+ </attribute>
+</mbean>
\ No newline at end of file
Modified: trunk/system-jmx/src/tests/org/jboss/test/system/metadata/value/valuefactory/test/ServiceValueFactoryParameterMetaDataUnitTestCase.java
===================================================================
--- trunk/system-jmx/src/tests/org/jboss/test/system/metadata/value/valuefactory/test/ServiceValueFactoryParameterMetaDataUnitTestCase.java 2008-08-11 20:01:03 UTC (rev 76955)
+++ trunk/system-jmx/src/tests/org/jboss/test/system/metadata/value/valuefactory/test/ServiceValueFactoryParameterMetaDataUnitTestCase.java 2008-08-11 21:45:13 UTC (rev 76956)
@@ -24,7 +24,12 @@
import static org.jboss.system.metadata.ServiceValueFactoryParameterMetaData.getValue;
+import java.beans.PropertyEditor;
+import java.beans.PropertyEditorManager;
+
import org.jboss.deployers.spi.DeploymentException;
+import org.jboss.util.propertyeditor.ElementEditor;
+import org.w3c.dom.Element;
import junit.framework.TestCase;
@@ -45,6 +50,10 @@
private static final String STRING = "java.lang.String";
private static final String HASH_MAP = "java.util.HashMap";
private static final String ATTR = "Attr";
+ private static final String ELEMENT = Element.class.getName();
+ private static final String ELEMENT_ATTR = "<element/>";
+
+ private PropertyEditor existingElementEditor;
/**
* Create a new ServiceValueFactoryParameterMetaDataUnitTestCase.
@@ -56,6 +65,27 @@
super(name);
}
+ @Override
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+
+ existingElementEditor = PropertyEditorManager.findEditor(Element.class);
+ if (existingElementEditor == null)
+ PropertyEditorManager.registerEditor(Element.class, ElementEditor.class);
+ }
+
+ @Override
+ protected void tearDown() throws Exception
+ {
+ if (existingElementEditor == null)
+ PropertyEditorManager.registerEditor(Element.class, null);
+
+ super.tearDown();
+ }
+
+
+
public void testGetValueNull() throws Exception
{
assertNull(getValue(Thread.currentThread().getContextClassLoader(), null, STRING, ATTR));
@@ -86,6 +116,12 @@
assertEquals(ONE, getValue(Thread.currentThread().getContextClassLoader(), ONE, STRING, ATTR));
}
+ public void testGetValueElement() throws Exception
+ {
+ Element result = (Element) getValue(Thread.currentThread().getContextClassLoader(), ELEMENT_ATTR, ELEMENT, ATTR);
+ assertEquals("element", result.getNodeName());
+ }
+
public void testGetValueNoPropertyEditor()
{
try
Modified: trunk/system-jmx/src/tests/org/jboss/test/system/metadata/value/valuefactory/test/ValueFactoryParsingUnitTestCase.java
===================================================================
--- trunk/system-jmx/src/tests/org/jboss/test/system/metadata/value/valuefactory/test/ValueFactoryParsingUnitTestCase.java 2008-08-11 20:01:03 UTC (rev 76955)
+++ trunk/system-jmx/src/tests/org/jboss/test/system/metadata/value/valuefactory/test/ValueFactoryParsingUnitTestCase.java 2008-08-11 21:45:13 UTC (rev 76956)
@@ -48,6 +48,7 @@
private static final String LONG = "java.lang.Long";
private static final ServiceValueFactoryParameterMetaData PARAMETER_MD = new ServiceValueFactoryParameterMetaData(PARAMETER, null, null);
private static final List<ServiceValueFactoryParameterMetaData> SIMPLE_LIST = Arrays.asList(PARAMETER_MD);
+ private static final String UNFIXED = " ${valuefactory.test.property:1} ";
public ValueFactoryParsingUnitTestCase(String name)
{
@@ -98,6 +99,13 @@
assertValueFactoryValue(value, Arrays.asList(one, two), DEFAULT);
}
+ public void testElementParameter() throws Exception
+ {
+ ServiceValueMetaData value = unmarshallSingleValue();
+ ServiceValueFactoryParameterMetaData one = new ServiceValueFactoryParameterMetaData("<element/>", null, null);
+ assertValueFactoryValue(value, Arrays.asList(one), DEFAULT);
+ }
+
public void testState() throws Exception
{
ServiceValueMetaData value = unmarshallSingleValue();
@@ -137,4 +145,36 @@
assertValueFactoryValue(value, Arrays.asList(one, two), DEFAULT);
}
+
+ public void testTrimAndReplace() throws Exception
+ {
+ ServiceValueMetaData value = unmarshallSingleValue();
+ ServiceValueFactoryParameterMetaData one = new ServiceValueFactoryParameterMetaData(ONE, null, null);
+ ServiceValueFactoryParameterMetaData two = new ServiceValueFactoryParameterMetaData(UNFIXED, null, null);
+
+ assertValueFactoryValue(value, Arrays.asList(one, two), DEFAULT);
+
+ }
+
+ public void testTrimAndReplaceWithValue() throws Exception
+ {
+ ServiceValueMetaData value = unmarshallSingleValue();
+ ServiceValueFactoryParameterMetaData one = new ServiceValueFactoryParameterMetaData(ONE, null, INTEGER);
+ ServiceValueFactoryParameterMetaData two = new ServiceValueFactoryParameterMetaData(UNFIXED, null, null);
+
+ assertValueFactoryValue(value, Arrays.asList(one, two), DEFAULT);
+
+ }
+
+ public void testTrimAndReplaceWithValueAndOverride() throws Exception
+ {
+ ServiceValueMetaData value = unmarshallSingleValue();
+ ServiceValueFactoryParameterMetaData one = new ServiceValueFactoryParameterMetaData(ONE, null, INTEGER);
+ ServiceValueFactoryParameterMetaData two = new ServiceValueFactoryParameterMetaData(ONE, null, null);
+
+ assertValueFactoryValue(value, Arrays.asList(one, two), DEFAULT);
+
+ }
+
+
}
More information about the jboss-cvs-commits
mailing list