Search in sources :

Example 31 with Resource

use of aQute.bnd.osgi.Resource in project bnd by bndtools.

the class Make method process.

public Resource process(String source) {
    Map<Instruction, Map<String, String>> make = getMakeHeader();
    logger.debug("make {}", source);
    for (Map.Entry<Instruction, Map<String, String>> entry : make.entrySet()) {
        Instruction instr = entry.getKey();
        Matcher m = instr.getMatcher(source);
        if (m.matches() || instr.isNegated()) {
            Map<String, String> arguments = replace(m, entry.getValue());
            List<MakePlugin> plugins = builder.getPlugins(MakePlugin.class);
            for (MakePlugin plugin : plugins) {
                try {
                    Resource resource = plugin.make(builder, source, arguments);
                    if (resource != null) {
                        logger.debug("Made {} from args {} with {}", source, arguments, plugin);
                        return resource;
                    }
                } catch (Exception e) {
                    builder.exception(e, "Plugin %s generates error when use in making %s with args %s", plugin, source, arguments);
                }
            }
        }
    }
    return null;
}
Also used : Matcher(java.util.regex.Matcher) MakePlugin(aQute.bnd.service.MakePlugin) Resource(aQute.bnd.osgi.Resource) Instruction(aQute.bnd.osgi.Instruction) Map(java.util.Map)

Example 32 with Resource

use of aQute.bnd.osgi.Resource in project bnd by bndtools.

the class MavenBndRepository method put.

@Override
public PutResult put(InputStream stream, PutOptions options) throws Exception {
    init();
    File binaryFile = File.createTempFile("put", ".jar");
    File pomFile = File.createTempFile("pom", ".xml");
    LocalPutResult result = new LocalPutResult();
    try {
        if (options == null)
            options = new PutOptions();
        else {
            result.options = options;
        }
        IO.copy(stream, binaryFile);
        if (options.digest != null) {
            byte[] digest = SHA1.digest(binaryFile).digest();
            if (!Arrays.equals(options.digest, digest))
                throw new IllegalArgumentException("The given sha-1 does not match the contents sha-1");
        }
        if (options.context == null) {
            options.context = registry.getPlugin(Workspace.class);
            if (options.context == null)
                options.context = new Processor();
        }
        ReleaseDTO instructions = getReleaseDTO(options.context);
        try (Jar binary = new Jar(binaryFile)) {
            Resource pomResource;
            if (instructions.pom.path != null) {
                File f = options.context.getFile(instructions.pom.path);
                if (!f.isFile())
                    throw new IllegalArgumentException("-maven-release specifies " + f + " as pom file but this file is not found");
                pomResource = new FileResource(f);
            } else {
                pomResource = getPomResource(binary);
                if (pomResource == null) {
                    pomResource = createPomResource(binary, options.context);
                    if (pomResource == null)
                        throw new IllegalArgumentException("No POM resource in META-INF/maven/... The Maven Bnd Repository requires this pom.");
                }
            }
            IO.copy(pomResource.openInputStream(), pomFile);
            IPom pom;
            try (InputStream fin = IO.stream(pomFile)) {
                pom = storage.getPom(fin);
            }
            Archive binaryArchive = pom.binaryArchive();
            checkRemotePossible(instructions, binaryArchive.isSnapshot());
            if (!binaryArchive.isSnapshot()) {
                releasePlugin.add(options.context, pom);
                if (storage.exists(binaryArchive)) {
                    logger.debug("Already released {} to {}", pom.getRevision(), this);
                    result.alreadyReleased = true;
                    return result;
                }
            }
            logger.debug("Put release {}", pom.getRevision());
            try (Release releaser = storage.release(pom.getRevision(), options.context.getProperties())) {
                if (releaser == null) {
                    logger.debug("Already released {}", pom.getRevision());
                    return result;
                }
                if (instructions.snapshot >= 0)
                    releaser.setBuild(instructions.snapshot, null);
                if (isLocal(instructions))
                    releaser.setLocalOnly();
                releaser.add(pom.getRevision().pomArchive(), pomFile);
                releaser.add(binaryArchive, binaryFile);
                result.binaryArchive = binaryArchive;
                result.pomArchive = pom.getRevision().pomArchive();
                if (!isLocal(instructions)) {
                    try (Tool tool = new Tool(options.context, binary)) {
                        if (instructions.javadoc != null) {
                            if (!NONE.equals(instructions.javadoc.path)) {
                                try (Jar jar = getJavadoc(tool, options.context, instructions.javadoc.path, instructions.javadoc.options, instructions.javadoc.packages == JavadocPackages.EXPORT)) {
                                    save(releaser, pom.getRevision(), jar, "javadoc");
                                }
                            }
                        }
                        if (instructions.sources != null) {
                            if (!NONE.equals(instructions.sources.path)) {
                                try (Jar jar = getSource(tool, options.context, instructions.sources.path)) {
                                    save(releaser, pom.getRevision(), jar, "sources");
                                }
                            }
                        }
                    }
                }
            }
            if (configuration.noupdateOnRelease() == false && !binaryArchive.isSnapshot())
                index.add(binaryArchive);
        }
        return result;
    } catch (Exception e) {
        result.failed = e.getMessage();
        throw e;
    } finally {
        IO.delete(binaryFile);
        IO.delete(pomFile);
    }
}
Also used : Processor(aQute.bnd.osgi.Processor) Archive(aQute.maven.api.Archive) InputStream(java.io.InputStream) IPom(aQute.maven.api.IPom) Resource(aQute.bnd.osgi.Resource) FileResource(aQute.bnd.osgi.FileResource) PomResource(aQute.bnd.maven.PomResource) FileResource(aQute.bnd.osgi.FileResource) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Jar(aQute.bnd.osgi.Jar) File(java.io.File) Release(aQute.maven.api.Release) Workspace(aQute.bnd.build.Workspace)

