samples/messagedigest/src-bridge/org/apidesign/impl/security/extension/BridgeToOld.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:52:23 +0200
changeset 46 c75861f07646
parent 44 716af5f2ebd1
child 48 c5742322dbc8
permissions -rw-r--r--
Using real pieces of code the talk about bridges
jtulach@44
     1
/*
jtulach@44
     2
 * To change this template, choose Tools | Templates
jtulach@44
     3
 * and open the template in the editor.
jtulach@44
     4
 */
jtulach@44
     5
jtulach@44
     6
package org.apidesign.impl.security.extension;
jtulach@44
     7
jtulach@44
     8
import org.apidesign.api.security.Digest;
jtulach@44
     9
import java.nio.ByteBuffer;
jtulach@44
    10
import java.security.MessageDigest;
jtulach@44
    11
import java.security.NoSuchAlgorithmException;
jtulach@44
    12
import java.security.Provider;
jtulach@44
    13
import java.security.Security;
jtulach@44
    14
import java.util.Collections;
jtulach@44
    15
import java.util.List;
jtulach@44
    16
import java.util.Map;
jtulach@44
    17
jtulach@44
    18
/**
jtulach@44
    19
 *
jtulach@44
    20
 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jtulach@44
    21
 */
jtulach@46
    22
// BEGIN: day.end.bridges.BridgeToOld
jtulach@44
    23
public class BridgeToOld extends Provider {
jtulach@44
    24
jtulach@44
    25
    public BridgeToOld() {
jtulach@44
    26
        super("spi.Digestor", 1.0, "");
jtulach@44
    27
        Security.addProvider(this);
jtulach@44
    28
    }
jtulach@44
    29
    
jtulach@44
    30
    @Override
jtulach@44
    31
    public synchronized Service getService(String type, String algorithm) {
jtulach@44
    32
        if ("MessageDigest".equals(type)) {
jtulach@44
    33
            Digest dig = Digest.getInstance(algorithm);
jtulach@44
    34
            if (dig != null) {
jtulach@44
    35
                return new ServiceImpl(dig, this, type, algorithm, "", Collections.<String>emptyList(), Collections.<String,String>emptyMap());
jtulach@44
    36
            }
jtulach@44
    37
        }
jtulach@44
    38
        return null;
jtulach@44
    39
    }
jtulach@44
    40
jtulach@44
    41
    private static class ServiceImpl<Data> extends Service {
jtulach@44
    42
        Digest dig;
jtulach@44
    43
jtulach@44
    44
        public ServiceImpl(Digest dig, Provider provider, String type, String algorithm, String className, List<String> aliases, Map<String, String> attributes) {
jtulach@44
    45
            super(provider, type, algorithm, className, aliases, attributes);
jtulach@44
    46
            this.dig = dig;
jtulach@44
    47
        }
jtulach@44
    48
jtulach@44
    49
        @Override
jtulach@44
    50
        public Object newInstance(Object constructorParameter) throws NoSuchAlgorithmException {
jtulach@44
    51
            return new MessageDigest(getAlgorithm()) {
jtulach@44
    52
                private byte[] res;
jtulach@44
    53
                
jtulach@44
    54
                @Override
jtulach@44
    55
                protected void engineUpdate(byte input) {
jtulach@44
    56
                    ByteBuffer bb = ByteBuffer.wrap(new byte[] { input });
jtulach@44
    57
                    res = dig.digest(bb);
jtulach@44
    58
                }
jtulach@44
    59
jtulach@44
    60
                @Override
jtulach@44
    61
                protected void engineUpdate(byte[] input, int offset, int len) {
jtulach@44
    62
                    ByteBuffer bb = ByteBuffer.wrap(input);
jtulach@44
    63
                    bb.position(offset);
jtulach@44
    64
                    bb.limit(offset + len);
jtulach@44
    65
                    res = dig.digest(bb);
jtulach@44
    66
                }
jtulach@44
    67
jtulach@44
    68
                @Override
jtulach@44
    69
                protected byte[] engineDigest() {
jtulach@44
    70
                    return res;
jtulach@44
    71
                }
jtulach@44
    72
jtulach@44
    73
                @Override
jtulach@44
    74
                protected void engineReset() {
jtulach@44
    75
                    dig = Digest.getInstance(getAlgorithm());
jtulach@44
    76
                }
jtulach@44
    77
            };
jtulach@44
    78
        }
jtulach@44
    79
        
jtulach@44
    80
        
jtulach@44
    81
    }
jtulach@44
    82
jtulach@44
    83
}
jtulach@46
    84
// END: day.end.bridges.BridgeToOld