Example usage for java.util.jar Attributes getValue

List of usage examples for java.util.jar Attributes getValue

Introduction

In this page you can find the example usage for java.util.jar Attributes getValue.

Prototype

public String getValue(Name name) 

Source Link

Document

Returns the value of the specified Attributes.Name, or null if the attribute was not found.

Usage

From source file:org.thiesen.ant.git.ExtractGitInfo.java

private String loadVersion() {
    try {/*from   w  w  w  .j a va 2s.  c o m*/
        final Enumeration<URL> resources = getClass().getClassLoader().getResources("META-INF/MANIFEST.MF");
        while (resources.hasMoreElements()) {

            final Manifest manifest = new Manifest(resources.nextElement().openStream());

            final Attributes mainAttributes = manifest.getMainAttributes();

            if ("gitant".equalsIgnoreCase(mainAttributes.getValue("Project-Name"))) {
                return mainAttributes.getValue("Project-Version");
            }

        }

    } catch (final IOException E) {
        // do nothing
    }
    return "unknown version";

}

From source file:se.crisp.codekvast.agent.daemon.appversion.ManifestAppVersionStrategy.java

@Override
public String resolveAppVersion(Collection<File> codeBases, String[] args) {
    String jarUri = args[1];/*  w  w w .  java 2 s  .c  om*/
    String manifestAttribute = args.length > 2 ? args[2] : DEFAULT_MANIFEST_ATTRIBUTE;
    for (File codeBaseFile : codeBases) {
        try (JarFile jarFile = new JarFile(getJarFile(codeBaseFile, jarUri))) {
            Attributes attributes = jarFile.getManifest().getMainAttributes();
            String resolvedVersion = attributes.getValue(manifestAttribute);
            if (resolvedVersion != null) {
                log.debug("{}!/META-INF/MANIFEST.MF:{}={}", jarUri, manifestAttribute, resolvedVersion);
                return resolvedVersion;
            }
            if (!manifestAttribute.equalsIgnoreCase(DEFAULT_MANIFEST_ATTRIBUTE)) {
                resolvedVersion = attributes.getValue(DEFAULT_MANIFEST_ATTRIBUTE);
            }
            if (resolvedVersion != null) {
                log.debug("{}!/META-INF/MANIFEST.MF:{}={}", jarUri, DEFAULT_MANIFEST_ATTRIBUTE,
                        resolvedVersion);
                return resolvedVersion;
            }
        } catch (Exception e) {
            log.error("Cannot open " + jarUri + ": " + e);
        }
    }
    log.error("Cannot resolve {}!/META-INF/MANIFEST.MF:{}", jarUri, manifestAttribute);
    return UNKNOWN_VERSION;
}

From source file:org.thiesen.ant.git.ExtractGitInfo.java

private String loadJGitVersion() {
    try {/* w  w  w  .  j  av a2s . c om*/
        final Enumeration<URL> resources = getClass().getClassLoader().getResources("META-INF/MANIFEST.MF");
        while (resources.hasMoreElements()) {

            final Manifest manifest = new Manifest(resources.nextElement().openStream());

            final Attributes mainAttributes = manifest.getMainAttributes();

            if ("org.eclipse.jgit".equalsIgnoreCase(mainAttributes.getValue("Bundle-SymbolicName"))) {
                return mainAttributes.getValue("Implementation-Title") + " "
                        + mainAttributes.getValue("Implementation-Version");
            }

        }

    } catch (final IOException E) {
        // do nothing
    }
    return "unknown version";

}

From source file:org.craftercms.commons.monitoring.VersionMonitor.java

private void initFromManifest(Attributes mainAttrs) {

    if (mainAttrs.getValue(BUILD_ON) != null) {
        build_date = DATETIME_FORMATTER.format(new Date(Long.parseLong(mainAttrs.getValue(BUILD_ON))));
    } else {//from   w w  w .  j  a va  2  s .co m
        build_date = DATETIME_FORMATTER.format(new Date(0));
    }
    name = mainAttrs.getValue(IMPLEMENTATION_TITLE);
    packageVersion = mainAttrs.getValue(IMPLEMENTATION_VERSION);
    build = mainAttrs.getValue(IMPLEMENTATION_BUILD);

}

From source file:org.wisdom.maven.mojos.WebJarPackagerTest.java

