You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cz...@apache.org on 2007/08/06 10:44:48 UTC

svn commit: r563066 - in /felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin: SCRDescriptorMojo.java om/Property.java om/Reference.java xml/ComponentDescriptorIO.java

Author: cziegeler
Date: Mon Aug  6 01:44:47 2007
New Revision: 563066

URL: http://svn.apache.org/viewvc?view=rev&rev=563066
Log:
Continue refactoring to unified object model; add new xml mapper class.

Added:
    felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/xml/ComponentDescriptorIO.java   (with props)
Modified:
    felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/SCRDescriptorMojo.java
    felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/om/Property.java
    felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/om/Reference.java

Modified: felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/SCRDescriptorMojo.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/SCRDescriptorMojo.java?view=diff&rev=563066&r1=563065&r2=563066
==============================================================================
--- felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/SCRDescriptorMojo.java (original)
+++ felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/SCRDescriptorMojo.java Mon Aug  6 01:44:47 2007
@@ -286,7 +286,7 @@
             for (int i=0; fields != null && i < fields.length; i++) {
                 JavaTag tag = fields[i].getTagByName(SCRDescriptor.REFERENCE);
                 if (tag != null) {
-                    // this.doReference(tag, fields[i].getName(), sd);
+                    this.doReference(tag, fields[i].getName(), component);
                 }
 
                 tag = fields[i].getTagByName(SCRDescriptor.PROPERTY);
@@ -509,12 +509,27 @@
             prop.setDescription(property.getNamedParameter(SCRDescriptor.PROPERTY_DESCRIPTION));
             prop.setValue(property.getNamedParameter(SCRDescriptor.PROPERTY_VALUE));
             prop.setType(property.getNamedParameter(SCRDescriptor.PROPERTY_TYPE));
-            //prop.setPrivateProperty(this.getBoolean(property,
-            //    SCRDescriptor.PROPERTY_PRIVATE, prop.isPrivateProperty()));
+            prop.setPrivateProperty(this.getBoolean(property,
+                SCRDescriptor.PROPERTY_PRIVATE, prop.isPrivateProperty()));
 
             // set optional multivalues, cardinality might be overwritten by setValues !!
-            //prop.setCardinality(property.getNamedParameter(SCRDescriptor.PROPERTY_CARDINALITY));
-            //prop.setValues(property.getNamedParameterMap());
+            final String value = property.getNamedParameter(SCRDescriptor.PROPERTY_CARDINALITY);
+            if (value != null) {
+                if ("-".equals(value)) {
+                    // unlimited vector
+                    prop.setCardinality(new Integer(Integer.MIN_VALUE));
+                } else if ("+".equals(value)) {
+                   // unlimited array
+                    prop.setCardinality(new Integer(Integer.MAX_VALUE));
+                } else {
+                    try {
+                        prop.setCardinality(Integer.valueOf(value));
+                    } catch (NumberFormatException nfe) {
+                        // default to scalar in case of conversion problem
+                    }
+                }
+            }
+            prop.setValues(property.getNamedParameterMap());
 
             // check options
             String[] parameters = property.getParameters();
@@ -531,7 +546,7 @@
                     j += 2;
                 }
             }
-            //prop.setOptions(options);
+            prop.setOptions(options);
 
             component.addProperty(prop);
         }

Modified: felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/om/Property.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/om/Property.java?view=diff&rev=563066&r1=563065&r2=563066
==============================================================================
--- felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/om/Property.java (original)
+++ felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/om/Property.java Mon Aug  6 01:44:47 2007
@@ -18,7 +18,10 @@
  */
 package org.apache.felix.sandbox.scrplugin.om;
 
+import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.felix.sandbox.scrplugin.tags.JavaTag;
 
@@ -29,13 +32,19 @@
 public class Property extends AbstractObject {
 
     protected String name;
-    protected String value;
+    protected Object value;
     protected String type;
     protected String text;
 
     protected String label;
     protected String description;
 
+    protected Integer cardinality;
+
+    protected boolean privateProperty = false;
+
+    protected Map options;
+
     /**
      * Default constructor.
      */
@@ -58,7 +67,7 @@
         this.name = name;
     }
 
