Search in sources :

Example 1 with Name

use of java.util.jar.Attributes.Name in project eureka by Netflix.

the class DiscoveryBuildInfo method getManifestAttribute.

String getManifestAttribute(String name, String defaultValue) {
    if (manifest == null) {
        return defaultValue;
    }
    Name attrName = new Name(name);
    Object value = manifest.getMainAttributes().get(attrName);
    return value == null ? defaultValue : value.toString();
}
Also used : Name(java.util.jar.Attributes.Name)

Example 2 with Name

use of java.util.jar.Attributes.Name in project bnd by bndtools.

the class Analyzer method mergeManifest.

/**
	 * Merge the existing manifest with the instructions but do not override
	 * existing properties.
	 * 
	 * @param manifest The manifest to merge with
	 * @throws IOException
	 */
public void mergeManifest(Manifest manifest) throws IOException {
    if (manifest != null) {
        Attributes attributes = manifest.getMainAttributes();
        for (Iterator<Object> i = attributes.keySet().iterator(); i.hasNext(); ) {
            Name name = (Name) i.next();
            String key = name.toString();
            // Dont want instructions
            if (key.startsWith("-"))
                continue;
            if (getProperty(key) == null)
                setProperty(key, attributes.getValue(name));
        }
    }
}
Also used : Attributes(java.util.jar.Attributes) Name(java.util.jar.Attributes.Name)

Example 3 with Name

use of java.util.jar.Attributes.Name in project bnd by bndtools.

the class bnd method _grep.