@Test
public void testDefaultPackaging() throws MojoExecutionException, IOException {
    WebJarPackager packager = new WebJarPackager();
    packager.project = mock(MavenProject.class);
    packager.projectHelper = mock(MavenProjectHelper.class);
    when(packager.project.getArtifactId()).thenReturn("test");
    when(packager.project.getVersion()).thenReturn("1.1");
    when(packager.project.getBasedir()).thenReturn(fake);
    packager.buildDirectory = new File("target/junk");
    copy();//  ww w . ja  v  a  2  s . c om
    packager.packageWebJar = true;
    packager.deployWebJarToWisdom = true;
    packager.execute();
    final File wj = new File(packager.buildDirectory, "test-1.1-webjar.jar");
    assertThat(wj).isFile();
    JarFile jar = new JarFile(wj);
    assertThat(jar.getEntry(WebJarPackager.ROOT + "test/1.1/less/style.less")).isNotNull();
    assertThat(jar.getEntry(WebJarPackager.ROOT + "test/1.1/missing")).isNull();
    assertThat(jar.getEntry(WebJarPackager.ROOT + "test/1.1/coffee/script.coffee")).isNotNull();
    Attributes attributes = jar.getManifest().getMainAttributes();
    assertThat(attributes.getValue("Webjar-Name")).isEqualTo("test");
    assertThat(attributes.getValue("Webjar-Version")).isEqualTo("1.1");
}

From source file:org.wisdom.maven.mojos.WebJarPackagerTest.java

@Test
public void testNameVersionAndClassifierCustomization() throws MojoExecutionException, IOException {
    WebJarPackager packager = new WebJarPackager();
    packager.project = mock(MavenProject.class);
    packager.projectHelper = mock(MavenProjectHelper.class);
    when(packager.project.getArtifactId()).thenReturn("test");
    when(packager.project.getVersion()).thenReturn("1.0");
    when(packager.project.getBasedir()).thenReturn(fake);
    packager.buildDirectory = new File("target/junk");
    copy();/*from ww w . ja  v a 2 s .com*/
    packager.webjar = new WebJar();
    packager.webjar.setName("library");
    packager.webjar.setVersion("2.0");
    packager.webjar.setClassifier("wb");
    packager.execute();
    final File wj = new File(packager.buildDirectory, "library-2.0-wb.jar");
    assertThat(wj).isFile();
    JarFile jar = new JarFile(wj);
    assertThat(jar.getEntry(WebJarPackager.ROOT + "library/2.0/missing")).isNull();
    assertThat(jar.getEntry(WebJarPackager.ROOT + "library/2.0/coffee/script.coffee")).isNotNull();
    assertThat(jar.getEntry(WebJarPackager.ROOT + "library/2.0/less/style.less")).isNotNull();
    Attributes attributes = jar.getManifest().getMainAttributes();
    assertThat(attributes.getValue("Webjar-Name")).isEqualTo("library");
    assertThat(attributes.getValue("Webjar-Version")).isEqualTo("2.0");
}

From source file:com.ikon.util.cl.BinaryClassLoader.java

/**
 * Create internal classes and resources cache
 *//*from w  w  w . ja v  a  2  s. c  o  m*/
