Search in sources :

Example 61 with Resource

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

the class DSAnnotationTest method checkDS13Anno.

private void checkDS13Anno(Jar jar, String name, String pids) throws Exception, XPathExpressionException {
    Resource r = jar.getResource("OSGI-INF/" + name + ".xml");
    System.err.println(Processor.join(jar.getResources().keySet(), "\n"));
    assertNotNull(r);
    r.write(System.err);
    XmlTester xt = new XmlTester(r.openInputStream(), "scr", "http://www.osgi.org/xmlns/scr/v1.3.0");
    // Test the defaults
    xt.assertAttribute(name, "scr:component/implementation/@class");
    // Default must be the implementation class
    xt.assertAttribute(name, "scr:component/@name");
    xt.assertAttribute("", "scr:component/@configuration-policy");
    xt.assertAttribute("", "scr:component/@immediate");
    xt.assertAttribute("", "scr:component/@enabled");
    xt.assertAttribute("", "scr:component/@factory");
    xt.assertAttribute("", "scr:component/service/@scope");
    xt.assertAttribute(pids, "scr:component/@configuration-pid");
    xt.assertAttribute("activate", "scr:component/@activate");
    xt.assertAttribute("deactivate", "scr:component/@deactivate");
    xt.assertAttribute("modified", "scr:component/@modified");
    xt.assertAttribute("java.io.Serializable", "scr:component/service/provide[1]/@interface");
    xt.assertAttribute("java.lang.Runnable", "scr:component/service/provide[2]/@interface");
    xt.assertAttribute("0", "count(scr:component/properties)");
    xt.assertAttribute("18", "count(scr:component/property)");
    xt.assertAttribute("foo", "scr:component/property[@name='myString']/@value");
    xt.assertAttribute("String", "scr:component/property[@name='myString']/@type");
    xt.assertTrimmedAttribute("foo\\nbar", "scr:component/property[@name='myStringArray']");
    xt.assertAttribute("String", "scr:component/property[@name='myStringArray']/@type");
    xt.assertTrimmedAttribute("baz", "scr:component/property[@name='mySingleStringArray']");
    xt.assertAttribute("String", "scr:component/property[@name='mySingleStringArray']/@type");
    xt.assertAttribute("1", "scr:component/property[@name='myInt']/@value");
    xt.assertAttribute("Integer", "scr:component/property[@name='myInt']/@type");
    xt.assertTrimmedAttribute("2\\n3", "scr:component/property[@name='myIntArray']");
    xt.assertAttribute("Integer", "scr:component/property[@name='myIntArray']/@type");
    xt.assertTrimmedAttribute("4", "scr:component/property[@name='mySingleIntArray']");
    xt.assertAttribute("Integer", "scr:component/property[@name='mySingleIntArray']/@type");
    xt.assertAttribute("test.component.DSAnnotationTest$ConfigTypes", "scr:component/property[@name='myClass']/@value");
    xt.assertAttribute("String", "scr:component/property[@name='myClass']/@type");
    xt.assertTrimmedAttribute("test.component.DSAnnotationTest$ConfigTypes\\ntest.component.DSAnnotationTest$ConfigTypes", "scr:component/property[@name='myClassArray']");
    xt.assertAttribute("String", "scr:component/property[@name='myClassArray']/@type");
    xt.assertTrimmedAttribute("test.component.DSAnnotationTest$ConfigTypes", "scr:component/property[@name='mySingleClassArray']");
    xt.assertAttribute("String", "scr:component/property[@name='mySingleClassArray']/@type");
    xt.assertAttribute("A", "scr:component/property[@name='myEnum']/@value");
    xt.assertAttribute("String", "scr:component/property[@name='myEnum']/@type");
    xt.assertTrimmedAttribute("A\\nB", "scr:component/property[@name='myEnumArray']");
    xt.assertAttribute("String", "scr:component/property[@name='myEnumArray']/@type");
    xt.assertTrimmedAttribute("B", "scr:component/property[@name='mySingleEnumArray']");
    xt.assertAttribute("String", "scr:component/property[@name='mySingleEnumArray']/@type");
    xt.assertAttribute("1.0", "scr:component/property[@name='myFloat']/@value");
    xt.assertAttribute("Float", "scr:component/property[@name='myFloat']/@type");
    xt.assertAttribute("97", "scr:component/property[@name='myChar']/@value");
    xt.assertAttribute("Character", "scr:component/property[@name='myChar']/@type");
    xt.assertAttribute("2", "scr:component/property[@name='myByte']/@value");
    xt.assertAttribute("Byte", "scr:component/property[@name='myByte']/@type");
    xt.assertAttribute("298", "scr:component/property[@name='myShort']/@value");
    xt.assertAttribute("Short", "scr:component/property[@name='myShort']/@type");
    xt.assertAttribute("9876543210", "scr:component/property[@name='myLong']/@value");
    xt.assertAttribute("Long", "scr:component/property[@name='myLong']/@type");
    xt.assertAttribute("2.1", "scr:component/property[@name='myDouble']/@value");
    xt.assertAttribute("Double", "scr:component/property[@name='myDouble']/@type");
}
Also used : XmlTester(aQute.bnd.test.XmlTester) Resource(aQute.bnd.osgi.Resource)

