samples/messagedigest/src-bridge/org/apidesign/impl/security/extension/BridgeToNew.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:56:12 +0200
changeset 132 3bc4c54f4bcc
parent 49 294a39765b0b
child 153 b5cbb797ec0a
permissions -rw-r--r--
Truncating all examples to 80 characters per line
jtulach@46
     1
package org.apidesign.impl.security.extension;
jtulach@46
     2
jtulach@46
     3
import java.nio.ByteBuffer;
jtulach@46
     4
import java.security.MessageDigest;
jtulach@46
     5
import java.security.NoSuchAlgorithmException;
jtulach@46
     6
import java.util.logging.Level;
jtulach@46
     7
import java.util.logging.Logger;
jtulach@47
     8
import org.apidesign.spi.security.Digestor;
jtulach@46
     9
jtulach@46
    10
/**
jtulach@46
    11
 *
jtulach@46
    12
 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jtulach@46
    13
 */
jtulach@46
    14
// BEGIN: day.end.bridges.BridgeToNew
jtulach@47
    15
public class BridgeToNew extends Digestor<MessageDigest> {
jtulach@132
    16
    /** initializes the other bridge, and allow us to eliminate stack 
jtulach@132
    17
     * overflow */
jtulach@48
    18
    private static final BridgeToOld oldBridge = new BridgeToOld();
jtulach@48
    19
    
jtulach@47
    20
    @Override
jtulach@47
    21
    protected MessageDigest create(String algorithm) {
jtulach@49
    22
        // BEGIN: day.end.bridges.cyclecheck
jtulach@48
    23
        if (oldBridge.isSearching()) {
jtulach@49
    24
            // if the call is initiated from the other bridge, do not do
jtulach@49
    25
            // any delegation
jtulach@48
    26
            return null;
jtulach@48
    27
        }
jtulach@49
    28
        // END: day.end.bridges.cyclecheck
jtulach@48
    29
        
jtulach@46
    30
        try {
jtulach@47
    31
            return MessageDigest.getInstance(algorithm);
jtulach@46
    32
        } catch (NoSuchAlgorithmException ex) {
jtulach@132
    33
            Logger.getLogger(BridgeToNew.class.getName()).log(
jtulach@132
    34
                Level.FINE, "Cannot find " + algorithm, ex
jtulach@132
    35
            );
jtulach@46
    36
            return null;
jtulach@46
    37
        }
jtulach@46
    38
    }
jtulach@47
    39
jtulach@47
    40
    @Override
jtulach@47
    41
    protected byte[] digest(MessageDigest data) {
jtulach@47
    42
        return data.digest();
jtulach@47
    43
    }
jtulach@47
    44
jtulach@47
    45
    @Override
jtulach@47
    46
    protected void update(MessageDigest data, ByteBuffer input) {
jtulach@47
    47
        data.update(input);
jtulach@47
    48
    }
jtulach@48
    49
    
jtulach@48
    50
    static {
jtulach@48
    51
        new BridgeToOld();
jtulach@48
    52
    }
jtulach@46
    53
}
jtulach@46
    54
// END: day.end.bridges.BridgeToNew