Example usage for java.util.jar Attributes putValue

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

Introduction

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

Prototype

public String putValue(String name, String value) 

Source Link

Document

Associates the specified value with the specified attribute name, specified as a String.

Usage

From source file:com.taobao.android.builder.tools.sign.LocalSignedJarBuilder.java

/**
 * Creates a {@link SignedJarBuilder} with a given output stream, and signing information.
 * <p/>If either <code>key</code> or <code>certificate</code> is <code>null</code> then
 * the archive will not be signed.//from www .  j av a2  s  .c o  m
 *
 * @param out         the {@link OutputStream} where to write the Jar archive.
 * @param key         the {@link PrivateKey} used to sign the archive, or <code>null</code>.
 * @param certificate the {@link X509Certificate} used to sign the archive, or
 *                    <code>null</code>.
 * @throws IOException
 * @throws NoSuchAlgorithmException
 */
public LocalSignedJarBuilder(@NonNull OutputStream out, @Nullable PrivateKey key,
        @Nullable X509Certificate certificate, @Nullable String builtBy, @Nullable String createdBy,
        @Nullable String signFile) throws IOException, NoSuchAlgorithmException {
    mOutputJar = new JarOutputStream(new BufferedOutputStream(out));
    mOutputJar.setLevel(9);
    mKey = key;
    mCertificate = certificate;
    mSignFile = signFile;

    if (mKey != null && mCertificate != null) {
        mManifest = new Manifest();
        Attributes main = mManifest.getMainAttributes();
        main.putValue("Manifest-Version", "1.0");
        if (builtBy != null) {
            main.putValue("Built-By", builtBy);
        }
        if (createdBy != null) {
            main.putValue("Created-By", createdBy);
        }

        mMessageDigest = MessageDigest.getInstance(DIGEST_ALGORITHM);
    }
}

From source file:com.taobao.android.builder.tools.sign.LocalSignedJarBuilder.java

/**
 * Writes a .SF file with a digest to the manifest.
 *///from  www.j  a  v a 2  s. c om
private void writeSignatureFile(OutputStream out) throws IOException, GeneralSecurityException {
    Manifest sf = new Manifest();
    Attributes main = sf.getMainAttributes();
    main.putValue("Signature-Version", "1.0");
    main.putValue("Created-By", "1.0 (Android)");

    MessageDigest md = MessageDigest.getInstance(DIGEST_ALGORITHM);
    PrintStream print = new PrintStream(new DigestOutputStream(new ByteArrayOutputStream(), md), true,
            SdkConstants.UTF_8);

    // Digest of the entire manifest
    mManifest.write(print);
    print.flush();
    main.putValue(DIGEST_MANIFEST_ATTR, new String(Base64.encode(md.digest()), "ASCII"));

    Map<String, Attributes> entries = mManifest.getEntries();
    for (Map.Entry<String, Attributes> entry : entries.entrySet()) {
        // Digest of the manifest stanza for this entry.
        print.print("Name: " + entry.getKey() + "\r\n");
        for (Map.Entry<Object, Object> att : entry.getValue().entrySet()) {
            print.print(att.getKey() + ": " + att.getValue() + "\r\n");
        }
        print.print("\r\n");
        print.flush();

        Attributes sfAttr = new Attributes();
        sfAttr.putValue(DIGEST_ATTR, new String(Base64.encode(md.digest()), "ASCII"));
        sf.getEntries().put(entry.getKey(), sfAttr);
    }
    CountOutputStream cout = new CountOutputStream(out);
    sf.write(cout);

    // A bug in the java.util.jar implementation of Android platforms
    // up to version 1.6 will cause a spurious IOException to be thrown
    // if the length of the signature file is a multiple of 1024 bytes.
    // As a workaround, add an extra CRLF in this case.
    if ((cout.size() % 1024) == 0) {
        cout.write('\r');
        cout.write('\n');
    }
}

From source file:com.github.sampov2.OneJarMojo.java

private void addExplicitManifestEntries(Attributes mainAttributes) {
    // add explicitly specified manifest entries
    if (manifestEntries != null) {
        for (Entry<String, String> entry : manifestEntries.entrySet()) {
            debug("adding entry [%s:%s] to the one-jar manifest", entry.getKey(), entry.getValue());
            mainAttributes.putValue(entry.getKey(), entry.getValue());
        }//from   w  ww  . jav  a  2 s  .c o m
    }
}

From source file:com.taobao.android.builder.tools.sign.LocalSignedJarBuilder.java

/**
 * Adds an entry to the output jar, and write its content from the {@link InputStream}
 *
 * @param input The input stream from where to write the entry content.
 * @param entry the entry to write in the jar.
 * @throws IOException/*from ww w . j  a  v a  2  s .c om*/
 */
private void writeEntry(InputStream input, JarEntry entry) throws IOException {
    // add the entry to the jar archive
    mOutputJar.putNextEntry(entry);

    // read the content of the entry from the input stream, and write it into the archive.
    int count;
    while ((count = input.read(mBuffer)) != -1) {
        mOutputJar.write(mBuffer, 0, count);

        // update the digest
        if (mMessageDigest != null) {
            mMessageDigest.update(mBuffer, 0, count);
        }
    }

    // close the entry for this file
    mOutputJar.closeEntry();

    if (mManifest != null) {
        // update the manifest for this entry.
        Attributes attr = mManifest.getAttributes(entry.getName());
        if (attr == null) {
            attr = new Attributes();
            mManifest.getEntries().put(entry.getName(), attr);
        }
        attr.putValue(DIGEST_ATTR, new String(Base64.encode(mMessageDigest.digest()), "ASCII"));
    }
}