Example 62 with Resource

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

the class DSAnnotationTest method testFinalDynamicCollectionField.

public void testFinalDynamicCollectionField() throws Exception {
    Builder b = new Builder();
    b.setProperty(Constants.DSANNOTATIONS, "test.component.*FinalDynamicCollectionField");
    b.setProperty("Private-Package", "test.component");
    b.addClasspath(new File("bin"));
    Jar jar = b.build();
    assertOk(b);
    Attributes a = getAttr(jar);
    checkRequires(a, "1.3.0", LogService.class.getName());
    Resource r = jar.getResource("OSGI-INF/test.component.DSAnnotationTest$FinalDynamicCollectionField.xml");
    System.err.println(Processor.join(jar.getResources().keySet(), "\n"));
    assertNotNull(r);
    r.write(System.err);
    XmlTester xt = new XmlTester(r.openInputStream(), "scr", "http://www.osgi.org/xmlns/scr/v1.3.0");
    // Test the defaults
    xt.assertAttribute("test.component.DSAnnotationTest$FinalDynamicCollectionField", "scr:component/implementation/@class");
    xt.assertAttribute("logs1", "scr:component/reference[1]/@name");
    xt.assertAttribute(LogService.class.getName(), "scr:component/reference[1]/@interface");
    xt.assertAttribute("dynamic", "scr:component/reference[1]/@policy");
    xt.assertAttribute("update", "scr:component/reference[1]/@field-option");
    xt.assertAttribute("logs2", "scr:component/reference[2]/@name");
    xt.assertAttribute(LogService.class.getName(), "scr:component/reference[2]/@interface");
    xt.assertNoAttribute("scr:component/reference[2]/@policy");
    xt.assertNoAttribute("scr:component/reference[2]/@field-option");
}
Also used : XmlTester(aQute.bnd.test.XmlTester) Builder(aQute.bnd.osgi.Builder) Attributes(java.util.jar.Attributes) Resource(aQute.bnd.osgi.Resource) Jar(aQute.bnd.osgi.Jar) File(java.io.File) LogService(org.osgi.service.log.LogService)

Example 63 with Resource

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

the class MavenCommand method bundle.

/**
	 * Create a maven bundle.
	 * 
	 * @param args
	 * @param i
	 * @throws Exception
	 */
private void bundle(String[] args, int i) throws Exception {
    List<String> developers = new ArrayList<String>();
    Properties properties = new UTF8Properties();
    String scm = null;
    String passphrase = null;
    String javadoc = null;
    String source = null;
    String output = "bundle.jar";
    String url = null;
    String artifact = null;
    String group = null;
    String version = null;
    boolean nodelete = false;
    while (i < args.length && args[i].startsWith("-")) {
        String option = args[i++];
        logger.debug("bundle option {}", option);
        if (option.equals("-scm"))
            scm = args[i++];
        else if (option.equals("-group"))
            group = args[i++];
        else if (option.equals("-artifact"))
            artifact = args[i++];
        else if (option.equals("-version"))
            version = args[i++];
        else if (option.equals("-developer"))
            developers.add(args[i++]);
        else if (option.equals("-passphrase")) {
            passphrase = args[i++];
        } else if (option.equals("-url")) {
            url = args[i++];
        } else if (option.equals("-javadoc"))
            javadoc = args[i++];
        else if (option.equals("-source"))
            source = args[i++];
        else if (option.equals("-output"))
            output = args[i++];
        else if (option.equals("-nodelete"))
            nodelete = true;
        else if (option.startsWith("-properties")) {
            try (InputStream in = IO.stream(Paths.get(args[i++]))) {
                properties.load(in);
            } catch (Exception e) {
            }
        }
    }
    if (developers.isEmpty()) {
        String email = settings.remove("email");
        if (email == null)
            error("No developer email set, you can set global default email with: bnd global email Peter.Kriens@aQute.biz");
        else
            developers.add(email);
    }
    if (i == args.length) {
        error("too few arguments, no bundle specified");
        return;
    }
    if (i != args.length - 1) {
        error("too many arguments, only one bundle allowed");
        return;
    }
    String input = args[i++];
    Jar binaryJar = getJarFromFileOrURL(input);
    logger.debug("got {}", binaryJar);
    if (binaryJar == null) {
        error("JAR does not exist: %s", input);
        return;
    }
    File original = getFile(temp, "original");
    IO.mkdirs(original);
    binaryJar.expand(original);
    binaryJar.calcChecksums(null);
    Manifest manifest = binaryJar.getManifest();
    logger.debug("got manifest");
    @SuppressWarnings("resource") PomFromManifest pom = new PomFromManifest(manifest);
    if (scm != null)
        pom.setSCM(scm);
    if (url != null)
        pom.setURL(url);
    if (artifact != null)
        pom.setArtifact(artifact);
    if (artifact != null)
        pom.setGroup(group);
    if (version != null)
        pom.setVersion(version);
    logger.debug("{}", url);
    for (String d : developers) pom.addDeveloper(d);
    Set<String> exports = OSGiHeader.parseHeader(manifest.getMainAttributes().getValue(Constants.EXPORT_PACKAGE)).keySet();
    Jar sourceJar;
    if (source == null) {
        logger.debug("Splitting source code");
        sourceJar = new Jar("source");
        for (Map.Entry<String, Resource> entry : binaryJar.getResources().entrySet()) {
            if (entry.getKey().startsWith("OSGI-OPT/src")) {
                sourceJar.putResource(entry.getKey().substring("OSGI-OPT/src/".length()), entry.getValue());
            }
        }
        copyInfo(binaryJar, sourceJar, "source");
    } else {
        sourceJar = getJarFromFileOrURL(source);
    }
    sourceJar.calcChecksums(null);
    Jar javadocJar;
    if (javadoc == null) {
        logger.debug("creating javadoc because -javadoc not used");
        javadocJar = javadoc(getFile(original, "OSGI-OPT/src"), exports, manifest, properties);
        if (javadocJar == null) {
            error("Cannot find source code in OSGI-OPT/src to generate Javadoc");
            return;
        }
        copyInfo(binaryJar, javadocJar, "javadoc");
    } else {
        logger.debug("Loading javadoc externally {}", javadoc);
        javadocJar = getJarFromFileOrURL(javadoc);
    }
    javadocJar.calcChecksums(null);
    addClose(binaryJar);
    addClose(sourceJar);
    addClose(javadocJar);
    logger.debug("creating bundle dir");
    File bundle = new File(temp, "bundle");
    IO.mkdirs(bundle);
    String prefix = pom.getArtifactId() + "-" + pom.getVersion();
    File binaryFile = new File(bundle, prefix + ".jar");
    File sourceFile = new File(bundle, prefix + "-sources.jar");
    File javadocFile = new File(bundle, prefix + "-javadoc.jar");
    File pomFile = new File(bundle, "pom.xml").getAbsoluteFile();
    logger.debug("creating output files {}, {}, {}, and {}", binaryFile, sourceFile, javadocFile, pomFile);
    IO.copy(pom.openInputStream(), pomFile);
    logger.debug("copied pom");
    logger.debug("writing binary {}", binaryFile);
    binaryJar.write(binaryFile);
    logger.debug("writing source {}", sourceFile);
    sourceJar.write(sourceFile);
    logger.debug("writing javadoc {}", javadocFile);
    javadocJar.write(javadocFile);
    sign(binaryFile, passphrase);
    sign(sourceFile, passphrase);
    sign(javadocFile, passphrase);
    sign(pomFile, passphrase);
    logger.debug("create bundle");
    Jar bundleJar = new Jar(bundle);
    addClose(bundleJar);
    File outputFile = getFile(output);
    bundleJar.write(outputFile);
    logger.debug("created bundle {}", outputFile);
    binaryJar.close();
    sourceJar.close();
    javadocJar.close();
    bundleJar.close();
    if (!nodelete)
        IO.delete(temp);
}
Also used : InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Resource(aQute.bnd.osgi.Resource) UTF8Properties(aQute.lib.utf8properties.UTF8Properties) Properties(java.util.Properties) Manifest(java.util.jar.Manifest) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Jar(aQute.bnd.osgi.Jar) File(java.io.File) Map(java.util.Map) UTF8Properties(aQute.lib.utf8properties.UTF8Properties)

Example 64 with Resource

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

the class ManifestTest method testNameSection.

public static void testNameSection() throws Exception {
    Builder b = new Builder();
    b.setProperty("Export-Package", "org.osgi.framework");
    b.addClasspath(IO.getFile("jar/osgi.jar"));
    Jar jar = b.build();
    jar.calcChecksums(null);
    File f = File.createTempFile("abc", ".jar");
    f.deleteOnExit();
    jar.write(f);
    jar = new Jar(f);
    f.delete();
    assertTrue(b.check());
    Resource r = jar.getResource("META-INF/MANIFEST.MF");
    assertNotNull(r);
    // String ms = IO.collect( r.openInputStream());
    Manifest m = new Manifest(r.openInputStream());
    assertEquals(31, m.getEntries().size());
    Attributes ba = m.getAttributes("org/osgi/framework/BundleActivator.class");
    assertNotNull(ba);
    assertEquals("RTRhr3kadnulINegRhpmog==", ba.getValue("MD5-Digest"));
    Attributes bundle = m.getAttributes("org/osgi/framework/Bundle.class");
    assertNotNull(bundle);
    assertEquals("fpQdL60w3CQK+7xlXtM6oA==", bundle.getValue("MD5-Digest"));
    Attributes sl = m.getAttributes("org/osgi/framework/ServiceListener.class");
    assertNotNull(sl);
    assertEquals("nzDRN19MrTJG+LP8ayKZITZ653g=", sl.getValue("SHA-Digest"));
}
Also used : Builder(aQute.bnd.osgi.Builder) Resource(aQute.bnd.osgi.Resource) Attributes(java.util.jar.Attributes) Jar(aQute.bnd.osgi.Jar) Manifest(java.util.jar.Manifest) File(java.io.File)

Example 65 with Resource

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

the class MavenTest method testPom.

void testPom(String where, String pom, String bsn, String version, String groupId, String artifactId, String mversion, String scm, String developers, String license) throws IOException, SAXException, ParserConfigurationException, Exception {
    Builder b = new Builder();
    b.setProperty("-pom", pom);
    b.setBundleSymbolicName(bsn);
    b.setBundleVersion(version);
    b.setProperty("-resourceonly", "true");
    if (developers != null)
        b.setProperty(Constants.BUNDLE_DEVELOPERS, developers);
    if (scm != null)
        b.setProperty(Constants.BUNDLE_SCM, scm);
    if (license != null)
        b.setProperty(Constants.BUNDLE_LICENSE, license);
    Jar jar = b.build();
    assertTrue(b.check());
    Resource r = jar.getResource(where);
    IO.copy(r.openInputStream(), System.out);
    Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(r.openInputStream());
    XPath xpath = XPathFactory.newInstance().newXPath();
    assertEquals(groupId, xpath.evaluate("/project/groupId", d));
    assertEquals(artifactId, xpath.evaluate("/project/artifactId", d));
    assertEquals(mversion, xpath.evaluate("/project/version", d));
    assertEquals((developers == null) ? "0" : "1", xpath.evaluate("count(/project/developers)", d));
    assertEquals((scm == null) ? "0" : "1", xpath.evaluate("count(/project/scm)", d));
    assertEquals(((license == null) || license.trim().equals("<<EXTERNAL>>")) ? "0" : "1", xpath.evaluate("count(/project/licenses)", d));
}
Also used : XPath(javax.xml.xpath.XPath) Builder(aQute.bnd.osgi.Builder) Resource(aQute.bnd.osgi.Resource) Jar(aQute.bnd.osgi.Jar) Document(org.w3c.dom.Document)

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