@Description("Grep the manifest of bundles/jar files. ")
public void _grep(grepOptions opts) throws Exception {
    List<String> args = opts._arguments();
    String s = args.remove(0);
    Pattern pattern = Glob.toPattern(s);
    if (pattern == null) {
        messages.InvalidGlobPattern_(s);
        return;
    }
    if (args.isEmpty()) {
        args = new ExtList<String>(getBase().list(new FilenameFilter() {

            public boolean accept(File dir, String name) {
                return name.endsWith(".jar");
            }
        }));
    }
    Set<String> headers = opts.headers();
    if (headers == null)
        headers = new TreeSet<String>();
    if (opts.exports())
        headers.add(Constants.EXPORT_PACKAGE);
    if (opts.bsn())
        headers.add(Constants.BUNDLE_SYMBOLICNAME);
    if (opts.imports())
        headers.add(Constants.IMPORT_PACKAGE);
    Instructions instructions = new Instructions(headers);
    for (String fileName : args) {
        File file = getFile(fileName);
        if (!file.isFile()) {
            messages.NoSuchFile_(file);
            continue;
        }
        try (JarInputStream in = new JarInputStream(IO.stream(file))) {
            Manifest m = in.getManifest();
            for (Object header : m.getMainAttributes().keySet()) {
                Attributes.Name name = (Name) header;
                if (instructions.isEmpty() || instructions.matches(name.toString())) {
                    String h = m.getMainAttributes().getValue(name);
                    QuotedTokenizer qt = new QuotedTokenizer(h, ",;=");
                    for (String value : qt.getTokenSet()) {
                        Matcher matcher = pattern.matcher(value);
                        while (matcher.find()) {
                            int start = matcher.start() - 8;
                            if (start < 0)
                                start = 0;
                            int end = matcher.end() + 8;
                            if (end > value.length())
                                end = value.length();
                            out.printf("%40s : %20s ...%s[%s]%s...\n", fileName, name, value.substring(start, matcher.start()), value.substring(matcher.start(), matcher.end()), value.substring(matcher.end(), end));
                        }
                    }
                }
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) QuotedTokenizer(aQute.libg.qtokens.QuotedTokenizer) JarInputStream(java.util.jar.JarInputStream) Matcher(java.util.regex.Matcher) Attributes(java.util.jar.Attributes) Instructions(aQute.bnd.osgi.Instructions) Name(java.util.jar.Attributes.Name) Manifest(java.util.jar.Manifest) PomFromManifest(aQute.bnd.maven.PomFromManifest) Name(java.util.jar.Attributes.Name) FilenameFilter(java.io.FilenameFilter) TreeSet(java.util.TreeSet) File(java.io.File) Description(aQute.lib.getopt.Description)

Example 4 with Name

use of java.util.jar.Attributes.Name in project dex2jar by pxb1988.

the class InvocationWeaver method wave.

public void wave(Path from, final Path to) throws IOException {
    BaseCmd.walkJarOrDir(from, new BaseCmd.FileVisitorX() {

        @Override
        public void visitFile(Path file, String relative) throws IOException {
            String name = relative;
            Path targetPath = to.resolve(relative);
            BaseCmd.createParentDirectories(targetPath);
            if (name.endsWith(".class")) {
                String clzName = name.substring(0, name.length() - ".class".length());
                if (ignores.contains(clzName)) {
                    Files.copy(file, targetPath);
                } else {
                    byte[] out = wave0(Files.readAllBytes(file));
                    Files.write(targetPath, out);
                }
            } else {
                if (name.startsWith("META-INF/")) {
                    if (name.equals(JarFile.MANIFEST_NAME)) {
                        try (InputStream in = Files.newInputStream(file)) {
                            Manifest mf = new Manifest(in);
                            mf.getMainAttributes().put(new Name("X-NOTICE"), "Modified");
                            mf.getEntries().clear();
                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
                            mf.write(baos);
                            baos.flush();
                            Files.write(targetPath, baos.toByteArray());
                        }
                    } else if (name.endsWith(".DSA") || name.endsWith(".RSA") || name.endsWith(".SF") || name.endsWith(".ECDSA")) {
                    // ignored
                    } else {
                        Files.copy(file, targetPath);
                    }
                } else {
                    Files.copy(file, targetPath);
                }
            }
        }
    });
    if (callbacks.size() > 0) {
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        String type = buildInvocationClz(cw);
        byte[] data = cw.toByteArray();
        Path target = to.resolve(type + ".class");
        BaseCmd.createParentDirectories(target);
        Files.write(target, data);
        nextInvocationName();
    }
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Manifest(java.util.jar.Manifest) BaseCmd(com.googlecode.dex2jar.tools.BaseCmd) Name(java.util.jar.Attributes.Name)

Example 5 with Name

use of java.util.jar.Attributes.Name in project bnd by bndtools.

the class Analyzer method getManifest.

/**
	 * Specifically for Maven
	 * 
	 */
public static Properties getManifest(File dirOrJar) throws Exception {
    try (Analyzer analyzer = new Analyzer()) {
        analyzer.setJar(dirOrJar);
        Properties properties = new UTF8Properties();
        properties.put(IMPORT_PACKAGE, "*");
        properties.put(EXPORT_PACKAGE, "*");
        analyzer.setProperties(properties);
        Manifest m = analyzer.calcManifest();
        Properties result = new UTF8Properties();
        for (Iterator<Object> i = m.getMainAttributes().keySet().iterator(); i.hasNext(); ) {
            Attributes.Name name = (Attributes.Name) i.next();
            result.put(name.toString(), m.getMainAttributes().getValue(name));
        }
        return result;
    }
}
Also used : Attributes(java.util.jar.Attributes) Name(java.util.jar.Attributes.Name) UTF8Properties(aQute.lib.utf8properties.UTF8Properties) Properties(java.util.Properties) Manifest(java.util.jar.Manifest) UTF8Properties(aQute.lib.utf8properties.UTF8Properties) Name(java.util.jar.Attributes.Name)

Aggregations

Name (java.util.jar.Attributes.Name)6 Attributes (java.util.jar.Attributes)4 Manifest (java.util.jar.Manifest)3 PomFromManifest (aQute.bnd.maven.PomFromManifest)1 Instructions (aQute.bnd.osgi.Instructions)1 MultiMap (aQute.lib.collections.MultiMap)1 Description (aQute.lib.getopt.Description)1 UTF8Properties (aQute.lib.utf8properties.UTF8Properties)1 QuotedTokenizer (aQute.libg.qtokens.QuotedTokenizer)1 BaseCmd (com.googlecode.dex2jar.tools.BaseCmd)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FilenameFilter (java.io.FilenameFilter)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Properties (java.util.Properties)1