/** * Creates a new deployment package. * * @param name * the name of the deployment package * @param version * the version of the deployment package * @return a builder to further add data to the deployment package */ public static DeploymentPackageBuilder createDeploymentPackage(String name, String version) { return new DeploymentPackageBuilder(name, version); }
/** * Adds a bundle to the deployment package. * * @param url * a url that refers to the bundle * @return a builder to further add data to the deployment package * @throws Exception * if something goes wrong while building */ public DeploymentPackageBuilder addBundle(URL url) throws Exception { return addBundleArtifact(url, false); }
/** * Generates a deployment package and streams it to the output stream you provide. Before it starts generating, it * will first validate that you have actually specified a resource processor for each type of artifact you provided. * * @param output * the output stream to write to * @throws Exception * if something goes wrong while validating or generating */ public void generate(OutputStream output) throws Exception { validateArtifacts(); List<ArtifactData> artifacts = new ArrayList<>(); artifacts.addAll(m_bundles); artifacts.addAll(m_processors); artifacts.addAll(m_artifacts); Manifest m = createManifest(artifacts); writeStream(artifacts, m, output); }
@Test() public void testProcessorAndResourceDeploymentPackage() throws Exception { File tempFile = File.createTempFile("output-", ".jar"); FileOutputStream output = new FileOutputStream(tempFile); String name = "test"; String version = "1.0.0"; String bundleSymbolicName = "bundle"; String bundleVersion = "1.0.0"; File tempBundleFile = File.createTempFile(bundleSymbolicName + "-" + bundleVersion + "-", ".jar"); String pid = "my.processor"; File tempArtifactFile = File.createTempFile("artifact-", ".jar"); DeploymentPackageBuilder.createDeploymentPackage(name, version) .addResourceProcessor(createResourceProcessor(bundleSymbolicName, bundleVersion, pid, tempBundleFile)) .addArtifact(createArtifact(pid, tempArtifactFile), pid) .generate(output); // the deployment package should have a name and a version, and a single entry (our bundle) Manifest m = getManifest(tempFile); Assert.assertEquals(name, m.getMainAttributes().getValue("DeploymentPackage-SymbolicName")); Assert.assertEquals(version, m.getMainAttributes().getValue("DeploymentPackage-Version")); Map<String, Attributes> entries = m.getEntries(); Assert.assertEquals(2, entries.size()); contains(entries.values(), "Bundle-SymbolicName", bundleSymbolicName, "Bundle-Version", bundleVersion, "DeploymentPackage-Customizer", "true", "Deployment-ProvidesResourceProcessor", pid); contains(entries.values(), "Resource-Processor", pid); }
@Test() public void testSingleBundleDeploymentPackage() throws Exception { File tempFile = File.createTempFile("output-", ".jar"); FileOutputStream output = new FileOutputStream(tempFile); String name = "test"; String version = "1.0.0"; String bundleSymbolicName = "bundle"; String bundleVersion = "1.0.0"; File tempBundleFile = File.createTempFile(bundleSymbolicName + "-" + bundleVersion + "-", ".jar"); DeploymentPackageBuilder.createDeploymentPackage(name, version) .addBundle(createBundle(bundleSymbolicName, bundleVersion, tempBundleFile)) .generate(output); // the deployment package should have a name and a version, and a single entry (our bundle) Manifest m = getManifest(tempFile); Assert.assertEquals(name, m.getMainAttributes().getValue("DeploymentPackage-SymbolicName")); Assert.assertEquals(version, m.getMainAttributes().getValue("DeploymentPackage-Version")); Assert.assertEquals(1, m.getEntries().size()); contains(m.getEntries().values(), "Bundle-SymbolicName", bundleSymbolicName, "Bundle-Version", bundleVersion); }
@Test(expectedExceptions = { Exception.class }) public void testResourceWithoutProcessorDeploymentPackage() throws Exception { File tempFile = File.createTempFile("output-", ".jar"); FileOutputStream output = new FileOutputStream(tempFile); String name = "test"; String version = "1.0.0"; String pid = "my.processor"; File tempArtifactFile = File.createTempFile("artifact-", ".jar"); DeploymentPackageBuilder.createDeploymentPackage(name, version) .addArtifact(createArtifact(pid, tempArtifactFile), pid) .generate(output); }
@Test() public void testEmptyDeploymentPackage() throws Exception { File tempFile = File.createTempFile("output-", ".jar"); System.out.println("File: " + tempFile); FileOutputStream output = new FileOutputStream(tempFile); String name = "test"; String version = "1.0.0"; DeploymentPackageBuilder.createDeploymentPackage(name, version).generate(output); Manifest m = getManifest(tempFile); // the deployment package should have just a name and a version, but no entries Assert.assertEquals(name, m.getMainAttributes().getValue("DeploymentPackage-SymbolicName")); Assert.assertEquals(version, m.getMainAttributes().getValue("DeploymentPackage-Version")); Assert.assertTrue(m.getEntries().isEmpty()); }
/** * Adds an artifact to the deployment package. * * @param url * a url that refers to the artifact * @param processorPID * the PID of the processor for this artifact * @return a builder to further add data to the deployment package * @throws Exception * if something goes wrong while building */ public DeploymentPackageBuilder addArtifact(URL url, String processorPID) throws Exception { String path = url.getPath(); int i = path.lastIndexOf('/'); if (i > 0 && i < (path.length() - 1)) { path = path.substring(i + 1); } String name = PREFIX_ARTIFACT + getUniqueID() + "-" + path; m_artifacts.add(ArtifactData.createArtifact(url, name, processorPID)); return this; }
String bundleSymbolicName = getRequiredHeader(attributes, "Bundle-SymbolicName"); String bundleVersion = getRequiredHeader(attributes, "Bundle-Version"); if (!"true".equals(getRequiredHeader(attributes, "DeploymentPackage-Customizer"))) { throw new IOException("Invalid DeploymentPackage-Customizer header in: " + url); String processorPID = getRequiredHeader(attributes, "Deployment-ProvidesResourceProcessor"); m_processors.add(ArtifactData.createResourceProcessor(url, name, bundleSymbolicName, bundleVersion, processorPID));
@Test() public void testTwoBundleDeploymentPackage() throws Exception { File tempFile = File.createTempFile("output-", ".jar"); FileOutputStream output = new FileOutputStream(tempFile); String name = "test"; String version = "1.0.0"; String bundleSymbolicName = "bundle"; String bundleVersion = "1.0.0"; File tempBundleFile = File.createTempFile(bundleSymbolicName + "-" + bundleVersion + "-", ".jar"); String bundleSymbolicName2 = "bundle-two"; String bundleVersion2 = "1.2.0"; File tempBundleFile2 = File.createTempFile(bundleSymbolicName2 + "-" + bundleVersion2 + "-", ".jar"); DeploymentPackageBuilder.createDeploymentPackage(name, version) .addBundle(createBundle(bundleSymbolicName, bundleVersion, tempBundleFile)) .addBundle(createBundle(bundleSymbolicName2, bundleVersion2, tempBundleFile2)) .generate(output); // the deployment package should have a name and a version, and a single entry (our bundle) Manifest m = getManifest(tempFile); Assert.assertEquals(name, m.getMainAttributes().getValue("DeploymentPackage-SymbolicName")); Assert.assertEquals(version, m.getMainAttributes().getValue("DeploymentPackage-Version")); Assert.assertEquals(2, m.getEntries().size()); contains(m.getEntries().values(), "Bundle-SymbolicName", bundleSymbolicName, "Bundle-Version", bundleVersion); contains(m.getEntries().values(), "Bundle-SymbolicName", bundleSymbolicName2, "Bundle-Version", bundleVersion2); }
/** * Adds a resource processor to the deployment package. A resource processor is a special type of bundle. * * @param url * a url that refers to the resource processor * @return a builder to further add data to the deployment package * @throws Exception * if something goes wrong while building */ public DeploymentPackageBuilder addResourceProcessor(URL url) throws Exception { return addBundleArtifact(url, true); }