001// Licensed under the Apache License, Version 2.0 (the "License");
002// you may not use this file except in compliance with the License.
003// You may obtain a copy of the License at
004//
005// http://www.apache.org/licenses/LICENSE-2.0
006//
007// Unless required by applicable law or agreed to in writing, software
008// distributed under the License is distributed on an "AS IS" BASIS,
009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010// See the License for the specific language governing permissions and
011// limitations under the License.
012
013package org.apache.tapestry5.internal.webresources;
014
015import com.google.javascript.jscomp.*;
016import com.google.javascript.jscomp.Compiler;
017import org.apache.commons.io.IOUtils;
018import org.apache.tapestry5.TapestryConstants;
019import org.apache.tapestry5.ioc.OperationTracker;
020import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
021import org.apache.tapestry5.ioc.internal.util.InternalUtils;
022import org.apache.tapestry5.services.Request;
023import org.apache.tapestry5.services.assets.AssetChecksumGenerator;
024import org.apache.tapestry5.services.assets.StreamableResource;
025import org.slf4j.Logger;
026
027import java.io.IOException;
028import java.io.InputStream;
029import java.util.Collections;
030import java.util.List;
031import java.util.logging.Level;
032
033/**
034 * A wrapper around the Google Closure {@link Compiler} used to minimize
035 * a JavaScript resource.
036 */
037public class GoogleClosureMinimizer extends AbstractMinimizer
038{
039
040    private final static String OUTPUT_CHARSET = "utf-8";
041
042    private final List<SourceFile> EXTERNS = Collections.emptyList();
043
044    private final Request request;
045
046    static
047    {
048        Compiler.setLoggingLevel(Level.SEVERE);
049    }
050
051    public GoogleClosureMinimizer(Logger logger, OperationTracker tracker, AssetChecksumGenerator checksumGenerator, Request request)
052    {
053        super(logger, tracker, checksumGenerator, "text/javascript");
054        this.request = request;
055    }
056
057    @Override
058    protected boolean isEnabled(StreamableResource resource)
059    {
060        return request.getAttribute(TapestryConstants.DISABLE_JAVASCRIPT_MINIMIZATION) == null;
061    }
062
063    @Override
064    protected InputStream doMinimize(StreamableResource resource) throws IOException
065    {
066        // Don't bother to pool the Compiler
067
068        CompilerOptions options = new CompilerOptions();
069        options.setCodingConvention(new ClosureCodingConvention());
070        options.setOutputCharset(OUTPUT_CHARSET);
071        options.setWarningLevel(DiagnosticGroups.CHECK_VARIABLES, CheckLevel.WARNING);
072
073        Compiler compiler = new Compiler();
074
075        compiler.disableThreads();
076
077        SourceFile input = SourceFile.fromInputStream(resource.toString(), resource.openStream());
078
079        List<SourceFile> inputs = Collections.singletonList(input);
080
081        Result result = compiler.compile(EXTERNS, inputs, options);
082
083        if (result.success)
084        {
085            return IOUtils.toInputStream(compiler.toSource(), OUTPUT_CHARSET);
086        }
087
088        throw new RuntimeException(String.format("Compilation failed: %s.",
089                InternalUtils.join(CollectionFactory.newList(result.errors), ";")));
090    }
091}