]> source.dussan.org Git - archiva.git/commitdiff
add support for Properties as plexus <configuration>
authorNicolas De Loof <nicolas@apache.org>
Thu, 28 Feb 2008 15:48:18 +0000 (15:48 +0000)
committerNicolas De Loof <nicolas@apache.org>
Thu, 28 Feb 2008 15:48:18 +0000 (15:48 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/branches@632030 13f79535-47bb-0310-9956-ffa450edef68

springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusApplicationContextDelegate.java
springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusNamespaceHandler.java
springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PropertiesPropertyEditor.java [new file with mode: 0644]
springy/plexus-spring/src/test/java/org/codehaus/plexus/spring/PlexusClassPathXmlApplicationContextTest.java
springy/plexus-spring/src/test/java/org/codehaus/plexus/spring/PropertiesPlexusBean.java [new file with mode: 0644]
springy/plexus-spring/src/test/resources/testInjectProperties.xml [new file with mode: 0644]

index d75e03df85d9365ed4b8507725586f73b9d4f449..0392ed38cc0764b2c7c5a67c5b4b6f986560d92b 100644 (file)
@@ -80,6 +80,7 @@ public class PlexusApplicationContextDelegate
         // Register a PorpertyEditor to support plexus XML <configuration> set as CDATA in
         // a spring context XML file.
         beanFactory.addPropertyEditorRegistrar( new PlexusConfigurationPropertyEditor() );
+        beanFactory.addPropertyEditorRegistrar( new PropertiesPropertyEditor() );
     }
 
     /**
index fa5f1deaa0bc8f907792e8e138b5262d9af3ab1b..67251782194fce040c59acd4ef1d09b4fc29b2bd 100644 (file)
@@ -170,7 +170,11 @@ public class PlexusNamespaceHandler
         for ( int i = 0; i < childNodes.getLength(); i++ )\r
         {\r
             Node node = childNodes.item( i );\r
-            if (node.getNodeType() == Node.ELEMENT_NODE )\r
+            if (node.getNodeType() == Node.TEXT_NODE )\r
+            {\r
+                out.print( node.getTextContent() );\r
+            }\r
+            else if (node.getNodeType() == Node.ELEMENT_NODE )\r
             {\r
                 flatten( (Element) node, out );\r
             }\r
diff --git a/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PropertiesPropertyEditor.java b/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PropertiesPropertyEditor.java
new file mode 100644 (file)
index 0000000..99ef927
--- /dev/null
@@ -0,0 +1,94 @@
+package org.codehaus.plexus.spring;
+
+/*
+ * 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.
+ */
+
+import java.beans.PropertyEditorSupport;
+import java.io.StringReader;
+import java.util.Iterator;
+import java.util.Properties;
+
+import org.apache.commons.lang.StringUtils;
+import org.dom4j.Document;
+import org.dom4j.Element;
+import org.dom4j.io.SAXReader;
+import org.springframework.beans.PropertyEditorRegistrar;
+import org.springframework.beans.PropertyEditorRegistry;
+
+/**
+ * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
+ */
+public class PropertiesPropertyEditor
+    extends PropertyEditorSupport
+    implements PropertyEditorRegistrar
+{
+
+    /**
+     * {@inheritDoc}
+     *
+     * @see org.springframework.beans.PropertyEditorRegistrar#registerCustomEditors(org.springframework.beans.PropertyEditorRegistry)
+     */
+    public void registerCustomEditors( PropertyEditorRegistry registry )
+    {
+        registry.registerCustomEditor( Properties.class, this );
+    }
+
+    /**
+     * {@inheritDoc}
+     * <p>
+     * Support for plexus properties injection
+     * <pre>
+     * &lt;fieldName>
+     *     &lt;property&gt;
+     *       &lt;name&gt;key&lt;/name&gt;
+     *       &lt;value&gt;true&lt;/value&gt;
+     *     &lt;/property&gt;
+     * ...
+     * </pre>
+     *
+     * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
+     */
+    public void setAsText( String text )
+        throws IllegalArgumentException
+    {
+        if ( StringUtils.isBlank( text ) )
+        {
+            setValue( null );
+            return;
+        }
+        Properties properties = new Properties();
+        try
+        {
+            SAXReader reader = new SAXReader();
+            Document doc = reader.read( new StringReader( text ) );
+            Element root = doc.getRootElement();
+            for ( Iterator i = root.elementIterator(); i.hasNext(); )
+            {
+                Element element = (Element) i.next();
+                properties.setProperty( element.element( "name" ).getText(), element.element( "value" ).getText() );
+            }
+        }
+        catch ( Exception e )
+        {
+            throw new IllegalArgumentException( "Failed to convert to Properties", e );
+        }
+        setValue( properties );
+    }
+
+}
index 7d57730c6e0c13185816fac80ebb715ce3e30e92..2dabd6d22398c5e97e1a6a7116a889b430e52559 100644 (file)
@@ -98,4 +98,14 @@ public class PlexusClassPathXmlApplicationContextTest
         assertEquals( "expected", plexusBean.getConfig().getChild( "xml" ).getAttribute( "test" ) );
     }
 
+    public void testInjectProperties()
+        throws Exception
+    {
+        ConfigurableApplicationContext applicationContext =
+            new PlexusClassPathXmlApplicationContext( new String[] {
+                "testInjectProperties.xml" } );
+        PropertiesPlexusBean plexusBean = (PropertiesPlexusBean) applicationContext.getBean( "plexusBean" );
+        assertEquals( "expected", plexusBean.getProperties().getProperty( "test" ) );
+    }
+
 }
diff --git a/springy/plexus-spring/src/test/java/org/codehaus/plexus/spring/PropertiesPlexusBean.java b/springy/plexus-spring/src/test/java/org/codehaus/plexus/spring/PropertiesPlexusBean.java
new file mode 100644 (file)
index 0000000..80fb11c
--- /dev/null
@@ -0,0 +1,40 @@
+package org.codehaus.plexus.spring;
+
+import java.util.Properties;
+
+/*
+ * 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.
+ */
+
+/**
+ * A typical plexus component implementation
+ *
+ * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
+ */
+public class PropertiesPlexusBean
+{
+    private Properties properties;
+
+    public Properties getProperties()
+    {
+        return properties;
+    }
+
+
+
+}
diff --git a/springy/plexus-spring/src/test/resources/testInjectProperties.xml b/springy/plexus-spring/src/test/resources/testInjectProperties.xml
new file mode 100644 (file)
index 0000000..e4c1ee7
--- /dev/null
@@ -0,0 +1,20 @@
+<component-set>
+  <components>
+    <component>
+      <role>org.codehaus.plexus.spring.PlexusBean</role>
+      <implementation>org.codehaus.plexus.spring.PropertiesPlexusBean</implementation>
+      <configuration>
+        <properties>
+            <property>
+                <name>test</name>
+                <value>expected</value>
+            </property>
+            <property>
+                <name>other</name>
+                <value>value</value>
+            </property>
+        </properties>
+      </configuration>
+    </component>
+  </components>
+</component-set>