[DynamicRebuildWindow] Initial changes for the project
authorAntonio Moreno <antonio.moreno@openbravo.com>
Mon, 26 Apr 2010 18:19:54 +0200
changeset 7480 2772436041d8
parent 7154 783d62a77511
child 7481 bc9a75ed2ffa
[DynamicRebuildWindow] Initial changes for the project
build.xml
src-core/.classpath
src-core/src/org/openbravo/base/AntExecutor.java
src-core/src/org/openbravo/base/BuildTask.java
src-wad/build.xml
src/org/openbravo/erpCommon/ad_process/ApplyModules.java
--- a/build.xml	Thu Apr 22 11:47:46 2010 +0530
+++ b/build.xml	Mon Apr 26 18:19:54 2010 +0200
@@ -592,6 +592,7 @@
     <ant dir="${base.db}" target="update.database.java" inheritAll="true" inheritRefs="true" />
     <updatesystemstatus v="RB21"/>
     <antcall target="generate.entities.quick" />
+    <updatesystemstatus v="RB31"/>
     <antcall target="db.apply.modules" />
   </target>
 
@@ -659,7 +660,6 @@
   </target>
 
   <target name="apply.module">
-    <updatesystemstatus v="RB31"/>
     <java classname="org.openbravo.erpCommon.modules.ApplyModuleTask" fork="true" maxmemory="${build.maxmemory}" failonerror="true">
       <arg line="'${base.src}'" />
       <classpath refid="project.class.path" />
@@ -936,4 +936,23 @@
   <target name="validate.wad">
     <ant dir="${base.src}" target="wadvalidation" inheritAll="true" inheritRefs="true" />
   </target>
+  
+  <target name="UIrebuild" depends="core.lib">
+    <java classname="org.openbravo.base.BuildTask" jvm="${env.JAVA_HOME}/bin/java" fork="yes" maxmemory="${build.maxmemory}">
+      <arg line="'${base.config}/Openbravo.properties' ${logFileName}" />
+      <classpath>
+        <pathelement path="${build.core.lib}/openbravo-core.jar" />
+        <pathelement path="${base.lib}/runtime/log4j-1.2.8.jar" />
+        <pathelement path="${base.lib}/runtime/commons-pool.jar" />
+        <pathelement path="${base.lib}/runtime/commons-dbcp.jar" />
+        <pathelement path="${base.lib}/runtime/postgresql-jdbc3-8.2.jar" />
+        <pathelement path="${base.lib}/runtime/ojdbc6.jar" />
+        <pathelement path="${base.lib}/runtime/commons-collections.jar" />
+        <pathelement path="${base.lib}/runtime/ant-1.7.1.jar" />
+        <pathelement path="${base.lib}/runtime/ant-launcher.jar" />
+        <pathelement path="${base.lib}/runtime/ant-apache-log4j.jar" />
+        <pathelement path="${base.lib}/runtime/ant-nodeps.jar" />
+      </classpath>
+    </java>
+  </target>
 </project>
--- a/src-core/.classpath	Thu Apr 22 11:47:46 2010 +0530
+++ b/src-core/.classpath	Mon Apr 26 18:19:54 2010 +0200
@@ -15,5 +15,6 @@
 	<classpathentry kind="lib" path="/openbravo/lib/runtime/log4j-1.2.8.jar"/>
 	<classpathentry kind="lib" path="/openbravo/lib/runtime/commons-io-1.4.jar"/>
 	<classpathentry kind="lib" path="/openbravo/lib/runtime/ant-1.7.1.jar"/>
+	<classpathentry kind="lib" path="/openbravo/lib/runtime/ant-apache-log4j.jar"/>
 	<classpathentry kind="output" path="build/classes"/>
 </classpath>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-core/src/org/openbravo/base/AntExecutor.java	Mon Apr 26 18:19:54 2010 +0200