From source file:com.github.sampov2.OneJarMojo.java

/**
  * Adds option to manifest entries, if applicable.
  * @param optionToCheck to perform null check, if null then nothing added
  * @param mainAttributes to add to/*  ww w .j  a  v a 2s  .  c o m*/
  * @param keyPairToSet both the key and value to add if applicable
  * @param entryNamesForValue keys to look for to prevent duplicating or overwriting keys
  */
private void setOptional(Object optionToCheck, Attributes mainAttributes, AttributeEntry keyPairToSet,
        String... entryNamesForValue) {
    if (optionToCheck != null) {
        for (String keyToCheck : entryNamesForValue) {
            if (mainAttributes.containsKey(keyToCheck)) {
                // key is already set, don't override
                return;
            }
        }
        // if key not found, add it
        mainAttributes.putValue(keyPairToSet.getKey(), keyPairToSet.getValue());
    }
}

From source file:com.aimluck.eip.msgboard.MsgboardTopicSelectData.java

/**
 * @return//from w  w w  .  j  av  a 2 s .co m
 *
 */
@Override
protected Attributes getColumnMap() {
    Attributes map = new Attributes();
    map.putValue("topic_name", EipTMsgboardTopic.TOPIC_NAME_PROPERTY);
    map.putValue("update_date", EipTMsgboardTopic.UPDATE_DATE_PROPERTY);
    map.putValue("category", EipTMsgboardCategory.CATEGORY_ID_PK_COLUMN);
    map.putValue("category_name", EipTMsgboardTopic.EIP_TMSGBOARD_CATEGORY_PROPERTY + "."
            + EipTMsgboardCategory.CATEGORY_NAME_PROPERTY);
    map.putValue("owner_name", EipTMsgboardTopic.OWNER_ID_PROPERTY);
    map.putValue("update_user", EipTMsgboardTopic.UPDATE_USER_ID_PROPERTY);

    return map;
}

From source file:com.aimluck.eip.project.ProjectTaskSimpleSelectData.java

/**
 * ??/*w  w  w .  j  a  v a2s .c  o  m*/
 * 
 * @return 
 */
@Override
protected Attributes getColumnMap() {
    Attributes map = new Attributes();
    map.putValue("task_name", "task_name");
    map.putValue("progress_rate", "progress_rate");
    return map;
}

From source file:com.android.builder.internal.packaging.sign.SignatureExtension.java

/**
 * Adds / updates the signature for an entry. If this entry has no signature, or its digest
 * doesn't match the one in the signature file (or manifest), it will be updated.
 *
 * @param entry the entry/*from   w w  w .  j ava  2  s.c o m*/
 * @throws IOException failed to compute the entry's digest
 */
private void setDigestForEntry(@NonNull StoredEntry entry) throws IOException {
    String entryName = entry.getCentralDirectoryHeader().getName();
    byte[] entryDigestArray = mMessageDigest.digest(entry.read());
    String entryDigest = new String(Base64.encodeBase64(entryDigestArray), Charsets.US_ASCII);

    Attributes signatureAttributes = mSignatureFile.getEntries().get(entryName);
    if (signatureAttributes == null) {
        signatureAttributes = new Attributes();
        mSignatureFile.getEntries().put(entryName, signatureAttributes);
        mDirty = true;
    }

    if (!entryDigest.equals(signatureAttributes.getValue(mDigestAlgorithm.entryAttributeName))) {
        signatureAttributes.putValue(mDigestAlgorithm.entryAttributeName, entryDigest);
        mDirty = true;
    }

    /*
     * setAttribute will not mark the manifest as changed if the attribute is already there
     * and with the same value.
     */
    mManifestExtension.setAttribute(entryName, mDigestAlgorithm.entryAttributeName, entryDigest);
}

From source file:com.aimluck.eip.project.ProjectTaskSelectData.java

/**
 * ??//from  w ww  .jav  a 2  s.  c om
 *
 * @return 
 */
@Override
protected Attributes getColumnMap() {
    Attributes map = new Attributes();
    map.putValue("task_name", "task_name");
    map.putValue("tracker", "tracker");
    map.putValue("status", "status");
    map.putValue("workload", "workload");
    map.putValue("plan_workload", "plan_workload");
    map.putValue("start_plan_date", "start_plan_date");
    map.putValue("end_plan_date", "end_plan_date");
    map.putValue("progress_rate", "progress_rate");
    return map;
}

From source file:com.taobao.android.tools.TPatchTool.java

/**
 * create manifest for patch file/*from w w w. j  a v  a  2  s . co m*/
 *
 * @return
 */
private Manifest createManifest() {
    Manifest manifest = new Manifest();
    Attributes main = manifest.getMainAttributes();
    main.putValue("Manifest-Version", "1.0");
    main.putValue("Created-By", "1.0 (DexPatch)");
    main.putValue("Created-Time", new Date(System.currentTimeMillis()).toGMTString());
    return manifest;
}