Commit 29e2cda4 authored by Gwenael Cattez's avatar Gwenael Cattez

Clean up code, comments, tests and doc

parent e68e484c
......@@ -44,6 +44,10 @@
<url>http://frascati.ow2.org</url>
<properties>
<java.util.logging.config.file>./logging.properties</java.util.logging.config.file>
</properties>
<!-- ============ -->
<!-- Dependencies -->
<!-- ============ -->
......@@ -63,7 +67,52 @@
<type>jar</type>
</dependency>
<!--Used to test deployment of generated contribution-->
<dependency>
<groupId>org.ow2.frascati</groupId>
<artifactId>frascati-runtime-factory</artifactId>
<version>${project.version}</version>
<scope>test</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.ow2.frascati</groupId>
<artifactId>frascati-introspection-impl</artifactId>
<version>${project.version}</version>
<scope>test</scope>
<type>jar</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy</id>
<phase>process-test-resources</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>target/test-classes</outputDirectory>
<artifactItems>
<artifactItem>
<groupId>org.ow2.frascati.examples</groupId>
<artifactId>helloworld-annotations</artifactId>
<version>1.5-SNAPSHOT</version>
<type>jar</type>
<destFileName>helloworld-annotated.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
......@@ -126,7 +126,7 @@
<title>Parameters</title>
<para>The contribution plugin can be configured with the following
parameters : </para>
parameters :</para>
<itemizedlist>
<listitem>
......@@ -176,7 +176,7 @@
<section>
<title>Utility Class</title>
<para>A utility class: org.ow2.frascati.mojo.ContributionUtil allows to
<para>A utility class: org.ow2.frascati.mojo.Contribution allows to
create contribution files via a simple call. An usage example is given
below.</para>
......@@ -184,13 +184,14 @@
import org.ow2.frascati.mojo.Import;
import org.ow2.frascati.mojo.Export;
Collection&lt;File&gt; jars = new ArrayList&lt;File&gt;();
Collection&lt;String&gt; deployables = new ArrayList&lt;String&gt;();
Collection&lt;Import&gt; imports = new ArrayList&lt;Import&gt;();
Collection&lt;Export&gt; exports = new ArrayList&lt;Export&gt;();
String contributionName="myContribution";
List&lt;File&gt; jars = new ArrayList&lt;File&gt;();
List&lt;String&gt; deployables = new ArrayList&lt;String&gt;();
List&lt;Import&gt; imports = new ArrayList&lt;Import&gt;();
List&lt;Export&gt; exports = new ArrayList&lt;Export&gt;();
jars.add(new File("dependency1.jar"));
jars.add(new File("dependency2.jar"));
jars.add(new File("myComposite.jar"));
deployables.add("composite1.composite");
deployables.add("composite2.composite");
......@@ -199,9 +200,12 @@ imports.add(new Import("resource","path/to/resource/to/import"));
exports.add(new Export("java","package.to.export"));
exports.add(new Export("resource","path/to/resource/to/export"));
String contributionName="myContribution";
Contribution contribution=new Contribution(contributionName,deployables,imports,exports,jars);
contribution.addDeployable("composite3");
contribution.addLib(new File("dependency2.jar"));
File workingDir = new File("./target");
File contribFile = ContributionUtil.makeContribution(jars, deployables, imports, exports,contributionName, workingDir);</programlisting>
File contribFile = contribution.generate(workingDir);</programlisting>
</section>
</section>
</section>
/**
* OW2 FraSCAti Contribution Maven Plugin
*
* Copyright (c) 2011-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(s): Damien Fournier, Christophe Demarey
*
* Contributor(s): Philippe Merle, Rémi Mélisson, Antonio Souza Neto
*
*/
package org.ow2.frascati.mojo;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 {
/**
*
*
* @param jars The list of input Files (jar files) to add to the contribution.
* @param deployables deployables A list of composite files to deploy from the contribution.
* @param contributionName The name of the contribution file without the extension.
* @param workingDir The directory that will be used to generate the zip file.
* @return the generated contribution zip file.
*/
public static File makeContribution(Collection<File> jars, Collection<String> deployables,String contributionName, File workingDir)
{
Map<File, String> map = new HashMap<File, String>();
for (File file: jars)
map.put(file, null);
return makeContribution(map, deployables,null, null, contributionName, workingDir);
}
/**
* Build an SCA contribution from a list of jars and deployable composites.
*
* @param jars jars A map with input File as key and target path as value. If value is null, the same name will be used.
* @param deployables deployables A list of composite files to deploy from the contribution.
* @param contributionName The name of the contribution file without the extension.
* @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,String contributionName, File workingDir)
{
return makeContribution(jars, deployables,null, null, contributionName, workingDir);
}
/**
* Build an SCA contribution from a list of jars and deployable composites.
*
* @param jars The list of input Files (jar files) to add to the contribution.
* @param deployables A list of composite files to deploy from the contribution.
* @param exports A list of Export object that define a SCA Export
* @param imports A list of Import object that define a SCA import
* @param contributionName The name of the contribution file without the extension.
* @param workingDir The directory that will be used to generate the zip file.
* @return the generated contribution zip file.
*/
public static File makeContribution(Collection<File> jars, Collection<String> deployables,List<Import> imports, List<Export> exports, String contributionName, File workingDir) {
Map<File, String> map = new HashMap<File, String>();
for (File file: jars)
map.put(file, null);
return makeContribution(map, deployables,imports, exports, contributionName, workingDir);
}
/**
* Build an SCA contribution from a list of jars and deployable composites.
*
* @param jars A map with input File as key and target path as value. If value is null, the same name will be used.
* @param deployables A list of composite files to deploy from the contribution.
* @param exports A list of Export object that define a SCA Export
* @param imports A list of Import object that define a SCA import
* @param contributionName The name of the contribution file without the extension.
* @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)
{
Contribution contribution=new Contribution(contributionName, new ArrayList<String>(deployables), imports, exports, jars);
File contributionZip=contribution.generate(workingDir);
return contributionZip;
}
}
......@@ -52,6 +52,7 @@ 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.contribution.Contribution;
import org.ow2.frascati.mojo.importexport.Export;
import org.ow2.frascati.mojo.importexport.Import;
......@@ -147,13 +148,6 @@ public class FrascatiContributionMojo<rep> extends AbstractMojo
*/
protected ArtifactFactory factory;
/**
* @parameter expression="${plugin.artifacts}"
* @readonly
* @since 1.1-beta-1
*/
private List<?> pluginDependencies;
/**
* List of dependencies to include into the contribution.
*
......@@ -258,11 +252,18 @@ 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);
Contribution contribution=new Contribution(contributionName, Arrays.asList(deployables), imports, exports, jars);
File contrib =contribution.generate(targetDir);
File contributionFile = null;
try
{
Contribution contribution = new Contribution(contributionName, Arrays.asList(deployables), imports, exports, jars);
contributionFile =contribution.generate(targetDir);
}
catch (IOException e)
{
getLog().debug("Error when create Contribution Object",e);
}
String classifierName;
if(this.classifier!=null && !"".equals(this.classifier))
......@@ -275,7 +276,7 @@ public class FrascatiContributionMojo<rep> extends AbstractMojo
}
//install the generated zip in the local repository
projectHelper.attachArtifact(project, "zip", classifierName, contrib);
projectHelper.attachArtifact(project, "zip", classifierName, contributionFile);
}
/**
......
......@@ -33,4 +33,15 @@ package org.ow2.frascati.mojo.importexport;
* Helper class to marshal export definition in pom file
*
*/
public class Export extends ImportExport{}
public class Export extends ImportExport
{
public Export()
{
super();
}
public Export(String type, String value)
{
super(type, value);
}
}
......@@ -33,4 +33,15 @@ package org.ow2.frascati.mojo.importexport;
* Helper class to marshal import definition in pom file
*
*/
public class Import extends ImportExport{}
public class Import extends ImportExport
{
public Import()
{
super();
}
public Import(String type, String value)
{
super(type, value);
}
}
......@@ -90,5 +90,18 @@ public abstract class ImportExport
return "anyAttribute";
}
@Override
public boolean equals(Object obj)
{
if(!this.getClass().isInstance(obj))
{
return false;
}
ImportExport importExport=(ImportExport) obj;
return this.type.equals(importExport.getType()) && this.value.equals(importExport.getValue());
}
}
......@@ -37,13 +37,12 @@ import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Random;
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;
/**
*
......@@ -51,6 +50,8 @@ import org.ow2.frascati.mojo.Contribution;
public class ZipUtil
{
private final static int RANDOM_MAX_VALUE=1000000;
/**
* Utility method to create contribution zip file.
*
......@@ -96,18 +97,17 @@ public class ZipUtil
{
dir.mkdirs();
}
Enumeration files = zipFile.entries();
Enumeration<? extends ZipEntry> entries = zipFile.entries();
ZipEntry entry;
File f = null;
FileOutputStream fos = null;
while (files.hasMoreElements())
while (entries.hasMoreElements())
{
try
{
ZipEntry entry = (ZipEntry) files.nextElement();
entry = entries.nextElement();
InputStream eis = zipFile.getInputStream(entry);
byte[] buffer = new byte[1024];
int bytesRead = 0;
f=new File(dir,entry.getName());
if (entry.isDirectory())
......@@ -142,54 +142,15 @@ public class ZipUtil
}
}
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
/**
* List files recursively. Does not include directories.
*
* @param directory The directory to list.
* @return A list of paths relative to the directory parameter.
*/
public static Collection<String> listFiles(File directory)
{
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;
return listFiles(directory, "");
}
/**
......@@ -206,7 +167,6 @@ public class ZipUtil
// Go over entries
for (File entry : directory.listFiles())
{
if (entry.isDirectory()) {
StringBuilder sb = new StringBuilder(currentDirName);
sb.append(entry.getName()).append(File.separator);
......@@ -219,4 +179,64 @@ public class ZipUtil
// Return collection of files
return files;
}
/**
* List files recursively. Does not include directories.
*
* @param directory The directory to list.
* @return A list File relative to the directory parameter.
*/
public static Collection<File> listFile(File directory)
{
Collection<File> files = new ArrayList<File>();
for (File entry : directory.listFiles())
{
if (entry.isDirectory())
{
files.addAll(listFile(entry));
} else
{
files.add(entry);
}
}
// Return collection of files
return files;
}
/**
* create a new temp file in java temp directory
* Use default suffix : frascati
* Use default prefix : contribution
*
* @return the new temp file
*/
public static File newTempFile()
{
return newTempFile("frascati", "contribution");
}
/**
* create a new temp file in java temp directory
*
* @param suffix suffix of the temp file
* @param prefix prefix of the temp file
* @return the new temp file
*/
public static File newTempFile(String suffix, String prefix)
{
File tempDirectory=new File(System.getProperty("java.io.tmpdir"));
Random random = new Random();
String tempFileName;
int randomInt;
File tempFile;
do
{
randomInt=random.nextInt(RANDOM_MAX_VALUE);
tempFileName =suffix+randomInt+prefix;
tempFile=new File(tempDirectory,tempFileName);
}
while(tempFile.exists());
return tempFile;
}
}
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 to comment