IDEMPIERE-3551 1008086 - External (non-plugin) 2Pack automation

This commit is contained in:
Carlos Ruiz 2017-11-08 18:29:30 +01:00
parent da15fe4de4
commit c582c21810
10 changed files with 386 additions and 23 deletions

View File

@ -0,0 +1,15 @@
SET SQLBLANKLINES ON
SET DEFINE OFF
-- Jun 22, 2017 5:41:04 PM CEST
-- Automatic Packin
INSERT INTO AD_SysConfig (AD_SysConfig_ID,ConfigurationLevel,Updated,Value,Description,AD_SysConfig_UU,IsActive,Name,Created,AD_Org_ID,CreatedBy,UpdatedBy,AD_Client_ID,EntityType) VALUES (200101,'S',TO_DATE('2017-06-22 17:41:03','YYYY-MM-DD HH24:MI:SS'),'120','Delay in seconds to start applying the external automatic packin - see AUTOMATIC_PACKIN_FOLDERS','7db52ef2-ce9e-4989-b583-b64429fca5c8','Y','AUTOMATIC_PACKIN_INITIAL_DELAY',TO_DATE('2017-06-22 17:41:03','YYYY-MM-DD HH24:MI:SS'),0,100,100,0,'D')
;
-- Jun 22, 2017 5:44:09 PM CEST
INSERT INTO AD_SysConfig (AD_SysConfig_ID,ConfigurationLevel,Updated,Value,Description,AD_SysConfig_UU,IsActive,Name,Created,AD_Org_ID,CreatedBy,UpdatedBy,AD_Client_ID,EntityType) VALUES (200102,'S',TO_DATE('2017-06-22 17:44:09','YYYY-MM-DD HH24:MI:SS'),' ','List of folders separated by semicolon (;) - allowed is .zip file or folder containing .zip files (recursive) - filename notation yyyymmddHHMM_ClientValue_InformationalDescription.zip','5468245a-0bb1-4357-b293-2ffb81c7cf2d','Y','AUTOMATIC_PACKIN_FOLDERS',TO_DATE('2017-06-22 17:44:09','YYYY-MM-DD HH24:MI:SS'),0,100,100,0,'D')
;
SELECT register_migration_script('201706221744_AutomaticExternalPackin.sql') FROM dual
;

View File

@ -0,0 +1,12 @@
-- Jun 22, 2017 5:41:04 PM CEST
-- Automatic Packin
INSERT INTO AD_SysConfig (AD_SysConfig_ID,ConfigurationLevel,Updated,Value,Description,AD_SysConfig_UU,IsActive,Name,Created,AD_Org_ID,CreatedBy,UpdatedBy,AD_Client_ID,EntityType) VALUES (200101,'S',TO_TIMESTAMP('2017-06-22 17:41:03','YYYY-MM-DD HH24:MI:SS'),'120','Delay in seconds to start applying the external automatic packin - see AUTOMATIC_PACKIN_FOLDERS','7db52ef2-ce9e-4989-b583-b64429fca5c8','Y','AUTOMATIC_PACKIN_INITIAL_DELAY',TO_TIMESTAMP('2017-06-22 17:41:03','YYYY-MM-DD HH24:MI:SS'),0,100,100,0,'D')
;
-- Jun 22, 2017 5:44:09 PM CEST
INSERT INTO AD_SysConfig (AD_SysConfig_ID,ConfigurationLevel,Updated,Value,Description,AD_SysConfig_UU,IsActive,Name,Created,AD_Org_ID,CreatedBy,UpdatedBy,AD_Client_ID,EntityType) VALUES (200102,'S',TO_TIMESTAMP('2017-06-22 17:44:09','YYYY-MM-DD HH24:MI:SS'),' ','List of folders separated by semicolon (;) - allowed is .zip file or folder containing .zip files (recursive) - filename notation yyyymmddHHMM_ClientValue_InformationalDescription.zip','5468245a-0bb1-4357-b293-2ffb81c7cf2d','Y','AUTOMATIC_PACKIN_FOLDERS',TO_TIMESTAMP('2017-06-22 17:44:09','YYYY-MM-DD HH24:MI:SS'),0,100,100,0,'D')
;
SELECT register_migration_script('201706221744_AutomaticExternalPackin.sql') FROM dual
;

