Commit 41c17cbe authored by Gwenael Cattez's avatar Gwenael Cattez

Change the way contributions are generated (Contribution Object instead of...

Change the way contributions are generated (Contribution Object instead of ContributionUtil static access)
Create Contribution Object from existing contribution File
Beta : Set name of a deployable composite inside of a contribution
parent 7aa998ad
......@@ -56,6 +56,14 @@
<version>1.3.2</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.1</version>
<type>jar</type>
</dependency>
</dependencies>
</project>
/**
* OW2 FraSCAti Contribution Maven Plugin
* Copyright (C) 2013 Inria, University of Lille 1
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
* Contact: frascati@ow2.org
*
* Author: Gwenael Cattez
*
* Contributor(s):
*
*/
package org.ow2.frascati.mojo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.ow2.frascati.mojo.importexport.Export;
import org.ow2.frascati.mojo.importexport.Import;
import org.ow2.frascati.mojo.util.ZipUtil;
/**
*
*/
public class Contribution
{
private final Logger logger = Logger.getLogger(Contribution.class.getName());
public final static String CONTRIB_REGEX = "(META-INF)(.)(.+)(\\.contrib)";
public final static String SCA_CONTRIB_REGEX="(META-INF)(.)sca-contribution.xml";
public final static String LIB_REGEX="(lib)(.*).jar";
private String contributionName;
private List<String> deployables;
private List<Import> imports;
private List<Export> exports;
private Map<File, String> libJars;
private ZipFile zipFile;
private List<ZipEntry> libZipEntries;
private Contribution()
{
this.deployables=new ArrayList<String>();
this.imports=new ArrayList<Import>();
this.exports=new ArrayList<Export>();
this.libJars=new HashMap<File, String>();
this.libZipEntries=new ArrayList<ZipEntry>();
}
public Contribution(String contributionName, List<String> deployables, List<Import> imports, List<Export> exports, Map<File, String> jars)
{
this();
this.contributionName=contributionName;
if(deployables!=null)
{
this.deployables=deployables;
}
if(imports!=null)
{
this.imports=imports;
}
if(exports!=null)
{
this.exports=exports;
}
if (jars!=null)
{
this.libJars=jars;
}
}
public Contribution(File contributionFile) throws ZipException, IOException, JDOMException
{
this();
zipFile = new ZipFile(contributionFile);
this.libZipEntries=new ArrayList<ZipEntry>();
Enumeration<? extends ZipEntry> entries = zipFile.entries();
String entryName;
while (entries.hasMoreElements())
{
ZipEntry entry = entries.nextElement();
entryName=entry.getName();
logger.info("entry : "+entryName);
if(entryName.matches(CONTRIB_REGEX))
{
this.contributionName=entryName.replaceAll(CONTRIB_REGEX, "$3");
logger.info("set contribution name : "+this.contributionName);
}
else if(entryName.matches(SCA_CONTRIB_REGEX))
{
logger.info("found "+entryName);
this.proceedSCAontributionFile(zipFile.getInputStream(entry));
}
else if(entryName.matches(LIB_REGEX))
{
this.libZipEntries.add(entry);
}
}
}
public String getContributionName()
{
return contributionName;
}
public void setContributionName(String contributionName)
{
this.contributionName = contributionName;
}
public void setDeployable(String compositeName, String newCompositeName, File workingDir) throws IOException, JDOMException
{
/*Get the jar that contains file compositeName*/
Enumeration<? extends ZipEntry> entries = zipFile.entries();
File jarFile = null;
ZipEntry jarEntry = null;
while (entries.hasMoreElements())
{
jarEntry=entries.nextElement();
jarFile=ZipUtil.getCompositeFile(zipFile, jarEntry, compositeName);
if(jarFile!=null)
{
break;
}
}
/*if no jar is found return*/
if(jarFile==null)
{
logger.warning("No jarFile found");
return;
}
/*unzip the composite jar*/
ZipFile jarZipFile=new ZipFile(jarFile);
File unzippedJar=new File(workingDir,"unzipped");
ZipUtil.unzipFile(jarZipFile, unzippedJar);
/*the current composite file*/
File compositeFile=new File(unzippedJar, compositeName+".composite");
/*we read the current compopsite*/
InputStream compositeInputStream=new FileInputStream(compositeFile);
SAXBuilder saxBuilder=new SAXBuilder();
Document compositeDocument=saxBuilder.build(compositeInputStream);
compositeInputStream.close();
/*set composite name*/
Element compositeElement=compositeDocument.getRootElement();
compositeElement.setAttribute("name", newCompositeName);
OutputStream compositeOutputStream=new FileOutputStream(compositeFile);
XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
sortie.output(compositeDocument, compositeOutputStream);
compositeOutputStream.close();
File reconfiguredCompositeFile=new File(workingDir, compositeName+"-reconfigured.jar");
ZipUtil.zip(unzippedJar, reconfiguredCompositeFile);
FileUtils.deleteDirectory(unzippedJar);
/*unzip the current contribution*/
File unzippedContribution=new File(workingDir,"unzippedContribution");
ZipUtil.unzipFile(this.zipFile, unzippedContribution);
File unzippedContributionComposite=new File(unzippedContribution,jarEntry.getName());
OutputStream unzippedContributionOutputStream=new FileOutputStream(unzippedContributionComposite);
InputStream reconfiguredCompositeInputStream=new FileInputStream(reconfiguredCompositeFile);
IOUtils.copy(reconfiguredCompositeInputStream, unzippedContributionOutputStream);
unzippedContributionOutputStream.close();
reconfiguredCompositeInputStream.close();
reconfiguredCompositeFile.delete();
File contributionFile=new File(workingDir,this.contributionName+".zip");
ZipUtil.zip(unzippedContribution, contributionFile);
this.zipFile=new ZipFile(contributionFile);
FileUtils.deleteDirectory(unzippedContribution);
}
public void addImport(String type, String value)
{
Import imp=new Import();
imp.setType(type);
imp.setValue(value);
this.addImport(imp);
}
public void addImport(Import imp)
{
this.imports.add(imp);
}
public void addExport(String type, String value)
{
Export exp=new Export();
exp.setType(type);
exp.setValue(value);
this.addExport(exp);
}
public void addExport(Export exp)
{
this.exports.add(exp);
}
public void addLib(File libFile)
{
this.libJars.put(libFile,"");
}
@SuppressWarnings("unchecked")
private Element proceedSCAontributionFile(InputStream contributionInputStream) throws JDOMException, IOException
{
logger.info("readSCAContributionFile ");
SAXBuilder saxBuilder=new SAXBuilder();
Document document=saxBuilder.build(contributionInputStream);
Element contribution=document.getRootElement();
List<Element> deployablesElement=contribution.getChildren("deployable");
String compositeName;
for(Object deployableElement : deployablesElement)
{
compositeName=((Element)deployableElement).getAttributeValue("composite");
logger.info("deployable : "+compositeName);
this.deployables.add(compositeName);
}
List<Element> javaImportsElement=contribution.getChildren("import.java");
String packageName;
Import imp;
for(Element javaImport : javaImportsElement)
{
packageName=javaImport.getAttributeValue("package");
logger.info("import package : "+packageName);
imp=new Import();
imp.setType("java");
imp.setValue(packageName);
this.imports.add(imp);
}
String uri;
List<Element> resourceImportsElement=contribution.getChildren("import.resource");
for(Element resourceImport : resourceImportsElement)
{
uri=resourceImport.getAttributeValue("uri");
logger.info("import resource : "+uri);
imp=new Import();
imp.setType("resource");
imp.setValue(uri);
this.imports.add(imp);
}
List<Element> javaExportsElement=contribution.getChildren("export.java");
Export exp;
for(Element javaImport : javaExportsElement)
{
packageName=javaImport.getAttributeValue("package");
logger.info("export package : "+packageName);
exp=new Export();
exp.setType("java");
exp.setValue(packageName);
this.exports.add(exp);
}
List<Element> resourceExportsElement=contribution.getChildren("export.resource");
for(Element resourceImport : resourceExportsElement)
{
uri=resourceImport.getAttributeValue("uri");
logger.info("export resource : "+uri);
exp=new Export();
exp.setType("resource");
exp.setValue(uri);
this.exports.add(exp);
}
return contribution;
}
public File generate(File workingDir)
{
// Create working directory if needed
if (!workingDir.exists()) {
workingDir.mkdirs();
}
// Delete previous directory
File contributionOutDir = new File(workingDir.getAbsolutePath()+ File.separator + contributionName);
if(contributionOutDir.exists())
{
try
{
FileUtils.deleteDirectory(contributionOutDir);
} catch (IOException e)
{
logger.warning("Can not delete contribution directory : "+contributionOutDir.getPath());
}
}
File metaDir = new File(contributionOutDir + File.separator + "META-INF");
metaDir.mkdirs();
//Create a void file having the name (identifier) of the contribution
File contributionIdentifier = new File(metaDir, contributionName+ ".contrib");
// Create contribution descriptor
File contributionFile = new File(metaDir, "sca-contribution.xml");
try
{
contributionIdentifier.createNewFile();
} catch (IOException e1)
{
logger.severe("Could not create contribution identifier");
}
try
{
logger.info("Creating SCA contribution descriptor");
contributionFile.createNewFile();
FileOutputStream outputStream = new FileOutputStream(contributionFile);
Element contribution = new Element("contribution");
Namespace defaultNamespace = Namespace.getNamespace("http://www.osoa.org/xmlns/sca/1.0");
contribution.setNamespace(defaultNamespace);
Element element;
Attribute attribute;
for (String deployable : this.deployables)
{
element=new Element("deployable");
attribute=new Attribute("composite", deployable);
element.setAttribute(attribute);
contribution.addContent(element);
}
String importName;
for (Import importObject : imports)
{
importName = "import." + importObject.getType();
element = new Element(importName);
attribute = new Attribute(importObject.getAttributeTag(), importObject.getValue());
element.setAttribute(attribute);
contribution.addContent(element);
}
String exportName;
for (Export exportObject : exports)
{
exportName = "export." + exportObject.getType();
element = new Element(exportName);
attribute = new Attribute(exportObject.getAttributeTag(), exportObject.getValue());
element.setAttribute(attribute);
contribution.addContent(element);
}
Document document = new Document(contribution);
XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
sortie.output(document, outputStream);
outputStream.close();
}
catch (IOException e) {
logger.severe("Could not create contribution descriptor");
}
File libDir = new File(contributionOutDir + File.separator + "lib");
libDir.mkdir();
if(this.libZipEntries!=null && !this.libZipEntries.isEmpty())
{
copyLibFromZip(libDir);
}
else
{
copyLibFromMap(libDir);
}
String packagename = contributionName + ".zip";
File outputZip=null;
try {
outputZip = new File(workingDir.getAbsolutePath(), packagename);
logger.info("Create contibution package " + packagename);
ZipUtil.zip(contributionOutDir, outputZip);
} catch (IOException e) {
logger.log(Level.SEVERE, "Could not create contibution package " + packagename, e);
}
try {
logger.info("Delete temp directory " + contributionOutDir.getName());
FileUtils.deleteDirectory(contributionOutDir);
this.zipFile=new ZipFile(outputZip);
} catch (IOException e) {
logger.log(Level.WARNING, "Could not delete temp directory " + contributionOutDir.getName());
}
return outputZip;
}
private void copyLibFromMap(File libDir)
{
// Copy libraries
try
{
for (File file : libJars.keySet())
{
if (libJars.get(file) != null)
{ // A new name is specified
File destFile = new File(libDir + File.separator + libJars.get(file));
FileUtils.copyFile(file, destFile);
} else
{
FileUtils.copyFileToDirectory(file, libDir);
}
logger.info("Added library " + file.getName());
}
} catch (Exception e)
{
logger.log(Level.SEVERE, "Problem with the dependency management.", e);
}
}
private void copyLibFromZip(File libDir)
{
try
{
File entryDestination;
for(ZipEntry libZipEntry : this.libZipEntries)
{
String entryName=libZipEntry.getName();
String libName=entryName.replaceAll(LIB_REGEX, "$2");
entryDestination = new File(libDir, libName);
if(!entryDestination.exists())
{
entryDestination.getParentFile().mkdirs();
InputStream in = zipFile.getInputStream(libZipEntry);
OutputStream out = new FileOutputStream(entryDestination);
IOUtils.copy(in, out);
in.close();
out.close();
}
}
} catch (Exception e)
{
logger.log(Level.SEVERE, "Problem with the dependency management.", e);
}
}
}
......@@ -27,30 +27,20 @@
*/
package org.ow2.frascati.mojo;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.commons.io.FileUtils;
import org.ow2.frascati.mojo.importexport.Export;
import org.ow2.frascati.mojo.importexport.Import;
/**
* Utility class used to generata SCA contributions (zip files).
*/
public class ContributionUtil {
private static Logger log =
Logger.getLogger(ContributionUtil.class.getName());
/**
*
......@@ -114,212 +104,11 @@ public class ContributionUtil {
* @param workingDir The directory that will be used to generate the zip file.
* @return the generated contribution zip file.
*/
public static File makeContribution(Map<File, String> jars, Collection<String> deployables, List<Import> imports, List<Export> exports, String contributionName, File workingDir) {
// Create working directory if needed
if (!workingDir.exists()) {
workingDir.mkdirs();
}
// Create directories
File contributionOutDir = new File(workingDir.getAbsolutePath()
+ File.separator + contributionName);
File metaDir = new File(contributionOutDir + File.separator + "META-INF");
File libDir = new File(contributionOutDir + File.separator + "lib");
if (!metaDir.exists())
metaDir.mkdirs();
if (!libDir.exists())
libDir.mkdirs();