[demand] Implemented ant task to allow admins to activate instance from console
authorAsier Lostalé <asier.lostale@openbravo.com>
Thu, 17 May 2012 10:12:41 +0200
changeset 16641 886177e54298
parent 16596 9715e0922173
child 16642 9f90a57bdd42
[demand] Implemented ant task to allow admins to activate instance from console
build.xml
src/org/openbravo/erpCommon/obps/ActivationKey.java
src/org/openbravo/erpCommon/obps/ActivationTask.java
src/org/openbravo/erpCommon/obps/ActiveInstanceProcess.java
src/org/openbravo/erpCommon/obps/DisabledModules.java
--- a/build.xml	Tue May 22 14:14:35 2012 +0200
+++ b/build.xml	Thu May 17 10:12:41 2012 +0200
@@ -1140,6 +1140,23 @@
       <ant dir="${base.src.core}" target="clean" inheritAll="true" inheritRefs="true" />
       <ant dir="${base.src.trl}" target="clean" inheritAll="true" inheritRefs="true" />
       <ant dir="${base.src.wad}" target="clean" inheritAll="true" inheritRefs="true" />
-    </target>
+  </target>
+  
+  <target name="activate.instance" depends="init"
+          description="Activates the instance with the provided public key">
+   <taskdef name="activateInstace" classname="org.openbravo.erpCommon.obps.ActivationTask">
+     <classpath refid="project.class.path" />
+   </taskdef>
+   <property name="purpose" value="" />
+   <property name="publicKey" value="" />
+   <property name="publicKeyFile" value="" />
+    
+   <activateInstace purpose="${purpose}"
+                    publicKey="${publicKey}"
+                    publicKeyFile="${publicKeyFile}"
+                    userId="0" 
+                    adminMode="true" 
+                    propertiesFile="${base.config}/Openbravo.properties"/>
+  </target>
   
 </project>