View File

@ -42,7 +42,7 @@ public class MSysConfig extends X_AD_SysConfig
/**
*
*/
private static final long serialVersionUID = -7805174199114873428L;
private static final long serialVersionUID = -4917976995339273240L;
public static final String ADDRESS_VALIDATION = "ADDRESS_VALIDATION";
public static final String ALERT_SEND_ATTACHMENT_AS_XLS = "ALERT_SEND_ATTACHMENT_AS_XLS";
@ -59,10 +59,12 @@ public class MSysConfig extends X_AD_SysConfig
public static final String APPLICATION_MAIN_VERSION = "APPLICATION_MAIN_VERSION";
public static final String APPLICATION_MAIN_VERSION_SHOWN = "APPLICATION_MAIN_VERSION_SHOWN";
public static final String APPLICATION_OS_INFO_SHOWN = "APPLICATION_OS_INFO_SHOWN";
public static final String AUTOMATIC_PACKIN_PROCESSING = "AUTOMATIC_PACKIN_PROCESSING";
public static final String AUTOMATIC_PACKIN_TIMEOUT = "AUTOMATIC_PACKIN_TIMEOUT";
public static final String AUTOMATIC_PACKIN_RETRIES = "AUTOMATIC_PACKIN_RETRIES";
public static final String ATTACH_EMBEDDED_2PACK = "ATTACH_EMBEDDED_2PACK";
public static final String AUTOMATIC_PACKIN_FOLDERS = null;
public static final String AUTOMATIC_PACKIN_INITIAL_DELAY = null;
public static final String AUTOMATIC_PACKIN_PROCESSING = "AUTOMATIC_PACKIN_PROCESSING";
public static final String AUTOMATIC_PACKIN_RETRIES = "AUTOMATIC_PACKIN_RETRIES";
public static final String AUTOMATIC_PACKIN_TIMEOUT = "AUTOMATIC_PACKIN_TIMEOUT";
public static final String BACKGROUND_JOB_ALLOWED = "BACKGROUND_JOB_ALLOWED";
public static final String BACKGROUND_JOB_BY_DEFAULT = "BACKGROUND_JOB_BY_DEFAULT";
public static final String BACKGROUND_JOB_MAX_IN_SYSTEM = "BACKGROUND_JOB_MAX_IN_SYSTEM";
@ -151,8 +153,8 @@ public class MSysConfig extends X_AD_SysConfig
public static final String ZK_BROWSER_TITLE = "ZK_BROWSER_TITLE";
public static final String ZK_BUTTON_STYLE = "ZK_BUTTON_STYLE";
public static final String ZK_DASHBOARD_CALENDAR_REQUEST_DISPLAY_MODE = "ZK_DASHBOARD_CALENDAR_REQUEST_DISPLAY_MODE";
public static final String ZK_DASHBOARD_PERFORMANCE_TIMEOUT = "ZK_DASHBOARD_PERFORMANCE_TIMEOUT";
public static final String ZK_DASHBOARD_PERFORMANCE_REFRESH_INTERVAL = "ZK_DASHBOARD_PERFORMANCE_REFRESH_INTERVAL";
public static final String ZK_DASHBOARD_PERFORMANCE_TIMEOUT = "ZK_DASHBOARD_PERFORMANCE_TIMEOUT";
public static final String ZK_DASHBOARD_REFRESH_INTERVAL = "ZK_DASHBOARD_REFRESH_INTERVAL";
public static final String ZK_DECIMALBOX_PROCESS_DOTKEYPAD = "ZK_DECIMALBOX_PROCESS_DOTKEYPAD";
public static final String ZK_DESKTOP_CLASS = "ZK_DESKTOP_CLASS";

View File

@ -13,6 +13,7 @@ Import-Package: org.apache.commons.codec,
Export-Package: org.adempiere.pipo.srv,
org.adempiere.pipo2,
org.adempiere.pipo2.exception
Require-Bundle: org.adempiere.base;bundle-version="0.0.0"
Require-Bundle: org.adempiere.base;bundle-version="0.0.0",
org.adempiere.plugin.utils;bundle-version="0.0.0"
Service-Component: OSGI-INF/dictionaryservice.xml
Bundle-ActivationPolicy: lazy
Bundle-Activator: org.adempiere.plugin.utils.PackInApplicationActivator