Example 33 with Resource

use of aQute.bnd.osgi.Resource in project bnd by bndtools.

the class PatchCommand method _create.

public void _create(createOptions opts) throws Exception {
    List<String> arguments = opts._arguments();
    Jar a = new Jar(bnd.getFile(arguments.remove(0)));
    Manifest am = a.getManifest();
    Jar b = new Jar(bnd.getFile(arguments.remove(0)));
    Manifest bm = b.getManifest();
    File patch = bnd.getFile(arguments.remove(0));
    // TODO check arguments
    Set<String> delete = Create.set();
    for (String path : a.getResources().keySet()) {
        Resource br = b.getResource(path);
        if (br == null) {
            logger.debug("DELETE    {}", path);
            delete.add(path);
        } else {
            Resource ar = a.getResource(path);
            if (isEqual(ar, br)) {
                logger.debug("UNCHANGED {}", path);
                b.remove(path);
            } else
                logger.debug("UPDATE    {}", path);
        }
    }
    bm.getMainAttributes().putValue("Patch-Delete", Processor.join(delete, ", "));
    bm.getMainAttributes().putValue("Patch-Version", am.getMainAttributes().getValue(Constants.BUNDLE_VERSION));
    b.write(patch);
    a.close();
    b.close();
// TODO proper close
}
Also used : Resource(aQute.bnd.osgi.Resource) Jar(aQute.bnd.osgi.Jar) Manifest(java.util.jar.Manifest) File(java.io.File)

Example 34 with Resource

use of aQute.bnd.osgi.Resource in project bnd by bndtools.

the class MergeTest method testMerge.

static void testMerge(String type, String[] in, String[] out, String c, int errors, int warnings) throws Exception {
    Builder b = new Builder();
    try {
        b.setClasspath(new File[] { IO.getFile("src/test/split/split-a.jar"), IO.getFile("src/test/split/split-b.jar") });
        Properties p = new Properties();
        if (type != null)
            p.put("Export-Package", "test.split;-split-package:=" + type);
        else
            p.put("Export-Package", "test.split");
        p.put("Import-Package", "");
        b.setProperties(p);
        Jar jar = b.build();
        System.err.println("Errors     :" + b.getErrors());
        System.err.println("Warnings   :" + b.getWarnings());
        assertEquals(errors, b.getErrors().size());
        assertEquals(warnings, b.getWarnings().size());
        if (errors != 0)
            return;
        for (int i = 0; in != null && i < in.length; i++) assertNotNull("Contains " + in[i], jar.getResource("test/split/" + in[i]));
        for (int i = 0; out != null && i < out.length; i++) assertNull("Does not contain " + out[i], jar.getResource("test/split/" + out[i]));
        Resource r = jar.getResource("test/split/C");
        InputStream is = r.openInputStream();
        BufferedReader dis = new BufferedReader(new InputStreamReader(is));
        String s = dis.readLine();
        assertEquals(s, c);
    } finally {
        b.close();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Builder(aQute.bnd.osgi.Builder) Resource(aQute.bnd.osgi.Resource) BufferedReader(java.io.BufferedReader) Jar(aQute.bnd.osgi.Jar) Properties(java.util.Properties)

Example 35 with Resource

use of aQute.bnd.osgi.Resource in project bnd by bndtools.

the class PreprocessTest method testPreProcessExcludeExtensionGlobal.

/**
	 * Check if we can override
	 * 
	 * @throws Exception
	 */
public static void testPreProcessExcludeExtensionGlobal() throws Exception {
    Builder b = new Builder();
    b.setProperty(Analyzer.PREPROCESSMATCHERS, "!*.TXT:i,*");
    b.setProperty(Analyzer.INCLUDE_RESOURCE, "{src/test/builder-preprocess.txt},{src/test/builder-preprocess.txt2}");
    b.setProperty("var", "Yes!");
    ;
    b.build();
    assertTrue(b.check());
    System.out.println("testPreProcessExcludeExtensionsGlobal");
    Jar jar = b.getJar();
    Resource resource = jar.getResource("builder-preprocess.txt");
    String s = IO.collect(resource.openInputStream());
    System.out.println(s);
    assertTrue(s.contains("${var}"));
    assertFalse(s.contains("!Yes"));
    resource = jar.getResource("builder-preprocess.txt2");
    s = IO.collect(resource.openInputStream());
    System.out.println(s);
    assertTrue(s.contains("Yes!"));
    assertFalse(s.contains("${var}"));
    b.close();
}
Also used : Builder(aQute.bnd.osgi.Builder) Resource(aQute.bnd.osgi.Resource) Jar(aQute.bnd.osgi.Jar)

Aggregations

Resource (aQute.bnd.osgi.Resource)147 Jar (aQute.bnd.osgi.Jar)87 Builder (aQute.bnd.osgi.Builder)83 File (java.io.File)76 XmlTester (aQute.bnd.test.XmlTester)48 JarResource (aQute.bnd.osgi.JarResource)20 Attributes (java.util.jar.Attributes)20 Map (java.util.Map)19 Manifest (java.util.jar.Manifest)19 FileResource (aQute.bnd.osgi.FileResource)17 LogService (org.osgi.service.log.LogService)15 HashMap (java.util.HashMap)14 Document (org.w3c.dom.Document)14 Properties (java.util.Properties)12 EmbeddedResource (aQute.bnd.osgi.EmbeddedResource)11 DocumentBuilder (javax.xml.parsers.DocumentBuilder)11 InputStream (java.io.InputStream)9 Attrs (aQute.bnd.header.Attrs)8 ArrayList (java.util.ArrayList)8 TreeMap (java.util.TreeMap)8