From 3d51593d8a28049a06686437bd14409bec710f27 Mon Sep 17 00:00:00 2001
From: Manlio Bacco <manlio.bacco@isti.cnr.it>
Date: Mon, 18 Mar 2013 17:14:46 +0000
Subject: [PATCH] fixed generation of Karaf features; generation of .kar file
 forced

---
 .../META-INF/MANIFEST.MF                      |  8 +-
 .../tools/packaging/tool/gui/PagePartDU.java  | 13 +--
 .../tool/util/KarafFeaturesGenerator.java     | 79 ++++++++++++++++---
 .../tools/packaging/tool/zip/CreateJar.java   |  2 +-
 4 files changed, 82 insertions(+), 20 deletions(-)

diff --git a/mpa/org.universaal.tools.packaging.tool/META-INF/MANIFEST.MF b/mpa/org.universaal.tools.packaging.tool/META-INF/MANIFEST.MF
index b4f320e90..9b18c81d4 100644
--- a/mpa/org.universaal.tools.packaging.tool/META-INF/MANIFEST.MF
+++ b/mpa/org.universaal.tools.packaging.tool/META-INF/MANIFEST.MF
@@ -2,13 +2,13 @@ Manifest-Version: 1.0
 Bundle-ManifestVersion: 2
 Bundle-Name: AAL Studio MultiPart Application Packager
 Bundle-SymbolicName: org.universaal.tools.packaging.tool; singleton:=true
-Bundle-Version: 0.3.6
+Bundle-Version: 0.4.0
 Bundle-Activator: org.universaal.tools.packaging.tool.Activator
 Require-Bundle: org.eclipse.ui,
  org.eclipse.core.runtime,
- javax.xml;bundle-version="1.3.4",
- javax.xml.bind;bundle-version="2.2.0",
- org.eclipse.m2e.core;bundle-version="1.2.0",
+ javax.xml,
+ javax.xml.bind,
+ org.eclipse.m2e.core,
  org.eclipse.m2e.maven.runtime
 Bundle-ActivationPolicy: lazy
 Bundle-RequiredExecutionEnvironment: J2SE-1.5
