jtulach@47
|
1 |
/*
|
jtulach@47
|
2 |
* To change this template, choose Tools | Templates
|
jtulach@47
|
3 |
* and open the template in the editor.
|
jtulach@47
|
4 |
*/
|
jtulach@47
|
5 |
|
jtulach@47
|
6 |
package org.apidesign.impl;
|
jtulach@47
|
7 |
|
jtulach@47
|
8 |
import java.nio.ByteBuffer;
|
jtulach@47
|
9 |
import org.apidesign.spi.security.Digestor;
|
jtulach@47
|
10 |
|
jtulach@47
|
11 |
/**
|
jtulach@47
|
12 |
*
|
jtulach@47
|
13 |
* @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jtulach@47
|
14 |
*/
|
jtulach@47
|
15 |
public abstract class DigestorAccessor {
|
jtulach@47
|
16 |
private static DigestorAccessor INSTANCE;
|
jtulach@47
|
17 |
|
jtulach@47
|
18 |
protected DigestorAccessor() {
|
jtulach@47
|
19 |
if (this.getClass().getName().equals("org.apidesign.spi.security.DigestorAccessorImpl")) {
|
jtulach@47
|
20 |
assert INSTANCE == null;
|
jtulach@47
|
21 |
INSTANCE = this;
|
jtulach@47
|
22 |
return;
|
jtulach@47
|
23 |
}
|
jtulach@47
|
24 |
if (this.getClass().getName().equals("org.apidesign.api.security.DigestorAccessorImpl")) {
|
jtulach@47
|
25 |
return;
|
jtulach@47
|
26 |
}
|
jtulach@47
|
27 |
throw new IllegalStateException();
|
jtulach@47
|
28 |
}
|
jtulach@47
|
29 |
|
jtulach@47
|
30 |
final DigestorAccessor getDefault() {
|
jtulach@47
|
31 |
try {
|
jtulach@47
|
32 |
Class.forName(Digestor.class.getName(), true, DigestorAccessor.class.getClassLoader());
|
jtulach@47
|
33 |
return INSTANCE;
|
jtulach@47
|
34 |
} catch (ClassNotFoundException ex) {
|
jtulach@47
|
35 |
throw new IllegalStateException(ex);
|
jtulach@47
|
36 |
}
|
jtulach@47
|
37 |
}
|
jtulach@47
|
38 |
|
jtulach@47
|
39 |
protected abstract <Data> byte[] digest(Digestor<Data> dig, Data data);
|
jtulach@47
|
40 |
protected abstract <Data> Data create(Digestor<Data> dig, String algorithm);
|
jtulach@47
|
41 |
protected abstract <Data> void update(Digestor<Data> dig, Data data, ByteBuffer input);
|
jtulach@47
|
42 |
|
jtulach@47
|
43 |
|
jtulach@47
|
44 |
protected final <Data> byte[] defaultDigest(Digestor<Data> dig, Data data) {
|
jtulach@47
|
45 |
return getDefault().digest(dig, data);
|
jtulach@47
|
46 |
}
|
jtulach@47
|
47 |
protected final <Data> Data defaultCreate(Digestor<Data> dig, String algorithm) {
|
jtulach@47
|
48 |
return getDefault().create(dig, algorithm);
|
jtulach@47
|
49 |
}
|
jtulach@47
|
50 |
protected final <Data> void defaultUpdate(Digestor<Data> dig, Data data, ByteBuffer input) {
|
jtulach@47
|
51 |
getDefault().update(dig, data, input);
|
jtulach@47
|
52 |
}
|
jtulach@47
|
53 |
}
|