--- a/src/org/openbravo/erpCommon/obps/ActivationKey.java	Tue May 22 14:14:35 2012 +0200
+++ b/src/org/openbravo/erpCommon/obps/ActivationKey.java	Thu May 17 10:12:41 2012 +0200
@@ -527,8 +527,18 @@
 
     try {
       // read restriction file from context directory
-      String restrictionsFilePath = DalContextListener.getServletContext().getRealPath(
-          "src-loc/design/org/openbravo/erpCommon/obps/licenseRestrictions");
+      String restrictionsFilePath = null;
+      if (DalContextListener.getServletContext() != null) {
+        // Taking restrictions from Tomcat context
+        restrictionsFilePath = DalContextListener.getServletContext().getRealPath(
+            "src-loc/design/org/openbravo/erpCommon/obps/licenseRestrictions");
+      } else {
+        // Not in Tomcat context, taking restrictions from sources
+        restrictionsFilePath = OBPropertiesProvider.getInstance().getOpenbravoProperties()
+            .getProperty("source.path")
+            + "/src/org/openbravo/erpCommon/obps/licenseRestrictions";
+      }
+
       File restrictionsFile = new File(restrictionsFilePath);
       log4j.debug("Restrictions file: " + restrictionsFile.getAbsolutePath());
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/openbravo/erpCommon/obps/ActivationTask.java	Thu May 17 10:12:41 2012 +0200
@@ -0,0 +1,146 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.1  (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) 2012 Openbravo SLU 
+ * All Rights Reserved. 
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+
+package org.openbravo.erpCommon.obps;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang.StringUtils;
+import org.apache.log4j.Logger;
+import org.apache.tools.ant.BuildException;
+import org.openbravo.base.secureApp.VariablesSecureApp;
+import org.openbravo.dal.core.DalInitializingTask;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.erpCommon.utility.OBError;
+import org.openbravo.erpCommon.utility.Utility;
+import org.openbravo.model.ad.domain.List;
+import org.openbravo.model.ad.domain.Reference;
+import org.openbravo.scheduling.ProcessBundle;
+import org.openbravo.service.db.DalConnectionProvider;
+
+/**
+ * This class provides an ant task to activate instance in command line without need of use UI.
+ * 
+ * It can be executed with <code>ant activate.instance</code>
+ * 
+ * @author alostale
+ * 
+ */
+public class ActivationTask extends DalInitializingTask {
+
+  final static Logger log = Logger.getLogger(ActivationTask.class);
+
+  private static final String PURPOSE_REFERENCE_ID = "60E231391A7348DDA7171E780F62EF99";
+
+  private String publicKey;
+  private File publicKeyFile;
+  private String purpose;
+
+  @Override
+  protected void doExecute() {
+    verifyParameters();
+    VariablesSecureApp vars = new VariablesSecureApp("0", "0", "0");
+
+    Map<String, Object> params = new HashMap<String, Object>();
+    params.put("activate", true);
+    params.put("purpose", purpose);
+    params.put("publicKey", publicKey);
+
+    ProcessBundle pb = new ProcessBundle(null, vars);
+    pb.setParams(params);
+
+    try {
+      new ActiveInstanceProcess().execute(pb);
+      OBError msg = (OBError) pb.getResult();
+      boolean success = "Success".equals(msg.getType());
+      String msgTxt = Utility.parseTranslation(new DalConnectionProvider(false), vars, "en_US",
+          msg.getMessage());
+
+      if (success) {
+        log.info(msgTxt);
+      } else {
+        throw new BuildException(msgTxt);
+      }
+    } catch (Exception e) {
+      throw new BuildException(e);
+    }
+  }
+
+  private void verifyParameters() {
+    // Purpose
+    if (StringUtils.isEmpty(purpose)) {
+      throw new BuildException("purpose parameter is required");
+    } else {
+      Reference purposeRef = OBDal.getInstance().get(Reference.class, PURPOSE_REFERENCE_ID);
+      String msg = "";
+      boolean foundValue = false;
+      for (List value : purposeRef.getADListList()) {
+        if (purpose.equals(value.getSearchKey())) {
+          foundValue = true;
+          break;
+        }
+        msg += "\n  *" + value.getSearchKey() + " [" + value.getName() + "]";
+      }
+      if (!foundValue) {
+        throw new BuildException(purpose + " is not one of the valid values for purpose parameter:"
+            + msg);
+      }
+    }
+
+    // Public key
+    if (StringUtils.isEmpty(publicKey)
+        && (publicKeyFile == null || getProject().getBaseDir().equals(publicKeyFile))) {
+      throw new BuildException(
+          "Public key must be provided through one of publicKey or publicKeyFile parameter");
+    }
+
+    if (StringUtils.isNotEmpty(publicKey) && publicKeyFile != null
+        && !getProject().getBaseDir().equals(publicKeyFile)) {
+      throw new BuildException(
+          "Only one of the publicKey or publicKeyFile parameter can be set at the same time");
+    }
+
+    if (publicKeyFile != null && !getProject().getBaseDir().equals(publicKeyFile)) {
+      try {
+        publicKey = FileUtils.readFileToString(publicKeyFile, "utf-8");
+      } catch (IOException e) {
+        throw new BuildException(e);
+      }
+      if (StringUtils.isEmpty(publicKey)) {
+        throw new BuildException(publicKeyFile.getAbsolutePath() + " is empty");
+      }
+    }
+  }
+
+  public void setPublicKey(String publicKey) {
+    this.publicKey = publicKey;
+  }
+
+  public void setPublicKeyFile(File publicKeyFile) {
+    this.publicKeyFile = publicKeyFile;
+  }
+
+  public void setPurpose(String purpose) {
+    this.purpose = purpose;
+  }
+}
--- a/src/org/openbravo/erpCommon/obps/ActiveInstanceProcess.java	Tue May 22 14:14:35 2012 +0200
+++ b/src/org/openbravo/erpCommon/obps/ActiveInstanceProcess.java	Thu May 17 10:12:41 2012 +0200
@@ -68,6 +68,7 @@
     boolean localActivation = localActivationKey != null && !localActivationKey.isEmpty();
 
     if (!localActivation) {
+      log.info("Connecting to butler to get activation...");
       String instanceNo = (String) bundle.getParams().get("instanceNo");
       if (!HttpsUtils.isInternetAvailable()) {
         msg.setType("Error");
@@ -90,6 +91,7 @@
       if (localActivation) {
         activationKey = localActivationKey;
       } else {
+        log.info("Activation obtained from butler, activating instance...");
         activationKey = result[1];
       }
 
--- a/src/org/openbravo/erpCommon/obps/DisabledModules.java	Tue May 22 14:14:35 2012 +0200
+++ b/src/org/openbravo/erpCommon/obps/DisabledModules.java	Thu May 17 10:12:41 2012 +0200
@@ -55,7 +55,7 @@
    * Reloads information about disabled elements reading from database.
    */
   public static synchronized void reload() {
-    log4j.info("Loading disabled modules...");
+    log4j.debug("Loading disabled modules...");
     disabledModules = new ArrayList<String>();
     disabledWindows = new ArrayList<String>();
     disabledTabs = new ArrayList<String>();