samples/messagedigest/src-bridge/org/apidesign/impl/security/extension/BridgeToOld.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:52:26 +0200
changeset 51 130e099942d8
parent 50 019f1e9f7741
child 132 3bc4c54f4bcc
permissions -rw-r--r--
Merge: Geertjan's changes up to 4500
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@51
    22
// BEGIN: day.end.bridges.BridgeToOld
jtulach@51
    23
public final 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@51
    29
jtulach@51
    30
    // BEGIN: day.end.bridges.cycle
jtulach@51
    31
    private ThreadLocal<Boolean> searching = new ThreadLocal<Boolean>();
jtulach@51
    32
    final boolean isSearching() {
jtulach@51
    33
        return Boolean.TRUE.equals(searching.get());
jtulach@51
    34
    }
jtulach@44
    35
    
jtulach@44
    36
    @Override
jtulach@44
    37
    public synchronized Service getService(String type, String algorithm) {
jtulach@51
    38
        Boolean prev = searching.get();
jtulach@51
    39
        try {
jtulach@51
    40
            searching.set(Boolean.TRUE);
jtulach@51
    41
            if ("MessageDigest".equals(type)) {
jtulach@51
    42
                Digest dig = Digest.getInstance(algorithm);
jtulach@51
    43
                if (dig != null) {
jtulach@51
    44
                    return new ServiceImpl(dig, this, type, algorithm, "", Collections.<String>emptyList(), Collections.<String,String>emptyMap());
jtulach@51
    45
                }
jtulach@44
    46
            }
jtulach@51
    47
            return null;
jtulach@51
    48
        } finally {
jtulach@51
    49
            searching.set(prev);
jtulach@44
    50
        }
jtulach@44
    51
    }
jtulach@51
    52
    // END: day.end.bridges.cycle
jtulach@44
    53
jtulach@44
    54
    private static class ServiceImpl<Data> extends Service {
jtulach@44
    55
        Digest dig;
jtulach@44
    56
jtulach@44
    57
        public ServiceImpl(Digest dig, Provider provider, String type, String algorithm, String className, List<String> aliases, Map<String, String> attributes) {
jtulach@44
    58
            super(provider, type, algorithm, className, aliases, attributes);
jtulach@44
    59
            this.dig = dig;
jtulach@44
    60
        }
jtulach@44
    61
jtulach@44
    62
        @Override
jtulach@44
    63
        public Object newInstance(Object constructorParameter) throws NoSuchAlgorithmException {
jtulach@44
    64
            return new MessageDigest(getAlgorithm()) {
jtulach@44
    65
                private byte[] res;
jtulach@44
    66
                
jtulach@44
    67
                @Override
jtulach@44
    68
                protected void engineUpdate(byte input) {
jtulach@44
    69
                    ByteBuffer bb = ByteBuffer.wrap(new byte[] { input });
jtulach@44
    70
                    res = dig.digest(bb);
jtulach@44
    71
                }
jtulach@44
    72
jtulach@44
    73
                @Override
jtulach@44
    74
                protected void engineUpdate(byte[] input, int offset, int len) {
jtulach@44
    75
                    ByteBuffer bb = ByteBuffer.wrap(input);
jtulach@44
    76
                    bb.position(offset);
jtulach@44
    77
                    bb.limit(offset + len);
jtulach@44
    78
                    res = dig.digest(bb);
jtulach@44
    79
                }
jtulach@44
    80
jtulach@44
    81
                @Override
jtulach@44
    82
                protected byte[] engineDigest() {
jtulach@44
    83
                    return res;
jtulach@44
    84
                }
jtulach@44
    85
jtulach@44
    86
                @Override
jtulach@44
    87
                protected void engineReset() {
jtulach@44
    88
                    dig = Digest.getInstance(getAlgorithm());
jtulach@44
    89
                }
jtulach@44
    90
            };
jtulach@44
    91
        }
jtulach@44
    92
        
jtulach@44
    93
        
jtulach@44
    94
    }
jtulach@44
    95
jtulach@44
    96
}
jtulach@51
    97
// END: day.end.bridges.BridgeToOld