001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.util.transformers;
011:
012: import org.mmbase.util.functions.*;
013: import java.util.zip.Checksum;
014: import java.io.*;
015:
016: import org.mmbase.util.logging.Logger;
017: import org.mmbase.util.logging.Logging;
018:
019: /**
020: * Creates ByteToCharTransformers, creating a 'checksum' string of a byte-arrays. Parameterized by
021: * the checksum implementation (defaults to java.util.zip.Adler32).
022: *
023: * @author Michiel Meeuwissen
024: * @since MMBase-1.8
025: * @version $Id: ChecksumFactory.java,v 1.11 2008/02/03 17:33:56 nklasens Exp $
026: */
027:
028: public class ChecksumFactory implements ParameterizedTransformerFactory {
029: private static final Logger log = Logging
030: .getLoggerInstance(ChecksumFactory.class);
031:
032: public static final Parameter[] PARAMS = new Parameter[] { new Parameter<String>(
033: "implementation", String.class, java.util.zip.Adler32.class
034: .getName()) };
035:
036: public Parameters createParameters() {
037: return new Parameters(PARAMS);
038: }
039:
040: /**
041: * Creates a parameterized transformer.
042: */
043: public Transformer createTransformer(Parameters parameters) {
044: String impl = (String) parameters.get("implementation");
045: try {
046: Class<?> clazz = Class.forName(impl);
047: if (Checksum.class.isAssignableFrom(clazz)) {
048: Checksum checksum = (Checksum) clazz.newInstance();
049: return new ChecksumTransformer(checksum);
050: }
051: if (impl.equalsIgnoreCase("md5")) {
052: return new MD5();
053: }
054: } catch (Exception e) {
055: log.error(e);
056: }
057: return Transformers.getCharTransformer(impl, "",
058: "ChecksumFactory", false);
059:
060: }
061:
062: protected class ChecksumTransformer extends
063: ByteArrayToCharTransformer implements ByteToCharTransformer {
064: private static final long serialVersionUID = 1L;
065: private Checksum checksum;
066:
067: ChecksumTransformer(Checksum c) {
068: checksum = c;
069: }
070:
071: public String transform(byte[] bytes) {
072: if (bytes == null)
073: return null;
074: synchronized (checksum) {
075: checksum.reset();
076: checksum.update(bytes, 0, bytes.length);
077: return "" + checksum.getValue();
078: }
079: }
080:
081: public String toString() {
082: return "checksum(" + checksum + ")";
083: }
084:
085: // implementation of serializable
086: private void writeObject(ObjectOutputStream out)
087: throws IOException {
088: if (checksum instanceof Serializable) {
089: out.writeObject(checksum);
090: } else {
091: out.writeObject(checksum.getClass());
092: }
093: }
094:
095: // implementation of serializable
096: private void readObject(ObjectInputStream in)
097: throws IOException, ClassNotFoundException {
098: Object cs = in.readObject();
099: if (cs instanceof Class) {
100: try {
101: checksum = (Checksum) ((Class<?>) cs).newInstance();
102: } catch (InstantiationException e) {
103: throw new IOException(e.getMessage());
104: } catch (IllegalAccessException e) {
105: throw new IOException(e.getMessage());
106: }
107: } else {
108: checksum = (Checksum) cs;
109: }
110: }
111:
112: }
113:
114: public static void main(String argv[]) {
115: ChecksumFactory fact = new ChecksumFactory();
116: Parameters params = fact.createParameters();
117: params.set("implementation", java.util.zip.Adler32.class
118: .getName());
119: CharTransformer transformer = new ByteCharTransformer(
120: (ByteToCharTransformer) fact.createTransformer(params));
121: System.out.println("" + transformer.transform("hoi"));
122: }
123:
124: }
|