diff --git a/mpa/org.universaal.tools.packaging.tool/src/org/universaal/tools/packaging/tool/gui/PagePartDU.java b/mpa/org.universaal.tools.packaging.tool/src/org/universaal/tools/packaging/tool/gui/PagePartDU.java
index aedb23ae7..0720d4229 100644
--- a/mpa/org.universaal.tools.packaging.tool/src/org/universaal/tools/packaging/tool/gui/PagePartDU.java
+++ b/mpa/org.universaal.tools.packaging.tool/src/org/universaal/tools/packaging/tool/gui/PagePartDU.java
@@ -248,9 +248,9 @@ public class PagePartDU extends PageImpl {
 			andURI.setText("");	
 		andURI.setLayoutData(gd);
 
-		Label karFile = new Label(container, SWT.NULL);
-		karFile.setText("Select this checkbox to create KAR file (valid if using Karaf container)");
-		ckbKar = new Button(container, SWT.CHECK);
+		//		Label karFile = new Label(container, SWT.NULL);
+		//		karFile.setText("Select this checkbox to create KAR file (valid if using Karaf container)");
+		//		ckbKar = new Button(container, SWT.CHECK);
 
 		Label empty1 = new Label(container, SWT.NULL);
 		empty1.setText("");
@@ -286,9 +286,12 @@ public class PagePartDU extends PageImpl {
 			ContainerUnit cu = null;
 			if(cu1.getText().equals(Container.KARAF.toString())){
 
-				//waiting.setVisible(true);
 				KarafFeaturesGenerator krf = new KarafFeaturesGenerator();
-				cu = new ContainerUnit(Embedding.valueOf(emb1.getText()), krf.generate(this.part, ckbKar.getSelection()));
+				String karaf = krf.generate(this.part, /*ckbKar.getSelection()*/true, partNumber);
+				if(karaf != null && !karaf.isEmpty())
+					cu = new ContainerUnit(Embedding.valueOf(emb1.getText()), karaf);
+				else
+					cu = new ContainerUnit(Embedding.valueOf(emb1.getText()), "");				
 			}
 			else if(cu1.getText().equals(Container.ANDROID.toString())){
 				cu = new ContainerUnit(new Android(andN.getText(), andD.getText(), URI.create(removeBlanks(andURI.getText()))));
diff --git a/mpa/org.universaal.tools.packaging.tool/src/org/universaal/tools/packaging/tool/util/KarafFeaturesGenerator.java b/mpa/org.universaal.tools.packaging.tool/src/org/universaal/tools/packaging/tool/util/KarafFeaturesGenerator.java
index 2759a8aeb..3418c4377 100644
--- a/mpa/org.universaal.tools.packaging.tool/src/org/universaal/tools/packaging/tool/util/KarafFeaturesGenerator.java
+++ b/mpa/org.universaal.tools.packaging.tool/src/org/universaal/tools/packaging/tool/util/KarafFeaturesGenerator.java
@@ -8,7 +8,10 @@ import java.io.FileReader;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 import java.util.Properties;
 
 import javax.xml.parsers.DocumentBuilder;
@@ -34,6 +37,7 @@ import org.universaal.tools.packaging.api.Page;
 import org.universaal.tools.packaging.tool.gui.GUI;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
+import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
 public class KarafFeaturesGenerator {
@@ -59,20 +63,25 @@ public class KarafFeaturesGenerator {
 
 	private final String GROUP_ID = "org.apache.karaf.tooling";
 	private final String ARTIFACT_ID = "features-maven-plugin";
-	private final String VERSION = "2.3.1";
+	private final String VERSION = "2.3.0";//"2.3.1";
 
 	private final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
 
-	//private final String ENCODING = "UTF-8";
-
-	public String generate(IProject part, boolean createKar){		
+	public String generate(IProject part, boolean createKar, int partNumber){		
 
 		if(verifyPreConditions(part.getName()))
 			if(generateKarafFeatures(part.getName())){	
 				if(createKar)
 					generateKarFile(part);
-				return returnKrfFeat(part);
+				return returnKrfFeat(part, partNumber);
 			}
+
+		String ret = "";
+		if(execution_result != null && execution_result.getExceptions() != null)
+			for(int i = 0; i < execution_result.getExceptions().size(); i++)
+				ret = ret.concat(execution_result.getExceptions().get(i).getMessage()+"\n");
+		System.out.println("[ERROR] The generation of Karaf features has failed: "+ret);
+
 		return "";
 	}
 
@@ -95,8 +104,12 @@ public class KarafFeaturesGenerator {
 				NodeList plugins_children = plug_ins.item(i).getChildNodes();
 				for(int j = 0; j < plugins_children.getLength(); j++){
 					if(plugins_children.item(j).getNodeName().equals("artifactId"))
-						if(plugins_children.item(j).getTextContent().equalsIgnoreCase(ARTIFACT_ID))
-							return true;
+						if(plugins_children.item(j).getTextContent().equalsIgnoreCase(ARTIFACT_ID)){
+							if(plugins_children.item(j).getNodeName().equals("version"))
+								if(plugins_children.item(j).getTextContent().equalsIgnoreCase(VERSION))
+									return true;								
+						}
+					//return true;
 				}
 			}
 			// add features-maven-plugin declaration
@@ -150,6 +163,7 @@ public class KarafFeaturesGenerator {
 		return false;
 	}
 
+	private MavenExecutionResult execution_result;
 	private boolean generateKarafFeatures(String projectName){
 
 		try{
@@ -173,7 +187,7 @@ public class KarafFeaturesGenerator {
 
 				request.setGoals(goals);
 				request.setUserProperties(props);
-				MavenExecutionResult execution_result = maven.execute(request, null);
+				execution_result = maven.execute(request, null);
 				if(execution_result.getExceptions() == null || execution_result.getExceptions().isEmpty())
 					return true;
 			}
@@ -241,7 +255,7 @@ public class KarafFeaturesGenerator {
 		}
 	}
 
-	private String returnKrfFeat(IProject part){
+	private String returnKrfFeat(IProject part, int partNumber){
 
 		String xml = "";
 		try {
@@ -249,6 +263,22 @@ public class KarafFeaturesGenerator {
 			String path = ResourcesPlugin.getWorkspace().getRoot().getLocation().makeAbsolute()+"/"+part.getDescription().getName();
 			File features = new File(path+"/target/classes/feature.xml");
 
+			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
+			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
+			Document parsedPom = docBuilder.parse(features);
+			parsedPom.getDocumentElement().normalize();
+
+			Map<String, Element> dps = new HashMap<String, Element>();
+			NodeList fts = parsedPom.getElementsByTagName("feature");
+			for(int i = 0; i < fts.getLength(); i++){
+				if(fts.item(i).getNodeType() == Node.ELEMENT_NODE){
+					Element item = (Element) fts.item(i);
+					if(item.getAttribute("name") != null && !item.getAttribute("name").isEmpty())
+						dps.put(item.getTextContent(), item);
+				}
+			}
+
+			// feature.xml modifications
 			BufferedReader reader = new BufferedReader(new FileReader(features));
 			String line = "", oldtext = "";
 			while((line = reader.readLine()) != null)
@@ -256,11 +286,40 @@ public class KarafFeaturesGenerator {
 
 			reader.close(); 
 
+			// add feature of current part: 
+			/*
+			 * <feature name="Help-when-outdoor-servlet"
+								description="Servlet part of HWO Service"
+								version="0.1" resolver="(obr)">
+								<feature>universAAL2.0</feature>
+								<bundle start-level="85" start="false">
+									file://../bin/part1/hwo.servlet_1.2.1.SNAPSHOT.jar</bundle>
+							</feature>
+			 */
+			POMParser p = new POMParser(new File(part.getFile("pom.xml").getLocation()+""));
+			String fileName = p.getArtifactID()+"-"+p.getVersion()+".jar";
+			partNumber++;
+			String thisPart = "<feature name='"+p.getName()+"' description='"+p.getDescription()+"' version='"+p.getVersion()+"' resolver=''>";
+
+			Iterator<Element> it = dps.values().iterator();
+			while(it.hasNext()){
+				Element current = it.next();
+				String f = "<feature version='"+current.getAttribute("version")+"'>"+current.getAttribute("name")+"</feature>";
+				thisPart = thisPart.concat(f);
+			}
+
+			thisPart = thisPart.concat(/*"<feature>"+p.getDescription()+"</feature>" +*/
+					"<bundle start-level='0' start='false'>"+"file://../bin/part"+partNumber+"/"+fileName+"</bundle>" +
+					"</feature>" +
+					"</features>");
+			oldtext = oldtext.replace("</features>", thisPart);
+
 			xml = oldtext.substring(XML_HEADER.length(), oldtext.length()); // remove XML header
 			xml = xml.replaceAll("<", "<"+Page.KARAF_NAMESPACE+":"); // add KARAF namespace
 			xml = xml.replaceAll("<"+Page.KARAF_NAMESPACE+":/", "</"+Page.KARAF_NAMESPACE+":"); // correct end tags
 
-		} catch (Exception e) {
+		} 
+		catch (Exception e) {
 			e.printStackTrace();
 		}
 
diff --git a/mpa/org.universaal.tools.packaging.tool/src/org/universaal/tools/packaging/tool/zip/CreateJar.java b/mpa/org.universaal.tools.packaging.tool/src/org/universaal/tools/packaging/tool/zip/CreateJar.java
index 5555871d3..48db39d9c 100644
--- a/mpa/org.universaal.tools.packaging.tool/src/org/universaal/tools/packaging/tool/zip/CreateJar.java
+++ b/mpa/org.universaal.tools.packaging.tool/src/org/universaal/tools/packaging/tool/zip/CreateJar.java
@@ -27,7 +27,7 @@ public class CreateJar {
 		try{			
 			String path = ResourcesPlugin.getWorkspace().getRoot().getLocation().makeAbsolute()+"/"+part.getDescription().getName();
 			POMParser p = new POMParser(new File(part.getFile("pom.xml").getLocation()+""));			
-			String fileName = p.getArtifactID()+p.getVersion()+".jar";
+			String fileName = p.getArtifactID()+"-"+p.getVersion()+".jar";
 
 			Manifest manifest = new Manifest();
 			manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");			
-- 
GitLab