Java ClassLoader searchByAttributeKey(String key)

Here you can find the source of searchByAttributeKey(String key)

Description

search By Attribute Key

License

Apache License

Return

manifests that contains a main attribute with the specified key

Declaration

public static List<Manifest> searchByAttributeKey(String key) 

Method Source Code


//package com.java2s;
/*//from w ww  .  j ava2  s  .c  om
 *    Copyright 2015 Textocat
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

import com.google.common.collect.Lists;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

public class Main {
    /**
     * @return manifests that contains a main attribute with the specified key
     */
    public static List<Manifest> searchByAttributeKey(String key) {
        try {
            Enumeration<URL> resEnum = Thread.currentThread().getContextClassLoader()
                    .getResources(JarFile.MANIFEST_NAME);
            List<Manifest> resultList = Lists.newLinkedList();
            while (resEnum.hasMoreElements()) {
                URL url = resEnum.nextElement();
                try (InputStream is = url.openStream()) {
                    Manifest manifest = new Manifest(is);
                    Attributes mainAttribs = manifest.getMainAttributes();
                    if (mainAttribs.getValue(key) != null) {
                        resultList.add(manifest);
                    }
                }
            }
            return resultList;
        } catch (IOException e) {
            throw new IllegalStateException("manifest lookup error", e);
        }
    }
}

Related

  1. read(String fileName)
  2. readFromFile(Object obj, String fileName)
  3. readSampleJson(String name)
  4. saveProperties(Properties properties, String name)
  5. scanPackage(String path)
  6. storePropertiesFile(String fileName, String filePath, String storeComment)
  7. toTypeArray(String[] s)