-    public String getValue() {
+    public Object getValue() {
         return this.value;
     }
 
@@ -98,11 +107,56 @@
         this.description = description;
     }
 
+    public Integer getCardinality() {
+        return this.cardinality;
+    }
+
+    public void setCardinality(Integer cardinality) {
+        this.cardinality = cardinality;
+    }
+
+    public boolean isPrivateProperty() {
+        return this.privateProperty;
+    }
+
+    public void setPrivateProperty(boolean privateProperty) {
+        this.privateProperty = privateProperty;
+    }
+
+    public void setValues(Map valueMap) {
+        List values = new ArrayList();
+        for (Iterator vi = valueMap.entrySet().iterator(); vi.hasNext();) {
+            Map.Entry entry = (Map.Entry) vi.next();
+            String key = (String) entry.getKey();
+            if (key.startsWith("values")) {
+                values.add(entry.getValue());
+            }
+        }
+
+        if (!values.isEmpty()) {
+            this.value = values;
+
+            // assume array if set to scalar currently
+            if (this.cardinality == null) {
+                this.cardinality = new Integer(Integer.MAX_VALUE);
+            }
+        }
+    }
+
+    public Map getOptions() {
+        return this.options;
+    }
+
+    public void setOptions(Map options) {
+        this.options = options;
+    }
+
     /**
      * Validate the property.
      * If errors occur a message is added to the issues list,
      * warnings can be added to the warnings list.
      */
     public void validate(List issues, List warnings) {
+        // might want to check name and type
     }
 }

Modified: felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/om/Reference.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/om/Reference.java?view=diff&rev=563066&r1=563065&r2=563066
==============================================================================
--- felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/om/Reference.java (original)
+++ felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/om/Reference.java Mon Aug  6 01:44:47 2007
@@ -20,7 +20,11 @@
 
 import java.util.List;
 
+import org.apache.felix.sandbox.scrplugin.tags.JavaClassDescription;
+import org.apache.felix.sandbox.scrplugin.tags.JavaMethod;
 import org.apache.felix.sandbox.scrplugin.tags.JavaTag;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.codehaus.plexus.util.StringUtils;
 
 /**
  * <code>Reference.java</code>...
@@ -98,7 +102,99 @@
      * If errors occur a message is added to the issues list,
      * warnings can be added to the warnings list.
      */
-    public void validate(List issues, List warnings) {
+    public void validate(List issues, List warnings)
+    throws MojoExecutionException {
+        // validate name
+        if (StringUtils.isEmpty(this.name)) {
+            issues.add(this.getMessage("Reference has no name"));
+        }
+
+        // validate interface
+        if (StringUtils.isEmpty(this.interfacename)) {
+            issues.add(this.getMessage("Missing interface name"));
+        }
+
+        // validate cardinality
+        if (this.cardinality == null) {
+            this.cardinality = "1..1";
+        } else if (!"0..1".equals(this.cardinality) && !"1..1".equals(this.cardinality)
+            && !"0..n".equals(this.cardinality) && !"1..n".equals(this.cardinality)) {
+            issues.add(this.getMessage("Invalid Cardinality specification " + this.cardinality));
+        }
+
+        // validate policy
+        if (this.policy == null) {
+            this.policy = "static";
+        } else if (!"static".equals(this.policy) && !"dynamic".equals(this.policy)) {
+            issues.add(this.getMessage("Invalid Policy specification " + this.policy));
+        }
+
+        // validate bind and unbind methods
+        JavaClassDescription javaClass = this.tag.getJavaClassDescription();
+        if (javaClass != null) {
+            this.bind = this.validateMethod(javaClass, this.bind, issues, warnings);
+            this.unbind = this.validateMethod(javaClass, this.unbind, issues, warnings);
+        } else {
+            issues.add(this.getMessage("Cannot find Java class to which the reference belongs"));
+        }
+    }
+
+    protected String validateMethod(JavaClassDescription javaClass, String methodName, List issues, List warnings)
+    throws MojoExecutionException {
+
+        JavaMethod method = this.findMethod(javaClass, methodName);
+
+        if (method == null) {
+            issues.add(this.getMessage("Missing method " + methodName + " for reference " + this.getName()));
+            return null;
+        }
+
+        if (method.isPublic()) {
+            warnings.add(this.getMessage("Method " + method.getName() + " should be declared protected"));
+        } else if (!method.isProtected()) {
+            issues.add(this.getMessage("Method " + method.getName() + " has wrong qualifier, public or protected required"));
+            return null;
+        }
+
+        return method.getName();
+    }
+
+    protected JavaMethod findMethod(JavaClassDescription javaClass, String methodName)
+    throws MojoExecutionException {
+
+        String[] sig = new String[]{ this.getInterfacename() };
+        String[] sig2 = new String[]{ "org.osgi.framework.ServiceReference" };
+
+        // service interface or ServiceReference first
+        String realMethodName = methodName;
+        JavaMethod method = javaClass.getMethodBySignature(realMethodName, sig);
+        if (method == null) {
+            method = javaClass.getMethodBySignature(realMethodName, sig2);
+        }
+
+        // append reference name with service interface and ServiceReference
+        if (method == null) {
+            realMethodName = methodName + Character.toUpperCase(this.name.charAt(0))
+            + this.name.substring(1);
+
+            method = javaClass.getMethodBySignature(realMethodName, sig);
+        }
+        if (method == null) {
+            method = javaClass.getMethodBySignature(realMethodName, sig2);
+        }
+
+        // append type name with service interface and ServiceReference
+        if (method == null) {
+            int lastDot = this.getInterfacename().lastIndexOf('.');
+            realMethodName = methodName
+                + this.getInterfacename().substring(lastDot + 1);
+            method = javaClass.getMethodBySignature(realMethodName, sig);
+        }
+        if (method == null) {
+            method = javaClass.getMethodBySignature(realMethodName, sig2);
+        }
+
+        return method;
     }
 
 }

