/*
 * 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.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;


/**
 *  Uses <a href="http://dean.edwards.name/packer/">Dean Edwards packer utility</a> 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";
  /**
   * Engine.
   */
  private PackerJs engine;

  /**
   * {@inheritDoc}
   */
  public void process(final Resource resource, final Reader reader, final Writer writer)
    throws IOException {
    final String content = IOUtils.toString(reader);
    try {
      writer.write(getEngine().pack(content));
    } catch (final WroRuntimeException e) {
      onException(e);
      writer.write(content);
      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();
    }
  }

  /**
   * Invoked when a processing exception occurs.
   */
  protected void onException(final WroRuntimeException e) {
  }

  /**
   * @return PackerJs engine.
   */
  private PackerJs getEngine() {
    if (engine == null) {
      engine = new PackerJs();
    }
    return engine;
  }


  /**
   * {@inheritDoc}
   */
  public void process(final Reader reader, final Writer writer)
    throws IOException {
    process(null, reader, writer);
  }

}

@@ -361,24 +361,91 @@
 Exception;%0D%0A
+import ro.isdc.wro.extensions.processor.support.ObjectPoolHelper;%0D%0A
 import ro.is
@@ -837,16 +837,56 @@
 essor;%0D%0A
+import ro.isdc.wro.util.ObjectFactory;%0D%0A
 %0D%0A%0D%0A/**%0D
@@ -1340,60 +1340,278 @@
 %0D%0A  
-/**%0D%0A   * Engine.%0D%0A   */%0D%0A  private PackerJs engine;
+private ObjectPoolHelper%3CPackerJs%3E enginePool;%0D%0A%0D%0A%0D%0A  public PackerJsProcessor() %7B%0D%0A    enginePool = new ObjectPoolHelper%3CPackerJs%3E(new ObjectFactory%3CPackerJs%3E() %7B%0D%0A      @Override%0D%0A      public PackerJs create() %7B%0D%0A        return newPackerJs();%0D%0A      %7D%0D%0A    %7D);%0D%0A  %7D%0D%0A
 %0D%0A%0D%0A
@@ -1810,24 +1810,79 @@
 g(reader);%0D%0A
+    final PackerJs packerJs = enginePool.getObject();%0D%0A
     try %7B%0D%0A 
@@ -1899,27 +1899,24 @@
 r.write(
-getEngine()
+packerJs
 .pack(co
@@ -2352,24 +2352,66 @@
 r.close();%0D%0A
+      enginePool.returnObject(packerJs);%0D%0A
     %7D%0D%0A  %7D%0D%0A
@@ -2593,21 +2593,23 @@
 */%0D%0A  pr
-ivate
+otected
  PackerJ
@@ -2614,106 +2614,50 @@
 rJs 
-getEngine() %7B%0D%0A    if (engine == null) %7B%0D%0A      engine = new PackerJs();%0D%0A    %7D%0D%0A    return engine
+newPackerJs() %7B%0D%0A    return new PackerJs()
 ;%0D%0A 

make packerJs thread-safe