// Copyright 2013 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.apache.tapestry5.internal.services.assets;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.tapestry5.ioc.Resource;
import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
import org.apache.tapestry5.services.assets.AssetChecksumGenerator;
import org.apache.tapestry5.services.assets.StreamableResource;
import org.apache.tapestry5.services.assets.StreamableResourceProcessing;
import org.apache.tapestry5.services.assets.StreamableResourceSource;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

public class AssetChecksumGeneratorImpl implements AssetChecksumGenerator
{

    private final StreamableResourceSource streamableResourceSource;

    private final ResourceChangeTracker tracker;

    private final Map<StreamableResource, String> cache = CollectionFactory.newConcurrentMap();

    public AssetChecksumGeneratorImpl(StreamableResourceSource streamableResourceSource, ResourceChangeTracker tracker)
    {
        this.streamableResourceSource = streamableResourceSource;
        this.tracker = tracker;

        tracker.clearOnInvalidation(cache);
    }

    public String generateChecksum(Resource resource) throws IOException
    {
        StreamableResource streamable = streamableResourceSource.getStreamableResource(resource, StreamableResourceProcessing.COMPRESSION_DISABLED,
                tracker);

        return generateChecksum(streamable);
    }

    @Override
    public String generateChecksum(StreamableResource resource) throws IOException
    {
        String result = cache.get(resource);

        if (result == null)
        {
            result = toChecksum(resource.openStream());

            cache.put(resource, result);
        }

        return result;
    }

    private String toChecksum(InputStream is) throws IOException
    {
        byte[] digest = DigestUtils.md5(is);

        return Hex.encodeHexString(digest);
    }

}

@@ -657,104 +657,8 @@
 s;%0A%0A
-import org.apache.commons.codec.binary.Hex;%0Aimport org.apache.commons.codec.digest.DigestUtils;%0A
 impo
@@ -1114,16 +1114,46 @@
 til.Map;
+%0Aimport java.util.zip.Adler32;
 %0A%0Apublic
@@ -2431,95 +2431,699 @@
     
-byte%5B%5D digest = DigestUtils.md5(is);%0A%0A        return Hex.encodeHexString(digest
+// Adler32 is very fast and suitable for these purposes (MD5 and SHA are slower, and%0A        // are targetted at cryptographic solutions).%0A        Adler32 checksum = new Adler32();%0A%0A        byte%5B%5D buffer = new byte%5B1024%5D;%0A%0A        try%0A        %7B%0A            while (true)%0A            %7B%0A                int length = is.read(buffer);%0A%0A                if (length %3C 0)%0A                %7B%0A                    break;%0A                %7D%0A%0A                checksum.update(buffer, 0, length);%0A            %7D%0A%0A            // Reduces it down to just 32 bits which we express in hex.'%0A            return Long.toHexString(checksum.getValue()
 );%0A    
+ 
+   %7D finally%0A        %7B%0A            is.close();%0A        %7D%0A    
 %7D
-%0A
 %0A%7D%0A

Use Adler32 instead of MD5