# HG changeset patch
# User Rafa Alonso <rafael.alonso@openbravo.com>
# Date 1410531929 -7200
#      Fri Sep 12 16:25:29 2014 +0200
# Node ID d6d87d516f6ae42bb6ddf92dd9e83e8f6e4b56bc
# Parent  1e71aac70f247de9852a8e2364b8be6bbe0fbff3
Login404

diff -r 1e71aac70f24 -r d6d87d516f6a src/org/openbravo/mobile/core/MobileCoreApplicationCacheComponent.java
--- a/src/org/openbravo/mobile/core/MobileCoreApplicationCacheComponent.java	Tue Sep 09 20:35:31 2014 +0200
+++ b/src/org/openbravo/mobile/core/MobileCoreApplicationCacheComponent.java	Fri Sep 12 16:25:29 2014 +0200
@@ -12,7 +12,6 @@
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Map;
 import java.util.UUID;
 
 import javax.enterprise.context.RequestScoped;
@@ -23,7 +22,6 @@
 import org.openbravo.client.kernel.BaseTemplateComponent;
 import org.openbravo.client.kernel.KernelUtils;
 import org.openbravo.client.kernel.RequestContext;
-import org.openbravo.client.kernel.StaticResourceComponent;
 import org.openbravo.client.kernel.Template;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.model.ad.module.Module;
@@ -36,14 +34,14 @@
 @RequestScoped
 public class MobileCoreApplicationCacheComponent extends BaseTemplateComponent {
 
-  @Inject
-  private StaticResourceComponent src;
-
   private static final String TEMPLATE_ID = "7A911850D921448EA5AC2E6F4D5FDE2D";
 
   @Inject
   private LibraryResource libraryResource;
 
+  @Inject
+  private MobileStaticResourceComponent mobileStaticResourceComponent;
+
   private String version = null;
 
   @Override
@@ -151,11 +149,7 @@
   }
 
   public String getGenFileName() {
-    Map<String, Object> params = getParameters();
-    String filePath = MobileStaticResourceComponent.getStaticResourceFileName();
-    if (params.containsKey("_appName")) {
-      filePath += (String) params.get("_appName");
-    }
-    return filePath;
+    mobileStaticResourceComponent.setParameters(getParameters());
+    return mobileStaticResourceComponent.getGeneratedJavascriptFilename();
   }
 }
diff -r 1e71aac70f24 -r d6d87d516f6a src/org/openbravo/mobile/core/MobileStaticResourceComponent.java
--- a/src/org/openbravo/mobile/core/MobileStaticResourceComponent.java	Tue Sep 09 20:35:31 2014 +0200
+++ b/src/org/openbravo/mobile/core/MobileStaticResourceComponent.java	Fri Sep 12 16:25:29 2014 +0200
@@ -9,22 +9,15 @@
 package org.openbravo.mobile.core;
 
 import java.io.File;
-import java.util.List;
-import java.util.Map;
 
-import javax.enterprise.inject.Any;
-import javax.enterprise.inject.Instance;
 import javax.inject.Inject;
 import javax.servlet.ServletContext;
 
-import org.apache.commons.codec.digest.DigestUtils;
-import org.openbravo.base.exception.OBException;
 import org.openbravo.client.kernel.BaseComponent;
 import org.openbravo.client.kernel.KernelConstants;
 import org.openbravo.client.kernel.KernelUtils;
 import org.openbravo.client.kernel.StaticResourceComponent;
 import org.openbravo.model.ad.module.Module;
-import org.openbravo.utils.FileUtility;
 
 /**
  * @author iperdomo
@@ -32,53 +25,45 @@
  */
 public class MobileStaticResourceComponent extends BaseComponent {
 
+  private static final String GEN_TARGET_LOCATION = "web/js/gen";
+  private static String generatedJavascriptFilename = null;
+
+  /**
+   * generates a new javascript file if there wasn't any or if in development
+   *
+   * synchronized because its quite heavy to do
+   */
+  private static synchronized String generateStaticResources(StaticResourceComponent src,
+      String realPath) {
+    // decide if a new javascript file must be generated
+    if (generatedJavascriptFilename == null
+        || !new File(realPath + "/" + generatedJavascriptFilename + ".js").exists()
+        || isDevelopment()) {
+      // generate a new javascript file. get the name of the new file. and remember it
+      generatedJavascriptFilename = src.getStaticResourceFileName();
+    }
+    return generatedJavascriptFilename;
+  }
+
   @Inject
-  @Any
-  private Instance<StaticResourceComponent> rc;
-
-  private static final String GEN_TARGET_LOCATION = "web/js/gen";
+  private StaticResourceComponent staticResourceComponent;
 
   @Override
   public String generate() {
-    String filePath = "";
-    String tempFilePath;
-    File finalFile;
+    return "enyo.depends('" + getContextUrl() + GEN_TARGET_LOCATION + "/"
+        + getGeneratedJavascriptFilename() + ".js" + "');";
+  }
 
-    Map<String, Object> params = getParameters();
-
+  /**
+   * the exposed method to get the generated javascript filename if the file does not exists, the
+   * file is created before this method returns. this can take several milliseconds
+   */
+  public String getGeneratedJavascriptFilename() {
+    staticResourceComponent.setParameters(getParameters());
     final ServletContext context = (ServletContext) getParameters().get(
         KernelConstants.SERVLET_CONTEXT);
-    filePath = GEN_TARGET_LOCATION + "/" + getStaticResourceFileName();
-    if (params.containsKey("_appName")) {
-      filePath += (String) params.get("_appName");
-    }
-
-    filePath += ".js";
-    if (!isDevelopment()) {
-      finalFile = new File(context.getRealPath(filePath));
-      if (finalFile.exists()) {
-        return "enyo.depends('" + getContextUrl() + filePath + "');";
-      }
-    }
-    StaticResourceComponent sr = rc.get();
-    sr.setParameters(getParameters());
-    tempFilePath = sr.getStaticResourceFileName();
-    if (isDevelopment()) {
-      filePath = GEN_TARGET_LOCATION + "/" + tempFilePath + ".js";
-    } else {
-      finalFile = new File(context.getRealPath(filePath));
-
-      File tempFile = new File(
-          context.getRealPath(GEN_TARGET_LOCATION + "/" + tempFilePath + ".js"));
-      try {
-        finalFile.createNewFile();
-        FileUtility.copyFile(tempFile, finalFile);
-      } catch (Exception e) {
-        throw new OBException("There was a problem when generating the static resources file", e);
-      }
-    }
-    return "enyo.depends('" + getContextUrl() + filePath + "');";
-
+    return generateStaticResources(staticResourceComponent,
+        context.getRealPath(GEN_TARGET_LOCATION) + "/");
   }
 
   @Override
@@ -86,15 +71,6 @@
     return null;
   }
 
-  public static String getStaticResourceFileName() {
-    StringBuffer versionString = new StringBuffer();
-    final List<Module> modules = KernelUtils.getInstance().getModulesOrderedByDependency();
-    for (Module module : modules) {
-      versionString.append(module.getVersion());
-    }
-    return DigestUtils.md5Hex(versionString.toString());
-  }
-
   public static boolean isDevelopment() {
     for (Module module : KernelUtils.getInstance().getModulesOrderedByDependency()) {
       if (module.isInDevelopment()) {
