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>
......@@ -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();
//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) {
log.severe("Could not create contribution identifier");
}
try {
log.info("Creating SCA contribution descriptor");
contributionFile.createNewFile();
FileOutputStream outputStream = new FileOutputStream(contributionFile);
try {
String begin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<contribution xmlns=\"http://www.osoa.org/xmlns/sca/1.0\">\n";
String end = "</contribution>";
String body = "";
for (String deployable : deployables) {
body = body + "\t<deployable composite=\"" + deployable + "\"/>\n";
log.info("Added deployable composite : " + deployable);
}
// Add imports to contribution file
if(imports!=null)
{
for(Import importObject : imports)
{
body+="\t";
body+=ContributionUtil.proceedImport(importObject);
body+="\n";
log.info("Added import "+importObject.getType()+" : "+importObject.getValue());
}
}
// Add exports to contribution file
if(exports!=null)
{
for(Export exportObject : exports)
{
body+="\t";
body+=ContributionUtil.proceedExport(exportObject);
body+="\n";
log.info("Added export "+exportObject.getType()+" : "+exportObject.getValue());
}
}
// write contribution file
outputStream.write((begin + body + end).getBytes());
outputStream.flush();
} finally {
outputStream.close();
}
} catch (IOException e) {
log.severe("Could not create contribution descriptor");
}
// Copy libraries
try {
for (File file : jars.keySet()) {
if (jars.get(file) != null) { // A new name is specified
File destFile = new File(libDir + File.separator + jars.get(file));
FileUtils.copyFile(file, destFile);
} else {
FileUtils.copyFileToDirectory(file, libDir);
}
log.info("Added library " + file.getName());
}
} catch (Exception e) {
log.log(Level.SEVERE, "Problem with the dependency management.", e);
}
// Create contribution zip
String packagename = contributionName + ".zip";
File outputZip = new File(workingDir.getAbsolutePath(), packagename);
try {
log.info("Create contibution package " + packagename);
ContributionUtil.zip(contributionOutDir, outputZip);
} catch (IOException e) {
log.log(Level.SEVERE, "Could not create contibution package " + packagename, e);
}
try {
log.info("Delete temp directory " + contributionOutDir.getName());
FileUtils.deleteDirectory(contributionOutDir);
} catch (IOException e) {
log.log(Level.WARNING, "Could not delete temp directory " + contributionOutDir.getName());
}
return outputZip;
}
private static String proceedImport(Import importObject)
{
return ContributionUtil.proceedImportExport(importObject, "import");
}
private static String proceedExport(Export exportObject)
public static File makeContribution(Map<File, String> jars, Collection<String> deployables, List<Import> imports, List<Export> exports, String contributionName, File workingDir)
{
return ContributionUtil.proceedImportExport(exportObject, "export");
}
private static String proceedImportExport(ImportExport importExport, String kind)
{
StringBuilder importExportStringBuilder=new StringBuilder();
importExportStringBuilder.append("<");
importExportStringBuilder.append(kind);
importExportStringBuilder.append(".");
importExportStringBuilder.append(importExport.getType());
importExportStringBuilder.append(" ");
importExportStringBuilder.append(importExport.getAttributeTag());
importExportStringBuilder.append("=\"");
importExportStringBuilder.append(importExport.getValue());
importExportStringBuilder.append("\"/>");
return importExportStringBuilder.toString();
}
/**
* Utility method to create contribution zip file.
*
* @param inFile The directory to zip.
* @param outFile The output file name.
* @throws IOException
*/
public static void zip(File inFile, File outFile) throws IOException {
final int buffer = 2048;
FileOutputStream dest = new FileOutputStream(outFile);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
try {
byte data[] = new byte[buffer];
Collection<String> list = listFiles(inFile, "");
for (String file : list) {
FileInputStream fis = new FileInputStream(new File(inFile, file));
BufferedInputStream origin = new BufferedInputStream(fis, buffer);
ZipEntry entry = new ZipEntry(file);
out.putNextEntry(entry);
try {
int count;
while ((count = origin.read(data, 0, buffer)) != -1) {
out.write(data, 0, count);
}
} finally {
origin.close();
fis.close();
}
}
} catch (Exception e) {
log.log(Level.SEVERE, "Problem while zipping file!", e);
} finally {
out.close();
dest.close();
}
}
/**
* List files recursively. Does not include directories.
*
* @param directory The directory to list.
* @param currentDirName The current directory name.
* @return A list of paths relative to the directory parameter.
*/
public static Collection<String> listFiles(File directory, String currentDirName)
{
// List of files / directories
Collection<String> files = new ArrayList<String>();
// Go over entries
for (File entry : directory.listFiles())
{
if (entry.isDirectory()) {
StringBuilder sb = new StringBuilder(currentDirName);
sb.append(entry.getName()).append(File.separator);
files.addAll( listFiles(entry, sb.toString()) );
} else {
files.add(currentDirName + entry.getName());
}
}
// Return collection of files
return files;
Contribution contribution=new Contribution(contributionName, new ArrayList<String>(deployables), imports, exports, jars);
File contributionZip=contribution.generate(workingDir);
return contributionZip;
}
}
......@@ -52,6 +52,8 @@ import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.ow2.frascati.mojo.importexport.Export;
import org.ow2.frascati.mojo.importexport.Import;
/**
* A maven plugin for packaging FraSCAti contributions
......@@ -257,8 +259,11 @@ public class FrascatiContributionMojo<rep> extends AbstractMojo
contributionName=this.project.getArtifactId();
}
// Make the zip
File contrib = ContributionUtil.makeContribution(jars, Arrays.asList(deployables), imports, exports, contributionName, targetDir);
// File contrib = ContributionUtil.makeContribution(jars, Arrays.asList(deployables), imports, exports, contributionName, targetDir);
Contribution contribution=new Contribution(contributionName, Arrays.asList(deployables), imports, exports, jars);
File contrib =contribution.generate(targetDir);
String classifierName;
if(this.classifier!=null && !"".equals(this.classifier))
{
......
/**
* OW2 FraSCAti Contribution Maven Plugin
*
* Copyright (c) 2011-2013 INRIA, University of Lille 1
* 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
......@@ -26,7 +26,7 @@
*
*/
package org.ow2.frascati.mojo;
package org.ow2.frascati.mojo.importexport;
/**
*
......
/**
* 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.util;
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.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.apache.commons.io.IOUtils;
import org.ow2.frascati.mojo.Contribution;
/**
*
*/
public class ZipUtil
{
/**
* Utility method to create contribution zip file.
*
* @param inFile The directory to zip.
* @param outFile The output file name.
* @throws IOException
*/
public static void zip(File inFile, File outFile) throws IOException {
final int buffer = 2048;
FileOutputStream dest = new FileOutputStream(outFile);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
try {
byte data[] = new byte[buffer];
Collection<String> list = listFiles(inFile, "");
for (String file : list) {
FileInputStream fis = new FileInputStream(new File(inFile, file));
BufferedInputStream origin = new BufferedInputStream(fis, buffer);
ZipEntry entry = new ZipEntry(file);
out.putNextEntry(entry);
try {
int count;
while ((count = origin.read(data, 0, buffer)) != -1) {
out.write(data, 0, count);
}
} finally {
origin.close();
fis.close();
}
}
} catch (Exception e) {
} finally {
out.close();
dest.close();
}
}
public static void unzipFile(ZipFile zipFile, File dir)
{
if(!dir.exists())
{
dir.mkdirs();
}
Enumeration files = zipFile.entries();
File f = null;
FileOutputStream fos = null;
while (files.hasMoreElements())
{
try
{
ZipEntry entry = (ZipEntry) files.nextElement();
InputStream eis = zipFile.getInputStream(entry);
byte[] buffer = new byte[1024];
int bytesRead = 0;
f=new File(dir,entry.getName());
if (entry.isDirectory())
{
f.mkdirs();
continue;
} else
{
f.getParentFile().mkdirs();
f.createNewFile();
}
fos = new FileOutputStream(f);
IOUtils.copy(eis, fos);
} catch (IOException e)
{
e.printStackTrace();
continue;
} finally
{
if (fos != null)
{
try
{
fos.close();
} catch (IOException e)
{
// ignore
}
}
}
}
}
public static File getCompositeFile(ZipFile zipFile, String compositeName) throws IOException
{
Enumeration<? extends ZipEntry> entries = zipFile.entries();
File compositeFile;
ZipEntry entry;
while (entries.hasMoreElements())
{
entry=entries.nextElement();
compositeFile=getCompositeFile(zipFile, entry, compositeName);
if(compositeFile!=null)
{
return compositeFile;
}
}
return null;
}
public static File getCompositeFile(ZipFile zipFile, ZipEntry entry, String compositeName) throws IOException
{
String compositeRegex="(.*)"+compositeName+".composite";
ZipEntry jarEntry;
ZipInputStream jarStream;
String entryName,jarEntryName;
entryName=entry.getName();
if(entryName.matches(Contribution.LIB_REGEX))
{
jarStream=new ZipInputStream(zipFile.getInputStream(entry));
jarEntry=jarStream.getNextEntry();
while(jarEntry!=null)
{
jarEntryName=jarEntry.getName();
if(jarEntryName.matches(compositeRegex))
{
jarStream.close();
File jarFile=File.createTempFile("frascati","tmpComposite.jar");
InputStream in=zipFile.getInputStream(entry);
FileOutputStream out=new FileOutputStream(jarFile);
IOUtils.copy(in, out);
in.close();
out.close();
return jarFile;
}
jarEntry=jarStream.getNextEntry();
}
jarStream.close();
}
return null;
}
/**
* List files recursively. Does not include directories.
*
* @param directory The directory to list.
* @param currentDirName The current directory name.
* @return A list of paths relative to the directory parameter.
*/
public static Collection<String> listFiles(File directory, String currentDirName)
{
// List of files / directories
Collection<String> files = new ArrayList<String>();
// Go over entries
for (File entry : directory.listFiles())
{
if (entry.isDirectory()) {
StringBuilder sb = new StringBuilder(currentDirName);
sb.append(entry.getName()).append(File.separator);
files.addAll( listFiles(entry, sb.toString()) );
} else {
files.add(currentDirName + entry.getName());
}
}
// Return collection of files
return files;
}
}
/**
* 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.test;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipException;
import org.jdom.JDOMException;
import org.junit.BeforeClass;
import org.junit.Test;
import org.ow2.frascati.mojo.Contribution;
import org.ow2.frascati.mojo.importexport.Export;
import org.ow2.frascati.mojo.importexport.Import;
/**
*
*/
public class FrascatiContributionTest
{
private static File workingDir;
@BeforeClass
public static void init()
{
workingDir=new File("target/test-classes");
}
@Test
public void test()
{
List<String> deployables=new ArrayList<String>();
deployables.add("composite1.composite");
deployables.add("composite2.composite");
List<Import> imports=new ArrayList<Import>();
Import imp=new Import();
imp.setType("java");
imp.setValue("package.to.import");
imports.add(imp);
imp=new Import();
imp.setType("resource");
imp.setValue("path/to/resource/to/import");
imports.add(imp);
List<Export> exports=new ArrayList<Export>();
Export exp=new Export();
exp.setType("java");
exp.setValue("package.to.export");
exports.add(exp);
exp=new Export();
exp.setType("resource");
exp.setValue("path/to/resource/to/export");
exports.add(exp);
Contribution contribution=new Contribution("contribution-test",deployables,imports,exports,null);
contribution.generate(workingDir);
}
@Test
public void create() throws ZipException, IOException, JDOMException
{
File contributionFile=new File("src/test/resources/helloworld-binding-sca.zip");
Contribution contribution=new Contribution(contributionFile);
contribution.generate(workingDir);
contribution.setContributionName("contribution-test");
contribution.setDeployable("helloworld-binding-sca", "newName",workingDir);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or