View File

@ -47,6 +47,10 @@ public class PipoDictionaryService implements IDictionaryService {
trxName = Trx.createTrxName("PipoDS");
Trx.get(trxName, true).setDisplayName(getClass().getName()+"_merge");
packIn = new PackIn();
//external files must not start with "2Pack" prefix in order to work correctly
if ("org.adempiere.pipo".equals(context.getBundle().getSymbolicName()) && !packageFile.getName().startsWith("2Pack"))
packIn.setPackageName(packageFile.getName());
else
packIn.setPackageName(context.getBundle().getSymbolicName());
if (Env.getCtx().getProperty("#AD_Client_ID") == null) {

View File

@ -64,7 +64,7 @@ public abstract class AbstractActivator implements BundleActivator, ServiceTrack
Query q = new Query(Env.getCtx(), X_AD_Package_Imp.Table_Name,
where.toString(), null);
q.setParameters(params);
return q.first() != null;
return q.match();
}
public abstract String getName();

View File

@ -10,6 +10,7 @@ import java.util.logging.Level;
import org.adempiere.base.IDictionaryService;
import org.adempiere.util.ServerContext;
import org.compiere.Adempiere;
import org.compiere.model.MSession;
import org.compiere.model.Query;
import org.compiere.model.ServerStateChangeEvent;
import org.compiere.model.ServerStateChangeListener;
@ -104,6 +105,8 @@ public class AdempiereActivator extends AbstractActivator {
protected void packIn() {
URL packout = context.getBundle().getEntry("/META-INF/2Pack.zip");
if (packout != null && service != null) {
//Create Session to be able to create records in AD_ChangeLog
MSession.get(Env.getCtx(), true);
FileOutputStream zipstream = null;
try {
// copy the resource to a temporary file to process it with 2pack
@ -211,6 +214,7 @@ public class AdempiereActivator extends AbstractActivator {
protected void setupPackInContext() {
Properties serverContext = new Properties();
serverContext.setProperty("#AD_Client_ID", "0");
ServerContext.setCurrentInstance(serverContext);
};
}

View File

@ -28,6 +28,7 @@ import java.util.logging.Level;
import org.adempiere.base.IDictionaryService;
import org.adempiere.util.ServerContext;
import org.compiere.Adempiere;
import org.compiere.model.MSession;
import org.compiere.model.Query;
import org.compiere.model.ServerStateChangeEvent;
import org.compiere.model.ServerStateChangeListener;
@ -215,6 +216,8 @@ public class Incremental2PackActivator extends AbstractActivator {
protected boolean packIn(URL packout) {
if (packout != null && service != null) {
//Create Session to be able to create records in AD_ChangeLog
MSession.get(Env.getCtx(), true);
String path = packout.getPath();
String suffix = "_"+path.substring(path.lastIndexOf("2Pack_"));
System.out.println("Installing " + getName() + " " + path + " ...");
@ -334,6 +337,7 @@ public class Incremental2PackActivator extends AbstractActivator {
protected void setupPackInContext() {
Properties serverContext = new Properties();
serverContext.setProperty("#AD_Client_ID", "0");
ServerContext.setCurrentInstance(serverContext);
};
}

View File

@ -0,0 +1,317 @@
/******************************************************************************
* Copyright (C) 2017 Diego Ruiz *
* Copyright (C) 2017 Trek Global *
* This program is free software; you can redistribute it and/or modify it *
* under the terms version 2 of the GNU General Public License as published *
* by the Free Software Foundation. This program is distributed in the hope *
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU General Public License for more details. *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
*****************************************************************************/
package org.adempiere.plugin.utils;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import org.adempiere.base.IDictionaryService;
import org.adempiere.util.ServerContext;
import org.compiere.Adempiere;
import org.compiere.model.MClient;
import org.compiere.model.MSession;
import org.compiere.model.MSysConfig;
import org.compiere.model.Query;
import org.compiere.model.ServerStateChangeEvent;
import org.compiere.model.ServerStateChangeListener;
import org.compiere.util.AdempiereSystemError;
import org.compiere.util.CLogger;
import org.compiere.util.Env;
import org.compiere.util.Util;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;
public class PackInApplicationActivator extends AbstractActivator {
protected final static CLogger logger = CLogger.getCLogger(PackInApplicationActivator.class.getName());
private List<File> filesToProcess = new ArrayList<>();
private File currentFile;
@Override
public void start(BundleContext context) throws Exception {
this.context = context;
if (logger.isLoggable(Level.INFO)) logger.info(getName() + " starting...");
serviceTracker = new ServiceTracker<IDictionaryService, IDictionaryService>(context, IDictionaryService.class.getName(), this);
serviceTracker.open();
start();
if (logger.isLoggable(Level.INFO))
logger.info(getName() + " ready.");
}
/**
* call when bundle have been started ( after this.context have been set )
*/
protected void start() {
};
/**
* call when bundle is stop ( before this.context is set to null )
*/
protected void stop() {
}
@Override
public void stop(BundleContext context) throws Exception {
stop();
serviceTracker.close();
this.context = null;
if (logger.isLoggable(Level.INFO)) logger.info(context.getBundle().getSymbolicName() + " "
+ context.getBundle().getHeaders().get("Bundle-Version")
+ " stopped.");
}
@Override
public IDictionaryService addingService(ServiceReference<IDictionaryService> reference) {
service = context.getService(reference);
if (Adempiere.getThreadPoolExecutor() != null) {
Adempiere.getThreadPoolExecutor().execute(new Runnable() {
@Override
public void run() {
automaticPackin();
}
});
} else {
Adempiere.addServerStateChangeListener(new ServerStateChangeListener() {
@Override
public void stateChange(ServerStateChangeEvent event) {
if (event.getEventType() == ServerStateChangeEvent.SERVER_START && service != null) {
automaticPackin();
}
}
});
}
return null;
}
private void automaticPackin() {
//Initial delay
Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(PackInApplicationActivator.class.getClassLoader());
setupPackInContext();
installPackages();
} finally {
ServerContext.dispose();
service = null;
Thread.currentThread().setContextClassLoader(cl);
}
t.cancel();
}
}, MSysConfig.getIntValue(MSysConfig.AUTOMATIC_PACKIN_INITIAL_DELAY, 120) * 1000);
}
private void installPackages() {
String folders = MSysConfig.getValue(MSysConfig.AUTOMATIC_PACKIN_FOLDERS);
if (Util.isEmpty(folders, true)) {
logger.log(Level.INFO, "Not specified folders for automatic packin");
return;
}
File[] fileArray = getFilesToProcess(folders);
if (fileArray.length <= 0) {
System.out.println("No zip files to process");
return;
}
try {
if (getDBLock()) {
//Create Session to be able to create records in AD_ChangeLog
MSession.get(Env.getCtx(), true);
for(File zipFile : fileArray) {
currentFile = zipFile;
if (!packIn(zipFile)) {
// stop processing further packages if one fail
break;
}
filesToProcess.remove(zipFile);
}
releaseLock();
} else {
logger.log(Level.SEVERE, "Could not acquire the DB lock to automatically install the packins");
}
} catch (AdempiereSystemError e) {
e.printStackTrace();
}
if (filesToProcess.size() > 0) {
System.out.println("The following packages were not applied: ");
for (File file : filesToProcess) {
System.out.println(file.getName());
}
}
}
private boolean packIn(File packinFile) {
if (packinFile != null && service != null) {
String fileName = packinFile.getName();
System.out.println("Installing " + fileName + " ...");
// The convention for package names is: yyyymmddHHMM_ClientValue_InformationalDescription.zip
String [] parts = fileName.split("_");
String clientValue = parts[1];
MClient client = getClient(clientValue);
if (client == null) {
logger.log(Level.SEVERE, "Client does not exist: " + clientValue);
return false;
}
Env.setContext(Env.getCtx(), "#AD_Client_ID", client.getAD_Client_ID());
try {
// call 2pack
if (!merge(packinFile, null))
return false;
} catch (Throwable e) {
logger.log(Level.SEVERE, "Pack in failed.", e);
return false;
}
System.out.println(packinFile.getPath() + " installed");
}
return true;
}
private File[] getFilesToProcess(String folders) {
String filePaths[] = null;
if (folders.indexOf(";") > 0) {
filePaths = folders.split("[;]");
} else {
filePaths = new String[]{folders};
}
//Add files to the array
for (String filePath : filePaths) {
File toProcess = new File(filePath.trim());
if (!toProcess.exists()) {
logger.log(Level.SEVERE, filePath + " does not exist");
continue;
}
processFilePath(toProcess);
}
//Verify installed packages and remove those that were installed previously
verifyInstalledPackages();
File[] fileArray = filesToProcess.toArray(new File[filesToProcess.size()]);
// Sort files by name
Arrays.sort(fileArray, new Comparator<File>() {
@Override
public int compare(File f1, File f2) {
return f1.getName().compareTo(f2.getName());
}
});
return fileArray;
}
private void processFilePath(File toProcess) {
if (toProcess.isFile()) {
if (toProcess.getName().toLowerCase().endsWith(".zip"))
filesToProcess.add(toProcess);
else {
logger.log(Level.SEVERE, toProcess.getName() + " is not a valid .zip file");
return;
}
} else if (toProcess.isDirectory()) {
FileFilter filter = new FileFilter() {
public boolean accept(File file) {
if (file.getName().toUpperCase().endsWith(".ZIP") || file.isDirectory())
return true;
else
return false;
}
};
boolean found = false;
for (File fileToProcess : toProcess.listFiles(filter)) {
if (!found) {
System.out.println("*** Creating list from folder " + toProcess.toString());
found = true;
}
if (fileToProcess.isDirectory())
processFilePath(fileToProcess);
else
filesToProcess.add(fileToProcess);
}
if (!found) {
logger.log(Level.SEVERE, toProcess.getName() + " does not have .zip files or subfolders");
return;
}
} else {
logger.log(Level.SEVERE, toProcess.getName() + " not a file or folder");
}
}
private void verifyInstalledPackages() {
Iterator<File> iterator = filesToProcess.iterator();
while(iterator.hasNext()){
currentFile = iterator.next();
if (installedPackage(null)) {
logger.log(Level.INFO, currentFile.getName() + " already installed. Removing it from the list...");
iterator.remove();
}
}
}
private MClient getClient(String clientValue) {
String where = "upper(Value) = upper(?)";
Query q = new Query(Env.getCtx(), MClient.Table_Name,
where, null)
.setParameters(new Object[] {clientValue})
.setOnlyActiveRecords(true);
return q.first();
}
@Override
public void modifiedService(ServiceReference<IDictionaryService> reference, IDictionaryService service) {
}
@Override
public void removedService(ServiceReference<IDictionaryService> reference, IDictionaryService service) {
}
@Override
public String getName() {
if (currentFile != null)
return currentFile.getName();
else
return "";
}
protected void setupPackInContext() {
Properties serverContext = new Properties();
serverContext.setProperty("#AD_Client_ID", "0");
ServerContext.setCurrentInstance(serverContext);
};
}

View File

@ -28,6 +28,7 @@ import java.util.logging.Level;
import org.adempiere.base.IDictionaryService;
import org.adempiere.util.ServerContext;
import org.compiere.Adempiere;
import org.compiere.model.MSession;
import org.compiere.model.Query;
import org.compiere.model.ServerStateChangeEvent;
import org.compiere.model.ServerStateChangeListener;
@ -160,6 +161,8 @@ public class Version2PackActivator extends AbstractActivator {
try {
if (getDBLock()) {
//Create Session to be able to create records in AD_ChangeLog
MSession.get(Env.getCtx(), true);
for(TwoPackEntry entry : list) {
if (!packIn(entry.url)) {
// stop processing further packages if one fail
@ -304,6 +307,7 @@ public class Version2PackActivator extends AbstractActivator {
protected void setupPackInContext() {
Properties serverContext = new Properties();
serverContext.setProperty("#AD_Client_ID", "0");
ServerContext.setCurrentInstance(serverContext);
};
}