@@ -0,0 +1,216 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
+ * Version 1.1  with a permitted attribution clause; you may not  use this
+ * file except in compliance with the License. You  may  obtain  a copy of
+ * the License at http://www.openbravo.com/legal/license.html 
+ * Software distributed under the License  is  distributed  on  an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific  language  governing  rights  and  limitations
+ * under the License. 
+ * The Original Code is Openbravo ERP. 
+ * The Initial Developer of the Original Code is Openbravo SLU 
+ * All portions are Copyright (C) 2008-2009 Openbravo SLU 
+ * All Rights Reserved. 
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+package org.openbravo.base;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.util.Vector;
+
+import org.apache.log4j.Logger;
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.DefaultLogger;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.ProjectHelper;
+import org.apache.tools.ant.listener.Log4jListener;
+
+/**
+ * The AntExecutor class allows to execute ant tasks in a given build.xml file.
+ * 
+ * 
+ */
+public class AntExecutor {
+  private static final Logger logger = Logger.getLogger(AntExecutor.class);
+
+  private Project project;
+  private String baseDir;
+  private PrintStream log;
+  private PrintStream err;
+  private String returnMessage;
+  private PrintWriter out;
+
+  private FileOutputStream logFile;
+  private PrintStream ps;
+
+  /**
+   * Initializes a newly created AntExecutor object assigning it the build.xml file to execute tasks
+   * from and the base directory where they will be executed.
+   * 
+   * @param buildFile
+   *          - Complete path to the build.xml file
+   * @param baseDir
+   *          - Complete path to the base directory
+   * @throws Exception
+   *           - if an error occurs loading the xml file
+   */
+  public AntExecutor(String buildFile, String baseDir) throws Exception {
+    project = new Project();
+    this.baseDir = (baseDir == null || baseDir.equals("") ? "." : baseDir);
+    try {
+      project.init();
+      project.setBasedir(this.baseDir);
+      ProjectHelper.getProjectHelper().parse(project, new File(buildFile));
+    } catch (final BuildException e) {
+      throw new Exception("ErrorLoadingBuildXML", e);
+    }
+  }
+
+  /**
+   * Initializes a new AntExecutor object assigning it the build.xml file in the directory passed as
+   * parameter, the base directory is the same as the one the build.xml is in.
+   * 
+   * @param buildDir
+   *          - Directory where is the build.xml file and that will be the base directory
+   * @throws Exception
+   *           - if an error occurs loading the xml file
+   */
+  public AntExecutor(String buildDir) throws Exception {
+    this(buildDir + "/build.xml", buildDir);
+  }
+
+  @Deprecated
+  public void setPrintWriter(PrintWriter p) {
+    out = p;
+  }
+
+  /**
+   * Sets a file where the execution log will be saved.
+   * 
+   * @param directory
+   *          - Path to the directory for the file
+   * @param logFileName
+   *          - Name of the log file
+   * @return - The complete file name (including directory)
+   * @throws Exception
+   */
+  @Deprecated
+  public String setLogFile(String directory, String logFileName) throws Exception {
+    // DefaultLogger logger = new DefaultLogger();
+    final FileOutputStream logFile = new FileOutputStream(directory + "/" + logFileName);
+    // PrintStream ps = new PrintStream(logFile);
+    // logger.setOutputPrintStream(ps);
+    // logger.setErrorPrintStream(ps);
+    // logger.setMessageOutputLevel(Project.MSG_INFO);
+    // project.addBuildListener(logger);
+    return directory + "/" + logFileName;
+  }
+
+  /**
+   * Sets a file where the execution log will be saved. It only receives the file name, the path is
+   * the log directory inside the base directory
+   * 
+   * @param name
+   *          - File name
+   * @return - The complete file name (including directory)
+   * @throws Exception
+   */
+  @Deprecated
+  public String setLogFile(String name) throws Exception {
+    final File dir = new File(baseDir + "/log");
+    if (!dir.exists())
+      if (!dir.mkdir())
+        return null;
+    return setLogFile(baseDir + "/log", name);
+  }
+
+  public void setLogFileAndListener(String filename) {
+    File logFolder = new File(baseDir, "log");
+    if (!logFolder.exists()) {
+      logFolder.mkdir();
+    }
+    File file = new File(baseDir + "/log", filename + "-apply.log");
+    final DefaultLogger logger1 = new DefaultLogger();
+    try {
+      logFile = new FileOutputStream(file);
+      ps = new PrintStream(logFile);
+      logger1.setOutputPrintStream(ps);
+      logger1.setErrorPrintStream(ps);
+      logger1.setMessageOutputLevel(Project.MSG_INFO);
+      project.addBuildListener(logger1);
+
+      Log4jListener listener = new Log4jListener();
+      project.addBuildListener(listener);
+    } catch (FileNotFoundException e) {
+      logger.error("Error assigning rebuild log file.", e);
+    }
+  }
+
+  /**
+   * Set a value to a property to the project.
+   * 
+   * @param property
+   *          - Property name
+   * @param value
+   *          - Value to assign
+   */
+  public void setProperty(String property, String value) {
+    project.setProperty(property, value);
+  }
+
+  /**
+   * Executes an ant task
+   * 
+   * @param task
+   *          - Name of the task to execute
+   * @throws Exception
+   *           - In case the project is not loaded
+   */
+  public void runTask(String task) throws Exception {
+    if (project == null)
+      throw new Exception("NoProjectLoaded");
+    if (task == null)
+      task = project.getDefaultTarget();
+    try {
+      project.executeTarget(task);
+    } catch (final BuildException e) {
+      logger.error(e.getMessage(), e);
+    }
+  }
+
+  /**
+   * Executes a set of ant tasks
+   * 
+   * @param tasks
+   *          - A Vector<String> with the names of the tasks to be executed
+   * @throws Exception
+   *           - In case the project is not loaded
+   */
+  public void runTask(Vector<String> tasks) throws Exception {
+    if (project == null)
+      throw new Exception("NoProjectLoaded");
+    try {
+      project.executeTargets(tasks);
+    } catch (final BuildException e) {
+      logger.error(e.getMessage(), e);
+    }
+  }
+
+  public void closeLogFile() {
+    try {
+      if (logFile != null) {
+        ps.flush();
+        logFile.close();
+      }
+    } catch (IOException e) {
+    }
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-core/src/org/openbravo/base/BuildTask.java	Mon Apr 26 18:19:54 2010 +0200
@@ -0,0 +1,161 @@
+package org.openbravo.base;
+
+import java.io.FileInputStream;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.util.Properties;
+import java.util.Vector;
+import java.util.logging.Level;
+
+import org.apache.log4j.LogManager;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PropertyConfigurator;
+import org.openbravo.database.CPStandAlone;
+import org.openbravo.database.ConnectionProvider;
+
+public class BuildTask {
+
+  static String propertiesFile;
+  protected static Logger log;
+
+  /**
+   * This class starts a build of Openbravo. The kind of build is decided by this class logic
+   */
+  public static void main(String[] args) throws Exception {
+    try {
+
+      final Properties props = new Properties();
+      final String level = Level.INFO.toString();
+      props.setProperty("log4j.rootCategory", level + ",A,O2");
+      props.setProperty("log4j.appender.A", "org.apache.log4j.ConsoleAppender");
+      // "org.apache.log4j.ConsoleAppender");
+      props.setProperty("log4j.appender.A.layout", "org.apache.log4j.PatternLayout");
+      props.setProperty("log4j.appender.A.layout.ConversionPattern", "%-4r %-5p - %m%n");
+      // we don't want debug logging from Digester/Betwixt
+      props.setProperty("log4j.logger.org.apache.commons", "WARN");
+      props.setProperty("log4j.logger.org.hibernate", "WARN");
+
+      // Adding properties for log of Improved Upgrade Process
+      props.setProperty("log4j.appender.O2", "org.openbravo.utils.OBRebuildAppender");
+      props.setProperty("log4j.appender.O2.layout", "org.apache.log4j.PatternLayout");
+      props.setProperty("log4j.appender.O2.layout.ConversionPattern", "%-4r [%t] %-5p %c - %m%n");
+      LogManager.resetConfiguration();
+      PropertyConfigurator.configure(props);
+      log = Logger.getLogger(BuildTask.class);
+
+      log.info("principio");
+      propertiesFile = args[0];
+      String logFileName = args[1];
+      System.out.println(propertiesFile);
+      Properties properties = new Properties();
+      properties.load(new FileInputStream(propertiesFile));
+      AntExecutor ant = new AntExecutor(properties.getProperty("source.path"));
+      log.info("File!!!: " + logFileName);
+      ant.setLogFileAndListener(logFileName);
+      final Vector<String> tasks = new Vector<String>();
+      final String unnappliedModules = getUnnapliedModules();
+      if (isUpdatingCoreOrTemplate()) {
+        tasks.add("update.database");
+        tasks.add("core.lib");
+        tasks.add("wad.lib");
+        tasks.add("trl.lib");
+        tasks.add("compile.complete.deploy");
+        ant.setProperty("apply.on.create", "true");
+      } else {
+        if (compileCompleteNeeded()) {
+          // compile complete is needed for templates because in this case it is not needed which
+          // elements belong to the template and for uninistalling modules in order to remove old
+          // files and references
+          ant.setProperty("apply.modules.complete.compilation", "true");
+        }
+        ant.setProperty("force", "true");
+        tasks.add("apply.modules");
+        ant.setProperty("module", unnappliedModules);
+      }
+      for (String task : tasks)
+        System.out.println(task);
+      System.out.println("modules: " + unnappliedModules);
+      ant.runTask(tasks);
+      log.info("fin");
+    } catch (Exception e) {
+      e.printStackTrace();
+      throw new Exception("jarl");
+    }
+  }
+
+  private static void setLog() {
+
+  }
+
+  private static String getUnnapliedModules() {
+    try {
+      String strSql = "";
+      strSql = strSql + "         SELECT JAVAPACKAGE AS NAME" + "           FROM AD_MODULE M"
+          + "          WHERE ISACTIVE='Y'"
+          + "            AND (STATUS='I' OR STATUS='U' OR STATUS='P')  "
+          + "            AND NOT EXISTS (SELECT 1 "
+          + "                              FROM AD_MODULE_INSTALL"
+          + "                             WHERE AD_MODULE_ID = M.AD_MODULE_ID)" + "          UNION"
+          + "          SELECT JAVAPACKAGE AS NAME" + "           FROM AD_MODULE_INSTALL         ";
+      ConnectionProvider cp = getConnectionProvider();
+      PreparedStatement ps = cp.getPreparedStatement(strSql);
+      ResultSet rs = ps.executeQuery();
+      String rt = "";
+      int i = 0;
+      while (rs.next()) {
+        if (i > 0)
+          rt += ",";
+        rt += rs.getString(1);
+      }
+      return rt;
+    } catch (Exception e) {
+      // What should we do here?
+    }
+    return "";
+  }
+
+  private static boolean isUpdatingCoreOrTemplate() {
+    try {
+      String strSql = "";
+      strSql = strSql + "         SELECT count(*) as NAME FROM" + "            ((SELECT 1  "
+          + "                FROM AD_MODULE"
+          + "                WHERE (STATUS='I' OR STATUS='P')      "
+          + "               AND (AD_MODULE_ID = '0' OR TYPE='T'))" + "            UNION"
+          + "            (SELECT 1" + "                FROM AD_MODULE_INSTALL"
+          + "                WHERE (STATUS='I' OR STATUS='P')      "
+          + "               AND (AD_MODULE_ID = '0' OR TYPE='T'))) q";
+      ConnectionProvider cp = getConnectionProvider();
+      PreparedStatement ps = cp.getPreparedStatement(strSql);
+      ResultSet rs = ps.executeQuery();
+      rs.next();
+      return rs.getInt(1) != 0;
+    } catch (Exception e) {
+      // What should we do here?
+    }
+    return true;
+  }
+
+  private static boolean compileCompleteNeeded() {
+    try {
+      String strSql = "";
+      strSql = strSql + "         SELECT count(*) as NAME" + "           FROM AD_MODULE"
+          + "          WHERE ((STATUS='I' OR STATUS='P')      "
+          + "                 AND TYPE = 'T')" + "             OR (STATUS='U')";
+      ConnectionProvider cp = getConnectionProvider();
+      PreparedStatement ps = cp.getPreparedStatement(strSql);
+      ResultSet rs = ps.executeQuery();
+      rs.next();
+      return rs.getInt(1) != 0;
+    } catch (Exception e) {
+      // What should we do here?
+    }
+    return true;
+  }
+
+  private static ConnectionProvider getConnectionProvider() {
+    ConnectionProvider cp = null;
+    cp = new CPStandAlone(propertiesFile);
+    return cp;
+  }
+
+}
--- a/src-wad/build.xml	Thu Apr 22 11:47:46 2010 +0530
+++ b/src-wad/build.xml	Mon Apr 26 18:19:54 2010 +0200
@@ -70,13 +70,13 @@
   </target>
 
   <target name="compileSqlc" depends="sqlc">
-    <javac srcdir="${build.wad.sqlc}/src" includes="**/*.java" destdir="${build.wad}" encoding="UTF-8" debug="true" debuglevel="lines,vars,source">
+    <javac srcdir="${build.wad.sqlc}/src" includes="**/*.java" destdir="${build.wad}" encoding="UTF-8" debug="true" debuglevel="lines,vars,source" fork="true" >
       <classpath refid="wad.class.path" />
     </javac>
   </target>
 
   <target name="compile" depends="compileSqlc">
-    <javac srcdir="${main.dir}" includes="src-wad/src/**/*.java,modules/*/src-wad/**/*.java" destdir="${build.wad}" deprecation="on" encoding="UTF-8" debug="true" debuglevel="lines,vars,source">
+    <javac srcdir="${main.dir}" includes="src-wad/src/**/*.java,modules/*/src-wad/**/*.java" destdir="${build.wad}" deprecation="on" encoding="UTF-8" debug="true" debuglevel="lines,vars,source" fork="true" >
       <classpath refid="wad.class.path" />
     </javac>
     <copy todir="${build.wad}" encoding="UTF-8">
--- a/src/org/openbravo/erpCommon/ad_process/ApplyModules.java	Thu Apr 22 11:47:46 2010 +0530
+++ b/src/org/openbravo/erpCommon/ad_process/ApplyModules.java	Mon Apr 26 18:19:54 2010 +0200
@@ -234,34 +234,14 @@
       response.setContentType("text/html; charset=UTF-8");
       final PrintWriter out = response.getWriter();
 
-      ant.setLogFileAndListener(vars.getStringParameter("logfile"));
-
       // do not execute translation process (all entries should be already in the module)
       ant.setProperty("tr", "no");
       // We will show special, friendly warnings when they are available
       ant.setProperty("friendlyWarnings", "true");
+      ant.setProperty("logFileName", vars.getStringParameter("logfile"));
 
       final Vector<String> tasks = new Vector<String>();
-
-      final String unnappliedModules = getUnnapliedModules();
-      if (ApplyModulesData.isUpdatingCoreOrTemplate(this)) {
-        tasks.add("update.database");
-        tasks.add("core.lib");
-        tasks.add("wad.lib");
-        tasks.add("trl.lib");
-        tasks.add("compile.complete.deploy");
-        ant.setProperty("apply.on.create", "true");
-      } else {
-        if (ApplyModulesData.compileCompleteNeeded(this)) {
-          // compile complete is needed for templates because in this case it is not needed which
-          // elements belong to the template and for uninistalling modules in order to remove old
-          // files and references
-          ant.setProperty("apply.modules.complete.compilation", "true");
-        }
-        ant.setProperty("force", "true");
-        tasks.add("apply.modules");
-        ant.setProperty("module", unnappliedModules);
-      }
+      tasks.add("UIrebuild");
 
       // We also cancel sessions opened for users different from the current one
       updateSession = getPreparedStatement("UPDATE AD_SESSION SET SESSION_ACTIVE='N' WHERE CREATEDBY<>?");