Package org.jibeframework.core.app.store

Source Code of org.jibeframework.core.app.store.JavascriptStore

package org.jibeframework.core.app.store;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jibeframework.core.Context;
import org.jibeframework.core.JibeRuntimeException;
import org.jibeframework.core.app.Application;
import org.jibeframework.core.app.ApplicationInitializedListener;
import org.jibeframework.core.app.bootstrap.Bootstrapable;
import org.jibeframework.core.util.JSMin;
import org.springframework.util.ResourceUtils;

public class JavascriptStore implements ApplicationInitializedListener, Bootstrapable<List<String>> {
  public static final String BEAN_ID = "jibe.JavascriptStore";

  private static final Log logger = LogFactory.getLog(JavascriptStore.class);

  private List<String> javaScriptFiles = new ArrayList<String>();
  private String javaScriptsMinimized;
  private Date modified = null;

  public void bootstrap(List<String> scripts) {
    javaScriptFiles.addAll(scripts);
  }

  public final String get() {
    if (Application.isProduction()
        && !StringUtils.equals("true", (String) Context.getCurrentContext().getParams().get("debug"))) {
      return javaScriptsMinimized;
    } else {

      return getJavaScriptContent();
    }
  }

  public void onApplicationInitialized() {
    try {
      String js = getJavaScriptContent();
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      JSMin jsmin = new JSMin(new ByteArrayInputStream(js.getBytes()), bos);
      jsmin.jsmin();
      javaScriptsMinimized = bos.toString();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    // modified = new Date();
  }

  private String getJavaScriptContent() {
    StringBuffer javaScriptsBuffer = new StringBuffer();
    for (String script : javaScriptFiles) {
      javaScriptsBuffer.append(String.format("/*%1$s*/", script));
      javaScriptsBuffer.append(System.getProperty("line.separator"));
      try {
        File file = ResourceUtils.getFile(Application.getClasspathPrefix() + script);
        javaScriptsBuffer.append(FileUtils.readFileToString(file, "UTF-8"));
      } catch (IOException e) {
        throw new JibeRuntimeException("Can not read javascript file:" + script);
      }
    }
    return javaScriptsBuffer.toString();

  }

  /**
   *
   * @return the last modified Date.getTime() or 0L if modified is null
   */
  public long getLastModified() {
    if (modified != null)
      return modified.getTime();
    else
      return 0L;
  }

}
TOP

Related Classes of org.jibeframework.core.app.store.JavascriptStore

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.