import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
import java.util.Iterator;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import org.argeo.slc.CategorizedNameVersion;
import org.argeo.slc.NameVersion;
import org.argeo.slc.SlcException;
+import org.eclipse.aether.artifact.Artifact;
+import org.eclipse.aether.artifact.DefaultArtifact;
import org.osgi.framework.Constants;
-import org.sonatype.aether.artifact.Artifact;
-import org.sonatype.aether.util.artifact.DefaultArtifact;
/**
* Creates a jar bundle from an ArgeoOsgiDistribution. This jar is then
* persisted and indexed in a java repository using the OSGI Factory.
*
- * It does the following <list><li>Creates a Manifest</li><li>Creates files
- * indexes (csv, feature.xml ...)</li><li>Populate the corresponding jar</li><li>
- * Save it in the repository</li><li>Index the node and creates corresponding
- * sha1 and md5 files</li> </list>
+ * It does the following <list>
+ * <li>Creates a Manifest</li>
+ * <li>Creates files indexes (csv, feature.xml ...)</li>
+ * <li>Populate the corresponding jar</li>
+ * <li>Save it in the repository</li>
+ * <li>Index the node and creates corresponding sha1 and md5 files</li> </list>
*
*/
public class ModularDistributionFactory implements Runnable {
// Constants
private final static String CSV_FILE_NAME = "modularDistribution.csv";
+ private final DateFormat snapshotTimestamp = new SimpleDateFormat("YYYYMMddhhmm");
// private final static String FEATURE_FILE_NAME = "feature.xml";
// private static int BUFFER_SIZE = 10240;
/** Convenience constructor with minimal configuration */
- public ModularDistributionFactory(OsgiFactory osgiFactory,
- ArgeoOsgiDistribution osgiDistribution) {
+ public ModularDistributionFactory(OsgiFactory osgiFactory, ArgeoOsgiDistribution osgiDistribution) {
this.osgiFactory = osgiFactory;
this.osgiDistribution = osgiDistribution;
}
else if (artifactType == "pom")
distFile = generatePomFile();
else
- throw new SlcException(
- "Unimplemented distribution artifact type: "
- + artifactType + " for "
- + osgiDistribution.toString());
+ throw new SlcException("Unimplemented distribution artifact type: " + artifactType + " for "
+ + osgiDistribution.toString());
// Save in java repository
- Artifact osgiArtifact = new DefaultArtifact(
- osgiDistribution.getCategory(), osgiDistribution.getName(),
+ Artifact osgiArtifact = new DefaultArtifact(osgiDistribution.getCategory(), osgiDistribution.getName(),
artifactType, osgiDistribution.getVersion());
- Node distNode = RepoUtils.copyBytesAsArtifact(
- javaSession.getNode(artifactBasePath), osgiArtifact,
+ Node distNode = RepoUtils.copyBytesAsArtifact(javaSession.getNode(artifactBasePath), osgiArtifact,
distFile);
// index
javaSession.save();
} catch (RepositoryException e) {
throw new SlcException(
- "JCR error while persisting modular distribution in JCR "
- + osgiDistribution.toString(), e);
+ "JCR error while persisting modular distribution in JCR " + osgiDistribution.toString(), e);
} finally {
JcrUtils.logoutQuietly(javaSession);
}
jarOut.close();
return byteOut.toByteArray();
} catch (IOException e) {
- throw new SlcException(
- "IO error while generating modular distribution "
- + osgiDistribution.toString(), e);
+ throw new SlcException("IO error while generating modular distribution " + osgiDistribution.toString(), e);
} finally {
IOUtils.closeQuietly(byteOut);
IOUtils.closeQuietly(jarOut);
Manifest manifest = new Manifest();
// TODO make this configurable
- manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION,
- "1.0");
- addManifestAttribute(manifest,
- Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT, "JavaSE-1.6");
+ manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
+// addManifestAttribute(manifest, Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT, "JavaSE-1.8");
addManifestAttribute(manifest, Constants.BUNDLE_VENDOR, "Argeo");
addManifestAttribute(manifest, Constants.BUNDLE_MANIFESTVERSION, "2");
- addManifestAttribute(manifest, "Bundle-License",
- "http://www.apache.org/licenses/LICENSE-2.0.txt");
+// addManifestAttribute(manifest, "Bundle-License", "http://www.apache.org/licenses/LICENSE-2.0.txt");
// TODO define a user friendly name
- addManifestAttribute(manifest, Constants.BUNDLE_NAME,
- osgiDistribution.getName());
+ addManifestAttribute(manifest, Constants.BUNDLE_NAME, osgiDistribution.getName());
// Categorized name version
- addManifestAttribute(manifest, RepoConstants.SLC_GROUP_ID,
- osgiDistribution.getCategory());
- addManifestAttribute(manifest, Constants.BUNDLE_SYMBOLICNAME,
- osgiDistribution.getName());
- addManifestAttribute(manifest, Constants.BUNDLE_VERSION,
- osgiDistribution.getVersion());
+ addManifestAttribute(manifest, RepoConstants.SLC_CATEGORY_ID, osgiDistribution.getCategory());
+ addManifestAttribute(manifest, Constants.BUNDLE_SYMBOLICNAME, osgiDistribution.getName());
+ String version = osgiDistribution.getVersion();
+ if (version.endsWith("-SNAPSHOT")) {
+ version = version.substring(0, version.length() - "-SNAPSHOT".length());
+ version = version + ".SNAPSHOT-r" + snapshotTimestamp.format(new Date());
+ }
+ addManifestAttribute(manifest, Constants.BUNDLE_VERSION, version);
return manifest;
}
- private void addManifestAttribute(Manifest manifest, String name,
- String value) {
+ private void addManifestAttribute(Manifest manifest, String name, String value) {
manifest.getMainAttributes().put(new Attributes.Name(name), value);
}
tmpFile.deleteOnExit();
writer = new FileWriter(tmpFile);
// Populate the file
- for (Iterator<? extends NameVersion> it = osgiDistribution
- .nameVersions(); it.hasNext();)
+ for (Iterator<? extends NameVersion> it = osgiDistribution.nameVersions(); it.hasNext();)
writer.write(getCsvLine(it.next()));
writer.flush();
return FileUtils.readFileToByteArray(tmpFile);
} catch (Exception e) {
- throw new SlcException(
- "unable to create csv distribution file for "
- + osgiDistribution.toString(), e);
+ throw new SlcException("unable to create csv distribution file for " + osgiDistribution.toString(), e);
} finally {
IOUtils.closeQuietly(writer);
}
StringBuilder b = new StringBuilder();
// XML header
b.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
- b.append("<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">\n");
+ b.append(
+ "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">\n");
b.append("<modelVersion>4.0.0</modelVersion>");
// Artifact
- b.append("<groupId>").append(osgiDistribution.getCategory())
- .append("</groupId>\n");
- b.append("<artifactId>").append(osgiDistribution.getName())
- .append("</artifactId>\n");
- b.append("<version>").append(osgiDistribution.getVersion())
- .append("</version>\n");
+ b.append("<groupId>").append(osgiDistribution.getCategory()).append("</groupId>\n");
+ b.append("<artifactId>").append(osgiDistribution.getName()).append("</artifactId>\n");
+ b.append("<version>").append(osgiDistribution.getVersion()).append("</version>\n");
b.append("<packaging>pom</packaging>\n");
// p.append("<name>").append("Bundle Name").append("</name>\n");
- // p.append("<description>").append("Bundle Description").append("</description>\n");
+ // p.append("<description>").append("Bundle
+ // Description").append("</description>\n");
// Dependencies
b.append("<dependencies>\n");
- for (Iterator<? extends NameVersion> it = osgiDistribution
- .nameVersions(); it.hasNext();) {
+ for (Iterator<? extends NameVersion> it = osgiDistribution.nameVersions(); it.hasNext();) {
NameVersion nameVersion = it.next();
if (!(nameVersion instanceof CategorizedNameVersion))
- throw new SlcException("Unsupported type "
- + nameVersion.getClass());
+ throw new SlcException("Unsupported type " + nameVersion.getClass());
CategorizedNameVersion nv = (CategorizedNameVersion) nameVersion;
b.append(getDependencySnippet(nv, false));
}
b.append("<dependencyManagement>\n");
b.append("<dependencies>\n");
- for (Iterator<? extends NameVersion> it = osgiDistribution
- .nameVersions(); it.hasNext();)
- b.append(getDependencySnippet((CategorizedNameVersion) it.next(),
- true));
+ for (Iterator<? extends NameVersion> it = osgiDistribution.nameVersions(); it.hasNext();)
+ b.append(getDependencySnippet((CategorizedNameVersion) it.next(), true));
b.append("</dependencies>\n");
b.append("</dependencyManagement>\n");
return b.toString().getBytes();
}
- private String getDependencySnippet(CategorizedNameVersion cnv,
- boolean includeVersion) { // , String type, String scope
+ private String getDependencySnippet(CategorizedNameVersion cnv, boolean includeVersion) { // , String type, String
+ // scope
StringBuilder b = new StringBuilder();
b.append("<dependency>\n");
- b.append("\t<groupId>").append(cnv.getCategory())
- .append("</groupId>\n");
- b.append("\t<artifactId>").append(cnv.getName())
- .append("</artifactId>\n");
+ b.append("\t<groupId>").append(cnv.getCategory()).append("</groupId>\n");
+ b.append("\t<artifactId>").append(cnv.getName()).append("</artifactId>\n");
if (includeVersion)
- b.append("\t<version>").append(cnv.getVersion())
- .append("</version>\n");
+ b.append("\t<version>").append(cnv.getVersion()).append("</version>\n");
// if (type!= null)
// p.append("\t<type>").append(type).append("</type>\n");
// if (type!= null)
}
// Helpers
- private void addToJar(byte[] content, String name, JarOutputStream target)
- throws IOException {
+ private void addToJar(byte[] content, String name, JarOutputStream target) throws IOException {
ByteArrayInputStream in = null;
try {
target.putNextEntry(new JarEntry(name));
}
}
- private String getCsvLine(NameVersion nameVersion)
- throws RepositoryException {
+ private String getCsvLine(NameVersion nameVersion) throws RepositoryException {
if (!(nameVersion instanceof CategorizedNameVersion))
throw new SlcException("Unsupported type " + nameVersion.getClass());
CategorizedNameVersion cnv = (CategorizedNameVersion) nameVersion;
this.osgiDistribution = osgiDistribution;
}
- public void setModularDistributionSeparator(
- String modularDistributionSeparator) {
+ public void setModularDistributionSeparator(String modularDistributionSeparator) {
this.modularDistributionSeparator = modularDistributionSeparator;
}