]> source.dussan.org Git - archiva.git/commitdiff
incomplete support for PlexusConfiguration based on spring-context nodes
authorNicolas De Loof <nicolas@apache.org>
Thu, 28 Feb 2008 07:57:43 +0000 (07:57 +0000)
committerNicolas De Loof <nicolas@apache.org>
Thu, 28 Feb 2008 07:57:43 +0000 (07:57 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/branches@631880 13f79535-47bb-0310-9956-ffa450edef68

springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/DomPlexusConfiguration.java
springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusClassPathXmlApplicationContext.java
springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusConfigurationPropertyEditor.java [new file with mode: 0644]
springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusNamespaceHandler.java

index 62bad17473c9953ae2e366b5df5066c46ba02cba..b4b4921850a22e21d7707d32da022e524b4f62a5 100644 (file)
@@ -19,9 +19,19 @@ package org.codehaus.plexus.spring;
  * under the License.
  */
 
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.smartcardio.ATR;
+
 import org.codehaus.plexus.configuration.PlexusConfiguration;
 import org.codehaus.plexus.configuration.PlexusConfigurationException;
+import org.springframework.util.xml.DomUtils;
+import org.w3c.dom.Document;
 import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 
 /**
  * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
@@ -31,14 +41,11 @@ public class DomPlexusConfiguration
 {
     private Element root;
 
-    private String name;
-
     /**
      *
      */
-    public DomPlexusConfiguration( String name, Element root )
+    public DomPlexusConfiguration( Element root )
     {
-        this.name = name;
         this.root = root;
     }
 
@@ -59,8 +66,7 @@ public class DomPlexusConfiguration
     public String getAttribute( String paramName )
         throws PlexusConfigurationException
     {
-        // TODO Auto-generated method stub
-        return null;
+        return root.getAttribute( paramName );
     }
 
     /**
@@ -69,8 +75,12 @@ public class DomPlexusConfiguration
      */
     public String getAttribute( String name, String defaultValue )
     {
-        // TODO Auto-generated method stub
-        return null;
+        String attribute = root.getAttribute( name );
+        if ( attribute == null )
+        {
+            attribute = defaultValue;
+        }
+        return attribute;
     }
 
     /**
@@ -79,8 +89,13 @@ public class DomPlexusConfiguration
      */
     public String[] getAttributeNames()
     {
-        // TODO Auto-generated method stub
-        return null;
+        NamedNodeMap attributes = root.getAttributes();
+        String[] names = new String[attributes.getLength()];
+        for ( int i = 0; i < names.length; i++ )
+        {
+            names[i] = attributes.item( i ).getLocalName();
+        }
+        return names;
     }
 
     /**
@@ -89,7 +104,11 @@ public class DomPlexusConfiguration
      */
     public PlexusConfiguration getChild( String child )
     {
-        // TODO Auto-generated method stub
+        Element e = DomUtils.getChildElementByTagName( root, child );
+        if (e != null)
+        {
+            return new DomPlexusConfiguration( e );
+        }
         return null;
     }
 
@@ -99,7 +118,18 @@ public class DomPlexusConfiguration
      */
     public PlexusConfiguration getChild( int i )
     {
-        // TODO Auto-generated method stub
+        NodeList childs = root.getChildNodes();
+        for ( int j = 0; j < childs.getLength(); j++ )
+        {
+            Node child = childs.item( j );
+            if ( child.getNodeType() == Node.ELEMENT_NODE )
+            {
+                if (i-- == 0)
+                {
+                    return new DomPlexusConfiguration( (Element) child );
+                }
+            }
+        }
         return null;
     }
 
@@ -109,8 +139,13 @@ public class DomPlexusConfiguration
      */
     public PlexusConfiguration getChild( String child, boolean createChild )
     {
-        // TODO Auto-generated method stub
-        return null;
+        PlexusConfiguration config = getChild( child );
+        if (config == null && createChild )
+        {
+            // Creating a new child requires a Document
+            throw new UnsupportedOperationException( "Not implemented" );
+        }
+        return config;
     }
 
     /**
@@ -119,8 +154,17 @@ public class DomPlexusConfiguration
      */
     public int getChildCount()
     {
-        // TODO Auto-generated method stub
-        return 0;
+        int count = 0;
+        NodeList childs = root.getChildNodes();
+        for ( int i = 0; i < childs.getLength(); i++ )
+        {
+            Node child = childs.item( i );
+            if ( child.getNodeType() == Node.ELEMENT_NODE )
+            {
+                count++;
+            }
+        }
+        return count;
     }
 
     /**
@@ -129,8 +173,13 @@ public class DomPlexusConfiguration
      */
     public PlexusConfiguration[] getChildren()
     {
-        // TODO Auto-generated method stub
-        return null;
+        int count = getChildCount();
+        PlexusConfiguration[] children = new PlexusConfiguration[count];
+        for ( int i = 0; i < children.length; i++ )
+        {
+            children[i] = getChild( i );
+        }
+        return children;
     }
 
     /**
@@ -139,8 +188,7 @@ public class DomPlexusConfiguration
      */
     public PlexusConfiguration[] getChildren( String name )
     {
-        // TODO Auto-generated method stub
-        return null;
+        throw new UnsupportedOperationException( "Not implemented" );
     }
 
     /**
@@ -149,8 +197,7 @@ public class DomPlexusConfiguration
      */
     public String getName()
     {
-        // TODO Auto-generated method stub
-        return null;
+        return root.getNodeName();
     }
 
     /**
@@ -160,8 +207,7 @@ public class DomPlexusConfiguration
     public String getValue()
         throws PlexusConfigurationException
     {
-        // TODO Auto-generated method stub
-        return null;
+        return root.getNodeValue();
     }
 
     /**
@@ -170,7 +216,11 @@ public class DomPlexusConfiguration
      */
     public String getValue( String defaultValue )
     {
-        // TODO Auto-generated method stub
-        return null;
+        String value = root.getTextContent();
+        if ( value == null )
+        {
+            value = defaultValue;
+        }
+        return value;
     }
 }
