Updating SCRDescriptorGenerator to generate cfgdef files

Change-Id: Icb900a656b326a0c1688e7adb3a58fec9bd88081
diff --git a/tools/org.apache.felix.scr.bnd/src/main/java/org/apache/felix/scrplugin/bnd/SCRDescriptorBndPlugin.java b/tools/org.apache.felix.scr.bnd/src/main/java/org/apache/felix/scrplugin/bnd/SCRDescriptorBndPlugin.java
index f8774d4..cd31785 100644
--- a/tools/org.apache.felix.scr.bnd/src/main/java/org/apache/felix/scrplugin/bnd/SCRDescriptorBndPlugin.java
+++ b/tools/org.apache.felix.scr.bnd/src/main/java/org/apache/felix/scrplugin/bnd/SCRDescriptorBndPlugin.java
@@ -18,12 +18,31 @@
  */
 package org.apache.felix.scrplugin.bnd;
 
+import aQute.bnd.osgi.Analyzer;
+import aQute.bnd.osgi.Clazz;
+import aQute.bnd.osgi.Clazz.QUERY;
+import aQute.bnd.osgi.EmbeddedResource;
+import aQute.bnd.osgi.Jar;
+import aQute.bnd.service.AnalyzerPlugin;
+import aQute.bnd.service.Plugin;
+import aQute.service.reporter.Reporter;
+import org.apache.felix.scrplugin.Options;
+import org.apache.felix.scrplugin.Result;
+import org.apache.felix.scrplugin.SCRDescriptorGenerator;
+import org.apache.felix.scrplugin.Source;
+import org.apache.felix.scrplugin.SpecVersion;
+import org.apache.felix.scrplugin.description.ClassDescription;
+import org.apache.felix.scrplugin.description.PropertyDescription;
+import org.apache.felix.scrplugin.helper.ComponentContainer;
+
 import java.io.BufferedInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.PrintWriter;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.util.ArrayList;
@@ -35,21 +54,6 @@
 import java.util.Map;
 import java.util.Set;
 
-import org.apache.felix.scrplugin.Options;
-import org.apache.felix.scrplugin.Result;
-import org.apache.felix.scrplugin.SCRDescriptorGenerator;
-import org.apache.felix.scrplugin.Source;
-import org.apache.felix.scrplugin.SpecVersion;
-
-import aQute.bnd.osgi.Analyzer;
-import aQute.bnd.osgi.Clazz;
-import aQute.bnd.osgi.Clazz.QUERY;
-import aQute.bnd.osgi.EmbeddedResource;
-import aQute.bnd.osgi.Jar;
-import aQute.bnd.service.AnalyzerPlugin;
-import aQute.bnd.service.Plugin;
-import aQute.service.reporter.Reporter;
-
 /**
  * The <code>SCRDescriptorBndPlugin</code> class is a <code>bnd</code> analyzer
  * plugin which generates a service descriptor file based on annotations found
@@ -208,6 +212,18 @@
 					putResource(analyzer, processedScrFile);
 				}
 			}
+
+			// Embed the properties files in the target jar
+			List<ComponentContainer> components = r.getComponents();
+			if (components != null) {
+				for (ComponentContainer component : components) {
+					String catalog = writeCatalog(component);
+					if (catalog != null) {
+						log.info("Processing catalog file: " + catalog);
+						putResource(analyzer, catalog);
+					}
+				}
+			}
 		} catch (Throwable t) {
 			log.error("Got unexpected exception while analyzing",
 					t);
@@ -343,4 +359,52 @@
 		log.info("Using dependencies: " + files);
 		return files;
 	}
+
+	private String writeCatalog(ComponentContainer component) {
+
+		if (component.getProperties().size() == 0) {
+			// There are no properties, so there is no need for a catalog
+			return null;
+		}
+
+		List<String> lines = new ArrayList<String>();
+		for (PropertyDescription property : component.getProperties().values()) {
+			lines.add(processField(property));
+		}
+
+		ClassDescription javaClass = component.getClassDescription();
+
+		// Make dest directory... which should not be needed
+//        File dir = new File(dstDirectory, javaClass.getPackageName().replace('.', '/'));
+//        dir.mkdirs();
+
+		// Write cfgdef file
+		String filename = javaClass.getSource().replace(".class", ".cfgdef");
+		File cfgDef = new File(destDir + File.separator + filename);
+		try {
+			PrintWriter writer = new PrintWriter(new FileWriter(cfgDef));
+
+			writer.println("# This file is auto-generated by onos-maven-plugin");
+			for (String line : lines) {
+				writer.println(line);
+			}
+
+			writer.close();
+		} catch (IOException e) {
+			System.err.println("Unable to write catalog for " + javaClass.toString());
+			e.printStackTrace();
+		}
+		return filename;
+	}
+
+	private static final String SEP = "|";
+	private String processField(PropertyDescription property) {
+		return property.getName() +
+				SEP +
+				property.getType().name().toUpperCase() +
+				SEP +
+				property.getValue() +
+				SEP +
+				property.getLabel();
+	}
 }
diff --git a/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/Result.java b/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/Result.java
index 98911b2..9d574f4 100644
--- a/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/Result.java
+++ b/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/Result.java
@@ -18,6 +18,8 @@
  */
 package org.apache.felix.scrplugin;
 
+import org.apache.felix.scrplugin.helper.ComponentContainer;
+
 import java.util.ArrayList;
 import java.util.List;
 
@@ -35,7 +37,10 @@
 
     /** The list of processed source files. */
     private List<String> processedSourceFiles = new ArrayList<String>();
-    
+
+    /** The list of components. */
+    private List<ComponentContainer> components;
+
     /**
      * Set the list of metatype files
      */
@@ -53,12 +58,20 @@
     /**
      * Adds a source file to the list of processed source files
      * 
-	 * @param source the processed source file
+	 * @param processedSourceFile the processed source file
 	 */
 	public void addProcessedSourceFile(String processedSourceFile) {
-		
 		this.processedSourceFiles.add(processedSourceFile);
 	}
+
+    /**
+     * Sets the list of components.
+     *
+     * @param components components
+     */
+    public void setComponents(List<ComponentContainer> components) {
+        this.components = components;
+    }
     
     /**
      * Return a list of generated metatype files
@@ -85,4 +98,13 @@
 	public List<String> getProcessedSourceFiles() {
 		return processedSourceFiles;
 	}
+
+    /**
+     * Returns a list of components.
+     *
+     * @return the list of components
+     */
+    public List<ComponentContainer> getComponents() {
+        return components;
+    }
 }
diff --git a/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/SCRDescriptorGenerator.java b/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/SCRDescriptorGenerator.java
index 43345f3..66a3833 100644
--- a/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/SCRDescriptorGenerator.java
+++ b/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/SCRDescriptorGenerator.java
@@ -227,6 +227,8 @@
         result.setMetatypeFiles(MetaTypeIO.generateDescriptors(module, this.project, this.options, this.logger));
         result.setScrFiles(ComponentDescriptorIO.generateDescriptorFiles(module, this.options, logger));
 
+        result.setComponents(processedContainers);
+
         return result;
     }