1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/samples/messagedigest/src-new-api/org/apidesign/impl/DigestorAccessor.java Sat Jun 14 09:52:25 2008 +0200
1.3 @@ -0,0 +1,53 @@
1.4 +/*
1.5 + * To change this template, choose Tools | Templates
1.6 + * and open the template in the editor.
1.7 + */
1.8 +
1.9 +package org.apidesign.impl;
1.10 +
1.11 +import java.nio.ByteBuffer;
1.12 +import org.apidesign.spi.security.Digestor;
1.13 +
1.14 +/**
1.15 + *
1.16 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.17 + */
1.18 +public abstract class DigestorAccessor {
1.19 + private static DigestorAccessor INSTANCE;
1.20 +
1.21 + protected DigestorAccessor() {
1.22 + if (this.getClass().getName().equals("org.apidesign.spi.security.DigestorAccessorImpl")) {
1.23 + assert INSTANCE == null;
1.24 + INSTANCE = this;
1.25 + return;
1.26 + }
1.27 + if (this.getClass().getName().equals("org.apidesign.api.security.DigestorAccessorImpl")) {
1.28 + return;
1.29 + }
1.30 + throw new IllegalStateException();
1.31 + }
1.32 +
1.33 + final DigestorAccessor getDefault() {
1.34 + try {
1.35 + Class.forName(Digestor.class.getName(), true, DigestorAccessor.class.getClassLoader());
1.36 + return INSTANCE;
1.37 + } catch (ClassNotFoundException ex) {
1.38 + throw new IllegalStateException(ex);
1.39 + }
1.40 + }
1.41 +
1.42 + protected abstract <Data> byte[] digest(Digestor<Data> dig, Data data);
1.43 + protected abstract <Data> Data create(Digestor<Data> dig, String algorithm);
1.44 + protected abstract <Data> void update(Digestor<Data> dig, Data data, ByteBuffer input);
1.45 +
1.46 +
1.47 + protected final <Data> byte[] defaultDigest(Digestor<Data> dig, Data data) {
1.48 + return getDefault().digest(dig, data);
1.49 + }
1.50 + protected final <Data> Data defaultCreate(Digestor<Data> dig, String algorithm) {
1.51 + return getDefault().create(dig, algorithm);
1.52 + }
1.53 + protected final <Data> void defaultUpdate(Digestor<Data> dig, Data data, ByteBuffer input) {
1.54 + getDefault().update(dig, data, input);
1.55 + }
1.56 +}