package fr.gouv.finances.dgfip.xemelios.batch.exports;
-import fr.gouv.finances.dgfip.xemelios.batch.PostProcessor;
+import java.io.File;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
import fr.gouv.finances.cp.xemelios.export.ExportUndertaker;
import fr.gouv.finances.cp.xemelios.export.Exporter;
import fr.gouv.finances.dgfip.xemelios.auth.XemeliosUser;
import fr.gouv.finances.dgfip.xemelios.batch.Batch;
import fr.gouv.finances.dgfip.xemelios.batch.BatchRunner;
+import fr.gouv.finances.dgfip.xemelios.batch.PostProcessor;
import fr.gouv.finances.dgfip.xemelios.common.config.DocumentModel;
import fr.gouv.finances.dgfip.xemelios.common.config.DocumentsModel;
import fr.gouv.finances.dgfip.xemelios.common.config.ElementModel;
import fr.gouv.finances.dgfip.xemelios.export.ConfigModel;
import fr.gouv.finances.dgfip.xemelios.export.ExportJob;
import fr.gouv.finances.dgfip.xemelios.utils.FileUtils;
-import java.io.File;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.ArrayList;
-import java.util.List;
-import org.apache.log4j.Logger;
/**
- *
+ *
* @author Christophe Marchand <christophe.marchand@axyus.com>
*/
public class ExportRunner extends Batch {
- private static final Logger logger = Logger.getLogger(ExportRunner.class);
-
- public static final String PROP_PRIORITY_LEVEL = "priority.level";
- public static final String PROP_MAX_CONCURRENT_THREADS = "max.concurrent.threads";
- public static final String PROP_DOCUMENTS_DEF_DIR = "documents-def.dir";
-// public static final String PROP_DATALAYER_NAME = "datalayer.name";
- public static final String PROP_UNDERTAKER = "exporter.class.name";
-
- private int priorityLevel = 1;
- private int maxConcurrentThreads = 1;
-
- private String exportTempDirectory = null;
- private String undertakerClassName;
-
- private static boolean initialized = false;
-
- private ThreadPool<ExportRunnerImpl> pool = null;
-
- public ExportRunner(String args[]) {
- super(args);
- }
-
- @Override
- protected void initialize() throws Exception {
- super.initialize();
- String sTmp = getProps().getProperty(PROP_MAX_CONCURRENT_THREADS);
- if(sTmp!=null && sTmp.length()>0) {
- try {
- maxConcurrentThreads = Integer.parseInt(sTmp);
- } catch(Throwable t) {}
- }
- try {
- String s = getProps().getProperty(PROP_PRIORITY_LEVEL);
- if(s!=null && s.length()>0) {
- priorityLevel = Integer.parseInt(s);
- }
- } catch(Throwable t) {}
- undertakerClassName = getProps().getProperty(PROP_UNDERTAKER);
- }
-
- @Override
- protected void doProcess() throws Exception {
- logger.debug("running export.runner for priority "+priorityLevel);
- Connection con = null;
- Statement st = null;
- PreparedStatement ps = null;
- ResultSet rs = null;
- ArrayList<ExportRunnerImpl> threads = new ArrayList<ExportRunnerImpl>();
- try {
- con = PoolManager.getInstance().getConnection();
- String sqlParam = "SELECT PARAM_VALUE FROM PARAMETERS WHERE PARAM_NAME='export.temp.directory'";
- st = con.createStatement();
- rs = st.executeQuery(sqlParam);
- if(rs.next()) {
- exportTempDirectory = rs.getString(1);
- rs.close();
- } else {
- logger.error("parameter 'export.temp.directory' not set !");
- throw new Exception("parameter 'export.temp.directory' not set !");
- }
- String statusColName = null;
- if("mysql".equals(DataLayerManager.getImplementation().getLayerName()))
- statusColName = "STATUS";
- else if("oracle".equals(DataLayerManager.getImplementation().getLayerName()))
- statusColName = "EXPORT_STATUS";
- String sql = "SELECT EXPORT_ID, UTI_LOGIN FROM EXPORT_JOBS WHERE "+statusColName+"=1 AND PRIORITY=? AND CLEANED IS NULL";
- ps = con.prepareStatement(sql);
- ps.setInt(1,priorityLevel);
- rs = ps.executeQuery();
- while(rs.next()) {
- Long exportId = rs.getLong(1);
- String user = rs.getString(2);
- logger.debug("planning ("+exportId+","+user+") for priority "+priorityLevel+" - threads.size="+threads.size());
- ExportRunnerImpl impl = new ExportRunnerImpl(exportId, user,priorityLevel,exportTempDirectory,BatchRunner.getInstance().getDocuments(),undertakerClassName,getPostProcessors());
- logger.debug("ExportRunnerImpl created");
- threads.add(impl);
- logger.debug("thread "+exportId+","+user+"added, size is now "+threads.size());
- }
- logger.debug("creating pool for priority "+priorityLevel);
- pool = new ThreadPool<ExportRunnerImpl>(threads);
- logger.debug("starting pool for priority "+priorityLevel+" with "+maxConcurrentThreads+" max concurrent threads");
- pool.start(maxConcurrentThreads);
- } catch(SQLException sqlEx) {
- logger.error("doProcess()",sqlEx);
- } catch(Throwable t) {
- t.printStackTrace();
- } finally {
- if(con!=null) PoolManager.getInstance().releaseConnection(con);
- }
- }
-
- @Override
- public String getResumeTraitement() {
- return "export.runner.resume";
- }
-
- @Override
- public String typeTraitementRefCode() {
- return "export.runner<"+priorityLevel+">";
- }
-
- @Override
- public String getInformations() {
- StringBuilder sb = new StringBuilder();
- if(isStarted()) {
- sb.append(typeTraitementRefCode()).append(" - ").append(pool.getRunningThreadCount()).append(" running thread(s)\n");
- for(RunnableBatch rb: pool.getThreads()) {
- sb.append("\t").append(rb.getName()).append(": ").append(rb.isAlive()?"running":"idle").append("\n");
- if(rb.isAlive()) sb.append(rb.getInformations());
- }
- } else {
- sb.append(typeTraitementRefCode()).append(" - not started\n");
- }
- return sb.toString();
- }
-
- @Override
- protected String getBatchVersion() {
- return "1.0";
- }
-
- protected class ExportRunnerImpl extends RunnableBatch {
- private Long exportId;
- private String user;
- private int priority;
- private String exportDirectory;
- private DocumentsModel docs;
- private String undertakerClassName;
- private List<String> postProcessors;
-
- private ExportJob job = null;
-
- private File generatedFile;
-
- private PostProcessor pp;
-
- private boolean exportTerminated = false;
-
- public ExportRunnerImpl(Long exportId, String user, int priority, String exportDirectory, DocumentsModel docs, String undertakerClassName,List<String> postProcessors) {
- super();
- logger.debug("ExportRunnerImpl<init>("+exportId+","+user+")");
- this.exportId=exportId;
- this.user=user;
- this.priority=priority;
- this.exportDirectory=exportDirectory;
- this.docs=docs;
- this.undertakerClassName=undertakerClassName;
- this.postProcessors=postProcessors;
- setName("ExportRunnerImpl<"+exportId+","+user+">");
-
- switch(priority) {
- case 2: { setPriority(Thread.MIN_PRIORITY); break; }
- default: { setPriority(Thread.NORM_PRIORITY); break; }
- }
- }
-
- @Override
- public void run() {
- logger.debug("running ExportRunnerImpl for "+user+"/"+exportId);
- Connection con = null;
- File temp = null;
- String statusColName = null;
- String currentDate = null;
- try {
- if("mysql".equals(DataLayerManager.getImplementation().getLayerName())) {
- statusColName = "STATUS";
- currentDate = "NOW()";
- } else if("oracle".equals(DataLayerManager.getImplementation().getLayerName())) {
- statusColName = "EXPORT_STATUS";
- currentDate="SYSDATE";
- }
- job = DataLayerManager.getImplementation().getExportJob(exportId,user);
- con = PoolManager.getInstance().getConnection();
- String sql = "UPDATE EXPORT_JOBS SET "+statusColName+"="+ExportJob.STATUS_RUNNING+", RUN_DATE="+currentDate+" WHERE EXPORT_ID=? AND UTI_LOGIN=?";
- PreparedStatement ps = con.prepareStatement(sql);
- ps.setLong(1, exportId);
- ps.setString(2,user);
- ps.executeUpdate();
- // on est obligé de la fermer ici, car l'export peut être très long
- PoolManager.getInstance().releaseConnection(con); con=null;
- DataResultSet drs = job.getDataResultSet();
- File dir = new File(exportDirectory);
- temp = File.createTempFile("exp-", null, dir);
- temp.delete();
- temp.mkdir();
- String fileName = null;
- if(job.getFileName().toLowerCase().endsWith(".xls")) fileName = job.getFileName();
- else fileName = job.getFileName().concat(".xls");
- generatedFile = new File(temp,fileName);
- DocumentModel dm = docs.getDocumentById(job.getDocumentId());
- User user2 = new User(user); // job.getOwner()
-logger.debug("getting ConfigModel");
- ConfigModel cm = DataLayerManager.getImplementation().getConfigExport(job.getDocumentId(), job.getEtatId(), job.getExportConfigId().toString(), user2);
-logger.debug("creating MyExporter");
- MyExporter exporter = new MyExporter(generatedFile, drs, dm, job.getEtatId(), dm.getEtatById(job.getEtatId()).getElementById(job.getElementId()), cm, this, undertakerClassName);
-logger.debug("running myExporter");
- exporter.runImmediatly();
- } catch(Exception ex) {
- logger.error("run()",ex);
- try {
- if(con==null)
- con = PoolManager.getInstance().getConnection();
- String sql = "UPDATE EXPORT_JOBS SET "+statusColName+"="+ExportJob.STATUS_ERROR+", ERROR_MESSAGE=? WHERE EXPORT_ID=? AND UTI_LOGIN=?";
- PreparedStatement ps = con.prepareStatement(sql);
- ps.setString(1,ex.getMessage());
- ps.setLong(2, exportId);
- ps.setString(3, user);
- ps.executeUpdate();
- } catch(SQLException sqlEx) {}
- if(temp!=null) {
- if(temp.exists()) {
- if(temp.isDirectory()) {
- try {
- FileUtils.dropRecursiveDirectory(temp);
- } catch(Exception ex2) {}
- } else {
- temp.delete();
- }
- }
- }
- } finally {
- if(con!=null) PoolManager.getInstance().releaseConnection(con);
- // c'est fait par le MyExporter
- // getNotifier().notifyThreadStop(this);
- }
- }
- @SuppressWarnings("unchecked")
- public void notifyTerminated() {
- exportTerminated = true;
- Connection con = null;
- String statusColName = null;
- try {
- if("mysql".equals(DataLayerManager.getImplementation().getLayerName())) {
- statusColName = "STATUS";
- } else if("oracle".equals(DataLayerManager.getImplementation().getLayerName())) {
- statusColName = "EXPORT_STATUS";
- }
- con = PoolManager.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("UPDATE EXPORT_JOBS SET GENERATED_FILE_NAME=? WHERE EXPORT_ID=? AND UTI_LOGIN=?");
- ps.setString(1, generatedFile.getAbsolutePath());
- ps.setLong(2, exportId);
- ps.setString(3,user);
- ps.executeUpdate();
- PoolManager.getInstance().releaseConnection(con); con=null;
- job.setGeneratedFileName(generatedFile.getAbsolutePath());
-
- // post processors
- for(String postProcessor:postProcessors) {
- pp = null;
- try {
- Class clazz = Class.forName(postProcessor);
- Object o = clazz.newInstance();
- pp = (PostProcessor)o;
- } catch(Exception ex) {
- logger.error("instanciating post processor",ex);
- }
- if(pp!=null) {
- pp.setConfigData(job);
- pp.postProcess();
- }
- }
-
- con = PoolManager.getInstance().getConnection();
- ps=con.prepareStatement("UPDATE EXPORT_JOBS SET "+statusColName+"="+ExportJob.STATUS_TERMINATED+" WHERE EXPORT_ID=? AND UTI_LOGIN=?");
- ps.setLong(1, exportId);
- ps.setString(2, user);
- ps.executeUpdate();
-
- } catch(Exception ex) {
- logger.error("notifyTerminated()",ex);
- try {
- String sql = "UPDATE EXPORT_JOBS SET "+statusColName+"="+ExportJob.STATUS_ERROR+", ERROR_MESSAGE=? WHERE EXPORT_ID=? AND UTI_LOGIN=?";
- if(con==null) con = PoolManager.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement(sql);
- ps.setString(1,ex.getMessage());
- ps.setLong(2, exportId);
- ps.setString(3, user);
- ps.executeUpdate();
- } catch(SQLException sqlEx) {}
- // cleanning
- File generated = new File(job.getGeneratedFileName());
- if(generated.exists()) {
- try {
- FileUtils.dropRecursiveDirectory(generated.getParentFile());
- } catch(Throwable t) {}
- }
- } finally {
- if(con!=null) {
- PoolManager.getInstance().releaseConnection(con);
- con=null;
- }
- }
- getNotifier().notifyThreadStop(this);
- }
-
- @Override
- public String getInformations() {
- StringBuilder sb = new StringBuilder();
- sb.append("\t").append(getName()).append(":");
- if(isAlive()) {
- sb.append("export running.");
- if(exportTerminated) {
- sb.append(" Main export terminated.");
- if(pp!=null) {
- sb.append(" Running post-processor ").append(pp.getClass().getName()).append(".");
- }
- } else {
- sb.append(" Main export running.");
- }
- } else {
- if(isStarted()) {
- sb.append("export fully terminated.");
- } else {
- sb.append("idle");
- }
- }
- sb.append("\n");
- return sb.toString();
- }
- }
-
- private class User implements XemeliosUser {
- private String userId;
- public User(String userId) {
- super();
- this.userId = userId;
- }
- @Override
- public String getId() {
- return userId;
- }
- @Override
- public String getDisplayName() {
- return userId;
- }
- @Override
- public boolean hasRole(String role) {
- return true;
- }
- @Override
- public boolean hasDocument(String document) {
- return true;
- }
- @Override
- public boolean hasCollectivite(String collectivite, DocumentModel dm) {
- return true;
- }
- }
-
- private class MyExporter extends Exporter {
- private ExportRunnerImpl impl;
- private String undertakerClassName;
-
- public MyExporter(File f, DataResultSet drs, DocumentModel dm, String etatId, ElementModel em, ConfigModel cf, ExportRunnerImpl notifier, String undertakerClassName) {
- super(f,drs,dm,etatId,em,cf,ExportRunner.this.getApplicationConfiguration());
- this.undertakerClassName=undertakerClassName;
- if(this.undertakerClassName==null || this.undertakerClassName.length()==0) {
- this.undertakerClassName = "fr.gouv.finances.cp.xemelios.export.JxlExporter";
- }
- this.impl=notifier;
- }
- public void runImmediatly() throws Exception {
- doExport();
- cleanAfterRun();
- impl.notifyTerminated();
- }
- @Override
- protected void done() {
-logger.debug("MyExporter.done()");
- super.done();
- impl.notifyTerminated();
- }
-
- @Override
- protected ExportUndertaker getUndertaker() {
- try {
- Class undertakerClass = Class.forName(undertakerClassName);
- Class[] exporterArgsClass = new Class[]{Exporter.class};
- Object[] exporterArgs = new Object[]{this};
- Constructor constructor = undertakerClass.getConstructor(exporterArgsClass);
- ExportUndertaker undertaker = (ExportUndertaker) constructor.newInstance(exporterArgs);
-
- return undertaker;
- } catch (ClassNotFoundException e) {
- logger.error("",e);
- return null;
- } catch (NoSuchMethodException e) {
- logger.error("",e);
- return null;
- } catch (InstantiationException e) {
- logger.error("",e);
- return null;
- } catch (IllegalAccessException e) {
- logger.error("",e);
- return null;
- } catch (IllegalArgumentException e) {
- logger.error("",e);
- return null;
- } catch (InvocationTargetException e) {
- logger.error("",e);
- return null;
- }
- }
-
- }
+ private static final Logger logger = Logger.getLogger(ExportRunner.class);
+
+ public static final String PROP_PRIORITY_LEVEL = "priority.level";
+ public static final String PROP_MAX_CONCURRENT_THREADS = "max.concurrent.threads";
+ public static final String PROP_DOCUMENTS_DEF_DIR = "documents-def.dir";
+ // public static final String PROP_DATALAYER_NAME = "datalayer.name";
+ public static final String PROP_UNDERTAKER = "exporter.class.name";
+
+ private int priorityLevel = 1;
+ private int maxConcurrentThreads = 1;
+
+ private String exportTempDirectory = null;
+ private String undertakerClassName;
+
+ private ThreadPool<ExportRunnerImpl> pool = null;
+
+ public ExportRunner(final String args[]) {
+ super(args);
+ }
+
+ @Override
+ protected void initialize() throws Exception {
+ super.initialize();
+ final String sTmp = this.getProps().getProperty(PROP_MAX_CONCURRENT_THREADS);
+ if (sTmp != null && sTmp.length() > 0) {
+ try {
+ this.maxConcurrentThreads = Integer.parseInt(sTmp);
+ } catch (final Throwable t) {
+ }
+ }
+ try {
+ final String s = this.getProps().getProperty(PROP_PRIORITY_LEVEL);
+ if (s != null && s.length() > 0) {
+ this.priorityLevel = Integer.parseInt(s);
+ }
+ } catch (final Throwable t) {
+ }
+ this.undertakerClassName = this.getProps().getProperty(PROP_UNDERTAKER);
+ }
+
+ @Override
+ protected void doProcess() throws Exception {
+ logger.debug("running export.runner for priority " + this.priorityLevel);
+ Connection con = null;
+ Statement st = null;
+ PreparedStatement ps = null;
+ ResultSet rs = null;
+ final ArrayList<ExportRunnerImpl> threads = new ArrayList<ExportRunnerImpl>();
+ try {
+ con = PoolManager.getInstance().getConnection();
+ final String sqlParam = "SELECT PARAM_VALUE FROM PARAMETERS WHERE PARAM_NAME='export.temp.directory'";
+ st = con.createStatement();
+ rs = st.executeQuery(sqlParam);
+ if (rs.next()) {
+ this.exportTempDirectory = rs.getString(1);
+ rs.close();
+ } else {
+ logger.error("parameter 'export.temp.directory' not set !");
+ throw new Exception("parameter 'export.temp.directory' not set !");
+ }
+ String statusColName = null;
+ if ("mysql".equals(DataLayerManager.getImplementation().getLayerName())) {
+ statusColName = "STATUS";
+ } else if ("oracle".equals(DataLayerManager.getImplementation().getLayerName())) {
+ statusColName = "EXPORT_STATUS";
+ }
+ final String sql = "SELECT EXPORT_ID, UTI_LOGIN FROM EXPORT_JOBS WHERE " + statusColName + "=1 AND PRIORITY=? AND CLEANED IS NULL";
+ ps = con.prepareStatement(sql);
+ ps.setInt(1, this.priorityLevel);
+ rs = ps.executeQuery();
+ while (rs.next()) {
+ final Long exportId = rs.getLong(1);
+ final String user = rs.getString(2);
+ logger.debug("planning (" + exportId + "," + user + ") for priority " + this.priorityLevel + " - threads.size=" + threads.size());
+ final ExportRunnerImpl impl = new ExportRunnerImpl(exportId, user, this.priorityLevel, this.exportTempDirectory, BatchRunner.getInstance().getDocuments(), this.undertakerClassName, this.getPostProcessors());
+ logger.debug("ExportRunnerImpl created");
+ threads.add(impl);
+ logger.debug("thread " + exportId + "," + user + "added, size is now " + threads.size());
+ }
+ logger.debug("creating pool for priority " + this.priorityLevel);
+ this.pool = new ThreadPool<ExportRunnerImpl>(threads);
+ logger.debug("starting pool for priority " + this.priorityLevel + " with " + this.maxConcurrentThreads + " max concurrent threads");
+ this.pool.start(this.maxConcurrentThreads);
+ } catch (final SQLException sqlEx) {
+ logger.error("doProcess()", sqlEx);
+ } catch (final Throwable t) {
+ t.printStackTrace();
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ }
+ }
+
+ @Override
+ public String getResumeTraitement() {
+ return "export.runner.resume";
+ }
+
+ @Override
+ public String typeTraitementRefCode() {
+ return "export.runner<" + this.priorityLevel + ">";
+ }
+
+ @Override
+ public String getInformations() {
+ final StringBuilder sb = new StringBuilder();
+ if (this.isStarted()) {
+ sb.append(this.typeTraitementRefCode()).append(" - ").append(this.pool.getRunningThreadCount()).append(" running thread(s)\n");
+ for (final RunnableBatch rb : this.pool.getThreads()) {
+ sb.append("\t").append(rb.getName()).append(": ").append(rb.isAlive() ? "running" : "idle").append("\n");
+ if (rb.isAlive()) {
+ sb.append(rb.getInformations());
+ }
+ }
+ } else {
+ sb.append(this.typeTraitementRefCode()).append(" - not started\n");
+ }
+ return sb.toString();
+ }
+
+ @Override
+ protected String getBatchVersion() {
+ return "1.0";
+ }
+
+ protected class ExportRunnerImpl extends RunnableBatch {
+ private final Long exportId;
+ private final String user;
+ private final String exportDirectory;
+ private final DocumentsModel docs;
+ private final String undertakerClassName;
+ private final List<String> postProcessors;
+
+ private ExportJob job = null;
+
+ private File generatedFile;
+
+ private PostProcessor pp;
+
+ private boolean exportTerminated = false;
+
+ public ExportRunnerImpl(final Long exportId, final String user, final int priority, final String exportDirectory, final DocumentsModel docs, final String undertakerClassName, final List<String> postProcessors) {
+ super();
+ logger.debug("ExportRunnerImpl<init>(" + exportId + "," + user + ")");
+ this.exportId = exportId;
+ this.user = user;
+ this.exportDirectory = exportDirectory;
+ this.docs = docs;
+ this.undertakerClassName = undertakerClassName;
+ this.postProcessors = postProcessors;
+ this.setName("ExportRunnerImpl<" + exportId + "," + user + ">");
+
+ switch (priority) {
+ case 2: {
+ this.setPriority(Thread.MIN_PRIORITY);
+ break;
+ }
+ default: {
+ this.setPriority(Thread.NORM_PRIORITY);
+ break;
+ }
+ }
+ }
+
+ @Override
+ public void run() {
+ logger.debug("running ExportRunnerImpl for " + this.user + "/" + this.exportId);
+ Connection con = null;
+ File temp = null;
+ String statusColName = null;
+ String currentDate = null;
+ try {
+ if ("mysql".equals(DataLayerManager.getImplementation().getLayerName())) {
+ statusColName = "STATUS";
+ currentDate = "NOW()";
+ } else if ("oracle".equals(DataLayerManager.getImplementation().getLayerName())) {
+ statusColName = "EXPORT_STATUS";
+ currentDate = "SYSDATE";
+ }
+ this.job = DataLayerManager.getImplementation().getExportJob(this.exportId, this.user);
+ con = PoolManager.getInstance().getConnection();
+ final String sql = "UPDATE EXPORT_JOBS SET " + statusColName + "=" + ExportJob.STATUS_RUNNING + ", RUN_DATE=" + currentDate + " WHERE EXPORT_ID=? AND UTI_LOGIN=?";
+ final PreparedStatement ps = con.prepareStatement(sql);
+ ps.setLong(1, this.exportId);
+ ps.setString(2, this.user);
+ ps.executeUpdate();
+ // on est obligé de la fermer ici, car l'export peut être très long
+ PoolManager.getInstance().releaseConnection(con);
+ con = null;
+ final DataResultSet drs = this.job.getDataResultSet();
+ final File dir = new File(this.exportDirectory);
+ temp = File.createTempFile("exp-", null, dir);
+ temp.delete();
+ temp.mkdir();
+ String fileName = null;
+ if (this.job.getFileName().toLowerCase().endsWith(".xls")) {
+ fileName = this.job.getFileName();
+ } else {
+ fileName = this.job.getFileName().concat(".xls");
+ }
+ this.generatedFile = new File(temp, fileName);
+ final DocumentModel dm = this.docs.getDocumentById(this.job.getDocumentId());
+ final User user2 = new User(this.user); // job.getOwner()
+ logger.debug("getting ConfigModel");
+ final ConfigModel cm = DataLayerManager.getImplementation().getConfigExport(this.job.getDocumentId(), this.job.getEtatId(), this.job.getExportConfigId().toString(), user2);
+ logger.debug("creating MyExporter");
+ final MyExporter exporter = new MyExporter(this.generatedFile, drs, dm, this.job.getEtatId(), dm.getEtatById(this.job.getEtatId()).getElementById(this.job.getElementId()), cm, this, this.undertakerClassName);
+ logger.debug("running myExporter");
+ exporter.runImmediatly();
+ } catch (final Exception ex) {
+ logger.error("run()", ex);
+ try {
+ if (con == null) {
+ con = PoolManager.getInstance().getConnection();
+ }
+ final String sql = "UPDATE EXPORT_JOBS SET " + statusColName + "=" + ExportJob.STATUS_ERROR + ", ERROR_MESSAGE=? WHERE EXPORT_ID=? AND UTI_LOGIN=?";
+ final PreparedStatement ps = con.prepareStatement(sql);
+ ps.setString(1, ex.getMessage());
+ ps.setLong(2, this.exportId);
+ ps.setString(3, this.user);
+ ps.executeUpdate();
+ } catch (final SQLException sqlEx) {
+ }
+ if (temp != null) {
+ if (temp.exists()) {
+ if (temp.isDirectory()) {
+ try {
+ FileUtils.dropRecursiveDirectory(temp);
+ } catch (final Exception ex2) {
+ }
+ } else {
+ temp.delete();
+ }
+ }
+ }
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ // c'est fait par le MyExporter
+ // getNotifier().notifyThreadStop(this);
+ }
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ public void notifyTerminated() {
+ this.exportTerminated = true;
+ Connection con = null;
+ String statusColName = null;
+ try {
+ if ("mysql".equals(DataLayerManager.getImplementation().getLayerName())) {
+ statusColName = "STATUS";
+ } else if ("oracle".equals(DataLayerManager.getImplementation().getLayerName())) {
+ statusColName = "EXPORT_STATUS";
+ }
+ con = PoolManager.getInstance().getConnection();
+ PreparedStatement ps = con.prepareStatement("UPDATE EXPORT_JOBS SET GENERATED_FILE_NAME=? WHERE EXPORT_ID=? AND UTI_LOGIN=?");
+ ps.setString(1, this.generatedFile.getAbsolutePath());
+ ps.setLong(2, this.exportId);
+ ps.setString(3, this.user);
+ ps.executeUpdate();
+ PoolManager.getInstance().releaseConnection(con);
+ con = null;
+ this.job.setGeneratedFileName(this.generatedFile.getAbsolutePath());
+
+ // post processors
+ for (final String postProcessor : this.postProcessors) {
+ this.pp = null;
+ try {
+ final Class clazz = Class.forName(postProcessor);
+ final Object o = clazz.newInstance();
+ this.pp = (PostProcessor) o;
+ } catch (final Exception ex) {
+ logger.error("instanciating post processor", ex);
+ }
+ if (this.pp != null) {
+ this.pp.setConfigData(this.job);
+ this.pp.postProcess();
+ }
+ }
+
+ con = PoolManager.getInstance().getConnection();
+ ps = con.prepareStatement("UPDATE EXPORT_JOBS SET " + statusColName + "=" + ExportJob.STATUS_TERMINATED + " WHERE EXPORT_ID=? AND UTI_LOGIN=?");
+ ps.setLong(1, this.exportId);
+ ps.setString(2, this.user);
+ ps.executeUpdate();
+
+ } catch (final Exception ex) {
+ logger.error("notifyTerminated()", ex);
+ try {
+ final String sql = "UPDATE EXPORT_JOBS SET " + statusColName + "=" + ExportJob.STATUS_ERROR + ", ERROR_MESSAGE=? WHERE EXPORT_ID=? AND UTI_LOGIN=?";
+ if (con == null) {
+ con = PoolManager.getInstance().getConnection();
+ }
+ final PreparedStatement ps = con.prepareStatement(sql);
+ ps.setString(1, ex.getMessage());
+ ps.setLong(2, this.exportId);
+ ps.setString(3, this.user);
+ ps.executeUpdate();
+ } catch (final SQLException sqlEx) {
+ }
+ // cleanning
+ final File generated = new File(this.job.getGeneratedFileName());
+ if (generated.exists()) {
+ try {
+ FileUtils.dropRecursiveDirectory(generated.getParentFile());
+ } catch (final Throwable t) {
+ }
+ }
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ con = null;
+ }
+ }
+ this.getNotifier().notifyThreadStop(this);
+ }
+
+ @Override
+ public String getInformations() {
+ final StringBuilder sb = new StringBuilder();
+ sb.append("\t").append(this.getName()).append(":");
+ if (this.isAlive()) {
+ sb.append("export running.");
+ if (this.exportTerminated) {
+ sb.append(" Main export terminated.");
+ if (this.pp != null) {
+ sb.append(" Running post-processor ").append(this.pp.getClass().getName()).append(".");
+ }
+ } else {
+ sb.append(" Main export running.");
+ }
+ } else {
+ if (ExportRunner.this.isStarted()) {
+ sb.append("export fully terminated.");
+ } else {
+ sb.append("idle");
+ }
+ }
+ sb.append("\n");
+ return sb.toString();
+ }
+ }
+
+ private class User implements XemeliosUser {
+ private final String userId;
+
+ public User(final String userId) {
+ super();
+ this.userId = userId;
+ }
+
+ @Override
+ public String getId() {
+ return this.userId;
+ }
+
+ @Override
+ public String getDisplayName() {
+ return this.userId;
+ }
+
+ @Override
+ public boolean hasRole(final String role) {
+ return true;
+ }
+
+ @Override
+ public boolean hasDocument(final String document) {
+ return true;
+ }
+
+ @Override
+ public boolean hasCollectivite(final String collectivite, final DocumentModel dm) {
+ return true;
+ }
+ }
+
+ private class MyExporter extends Exporter {
+ private final ExportRunnerImpl impl;
+ private String undertakerClassName;
+
+ public MyExporter(final File f, final DataResultSet drs, final DocumentModel dm, final String etatId, final ElementModel em, final ConfigModel cf, final ExportRunnerImpl notifier, final String undertakerClassName) {
+ super(f, drs, dm, etatId, em, cf, ExportRunner.this.getApplicationConfiguration());
+ this.undertakerClassName = undertakerClassName;
+ if (this.undertakerClassName == null || this.undertakerClassName.length() == 0) {
+ this.undertakerClassName = "fr.gouv.finances.cp.xemelios.export.JxlExporter";
+ }
+ this.impl = notifier;
+ }
+
+ public void runImmediatly() throws Exception {
+ this.doExport();
+ this.cleanAfterRun();
+ this.impl.notifyTerminated();
+ }
+
+ @Override
+ protected void done() {
+ logger.debug("MyExporter.done()");
+ super.done();
+ this.impl.notifyTerminated();
+ }
+
+ @Override
+ protected ExportUndertaker getUndertaker() {
+ try {
+ final Class undertakerClass = Class.forName(this.undertakerClassName);
+ final Class[] exporterArgsClass = new Class[] { Exporter.class };
+ final Object[] exporterArgs = new Object[] { this };
+ final Constructor constructor = undertakerClass.getConstructor(exporterArgsClass);
+ final ExportUndertaker undertaker = (ExportUndertaker) constructor.newInstance(exporterArgs);
+
+ return undertaker;
+ } catch (final ClassNotFoundException e) {
+ logger.error("", e);
+ return null;
+ } catch (final NoSuchMethodException e) {
+ logger.error("", e);
+ return null;
+ } catch (final InstantiationException e) {
+ logger.error("", e);
+ return null;
+ } catch (final IllegalAccessException e) {
+ logger.error("", e);
+ return null;
+ } catch (final IllegalArgumentException e) {
+ logger.error("", e);
+ return null;
+ } catch (final InvocationTargetException e) {
+ logger.error("", e);
+ return null;
+ }
+ }
+
+ }
}