private void createCache(byte[] buf) throws IOException {
    ByteArrayInputStream bais = null;
    JarInputStream jis = null;
    byte[] buffer = new byte[1024 * 4];

    try {
        bais = new ByteArrayInputStream(buf);
        jis = new JarInputStream(bais);
        Attributes attr = jis.getManifest().getMainAttributes();
        mainClassName = attr != null ? attr.getValue(Attributes.Name.MAIN_CLASS) : null;

        for (JarEntry entry = null; (entry = jis.getNextJarEntry()) != null;) {
            String name = entry.getName();

            if (!entry.isDirectory()) {
                ByteArrayOutputStream byteStream = new ByteArrayOutputStream();

                for (int n = 0; -1 != (n = jis.read(buffer));) {
                    byteStream.write(buffer, 0, n);
                }

                if (name.endsWith(".class")) {
                    String className = name.substring(0, name.indexOf('.')).replace('/', '.');
                    resources.put(className, byteStream.toByteArray());
                } else {
                    resources.put(name, byteStream.toByteArray());
                }

                byteStream.close();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(jis);
        IOUtils.closeQuietly(bais);
    }
}

From source file:jp.gr.java_conf.schkit.utils.ApplicationInfo.java

private void loadManifest() throws Exception {
    if (classLoader == null)
        return;/*from  w  w w  . j  av a 2  s.c  om*/

    URL resource = classLoader.findResource(MANIFEST_PATH);
    if (resource == null)
        return;

    Manifest manifest = new Manifest(resource.openStream());

    Attributes attrs = manifest.getMainAttributes();

    String value;
    value = attrs.getValue("Implementation-Title");
    if (value != null)
        name = value;
    value = attrs.getValue("Implementation-Version");
    if (value != null)
        version = value;
    value = attrs.getValue("Create-By");
    if (value != null)
        author = value;
    value = attrs.getValue("License-By");
    if (value != null)
        license = value;

    if (classLotation != null) {
        Date date = new Date(classLotation.openConnection().getLastModified());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        modified = sdf.format(date);
    }
}

From source file:com.datatorrent.stram.client.ConfigPackage.java

/**
 * Creates an Config Package object.//from  w w  w  .j a  v  a  2s  . c  om
 *
 * @param file
 * @throws java.io.IOException
 * @throws net.lingala.zip4j.exception.ZipException
 */
public ConfigPackage(File file) throws IOException, ZipException {
    super(file);
    Manifest manifest = getManifest();
    if (manifest == null) {
        throw new IOException("Not a valid config package. MANIFEST.MF is not present.");
    }
    Attributes attr = manifest.getMainAttributes();
    configPackageName = attr.getValue(ATTRIBUTE_DT_CONF_PACKAGE_NAME);
    appPackageName = attr.getValue(ATTRIBUTE_DT_APP_PACKAGE_NAME);
    appPackageGroupId = attr.getValue(ATTRIBUTE_DT_APP_PACKAGE_GROUP_ID);
    appPackageMinVersion = attr.getValue(ATTRIBUTE_DT_APP_PACKAGE_MIN_VERSION);
    appPackageMaxVersion = attr.getValue(ATTRIBUTE_DT_APP_PACKAGE_MAX_VERSION);
    configPackageDescription = attr.getValue(ATTRIBUTE_DT_CONF_PACKAGE_DESCRIPTION);
    String classPathString = attr.getValue(ATTRIBUTE_CLASS_PATH);
    String filesString = attr.getValue(ATTRIBUTE_FILES);
    if (configPackageName == null) {
        throw new IOException("Not a valid config package.  DT-Conf-Package-Name is missing from MANIFEST.MF");
    }
    if (!StringUtils.isBlank(classPathString)) {
        classPath.addAll(Arrays.asList(StringUtils.split(classPathString, " ")));
    }
    if (!StringUtils.isBlank(filesString)) {
        files.addAll(Arrays.asList(StringUtils.split(filesString, " ")));
    }

    ZipFile zipFile = new ZipFile(file);
    if (zipFile.isEncrypted()) {
        throw new ZipException("Encrypted conf package not supported yet");
    }
    File newDirectory = Files.createTempDirectory("dt-configPackage-").toFile();
    newDirectory.mkdirs();
    directory = newDirectory.getAbsolutePath();
    zipFile.extractAll(directory);
    processPropertiesXml();
}

From source file:org.musicrecital.webapp.listener.StartupListener.java

/**
 * {@inheritDoc}/*from  ww  w .  j  a v a 2 s .  c om*/
 */
@SuppressWarnings("unchecked")
public void contextInitialized(ServletContextEvent event) {
    log.debug("Initializing context...");

    ServletContext context = event.getServletContext();

    // Orion starts Servlets before Listeners, so check if the config
    // object already exists
    Map<String, Object> config = (HashMap<String, Object>) context.getAttribute(Constants.CONFIG);

    if (config == null) {
        config = new HashMap<String, Object>();
    }

    ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);

    PasswordEncoder passwordEncoder = null;
    try {
        ProviderManager provider = (ProviderManager) ctx
                .getBean("org.springframework.security.authentication.ProviderManager#0");
        for (Object o : provider.getProviders()) {
            AuthenticationProvider p = (AuthenticationProvider) o;
            if (p instanceof RememberMeAuthenticationProvider) {
                config.put("rememberMeEnabled", Boolean.TRUE);
            } else if (ctx.getBean("passwordEncoder") != null) {
                passwordEncoder = (PasswordEncoder) ctx.getBean("passwordEncoder");
            }
        }
    } catch (NoSuchBeanDefinitionException n) {
        log.debug("authenticationManager bean not found, assuming test and ignoring...");
        // ignore, should only happen when testing
    }

    context.setAttribute(Constants.CONFIG, config);

    // output the retrieved values for the Init and Context Parameters
    if (log.isDebugEnabled()) {
        log.debug("Remember Me Enabled? " + config.get("rememberMeEnabled"));
        if (passwordEncoder != null) {
            log.debug("Password Encoder: " + passwordEncoder.getClass().getSimpleName());
        }
        log.debug("Populating drop-downs...");
    }

    setupContext(context);

    // Determine version number for CSS and JS Assets
    String appVersion = null;
    try {
        InputStream is = context.getResourceAsStream("/META-INF/MANIFEST.MF");
        if (is == null) {
            log.warn("META-INF/MANIFEST.MF not found.");
        } else {
            Manifest mf = new Manifest();
            mf.read(is);
            Attributes atts = mf.getMainAttributes();
            appVersion = atts.getValue("Implementation-Version");
        }
    } catch (IOException e) {
        log.error("I/O Exception reading manifest: " + e.getMessage());
    }

    // If there was a build number defined in the war, then use it for
    // the cache buster. Otherwise, assume we are in development mode
    // and use a random cache buster so developers don't have to clear
    // their browser cache.
    if (appVersion == null || appVersion.contains("SNAPSHOT")) {
        appVersion = "" + new Random().nextInt(100000);
    }

    log.info("Application version set to: " + appVersion);
    context.setAttribute(Constants.ASSETS_VERSION, appVersion);
}