Added: felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/xml/ComponentDescriptorIO.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/xml/ComponentDescriptorIO.java?view=auto&rev=563066
==============================================================================
--- felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/xml/ComponentDescriptorIO.java (added)
+++ felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/xml/ComponentDescriptorIO.java Mon Aug  6 01:44:47 2007
@@ -0,0 +1,133 @@
+/*
+ * 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.
+ */
+package org.apache.felix.sandbox.scrplugin.xml;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.io.Writer;
+
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.maven.plugin.MojoExecutionException;
+
+import com.thoughtworks.xstream.XStream;
+
+/**
+ * <code>ComponentDescriptorIO</code>
+ *
+ * is a helper class to read and write component descriptor files.
+ *
+ */
+public class ComponentDescriptorIO {
+
+    protected final XStream xstream;
+
+    public ComponentDescriptorIO() {
+        this.xstream = new XStream();
+        this.xstream.setMode(XStream.NO_REFERENCES);
+
+        this.xstream.alias("components", org.apache.felix.sandbox.scrplugin.om.Components.class);
+        this.xstream.addImplicitCollection(org.apache.felix.sandbox.scrplugin.om.Components.class, "components");
+
+        this.xstream.alias("component", org.apache.felix.sandbox.scrplugin.om.Component.class);
+        this.xstream.useAttributeFor(org.apache.felix.sandbox.scrplugin.om.Component.class, "name");
+        this.xstream.useAttributeFor(org.apache.felix.sandbox.scrplugin.om.Component.class, "enabled");
+        this.xstream.useAttributeFor(org.apache.felix.sandbox.scrplugin.om.Component.class, "immediate");
+        this.xstream.omitField(org.apache.felix.sandbox.scrplugin.om.Component.class, "label");
+        this.xstream.omitField(org.apache.felix.sandbox.scrplugin.om.Component.class, "description");
+        this.xstream.omitField(org.apache.felix.sandbox.scrplugin.om.Component.class, "isAbstract");
+        this.xstream.omitField(org.apache.felix.sandbox.scrplugin.om.Component.class, "hasMetatype");
+        this.xstream.omitField(org.apache.felix.sandbox.scrplugin.om.Component.class, "serviceFactory");
+
+        this.xstream.alias("implementation", org.apache.felix.sandbox.scrplugin.om.Implementation.class);
+        this.xstream.useAttributeFor(org.apache.felix.sandbox.scrplugin.om.Implementation.class, "classname");
+
+        this.xstream.alias("property", org.apache.felix.sandbox.scrplugin.om.Property.class);
+        this.xstream.useAttributeFor(org.apache.felix.sandbox.scrplugin.om.Property.class, "name");
+        this.xstream.useAttributeFor(org.apache.felix.sandbox.scrplugin.om.Property.class, "value");
+        this.xstream.omitField(org.apache.felix.sandbox.scrplugin.om.Property.class, "label");
+        this.xstream.omitField(org.apache.felix.sandbox.scrplugin.om.Property.class, "description");
+        this.xstream.omitField(org.apache.felix.sandbox.scrplugin.om.Property.class, "options");
+        this.xstream.omitField(org.apache.felix.sandbox.scrplugin.om.Property.class, "privateProperty");
+
+        this.xstream.alias("service", org.apache.felix.sandbox.scrplugin.om.Service.class);
+        this.xstream.useAttributeFor(org.apache.felix.sandbox.scrplugin.om.Service.class, "servicefactory");
+
+        this.xstream.alias("provide", org.apache.felix.sandbox.scrplugin.om.Interface.class);
+        this.xstream.useAttributeFor(org.apache.felix.sandbox.scrplugin.om.Interface.class, "interfacename");
+
+        this.xstream.alias("reference", org.apache.felix.sandbox.scrplugin.om.Reference.class);
+        this.xstream.useAttributeFor(org.apache.felix.sandbox.scrplugin.om.Reference.class, "name");
+        this.xstream.useAttributeFor(org.apache.felix.sandbox.scrplugin.om.Reference.class, "interfacename");
+        this.xstream.useAttributeFor(org.apache.felix.sandbox.scrplugin.om.Reference.class, "target");
+        this.xstream.useAttributeFor(org.apache.felix.sandbox.scrplugin.om.Reference.class, "cardinality");
+        this.xstream.useAttributeFor(org.apache.felix.sandbox.scrplugin.om.Reference.class, "policy");
+        this.xstream.useAttributeFor(org.apache.felix.sandbox.scrplugin.om.Reference.class, "bind");
+        this.xstream.useAttributeFor(org.apache.felix.sandbox.scrplugin.om.Reference.class, "unbind");
+    }
+
+    public org.apache.felix.sandbox.scrplugin.om.Components read(File file) throws IOException, MojoExecutionException {
+        Writer buffer = new StringWriter();
+        final TransformerFactory factory = TransformerFactory.newInstance();
+        Transformer transformer;
+        try {
+            IOUtils.copy(new FileReader(file), buffer);
+            String xmlDoc = buffer.toString();
+            buffer = new StringWriter();
+            int pos = xmlDoc.indexOf("?>");
+            if ( pos > 0 ) {
+                xmlDoc = xmlDoc.substring(pos+2);
+            }
+            xmlDoc = "<components>" + xmlDoc + "</components>";
+            transformer = factory.newTransformer(new StreamSource(this.getClass().getResourceAsStream("/org/apache/felix/sandbox/scrplugin/xml/read.xsl")));
+            transformer.setOutputProperty(OutputKeys.INDENT, "no");
+            transformer.transform(new StreamSource(new StringReader(xmlDoc)), new StreamResult(buffer));
+            return (org.apache.felix.sandbox.scrplugin.om.Components)this.xstream.fromXML(new StringReader(buffer.toString()));
+        } catch (TransformerException e) {
+            throw new MojoExecutionException("Unable to read xml.", e);
+        }
+    }
+
+    public void write(File file, org.apache.felix.sandbox.scrplugin.om.Components components)
+    throws IOException, MojoExecutionException {
+        Writer buffer = new StringWriter();
+        this.xstream.toXML(components, buffer);
+
+        final TransformerFactory factory = TransformerFactory.newInstance();
+        Transformer transformer;
+        try {
+            transformer = factory.newTransformer(new StreamSource(this.getClass().getResourceAsStream("/org/apache/felix/sandbox/scrplugin/xml/write.xsl")));
+            transformer.setOutputProperty(OutputKeys.INDENT, "no");
+
+            transformer.transform(new StreamSource(new StringReader(buffer.toString())), new StreamResult(new FileWriter(file)));
+        } catch (TransformerException e) {
+            throw new MojoExecutionException("Unable to write xml.", e);
+        }
+    }
+}

Propchange: felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/xml/ComponentDescriptorIO.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/xml/ComponentDescriptorIO.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision url