Search in sources :

Example 6 with Instruction

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

the class ProjectBuilder method getBaselineJar.

/**
	 * This method attempts to find the baseline jar for the current project. It
	 * reads the -baseline property and treats it as instructions. These
	 * instructions are matched against the bsns of the jars (think sub
	 * builders!). If they match, the sub builder is selected.
	 * <p>
	 * The instruction can then specify the following options:
	 * 
	 * <pre>
	 *  version :
	 * baseline version from repository file : a file path
	 * </pre>
	 * 
	 * If neither is specified, the current version is used to find the highest
	 * version (without qualifier) that is below the current version. If a
	 * version is specified, we take the highest version with the same base
	 * version.
	 * <p>
	 * Since baselining is expensive and easily generates errors you must enable
	 * it. The easiest solution is to {@code -baseline: *}. This will match all
	 * sub builders and will calculate the version.
	 * 
	 * @return a Jar or null
	 */
public Jar getBaselineJar() throws Exception {
    String bl = getProperty(Constants.BASELINE);
    if (bl == null || Constants.NONE.equals(bl))
        return null;
    Instructions baselines = new Instructions(getProperty(Constants.BASELINE));
    if (baselines.isEmpty())
        // no baselining
        return null;
    RepositoryPlugin repo = getBaselineRepo();
    if (repo == null)
        // errors reported already
        return null;
    String bsn = getBsn();
    Version version = new Version(getVersion());
    SortedSet<Version> versions = removeStagedAndFilter(repo.versions(bsn), repo, bsn);
    if (versions.isEmpty()) {
        // We have a repo
        Version v = Version.parseVersion(getVersion()).getWithoutQualifier();
        if (v.compareTo(Version.ONE) > 0) {
            warning("There is no baseline for %s in the baseline repo %s. The build is for version %s, which is higher than 1.0.0 which suggests that there should be a prior version.", getBsn(), repo, v);
        }
        return null;
    }
    for (Entry<Instruction, Attrs> e : baselines.entrySet()) {
        if (e.getKey().matches(bsn)) {
            Attrs attrs = e.getValue();
            Version target;
            if (attrs.containsKey("version")) {
                // Specified version!
                String v = attrs.get("version");
                if (!Verifier.isVersion(v)) {
                    error("Not a valid version in %s %s", Constants.BASELINE, v);
                    return null;
                }
                Version base = new Version(v);
                SortedSet<Version> later = versions.tailSet(base);
                if (later.isEmpty()) {
                    error("For baselineing %s-%s, specified version %s not found", bsn, version, base);
                    return null;
                }
                // First element is equal or next to the base we desire
                target = later.first();
            // Now, we could end up with a higher version than our
            // current
            // project
            } else if (attrs.containsKey("file")) {
                // Can be useful to specify a file
                // for example when copying a bundle with a public api
                File f = getProject().getFile(attrs.get("file"));
                if (f != null && f.isFile()) {
                    Jar jar = new Jar(f);
                    addClose(jar);
                    return jar;
                }
                error("Specified file for baseline but could not find it %s", f);
                return null;
            } else {
                target = versions.last();
            }
            if (target.getWithoutQualifier().compareTo(version.getWithoutQualifier()) > 0) {
                error("The baseline version %s is higher than the current version %s for %s in %s", target, version, bsn, repo);
                return null;
            }
            if (target.getWithoutQualifier().compareTo(version.getWithoutQualifier()) == 0) {
                if (isPedantic()) {
                    warning("Baselining against jar");
                }
            }
            File file = repo.get(bsn, target, attrs);
            if (file == null || !file.isFile()) {
                error("Decided on version %s-%s but cannot get file from repo %s", bsn, version, repo);
                return null;
            }
            Jar jar = new Jar(file);
            addClose(jar);
            return jar;
        }
    }
    // Ignore, nothing matched
    return null;
}
Also used : Version(aQute.bnd.version.Version) Attrs(aQute.bnd.header.Attrs) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) Instructions(aQute.bnd.osgi.Instructions) Jar(aQute.bnd.osgi.Jar) Instruction(aQute.bnd.osgi.Instruction) File(java.io.File)

Example 7 with Instruction

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

the class MetatypeAnnotations method analyzeJar.

public boolean analyzeJar(Analyzer analyzer) throws Exception {
    this.minVersion = MetatypeVersion.VERSION_1_2;
    Parameters header = OSGiHeader.parseHeader(analyzer.getProperty(Constants.METATYPE_ANNOTATIONS, "*"));
    if (header.size() == 0)
        return false;
    Parameters optionsHeader = OSGiHeader.parseHeader(analyzer.getProperty(Constants.METATYPE_ANNOTATIONS_OPTIONS));
    EnumSet<Options> options = EnumSet.noneOf(Options.class);
    for (Map.Entry<String, Attrs> entry : optionsHeader.entrySet()) {
        try {
            Options.parseOption(entry, options, this);
        } catch (IllegalArgumentException e) {
            analyzer.error("Unrecognized %s value %s with attributes %s, expected values are %s", Constants.METATYPE_ANNOTATIONS_OPTIONS, entry.getKey(), entry.getValue(), EnumSet.allOf(Options.class));
        }
    }
    Map<TypeRef, OCDDef> classToOCDMap = new HashMap<TypeRef, OCDDef>();
    Set<String> ocdIds = new HashSet<String>();
    Set<String> pids = new HashSet<String>();
    Instructions instructions = new Instructions(header);
    XMLAttributeFinder finder = new XMLAttributeFinder(analyzer);
    List<Clazz> list = Create.list();
    for (Clazz c : analyzer.getClassspace().values()) {
        for (Instruction instruction : instructions.keySet()) {
            if (instruction.matches(c.getFQN())) {
                if (!instruction.isNegated()) {
                    list.add(c);
                    OCDDef definition = OCDReader.getOCDDef(c, analyzer, options, finder, minVersion);
                    if (definition != null) {
                        classToOCDMap.put(c.getClassName(), definition);
                    }
                }
                break;
            }
        }
    }
    // process Designate annotations after OCD annotations
    for (Clazz c : list) {
        DesignateReader.getDesignate(c, analyzer, classToOCDMap, finder);
    }
    for (Map.Entry<TypeRef, OCDDef> entry : classToOCDMap.entrySet()) {
        TypeRef c = entry.getKey();
        OCDDef definition = entry.getValue();
        definition.prepare(analyzer);
        if (!ocdIds.add(definition.id)) {
            analyzer.error("Duplicate OCD id %s from class %s; known ids %s", definition.id, c.getFQN(), ocdIds);
        }
        for (DesignateDef dDef : definition.designates) {
            if (dDef.pid != null && !pids.add(dDef.pid)) {
                analyzer.error("Duplicate pid %s from class %s", dDef.pid, c.getFQN());
            }
        }
        String name = "OSGI-INF/metatype/" + analyzer.validResourcePath(definition.id, "Invalid resource name") + ".xml";
        analyzer.getJar().putResource(name, new TagResource(definition.getTag()));
    }
    return false;
}
Also used : Parameters(aQute.bnd.header.Parameters) HashMap(java.util.HashMap) TagResource(aQute.bnd.component.TagResource) TypeRef(aQute.bnd.osgi.Descriptors.TypeRef) Attrs(aQute.bnd.header.Attrs) Instructions(aQute.bnd.osgi.Instructions) Instruction(aQute.bnd.osgi.Instruction) XMLAttributeFinder(aQute.bnd.xmlattribute.XMLAttributeFinder) Clazz(aQute.bnd.osgi.Clazz) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 8 with Instruction

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

the class DSAnnotations method analyzeJar.

public boolean analyzeJar(Analyzer analyzer) throws Exception {
    Parameters header = OSGiHeader.parseHeader(analyzer.getProperty(Constants.DSANNOTATIONS, "*"));
    if (header.size() == 0)
        return false;
    minVersion = AnnotationReader.V1_3;
    Parameters optionsHeader = OSGiHeader.parseHeader(analyzer.mergeProperties(Constants.DSANNOTATIONS_OPTIONS));
    EnumSet<Options> options = EnumSet.noneOf(Options.class);
    for (Map.Entry<String, Attrs> entry : optionsHeader.entrySet()) {
        try {
            Options.parseOption(entry, options, this);
        } catch (IllegalArgumentException e) {
            analyzer.error("Unrecognized %s value %s with attributes %s, expected values are %s", Constants.DSANNOTATIONS_OPTIONS, entry.getKey(), entry.getValue(), EnumSet.allOf(Options.class));
        }
    }
    // obsolete but backwards compatible, use the options instead
    if (Processor.isTrue(analyzer.getProperty("-dsannotations-inherit")))
        options.add(Options.inherit);
    if (Processor.isTrue(analyzer.getProperty("-ds-felix-extensions")))
        options.add(Options.felixExtensions);
    Instructions instructions = new Instructions(header);
    Collection<Clazz> list = analyzer.getClassspace().values();
    String sc = analyzer.getProperty(Constants.SERVICE_COMPONENT);
    List<String> names = new ArrayList<String>();
    if (sc != null && sc.trim().length() > 0)
        names.add(sc);
    TreeSet<String> provides = new TreeSet<String>();
    TreeSet<String> requires = new TreeSet<String>();
    Version maxVersion = AnnotationReader.V1_0;
    XMLAttributeFinder finder = new XMLAttributeFinder(analyzer);
    boolean componentProcessed = false;
    for (Clazz c : list) {
        for (Instruction instruction : instructions.keySet()) {
            if (instruction.matches(c.getFQN())) {
                if (instruction.isNegated())
                    break;
                ComponentDef definition = AnnotationReader.getDefinition(c, analyzer, options, finder, minVersion);
                if (definition != null) {
                    componentProcessed = true;
                    definition.sortReferences();
                    definition.prepare(analyzer);
                    String name = "OSGI-INF/" + analyzer.validResourcePath(definition.name, "Invalid component name") + ".xml";
                    names.add(name);
                    analyzer.getJar().putResource(name, new TagResource(definition.getTag()));
                    if (definition.service != null && !options.contains(Options.nocapabilities)) {
                        String[] objectClass = new String[definition.service.length];
                        for (int i = 0; i < definition.service.length; i++) {
                            Descriptors.TypeRef tr = definition.service[i];
                            objectClass[i] = tr.getFQN();
                        }
                        Arrays.sort(objectClass);
                        addServiceCapability(objectClass, provides);
                    }
                    if (!options.contains(Options.norequirements)) {
                        MergedRequirement serviceReqMerge = new MergedRequirement("osgi.service");
                        for (ReferenceDef ref : definition.references.values()) {
                            addServiceRequirement(ref, serviceReqMerge);
                        }
                        requires.addAll(serviceReqMerge.toStringList());
                    }
                    maxVersion = ComponentDef.max(maxVersion, definition.version);
                }
            }
        }
    }
    if (componentProcessed && (options.contains(Options.extender) || (maxVersion.compareTo(AnnotationReader.V1_3) >= 0))) {
        maxVersion = ComponentDef.max(maxVersion, AnnotationReader.V1_3);
        addExtenderRequirement(requires, maxVersion);
    }
    sc = Processor.append(names.toArray(new String[0]));
    analyzer.setProperty(Constants.SERVICE_COMPONENT, sc);
    updateHeader(analyzer, Constants.REQUIRE_CAPABILITY, requires);
    updateHeader(analyzer, Constants.PROVIDE_CAPABILITY, provides);
    return false;
}
Also used : Attrs(aQute.bnd.header.Attrs) ArrayList(java.util.ArrayList) Instruction(aQute.bnd.osgi.Instruction) XMLAttributeFinder(aQute.bnd.xmlattribute.XMLAttributeFinder) Version(aQute.bnd.version.Version) TreeSet(java.util.TreeSet) Clazz(aQute.bnd.osgi.Clazz) Descriptors(aQute.bnd.osgi.Descriptors) Parameters(aQute.bnd.header.Parameters) Instructions(aQute.bnd.osgi.Instructions) Map(java.util.Map)

Example 9 with Instruction

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

the class FileRepo method list.

public List<String> list(String regex) throws Exception {
    init();
    Instruction pattern = null;
    if (regex != null)
        pattern = new Instruction(regex);
    List<String> result = new ArrayList<String>();
    if (root == null) {
        if (reporter != null)
            reporter.error("FileRepo root directory is not set.");
    } else {
        File[] list = root.listFiles();
        if (list != null) {
            for (File f : list) {
                if (!f.isDirectory())
                    // ignore non-directories
                    continue;
                String fileName = f.getName();
                if (fileName.charAt(0) == '.')
                    // ignore hidden files
                    continue;
                if (pattern == null || pattern.matches(fileName))
                    result.add(fileName);
            }
        } else if (reporter != null)
            reporter.error("FileRepo root directory (%s) does not exist", root);
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Instruction(aQute.bnd.osgi.Instruction) File(java.io.File)

Example 10 with Instruction

use of aQute.bnd.osgi.Instruction in project sling by apache.

the class ModelsScannerPlugin method getClassesWithAnnotation.

/**
     * Get all classes that implement the given annotation via bnd Analyzer.
     * @param analyzer Analyzer
     * @param annotation Annotation
     * @return Class names
     */
private Collection<String> getClassesWithAnnotation(String annotationClassName, Analyzer analyzer) {
    SortedSet<String> classNames = new TreeSet<>();
    Collection<Clazz> clazzes = analyzer.getClassspace().values();
    Instruction instruction = new Instruction(annotationClassName);
    try {
        for (Clazz clazz : clazzes) {
            if (clazz.is(QUERY.ANNOTATED, instruction, analyzer)) {
                classNames.add(clazz.getClassName().getFQN());
            }
        }
    } catch (Exception ex) {
        reporter.exception(ex, "Error querying for classes with annotation: " + annotationClassName);
    }
    return classNames;
}
Also used : TreeSet(java.util.TreeSet) Clazz(aQute.bnd.osgi.Clazz) Instruction(aQute.bnd.osgi.Instruction)

Aggregations

Instruction (aQute.bnd.osgi.Instruction)13 Instructions (aQute.bnd.osgi.Instructions)6 Attrs (aQute.bnd.header.Attrs)5 Parameters (aQute.bnd.header.Parameters)5 File (java.io.File)5 Clazz (aQute.bnd.osgi.Clazz)4 Version (aQute.bnd.version.Version)4 HashSet (java.util.HashSet)4 TreeSet (java.util.TreeSet)4 Jar (aQute.bnd.osgi.Jar)3 RepositoryPlugin (aQute.bnd.service.RepositoryPlugin)3 Description (aQute.lib.getopt.Description)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 Domain (aQute.bnd.osgi.Domain)2 XMLAttributeFinder (aQute.bnd.xmlattribute.XMLAttributeFinder)2 SortedList (aQute.lib.collections.SortedList)2 HashMap (java.util.HashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 TagResource (aQute.bnd.component.TagResource)1