index 3f8a036d3b39948505f2c60ef539826f277d68c5..083013582b4162e15701ceac18e8e9a263b0de21 100644 (file)
@@ -111,13 +111,19 @@ public class PlexusClassPathXmlApplicationContext
      */\r
     protected void postProcessBeanFactory( ConfigurableListableBeanFactory beanFactory )\r
     {\r
+        // Register a PlexusContainerAdapter bean to allow context lookups using plexus API\r
         PlexusContainerAdapter plexus = new PlexusContainerAdapter();\r
         plexus.setApplicationContext( this );\r
         beanFactory.registerSingleton( "plexusContainer", plexus );\r
 \r
+        // Register a beanPostProcessor to handle plexus interface-based lifecycle management\r
         lifecycleBeanPostProcessor = new PlexusLifecycleBeanPostProcessor();\r
         lifecycleBeanPostProcessor.setBeanFactory( this );\r
         beanFactory.addBeanPostProcessor( lifecycleBeanPostProcessor );\r
+\r
+        // Register a PorpertyEditor to support plexus XML <configuration> set as CDATA in\r
+        // a spring context XML file.\r
+        beanFactory.addPropertyEditorRegistrar( new PlexusConfigurationPropertyEditor() );\r
     }\r
 \r
     /**\r
diff --git a/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusConfigurationPropertyEditor.java b/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusConfigurationPropertyEditor.java
new file mode 100644 (file)
index 0000000..fb9ed40
--- /dev/null
@@ -0,0 +1,80 @@
+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.IOException;
+import java.io.StringReader;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.commons.lang.StringUtils;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
+import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
+import org.codehaus.plexus.util.xml.Xpp3Dom;
+import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
+import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
+import org.springframework.beans.PropertyEditorRegistrar;
+import org.springframework.beans.PropertyEditorRegistry;
+
+/**
+ * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
+ */
+public class PlexusConfigurationPropertyEditor
+    extends PropertyEditorSupport
+    implements PropertyEditorRegistrar
+{
+
+    /**
+     * {@inheritDoc}
+     *
+     * @see org.springframework.beans.PropertyEditorRegistrar#registerCustomEditors(org.springframework.beans.PropertyEditorRegistry)
+     */
+    public void registerCustomEditors( PropertyEditorRegistry registry )
+    {
+        registry.registerCustomEditor( PlexusConfiguration.class, this );
+    }
+
+    /**
+     * {@inheritDoc}
+     *
+     * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
+     */
+    public void setAsText( String text )
+        throws IllegalArgumentException
+    {
+        if (StringUtils.isBlank( text ))
+        {
+            setValue( null );
+            return;
+        }
+        try
+        {
+            Xpp3Dom dom = Xpp3DomBuilder.build( new StringReader( text ) );
+            XmlPlexusConfiguration configuration = new XmlPlexusConfiguration( dom );
+            setValue( configuration );
+        }
+        catch ( Exception e )
+        {
+            throw new IllegalArgumentException( "Failed to convert to Plexus XML configuration", e );
+        }
+    }
+
+}
index 16bc05635c017dca0797740d3c125e4bc5102843..2d0b6c6e22c314fc6fac2dac2eacd8cbfc253693 100644 (file)
@@ -127,7 +127,7 @@ public class PlexusNamespaceHandler
                 }\r
                 else\r
                 {\r
-                    dependencies.put( name, new DomPlexusConfiguration( name, child ) );\r
+                    dependencies.put( name, new DomPlexusConfiguration( child ) );\r
                 }\r
             }\r
 \r