// 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 java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.zip.Adler32;

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;

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);
    }

    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
    {
        // Adler32 is very fast and suitable for these purposes (MD5 and SHA are slower, and
        // are targetted at cryptographic solutions).
        Adler32 checksum = new Adler32();

        byte[] buffer = new byte[1024];

        try
        {
            while (true)
            {
                int length = is.read(buffer);

                if (length < 0)
                {
                    break;
                }

                checksum.update(buffer, 0, length);
            }

            // Reduces it down to just 32 bits which we express in hex.'
            return Long.toHexString(checksum.getValue());
        } finally
        {
            is.close();
        }
    }
}

@@ -657,117 +657,8 @@
 s;%0A%0A
-import java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.util.Map;%0Aimport java.util.zip.Adler32;%0A%0A
 impo
@@ -1037,16 +1037,125 @@
 ource;%0A%0A
+import java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.util.Map;%0Aimport java.util.zip.Adler32;%0A%0A
 public c
@@ -2982,9 +2982,8 @@
 hex.
-'
 %0A   

Remove extraneous quote