ro.isdc.wro.extensions.processor.js.PackerJsProcessor Maven / Gradle / Ivy
/*
* Copyright (C) 2010.
* All rights reserved.
*/
package ro.isdc.wro.extensions.processor.js;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ro.isdc.wro.WroRuntimeException;
import ro.isdc.wro.extensions.processor.support.ObjectPoolHelper;
import ro.isdc.wro.extensions.processor.support.packer.PackerJs;
import ro.isdc.wro.model.group.processor.Minimize;
import ro.isdc.wro.model.resource.Resource;
import ro.isdc.wro.model.resource.ResourceType;
import ro.isdc.wro.model.resource.SupportedResourceType;
import ro.isdc.wro.model.resource.processor.ResourcePostProcessor;
import ro.isdc.wro.model.resource.processor.ResourcePreProcessor;
import ro.isdc.wro.util.ObjectFactory;
/**
* Uses Dean Edwards packer utility to pack js resources.
*
* @author Alex Objelean
* @created 31 Jul 2010
*/
@Minimize
@SupportedResourceType(ResourceType.JS)
public class PackerJsProcessor
implements ResourcePreProcessor, ResourcePostProcessor {
private static final Logger LOG = LoggerFactory.getLogger(PackerJsProcessor.class);
public static final String ALIAS = "packerJs";
private ObjectPoolHelper enginePool;
public PackerJsProcessor() {
enginePool = new ObjectPoolHelper(new ObjectFactory() {
@Override
public PackerJs create() {
return newPackerJs();
}
});
}
/**
* {@inheritDoc}
*/
public void process(final Resource resource, final Reader reader, final Writer writer)
throws IOException {
final String content = IOUtils.toString(reader);
final PackerJs packerJs = enginePool.getObject();
try {
writer.write(packerJs.pack(content));
} catch (final WroRuntimeException e) {
onException(e);
final String resourceUri = resource == null ? StringUtils.EMPTY : "[" + resource.getUri() + "]";
LOG.warn("Exception while applying " + getClass().getSimpleName() + " processor on the " + resourceUri
+ " resource, no processing applied...", e);
} finally {
reader.close();
writer.close();
enginePool.returnObject(packerJs);
}
}
/**
* Invoked when a processing exception occurs.
*/
protected void onException(final WroRuntimeException e) {
throw e;
}
/**
* @return PackerJs engine.
*/
protected PackerJs newPackerJs() {
return new PackerJs();
}
/**
* {@inheritDoc}
*/
public void process(final Reader reader, final Writer writer)
throws IOException {
process(null, reader, writer);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy