package fr.ccomptes.xemelios.credentials;
-import fr.gouv.finances.dgfip.xemelios.batch.Batch;
-import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Hashtable;
-import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.TreeSet;
+
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
+
import org.apache.log4j.Logger;
+import fr.gouv.finances.dgfip.xemelios.batch.Batch;
+import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
+
/**
* This class defines credentials
+ *
* @author cmarchand
*/
public class CredentialSetter extends Batch {
- public static final String INPUT = "input";
- public static final String OUTPUT = "output";
- public static final String INPUT_ENCODING = "input.encoding";
- public static final String INPUT_SEPARATOR = "input.separator";
- public static final String DATE_FORMAT = "dd/MM/yyyy";
- public static final String TARGET = "target";
- public static final String LDAP_CONFIG_FILE = "ldap.config.file";
- public static final String TOUS = "Tous";
- private File inputDir, outputDir;
- private String encoding;
- private String separator;
- private TARGETS target;
- private Properties ldapProperties;
- private static enum TARGETS { SPL, ETAT };
-
- @Override
- protected void doProcess() throws Exception {
- run();
- }
-
- @Override
- public String getResumeTraitement() {
- return null;
- }
-
- @Override
- public String typeTraitementRefCode() {
- return "Gestion Droits";
- }
-
- @Override
- public String getInformations() {
- return null;
- }
-
- @Override
- protected String getBatchVersion() {
- return "1.0";
- }
- /**
- * Acknowledgement status
- */
- public enum ACK_STATUS { OK, KO };
- private static final Logger logger = Logger.getLogger(CredentialSetter.class);
-// private Initializer initer = null;
- private SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
-
- /**
- * Creates a new process
- * @param initializer
- */
- public CredentialSetter(String[] args) {
- super(args);
-// this.initer = initializer;
-// initialize();
- }
-
- @Override
- protected void initialize() {
- inputDir = new File(getProps().getProperty(INPUT));
- outputDir = new File(getProps().getProperty(OUTPUT));
- outputDir.mkdirs();
- separator = getProps().getProperty(INPUT_SEPARATOR);
- encoding = getProps().getProperty(INPUT_ENCODING);
- String t = getProps().getProperty(TARGET);
- this.target = t.equals("SPL") ? TARGETS.SPL : TARGETS.ETAT;
- File fLdapConfig = new File(getProps().getProperty(LDAP_CONFIG_FILE));
- if(fLdapConfig.exists()) {
- try {
- ldapProperties = new Properties();
- FileInputStream fis = new FileInputStream(fLdapConfig);
- ldapProperties.load(fis);
- fis.close();
- } catch(Exception ex) {
- logger.error("while parsing "+fLdapConfig.getAbsolutePath());
- }
- }
- if(ldapProperties==null) throw new RuntimeException("Property "+LDAP_CONFIG_FILE+" not set !");
- }
-
- /**
- * Creates or removes credentials
- * @throws RuntimeException
- */
- public void run() throws RuntimeException {
- FileFilter ff = new FileFilter() {
- @Override
- public boolean accept(File pathname) {
- return !pathname.isDirectory();
- }
- };
- File[] inputFiles = inputDir.listFiles(ff);
- while(inputFiles.length>0) {
- try {
- String fileName = inputFiles[0].getName();
- BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(inputFiles[0]),encoding));
- String line = br.readLine();
- int numLine = 0;
- while(line!=null) {
- parseLine(line, numLine, fileName);
- line = br.readLine();
- numLine++;
- }
-// inputFiles[0].renameTo(new File(new File(inputFiles[0].getParentFile(),"done"),inputFiles[0].getName()));
- if(!inputFiles[0].delete()) {
- logger.error("impossible de supprimer "+inputFiles[0].getAbsolutePath());
- }
- } catch(Exception ex) {
- ex.printStackTrace();
- }
- inputFiles = inputDir.listFiles(ff);
- }
- }
- protected void parseLine(String line, int numLigne, String fileName) {
- String[] rec = line.split(separator, -1);
- if(rec.length!=6) {
- writeAck(fileName, numLigne, ACK_STATUS.KO, rec.length+" fields found, 6 expected", line);
- logger.error("parseLine:", new ParsingException(fileName+" - row "+numLigne+" : incorrect number of fields :"+rec.length+" founds, 6 were expected"));
- return;
- }
- String checkMsg = checkLineFormat(rec);
- if(checkMsg!=null) {
- writeAck(fileName, numLigne, ACK_STATUS.KO, checkMsg, line);
- logger.error("parseLine:",new ParsingException(fileName+" - row "+numLigne+" "+checkMsg));
- return;
- }
- UsersToSet users = rec[2]!=null ? new UsersToSet(rec[2]) : null;
- CollectivitesToSet collectivites = rec[3]!=null ? new CollectivitesToSet(rec[3]) : null;
- DocumentsToSet documents = rec[4]!=null ? new DocumentsToSet(rec[4]) : null;
- doGrants(users, collectivites, documents, rec[5], rec[1]);
- writeAck(fileName, numLigne, ACK_STATUS.OK, null, line);
- }
- /**
- * Checks {@literal line} format.
- * @param the splitted line
- * @return {@literal null} if format is correct and all fields values are acceptable, or error message
- */
- public String checkLineFormat(String[] rec) {
- // is this a date ?
- try {
- sdf.parse(rec[0]);
- } catch(ParseException pEx) {
- return rec[0] +" n'est pas une date valide au format "+DATE_FORMAT;
- }
- if(!"O".equals(rec[1]) && !"F".equals(rec[1]))
- return rec[1]+" n'est pas pas une action valide : attendu [O|F]";
- // no control on users
- // no control on collectivite
- // no control on document-type (we do not have the information here)
- // we check if profil exists
- Connection con = null;
- PreparedStatement ps = null;
- if(rec[5]!=null && rec[5].length()>0) {
- try {
- con = PoolManager.getInstance().getConnection();
- ps = con.prepareStatement("SELECT PRF_CODE FROM AUTH_PROFIL WHERE PRF_CODE=?");
- ps.setString(1, rec[5]);
- ResultSet rs = ps.executeQuery();
- boolean found = rs.next();
- rs.close();
- if(!found) {
- return rec[5]+" is not a known profil";
- }
- } catch(SQLException sqlEx) {
- logger.error("checking if PROFIL exsists:",sqlEx);
- } finally {
- if(ps!=null) {
- try {
- ps.close();
- } catch(SQLException ignoreIt) {}
- }
- if(con!=null) {
- PoolManager.getInstance().releaseConnection(con);
- con = null;
- }
- }
- }
- return null;
- }
- /**
- * Writed acknowledgment file
- * @param originalFileName The input file name
- * @param numLigne The input file line number
- * @param status {@link #ACK_STATUS}
- * @param message
- * @param originalLine
- * @throws RuntimeException if message contains a separator char
- */
- protected void writeAck(String originalFileName, int numLigne, ACK_STATUS status, String message, String originalLine) throws RuntimeException {
- int lastDot = originalFileName.lastIndexOf(".");
- String baseFileName = null;
- String extension = null;
- if(lastDot>=0) {
- baseFileName = originalFileName.substring(0, lastDot);
- extension = originalFileName.substring(lastDot);
- } else {
- baseFileName = originalFileName;
- extension = "";
- }
- String newName = baseFileName+"-"+numLigne+extension;
- File targetFile = new File(outputDir,newName);
- try {
- FileOutputStream fos = new FileOutputStream(targetFile);
- OutputStreamWriter fw = new OutputStreamWriter(fos, encoding);
- fw.write(status==ACK_STATUS.KO?"KO":"OK");
- fw.write(";");
- if(message!=null) fw.write(message);
- fw.write(";");
- fw.write(originalLine);
- fw.flush();
- fw.close();
- } catch(IOException ioEx) {
- logger.error("writeAck:",ioEx);
- }
- }
-
- /**
- * Query the LDAP directory to retrieve users that belongs to this CRC
- * @param crcCode
- * @return
- */
- protected List<Long> getUsersFromCRC(String crcCode) {
- TreeSet<String> ret = new TreeSet<String>();
- Hashtable<String,String> ldapEnv = new Hashtable<String, String>();
- ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
- ldapEnv.put(Context.PROVIDER_URL, "ldap://" + ldapProperties.getProperty("server") + ":" + ldapProperties.getProperty("port"));
- ldapEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
- ldapEnv.put(Context.SECURITY_PRINCIPAL, ldapProperties.getProperty("principal"));
- ldapEnv.put(Context.SECURITY_CREDENTIALS, ldapProperties.getProperty("credentials"));
-
- try {
- String searchFilter = ldapProperties.getProperty("filter");
- // cela peut aussi être Assistants ou Conseillers
- String searchBase = ldapProperties.getProperty("search.base").replaceAll("<GROUP>", crcCode);
- String[] roles = ldapProperties.getProperty("roles").split(",",0);
- for(String role:roles) {
- LdapContext ctx = new InitialLdapContext(ldapEnv,null);
- SearchControls searchCtls = new SearchControls();
- searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
- String localBase = searchBase.replaceAll("<ROLE>", role);
-//logger.debug("searching localBase="+localBase+", searchFilter="+searchFilter);
- NamingEnumeration<SearchResult> answer = ctx.search(localBase, searchFilter, searchCtls);
- String returnedAtts[] = {"members"};
- searchCtls.setReturningAttributes(returnedAtts);
- while(answer.hasMore()) {
-// found++;
- SearchResult sr = (SearchResult) answer.next();
- Attributes attrs = sr.getAttributes();
- if (attrs != null) {
- Attribute at = attrs.get(ldapProperties.getProperty("attribute.for.login"));
- ret.add(at.getAll().next().toString());
- }
- }
- ctx.close();
- }
- } catch(Exception ex) {
- ex.printStackTrace();
- }
- ArrayList<Long> lst = new ArrayList<Long>();
- for(Iterator<String> it=ret.iterator();it.hasNext();) {
- lst.add(getUserIdFromLogin(it.next()));
- }
- return lst;
-
- }
- /**
- * Query various dictionnaries to get the collectivites attached to the specified CRC
- * @param crcCode
- * @return
- */
- protected List<String> getCollectivitesFromCRC(String crcCode) {
- ArrayList<String> ret = new ArrayList<String>();
- switch(target) {
- case ETAT: {
- // in ETAT, we always get the codic, and never the CRC or room num...
- ret.add(crcCode);
- }
- default: {
- Connection con = null;
- PreparedStatement ps = null;
- ResultSet rs = null;
- try {
- con = PoolManager.getInstance().getConnection();
- ps = con.prepareStatement("SELECT DISTINCT DEPT_CODE FROM CRC_REFERENTIEL WHERE CRC_MAIL=?");
- ps.setString(1,crcCode);
- rs = ps.executeQuery();
- ArrayList<String> depts = new ArrayList<String>();
- while(rs.next()) {
- depts.add(rs.getString(1));
- }
- rs.close(); rs = null;
- ps.close(); ps = null;
- StringBuilder sb = new StringBuilder();
- for(String dept:depts) sb.append("CODIC LIKE '").append(dept).append("%' OR ");
- for(int i=0;i<4;i++) sb.deleteCharAt(sb.length()-1);
- ps = con.prepareStatement("SELECT DISTINCT IDCOLL FROM REF_COLL_SPL_COLLECTIVITESSPL_COLL_IX WHERE "+sb.toString());
- rs = ps.executeQuery();
- while(rs.next())
- ret.add(rs.getString(1));
- } catch(SQLException sqlEx) {
- logger.error("getCollectivitesFromCRC:",sqlEx);
- } finally {
- if(rs!=null) { try { rs.close(); } catch(SQLException sqlEx) {} }
- if(ps!=null) { try { ps.close(); } catch(SQLException sqlEx) {} }
- if(con!=null) { PoolManager.getInstance().releaseConnection(con); }
- }
- }
- }
- return ret;
- }
- private void doGrants(UsersToSet users, CollectivitesToSet collectivites, DocumentsToSet documents, String profil, String action) {
- if(users.all) {
- Connection con = null;
- PreparedStatement ps = null;
- ResultSet rs = null;
- try {
- con = PoolManager.getInstance().getConnection();
- ps = con.prepareStatement("SELECT UTI_ID FROM AUTH_UTILISATEUR");
- rs = ps.executeQuery();
- while(rs.next()) {
- grantsUser(rs.getLong(1), collectivites, documents, profil, action);
- }
- } catch(SQLException sqlEx) {
- logger.error("doGrants:",sqlEx);
- } finally {
- if(rs!=null) try { rs.close(); rs = null; } catch(SQLException sqlEx) {}
- if(ps!=null) try { ps.close(); ps = null; } catch(SQLException sqlEx) {}
- if(con!=null) {
- PoolManager.getInstance().releaseConnection(con);
- con = null;
- }
- }
- } else {
- for(Long user: users.users) {
- grantsUser(user, collectivites, documents, profil, action);
- }
- }
- }
- private void grantsUser(Long user, CollectivitesToSet collectivites, DocumentsToSet documents, String profil, String action) {
- Connection con = null;
- PreparedStatement ps = null;
- PreparedStatement ps2 = null;
- ResultSet rs = null;
- try {
- con = PoolManager.getInstance().getConnection();
- if("F".equals(action)) {
- if(collectivites!=null) {
- if(collectivites.all) {
- ps = con.prepareStatement("DELETE FROM AUTH_HAB_COL WHERE UTI_ID=?");
- ps.setLong(1, user);
- ps.executeUpdate();
- ps.close();
- ps = con.prepareStatement("UPDATE AUTH_UTILISATEUR SET UTI_ALL_COL=0 WHERE UTI_ID=?");
- ps.setLong(1,user);
- ps.executeUpdate();
- ps.close();
- ps = null;
-// logger.debug("removing all collectivites from "+user);
- } else {
- ps = con.prepareStatement("DELETE FROM AUTH_HAB_COL WHERE UTI_ID=? AND COL_CLEF=?");
- ps.setLong(1, user);
- for(String coll:collectivites.collectivites) {
- ps.setString(2,coll);
- ps.executeUpdate();
-// logger.debug("removing coll="+coll+" from "+user);
- }
- ps.close();
- ps = null;
- }
- }
- if(profil!=null) {
- ps = con.prepareStatement("DELETE FROM AUTH_UTI_PRF WHERE UTI_ID=? AND PRF_CODE=?");
- ps.setLong(1, user);
- ps.setString(2, profil);
- ps.executeUpdate();
- ps.close(); ps = null;
-// logger.debug("removing prf="+profil+" from "+user);
- }
- if(documents!=null) {
- if(documents.all) {
- ps = con.prepareStatement("DELETE FROM AUTH_HAB_DOC WHERE UTI_ID=?");
- ps.setLong(1, user);
- ps.executeUpdate();
- ps.close();
- ps = con.prepareStatement("UPDATE AUTH_UTILISATEUR SET UTI_ALL_DOC=0 WHERE UTI_ID=?");
- ps.setLong(1, user);
- ps.executeUpdate();
- ps.close(); ps = null;
-// logger.debug("removing all docs from "+user);
- } else {
- ps = con.prepareStatement("DELETE FROM AUTH_HAB_DOC WHERE UTI_ID=? AND DOC_CODE=?");
- ps.setLong(1, user);
- for(String docId: documents.documents) {
- ps.setString(2,docId);
- ps.executeUpdate();
-// logger.debug("removing doc="+docId+" from "+user);
- }
- ps.close();
- ps = null;
- }
- }
- } else {
- // open rights
- if(collectivites.all) {
- ps = con.prepareStatement("UPDATE AUTH_UTILISATEUR SET UTI_ALL_COL=1 WHERE UTI_ID=?");
- ps.setLong(1,user);
- ps.executeUpdate();
- ps.close();
- ps = null;
-// logger.debug("granting all coll to "+user);
- } else {
- ps2 = con.prepareStatement("SELECT 1 FROM AUTH_HAB_COL WHERE UTI_ID=? AND COL_CLEF=?");
- ps = con.prepareStatement("INSERT INTO AUTH_HAB_COL (UTI_ID, TID_CODE, COL_CLEF) VALUES (?,?,?)");
- ps2.setLong(1, user);
- ps.setLong(1, user);
- ps.setString(2, target==TARGETS.ETAT?"CODIC":"SIRET");
- for(String coll:collectivites.collectivites) {
- ps2.setString(2,coll);
- rs = ps2.executeQuery();
- if(rs.next()) {
- // nothing to do
-// logger.debug("user "+user+" is already granted on coll="+coll);
- } else {
- ps.setString(3,coll);
- ps.executeUpdate();
-// logger.debug("granting "+user+" on coll="+coll);
- }
- rs.close();
- rs = null;
- }
- ps2.close(); ps2 = null;
- ps.close(); ps = null;
- }
- // profils
- ps = con.prepareStatement("SELECT UTI_ALL_FONC FROM AUTH_UTILISATEUR WHERE UTI_ID=?");
- ps.setLong(1,user);
- rs = ps.executeQuery();
- if(rs.next()) {
- if(rs.getInt(1)==0) {
- rs.close(); rs = null;
- ps.close(); ps = null;
- ps = con.prepareStatement("SELECT 1 FROM AUTH_UTI_PRF WHERE UTI_ID=? AND PRF_CODE=?");
- ps.setLong(1,user);
- ps.setString(2,profil);
- rs = ps.executeQuery();
- if(!rs.next()) {
- rs.close(); rs = null;
- ps.close(); ps = null;
- ps = con.prepareStatement("INSERT INTO AUTH_UTI_PRF (UTI_ID, PRF_CODE) VALUES (?,?)");
- ps.setLong(1,user);
- ps.setString(2,profil);
- ps.executeUpdate();
-// logger.debug("granting prf="+profil+" to "+user);
-// ps.close(); ps =null;
- } else {
- rs.close(); rs = null;
- }
- }
- } else {
- // it should never happen, we have ensured that user exists
- rs.close(); rs = null;
- }
- ps.close(); ps = null;
- // documents
- if(documents.all) {
- ps = con.prepareStatement("UPDATE AUTH_UTILISATEUR SET UTI_ALL_DOC=1 WHERE UTI_ID=?");
- ps.setLong(1,user);
- ps.executeUpdate();
- ps.close();
- ps = null;
-// logger.debug("granting all docs to "+user);
- } else {
- ps2 = con.prepareStatement("SELECT 1 FROM AUTH_HAB_DOC WHERE UTI_ID=? AND DOC_CODE=?");
- ps = con.prepareStatement("INSERT INTO AUTH_HAB_DOC (UTI_ID, DOC_CODE) VALUES (?,?)");
- ps2.setLong(1, user);
- ps.setLong(1, user);
- for(String docId:documents.documents) {
- ps2.setString(2,docId);
- rs = ps2.executeQuery();
- if(rs.next()) {
- // nothing to do
-// logger.debug(user+" is already granted to doc="+docId);
- } else {
- ps.setString(2,docId);
- ps.executeUpdate();
-// logger.debug("granting doc="+docId+" to "+user);
- }
- rs.close();
- rs = null;
- }
- ps2.close(); ps2 = null;
- ps.close(); ps = null;
- }
- }
- } catch(SQLException sqlEx) {
- logger.error("grantsUser:",sqlEx);
- } finally {
- if(rs!=null) try { rs.close(); rs = null; } catch(SQLException sqlEx) {}
- if(ps2!=null) try { ps2.close(); ps2 = null; } catch(SQLException sqlEx) {}
- if(ps!=null) try { ps.close(); ps = null; } catch(SQLException sqlEx) {}
- if(con!=null) { PoolManager.getInstance().releaseConnection(con); con = null; }
- }
- }
- private Long getUserIdFromLogin(String login) {
- Connection con = null;
- PreparedStatement ps = null;
- ResultSet rs = null;
- try {
- con = PoolManager.getInstance().getConnection();
- ps = con.prepareStatement("SELECT UTI_ID FROM AUTH_UTILISATEUR WHERE UTI_LOGIN=?");
- ps.setString(1,login);
- rs = ps.executeQuery();
- if(rs.next()) {
- return rs.getLong(1);
- } else {
- // we must create it
- rs.close();
- ps.close();
- ps = con.prepareStatement("INSERT INTO AUTH_UTILISATEUR (UTI_LOGIN, UTI_ACTIF, UTI_ALL_FONC, UTI_ALL_DOC, UTI_ALL_COL) VALUES (?,1,0,0,0)");
- ps.setString(1,login);
- ps.executeUpdate();
- ps.close();
- ps = con.prepareStatement("SELECT MAX(UTI_ID) FROM AUTH_UTILISATEUR WHERE UTI_LOGIN=?");
- ps.setString(1,login);
- rs = ps.executeQuery();
- long userId = 0L;
- if(rs.next())
- userId = rs.getLong(1);
- rs.close();
- ps = con.prepareStatement("INSERT INTO AUTH_UTI_PRF (UTI_ID,PRF_CODE) VALUES (?,'CONNECT')");
- ps.setLong(1,userId);
- ps.executeUpdate();
- ps.close();
- return userId;
- }
- } catch(SQLException sqlEx) {
- logger.error("grantsUser:",sqlEx);
- } finally {
- if(rs!=null) try { rs.close(); rs = null; } catch(SQLException sqlEx) {}
- if(ps!=null) try { ps.close(); ps = null; } catch(SQLException sqlEx) {}
- if(con!=null) { PoolManager.getInstance().releaseConnection(con); con = null; }
- }
- return 0L;
- }
- private class UsersToSet {
- boolean all = false;
- ArrayList<Long> users = null;
- public UsersToSet(String spec) {
- super();
- users = new ArrayList<Long>();
- String[] items = spec.split(",");
- for(String item:items) {
- if(TOUS.equals(item)) {
- all = true;
- users.clear();
- return;
- } else if(item.startsWith("@")) {
- // on supprime le '@'
- users.addAll(getUsersFromCRC(item.substring(1)));
- } else {
- // we need to query LDAP for users in that CRC
- users.add(getUserIdFromLogin(item));
- }
- }
- }
- }
- private class CollectivitesToSet {
- private boolean all;
- private ArrayList<String> collectivites;
- public CollectivitesToSet(String spec) {
- super();
- collectivites = new ArrayList<String>();
- String[] items = spec.split(",");
- for(String item: items) {
- if(TOUS.equals(item)) {
- all = true;
- collectivites.clear();
- return;
- } else if(item.indexOf('@')==0) {
- // it's a CRC
- collectivites.addAll(getCollectivitesFromCRC(item.substring(1)));
- } else {
- collectivites.add(item);
- }
- }
- }
- }
- private class DocumentsToSet {
- private boolean all = false;
- private TreeSet<String> documents = null;
- public DocumentsToSet(String spec) {
- super();
- documents = new TreeSet<String>();
- String[] items = spec.split(",");
- for(String item:items) {
- if(TOUS.equals(item)) {
- all = true;
- documents.clear();
- return;
- } else {
- if("compteGestion".equals(item)) {
- documents.add("compteGestion");
- documents.add("cg-colloc");
- } else if("documentPaye".equals(item)) {
- documents.add("documentPaye");
- documents.add("cfg-paye");
- } else if("compteGestionEtat".equals(item)) {
- documents.add("compteGestionEtat");
- documents.add("cg-etat");
- } else if("DocumentRapport".equals(item)) {
- documents.add("DocumentRapport");
- documents.add("rapport");
- } else if("PES_Aller".equals(item)) {
- documents.add("PES_Aller");
- documents.add("pes-aller");
- } else {
- documents.add(item);
- }
- }
- }
- }
- }
+ public static final String INPUT = "input";
+ public static final String OUTPUT = "output";
+ public static final String INPUT_ENCODING = "input.encoding";
+ public static final String INPUT_SEPARATOR = "input.separator";
+ public static final String DATE_FORMAT = "dd/MM/yyyy";
+ public static final String TARGET = "target";
+ public static final String LDAP_CONFIG_FILE = "ldap.config.file";
+ public static final String TOUS = "Tous";
+ private File inputDir, outputDir;
+ private String encoding;
+ private String separator;
+ private TARGETS target;
+ private Properties ldapProperties;
+
+ private static enum TARGETS {
+ SPL, ETAT
+ };
+
+ @Override
+ protected void doProcess() throws Exception {
+ this.run();
+ }
+
+ @Override
+ public String getResumeTraitement() {
+ return null;
+ }
+
+ @Override
+ public String typeTraitementRefCode() {
+ return "Gestion Droits";
+ }
+
+ @Override
+ public String getInformations() {
+ return null;
+ }
+
+ @Override
+ protected String getBatchVersion() {
+ return "1.0";
+ }
+
+ /**
+ * Acknowledgement status
+ */
+ public enum ACK_STATUS {
+ OK, KO
+ };
+
+ private static final Logger logger = Logger.getLogger(CredentialSetter.class);
+ // private Initializer initer = null;
+ private final SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
+
+ /**
+ * Creates a new process
+ *
+ * @param initializer
+ */
+ public CredentialSetter(final String[] args) {
+ super(args);
+ // this.initer = initializer;
+ // initialize();
+ }
+
+ @Override
+ protected void initialize() {
+ this.inputDir = new File(this.getProps().getProperty(INPUT));
+ this.outputDir = new File(this.getProps().getProperty(OUTPUT));
+ this.outputDir.mkdirs();
+ this.separator = this.getProps().getProperty(INPUT_SEPARATOR);
+ this.encoding = this.getProps().getProperty(INPUT_ENCODING);
+ final String t = this.getProps().getProperty(TARGET);
+ this.target = t.equals("SPL") ? TARGETS.SPL : TARGETS.ETAT;
+ final File fLdapConfig = new File(this.getProps().getProperty(LDAP_CONFIG_FILE));
+ if (fLdapConfig.exists()) {
+ try {
+ this.ldapProperties = new Properties();
+ final FileInputStream fis = new FileInputStream(fLdapConfig);
+ this.ldapProperties.load(fis);
+ fis.close();
+ } catch (final Exception ex) {
+ logger.error("while parsing " + fLdapConfig.getAbsolutePath());
+ }
+ }
+ if (this.ldapProperties == null) {
+ throw new RuntimeException("Property " + LDAP_CONFIG_FILE + " not set !");
+ }
+ }
+
+ /**
+ * Creates or removes credentials
+ *
+ * @throws RuntimeException
+ */
+ public void run() throws RuntimeException {
+ final FileFilter ff = new FileFilter() {
+ @Override
+ public boolean accept(final File pathname) {
+ return !pathname.isDirectory();
+ }
+ };
+ File[] inputFiles = this.inputDir.listFiles(ff);
+ while (inputFiles.length > 0) {
+ try {
+ final String fileName = inputFiles[0].getName();
+ final BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(inputFiles[0]), this.encoding));
+ String line = br.readLine();
+ int numLine = 0;
+ while (line != null) {
+ this.parseLine(line, numLine, fileName);
+ line = br.readLine();
+ numLine++;
+ }
+ // inputFiles[0].renameTo(new File(new File(inputFiles[0].getParentFile(),"done"),inputFiles[0].getName()));
+ if (!inputFiles[0].delete()) {
+ logger.error("impossible de supprimer " + inputFiles[0].getAbsolutePath());
+ }
+ } catch (final Exception ex) {
+ ex.printStackTrace();
+ }
+ inputFiles = this.inputDir.listFiles(ff);
+ }
+ }
+
+ protected void parseLine(final String line, final int numLigne, final String fileName) {
+ final String[] rec = line.split(this.separator, -1);
+ if (rec.length != 6) {
+ this.writeAck(fileName, numLigne, ACK_STATUS.KO, rec.length + " fields found, 6 expected", line);
+ logger.error("parseLine:", new ParsingException(fileName + " - row " + numLigne + " : incorrect number of fields :" + rec.length + " founds, 6 were expected"));
+ return;
+ }
+ final String checkMsg = this.checkLineFormat(rec);
+ if (checkMsg != null) {
+ this.writeAck(fileName, numLigne, ACK_STATUS.KO, checkMsg, line);
+ logger.error("parseLine:", new ParsingException(fileName + " - row " + numLigne + " " + checkMsg));
+ return;
+ }
+ final UsersToSet users = rec[2] != null ? new UsersToSet(rec[2]) : null;
+ final CollectivitesToSet collectivites = rec[3] != null ? new CollectivitesToSet(rec[3]) : null;
+ final DocumentsToSet documents = rec[4] != null ? new DocumentsToSet(rec[4]) : null;
+ this.doGrants(users, collectivites, documents, rec[5], rec[1]);
+ this.writeAck(fileName, numLigne, ACK_STATUS.OK, null, line);
+ }
+
+ /**
+ * Checks {@literal line} format.
+ *
+ * @param the
+ * splitted line
+ * @return {@literal null} if format is correct and all fields values are acceptable, or error message
+ */
+ public String checkLineFormat(final String[] rec) {
+ // is this a date ?
+ try {
+ this.sdf.parse(rec[0]);
+ } catch (final ParseException pEx) {
+ return rec[0] + " n'est pas une date valide au format " + DATE_FORMAT;
+ }
+ if (!"O".equals(rec[1]) && !"F".equals(rec[1])) {
+ return rec[1] + " n'est pas pas une action valide : attendu [O|F]";
+ }
+ // no control on users
+ // no control on collectivite
+ // no control on document-type (we do not have the information here)
+ // we check if profil exists
+ Connection con = null;
+ PreparedStatement ps = null;
+ if (rec[5] != null && rec[5].length() > 0) {
+ try {
+ con = PoolManager.getInstance().getConnection();
+ ps = con.prepareStatement("SELECT PRF_CODE FROM AUTH_PROFIL WHERE PRF_CODE=?");
+ ps.setString(1, rec[5]);
+ final ResultSet rs = ps.executeQuery();
+ final boolean found = rs.next();
+ rs.close();
+ if (!found) {
+ return rec[5] + " is not a known profil";
+ }
+ } catch (final SQLException sqlEx) {
+ logger.error("checking if PROFIL exsists:", sqlEx);
+ } finally {
+ if (ps != null) {
+ try {
+ ps.close();
+ } catch (final SQLException ignoreIt) {
+ }
+ }
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ con = null;
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Writed acknowledgment file
+ *
+ * @param originalFileName
+ * The input file name
+ * @param numLigne
+ * The input file line number
+ * @param status
+ * {@link #ACK_STATUS}
+ * @param message
+ * @param originalLine
+ * @throws RuntimeException
+ * if message contains a separator char
+ */
+ protected void writeAck(final String originalFileName, final int numLigne, final ACK_STATUS status, final String message, final String originalLine) throws RuntimeException {
+ final int lastDot = originalFileName.lastIndexOf(".");
+ String baseFileName = null;
+ String extension = null;
+ if (lastDot >= 0) {
+ baseFileName = originalFileName.substring(0, lastDot);
+ extension = originalFileName.substring(lastDot);
+ } else {
+ baseFileName = originalFileName;
+ extension = "";
+ }
+ final String newName = baseFileName + "-" + numLigne + extension;
+ final File targetFile = new File(this.outputDir, newName);
+ try {
+ final FileOutputStream fos = new FileOutputStream(targetFile);
+ final OutputStreamWriter fw = new OutputStreamWriter(fos, this.encoding);
+ fw.write(status == ACK_STATUS.KO ? "KO" : "OK");
+ fw.write(";");
+ if (message != null) {
+ fw.write(message);
+ }
+ fw.write(";");
+ fw.write(originalLine);
+ fw.flush();
+ fw.close();
+ } catch (final IOException ioEx) {
+ logger.error("writeAck:", ioEx);
+ }
+ }
+
+ /**
+ * Query the LDAP directory to retrieve users that belongs to this CRC
+ *
+ * @param crcCode
+ * @return
+ */
+ protected List<Long> getUsersFromCRC(final String crcCode) {
+ final TreeSet<String> ret = new TreeSet<String>();
+ final Hashtable<String, String> ldapEnv = new Hashtable<String, String>();
+ ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
+ ldapEnv.put(Context.PROVIDER_URL, "ldap://" + this.ldapProperties.getProperty("server") + ":" + this.ldapProperties.getProperty("port"));
+ ldapEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
+ ldapEnv.put(Context.SECURITY_PRINCIPAL, this.ldapProperties.getProperty("principal"));
+ ldapEnv.put(Context.SECURITY_CREDENTIALS, this.ldapProperties.getProperty("credentials"));
+
+ try {
+ final String searchFilter = this.ldapProperties.getProperty("filter");
+ // cela peut aussi être Assistants ou Conseillers
+ final String searchBase = this.ldapProperties.getProperty("search.base").replaceAll("<GROUP>", crcCode);
+ final String[] roles = this.ldapProperties.getProperty("roles").split(",", 0);
+ for (final String role : roles) {
+ final LdapContext ctx = new InitialLdapContext(ldapEnv, null);
+ final SearchControls searchCtls = new SearchControls();
+ searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
+ final String localBase = searchBase.replaceAll("<ROLE>", role);
+ // logger.debug("searching localBase="+localBase+", searchFilter="+searchFilter);
+ final NamingEnumeration<SearchResult> answer = ctx.search(localBase, searchFilter, searchCtls);
+ final String returnedAtts[] = { "members" };
+ searchCtls.setReturningAttributes(returnedAtts);
+ while (answer.hasMore()) {
+ // found++;
+ final SearchResult sr = answer.next();
+ final Attributes attrs = sr.getAttributes();
+ if (attrs != null) {
+ final Attribute at = attrs.get(this.ldapProperties.getProperty("attribute.for.login"));
+ ret.add(at.getAll().next().toString());
+ }
+ }
+ ctx.close();
+ }
+ } catch (final Exception ex) {
+ ex.printStackTrace();
+ }
+ final ArrayList<Long> lst = new ArrayList<Long>();
+ for (final String string : ret) {
+ lst.add(this.getUserIdFromLogin(string));
+ }
+ return lst;
+
+ }
+
+ /**
+ * Query various dictionnaries to get the collectivites attached to the specified CRC
+ *
+ * @param crcCode
+ * @return
+ */
+ protected List<String> getCollectivitesFromCRC(final String crcCode) {
+ final ArrayList<String> ret = new ArrayList<String>();
+ switch (this.target) {
+ case ETAT: {
+ // in ETAT, we always get the codic, and never the CRC or room num...
+ ret.add(crcCode);
+ }
+ default: {
+ Connection con = null;
+ PreparedStatement ps = null;
+ ResultSet rs = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ ps = con.prepareStatement("SELECT DISTINCT DEPT_CODE FROM CRC_REFERENTIEL WHERE CRC_MAIL=?");
+ ps.setString(1, crcCode);
+ rs = ps.executeQuery();
+ final ArrayList<String> depts = new ArrayList<String>();
+ while (rs.next()) {
+ depts.add(rs.getString(1));
+ }
+ rs.close();
+ rs = null;
+ ps.close();
+ ps = null;
+ final StringBuilder sb = new StringBuilder();
+ for (final String dept : depts) {
+ sb.append("CODIC LIKE '").append(dept).append("%' OR ");
+ }
+ for (int i = 0; i < 4; i++) {
+ sb.deleteCharAt(sb.length() - 1);
+ }
+ ps = con.prepareStatement("SELECT DISTINCT IDCOLL FROM REF_COLL_SPL_COLLECTIVITESSPL_COLL_IX WHERE " + sb.toString());
+ rs = ps.executeQuery();
+ while (rs.next()) {
+ ret.add(rs.getString(1));
+ }
+ } catch (final SQLException sqlEx) {
+ logger.error("getCollectivitesFromCRC:", sqlEx);
+ } finally {
+ if (rs != null) {
+ try {
+ rs.close();
+ } catch (final SQLException sqlEx) {
+ }
+ }
+ if (ps != null) {
+ try {
+ ps.close();
+ } catch (final SQLException sqlEx) {
+ }
+ }
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ }
+ }
+ }
+ return ret;
+ }
+
+ private void doGrants(final UsersToSet users, final CollectivitesToSet collectivites, final DocumentsToSet documents, final String profil, final String action) {
+ if (users.all) {
+ Connection con = null;
+ PreparedStatement ps = null;
+ ResultSet rs = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ ps = con.prepareStatement("SELECT UTI_ID FROM AUTH_UTILISATEUR");
+ rs = ps.executeQuery();
+ while (rs.next()) {
+ this.grantsUser(rs.getLong(1), collectivites, documents, profil, action);
+ }
+ } catch (final SQLException sqlEx) {
+ logger.error("doGrants:", sqlEx);
+ } finally {
+ if (rs != null) {
+ try {
+ rs.close();
+ rs = null;
+ } catch (final SQLException sqlEx) {
+ }
+ }
+ if (ps != null) {
+ try {
+ ps.close();
+ ps = null;
+ } catch (final SQLException sqlEx) {
+ }
+ }
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ con = null;
+ }
+ }
+ } else {
+ for (final Long user : users.users) {
+ this.grantsUser(user, collectivites, documents, profil, action);
+ }
+ }
+ }
+
+ private void grantsUser(final Long user, final CollectivitesToSet collectivites, final DocumentsToSet documents, final String profil, final String action) {
+ Connection con = null;
+ PreparedStatement ps = null;
+ PreparedStatement ps2 = null;
+ ResultSet rs = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ if ("F".equals(action)) {
+ if (collectivites != null) {
+ if (collectivites.all) {
+ ps = con.prepareStatement("DELETE FROM AUTH_HAB_COL WHERE UTI_ID=?");
+ ps.setLong(1, user);
+ ps.executeUpdate();
+ ps.close();
+ ps = con.prepareStatement("UPDATE AUTH_UTILISATEUR SET UTI_ALL_COL=0 WHERE UTI_ID=?");
+ ps.setLong(1, user);
+ ps.executeUpdate();
+ ps.close();
+ ps = null;
+ // logger.debug("removing all collectivites from "+user);
+ } else {
+ ps = con.prepareStatement("DELETE FROM AUTH_HAB_COL WHERE UTI_ID=? AND COL_CLEF=?");
+ ps.setLong(1, user);
+ for (final String coll : collectivites.collectivites) {
+ ps.setString(2, coll);
+ ps.executeUpdate();
+ // logger.debug("removing coll="+coll+" from "+user);
+ }
+ ps.close();
+ ps = null;
+ }
+ }
+ if (profil != null) {
+ ps = con.prepareStatement("DELETE FROM AUTH_UTI_PRF WHERE UTI_ID=? AND PRF_CODE=?");
+ ps.setLong(1, user);
+ ps.setString(2, profil);
+ ps.executeUpdate();
+ ps.close();
+ ps = null;
+ // logger.debug("removing prf="+profil+" from "+user);
+ }
+ if (documents != null) {
+ if (documents.all) {
+ ps = con.prepareStatement("DELETE FROM AUTH_HAB_DOC WHERE UTI_ID=?");
+ ps.setLong(1, user);
+ ps.executeUpdate();
+ ps.close();
+ ps = con.prepareStatement("UPDATE AUTH_UTILISATEUR SET UTI_ALL_DOC=0 WHERE UTI_ID=?");
+ ps.setLong(1, user);
+ ps.executeUpdate();
+ ps.close();
+ ps = null;
+ // logger.debug("removing all docs from "+user);
+ } else {
+ ps = con.prepareStatement("DELETE FROM AUTH_HAB_DOC WHERE UTI_ID=? AND DOC_CODE=?");
+ ps.setLong(1, user);
+ for (final String docId : documents.documents) {
+ ps.setString(2, docId);
+ ps.executeUpdate();
+ // logger.debug("removing doc="+docId+" from "+user);
+ }
+ ps.close();
+ ps = null;
+ }
+ }
+ } else {
+ // open rights
+ if (collectivites.all) {
+ ps = con.prepareStatement("UPDATE AUTH_UTILISATEUR SET UTI_ALL_COL=1 WHERE UTI_ID=?");
+ ps.setLong(1, user);
+ ps.executeUpdate();
+ ps.close();
+ ps = null;
+ // logger.debug("granting all coll to "+user);
+ } else {
+ ps2 = con.prepareStatement("SELECT 1 FROM AUTH_HAB_COL WHERE UTI_ID=? AND COL_CLEF=?");
+ ps = con.prepareStatement("INSERT INTO AUTH_HAB_COL (UTI_ID, TID_CODE, COL_CLEF) VALUES (?,?,?)");
+ ps2.setLong(1, user);
+ ps.setLong(1, user);
+ ps.setString(2, this.target == TARGETS.ETAT ? "CODIC" : "SIRET");
+ for (final String coll : collectivites.collectivites) {
+ ps2.setString(2, coll);
+ rs = ps2.executeQuery();
+ if (rs.next()) {
+ // nothing to do
+ // logger.debug("user "+user+" is already granted on coll="+coll);
+ } else {
+ ps.setString(3, coll);
+ ps.executeUpdate();
+ // logger.debug("granting "+user+" on coll="+coll);
+ }
+ rs.close();
+ rs = null;
+ }
+ ps2.close();
+ ps2 = null;
+ ps.close();
+ ps = null;
+ }
+ // profils
+ ps = con.prepareStatement("SELECT UTI_ALL_FONC FROM AUTH_UTILISATEUR WHERE UTI_ID=?");
+ ps.setLong(1, user);
+ rs = ps.executeQuery();
+ if (rs.next()) {
+ if (rs.getInt(1) == 0) {
+ rs.close();
+ rs = null;
+ ps.close();
+ ps = null;
+ ps = con.prepareStatement("SELECT 1 FROM AUTH_UTI_PRF WHERE UTI_ID=? AND PRF_CODE=?");
+ ps.setLong(1, user);
+ ps.setString(2, profil);
+ rs = ps.executeQuery();
+ if (!rs.next()) {
+ rs.close();
+ rs = null;
+ ps.close();
+ ps = null;
+ ps = con.prepareStatement("INSERT INTO AUTH_UTI_PRF (UTI_ID, PRF_CODE) VALUES (?,?)");
+ ps.setLong(1, user);
+ ps.setString(2, profil);
+ ps.executeUpdate();
+ // logger.debug("granting prf="+profil+" to "+user);
+ // ps.close(); ps =null;
+ } else {
+ rs.close();
+ rs = null;
+ }
+ }
+ } else {
+ // it should never happen, we have ensured that user exists
+ rs.close();
+ rs = null;
+ }
+ ps.close();
+ ps = null;
+ // documents
+ if (documents.all) {
+ ps = con.prepareStatement("UPDATE AUTH_UTILISATEUR SET UTI_ALL_DOC=1 WHERE UTI_ID=?");
+ ps.setLong(1, user);
+ ps.executeUpdate();
+ ps.close();
+ ps = null;
+ // logger.debug("granting all docs to "+user);
+ } else {
+ ps2 = con.prepareStatement("SELECT 1 FROM AUTH_HAB_DOC WHERE UTI_ID=? AND DOC_CODE=?");
+ ps = con.prepareStatement("INSERT INTO AUTH_HAB_DOC (UTI_ID, DOC_CODE) VALUES (?,?)");
+ ps2.setLong(1, user);
+ ps.setLong(1, user);
+ for (final String docId : documents.documents) {
+ ps2.setString(2, docId);
+ rs = ps2.executeQuery();
+ if (rs.next()) {
+ // nothing to do
+ // logger.debug(user+" is already granted to doc="+docId);
+ } else {
+ ps.setString(2, docId);
+ ps.executeUpdate();
+ // logger.debug("granting doc="+docId+" to "+user);
+ }
+ rs.close();
+ rs = null;
+ }
+ ps2.close();
+ ps2 = null;
+ ps.close();
+ ps = null;
+ }
+ }
+ } catch (final SQLException sqlEx) {
+ logger.error("grantsUser:", sqlEx);
+ } finally {
+ if (rs != null) {
+ try {
+ rs.close();
+ rs = null;
+ } catch (final SQLException sqlEx) {
+ }
+ }
+ if (ps2 != null) {
+ try {
+ ps2.close();
+ ps2 = null;
+ } catch (final SQLException sqlEx) {
+ }
+ }
+ if (ps != null) {
+ try {
+ ps.close();
+ ps = null;
+ } catch (final SQLException sqlEx) {
+ }
+ }
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ con = null;
+ }
+ }
+ }
+
+ private Long getUserIdFromLogin(final String login) {
+ Connection con = null;
+ PreparedStatement ps = null;
+ ResultSet rs = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ ps = con.prepareStatement("SELECT UTI_ID FROM AUTH_UTILISATEUR WHERE UTI_LOGIN=?");
+ ps.setString(1, login);
+ rs = ps.executeQuery();
+ if (rs.next()) {
+ return rs.getLong(1);
+ } else {
+ // we must create it
+ rs.close();
+ ps.close();
+ ps = con.prepareStatement("INSERT INTO AUTH_UTILISATEUR (UTI_LOGIN, UTI_ACTIF, UTI_ALL_FONC, UTI_ALL_DOC, UTI_ALL_COL) VALUES (?,1,0,0,0)");
+ ps.setString(1, login);
+ ps.executeUpdate();
+ ps.close();
+ ps = con.prepareStatement("SELECT MAX(UTI_ID) FROM AUTH_UTILISATEUR WHERE UTI_LOGIN=?");
+ ps.setString(1, login);
+ rs = ps.executeQuery();
+ long userId = 0L;
+ if (rs.next()) {
+ userId = rs.getLong(1);
+ }
+ rs.close();
+ ps = con.prepareStatement("INSERT INTO AUTH_UTI_PRF (UTI_ID,PRF_CODE) VALUES (?,'CONNECT')");
+ ps.setLong(1, userId);
+ ps.executeUpdate();
+ ps.close();
+ return userId;
+ }
+ } catch (final SQLException sqlEx) {
+ logger.error("grantsUser:", sqlEx);
+ } finally {
+ if (rs != null) {
+ try {
+ rs.close();
+ rs = null;
+ } catch (final SQLException sqlEx) {
+ }
+ }
+ if (ps != null) {
+ try {
+ ps.close();
+ ps = null;
+ } catch (final SQLException sqlEx) {
+ }
+ }
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ con = null;
+ }
+ }
+ return 0L;
+ }
+
+ private class UsersToSet {
+ boolean all = false;
+ ArrayList<Long> users = null;
+
+ public UsersToSet(final String spec) {
+ super();
+ this.users = new ArrayList<Long>();
+ final String[] items = spec.split(",");
+ for (final String item : items) {
+ if (TOUS.equals(item)) {
+ this.all = true;
+ this.users.clear();
+ return;
+ } else if (item.startsWith("@")) {
+ // on supprime le '@'
+ this.users.addAll(CredentialSetter.this.getUsersFromCRC(item.substring(1)));
+ } else {
+ // we need to query LDAP for users in that CRC
+ this.users.add(CredentialSetter.this.getUserIdFromLogin(item));
+ }
+ }
+ }
+ }
+
+ private class CollectivitesToSet {
+ private boolean all;
+ private ArrayList<String> collectivites;
+
+ public CollectivitesToSet(final String spec) {
+ super();
+ this.collectivites = new ArrayList<String>();
+ final String[] items = spec.split(",");
+ for (final String item : items) {
+ if (TOUS.equals(item)) {
+ this.all = true;
+ this.collectivites.clear();
+ return;
+ } else if (item.indexOf('@') == 0) {
+ // it's a CRC
+ this.collectivites.addAll(CredentialSetter.this.getCollectivitesFromCRC(item.substring(1)));
+ } else {
+ this.collectivites.add(item);
+ }
+ }
+ }
+ }
+
+ private class DocumentsToSet {
+ private boolean all = false;
+ private TreeSet<String> documents = null;
+
+ public DocumentsToSet(final String spec) {
+ super();
+ this.documents = new TreeSet<String>();
+ final String[] items = spec.split(",");
+ for (final String item : items) {
+ if (TOUS.equals(item)) {
+ this.all = true;
+ this.documents.clear();
+ return;
+ } else {
+ if ("compteGestion".equals(item)) {
+ this.documents.add("compteGestion");
+ this.documents.add("cg-colloc");
+ } else if ("documentPaye".equals(item)) {
+ this.documents.add("documentPaye");
+ this.documents.add("cfg-paye");
+ } else if ("compteGestionEtat".equals(item)) {
+ this.documents.add("compteGestionEtat");
+ this.documents.add("cg-etat");
+ } else if ("DocumentRapport".equals(item)) {
+ this.documents.add("DocumentRapport");
+ this.documents.add("rapport");
+ } else if ("PES_Aller".equals(item)) {
+ this.documents.add("PES_Aller");
+ this.documents.add("pes-aller");
+ } else {
+ this.documents.add(item);
+ }
+ }
+ }
+ }
+ }
}
/**
* Exception thrown if input line format is incorrect
+ *
* @author cmarchand
*/
public class ParsingException extends Exception {
- public ParsingException() {
- super();
- }
- public ParsingException(String message) { super(message); }
- public ParsingException(String message, Throwable cause) { super(message, cause); }
- public ParsingException(Throwable cause) { super(cause); }
+ public ParsingException() {
+ super();
+ }
+
+ public ParsingException(final String message) {
+ super(message);
+ }
+
+ public ParsingException(final String message, final Throwable cause) {
+ super(message, cause);
+ }
+
+ public ParsingException(final Throwable cause) {
+ super(cause);
+ }
}
*/
package fr.gouv.finances.dgfip.xemelios.batch;
-import fr.gouv.finances.dgfip.utils.DateUtils;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
+
import org.apache.log4j.Logger;
+import fr.gouv.finances.dgfip.utils.DateUtils;
+
/**
* @author CBO
*/
public abstract class AbstractBatch {
- static private final int SUCCESS_RETURN_CODE = 0;
- static private final int ERROR_RETURN_CODE = 1;
- private String args[];
- private Logger logger;
- private Date startDate;
-// private String batchVersion;
- private SimpleDateFormat dateTimeFormatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
-
- abstract protected void process() throws Exception;
-
- public AbstractBatch(String args[]) {
- this.args = args;
- }
-
- protected boolean canStart() throws Exception {
- return true;
- }
-
- protected void processTerminaison() throws Exception {
- }
-
- public String[] getArgs() {
- return args;
- }
-
- public int launchBatch() {
- int codeRetour = ERROR_RETURN_CODE;
- try {
-// // On initialise dans un premier temps les logs :
-// File log4jConfiFile = getConfFile("log4j.conf.file", "log4j.xml");
-// if (log4jConfiFile == null) {
-// throw new Exception("Fichier de configuration de log4j introuvable");
-// }
-// DOMConfigurator.configure(log4jConfiFile.toString());
- this.logger = Logger.getLogger(this.getClass());
- // On initialise le reste :
- initialize();
- if (canStart()) {
- try {
- this.startDate = new Date();
-// getLogger().info("Début du traitement de " + getShortName() + " " + getBatchVersion() + " le " + DateUtils.formatDateTime(startDate));
-// getLogger().info("Max memory : " + (Runtime.getRuntime().maxMemory() / 1024 / 1024) + " Mb");
-// getLogger().info("Total free memory : " + ((Runtime.getRuntime().maxMemory() - Runtime.getRuntime().totalMemory() + Runtime.getRuntime().freeMemory()) / 1024 / 1024) + " Mb");
- process();
- Date endDate = new Date();
- long duration = endDate.getTime() - startDate.getTime();
-// getLogger().info("Fin du traitement de " + getShortName() + " " + getBatchVersion() + " le " + DateUtils.formatDateTime(endDate) + " , durée : " + DateUtils.durationToString(duration));
- } finally {
- processTerminaison();
- }
- }
- codeRetour = SUCCESS_RETURN_CODE;
- } catch (Throwable throwable) {
- String errorMessage = throwable.getMessage() != null ? throwable.getMessage() : throwable.toString();
- if (this.logger != null) {
- getLogger().error(errorMessage, throwable);
- }
- System.err.println("ERROR : " + DateUtils.formatDateTime(new Date()) + " : " + errorMessage);
- throwable.printStackTrace();
- }
- return codeRetour;
- }
-
- protected void initialize() throws Exception {
-// File poolConfigFile = getConfFile("pool.conf.file", "mysql.properties");
-// if (poolConfigFile == null) {
-// throw new Exception("Fichier de configuration d'accès à la base de données non paramétré");
-// }
-// if (!poolConfigFile.exists()) {
-// throw new Exception("Fichier de configuration d'accès à la base de données introuvabel: " + poolConfigFile.getAbsolutePath());
-// }
-// Properties props = new Properties();
-// props.load(new FileInputStream(poolConfigFile));
-// PoolManager.getInstance().init(props);
-// Properties pomProperties = new Properties();
-// this.batchVersion = "unknown";
- }
-
- protected Logger getLogger() {
- if (logger == null) {
- throw new RuntimeException("Logger is not yet initialized");
- }
- return this.logger;
- }
-
- public Date getStartDate() {
- return startDate;
- }
-
- protected String getShortName() {
- return getClass().getName().substring(getClass().getName().lastIndexOf(".") + 1);
- }
-
- protected abstract String getBatchVersion();
-
- protected File getConfFile(String fileNameSystemProperty, String fileNameIfSystemPropertyNotFound) {
- return getConfFile(fileNameSystemProperty, fileNameIfSystemPropertyNotFound, fileNameIfSystemPropertyNotFound);
- }
-
- protected File getConfFile(String fileNameSystemProperty, String fileNameIfSystemPropertyNotFound, String errorDisplayName) {
- String confFileName = System.getProperty(fileNameSystemProperty);
- File confFile = null;
- if (confFileName == null) {
- confFileName = "conf" + File.separator + fileNameIfSystemPropertyNotFound;
- confFile = new File(confFileName);
- if (!confFile.exists()) {
- confFileName = fileNameIfSystemPropertyNotFound;
- confFile = new File(confFileName);
- if (!confFile.exists()) {
- return null;
- }
- }
- } else {
- confFile = new File(confFileName);
- if (!confFile.exists()) {
- return null;
- }
- }
- return confFile;
- }
+ static private final int SUCCESS_RETURN_CODE = 0;
+ static private final int ERROR_RETURN_CODE = 1;
+ private final String args[];
+ private Logger logger;
+ private Date startDate;
+ // private String batchVersion;
+ private final SimpleDateFormat dateTimeFormatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
+
+ abstract protected void process() throws Exception;
+
+ public AbstractBatch(final String args[]) {
+ this.args = args;
+ }
+
+ protected boolean canStart() throws Exception {
+ return true;
+ }
+
+ protected void processTerminaison() throws Exception {
+ }
+
+ public String[] getArgs() {
+ return this.args;
+ }
+
+ public int launchBatch() {
+ int codeRetour = ERROR_RETURN_CODE;
+ try {
+ // // On initialise dans un premier temps les logs :
+ // File log4jConfiFile = getConfFile("log4j.conf.file", "log4j.xml");
+ // if (log4jConfiFile == null) {
+ // throw new Exception("Fichier de configuration de log4j introuvable");
+ // }
+ // DOMConfigurator.configure(log4jConfiFile.toString());
+ this.logger = Logger.getLogger(this.getClass());
+ // On initialise le reste :
+ this.initialize();
+ if (this.canStart()) {
+ try {
+ this.startDate = new Date();
+ // getLogger().info("Début du traitement de " + getShortName() + " " + getBatchVersion() + " le " + DateUtils.formatDateTime(startDate));
+ // getLogger().info("Max memory : " + (Runtime.getRuntime().maxMemory() / 1024 / 1024) + " Mb");
+ // getLogger().info("Total free memory : " + ((Runtime.getRuntime().maxMemory() - Runtime.getRuntime().totalMemory() +
+ // Runtime.getRuntime().freeMemory()) / 1024 / 1024) + " Mb");
+ this.process();
+ final Date endDate = new Date();
+ final long duration = endDate.getTime() - this.startDate.getTime();
+ // getLogger().info("Fin du traitement de " + getShortName() + " " + getBatchVersion() + " le " + DateUtils.formatDateTime(endDate) +
+ // " , durée : " + DateUtils.durationToString(duration));
+ } finally {
+ this.processTerminaison();
+ }
+ }
+ codeRetour = SUCCESS_RETURN_CODE;
+ } catch (final Throwable throwable) {
+ final String errorMessage = throwable.getMessage() != null ? throwable.getMessage() : throwable.toString();
+ if (this.logger != null) {
+ this.getLogger().error(errorMessage, throwable);
+ }
+ System.err.println("ERROR : " + DateUtils.formatDateTime(new Date()) + " : " + errorMessage);
+ throwable.printStackTrace();
+ }
+ return codeRetour;
+ }
+
+ protected void initialize() throws Exception {
+ // File poolConfigFile = getConfFile("pool.conf.file", "mysql.properties");
+ // if (poolConfigFile == null) {
+ // throw new Exception("Fichier de configuration d'accès à la base de données non paramétré");
+ // }
+ // if (!poolConfigFile.exists()) {
+ // throw new Exception("Fichier de configuration d'accès à la base de données introuvabel: " + poolConfigFile.getAbsolutePath());
+ // }
+ // Properties props = new Properties();
+ // props.load(new FileInputStream(poolConfigFile));
+ // PoolManager.getInstance().init(props);
+ // Properties pomProperties = new Properties();
+ // this.batchVersion = "unknown";
+ }
+
+ protected Logger getLogger() {
+ if (this.logger == null) {
+ throw new RuntimeException("Logger is not yet initialized");
+ }
+ return this.logger;
+ }
+
+ public Date getStartDate() {
+ return this.startDate;
+ }
+
+ protected String getShortName() {
+ return this.getClass().getName().substring(this.getClass().getName().lastIndexOf(".") + 1);
+ }
+
+ protected abstract String getBatchVersion();
+
+ protected File getConfFile(final String fileNameSystemProperty, final String fileNameIfSystemPropertyNotFound) {
+ return this.getConfFile(fileNameSystemProperty, fileNameIfSystemPropertyNotFound, fileNameIfSystemPropertyNotFound);
+ }
+
+ protected File getConfFile(final String fileNameSystemProperty, final String fileNameIfSystemPropertyNotFound, final String errorDisplayName) {
+ String confFileName = System.getProperty(fileNameSystemProperty);
+ File confFile = null;
+ if (confFileName == null) {
+ confFileName = "conf" + File.separator + fileNameIfSystemPropertyNotFound;
+ confFile = new File(confFileName);
+ if (!confFile.exists()) {
+ confFileName = fileNameIfSystemPropertyNotFound;
+ confFile = new File(confFileName);
+ if (!confFile.exists()) {
+ return null;
+ }
+ }
+ } else {
+ confFile = new File(confFileName);
+ if (!confFile.exists()) {
+ return null;
+ }
+ }
+ return confFile;
+ }
}
*/
package fr.gouv.finances.dgfip.xemelios.batch;
-import fr.gouv.finances.cp.utils.PropertiesExpansion;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;
+
import org.apache.log4j.Logger;
+import fr.gouv.finances.cp.utils.PropertiesExpansion;
+
/**
* Classe regroupant les elements communs aux traitements batch
+ *
* @author CBO
*/
public abstract class Batch extends AbstractBatch {
- private Properties props;
- private List<String> postProcessors;
- private boolean started = false;
- private PropertiesExpansion applicationConfiguration;
-
- /**
- * @param args Arguments éventuels de la ligne de commande
- */
- public Batch(String args[]) {
- super(args);
- BatchRunner.getInstance().registerBatch(this);
- }
-
- @Override
- protected void finalize() throws Throwable {
- BatchRunner.getInstance().unregisterBatch(this);
- super.finalize();
- }
-
-
- @Override
- protected void initialize() throws Exception {
- super.initialize();
- checkDirectories();
- }
-
- protected void checkDirectories() throws Exception { }
-
- @Override
- protected void processTerminaison() throws Exception {
-// Connection connection = null;
-// try {
-// connection = PoolManager.getInstance().getConnection();
-// } finally {
-// PoolManager.getInstance().releaseConnection(connection);
-// }
- }
-
- @Override
- protected boolean canStart() throws Exception {
- return true;
- }
-
-
- /**
- * Implémentation du traitement
- * @throws Exception
- */
- protected abstract void doProcess() throws Exception;
-
- /**
- * @return Resumé du traitement exemple: 35 fichiers traites avec succès, 3 dossiers en erreur
- */
- abstract public String getResumeTraitement();
-
- /**
- * @return Le refCode du type de traitement
- */
- abstract public String typeTraitementRefCode();
-
- @Override
- protected void process() throws Exception {
- startRapportTraitements();
- try {
- started = true;
- doProcess();
- } catch (Throwable e) {
- } finally {
- endRapportTraitements();
- }
- }
-
- public boolean isStarted() { return started; }
-
- /**
- * Démarre le rapport du traitement
- */
- private void startRapportTraitements() throws SQLException { }
-
- /**
- * Met fin au rapport traitememt
- */
- private void endRapportTraitements() throws SQLException { }
-
- public Properties getProps() {
- return props;
- }
-
- public void setProps(Properties props) {
- this.props = props;
- }
-
- public List<String> getPostProcessors() {
- return postProcessors;
- }
-
- public void setPostProcessors(List<String> postProcessors) {
- this.postProcessors = postProcessors;
- }
-
- public abstract String getInformations();
-
- @Override
- public int hashCode() {
- return System.identityHashCode(this);
- }
-
- @Override
- public boolean equals(Object obj) {
- if(obj instanceof Batch)
- return hashCode()==obj.hashCode();
- else
- return obj.equals(this);
- }
-
-
- public abstract class RunnableBatch extends Thread {
- private ThreadPool notifier = null;
- public void setNotifier(ThreadPool th) {
- this.notifier=th;
- }
- public ThreadPool getNotifier() { return notifier; }
- public abstract String getInformations();
- }
- public static class ThreadPool<T extends RunnableBatch> {
- private static final Logger logger = Logger.getLogger(ThreadPool.class);
- private List<T> threads;
- private int maxConcurrentThreads;
- private int runningThreads = 0;
- public ThreadPool(List<T> threads) {
- super();
- this.threads=threads;
- }
- public synchronized void notifyThreadStop(T thread) {
- threads.remove(thread);
- runningThreads--;
- synchronized(this) {
- for(RunnableBatch aThread:threads) {
- if(runningThreads==maxConcurrentThreads) break;
- if(!aThread.isAlive()) {
- aThread.setNotifier(this);
- runningThreads++;
- aThread.start();
- }
- }
- }
- }
- public void start(int maxConcurrentThreads) {
- logger.debug("starting with "+maxConcurrentThreads+" max threads");
- this.maxConcurrentThreads=maxConcurrentThreads;
- while(runningThreads<maxConcurrentThreads && threads.size()>0) {
- for(int i=0;i<threads.size();i++) {
- if(!threads.get(i).isAlive()) {
- threads.get(i).setNotifier(this);
- runningThreads++;
- threads.get(i).start();
- break;
- }
- }
- }
- }
- public int getRunningThreadCount() { return runningThreads; }
- public List<T> getThreads() { return threads; }
- }
-
- public PropertiesExpansion getApplicationConfiguration() {
- return applicationConfiguration;
- }
-
- public void setApplicationConfiguration(PropertiesExpansion applciationConfiguration) {
- this.applicationConfiguration = applciationConfiguration;
- }
+ private Properties props;
+ private List<String> postProcessors;
+ private boolean started = false;
+ private PropertiesExpansion applicationConfiguration;
+
+ /**
+ * @param args
+ * Arguments éventuels de la ligne de commande
+ */
+ public Batch(final String args[]) {
+ super(args);
+ BatchRunner.getInstance().registerBatch(this);
+ }
+
+ @Override
+ protected void finalize() throws Throwable {
+ BatchRunner.getInstance().unregisterBatch(this);
+ super.finalize();
+ }
+
+ @Override
+ protected void initialize() throws Exception {
+ super.initialize();
+ this.checkDirectories();
+ }
+
+ protected void checkDirectories() throws Exception {
+ }
+
+ @Override
+ protected void processTerminaison() throws Exception {
+ // Connection connection = null;
+ // try {
+ // connection = PoolManager.getInstance().getConnection();
+ // } finally {
+ // PoolManager.getInstance().releaseConnection(connection);
+ // }
+ }
+
+ @Override
+ protected boolean canStart() throws Exception {
+ return true;
+ }
+
+ /**
+ * Implémentation du traitement
+ *
+ * @throws Exception
+ */
+ protected abstract void doProcess() throws Exception;
+
+ /**
+ * @return Resumé du traitement exemple: 35 fichiers traites avec succès, 3 dossiers en erreur
+ */
+ abstract public String getResumeTraitement();
+
+ /**
+ * @return Le refCode du type de traitement
+ */
+ abstract public String typeTraitementRefCode();
+
+ @Override
+ protected void process() throws Exception {
+ this.startRapportTraitements();
+ try {
+ this.started = true;
+ this.doProcess();
+ } catch (final Throwable e) {
+ } finally {
+ this.endRapportTraitements();
+ }
+ }
+
+ public boolean isStarted() {
+ return this.started;
+ }
+
+ /**
+ * Démarre le rapport du traitement
+ */
+ private void startRapportTraitements() throws SQLException {
+ }
+
+ /**
+ * Met fin au rapport traitememt
+ */
+ private void endRapportTraitements() throws SQLException {
+ }
+
+ public Properties getProps() {
+ return this.props;
+ }
+
+ public void setProps(final Properties props) {
+ this.props = props;
+ }
+
+ public List<String> getPostProcessors() {
+ return this.postProcessors;
+ }
+
+ public void setPostProcessors(final List<String> postProcessors) {
+ this.postProcessors = postProcessors;
+ }
+
+ public abstract String getInformations();
+
+ @Override
+ public int hashCode() {
+ return System.identityHashCode(this);
+ }
+
+ @Override
+ public boolean equals(final Object obj) {
+ if (obj instanceof Batch) {
+ return this.hashCode() == obj.hashCode();
+ } else {
+ return obj.equals(this);
+ }
+ }
+
+ public abstract class RunnableBatch extends Thread {
+ private ThreadPool notifier = null;
+
+ public void setNotifier(final ThreadPool th) {
+ this.notifier = th;
+ }
+
+ public ThreadPool getNotifier() {
+ return this.notifier;
+ }
+
+ public abstract String getInformations();
+ }
+
+ public static class ThreadPool<T extends RunnableBatch> {
+ private static final Logger logger = Logger.getLogger(ThreadPool.class);
+ private final List<T> threads;
+ private int maxConcurrentThreads;
+ private int runningThreads = 0;
+
+ public ThreadPool(final List<T> threads) {
+ super();
+ this.threads = threads;
+ }
+
+ public synchronized void notifyThreadStop(final T thread) {
+ this.threads.remove(thread);
+ this.runningThreads--;
+ synchronized (this) {
+ for (final RunnableBatch aThread : this.threads) {
+ if (this.runningThreads == this.maxConcurrentThreads) {
+ break;
+ }
+ if (!aThread.isAlive()) {
+ aThread.setNotifier(this);
+ this.runningThreads++;
+ aThread.start();
+ }
+ }
+ }
+ }
+
+ public void start(final int maxConcurrentThreads) {
+ logger.debug("starting with " + maxConcurrentThreads + " max threads");
+ this.maxConcurrentThreads = maxConcurrentThreads;
+ while (this.runningThreads < maxConcurrentThreads && this.threads.size() > 0) {
+ for (int i = 0; i < this.threads.size(); i++) {
+ if (!this.threads.get(i).isAlive()) {
+ this.threads.get(i).setNotifier(this);
+ this.runningThreads++;
+ this.threads.get(i).start();
+ break;
+ }
+ }
+ }
+ }
+
+ public int getRunningThreadCount() {
+ return this.runningThreads;
+ }
+
+ public List<T> getThreads() {
+ return this.threads;
+ }
+ }
+
+ public PropertiesExpansion getApplicationConfiguration() {
+ return this.applicationConfiguration;
+ }
+
+ public void setApplicationConfiguration(final PropertiesExpansion applciationConfiguration) {
+ this.applicationConfiguration = applciationConfiguration;
+ }
}
import java.util.StringTokenizer;
/**
- *
+ *
* @author Christophe Marchand <christophe.marchand@axyus.com>
*/
public class BatchInformationListener {
- private boolean stop = false;
- private BatchRunner runner;
- private ServerSocket server_socket = null;
- private static BatchInformationListener instance = null;
- private int port;
-
- public BatchInformationListener(BatchRunner runner, int port) {
- super();
- BatchInformationListener.instance=this;
- this.runner = runner;
- this.port = port;
- Thread starter = new Thread() {
- public void run() {
- try {
- server_socket = new ServerSocket(BatchInformationListener.this.port);
- while (!stop) {
- Socket socket = server_socket.accept();
-
- // Construct handler to process the HTTP request message.
- try {
- httpRequestHandler request = new httpRequestHandler(socket, BatchInformationListener.this.runner);
- Thread thread = new Thread(request);
-
- // Start the thread.
- thread.start();
- } catch (Exception e) {
- }
- }
- } catch (IOException e) {
- } finally {
- try {
- server_socket.close();
- } catch (Throwable t) {
- }
- }
- }
- };
- starter.start();
- }
-
- public void stop() {
- stop = true;
- try {
- server_socket.close();
- } catch (IOException ioEx) {
- // on ignore
- }
-
- }
- public static BatchInformationListener getInstance() { return instance; }
+ private boolean stop = false;
+ private final BatchRunner runner;
+ private ServerSocket server_socket = null;
+ private static BatchInformationListener instance = null;
+ private final int port;
+
+ public BatchInformationListener(final BatchRunner runner, final int port) {
+ super();
+ BatchInformationListener.instance = this;
+ this.runner = runner;
+ this.port = port;
+ final Thread starter = new Thread() {
+ @Override
+ public void run() {
+ try {
+ BatchInformationListener.this.server_socket = new ServerSocket(BatchInformationListener.this.port);
+ while (!BatchInformationListener.this.stop) {
+ final Socket socket = BatchInformationListener.this.server_socket.accept();
+
+ // Construct handler to process the HTTP request message.
+ try {
+ final httpRequestHandler request = new httpRequestHandler(socket, BatchInformationListener.this.runner);
+ final Thread thread = new Thread(request);
+
+ // Start the thread.
+ thread.start();
+ } catch (final Exception e) {
+ }
+ }
+ } catch (final IOException e) {
+ } finally {
+ try {
+ BatchInformationListener.this.server_socket.close();
+ } catch (final Throwable t) {
+ }
+ }
+ }
+ };
+ starter.start();
+ }
+
+ public void stop() {
+ this.stop = true;
+ try {
+ this.server_socket.close();
+ } catch (final IOException ioEx) {
+ // on ignore
+ }
+
+ }
+
+ public static BatchInformationListener getInstance() {
+ return instance;
+ }
}
class httpRequestHandler implements Runnable {
- final static String CRLF = "\r\n";
- Socket socket;
- OutputStream output;
- BufferedReader br;
- private BatchRunner runner;
-
- // Constructor
- public httpRequestHandler(Socket socket, BatchRunner runner) throws Exception {
- this.socket = socket;
- this.runner = runner;
- this.output = socket.getOutputStream();
- this.br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- }
-
- // Implement the run() method of the Runnable interface.
- public void run() {
- try {
- processRequest();
- } catch (Exception e) {
- System.out.println(e);
- }
- }
-
- private void processRequest() throws Exception {
- String headerLine = br.readLine();
- if (CRLF.equals(headerLine) || "".equals(headerLine)) {
- return;
- }
-
- StringTokenizer s = new StringTokenizer(headerLine);
- String temp = s.nextToken();
-
- if (temp.equals("GET")) {
- String path = s.nextToken();
- String serverLine = "XeMeLios batch information serveur";
- String statusLine = null;
- String contentTypeLine = null;
- String entityBody = null;
- String contentLengthLine = "error";
- String informations = null;
- if("/".equals(path)) {
- if (runner != null) {
- statusLine = "HTTP/1.0 200 OK" + CRLF;
- contentTypeLine = "Content-type: text/plain" + CRLF;
- informations = runner.getInformations();
- contentLengthLine = "Content-Length: " + Integer.toString(informations.length()) + CRLF;
- } else {
- statusLine = "HTTP/1.0 404 Not Found" + CRLF;
- contentTypeLine = "text/html";
- entityBody = "<HTML>" +
- "<HEAD><TITLE>404 Not Found</TITLE></HEAD>" +
- "<BODY>404 Not Found" + "<br>usage: http://server:" + socket.getPort() + "/</BODY></HTML>";
- }
- } else {
- statusLine = "HTTP/1.0 404 Not Found" + CRLF;
- contentTypeLine = "text/html";
- entityBody = "<HTML>" +
- "<HEAD><TITLE>404 Not Found</TITLE></HEAD>" +
- "<BODY>404 Not Found" + "<br>usage: http://server:" + socket.getPort() + "/</BODY></HTML>";
- }
-
- output.write(statusLine.getBytes());
- output.write(serverLine.getBytes());
- // Send the content type line.
- output.write(contentTypeLine.getBytes());
- // Send the Content-Length
- output.write(contentLengthLine.getBytes());
- // Send a blank line to indicate the end of the header lines.
- output.write(CRLF.getBytes());
-
- // Send the entity body.
- if (runner != null) {
- output.write(informations.getBytes());
- } else {
- output.write(entityBody.getBytes());
- }
- }
-
- try {
- output.flush();
- output.close();
- br.close();
- socket.close();
- } catch (Exception e) {
- }
- }
-
- private static String contentType(String fileName) {
- if (fileName.endsWith(".htm") || fileName.endsWith(".html")) {
- return "text/html";
- } else if (fileName.endsWith(".zip")) {
- return "multipart/x-zip";
- }
-
- return "";
- }
+ final static String CRLF = "\r\n";
+ Socket socket;
+ OutputStream output;
+ BufferedReader br;
+ private final BatchRunner runner;
+
+ // Constructor
+ public httpRequestHandler(final Socket socket, final BatchRunner runner) throws Exception {
+ this.socket = socket;
+ this.runner = runner;
+ this.output = socket.getOutputStream();
+ this.br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
+ }
+
+ // Implement the run() method of the Runnable interface.
+ @Override
+ public void run() {
+ try {
+ this.processRequest();
+ } catch (final Exception e) {
+ System.out.println(e);
+ }
+ }
+
+ private void processRequest() throws Exception {
+ final String headerLine = this.br.readLine();
+ if (CRLF.equals(headerLine) || "".equals(headerLine)) {
+ return;
+ }
+
+ final StringTokenizer s = new StringTokenizer(headerLine);
+ final String temp = s.nextToken();
+
+ if (temp.equals("GET")) {
+ final String path = s.nextToken();
+ final String serverLine = "XeMeLios batch information serveur";
+ String statusLine = null;
+ String contentTypeLine = null;
+ String entityBody = null;
+ String contentLengthLine = "error";
+ String informations = null;
+ if ("/".equals(path)) {
+ if (this.runner != null) {
+ statusLine = "HTTP/1.0 200 OK" + CRLF;
+ contentTypeLine = "Content-type: text/plain" + CRLF;
+ informations = this.runner.getInformations();
+ contentLengthLine = "Content-Length: " + Integer.toString(informations.length()) + CRLF;
+ } else {
+ statusLine = "HTTP/1.0 404 Not Found" + CRLF;
+ contentTypeLine = "text/html";
+ entityBody = "<HTML>" + "<HEAD><TITLE>404 Not Found</TITLE></HEAD>" + "<BODY>404 Not Found" + "<br>usage: http://server:" + this.socket.getPort() + "/</BODY></HTML>";
+ }
+ } else {
+ statusLine = "HTTP/1.0 404 Not Found" + CRLF;
+ contentTypeLine = "text/html";
+ entityBody = "<HTML>" + "<HEAD><TITLE>404 Not Found</TITLE></HEAD>" + "<BODY>404 Not Found" + "<br>usage: http://server:" + this.socket.getPort() + "/</BODY></HTML>";
+ }
+
+ this.output.write(statusLine.getBytes());
+ this.output.write(serverLine.getBytes());
+ // Send the content type line.
+ this.output.write(contentTypeLine.getBytes());
+ // Send the Content-Length
+ this.output.write(contentLengthLine.getBytes());
+ // Send a blank line to indicate the end of the header lines.
+ this.output.write(CRLF.getBytes());
+
+ // Send the entity body.
+ if (this.runner != null) {
+ this.output.write(informations.getBytes());
+ } else {
+ this.output.write(entityBody.getBytes());
+ }
+ }
+
+ try {
+ this.output.flush();
+ this.output.close();
+ this.br.close();
+ this.socket.close();
+ } catch (final Exception e) {
+ }
+ }
+
+ private static String contentType(final String fileName) {
+ if (fileName.endsWith(".htm") || fileName.endsWith(".html")) {
+ return "text/html";
+ } else if (fileName.endsWith(".zip")) {
+ return "multipart/x-zip";
+ }
+
+ return "";
+ }
}
*/
package fr.gouv.finances.dgfip.xemelios.batch;
-import fr.gouv.finances.cp.utils.PropertiesExpansion;
-import fr.gouv.finances.dgfip.utils.xml.FactoryProvider;
-import fr.gouv.finances.dgfip.xemelios.common.Constants;
-import fr.gouv.finances.dgfip.xemelios.common.config.DocumentsModel;
-import fr.gouv.finances.dgfip.xemelios.common.config.Loader;
-import fr.gouv.finances.dgfip.xemelios.data.DataConfigurationException;
-import fr.gouv.finances.dgfip.xemelios.data.DataLayerManager;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Timer;
import java.util.TimerTask;
+
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
+
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
+import fr.gouv.finances.cp.utils.PropertiesExpansion;
+import fr.gouv.finances.dgfip.utils.xml.FactoryProvider;
+import fr.gouv.finances.dgfip.xemelios.common.Constants;
+import fr.gouv.finances.dgfip.xemelios.common.config.DocumentsModel;
+import fr.gouv.finances.dgfip.xemelios.common.config.Loader;
+import fr.gouv.finances.dgfip.xemelios.data.DataConfigurationException;
+import fr.gouv.finances.dgfip.xemelios.data.DataLayerManager;
+
/**
* This class plans batches, according to configuration file
+ *
* @author chm
*/
public class BatchRunner {
- private static final Logger logger = Logger.getLogger(BatchRunner.class);
- public static final String DATE_FORMAT="HH:mm";
- private static final SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
- private static final SimpleDateFormat debugSdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
-
- public static final int DEFAULT_STOP_PORT = 8006;
- public static final int DEFAULT_INFO_PORT = 8007;
- private File configFile;
- private ArrayList<MyTimer> timers;
-
- private static BatchRunner instance;
- private ArrayList<Batch> runningBatches;
- private BatchInformationListener infoListener = null;
- private boolean cancelled = false;
-
- private DocumentsModel documents;
-
- private PropertiesExpansion applicationConfiguration;
-
- /**
- * Point d'entrée
- * Cette classe est lancée avec l'environnement définit en propriétés système.
- * On les récupères pour faire un <code>applicationConfiguration</code> et le
- * transmettre au besoin.
- * @param args
- */
- public static void main(String args[]) {
- if (args.length == 0) {
- displaySyntax();
- throw new RuntimeException("invalid syntax");
- }
- File configFileName = new File(args[0]);
- if (!configFileName.exists()) {
- displaySyntax();
- throw new RuntimeException("config file not found: " + configFileName.getAbsolutePath());
- }
- if (args.length == 2 && "-stop".equals(args[1])) {
- new BatchRunner(configFileName, true);
- } else {
- new BatchRunner(configFileName);
- }
- }
-
- public BatchRunner(File configFile) {
- super();
- applicationConfiguration = restoreProperties();
- if(instance==null) {
- instance = this;
- runningBatches = new ArrayList<Batch>();
- }
- this.configFile = configFile;
- try {
- preInit();
- } catch(Throwable t) {
- t.printStackTrace();
- return;
- }
- try {
- initialize();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
-
- private static PropertiesExpansion restoreProperties() {
- PropertiesExpansion applicationConfiguration = new PropertiesExpansion(System.getProperties());
- // on va lire le mysql.properties
-// File f = new File(applicationConfiguration.getProperty(Constants.))
- return applicationConfiguration;
- }
-
- public BatchRunner(File configFile, boolean stop) {
- super();
- applicationConfiguration = restoreProperties();
- if(instance==null) {
- instance = this;
- runningBatches = new ArrayList<Batch>();
- }
- this.configFile = configFile;
- try {
- preInit();
- } catch(Throwable t) {
- return;
- }
- if (!stop) {
- try {
- initialize();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- } else {
- try {
- DocumentBuilderFactory domFactory = FactoryProvider.getDocumentBuilderFactory();
- domFactory.setNamespaceAware(true);
- DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
- Document config = domBuilder.parse(configFile);
- XPathFactory xpf = XPathFactory.newInstance();
- XPath xp = xpf.newXPath();
- XPath xp2 = xpf.newXPath();
- String sStopPort = (String) xp.evaluate("/batches/@stop-listen-port", config, XPathConstants.STRING);
- int stopPort = DEFAULT_STOP_PORT;
- try {
- stopPort = Integer.parseInt(sStopPort);
- } catch (NumberFormatException nfEx) {
- throw new Exception("config file contains an invalid stop-port number");
- }
- stop(stopPort);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
-
- @SuppressWarnings("unchecked")
- protected void initialize() throws Exception {
- DocumentBuilderFactory domFactory = FactoryProvider.getDocumentBuilderFactory();
- domFactory.setNamespaceAware(true);
- DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
- Document config = domBuilder.parse(configFile);
- XPathFactory xpf = FactoryProvider.getXPathFactory();
- XPath xp = xpf.newXPath();
- XPath xp2 = xpf.newXPath();
- timers = new ArrayList<MyTimer>();
- String sStopPort = (String) xp.evaluate("/batches/@stop-listen-port", config, XPathConstants.STRING);
- int stopPort = DEFAULT_STOP_PORT;
- try {
- stopPort = Integer.parseInt(sStopPort);
- } catch (NumberFormatException nfEx) {
- throw new Exception("config file contains an invalid stop-port number");
- }
- String allowedStoppers = (String) xp.evaluate("/batches/@allowedStoppers", config, XPathConstants.STRING);
- try {
- startStopListener(stopPort,allowedStoppers);
- } catch(Exception ex) {
- // si on a pas pu démarrer le listener, on quitte
- for(Timer tm:timers) {
- tm.cancel();
- tm.purge();
- }
- return;
- }
- // on demarre le infoListener
- String sInfoPort = (String) xp.evaluate("/batches/@info-listen-port", config, XPathConstants.STRING);
- int infoPort = DEFAULT_INFO_PORT;
- try {
- infoPort = Integer.parseInt(sInfoPort);
- } catch (NumberFormatException nfEx) {
- Exception ex = new Exception("config file contains an invalid info-port number");
- logger.error("while reading info-port number",ex);
- }
- try {
- startInfoListener(infoPort);
- } catch(Exception ex) {
- // si on a pas pu démarrer le listener, on logge
- logger.error("while starting info listener",ex);
- }
-
- // on charge la configuration des documents
- String documentsDefDir = (String)xp.evaluate("/batches/parameter[@name='documents-def.dir']/text()", config, XPathConstants.STRING);
- initDocuments(documentsDefDir);
-// Loader.getDocumentsInfos(documentsDefDir);
-
- // on initialise le DataLayer
- initDataLayer();
-
- Object ret = xp.evaluate("/batches/batch[not(exists(@periodicity))]", config, XPathConstants.NODESET);
- if (ret instanceof NodeList) {
- NodeList nl = (NodeList)ret;
- for( int i=0;i<nl.getLength(); i++) {
- Element batch = (Element)nl.item(i);
- if(batch.hasAttribute("if")) {
- String ifPropertyName = batch.getAttribute("if");
- if(!"true".equals(applicationConfiguration.getProperty(ifPropertyName)))
- continue;
- }
- String className = batch.getAttribute("class");
- Properties props = new Properties();
- NodeList parameters = (NodeList) xp2.evaluate("./parameter", batch, XPathConstants.NODESET);
- for (int pCount=0; pCount<parameters.getLength(); pCount++) {
- Element parameter = (Element)parameters.item(pCount);
- String pName = parameter.getAttribute("name");
- String pValue = null;
- if (parameter.hasAttribute("value")) {
- pValue = parameter.getAttribute("value");
- } else {
- pValue = parameter.getTextContent();
- }
- if (pName != null && pValue != null) {
- props.setProperty(pName, pValue);
- }
- }
- NodeList postProcessors = (NodeList) xp2.evaluate("./post-processor", batch, XPathConstants.NODESET);
- ArrayList<String> postProcessorsClasses = new ArrayList<String>();
- for(int ppCount = 0; ppCount < postProcessors.getLength(); ppCount++) {
- Element pp = (Element)postProcessors.item(ppCount);
- postProcessorsClasses.add(pp.getTextContent());
- }
- startNewBatch(className, /*periodicity, startTime,*/ props, postProcessorsClasses);
- }
-// } else if (ret instanceof List) {
-// for (Element batch : (List<Element>) ret) {
-// }
- }
-
-
- ret = xp.evaluate("/batches/batch[@periodicity]", config, XPathConstants.NODESET);
- if (ret instanceof NodeList) {
- NodeList nl = (NodeList)ret;
- for (int i=0; i<nl.getLength(); i++) {
- Element batch = (Element)nl.item(i);
- if(batch.hasAttribute("if")) {
- String ifProperty = batch.getAttribute("if");
- if(!"true".equals(applicationConfiguration.getProperty(ifProperty)))
- continue;
- }
- String className = batch.getAttribute("class");
- String periodicity = batch.getAttribute("periodicity");
- String startTime = batch.getAttribute("start-time");
- Properties props = new Properties();
- NodeList parameters = (NodeList) xp2.evaluate("./parameter", batch, XPathConstants.NODESET);
- for (int j=0;j<parameters.getLength();j++) {
- Element parameter = (Element)parameters.item(j);
- String pName = parameter.getAttribute("name");
- String pValue = null;
- if (parameter.hasAttribute("value")) {
- pValue = parameter.getAttribute("value");
- } else {
- pValue = parameter.getTextContent();
- }
- if (pName != null && pValue != null) {
- props.setProperty(pName, pValue);
- }
- }
- NodeList postProcessors = (NodeList) xp2.evaluate("./post-processor", batch, XPathConstants.NODESET);
- ArrayList<String> postProcessorsClasses = new ArrayList<String>();
- for(int k=0;k<postProcessors.getLength();k++) {
- Element el = (Element)postProcessors.item(k);
- postProcessorsClasses.add(el.getTextContent());
- }
- startNewBatch(className, periodicity, startTime, props, postProcessorsClasses);
- }
-// } else if (ret instanceof List) {
- }
-
- }
-
- public static String getCanonicalBaseDir() throws IOException {
- File baseDir = BatchRunner.getBaseDir();
- if(baseDir.getAbsolutePath().endsWith("bin"))
- baseDir = baseDir.getParentFile();
- String canon = baseDir.getCanonicalPath().replace("\\", "/");
- return canon;
- }
-
- protected void initDocuments(String dirs) {
- try {
-// File baseDir = BatchRunner.getBaseDir();
- String canon = getCanonicalBaseDir();
- String documentsDefDirs = dirs.replaceAll("%basedir%", canon);
- documents = Loader.getDocumentsInfos(documentsDefDirs);
- } catch(Exception ex) {
- System.err.println("Failed to load documents configuration from "+dirs);
- ex.printStackTrace();
- }
- }
-
- protected void startNewBatch(String className, String periodicity, String startTime, Properties props, ArrayList<String> postProcessors) {
- if(cancelled) return;
- try {
- Class clazz = Class.forName(className);
- TimerBatchTask task = new TimerBatchTask(clazz, props, postProcessors);
- char unity = periodicity.charAt(periodicity.length() - 1);
- int qty = Integer.parseInt(periodicity.substring(0, periodicity.length() - 1));
- long period = 0;
- switch (unity) {
- case 's': {
- period = qty * 1000;
- break;
- }
- case 'm': {
- period = qty * 1000 * 60;
- break;
- }
- case 'h': {
- period = qty * 1000 * 60 * 60;
- break;
- }
- case 'd': {
- period = qty * 1000 * 60 * 60 * 24;
- break;
- }
- default:
- throw new RuntimeException(periodicity + " is not a valid periodicity. Periodicity must be a quantity followed by 's', 'm', 'h' or 'd'");
- }
- if(startTime==null || startTime.length()==0) {
- try {
- MyTimer timer = new MyTimer(className);
- timers.add(timer);
- timer.scheduleAtFixedRate(task, 1000, period);
- } catch(Exception ex) {
- System.err.println("ERROR while starting "+task.batchClass.getName());
- }
- } else {
- Date d = sdf.parse(startTime);
- GregorianCalendar gc = new GregorianCalendar();
- GregorianCalendar thisMorning = new GregorianCalendar(gc.get(Calendar.YEAR),gc.get(Calendar.MONTH),gc.get(Calendar.DAY_OF_MONTH));
- Date start = new Date(thisMorning.getTimeInMillis()+d.getTime()+thisMorning.getTimeZone().getDSTSavings());
- MyTimer timer = new MyTimer(className);
- timers.add(timer);
- timer.scheduleAtFixedRate(task, start, period);
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- throw new RuntimeException(ex);
- }
- }
- protected void startNewBatch(String className, Properties props, ArrayList<String> postProcessors) {
- if(cancelled) return;
- try {
- Class clazz = Class.forName(className);
- Constructor<Batch> cstr = clazz.getConstructor(String[].class);
- Batch batch = cstr.newInstance(new Object[]{new String[]{}});
- batch.setProps(props);
- batch.setPostProcessors(postProcessors);
- batch.setApplicationConfiguration(applicationConfiguration);
- batch.launchBatch();
- } catch (Exception ex) {
- ex.printStackTrace();
- throw new RuntimeException(ex);
- }
- }
-
- public static void displaySyntax() {
- StringBuilder sb = new StringBuilder();
- sb.append("java ").append(BatchRunner.class.getName()).append(" <config-file> [-stop]\n\twhere <config-file> is the fully-qualified location of config file.");
- System.err.println(sb.toString());
- }
-
- public void stop(int port) throws Exception {
- logger.info("Stopping batch server");
- Socket so = new Socket("localhost", port);
- BufferedReader br = new BufferedReader(new InputStreamReader(so.getInputStream()));
- PrintWriter pw = new PrintWriter(so.getOutputStream(), true);
- String resp = br.readLine();
- if (resp.startsWith("+OK")) {
- pw.println("stop");
- resp = br.readLine();
- } else {
- logger.error("batch server has respond : " +resp);
- }
- so.close();
- }
-
- protected void preInit() throws Exception {
- // On initialise dans un premier temps les logs :
- File log4jConfiFile = getConfFile("log4j.conf.file", "log4j.xml");
- if (log4jConfiFile == null) {
- throw new Exception("Fichier de configuration de log4j introuvable");
- }
- DOMConfigurator.configure(log4jConfiFile.toString());
-// initDataLayer();
-// logger.debug("datalayer initialized");
- }
- protected void initDataLayer() throws DataConfigurationException {
- try {
- DataLayerManager.getImplementation();
- } catch(DataConfigurationException dcEx) {
- String availableLayers = applicationConfiguration.getProperty(Constants.SYS_PROP_AVAILABLE_LAYERS);
- if(availableLayers==null) availableLayers = "fr.gouv.finances.cp.xemelios.data.impl.MySqlDataLayer";
- StringTokenizer tokenizer = new StringTokenizer(availableLayers, ",");
- while (tokenizer.hasMoreTokens()) {
- String className = tokenizer.nextToken();
- try {
- Class.forName(className);
- } catch (ClassNotFoundException cnfEx) {
- logger.error("instanciating data layer: ", cnfEx);
- }
- }
- // aller chercher les propriétés dans le mysql.properties
-
- DataLayerManager.setApplicationProperties(applicationConfiguration);
- String layerName = applicationConfiguration.getProperty(Constants.SYS_PROP_DATA_IMPL);
- if(layerName==null) layerName = "mysql";
- DataLayerManager.setDataImpl(layerName);
-// // on interdit le cache des configs de persistence
-// DataLayerManager.getImplementation().setUseCachedPersistence(false);
- }
- }
-
- protected File getConfFile(String fileNameSystemProperty, String fileNameIfSystemPropertyNotFound) {
- return getConfFile(fileNameSystemProperty, fileNameIfSystemPropertyNotFound, fileNameIfSystemPropertyNotFound);
- }
-
- protected File getConfFile(String fileNameSystemProperty, String fileNameIfSystemPropertyNotFound, String errorDisplayName) {
- String confFileName = System.getProperty(fileNameSystemProperty);
- File confFile = null;
- if (confFileName == null) {
- confFileName = "conf" + File.separator + fileNameIfSystemPropertyNotFound;
- confFile = new File(confFileName);
- if (!confFile.exists()) {
- confFileName = fileNameIfSystemPropertyNotFound;
- confFile = new File(confFileName);
- if (!confFile.exists()) {
- return null;
- }
- }
- } else {
- confFile = new File(confFileName);
- if (!confFile.exists()) {
- return null;
- }
- }
- return confFile;
- }
-
- protected void startInfoListener(int port) throws Exception {
- infoListener = new BatchInformationListener(this,port);
- }
-
- protected void startStopListener(int port, String allowedStoppers) throws Exception {
- StopServer stopServer = new StopServer(port, timers, allowedStoppers);
- try {
- stopServer.start();
- } catch (Exception ex) {
- ex.printStackTrace();
- throw new RuntimeException("an other instance is already running.");
- }
- }
-
- public static File getBaseDir() {
- String runDir = System.getProperty("user.dir");
- File f = new File(runDir);
- // dans le cas d'un deploy batch
- if(f.getName().equals("bin")) return f.getParentFile();
- // dans le cas d'execution dans IDE
- else return f;
- }
-
- public static BatchRunner getInstance() { return instance; }
- public void registerBatch(Batch batch) {
- logger.info("registring batch "+batch.typeTraitementRefCode());
- runningBatches.add(batch);
- }
- public void unregisterBatch(Batch batch) {
- runningBatches.remove(batch);
- logger.info("unregistring batch "+batch.typeTraitementRefCode());
- }
- public String getInformations() {
- StringBuilder sb = new StringBuilder();
- for(Batch batch:runningBatches) {
- sb.append(batch.getInformations());
- }
- return sb.toString();
- }
- public class TimerBatchTask extends TimerTask {
-
- private Class batchClass;
- private Properties props;
- private List<String> postProcessors;
-
- public TimerBatchTask(Class clazz, Properties props, List<String> postProcessors) {
- super();
- this.batchClass = clazz;
- this.props = props;
- this.postProcessors=postProcessors;
- }
-
- @Override
- public void run() {
- try {
- @SuppressWarnings("unchecked")
- // TODO : vérifier que la précédente instance a finit
- Constructor<Batch> cstr = batchClass.getConstructor(String[].class);
- Batch batch = cstr.newInstance(new Object[]{new String[]{}});
- batch.setApplicationConfiguration(applicationConfiguration);
- batch.setProps(props);
- batch.setPostProcessors(postProcessors);
- batch.launchBatch();
- } catch (Exception ex) {
- ex.printStackTrace();
- throw new RuntimeException(ex);
- }
- }
- }
-
- /**
- * Renvoie la configuration des documents
- * @return
- */
- public DocumentsModel getDocuments () {
- return documents;
- }
+ private static final Logger logger = Logger.getLogger(BatchRunner.class);
+ public static final String DATE_FORMAT = "HH:mm";
+ private static final SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
+ private static final SimpleDateFormat debugSdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
+
+ public static final int DEFAULT_STOP_PORT = 8006;
+ public static final int DEFAULT_INFO_PORT = 8007;
+ private File configFile;
+ private ArrayList<MyTimer> timers;
+
+ private static BatchRunner instance;
+ private ArrayList<Batch> runningBatches;
+ private BatchInformationListener infoListener = null;
+ private final boolean cancelled = false;
+
+ private DocumentsModel documents;
+
+ private PropertiesExpansion applicationConfiguration;
+
+ /**
+ * Point d'entrée Cette classe est lancée avec l'environnement définit en propriétés système. On les récupères pour faire un
+ * <code>applicationConfiguration</code> et le transmettre au besoin.
+ *
+ * @param args
+ */
+ public static void main(final String args[]) {
+ if (args.length == 0) {
+ displaySyntax();
+ throw new RuntimeException("invalid syntax");
+ }
+ final File configFileName = new File(args[0]);
+ if (!configFileName.exists()) {
+ displaySyntax();
+ throw new RuntimeException("config file not found: " + configFileName.getAbsolutePath());
+ }
+ if (args.length == 2 && "-stop".equals(args[1])) {
+ new BatchRunner(configFileName, true);
+ } else {
+ new BatchRunner(configFileName);
+ }
+ }
+
+ public BatchRunner(final File configFile) {
+ super();
+ this.applicationConfiguration = restoreProperties();
+ if (instance == null) {
+ instance = this;
+ this.runningBatches = new ArrayList<Batch>();
+ }
+ this.configFile = configFile;
+ try {
+ this.preInit();
+ } catch (final Throwable t) {
+ t.printStackTrace();
+ return;
+ }
+ try {
+ this.initialize();
+ } catch (final Exception ex) {
+ ex.printStackTrace();
+ }
+ }
+
+ private static PropertiesExpansion restoreProperties() {
+ final PropertiesExpansion applicationConfiguration = new PropertiesExpansion(System.getProperties());
+ // on va lire le mysql.properties
+ // File f = new File(applicationConfiguration.getProperty(Constants.))
+ return applicationConfiguration;
+ }
+
+ public BatchRunner(final File configFile, final boolean stop) {
+ super();
+ this.applicationConfiguration = restoreProperties();
+ if (instance == null) {
+ instance = this;
+ this.runningBatches = new ArrayList<Batch>();
+ }
+ this.configFile = configFile;
+ try {
+ this.preInit();
+ } catch (final Throwable t) {
+ return;
+ }
+ if (!stop) {
+ try {
+ this.initialize();
+ } catch (final Exception ex) {
+ ex.printStackTrace();
+ }
+ } else {
+ try {
+ final DocumentBuilderFactory domFactory = FactoryProvider.getDocumentBuilderFactory();
+ domFactory.setNamespaceAware(true);
+ final DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
+ final Document config = domBuilder.parse(configFile);
+ final XPathFactory xpf = XPathFactory.newInstance();
+ final XPath xp = xpf.newXPath();
+ final XPath xp2 = xpf.newXPath();
+ final String sStopPort = (String) xp.evaluate("/batches/@stop-listen-port", config, XPathConstants.STRING);
+ int stopPort = DEFAULT_STOP_PORT;
+ try {
+ stopPort = Integer.parseInt(sStopPort);
+ } catch (final NumberFormatException nfEx) {
+ throw new Exception("config file contains an invalid stop-port number");
+ }
+ this.stop(stopPort);
+ } catch (final Exception ex) {
+ ex.printStackTrace();
+ }
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ protected void initialize() throws Exception {
+ final DocumentBuilderFactory domFactory = FactoryProvider.getDocumentBuilderFactory();
+ domFactory.setNamespaceAware(true);
+ final DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
+ final Document config = domBuilder.parse(this.configFile);
+ final XPathFactory xpf = FactoryProvider.getXPathFactory();
+ final XPath xp = xpf.newXPath();
+ final XPath xp2 = xpf.newXPath();
+ this.timers = new ArrayList<MyTimer>();
+ final String sStopPort = (String) xp.evaluate("/batches/@stop-listen-port", config, XPathConstants.STRING);
+ int stopPort = DEFAULT_STOP_PORT;
+ try {
+ stopPort = Integer.parseInt(sStopPort);
+ } catch (final NumberFormatException nfEx) {
+ throw new Exception("config file contains an invalid stop-port number");
+ }
+ final String allowedStoppers = (String) xp.evaluate("/batches/@allowedStoppers", config, XPathConstants.STRING);
+ try {
+ this.startStopListener(stopPort, allowedStoppers);
+ } catch (final Exception ex) {
+ // si on a pas pu démarrer le listener, on quitte
+ for (final Timer tm : this.timers) {
+ tm.cancel();
+ tm.purge();
+ }
+ return;
+ }
+ // on demarre le infoListener
+ final String sInfoPort = (String) xp.evaluate("/batches/@info-listen-port", config, XPathConstants.STRING);
+ int infoPort = DEFAULT_INFO_PORT;
+ try {
+ infoPort = Integer.parseInt(sInfoPort);
+ } catch (final NumberFormatException nfEx) {
+ final Exception ex = new Exception("config file contains an invalid info-port number");
+ logger.error("while reading info-port number", ex);
+ }
+ try {
+ this.startInfoListener(infoPort);
+ } catch (final Exception ex) {
+ // si on a pas pu démarrer le listener, on logge
+ logger.error("while starting info listener", ex);
+ }
+
+ // on charge la configuration des documents
+ final String documentsDefDir = (String) xp.evaluate("/batches/parameter[@name='documents-def.dir']/text()", config, XPathConstants.STRING);
+ this.initDocuments(documentsDefDir);
+ // Loader.getDocumentsInfos(documentsDefDir);
+
+ // on initialise le DataLayer
+ this.initDataLayer();
+
+ Object ret = xp.evaluate("/batches/batch[not(exists(@periodicity))]", config, XPathConstants.NODESET);
+ if (ret instanceof NodeList) {
+ final NodeList nl = (NodeList) ret;
+ for (int i = 0; i < nl.getLength(); i++) {
+ final Element batch = (Element) nl.item(i);
+ if (batch.hasAttribute("if")) {
+ final String ifPropertyName = batch.getAttribute("if");
+ if (!"true".equals(this.applicationConfiguration.getProperty(ifPropertyName))) {
+ continue;
+ }
+ }
+ final String className = batch.getAttribute("class");
+ final Properties props = new Properties();
+ final NodeList parameters = (NodeList) xp2.evaluate("./parameter", batch, XPathConstants.NODESET);
+ for (int pCount = 0; pCount < parameters.getLength(); pCount++) {
+ final Element parameter = (Element) parameters.item(pCount);
+ final String pName = parameter.getAttribute("name");
+ String pValue = null;
+ if (parameter.hasAttribute("value")) {
+ pValue = parameter.getAttribute("value");
+ } else {
+ pValue = parameter.getTextContent();
+ }
+ if (pName != null && pValue != null) {
+ props.setProperty(pName, pValue);
+ }
+ }
+ final NodeList postProcessors = (NodeList) xp2.evaluate("./post-processor", batch, XPathConstants.NODESET);
+ final ArrayList<String> postProcessorsClasses = new ArrayList<String>();
+ for (int ppCount = 0; ppCount < postProcessors.getLength(); ppCount++) {
+ final Element pp = (Element) postProcessors.item(ppCount);
+ postProcessorsClasses.add(pp.getTextContent());
+ }
+ this.startNewBatch(className, /* periodicity, startTime, */props, postProcessorsClasses);
+ }
+ // } else if (ret instanceof List) {
+ // for (Element batch : (List<Element>) ret) {
+ // }
+ }
+
+ ret = xp.evaluate("/batches/batch[@periodicity]", config, XPathConstants.NODESET);
+ if (ret instanceof NodeList) {
+ final NodeList nl = (NodeList) ret;
+ for (int i = 0; i < nl.getLength(); i++) {
+ final Element batch = (Element) nl.item(i);
+ if (batch.hasAttribute("if")) {
+ final String ifProperty = batch.getAttribute("if");
+ if (!"true".equals(this.applicationConfiguration.getProperty(ifProperty))) {
+ continue;
+ }
+ }
+ final String className = batch.getAttribute("class");
+ final String periodicity = batch.getAttribute("periodicity");
+ final String startTime = batch.getAttribute("start-time");
+ final Properties props = new Properties();
+ final NodeList parameters = (NodeList) xp2.evaluate("./parameter", batch, XPathConstants.NODESET);
+ for (int j = 0; j < parameters.getLength(); j++) {
+ final Element parameter = (Element) parameters.item(j);
+ final String pName = parameter.getAttribute("name");
+ String pValue = null;
+ if (parameter.hasAttribute("value")) {
+ pValue = parameter.getAttribute("value");
+ } else {
+ pValue = parameter.getTextContent();
+ }
+ if (pName != null && pValue != null) {
+ props.setProperty(pName, pValue);
+ }
+ }
+ final NodeList postProcessors = (NodeList) xp2.evaluate("./post-processor", batch, XPathConstants.NODESET);
+ final ArrayList<String> postProcessorsClasses = new ArrayList<String>();
+ for (int k = 0; k < postProcessors.getLength(); k++) {
+ final Element el = (Element) postProcessors.item(k);
+ postProcessorsClasses.add(el.getTextContent());
+ }
+ this.startNewBatch(className, periodicity, startTime, props, postProcessorsClasses);
+ }
+ // } else if (ret instanceof List) {
+ }
+
+ }
+
+ public static String getCanonicalBaseDir() throws IOException {
+ File baseDir = BatchRunner.getBaseDir();
+ if (baseDir.getAbsolutePath().endsWith("bin")) {
+ baseDir = baseDir.getParentFile();
+ }
+ final String canon = baseDir.getCanonicalPath().replace("\\", "/");
+ return canon;
+ }
+
+ protected void initDocuments(final String dirs) {
+ try {
+ // File baseDir = BatchRunner.getBaseDir();
+ final String canon = getCanonicalBaseDir();
+ final String documentsDefDirs = dirs.replaceAll("%basedir%", canon);
+ this.documents = Loader.getDocumentsInfos(documentsDefDirs);
+ } catch (final Exception ex) {
+ System.err.println("Failed to load documents configuration from " + dirs);
+ ex.printStackTrace();
+ }
+ }
+
+ protected void startNewBatch(final String className, final String periodicity, final String startTime, final Properties props, final ArrayList<String> postProcessors) {
+ if (this.cancelled) {
+ return;
+ }
+ try {
+ final Class clazz = Class.forName(className);
+ final TimerBatchTask task = new TimerBatchTask(clazz, props, postProcessors);
+ final char unity = periodicity.charAt(periodicity.length() - 1);
+ final int qty = Integer.parseInt(periodicity.substring(0, periodicity.length() - 1));
+ long period = 0;
+ switch (unity) {
+ case 's': {
+ period = qty * 1000;
+ break;
+ }
+ case 'm': {
+ period = qty * 1000 * 60;
+ break;
+ }
+ case 'h': {
+ period = qty * 1000 * 60 * 60;
+ break;
+ }
+ case 'd': {
+ period = qty * 1000 * 60 * 60 * 24;
+ break;
+ }
+ default:
+ throw new RuntimeException(periodicity + " is not a valid periodicity. Periodicity must be a quantity followed by 's', 'm', 'h' or 'd'");
+ }
+ if (startTime == null || startTime.length() == 0) {
+ try {
+ final MyTimer timer = new MyTimer(className);
+ this.timers.add(timer);
+ timer.scheduleAtFixedRate(task, 1000, period);
+ } catch (final Exception ex) {
+ System.err.println("ERROR while starting " + task.batchClass.getName());
+ }
+ } else {
+ final Date d = sdf.parse(startTime);
+ final GregorianCalendar gc = new GregorianCalendar();
+ final GregorianCalendar thisMorning = new GregorianCalendar(gc.get(Calendar.YEAR), gc.get(Calendar.MONTH), gc.get(Calendar.DAY_OF_MONTH));
+ final Date start = new Date(thisMorning.getTimeInMillis() + d.getTime() + thisMorning.getTimeZone().getDSTSavings());
+ final MyTimer timer = new MyTimer(className);
+ this.timers.add(timer);
+ timer.scheduleAtFixedRate(task, start, period);
+ }
+ } catch (final Exception ex) {
+ ex.printStackTrace();
+ throw new RuntimeException(ex);
+ }
+ }
+
+ protected void startNewBatch(final String className, final Properties props, final ArrayList<String> postProcessors) {
+ if (this.cancelled) {
+ return;
+ }
+ try {
+ final Class clazz = Class.forName(className);
+ final Constructor<Batch> cstr = clazz.getConstructor(String[].class);
+ final Batch batch = cstr.newInstance(new Object[] { new String[] {} });
+ batch.setProps(props);
+ batch.setPostProcessors(postProcessors);
+ batch.setApplicationConfiguration(this.applicationConfiguration);
+ batch.launchBatch();
+ } catch (final Exception ex) {
+ ex.printStackTrace();
+ throw new RuntimeException(ex);
+ }
+ }
+
+ public static void displaySyntax() {
+ final StringBuilder sb = new StringBuilder();
+ sb.append("java ").append(BatchRunner.class.getName()).append(" <config-file> [-stop]\n\twhere <config-file> is the fully-qualified location of config file.");
+ System.err.println(sb.toString());
+ }
+
+ public void stop(final int port) throws Exception {
+ logger.info("Stopping batch server");
+ final Socket so = new Socket("localhost", port);
+ final BufferedReader br = new BufferedReader(new InputStreamReader(so.getInputStream()));
+ final PrintWriter pw = new PrintWriter(so.getOutputStream(), true);
+ String resp = br.readLine();
+ if (resp.startsWith("+OK")) {
+ pw.println("stop");
+ resp = br.readLine();
+ } else {
+ logger.error("batch server has respond : " + resp);
+ }
+ so.close();
+ }
+
+ protected void preInit() throws Exception {
+ // On initialise dans un premier temps les logs :
+ final File log4jConfiFile = this.getConfFile("log4j.conf.file", "log4j.xml");
+ if (log4jConfiFile == null) {
+ throw new Exception("Fichier de configuration de log4j introuvable");
+ }
+ DOMConfigurator.configure(log4jConfiFile.toString());
+ // initDataLayer();
+ // logger.debug("datalayer initialized");
+ }
+
+ protected void initDataLayer() throws DataConfigurationException {
+ try {
+ DataLayerManager.getImplementation();
+ } catch (final DataConfigurationException dcEx) {
+ String availableLayers = this.applicationConfiguration.getProperty(Constants.SYS_PROP_AVAILABLE_LAYERS);
+ if (availableLayers == null) {
+ availableLayers = "fr.gouv.finances.cp.xemelios.data.impl.MySqlDataLayer";
+ }
+ final StringTokenizer tokenizer = new StringTokenizer(availableLayers, ",");
+ while (tokenizer.hasMoreTokens()) {
+ final String className = tokenizer.nextToken();
+ try {
+ Class.forName(className);
+ } catch (final ClassNotFoundException cnfEx) {
+ logger.error("instanciating data layer: ", cnfEx);
+ }
+ }
+ // aller chercher les propriétés dans le mysql.properties
+
+ DataLayerManager.setApplicationProperties(this.applicationConfiguration);
+ String layerName = this.applicationConfiguration.getProperty(Constants.SYS_PROP_DATA_IMPL);
+ if (layerName == null) {
+ layerName = "mysql";
+ }
+ DataLayerManager.setDataImpl(layerName);
+ // // on interdit le cache des configs de persistence
+ // DataLayerManager.getImplementation().setUseCachedPersistence(false);
+ }
+ }
+
+ protected File getConfFile(final String fileNameSystemProperty, final String fileNameIfSystemPropertyNotFound) {
+ return this.getConfFile(fileNameSystemProperty, fileNameIfSystemPropertyNotFound, fileNameIfSystemPropertyNotFound);
+ }
+
+ protected File getConfFile(final String fileNameSystemProperty, final String fileNameIfSystemPropertyNotFound, final String errorDisplayName) {
+ String confFileName = System.getProperty(fileNameSystemProperty);
+ File confFile = null;
+ if (confFileName == null) {
+ confFileName = "conf" + File.separator + fileNameIfSystemPropertyNotFound;
+ confFile = new File(confFileName);
+ if (!confFile.exists()) {
+ confFileName = fileNameIfSystemPropertyNotFound;
+ confFile = new File(confFileName);
+ if (!confFile.exists()) {
+ return null;
+ }
+ }
+ } else {
+ confFile = new File(confFileName);
+ if (!confFile.exists()) {
+ return null;
+ }
+ }
+ return confFile;
+ }
+
+ protected void startInfoListener(final int port) throws Exception {
+ this.infoListener = new BatchInformationListener(this, port);
+ }
+
+ protected void startStopListener(final int port, final String allowedStoppers) throws Exception {
+ final StopServer stopServer = new StopServer(port, this.timers, allowedStoppers);
+ try {
+ stopServer.start();
+ } catch (final Exception ex) {
+ ex.printStackTrace();
+ throw new RuntimeException("an other instance is already running.");
+ }
+ }
+
+ public static File getBaseDir() {
+ final String runDir = System.getProperty("user.dir");
+ final File f = new File(runDir);
+ // dans le cas d'un deploy batch
+ if (f.getName().equals("bin")) {
+ return f.getParentFile();
+ } else {
+ return f;
+ }
+ }
+
+ public static BatchRunner getInstance() {
+ return instance;
+ }
+
+ public void registerBatch(final Batch batch) {
+ logger.info("registring batch " + batch.typeTraitementRefCode());
+ this.runningBatches.add(batch);
+ }
+
+ public void unregisterBatch(final Batch batch) {
+ this.runningBatches.remove(batch);
+ logger.info("unregistring batch " + batch.typeTraitementRefCode());
+ }
+
+ public String getInformations() {
+ final StringBuilder sb = new StringBuilder();
+ for (final Batch batch : this.runningBatches) {
+ sb.append(batch.getInformations());
+ }
+ return sb.toString();
+ }
+
+ public class TimerBatchTask extends TimerTask {
+
+ private final Class batchClass;
+ private final Properties props;
+ private final List<String> postProcessors;
+
+ public TimerBatchTask(final Class clazz, final Properties props, final List<String> postProcessors) {
+ super();
+ this.batchClass = clazz;
+ this.props = props;
+ this.postProcessors = postProcessors;
+ }
+
+ @Override
+ public void run() {
+ try {
+ @SuppressWarnings("unchecked")
+ final// TODO : vérifier que la précédente instance a finit
+ Constructor<Batch> cstr = this.batchClass.getConstructor(String[].class);
+ final Batch batch = cstr.newInstance(new Object[] { new String[] {} });
+ batch.setApplicationConfiguration(BatchRunner.this.applicationConfiguration);
+ batch.setProps(this.props);
+ batch.setPostProcessors(this.postProcessors);
+ batch.launchBatch();
+ } catch (final Exception ex) {
+ ex.printStackTrace();
+ throw new RuntimeException(ex);
+ }
+ }
+ }
+
+ /**
+ * Renvoie la configuration des documents
+ *
+ * @return
+ */
+ public DocumentsModel getDocuments() {
+ return this.documents;
+ }
}
import java.util.Timer;
/**
- *
+ *
* @author Christophe Marchand <christophe.marchand@axyus.com>
*/
class MyTimer extends Timer {
- private boolean canceled = false;
-
- public MyTimer() {
- super();
- }
- public MyTimer(boolean isDeamon) {
- super(isDeamon);
- }
- public MyTimer(String name) {
- super(name);
- }
- public MyTimer(String name, boolean isDeamon) {
- super(name,isDeamon);
- }
-
- @Override
- public void cancel() {
- canceled = true;
- super.cancel();
- }
-
- public boolean isCanceled() { return canceled; }
+ private boolean canceled = false;
+
+ public MyTimer() {
+ super();
+ }
+
+ public MyTimer(final boolean isDeamon) {
+ super(isDeamon);
+ }
+
+ public MyTimer(final String name) {
+ super(name);
+ }
+
+ public MyTimer(final String name, final boolean isDeamon) {
+ super(name, isDeamon);
+ }
+
+ @Override
+ public void cancel() {
+ this.canceled = true;
+ super.cancel();
+ }
+
+ public boolean isCanceled() {
+ return this.canceled;
+ }
}
*/
package fr.gouv.finances.dgfip.xemelios.batch;
-import fr.gouv.finances.dgfip.xemelios.export.ExportJob;
/**
* Defines the post-export process
+ *
* @author Christophe Marchand <christophe.marchand@axyus.com>
*/
public interface PostProcessor {
- /**
- * Defines the export job to post-process
- * @param job
- */
- public void setConfigData(Object data);
- /**
- * Run the post process
- * @throws java.lang.Exception
- */
- public void postProcess() throws Exception;
+ /**
+ * Defines the export job to post-process
+ *
+ * @param job
+ */
+ public void setConfigData(Object data);
+
+ /**
+ * Run the post process
+ *
+ * @throws java.lang.Exception
+ */
+ public void postProcess() throws Exception;
}
*/
package fr.gouv.finances.dgfip.xemelios.batch;
-import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.Timer;
+
import org.apache.log4j.Logger;
+import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
+
/**
- *
+ *
* @author chm
*/
public class StopServer {
- private static final Logger logger = Logger.getLogger(StopServer.class);
- private int port = 0;
- private ArrayList<MyTimer> timers;
- private ServerSocket ss;
- private String[] allowedStoppers;
- protected boolean listening = true;
-
- /**
- *
- * @param port
- * @param timers
- * @param allowedStoppers
- */
- public StopServer(int port, final ArrayList<MyTimer> timers, String allowedStoppers) {
- super();
- this.port = port;
- this.timers=timers;
- StringTokenizer st = new StringTokenizer(allowedStoppers, ",");
- this.allowedStoppers = new String[st.countTokens()];
- for(int i=0;i<st.countTokens();i++) {
- this.allowedStoppers[i] = st.nextToken();
- }
- }
-
- public void start() throws Exception {
- Thread starter = new Thread() {
- @Override
- public void run() {
- try {
- ss = new ServerSocket(port);
- int count = 0;
- while(listening) {
- if(listening) {
- ++count;
- try {
- new StopServerThread(ss.accept(),StopServer.this,allowedStoppers).start();
- } catch(Exception ex) {
- // throwed when ss has been closed
- }
- }
- }
- if(!ss.isClosed()) ss.close();
- } catch(IOException ioEx) {
- System.err.println("Another instance is already running.");
- System.exit(2);
- }
- }
- };
- starter.start();
- }
- protected void stop() {
- listening=false;
- BatchInformationListener.getInstance().stop();
- for(Timer timer:timers) {
- timer.cancel();
- timer.purge();
- }
- PoolManager.getInstance().shutdown();
- System.exit(1);
- }
-
- private class StopServerThread extends Thread {
- private Socket so;
- private StopServer instance;
- private String[] allowedStoppers;
- public StopServerThread(Socket so, StopServer instance, String[] allowedStoppers) {
- super();
- this.so=so;
- this.instance=instance;
- this.allowedStoppers=allowedStoppers;
- }
- @Override
- public void run() {
- try {
- PrintWriter out = new PrintWriter(so.getOutputStream(),true);
-
- BufferedReader in = new BufferedReader(new InputStreamReader(so.getInputStream()));
-
- String inputLine, outputLine;
- if(isAcceptedStopper(so.getInetAddress().getHostAddress())) {
- logger.info("received stop order from "+so.getInetAddress().getHostAddress());
- Protocol kkp = new Protocol();
- outputLine = kkp.processInput(null);
- out.println(outputLine);
-
- while ((inputLine = in.readLine()) != null) {
- outputLine = kkp.processInput(inputLine);
- out.println(outputLine);
- if (outputLine.startsWith("+OK Bye.")) {
- instance.stop();
- break;
- }
- }
- } else {
- out.println("-ERR Unauthorized stopper, allowed are: "+allowedStoppers+", found "+so.getInetAddress().getHostAddress());
- }
- out.close();
- in.close();
- so.close();
- } catch(Exception ex) {
- ex.printStackTrace();
- }
- }
-
- protected boolean isAcceptedStopper(String address) {
- boolean ret = false;
- for(String s:allowedStoppers) {
- if("*".equals(s)) {
- ret = true;
- break;
- } else if(address.equals(s)) {
- ret = true;
- break;
- }
- }
- return ret;
- }
- }
-
- private class Protocol {
- private static final String WELCOME_STRING = "+OK Welcome on XeMeLios batch stopper";
- public String processInput(String input) {
- if(input==null) {
- return WELCOME_STRING;
- } else {
- if("stop".equals(input)) {
- return "+OK Bye.";
- } else {
- return "-ERR Unknown command.";
- }
- }
- }
- }
+ private static final Logger logger = Logger.getLogger(StopServer.class);
+ private int port = 0;
+ private final ArrayList<MyTimer> timers;
+ private ServerSocket ss;
+ private final String[] allowedStoppers;
+ protected boolean listening = true;
+
+ /**
+ *
+ * @param port
+ * @param timers
+ * @param allowedStoppers
+ */
+ public StopServer(final int port, final ArrayList<MyTimer> timers, final String allowedStoppers) {
+ super();
+ this.port = port;
+ this.timers = timers;
+ final StringTokenizer st = new StringTokenizer(allowedStoppers, ",");
+ this.allowedStoppers = new String[st.countTokens()];
+ for (int i = 0; i < st.countTokens(); i++) {
+ this.allowedStoppers[i] = st.nextToken();
+ }
+ }
+
+ public void start() throws Exception {
+ final Thread starter = new Thread() {
+ @Override
+ public void run() {
+ try {
+ StopServer.this.ss = new ServerSocket(StopServer.this.port);
+ int count = 0;
+ while (StopServer.this.listening) {
+ if (StopServer.this.listening) {
+ ++count;
+ try {
+ new StopServerThread(StopServer.this.ss.accept(), StopServer.this, StopServer.this.allowedStoppers).start();
+ } catch (final Exception ex) {
+ // throwed when ss has been closed
+ }
+ }
+ }
+ if (!StopServer.this.ss.isClosed()) {
+ StopServer.this.ss.close();
+ }
+ } catch (final IOException ioEx) {
+ System.err.println("Another instance is already running.");
+ System.exit(2);
+ }
+ }
+ };
+ starter.start();
+ }
+
+ protected void stop() {
+ this.listening = false;
+ BatchInformationListener.getInstance().stop();
+ for (final Timer timer : this.timers) {
+ timer.cancel();
+ timer.purge();
+ }
+ PoolManager.getInstance().shutdown();
+ System.exit(1);
+ }
+
+ private class StopServerThread extends Thread {
+ private final Socket so;
+ private final StopServer instance;
+ private final String[] allowedStoppers;
+
+ public StopServerThread(final Socket so, final StopServer instance, final String[] allowedStoppers) {
+ super();
+ this.so = so;
+ this.instance = instance;
+ this.allowedStoppers = allowedStoppers;
+ }
+
+ @Override
+ public void run() {
+ try {
+ final PrintWriter out = new PrintWriter(this.so.getOutputStream(), true);
+
+ final BufferedReader in = new BufferedReader(new InputStreamReader(this.so.getInputStream()));
+
+ String inputLine, outputLine;
+ if (this.isAcceptedStopper(this.so.getInetAddress().getHostAddress())) {
+ logger.info("received stop order from " + this.so.getInetAddress().getHostAddress());
+ final Protocol kkp = new Protocol();
+ outputLine = kkp.processInput(null);
+ out.println(outputLine);
+
+ while ((inputLine = in.readLine()) != null) {
+ outputLine = kkp.processInput(inputLine);
+ out.println(outputLine);
+ if (outputLine.startsWith("+OK Bye.")) {
+ this.instance.stop();
+ break;
+ }
+ }
+ } else {
+ out.println("-ERR Unauthorized stopper, allowed are: " + this.allowedStoppers + ", found " + this.so.getInetAddress().getHostAddress());
+ }
+ out.close();
+ in.close();
+ this.so.close();
+ } catch (final Exception ex) {
+ ex.printStackTrace();
+ }
+ }
+
+ protected boolean isAcceptedStopper(final String address) {
+ boolean ret = false;
+ for (final String s : this.allowedStoppers) {
+ if ("*".equals(s)) {
+ ret = true;
+ break;
+ } else if (address.equals(s)) {
+ ret = true;
+ break;
+ }
+ }
+ return ret;
+ }
+ }
+
+ private class Protocol {
+ private static final String WELCOME_STRING = "+OK Welcome on XeMeLios batch stopper";
+
+ public String processInput(final String input) {
+ if (input == null) {
+ return WELCOME_STRING;
+ } else {
+ if ("stop".equals(input)) {
+ return "+OK Bye.";
+ } else {
+ return "-ERR Unknown command.";
+ }
+ }
+ }
+ }
}
private static Object instance = null;
private static final Object locker = new Object();
- private Traitement traitement;
+ private final Traitement traitement;
private File dataDirectory;
private FileFilter fileFilter;
private AbstractImportPatcherImpl patch;
/**
* @param args
*/
- public AutoPatcher(String[] args) {
+ public AutoPatcher(final String[] args) {
super(args);
- traitement = new Traitement(AutoPatcher.class, BATCH_NAME);
+ this.traitement = new Traitement(AutoPatcher.class, BATCH_NAME);
}
@Override
protected void initialize() throws Exception {
logger.debug("Initialisation du batch : AutoPatcher");
- String dataDirProps = getProps().getProperty(DATA_DIR_PROPNAME);
- String patchClass = getProps().getProperty(PATCH_CLASS_PROPNAME);
- String filePattern = getProps().getProperty(FILE_PATTERN_PROPNAME);
+ final String dataDirProps = this.getProps().getProperty(DATA_DIR_PROPNAME);
+ final String patchClass = this.getProps().getProperty(PATCH_CLASS_PROPNAME);
+ final String filePattern = this.getProps().getProperty(FILE_PATTERN_PROPNAME);
if (!StringUtils.isEmpty(dataDirProps)) {
logger.debug("Initialisation du répertoire de donnée : " + dataDirProps);
- dataDirectory = new File(dataDirProps);
+ this.dataDirectory = new File(dataDirProps);
try {
- if (!dataDirectory.exists()) {
+ if (!this.dataDirectory.exists()) {
throw new Exception("'" + dataDirProps + "' n'existe pas");
}
- if (!dataDirectory.isDirectory()) {
+ if (!this.dataDirectory.isDirectory()) {
throw new Exception("'" + dataDirProps + "' n'est pas un réperoire");
}
- if (!dataDirectory.canWrite()) {
+ if (!this.dataDirectory.canWrite()) {
throw new Exception("'" + dataDirProps + "' n'a pas le droit d'écriture");
}
- if (!dataDirectory.canRead()) {
+ if (!this.dataDirectory.canRead()) {
throw new Exception("'" + dataDirProps + "' n'a pas le droit de lecture");
}
- } catch (Exception e) {
+ } catch (final Exception e) {
logger.error("Erreur lors de l'initialisation du répertoire de données: " + dataDirProps);
throw e;
}
logger.debug("Initialisation du filtre : " + filePattern);
final Pattern ffRegexep = Pattern.compile(StringUtils.isEmpty(filePattern) ? ".*" : filePattern);
- fileFilter = new FileFilter() {
+ this.fileFilter = new FileFilter() {
@Override
- public boolean accept(File f) {
- String absolutePath = f.getAbsolutePath();
- String path = f.getPath();
- String name = f.getName();
+ public boolean accept(final File f) {
+ final String absolutePath = f.getAbsolutePath();
+ final String path = f.getPath();
+ final String name = f.getName();
return ffRegexep.matcher(absolutePath).matches() || ffRegexep.matcher(path).matches() || ffRegexep.matcher(name).matches();
}
};
- } catch (Exception e) {
+ } catch (final Exception e) {
logger.error("Erreur lors de l'initialisation du filtre (regex): " + filePattern);
throw e;
}
} else {
- dataDirectory = null;
+ this.dataDirectory = null;
}
logger.debug("Initialisation du patch : " + patchClass);
try {
- patch = (AbstractImportPatcherImpl) Class.forName(patchClass).newInstance();
- patch.setImportServiceProvider(new ImportServiceBatchProvider(null, null, null));
- } catch (Exception e) {
+ this.patch = (AbstractImportPatcherImpl) Class.forName(patchClass).newInstance();
+ this.patch.setImportServiceProvider(new ImportServiceBatchProvider(null, null, null));
+ } catch (final Exception e) {
logger.error("Impossible d'initialiser le patch : " + patchClass);
throw e;
}
@Override
protected void doProcess() throws Exception {
- if (checkNotRunning()) {
+ if (this.checkNotRunning()) {
FileInfo patchResult;
try {
- if (dataDirectory != null) {
- File[] listFiles = dataDirectory.listFiles(fileFilter);
+ if (this.dataDirectory != null) {
+ final File[] listFiles = this.dataDirectory.listFiles(this.fileFilter);
- for (File patchData : listFiles) {
+ for (final File patchData : listFiles) {
try {
- logger.debug("Application de '" + patch.getClass() + "' avec '" + patchData.getName() + "'");
- patch.setParameter("index.file", patchData);
- patchResult = patch.run();
-
- if(!patchData.delete()){
+ logger.debug("Application de '" + this.patch.getClass() + "' avec '" + patchData.getName() + "'");
+ this.patch.setParameter("index.file", patchData);
+ patchResult = this.patch.run();
+
+ if (!patchData.delete()) {
logger.warn("Impossible de supprimer '" + patchData.getName() + "'");
}
- traitement.addMessage(Traitement.SEVERITY_INFO, patchResult.getMessage());
- } catch (Exception e) {
- logger.error("Lors de l'application de '" + patch.getClass() + "' avec '" + patchData.getName() + "'", e);
- traitement.addMessage(Traitement.SEVERITY_ERROR, "Lors de l'application de '" + patch.getClass() + "' avec '" + patchData.getName() + "' : " + e.getMessage());
+ this.traitement.addMessage(Traitement.SEVERITY_INFO, patchResult.getMessage());
+ } catch (final Exception e) {
+ logger.error("Lors de l'application de '" + this.patch.getClass() + "' avec '" + patchData.getName() + "'", e);
+ this.traitement.addMessage(Traitement.SEVERITY_ERROR, "Lors de l'application de '" + this.patch.getClass() + "' avec '" + patchData.getName() + "' : " + e.getMessage());
}
}
} else {
try {
- logger.debug("Application de '" + patch.getClass() + "'");
- patchResult = patch.run();
- } catch (Exception e) {
- logger.error("Lors de l'application de '" + patch.getClass() + "'", e);
- traitement.addMessage(Traitement.SEVERITY_ERROR, "Lors de l'application de '" + patch.getClass() + "' : " + e.getMessage());
+ logger.debug("Application de '" + this.patch.getClass() + "'");
+ patchResult = this.patch.run();
+ } catch (final Exception e) {
+ logger.error("Lors de l'application de '" + this.patch.getClass() + "'", e);
+ this.traitement.addMessage(Traitement.SEVERITY_ERROR, "Lors de l'application de '" + this.patch.getClass() + "' : " + e.getMessage());
}
}
} finally {
- logger.debug("Fin de '" + patch.getClass() + "'");
- clearLock();
+ logger.debug("Fin de '" + this.patch.getClass() + "'");
+ this.clearLock();
}
} else {
- traitement.setResume("Dejà en cours d'execution");
+ this.traitement.setResume("Dejà en cours d'execution");
}
- traitement.setEnd(System.currentTimeMillis());
+ this.traitement.setEnd(System.currentTimeMillis());
}
@Override
private static Object instance = null;
private static final Object locker = new Object();
- private Traitement traitement;
+ private final Traitement traitement;
private Hashtable adEnvConf;
/**
* @param args
*/
- public ActiveDirectorySynchronizer(String[] args) {
+ public ActiveDirectorySynchronizer(final String[] args) {
super(args);
- traitement = new Traitement(ActiveDirectorySynchronizer.class, BATCH_NAME);
+ this.traitement = new Traitement(ActiveDirectorySynchronizer.class, BATCH_NAME);
}
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void initialize() throws Exception {
- String adURL = getProps().getProperty(AD_URL_PROP);
- String adUser = getProps().getProperty(AD_USERNAME_PROP);
- String adPassword = Scramble.unScramblePassword(getProps().getProperty(AD_PASSWORD_PROP));
- String adResultPageSize = getProps().getProperty(AD_RESULT_PAGE_SIZE_PROP);
- String adFullSync = getProps().getProperty(SYNC_FULL_PROP);
+ final String adURL = this.getProps().getProperty(AD_URL_PROP);
+ final String adUser = this.getProps().getProperty(AD_USERNAME_PROP);
+ final String adPassword = Scramble.unScramblePassword(this.getProps().getProperty(AD_PASSWORD_PROP));
+ final String adResultPageSize = this.getProps().getProperty(AD_RESULT_PAGE_SIZE_PROP);
+ final String adFullSync = this.getProps().getProperty(SYNC_FULL_PROP);
- adBaseDn = getProps().getProperty(AD_BASE_DN_PROP);
- resultPageSize = (StringUtils.isEmpty(adResultPageSize) ? 100 : Integer.parseInt(adResultPageSize));
- fullSync = (StringUtils.isEmpty(adFullSync) ? false : Boolean.valueOf(adFullSync));
+ this.adBaseDn = this.getProps().getProperty(AD_BASE_DN_PROP);
+ this.resultPageSize = (StringUtils.isEmpty(adResultPageSize) ? 100 : Integer.parseInt(adResultPageSize));
+ this.fullSync = (StringUtils.isEmpty(adFullSync) ? false : Boolean.valueOf(adFullSync));
logger.debug("LDAP - url=" + adURL + " - user=" + adUser);
- adEnvConf = new Hashtable();
- adEnvConf.put(Context.INITIAL_CONTEXT_FACTORY, AD_LDAP_CONTEXT_FACTORY);
- adEnvConf.put(Context.PROVIDER_URL, adURL);
- adEnvConf.put(Context.SECURITY_AUTHENTICATION, AD_LDAP_SECURITY_AUTHENTICATION);
- adEnvConf.put(Context.SECURITY_PRINCIPAL, adUser);
- adEnvConf.put(Context.SECURITY_CREDENTIALS, adPassword);
- adEnvConf.put(Context.REFERRAL, "follow");
+ this.adEnvConf = new Hashtable();
+ this.adEnvConf.put(Context.INITIAL_CONTEXT_FACTORY, AD_LDAP_CONTEXT_FACTORY);
+ this.adEnvConf.put(Context.PROVIDER_URL, adURL);
+ this.adEnvConf.put(Context.SECURITY_AUTHENTICATION, AD_LDAP_SECURITY_AUTHENTICATION);
+ this.adEnvConf.put(Context.SECURITY_PRINCIPAL, adUser);
+ this.adEnvConf.put(Context.SECURITY_CREDENTIALS, adPassword);
+ this.adEnvConf.put(Context.REFERRAL, "follow");
logger.debug("Test d'initialisation du DirContext AD");
- InitialLdapContext adContext = new InitialLdapContext(adEnvConf, null);
+ final InitialLdapContext adContext = new InitialLdapContext(this.adEnvConf, null);
adContext.close();
logger.debug("Construction des filtres");
- DataImpl dataLayer = DataLayerManager.getImplementation();
- mainAdminDn = dataLayer.getParameterValue(PARAMETER_AD_MAIN_ADMIN_DN);
- delegAdminDn = dataLayer.getParameterValue(PARAMETER_AD_DELEGATED_ADMIN_DN);
- userDn = dataLayer.getParameterValue(PARAMETER_AD_USER_DN);
+ final DataImpl dataLayer = DataLayerManager.getImplementation();
+ this.mainAdminDn = dataLayer.getParameterValue(PARAMETER_AD_MAIN_ADMIN_DN);
+ this.delegAdminDn = dataLayer.getParameterValue(PARAMETER_AD_DELEGATED_ADMIN_DN);
+ this.userDn = dataLayer.getParameterValue(PARAMETER_AD_USER_DN);
- ldapFilterAllUser = buildFilter(LDAP_FILTER_ALL_USER, mainAdminDn, delegAdminDn, userDn);
- ldapFilterAllUserFromLastSync = buildFilter(LDAP_FILTER_USER_FROM_LAST_SYNC, mainAdminDn, delegAdminDn, userDn);
- ldapFilterUserNotXemelios = buildFilter(LDAP_FILTER_USER_NOT_XEMELIOS, mainAdminDn, delegAdminDn, userDn);
+ this.ldapFilterAllUser = this.buildFilter(LDAP_FILTER_ALL_USER, this.mainAdminDn, this.delegAdminDn, this.userDn);
+ this.ldapFilterAllUserFromLastSync = this.buildFilter(LDAP_FILTER_USER_FROM_LAST_SYNC, this.mainAdminDn, this.delegAdminDn, this.userDn);
+ this.ldapFilterUserNotXemelios = this.buildFilter(LDAP_FILTER_USER_NOT_XEMELIOS, this.mainAdminDn, this.delegAdminDn, this.userDn);
logger.debug("Inistialisation du QueryProvider");
- String queryFileName = dataLayer.getLayerName() + "-queries.properties";
- queryProvider = new QueryProvider(this.getClass().getResource(queryFileName));
+ final String queryFileName = dataLayer.getLayerName() + "-queries.properties";
+ this.queryProvider = new QueryProvider(this.getClass().getResource(queryFileName));
}
@SuppressWarnings("rawtypes")
@Override
protected void doProcess() throws Exception {
- if (checkNotRunning()) {
- PoolManager pool = PoolManager.getInstance();
- Connection connection = pool.getConnection();
- LdapContext adContext = new InitialLdapContext(adEnvConf, null);
+ if (this.checkNotRunning()) {
+ final PoolManager pool = PoolManager.getInstance();
+ final Connection connection = pool.getConnection();
+ final LdapContext adContext = new InitialLdapContext(this.adEnvConf, null);
long newUserSyncCount = 0, updatedUserSyncCount = 0, noneUserSyncCount = 0, deletedUserSyncCount = 0;
long startTime, endTime;
try {
- DataImpl dataLayer = DataLayerManager.getImplementation();
- String lastSyncDtFmt = dataLayer.getParameterValue(PARAMETER_AD_LAST_SYNC);
+ final DataImpl dataLayer = DataLayerManager.getImplementation();
+ final String lastSyncDtFmt = dataLayer.getParameterValue(PARAMETER_AD_LAST_SYNC);
NamingEnumeration usersToSynchronize;
- String filter = (lastSyncDtFmt == null || fullSync ? ldapFilterAllUser : ldapFilterAllUserFromLastSync);
- String adCurrentTime = getActiveDirectoryCurrentTime(adContext);
+ final String filter = (lastSyncDtFmt == null || this.fullSync ? this.ldapFilterAllUser : this.ldapFilterAllUserFromLastSync);
+ final String adCurrentTime = this.getActiveDirectoryCurrentTime(adContext);
logger.info("Dernière synchronisation LDAP effectuée le : " + lastSyncDtFmt);
connection.setAutoCommit(false);
- adContext.setRequestControls(new Control[] { new PagedResultsControl(resultPageSize, Control.CRITICAL) });
+ adContext.setRequestControls(new Control[] { new PagedResultsControl(this.resultPageSize, Control.CRITICAL) });
// -------- Traitement paginé des utilisateurs créés / mofifiés -------------
startTime = System.currentTimeMillis();
do {
- usersToSynchronize = getAdUsers(adContext, filter, lastSyncDtFmt);
+ usersToSynchronize = this.getAdUsers(adContext, filter, lastSyncDtFmt);
try {
while (usersToSynchronize != null && usersToSynchronize.hasMore()) {
- SearchResult adData = (SearchResult) usersToSynchronize.next();
- Attributes attributes = adData.getAttributes();
+ final SearchResult adData = (SearchResult) usersToSynchronize.next();
+ final Attributes attributes = adData.getAttributes();
if (attributes.get("userPrincipalName") == null) {
logger.warn("Utilisateur sans login ignoré [sync] : " + attributes.toString());
} else {
- ActiveDirectoryUser adUser = new ActiveDirectoryUser(attributes);
+ final ActiveDirectoryUser adUser = new ActiveDirectoryUser(attributes);
logger.debug("Synchronisation utilisateur : " + adUser.getLogin());
- switch (synchronizeUser(connection, adUser)) {
+ switch (this.synchronizeUser(connection, adUser)) {
case INSERT:
- updateUserProfiles(connection, adUser);
+ this.updateUserProfiles(connection, adUser);
newUserSyncCount++;
break;
case UPDATE:
- updateUserProfiles(connection, adUser);
+ this.updateUserProfiles(connection, adUser);
updatedUserSyncCount++;
break;
case NONE:
usersToSynchronize.close();
}
}
- } while (ldapNextPage(adContext));
+ } while (this.ldapNextPage(adContext));
// -------- Traitement paginé des utilisateurs supprimés des groupes xemelios -------------
do {
- usersToSynchronize = getAdUsers(adContext, ldapFilterUserNotXemelios, null);
+ usersToSynchronize = this.getAdUsers(adContext, this.ldapFilterUserNotXemelios, null);
try {
while (usersToSynchronize != null && usersToSynchronize.hasMore()) {
- SearchResult adData = (SearchResult) usersToSynchronize.next();
- Attributes attributes = adData.getAttributes();
+ final SearchResult adData = (SearchResult) usersToSynchronize.next();
+ final Attributes attributes = adData.getAttributes();
if (attributes.get("userPrincipalName") == null) {
logger.warn("Utilisateur sans login ignoré [unsync] : " + attributes.toString());
} else {
- ActiveDirectoryUser adUser = new ActiveDirectoryUser(attributes);
- Long userId = getUserIdFromGuid(connection, adUser.getGuid());
+ final ActiveDirectoryUser adUser = new ActiveDirectoryUser(attributes);
+ final Long userId = this.getUserIdFromGuid(connection, adUser.getGuid());
if (userId != null) {
- unsynchronizeUser(connection, userId);
+ this.unsynchronizeUser(connection, userId);
deletedUserSyncCount++;
}
}
usersToSynchronize.close();
}
}
- } while (ldapNextPage(adContext));
+ } while (this.ldapNextPage(adContext));
// on enregistre la date de synchronisation
- updateLastSyncParameter(adCurrentTime);
+ this.updateLastSyncParameter(adCurrentTime);
connection.commit();
endTime = System.currentTimeMillis();
// on calcul le périmètre d'action des utilisateurs
startTime = System.currentTimeMillis();
- buildUserScopes(connection);
+ this.buildUserScopes(connection);
connection.commit();
endTime = System.currentTimeMillis();
logger.info("Calcul des périmètres utilisateurs : " + String.format("%.2g", (endTime - startTime) / 1000.0) + " s");
- } catch (Throwable t) {
+ } catch (final Throwable t) {
logger.error("Synchonisation LDAP", t);
} finally {
logger.info("Fin de la synchronisation LDAP : " + newUserSyncCount + " utilisateurs créés, " + updatedUserSyncCount + " mis à jour, " + noneUserSyncCount + " non-synchronisés, " + deletedUserSyncCount + " supprimés");
- clearLock();
+ this.clearLock();
connection.rollback();
pool.releaseConnection(connection);
adContext.close();
}
} else {
- traitement.setResume("Dejà en cours d'execution");
+ this.traitement.setResume("Dejà en cours d'execution");
}
- traitement.setEnd(System.currentTimeMillis());
+ this.traitement.setEnd(System.currentTimeMillis());
}
- protected NamingEnumeration<?> getAdUsers(DirContext adContext, String filter, String syncDate) throws NamingException {
- SearchControls searchCtrl = new SearchControls();
+ protected NamingEnumeration<?> getAdUsers(final DirContext adContext, final String filter, final String syncDate) throws NamingException {
+ final SearchControls searchCtrl = new SearchControls();
searchCtrl.setSearchScope(SearchControls.SUBTREE_SCOPE);
- return adContext.search(adBaseDn, builFilter(filter, syncDate), searchCtrl);
+ return adContext.search(this.adBaseDn, this.builFilter(filter, syncDate), searchCtrl);
}
- protected boolean ldapNextPage(LdapContext adContext) throws NamingException, IOException {
+ protected boolean ldapNextPage(final LdapContext adContext) throws NamingException, IOException {
byte[] cookie = null;
- Control[] controls = adContext.getResponseControls();
+ final Control[] controls = adContext.getResponseControls();
if (controls != null) {
- for (int i = 0; i < controls.length; i++) {
- if (controls[i] instanceof PagedResultsResponseControl) {
- PagedResultsResponseControl prrc = (PagedResultsResponseControl) controls[i];
+ for (final Control control : controls) {
+ if (control instanceof PagedResultsResponseControl) {
+ final PagedResultsResponseControl prrc = (PagedResultsResponseControl) control;
// total = prrc.getResultSize();
cookie = prrc.getCookie();
}
}
// page suivante
- adContext.setRequestControls(new Control[] { new PagedResultsControl(resultPageSize, cookie, Control.CRITICAL) });
+ adContext.setRequestControls(new Control[] { new PagedResultsControl(this.resultPageSize, cookie, Control.CRITICAL) });
return (cookie != null);
}
- protected SyncType synchronizeUser(Connection connection, ActiveDirectoryUser adUser) throws SQLException {
- Long userId = getUserIdFromGuid(connection, adUser.getGuid());
+ protected SyncType synchronizeUser(final Connection connection, final ActiveDirectoryUser adUser) throws SQLException {
+ final Long userId = this.getUserIdFromGuid(connection, adUser.getGuid());
if (userId == null) {
- return insertXemeliosUser(connection, adUser) ? SyncType.INSERT : SyncType.NONE;
+ return this.insertXemeliosUser(connection, adUser) ? SyncType.INSERT : SyncType.NONE;
} else {
- return updateXemeliosUser(connection, adUser) ? SyncType.UPDATE : SyncType.NONE;
+ return this.updateXemeliosUser(connection, adUser) ? SyncType.UPDATE : SyncType.NONE;
}
}
- protected void unsynchronizeUser(Connection connection, Long userId) throws SQLException {
- String cleanProfiles = queryProvider.getQuery("unsynchronizeUser.cleanProfiles");
- String cleanDocuments = queryProvider.getQuery("unsynchronizeUser.cleanDocuments");
- String cleanCollectivites = queryProvider.getQuery("unsynchronizeUser.cleanCollectivites");
- String deleteUser = queryProvider.getQuery("unsynchronizeUser.deleteUser");
- SimplePStmtBinderBuilder binder = new SimplePStmtBinderBuilder();
+ protected void unsynchronizeUser(final Connection connection, final Long userId) throws SQLException {
+ final String cleanProfiles = this.queryProvider.getQuery("unsynchronizeUser.cleanProfiles");
+ final String cleanDocuments = this.queryProvider.getQuery("unsynchronizeUser.cleanDocuments");
+ final String cleanCollectivites = this.queryProvider.getQuery("unsynchronizeUser.cleanCollectivites");
+ final String deleteUser = this.queryProvider.getQuery("unsynchronizeUser.deleteUser");
+ final SimplePStmtBinderBuilder binder = new SimplePStmtBinderBuilder();
binder.add(userId);
JdbcUtils.executeUpdate(connection, deleteUser, binder.toPStmtBinder());
}
- protected Long getUserIdFromLogin(Connection connection, String login) throws SQLException {
- String getUserIdQuery = queryProvider.getQuery("getUserIdFromLogin");
- SimplePStmtBinderBuilder binder = new SimplePStmtBinderBuilder();
+ protected Long getUserIdFromLogin(final Connection connection, final String login) throws SQLException {
+ final String getUserIdQuery = this.queryProvider.getQuery("getUserIdFromLogin");
+ final SimplePStmtBinderBuilder binder = new SimplePStmtBinderBuilder();
binder.add(login);
return (Long) JdbcUtils.queryObject(connection, getUserIdQuery, new RowMapper.ColToLongRowMapper(), binder.toPStmtBinder());
}
- protected Long getUserIdFromGuid(Connection connection, String guid) throws SQLException {
- String getUserIdQuery = queryProvider.getQuery("getUserIdFromGuid");
- SimplePStmtBinderBuilder binder = new SimplePStmtBinderBuilder();
+ protected Long getUserIdFromGuid(final Connection connection, final String guid) throws SQLException {
+ final String getUserIdQuery = this.queryProvider.getQuery("getUserIdFromGuid");
+ final SimplePStmtBinderBuilder binder = new SimplePStmtBinderBuilder();
binder.add(guid);
return (Long) JdbcUtils.queryObject(connection, getUserIdQuery, new RowMapper.ColToLongRowMapper(), binder.toPStmtBinder());
}
- protected boolean insertXemeliosUser(Connection connection, ActiveDirectoryUser adUser) throws SQLException {
- String query = queryProvider.getQuery("insertXemeliosUser");
- SimplePStmtBinderBuilder binder = new SimplePStmtBinderBuilder();
+ protected boolean insertXemeliosUser(final Connection connection, final ActiveDirectoryUser adUser) throws SQLException {
+ final String query = this.queryProvider.getQuery("insertXemeliosUser");
+ final SimplePStmtBinderBuilder binder = new SimplePStmtBinderBuilder();
binder.add(adUser.getGuid());
binder.add(adUser.getLogin());
return JdbcUtils.executeUpdate(connection, query, binder.toPStmtBinder()).getCountUpdated() == 1;
}
- protected boolean updateXemeliosUser(Connection connection, ActiveDirectoryUser adUser) throws SQLException {
- String query = queryProvider.getQuery("updateXemeliosUser");
+ protected boolean updateXemeliosUser(final Connection connection, final ActiveDirectoryUser adUser) throws SQLException {
+ final String query = this.queryProvider.getQuery("updateXemeliosUser");
- SimplePStmtBinderBuilder binder = new SimplePStmtBinderBuilder();
+ final SimplePStmtBinderBuilder binder = new SimplePStmtBinderBuilder();
binder.add(adUser.getLogin());
binder.add(adUser.getFirstName());
return JdbcUtils.executeUpdate(connection, query, binder.toPStmtBinder()).getCountUpdated() > 0;
}
- protected void updateUserProfiles(Connection connection, ActiveDirectoryUser adUser) throws SQLException {
- String mainAdminAllPower = queryProvider.getQuery("updateUserProfiles.allPower");
- String cleanQuery = queryProvider.getQuery("updateUserProfiles.clean");
- String fillQuery = queryProvider.getQuery("updateUserProfiles.insertProfiles");
- Long userId = getUserIdFromGuid(connection, adUser.getGuid());
+ protected void updateUserProfiles(final Connection connection, final ActiveDirectoryUser adUser) throws SQLException {
+ final String mainAdminAllPower = this.queryProvider.getQuery("updateUserProfiles.allPower");
+ final String cleanQuery = this.queryProvider.getQuery("updateUserProfiles.clean");
+ final String fillQuery = this.queryProvider.getQuery("updateUserProfiles.insertProfiles");
+ final Long userId = this.getUserIdFromGuid(connection, adUser.getGuid());
SimplePStmtBinderBuilder binder = new SimplePStmtBinderBuilder();
binder.add(userId);
JdbcUtils.executeUpdate(connection, cleanQuery, binder.toPStmtBinder());
- for (String adGroupDN : adUser.getMemberOf()) {
+ for (final String adGroupDN : adUser.getMemberOf()) {
binder = new SimplePStmtBinderBuilder();
binder.add(userId);
- if (adGroupDN.equals(mainAdminDn)) {
+ if (adGroupDN.equals(this.mainAdminDn)) {
JdbcUtils.executeUpdate(connection, mainAdminAllPower, binder.toPStmtBinder());
binder.add(XEM_MAIN_ADMIN_PROFILE);
JdbcUtils.executeUpdate(connection, fillQuery, binder.toPStmtBinder());
- } else if (adGroupDN.equals(delegAdminDn)) {
+ } else if (adGroupDN.equals(this.delegAdminDn)) {
binder.add(XEM_DELEGATED_ADMIN_PROFILE);
JdbcUtils.executeUpdate(connection, fillQuery, binder.toPStmtBinder());
- } else if (adGroupDN.equals(userDn)) {
+ } else if (adGroupDN.equals(this.userDn)) {
binder.add(XEM_USER_PROFILE);
JdbcUtils.executeUpdate(connection, fillQuery, binder.toPStmtBinder());
} else {
}
}
- protected void buildUserScopes(Connection connection) throws SQLException {
- String cleanQuery = queryProvider.getQuery("buildUserScopes.clean");
- String getUsers = queryProvider.getQuery("buildUserScopes.getUsers");
- String insertUserScope = queryProvider.getQuery("buildUserScopes.insertUserScope");
+ protected void buildUserScopes(final Connection connection) throws SQLException {
+ final String cleanQuery = this.queryProvider.getQuery("buildUserScopes.clean");
+ final String getUsers = this.queryProvider.getQuery("buildUserScopes.getUsers");
+ final String insertUserScope = this.queryProvider.getQuery("buildUserScopes.insertUserScope");
JdbcUtils.executeUpdate(connection, cleanQuery);
connection.commit();
- List<BuildScopeUser> users = JdbcUtils.queryObjects(connection, getUsers, new RowMapper.ClassRowMapper(BuildScopeUser.class));
- for (BuildScopeUser u : users) {
+ final List<BuildScopeUser> users = JdbcUtils.queryObjects(connection, getUsers, new RowMapper.ClassRowMapper(BuildScopeUser.class));
+ for (final BuildScopeUser u : users) {
logger.debug("Build user scope : " + u.getUtiId() + " (dep:" + u.getDepCode() + ")");
JdbcUtils.executeUpdate(connection, insertUserScope, new PStmtBinder.SimplePStmtBinder(new Object[] { u.getUtiId(), u.getDepCode() }));
connection.commit();
}
}
- protected String getActiveDirectoryCurrentTime(DirContext adContext) throws NamingException, ParseException {
+ protected String getActiveDirectoryCurrentTime(final DirContext adContext) throws NamingException, ParseException {
String result = null;
- SearchControls searchCtrl = new SearchControls();
+ final SearchControls searchCtrl = new SearchControls();
searchCtrl.setSearchScope(SearchControls.OBJECT_SCOPE);
- NamingEnumeration<SearchResult> adRootDSEResult = adContext.search("", "(&(objectClass=*))", searchCtrl);
+ final NamingEnumeration<SearchResult> adRootDSEResult = adContext.search("", "(&(objectClass=*))", searchCtrl);
try {
while (adRootDSEResult.hasMoreElements()) {
- SearchResult sr = (SearchResult) adRootDSEResult.next();
- Attributes attributes = sr.getAttributes();
+ final SearchResult sr = adRootDSEResult.next();
+ final Attributes attributes = sr.getAttributes();
result = attributes.get("currentTime").get().toString();
}
} finally {
return result;
}
- protected void updateLastSyncParameter(String syncDate) throws DataConfigurationException {
- DataImpl dataLayer = DataLayerManager.getImplementation();
+ protected void updateLastSyncParameter(final String syncDate) throws DataConfigurationException {
+ final DataImpl dataLayer = DataLayerManager.getImplementation();
dataLayer.setParameterValue(PARAMETER_AD_LAST_SYNC, syncDate);
}
- protected String builFilter(String originalFilter, String syncDate) {
+ protected String builFilter(final String originalFilter, final String syncDate) {
if (syncDate == null) {
return originalFilter;
}
}
}
- private String buildFilter(String filterTemplate, String mainAdminDn, String delegatedAdminDn, String userDn) {
+ private String buildFilter(final String filterTemplate, final String mainAdminDn, final String delegatedAdminDn, final String userDn) {
String result = filterTemplate.replace(LDAP_MAIN_ADMIN_DN_TOKEN, mainAdminDn);
result = result.replace(LDAP_DELEGATED_ADMIN_DN_TOKEN, delegatedAdminDn);
result = result.replace(LDAP_USER_DN_TOKEN, userDn);
public static class BuildScopeUser {
- private Long utiId;
- private String depCode;
+ private final Long utiId;
+ private final String depCode;
- public BuildScopeUser(ResultSet rs) throws SQLException {
- utiId = rs.getLong(1);
- depCode = rs.getString(2);
+ public BuildScopeUser(final ResultSet rs) throws SQLException {
+ this.utiId = rs.getLong(1);
+ this.depCode = rs.getString(2);
}
public Long getUtiId() {
- return utiId;
+ return this.utiId;
}
public String getDepCode() {
- return depCode;
+ return this.depCode;
}
}
private static final int AD_ACCOUNT_ENABLED = 512;
// private static final int AD_ACCOUNT_DISABLED = 514;
- private String guid;
- private String login;
- private String name;
- private String firstName;
+ private final String guid;
+ private final String login;
+ private final String name;
+ private final String firstName;
private List<String> memberOf;
private boolean enabled;
- private String company;
+ private final String company;
- public ActiveDirectoryUser(Attributes attributes) throws NamingException {
- guid = (String) attributes.get("objectGUID").get();
- login = (String) attributes.get("userPrincipalName").get();
- name = (String) (attributes.get("sn") == null ? null : attributes.get("sn").get());
- firstName = (String) (attributes.get("givenName") == null ? null : attributes.get("givenName").get());
+ public ActiveDirectoryUser(final Attributes attributes) throws NamingException {
+ this.guid = (String) attributes.get("objectGUID").get();
+ this.login = (String) attributes.get("userPrincipalName").get();
+ this.name = (String) (attributes.get("sn") == null ? null : attributes.get("sn").get());
+ this.firstName = (String) (attributes.get("givenName") == null ? null : attributes.get("givenName").get());
if (attributes.get("memberOf") == null) {
- memberOf = Collections.emptyList();
+ this.memberOf = Collections.emptyList();
} else {
- memberOf = new LinkedList<String>();
- NamingEnumeration<?> allGrps = attributes.get("memberOf").getAll();
+ this.memberOf = new LinkedList<String>();
+ final NamingEnumeration<?> allGrps = attributes.get("memberOf").getAll();
try {
while (allGrps.hasMoreElements()) {
- String groupDN = allGrps.next().toString();
- memberOf.add(groupDN);
+ final String groupDN = allGrps.next().toString();
+ this.memberOf.add(groupDN);
}
} finally {
allGrps.close();
}
if (attributes.get("userAccountControl") != null) {
- String uAccCtrl = attributes.get("userAccountControl").get().toString();
- Integer uAccCtrlInt = Integer.parseInt(uAccCtrl);
+ final String uAccCtrl = attributes.get("userAccountControl").get().toString();
+ final Integer uAccCtrlInt = Integer.parseInt(uAccCtrl);
- enabled = ((uAccCtrlInt & AD_ACCOUNT_ENABLED) == AD_ACCOUNT_ENABLED);
+ this.enabled = ((uAccCtrlInt & AD_ACCOUNT_ENABLED) == AD_ACCOUNT_ENABLED);
} else {
// permet de gérer le cas de l'ADAM
- enabled = false;
+ this.enabled = false;
}
- company = (attributes.get("company") == null ? null : (String) attributes.get("company").get());
+ this.company = (attributes.get("company") == null ? null : (String) attributes.get("company").get());
}
public String getGuid() {
- return guid;
+ return this.guid;
}
public String getCompany() {
- return company;
+ return this.company;
}
public String getLogin() {
- return login;
+ return this.login;
}
public String getName() {
- return name;
+ return this.name;
}
public String getFirstName() {
- return firstName;
+ return this.firstName;
}
public List<String> getMemberOf() {
- return memberOf;
+ return this.memberOf;
}
public boolean isEnabled() {
- return enabled;
+ return this.enabled;
}
}
}
// la config pré-2010
private DocumentsModel oldConfig = null;
- public Importer(String[] args) {
+ public Importer(final String[] args) {
super(args);
}
@Override
protected void initialize() throws Exception {
- String _tmp = getProps().getProperty("max.concurrent.threads");
+ String _tmp = this.getProps().getProperty("max.concurrent.threads");
try {
- maxConcurrentThreads = Integer.parseInt(_tmp);
- } catch (Exception e) {
+ this.maxConcurrentThreads = Integer.parseInt(_tmp);
+ } catch (final Exception e) {
logger.error("max.concurrent.threads=" + _tmp + " : nombre invalide");
}
_tmp = null;
- _tmp = getProps().getProperty("pre2010.sur-chiffres.documents-def");
+ _tmp = this.getProps().getProperty("pre2010.sur-chiffres.documents-def");
_tmp = _tmp.replaceAll("%basedir%", BatchRunner.getCanonicalBaseDir());
- pre2010ConfDir = new File(_tmp);
- sectionName = getProps().getProperty("section.pre2010.name");
-
- if (!StringUtils.isEmpty(getProps().getProperty("archive.locator"))) {
- String archiveLocatorClassName = getProps().getProperty("archive.locator");
- Class clazz = Class.forName(archiveLocatorClassName);
- archiveLocator = (ArchiveLocator)clazz.newInstance();
- } else if (!StringUtils.isEmpty(getProps().getProperty("xar.storage"))) {
- String xarStoragePath = getProps().getProperty("xar.storage");
- archiveLocator = new BasicArchiveLocator(xarStoragePath);
- }else {
+ this.pre2010ConfDir = new File(_tmp);
+ this.sectionName = this.getProps().getProperty("section.pre2010.name");
+
+ if (!StringUtils.isEmpty(this.getProps().getProperty("archive.locator"))) {
+ final String archiveLocatorClassName = this.getProps().getProperty("archive.locator");
+ final Class clazz = Class.forName(archiveLocatorClassName);
+ this.archiveLocator = (ArchiveLocator) clazz.newInstance();
+ } else if (!StringUtils.isEmpty(this.getProps().getProperty("xar.storage"))) {
+ final String xarStoragePath = this.getProps().getProperty("xar.storage");
+ this.archiveLocator = new BasicArchiveLocator(xarStoragePath);
+ } else {
throw new Exception("le paramètre 'archive.locator' ou 'xar.storage' doit être renseigné");
}
- archiveLocator.initialize();
-
+ this.archiveLocator.initialize();
+
_tmp = null;
- _tmp = getProps().getProperty("temp.directory");
- if (_tmp == null)
+ _tmp = this.getProps().getProperty("temp.directory");
+ if (_tmp == null) {
_tmp = System.getProperty("java.io.tmpdir");
- rootTempDirectory = new File(_tmp);
+ }
+ this.rootTempDirectory = new File(_tmp);
_tmp = null;
- _tmp = getProps().getProperty("rules.directory");
+ _tmp = this.getProps().getProperty("rules.directory");
_tmp = _tmp.replaceAll("%basedir%", BatchRunner.getCanonicalBaseDir());
- rulesDirectory = new File(_tmp);
- LocalLoader ll = new LocalLoader();
- oldConfig = ll.loadConfigFromFiles(pre2010ConfDir.getAbsolutePath());
- if ("true".equals(getProps().getProperty("drop.log.files.after.import")))
- dropLogFilesAfterImport = true;
+ this.rulesDirectory = new File(_tmp);
+ final LocalLoader ll = new LocalLoader();
+ this.oldConfig = ll.loadConfigFromFiles(this.pre2010ConfDir.getAbsolutePath());
+ if ("true".equals(this.getProps().getProperty("drop.log.files.after.import"))) {
+ this.dropLogFilesAfterImport = true;
+ }
}
- public static boolean doesOpennedTaskExistsForArchive(String archiveName, String exercice) throws SQLException {
+ public static boolean doesOpennedTaskExistsForArchive(final String archiveName, final String exercice) throws SQLException {
boolean ret = false;
Connection con = null;
try {
con = PoolManager.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("SELECT IMPORT_ID FROM IMPORT_JOBS WHERE ARCHIVE_NAME=? AND EXERCICE=? AND IMPORT_STATUS IN (?,?)");
+ final PreparedStatement ps = con.prepareStatement("SELECT IMPORT_ID FROM IMPORT_JOBS WHERE ARCHIVE_NAME=? AND EXERCICE=? AND IMPORT_STATUS IN (?,?)");
int i = 1;
ps.setString(i++, archiveName);
ps.setString(i++, exercice);
ps.setInt(i++, IMPORT_JOB_STATUS_ASKED);
ps.setInt(i++, IMPORT_JOB_STATUS_RUNNING);
- ResultSet rs = ps.executeQuery();
+ final ResultSet rs = ps.executeQuery();
ret = rs.next();
rs.close();
ps.close();
- } catch (SQLException ex) {
+ } catch (final SQLException ex) {
logger.error("doesOpennedTaskExistsForArchive(String)", ex);
throw ex;
} finally {
- if (con != null)
+ if (con != null) {
PoolManager.getInstance().releaseConnection(con);
+ }
}
return ret;
}
- public static void cancelTasksForArchive(String archiveName) throws SQLException {
+ public static void cancelTasksForArchive(final String archiveName) throws SQLException {
Connection con = null;
try {
con = PoolManager.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("UPDATE IMPORT_JOBS SET IMPORT_STATUS=? WHERE ARCHIVE_NAME=? AND IMPORT_STATUS IN (?,?)");
+ final PreparedStatement ps = con.prepareStatement("UPDATE IMPORT_JOBS SET IMPORT_STATUS=? WHERE ARCHIVE_NAME=? AND IMPORT_STATUS IN (?,?)");
ps.setInt(1, IMPORT_JOB_STATUS_CANCELED);
ps.setString(2, archiveName);
ps.setInt(3, IMPORT_JOB_STATUS_ASKED);
ps.setInt(4, IMPORT_JOB_STATUS_RUNNING);
ps.executeUpdate();
ps.close();
- } catch (SQLException ex) {
+ } catch (final SQLException ex) {
logger.error("cancelTasksForArchive(String)", ex);
throw ex;
} finally {
- if (con != null)
+ if (con != null) {
PoolManager.getInstance().releaseConnection(con);
+ }
}
}
- public static void createImportJob(String archiveName, String exercice, String importScheme, String importUser) throws SQLException {
+ public static void createImportJob(final String archiveName, final String exercice, final String importScheme, final String importUser) throws SQLException {
Connection con = null;
try {
con = PoolManager.getInstance().getConnection();
if (con.getMetaData().getURL().contains(":mysql:")) {
- PreparedStatement ps = con.prepareStatement("INSERT INTO IMPORT_JOBS (ARCHIVE_NAME, EXERCICE, IMPORT_SCHEME, USER_LOGIN, IMPORT_STATUS,DATE_MESSAGE, DATE_RAPPEL, DATE_CONFIRM) VALUES (?,?,?,?,?,NULL,NULL,NULL)");
+ final PreparedStatement ps = con.prepareStatement("INSERT INTO IMPORT_JOBS (ARCHIVE_NAME, EXERCICE, IMPORT_SCHEME, USER_LOGIN, IMPORT_STATUS,DATE_MESSAGE, DATE_RAPPEL, DATE_CONFIRM) VALUES (?,?,?,?,?,NULL,NULL,NULL)");
int i = 1;
ps.setString(i++, archiveName);
ps.setString(i++, exercice);
ps.close();
} else if (con.getMetaData().getURL().contains(":oracle:")) {
long id = 0;
- ResultSet rs = con.createStatement().executeQuery("SELECT SQ_IMPORT_JOBS.NEXTVAL FROM DUAL");
- if (rs.next())
+ final ResultSet rs = con.createStatement().executeQuery("SELECT SQ_IMPORT_JOBS.NEXTVAL FROM DUAL");
+ if (rs.next()) {
id = rs.getLong(1);
- else
+ } else {
id = 1;
- PreparedStatement ps = con.prepareStatement("INSERT INTO IMPORT_JOBS (IMPORT_ID, ARCHIVE_NAME, EXERCICE, IMPORT_SCHEME, USER_LOGIN, IMPORT_STATUS) VALUES (?,?,?,?,?,?)");
+ }
+ final PreparedStatement ps = con.prepareStatement("INSERT INTO IMPORT_JOBS (IMPORT_ID, ARCHIVE_NAME, EXERCICE, IMPORT_SCHEME, USER_LOGIN, IMPORT_STATUS) VALUES (?,?,?,?,?,?)");
int i = 1;
ps.setLong(i++, id);
ps.setString(i++, archiveName);
ps.executeUpdate();
ps.close();
}
- } catch (SQLException ex) {
+ } catch (final SQLException ex) {
logger.error("createImportJob(String,String,String)", ex);
throw ex;
} finally {
- if (con != null)
+ if (con != null) {
PoolManager.getInstance().releaseConnection(con);
+ }
}
}
// super.beforeExecute(t, r);
// }
// };
- ExecutorService pool = Executors.newFixedThreadPool(maxConcurrentThreads);
- if (checkNotRunning()) {
+ final ExecutorService pool = Executors.newFixedThreadPool(this.maxConcurrentThreads);
+ if (this.checkNotRunning()) {
Connection con = null;
try {
con = PoolManager.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("select IMPORT_ID, ARCHIVE_NAME, EXERCICE, USER_LOGIN, IMPORT_SCHEME FROM IMPORT_JOBS WHERE IMPORT_STATUS=? ORDER BY DATE_DEMANDE ASC");
+ final PreparedStatement ps = con.prepareStatement("select IMPORT_ID, ARCHIVE_NAME, EXERCICE, USER_LOGIN, IMPORT_SCHEME FROM IMPORT_JOBS WHERE IMPORT_STATUS=? ORDER BY DATE_DEMANDE ASC");
ps.setInt(1, IMPORT_JOB_STATUS_ASKED);
- ResultSet rs = ps.executeQuery();
+ final ResultSet rs = ps.executeQuery();
int count = 0;
- while (rs.next() && count < (maxConcurrentThreads * 3)) {
+ while (rs.next() && count < (this.maxConcurrentThreads * 3)) {
count++;
int i = 1;
- long importId = rs.getLong(i++);
- String archiveName = rs.getString(i++);
- String exercice = rs.getString(i++);
- String user = rs.getString(i++);
- String scheme = rs.getString(i++);
- ImporterImpl ii = new ImporterImpl(importId, archiveName, exercice, user, scheme);
+ final long importId = rs.getLong(i++);
+ final String archiveName = rs.getString(i++);
+ final String exercice = rs.getString(i++);
+ final String user = rs.getString(i++);
+ final String scheme = rs.getString(i++);
+ final ImporterImpl ii = new ImporterImpl(importId, archiveName, exercice, user, scheme);
logger.debug("submitting importId=" + importId + " for " + archiveName);
pool.submit(ii);
}
rs.close();
pool.shutdown();
pool.awaitTermination(100, TimeUnit.DAYS);
- oldConfig.prepareForUnload();
- oldConfig = null;
- } catch (Exception ex) {
+ this.oldConfig.prepareForUnload();
+ this.oldConfig = null;
+ } catch (final Exception ex) {
logger.error("doProcess()", ex);
} finally {
- if (con != null)
+ if (con != null) {
PoolManager.getInstance().releaseConnection(con);
- clearLock();
+ }
+ this.clearLock();
}
// } else {
// clearLock();
Traitement traitement;
long importId;
String archiveName;
- String exercice;
String user;
String scheme;
- public ImporterImpl(long importId, String archiveName, String exercice, String user, String scheme) {
+ public ImporterImpl(final long importId, final String archiveName, final String exercice, final String user, final String scheme) {
super();
this.traitement = new Traitement(Importer.class, BATCH_NAME + "<" + archiveName + ">");
this.importId = importId;
this.archiveName = archiveName;
- this.exercice = exercice;
this.user = user;
this.scheme = scheme;
}
con = PoolManager.getInstance().getConnection();
PreparedStatement ps = con.prepareStatement("UPDATE IMPORT_JOBS SET IMPORT_STATUS=? WHERE IMPORT_ID=?");
ps.setInt(1, IMPORT_JOB_STATUS_RUNNING);
- ps.setLong(2, importId);
+ ps.setLong(2, this.importId);
ps.executeUpdate();
ps.close();
// on rend la connection le temps de l'import
PoolManager.getInstance().releaseConnection(con);
con = null;
-
+
// on prend toujours le premier volume
- String archiveFirstVolume = FilenameUtils.getBaseName(archiveName) + "_1." + FilenameUtils.getExtension(archiveName);
- File archiveFile = archiveLocator.getArchiveLocation(archiveFirstVolume);
-
+ final String archiveFirstVolume = FilenameUtils.getBaseName(this.archiveName) + "_1." + FilenameUtils.getExtension(this.archiveName);
+ File archiveFile = Importer.this.archiveLocator.getArchiveLocation(archiveFirstVolume);
+
// cas pourri des vieux EDMN
if (!archiveFile.exists()) {
- archiveFile = archiveLocator.getArchiveLocation(archiveName);
-
+ archiveFile = Importer.this.archiveLocator.getArchiveLocation(this.archiveName);
+
if (!archiveFile.exists()) {
- String archiveFolder = archiveLocator.getFolder().getAbsolutePath();
-
- logger.warn("Fichier non trouvé : ni " + archiveFirstVolume + " ni " + archiveName + " n'ont été trouvés dans " + archiveFolder);
- traitement.addMessage(Traitement.SEVERITY_ERROR, "Fichier non trouvé : ni " + archiveFirstVolume + " ni " + archiveName + " n'ont été trouvés dans " + archiveFolder);
+ final String archiveFolder = Importer.this.archiveLocator.getFolder().getAbsolutePath();
+
+ logger.warn("Fichier non trouvé : ni " + archiveFirstVolume + " ni " + this.archiveName + " n'ont été trouvés dans " + archiveFolder);
+ this.traitement.addMessage(Traitement.SEVERITY_ERROR, "Fichier non trouvé : ni " + archiveFirstVolume + " ni " + this.archiveName + " n'ont été trouvés dans " + archiveFolder);
return;
}
}
-
- DocumentsModel dm = BatchRunner.getInstance().getDocuments();
- ImportServiceProvider isp = new ImportServiceBatchProvider(null, null, null);
- RulesParser rp = new RulesParser(FactoryProvider.getSaxParserFactory());
- File rulesFile = new File(rulesDirectory, scheme);
+
+ final DocumentsModel dm = BatchRunner.getInstance().getDocuments();
+ final ImportServiceProvider isp = new ImportServiceBatchProvider(null, null, null);
+ final RulesParser rp = new RulesParser(FactoryProvider.getSaxParserFactory());
+ final File rulesFile = new File(Importer.this.rulesDirectory, this.scheme);
if (!rulesFile.exists()) {
- logger.error("Le fichier de règle " + scheme + " n'existe pas dans " + rulesDirectory);
- throw new FileNotFoundException("Le fichier de règle " + scheme + " n'existe pas dans " + rulesDirectory);
+ logger.error("Le fichier de règle " + this.scheme + " n'existe pas dans " + Importer.this.rulesDirectory);
+ throw new FileNotFoundException("Le fichier de règle " + this.scheme + " n'existe pas dans " + Importer.this.rulesDirectory);
}
rp.parse(rulesFile);
final RulesModel rules = (RulesModel) rp.getMarshallable();
- ArchiveImporter ai = new ArchiveImporter(dm, archiveFile, isp, getApplicationConfiguration(), rules) {
+ final ArchiveImporter ai = new ArchiveImporter(dm, archiveFile, isp, Importer.this.getApplicationConfiguration(), rules) {
// on vient ici modifier la config de documents, au besoin
@Override
- public void preImport(SectionModel section, Document archiveManifeste) {
- String exercice = archiveManifeste.getRootElement().getAttributeValue("exercice");
- String idColl = archiveManifeste.getRootElement().getAttributeValue("cgIdCol");
- if (exercice == null || idColl == null)
+ public void preImport(final SectionModel section, final Document archiveManifeste) {
+ final String exercice = archiveManifeste.getRootElement().getAttributeValue("exercice");
+ final String idColl = archiveManifeste.getRootElement().getAttributeValue("cgIdCol");
+ if (exercice == null || idColl == null) {
return;
+ }
logger.debug("preImport: -exercice=" + exercice + " -idColl=" + idColl);
if (idColl.length() > 7) {
// on est à peu près certain que ce sera du SPL
// PRE : code degueux pour dépanage (l'import complet provient d'une demande web)
logger.debug("RulesModel : " + rules.getId());
- if (Integer.parseInt(exercice) < 2011 && !"import-full.rul.xml".equals(scheme)) {
+ if (Integer.parseInt(exercice) < 2011 && !"import-full.rul.xml".equals(ImporterImpl.this.scheme)) {
// pré-2010
- if (section.getName().equals(sectionName)) {
+ if (section.getName().equals(Importer.this.sectionName)) {
// alors là, il faut changer de config de
// docs
logger.debug("oldConfig set");
- setDocumentsModel(oldConfig);
+ this.setDocumentsModel(Importer.this.oldConfig);
}
}
}
}
};
- ai.setUser(new LocalUser(user));
- File tempDir = new File(rootTempDirectory, FilenameUtils.getBaseName(archiveName));
+ ai.setUser(new LocalUser(this.user));
+ final File tempDir = new File(Importer.this.rootTempDirectory, FilenameUtils.getBaseName(this.archiveName));
tempDir.mkdirs();
ai.setLocalTempDir(tempDir);
- MDC.put(MultiThreadAppender.MDC_THREAD_KEY, FilenameUtils.getBaseName(archiveName));
+ MDC.put(MultiThreadAppender.MDC_THREAD_KEY, FilenameUtils.getBaseName(this.archiveName));
// on indique qu'il faut splitter les logs
logger.fatal(MultiThreadAppender.START_SPLIT_MSG);
- traitement.addMessage(Traitement.SEVERITY_INFO, "Début de l'import");
- Errors errors = ai.doImport();
+ this.traitement.addMessage(Traitement.SEVERITY_INFO, "Début de l'import");
+ final Errors errors = ai.doImport();
FileUtils.forceDelete(tempDir);
// on cloture le splitt des logs
logger.fatal(MultiThreadAppender.END_SPLIT_MSG);
// on reprend une connection pour la suite
- if (con == null)
+ if (con == null) {
con = PoolManager.getInstance().getConnection();
+ }
if (errors.containsWarning()) {
- StringBuilder sb = new StringBuilder();
- for (Errors.Error error : errors.getErrors()) {
- if (error.getSeverity() >= Errors.SEVERITY_WARNING)
+ final StringBuilder sb = new StringBuilder();
+ for (final Errors.Error error : errors.getErrors()) {
+ if (error.getSeverity() >= Errors.SEVERITY_WARNING) {
sb.append(error.getMessage()).append("\n");
+ }
}
- if (errors.containsError())
- traitement.addMessage(Traitement.SEVERITY_ERROR, sb.toString());
- else
- traitement.addMessage(Traitement.SEVERITY_WARN, sb.toString());
- if (errors.containsError())
- traitement.setResume("Import échoué");
- else
- traitement.setResume("Avertissements pendant l'import");
- traitement.setEnd(System.currentTimeMillis());
+ if (errors.containsError()) {
+ this.traitement.addMessage(Traitement.SEVERITY_ERROR, sb.toString());
+ } else {
+ this.traitement.addMessage(Traitement.SEVERITY_WARN, sb.toString());
+ }
+ if (errors.containsError()) {
+ this.traitement.setResume("Import échoué");
+ } else {
+ this.traitement.setResume("Avertissements pendant l'import");
+ }
+ this.traitement.setEnd(System.currentTimeMillis());
ps = con.prepareStatement("UPDATE IMPORT_JOBS SET IMPORT_STATUS=? WHERE IMPORT_ID=?");
- if (errors.containsError())
+ if (errors.containsError()) {
ps.setInt(1, IMPORT_JOB_STATUS_ERROR);
- else
+ } else {
ps.setInt(1, IMPORT_JOB_STATUS_TERMINATED);
- ps.setLong(2, importId);
+ }
+ ps.setLong(2, this.importId);
ps.executeUpdate();
ps.close();
} else {
ps = con.prepareStatement("UPDATE IMPORT_JOBS SET IMPORT_STATUS=? WHERE IMPORT_ID=?");
ps.setInt(1, IMPORT_JOB_STATUS_TERMINATED);
- ps.setLong(2, importId);
+ ps.setLong(2, this.importId);
ps.executeUpdate();
ps.close();
- traitement.setEnd(System.currentTimeMillis());
- traitement.setResume("Import terminé");
+ this.traitement.setEnd(System.currentTimeMillis());
+ this.traitement.setResume("Import terminé");
}
- File logFile = MultiThreadAppender.getFile(MDC.get(MultiThreadAppender.MDC_THREAD_KEY));
+ final File logFile = MultiThreadAppender.getFile(MDC.get(MultiThreadAppender.MDC_THREAD_KEY));
if (logFile != null) {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- GZIPOutputStream gz = new GZIPOutputStream(baos);
- FileInputStream fis = new FileInputStream(logFile);
- byte[] buff = new byte[2048];
+ final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ final GZIPOutputStream gz = new GZIPOutputStream(baos);
+ final FileInputStream fis = new FileInputStream(logFile);
+ final byte[] buff = new byte[2048];
int read = 0;
while ((read = fis.read(buff)) > 0) {
gz.write(buff, 0, read);
fis.close();
gz.flush();
gz.close();
- traitement.setBinaryData(baos.toByteArray());
- if (dropLogFilesAfterImport)
+ this.traitement.setBinaryData(baos.toByteArray());
+ if (Importer.this.dropLogFilesAfterImport) {
FileUtils.forceDelete(logFile);
+ }
}
MDC.clear();
- } catch (Exception ex) {
- Message msg = traitement.addMessage(Traitement.SEVERITY_ERROR, ex.getMessage());
+ } catch (final Exception ex) {
+ final Message msg = this.traitement.addMessage(Traitement.SEVERITY_ERROR, ex.getMessage());
if (con != null) {
try {
- PreparedStatement ps = con.prepareStatement("UPDATE IMPORT_JOBS SET IMPORT_STATUS=? WHERE IMPORT_ID=?");
+ final PreparedStatement ps = con.prepareStatement("UPDATE IMPORT_JOBS SET IMPORT_STATUS=? WHERE IMPORT_ID=?");
ps.setInt(1, IMPORT_JOB_STATUS_ERROR);
- ps.setLong(2, importId);
+ ps.setLong(2, this.importId);
ps.executeUpdate();
ps.close();
- } catch (SQLException sqlEx) {
+ } catch (final SQLException sqlEx) {
logger.error("setting status to error", ex);
}
}
} finally {
- if (con != null)
+ if (con != null) {
PoolManager.getInstance().releaseConnection(con);
+ }
}
}
}
private final static class LocalUser implements XemeliosUser {
- private String userId;
+ private final String userId;
- public LocalUser(String userId) {
+ public LocalUser(final String userId) {
super();
this.userId = userId;
}
@Override
public String getId() {
- return userId;
+ return this.userId;
}
@Override
public String getDisplayName() {
- return userId;
+ return this.userId;
}
@Override
- public boolean hasRole(String role) {
+ public boolean hasRole(final String role) {
return true;
}
@Override
- public boolean hasDocument(String document) {
+ public boolean hasDocument(final String document) {
return true;
}
@Override
- public boolean hasCollectivite(String collectivite, DocumentModel dm) {
+ public boolean hasCollectivite(final String collectivite, final DocumentModel dm) {
return true;
}
}
super();
}
- public DocumentsModel loadConfigFromFiles(String repertoires) throws SAXException, ParserConfigurationException, IOException {
+ public DocumentsModel loadConfigFromFiles(final String repertoires) throws SAXException, ParserConfigurationException, IOException {
return __loadConfigFromFiles(repertoires);
}
}
package fr.gouv.finances.dgfip.xemelios.batch.chaineimport.mailer;
-import fr.gouv.finances.dgfip.xemelios.batch.Batch;
-import fr.gouv.finances.dgfip.xemelios.batch.BatchRunner;
-import fr.gouv.finances.dgfip.xemelios.batch.chaineimport.importeur.Importer;
-import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
-import java.nio.CharBuffer;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
-import java.util.Enumeration;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
+
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.log4j.Logger;
+import fr.gouv.finances.dgfip.xemelios.batch.Batch;
+import fr.gouv.finances.dgfip.xemelios.batch.BatchRunner;
+import fr.gouv.finances.dgfip.xemelios.batch.chaineimport.importeur.Importer;
+import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
+
/**
- *
+ *
* @author cmarchand
*/
public class Mailer extends Batch {
- private static final Logger logger = Logger.getLogger(Mailer.class);
- public static final String BATCH_NAME = "mailer";
- public static final int DEFAULT_OUVERTURE = 365;
- public static final int DEFAULT_RAPPEL = 31;
- public static final String PARAM_PERIODE_OUVERTURE = "periode.ouverture";
- public static final String PARAM_PERIODE_RAPPEL = "periode.rappel";
- public static final String PARAM_EMAIL_SUBJECT = "email.subject";
- public static final String PARAM_MAIL_FERMETURE = "mail.fermeture";
- public static final String PARAM_MAIL_RAPPEL = "mail.rappel";
- public static final String PARAM_MAIL_DECHARGEMENT = "mail.dechargement";
- public static final String PARAM_SMTP_SERVER = "smtp.server";
- public static final String PARAM_SMTP_PORT = "smtp.port";
- public static final String PARAM_MAIL_FROM = "mail.from";
- public static final String PARAM_BOUNCE_ADDRESS = "bounce.address";
- public static final String PARAM_SMTP_LOGIN = "smtp.login";
- public static final String PARAM_SMTP_PASSWORD = "smtp.password";
- public static final String PARAM_IMPORT_FULL = "full.import.scheme";
- public static final String PARAM_IMPORT_PURGE = "purge.import.scheme";
- public static final String PARAM_NO_MAIL_USERS = "no.mail.users";
- public static final String PARAM_SMTP_TLS = "smtp.tls";
- private int periodeOuverture = DEFAULT_OUVERTURE;
- private int periodeRappel = DEFAULT_RAPPEL;
- private String emailSubject;
- private String mailFermeture;
- private String mailRappel;
- private String mailDechargement;
- private String smtpServer;
- private int smtpPort = 25;
- private String mailFrom;
- private String bounceAddress;
- private String smtpUser;
- private String smtpPassword;
- private String fullImportScheme;
- private String purgeImportScheme;
- private HashSet<String> avoidMailUsers;
- private boolean tls = false;
- private ExecutorService pool;
+ private static final Logger logger = Logger.getLogger(Mailer.class);
+ public static final String BATCH_NAME = "mailer";
+ public static final int DEFAULT_OUVERTURE = 365;
+ public static final int DEFAULT_RAPPEL = 31;
+ public static final String PARAM_PERIODE_OUVERTURE = "periode.ouverture";
+ public static final String PARAM_PERIODE_RAPPEL = "periode.rappel";
+ public static final String PARAM_EMAIL_SUBJECT = "email.subject";
+ public static final String PARAM_MAIL_FERMETURE = "mail.fermeture";
+ public static final String PARAM_MAIL_RAPPEL = "mail.rappel";
+ public static final String PARAM_MAIL_DECHARGEMENT = "mail.dechargement";
+ public static final String PARAM_SMTP_SERVER = "smtp.server";
+ public static final String PARAM_SMTP_PORT = "smtp.port";
+ public static final String PARAM_MAIL_FROM = "mail.from";
+ public static final String PARAM_BOUNCE_ADDRESS = "bounce.address";
+ public static final String PARAM_SMTP_LOGIN = "smtp.login";
+ public static final String PARAM_SMTP_PASSWORD = "smtp.password";
+ public static final String PARAM_IMPORT_FULL = "full.import.scheme";
+ public static final String PARAM_IMPORT_PURGE = "purge.import.scheme";
+ public static final String PARAM_NO_MAIL_USERS = "no.mail.users";
+ public static final String PARAM_SMTP_TLS = "smtp.tls";
+ private int periodeOuverture = DEFAULT_OUVERTURE;
+ private int periodeRappel = DEFAULT_RAPPEL;
+ private String emailSubject;
+ private String mailFermeture;
+ private String mailRappel;
+ private String mailDechargement;
+ private String smtpServer;
+ private int smtpPort = 25;
+ private String mailFrom;
+ private String bounceAddress;
+ private String smtpUser;
+ private String smtpPassword;
+ private String fullImportScheme;
+ private String purgeImportScheme;
+ private final HashSet<String> avoidMailUsers;
+ private boolean tls = false;
+ private ExecutorService pool;
+
+ private final Hashtable<String, StringBuilder> models;
+
+ public Mailer(final String[] args) {
+ super(args);
+ this.models = new Hashtable<String, StringBuilder>();
+ this.avoidMailUsers = new HashSet<String>();
+ }
- private Hashtable<String,StringBuilder> models;
+ @Override
+ protected void initialize() throws Exception {
+ super.initialize();
+ String sTmp = this.getProps().getProperty(PARAM_PERIODE_OUVERTURE);
+ try {
+ this.periodeOuverture = Integer.parseInt(sTmp);
+ } catch (final Exception ex) {
+ }
+ sTmp = this.getProps().getProperty(PARAM_PERIODE_RAPPEL);
+ try {
+ this.periodeRappel = Integer.parseInt(sTmp);
+ } catch (final Exception ex) {
+ }
+ this.emailSubject = this.getProps().getProperty(PARAM_EMAIL_SUBJECT);
+ this.mailFermeture = this.getProps().getProperty(PARAM_MAIL_FERMETURE);
+ this.mailRappel = this.getProps().getProperty(PARAM_MAIL_RAPPEL);
+ this.mailDechargement = this.getProps().getProperty(PARAM_MAIL_DECHARGEMENT);
+ this.smtpServer = this.getProps().getProperty(PARAM_SMTP_SERVER);
+ sTmp = this.getProps().getProperty(PARAM_SMTP_PORT);
+ try {
+ this.smtpPort = Integer.parseInt(sTmp);
+ } catch (final Exception ex) {
+ }
+ this.mailFrom = this.getProps().getProperty(PARAM_MAIL_FROM);
+ this.bounceAddress = this.getProps().getProperty(PARAM_BOUNCE_ADDRESS);
+ this.smtpUser = this.getProps().getProperty(PARAM_SMTP_LOGIN);
+ this.smtpPassword = this.getProps().getProperty(PARAM_SMTP_PASSWORD);
+ this.fullImportScheme = this.getProps().getProperty(PARAM_IMPORT_FULL);
+ this.purgeImportScheme = this.getProps().getProperty(PARAM_IMPORT_PURGE);
+ sTmp = this.getProps().getProperty(PARAM_NO_MAIL_USERS);
+ if (sTmp != null) {
+ final String[] users = sTmp.split(",");
+ for (final String user : users) {
+ this.avoidMailUsers.add(user.trim());
+ }
+ }
+ sTmp = this.getProps().getProperty(PARAM_SMTP_TLS);
+ if (sTmp != null) {
+ try {
+ this.tls = Boolean.valueOf(sTmp);
+ } catch (final Exception ex) {
+ }
+ }
+ if (this.smtpServer == null) {
+ throw new java.lang.IllegalArgumentException("Le paramètre " + PARAM_SMTP_SERVER + " est obligatoire");
+ }
+ if (this.mailFrom == null) {
+ throw new IllegalArgumentException("Le paramètre " + PARAM_MAIL_FROM + " est obligatoire");
+ }
+ if (this.emailSubject == null) {
+ throw new IllegalArgumentException("Le paramètre " + PARAM_EMAIL_SUBJECT + " est obligatoire");
+ }
+ if (this.mailFermeture == null) {
+ throw new IllegalArgumentException("Le paramètre " + PARAM_MAIL_FERMETURE + " est obligatoire");
+ }
+ if (this.mailRappel == null) {
+ throw new IllegalArgumentException("Le paramètre " + PARAM_MAIL_RAPPEL + " est obligatoire");
+ }
+ if (this.mailDechargement == null) {
+ throw new IllegalArgumentException("Le paramètre " + PARAM_MAIL_DECHARGEMENT + " est obligatoire");
+ }
+ if (this.fullImportScheme == null) {
+ throw new IllegalArgumentException("Le paramètre " + PARAM_IMPORT_FULL + " est obligatoire");
+ }
+ if (this.purgeImportScheme == null) {
+ throw new IllegalArgumentException("Le paramètre " + PARAM_IMPORT_PURGE + " est obligatoire");
+ }
+ }
- public Mailer(String[] args) {
- super(args);
- models = new Hashtable<String, StringBuilder>();
- avoidMailUsers = new HashSet<String>();
- }
+ @Override
+ protected void doProcess() throws Exception {
+ Connection con = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ this.pool = Executors.newSingleThreadExecutor();
+ // select
+ // I1.import_id
+ // from
+ // IMPORT_JOBS I1
+ // where
+ // I1.DATE_MESSAGE IS NULL
+ // AND I1.DATE_DEMANDE<?
+ // AND I1.IMPORT_SCHEME=?
+ // and not exists(
+ // select null
+ // from IMPORT_JOBS I2
+ // where
+ // I2.ARCHIVE_NAME=I1.ARCHIVE_NAME
+ // AND I2.DATE_DEMANDE>I1.DATE_DEMANDE
+ // AND I2.IMPORT_SCHEME=?
+ // )
- @Override
- protected void initialize() throws Exception {
- super.initialize();
- String sTmp = getProps().getProperty(PARAM_PERIODE_OUVERTURE);
- try { periodeOuverture = Integer.parseInt(sTmp); } catch(Exception ex) {}
- sTmp = getProps().getProperty(PARAM_PERIODE_RAPPEL);
- try { periodeRappel = Integer.parseInt(sTmp); } catch(Exception ex) {}
- emailSubject = getProps().getProperty(PARAM_EMAIL_SUBJECT);
- mailFermeture = getProps().getProperty(PARAM_MAIL_FERMETURE);
- mailRappel = getProps().getProperty(PARAM_MAIL_RAPPEL);
- mailDechargement = getProps().getProperty(PARAM_MAIL_DECHARGEMENT);
- smtpServer = getProps().getProperty(PARAM_SMTP_SERVER);
- sTmp = getProps().getProperty(PARAM_SMTP_PORT);
- try { smtpPort = Integer.parseInt(sTmp); } catch(Exception ex) {}
- mailFrom = getProps().getProperty(PARAM_MAIL_FROM);
- bounceAddress = getProps().getProperty(PARAM_BOUNCE_ADDRESS);
- smtpUser = getProps().getProperty(PARAM_SMTP_LOGIN);
- smtpPassword = getProps().getProperty(PARAM_SMTP_PASSWORD);
- fullImportScheme = getProps().getProperty(PARAM_IMPORT_FULL);
- purgeImportScheme = getProps().getProperty(PARAM_IMPORT_PURGE);
- sTmp = getProps().getProperty(PARAM_NO_MAIL_USERS);
- if(sTmp!=null) {
- String[] users = sTmp.split(",");
- for(String user:users)
- avoidMailUsers.add(user.trim());
- }
- sTmp = getProps().getProperty(PARAM_SMTP_TLS);
- if(sTmp!=null)
- try {
- tls = Boolean.valueOf(sTmp);
- } catch(Exception ex) {}
- if(smtpServer==null) throw new java.lang.IllegalArgumentException("Le paramètre "+PARAM_SMTP_SERVER+" est obligatoire");
- if(mailFrom==null) throw new IllegalArgumentException("Le paramètre "+PARAM_MAIL_FROM+" est obligatoire");
- if(emailSubject==null) throw new IllegalArgumentException("Le paramètre "+PARAM_EMAIL_SUBJECT+" est obligatoire");
- if(mailFermeture==null) throw new IllegalArgumentException("Le paramètre "+PARAM_MAIL_FERMETURE+" est obligatoire");
- if(mailRappel==null) throw new IllegalArgumentException("Le paramètre "+PARAM_MAIL_RAPPEL+" est obligatoire");
- if(mailDechargement==null) throw new IllegalArgumentException("Le paramètre "+PARAM_MAIL_DECHARGEMENT+" est obligatoire");
- if(fullImportScheme==null) throw new IllegalArgumentException("Le paramètre "+PARAM_IMPORT_FULL+" est obligatoire");
- if(purgeImportScheme==null) throw new IllegalArgumentException("Le paramètre "+PARAM_IMPORT_PURGE+" est obligatoire");
- }
-
- @Override
- protected void doProcess() throws Exception {
- Connection con = null;
- try {
- con = PoolManager.getInstance().getConnection();
- pool = Executors.newSingleThreadExecutor();
-//select
-// I1.import_id
-//from
-// IMPORT_JOBS I1
-//where
-// I1.DATE_MESSAGE IS NULL
-// AND I1.DATE_DEMANDE<?
-// AND I1.IMPORT_SCHEME=?
-// and not exists(
-// select null
-// from IMPORT_JOBS I2
-// where
-// I2.ARCHIVE_NAME=I1.ARCHIVE_NAME
-// AND I2.DATE_DEMANDE>I1.DATE_DEMANDE
-// AND I2.IMPORT_SCHEME=?
-//)
+ PreparedStatement ps = con
+ .prepareStatement("select I1.import_id from IMPORT_JOBS I1 where I1.DATE_MESSAGE IS NULL AND I1.DATE_DEMANDE<? AND I1.IMPORT_SCHEME=? and not exists(select null from IMPORT_JOBS I2 where I2.ARCHIVE_NAME=I1.ARCHIVE_NAME AND I2.DATE_DEMANDE>I1.DATE_DEMANDE AND I2.IMPORT_SCHEME=?)");
+ final Date now = new Date();
+ final Calendar cal = new GregorianCalendar();
+ cal.setTime(now);
+ cal.add(Calendar.DAY_OF_MONTH, -this.periodeOuverture);
+ ps.setDate(1, new java.sql.Date(cal.getTime().getTime()));
+ ps.setString(2, this.fullImportScheme);
+ ps.setString(3, this.purgeImportScheme);
+ ResultSet rs = ps.executeQuery();
+ while (rs.next()) {
+ final MailerImpl mi = new MailerImpl(rs.getLong(1), ACTION.FERMETURE);
+ this.pool.submit(mi);
+ }
+ // les rappels
+ ps = con.prepareStatement("select I1.import_id from IMPORT_JOBS I1 where I1.DATE_MESSAGE<? AND I1.DATE_RAPPEL IS NULL AND I1.IMPORT_SCHEME=? and not exists(select null from IMPORT_JOBS I2 where I2.ARCHIVE_NAME=I1.ARCHIVE_NAME AND I2.DATE_DEMANDE>I1.DATE_DEMANDE AND I2.IMPORT_SCHEME=?)");
+ final Calendar message = new GregorianCalendar();
+ message.setTime(now);
+ message.add(Calendar.DAY_OF_MONTH, -this.periodeRappel);
+ ps.setDate(1, new java.sql.Date(message.getTime().getTime()));
+ ps.setString(2, this.fullImportScheme);
+ ps.setString(3, this.purgeImportScheme);
+ rs = ps.executeQuery();
+ while (rs.next()) {
+ final MailerImpl mi = new MailerImpl(rs.getLong(1), ACTION.RAPPEL);
+ this.pool.submit(mi);
+ }
+ // les purges
+ ps = con.prepareStatement("select IMPORT_ID from IMPORT_JOBS where IMPORT_SCHEME=? and IMPORT_STATUS=? and DATE_CONFIRM IS NULL");
+ ps.setString(1, this.purgeImportScheme);
+ ps.setInt(2, Importer.IMPORT_JOB_STATUS_TERMINATED);
+ rs = ps.executeQuery();
+ while (rs.next()) {
+ final MailerImpl mi = new MailerImpl(rs.getLong(1), ACTION.DECHARGEMENT);
+ this.pool.submit(mi);
+ }
+ } catch (final Exception ex) {
+ logger.error("doProcess()", ex);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ }
+ }
- PreparedStatement ps = con.prepareStatement("select I1.import_id from IMPORT_JOBS I1 where I1.DATE_MESSAGE IS NULL AND I1.DATE_DEMANDE<? AND I1.IMPORT_SCHEME=? and not exists(select null from IMPORT_JOBS I2 where I2.ARCHIVE_NAME=I1.ARCHIVE_NAME AND I2.DATE_DEMANDE>I1.DATE_DEMANDE AND I2.IMPORT_SCHEME=?)");
- Date now = new Date();
- Calendar cal = new GregorianCalendar();
- cal.setTime(now);
- cal.add(Calendar.DAY_OF_MONTH, -periodeOuverture);
- ps.setDate(1,new java.sql.Date(cal.getTime().getTime()));
- ps.setString(2, fullImportScheme);
- ps.setString(3, purgeImportScheme);
- ResultSet rs = ps.executeQuery();
- while(rs.next()) {
- MailerImpl mi = new MailerImpl(rs.getLong(1), ACTION.FERMETURE);
- pool.submit(mi);
- }
- // les rappels
- ps = con.prepareStatement("select I1.import_id from IMPORT_JOBS I1 where I1.DATE_MESSAGE<? AND I1.DATE_RAPPEL IS NULL AND I1.IMPORT_SCHEME=? and not exists(select null from IMPORT_JOBS I2 where I2.ARCHIVE_NAME=I1.ARCHIVE_NAME AND I2.DATE_DEMANDE>I1.DATE_DEMANDE AND I2.IMPORT_SCHEME=?)");
- Calendar message = new GregorianCalendar();
- message.setTime(now);
- message.add(Calendar.DAY_OF_MONTH, -periodeRappel);
- ps.setDate(1,new java.sql.Date(message.getTime().getTime()));
- ps.setString(2, fullImportScheme);
- ps.setString(3, purgeImportScheme);
- rs = ps.executeQuery();
- while(rs.next()) {
- MailerImpl mi = new MailerImpl(rs.getLong(1), ACTION.RAPPEL);
- pool.submit(mi);
- }
- // les purges
- ps = con.prepareStatement("select IMPORT_ID from IMPORT_JOBS where IMPORT_SCHEME=? and IMPORT_STATUS=? and DATE_CONFIRM IS NULL");
- ps.setString(1, purgeImportScheme);
- ps.setInt(2, Importer.IMPORT_JOB_STATUS_TERMINATED);
- rs = ps.executeQuery();
- while(rs.next()) {
- MailerImpl mi = new MailerImpl(rs.getLong(1), ACTION.DECHARGEMENT);
- pool.submit(mi);
- }
- } catch(Exception ex) {
- logger.error("doProcess()",ex);
- } finally {
- if(con!=null)
- PoolManager.getInstance().releaseConnection(con);
- }
- }
+ @Override
+ public String getResumeTraitement() {
+ return "En cours";
+ }
- @Override
- public String getResumeTraitement() {
- return "En cours";
- }
+ @Override
+ public String typeTraitementRefCode() {
+ return BATCH_NAME;
+ }
- @Override
- public String typeTraitementRefCode() {
- return BATCH_NAME;
- }
+ @Override
+ public String getInformations() {
+ return "";
+ }
- @Override
- public String getInformations() {
- return "";
- }
+ @Override
+ protected String getBatchVersion() {
+ return "1.0";
+ }
- @Override
- protected String getBatchVersion() {
- return "1.0";
- }
+ private class MailerImpl extends RunnableBatch {
+ private final long importId;
+ private final ACTION action;
+ private final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
- private class MailerImpl extends RunnableBatch {
- private long importId;
- private ACTION action;
- private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
+ MailerImpl(final long importId, final ACTION action) {
+ super();
+ this.importId = importId;
+ this.action = action;
+ }
- MailerImpl(long importId, ACTION action) {
- super();
- this.importId = importId;
- this.action = action;
- }
+ @Override
+ public String getInformations() {
+ return "";
+ }
- @Override
- public String getInformations() {
- return "";
- }
+ @Override
+ public void run() {
+ Connection con = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ PreparedStatement ps = con.prepareStatement("SELECT ARCHIVE_NAME,EXERCICE,USER_LOGIN,DATE_DEMANDE,IMPORT_STATUS,DATE_MESSAGE,DATE_RAPPEL,DATE_CONFIRM FROM IMPORT_JOBS WHERE IMPORT_ID=?");
+ ps.setLong(1, this.importId);
+ final ResultSet rs = ps.executeQuery();
+ rs.next();
+ int i = 1;
+ final String archiveName = rs.getString(i++);
+ final String exercice = rs.getString(i++);
+ final String userLogin = rs.getString(i++);
+ final java.sql.Timestamp dateDemande = rs.getTimestamp(i++);
+ final int importStatus = rs.getInt(i++);
+ java.sql.Date dateMessage = rs.getDate(i++);
+ if (rs.wasNull()) {
+ dateMessage = null;
+ }
+ java.sql.Date dateRappel = rs.getDate(i++);
+ if (rs.wasNull()) {
+ dateRappel = null;
+ }
+ java.sql.Date dateConfirm = rs.getDate(i++);
+ if (rs.wasNull()) {
+ dateConfirm = null;
+ }
+ String mailFileName = null;
+ if (!Mailer.this.avoidMailUsers.contains(userLogin)) {
+ switch (this.action) {
+ case FERMETURE: {
+ mailFileName = Mailer.this.mailFermeture;
+ break;
+ }
+ case RAPPEL: {
+ mailFileName = Mailer.this.mailRappel;
+ break;
+ }
+ default:
+ mailFileName = Mailer.this.mailDechargement;
+ }
+ final StringBuilder sb = Mailer.this.getMailModel(mailFileName);
+ stringBuilderReplace(sb, "${ARCHIVE_NAME}", archiveName);
+ stringBuilderReplace(sb, "${EXERCICE}", exercice);
+ stringBuilderReplace(sb, "${USER_LOGIN}", userLogin);
+ stringBuilderReplace(sb, "${DATE_DEMANDE}", this.sdf.format(dateDemande));
+ stringBuilderReplace(sb, "${IMPORT_STATUS}", getImportStatusText(importStatus));
+ stringBuilderReplace(sb, "${DATE_MESSAGE}", dateMessage != null ? this.sdf.format(dateMessage) : "xx/xx/xxxx");
+ stringBuilderReplace(sb, "${DATE_RAPPEL}", dateRappel != null ? this.sdf.format(dateRappel) : "xx/xx/xxxx");
+ stringBuilderReplace(sb, "${DATE_CONFIRM}", dateConfirm != null ? this.sdf.format(dateConfirm) : "xx/xx/xxxx");
+ final HtmlEmail email = new HtmlEmail();
+ email.setHtmlMsg(sb.toString());
+ email.setHostName(Mailer.this.smtpServer);
+ email.setSmtpPort(Mailer.this.smtpPort);
+ if (Mailer.this.tls) {
+ email.setTLS(true);
+ }
+ if (Mailer.this.smtpUser != null && Mailer.this.smtpPassword != null) {
+ email.setAuthentication(Mailer.this.smtpUser, Mailer.this.smtpPassword);
+ }
+ email.setFrom(Mailer.this.mailFrom);
+ email.addTo(userLogin);
+ email.setSubject(Mailer.this.emailSubject);
+ if (Mailer.this.bounceAddress != null) {
+ email.setBounceAddress(Mailer.this.bounceAddress);
+ }
+ try {
+ email.send();
+ } catch (final EmailException emEx) {
+ logger.error("send mail error", emEx);
+ }
+ }
+ String update = "UPDATE IMPORT_JOBS set ";
+ switch (this.action) {
+ case FERMETURE:
+ update = update + "DATE_MESSAGE";
+ break;
+ case RAPPEL:
+ update = update + "DATE_RAPPEL";
+ break;
+ default:
+ update = update + "DATE_COMFIRM";
+ }
+ update = update + "=? WHERE IMPORT_ID=?";
+ ps = con.prepareStatement(update);
+ ps.setDate(1, new java.sql.Date(System.currentTimeMillis()));
+ ps.setLong(2, this.importId);
+ ps.executeUpdate();
+ } catch (final Exception ex) {
+ logger.error("impl<" + this.importId + ">", ex);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ }
+ }
+ }
- @Override
- public void run() {
- Connection con = null;
- try {
- con = PoolManager.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("SELECT ARCHIVE_NAME,EXERCICE,USER_LOGIN,DATE_DEMANDE,IMPORT_STATUS,DATE_MESSAGE,DATE_RAPPEL,DATE_CONFIRM FROM IMPORT_JOBS WHERE IMPORT_ID=?");
- ps.setLong(1,importId);
- ResultSet rs = ps.executeQuery();
- rs.next();
- int i =1;
- String archiveName = rs.getString(i++);
- String exercice = rs.getString(i++);
- String userLogin = rs.getString(i++);
- java.sql.Timestamp dateDemande = rs.getTimestamp(i++);
- int importStatus = rs.getInt(i++);
- java.sql.Date dateMessage = rs.getDate(i++);
- if(rs.wasNull()) dateMessage = null;
- java.sql.Date dateRappel = rs.getDate(i++);
- if(rs.wasNull()) dateRappel = null;
- java.sql.Date dateConfirm = rs.getDate(i++);
- if(rs.wasNull()) dateConfirm = null;
- String mailFileName = null;
- if(!avoidMailUsers.contains(userLogin)) {
- switch(action) {
- case FERMETURE: { mailFileName = mailFermeture; break; }
- case RAPPEL: { mailFileName = mailRappel; break; }
- default : mailFileName = mailDechargement;
- }
- StringBuilder sb = getMailModel(mailFileName);
- stringBuilderReplace(sb, "${ARCHIVE_NAME}", archiveName);
- stringBuilderReplace(sb, "${EXERCICE}", exercice);
- stringBuilderReplace(sb, "${USER_LOGIN}", userLogin);
- stringBuilderReplace(sb, "${DATE_DEMANDE}", sdf.format(dateDemande));
- stringBuilderReplace(sb, "${IMPORT_STATUS}", getImportStatusText(importStatus));
- stringBuilderReplace(sb, "${DATE_MESSAGE}", dateMessage!=null ? sdf.format(dateMessage) : "xx/xx/xxxx");
- stringBuilderReplace(sb, "${DATE_RAPPEL}", dateRappel!=null ? sdf.format(dateRappel) : "xx/xx/xxxx");
- stringBuilderReplace(sb, "${DATE_CONFIRM}", dateConfirm!=null ? sdf.format(dateConfirm) : "xx/xx/xxxx");
- HtmlEmail email = new HtmlEmail();
- email.setHtmlMsg(sb.toString());
- email.setHostName(smtpServer);
- email.setSmtpPort(smtpPort);
- if(tls) email.setTLS(true);
- if(smtpUser!=null && smtpPassword!=null)
- email.setAuthentication(smtpUser, smtpPassword);
- email.setFrom(mailFrom);
- email.addTo(userLogin);
- email.setSubject(emailSubject);
- if(bounceAddress!=null)
- email.setBounceAddress(bounceAddress);
- try {
- email.send();
- } catch(EmailException emEx) {
- logger.error("send mail error",emEx);
- }
- }
- String update = "UPDATE IMPORT_JOBS set ";
- switch(action) {
- case FERMETURE: update=update+"DATE_MESSAGE"; break;
- case RAPPEL: update=update+"DATE_RAPPEL"; break;
- default: update=update+"DATE_COMFIRM";
- }
- update = update+"=? WHERE IMPORT_ID=?";
- ps = con.prepareStatement(update);
- ps.setDate(1,new java.sql.Date(System.currentTimeMillis()));
- ps.setLong(2,importId);
- ps.executeUpdate();
- } catch(Exception ex) {
- logger.error("impl<"+importId+">",ex);
- } finally {
- if(con!=null) PoolManager.getInstance().releaseConnection(con);
- }
- }
- }
+ private StringBuilder getMailModel(final String fileName) {
+ StringBuilder ret = this.models.get(fileName);
+ if (ret == null) {
+ try {
- private StringBuilder getMailModel(String fileName) {
- StringBuilder ret = models.get(fileName);
- if(ret==null) {
- try {
+ final File f = new File(fileName.replace("%basedir%", BatchRunner.getCanonicalBaseDir()));
+ final FileInputStream fis = new FileInputStream(f);
+ final InputStreamReader isr = new InputStreamReader(fis);
+ ret = new StringBuilder();
+ final char[] buff = new char[2048];
+ int read = 0;
+ while ((read = isr.read(buff)) > 0) {
+ ret.append(buff, 0, read);
+ }
+ fis.close();
+ this.models.put(fileName, ret);
+ } catch (final Exception ex) {
+ logger.error("lecture de " + fileName, ex);
+ }
+ }
+ return ret;
+ }
- File f = new File(fileName.replace("%basedir%", BatchRunner.getCanonicalBaseDir()));
- FileInputStream fis = new FileInputStream(f);
- InputStreamReader isr = new InputStreamReader(fis);
- ret = new StringBuilder();
- char[] buff = new char[2048];
- int read = 0;
- while((read=isr.read(buff)) > 0) {
- ret.append(buff, 0, read);
- }
- fis.close();
- models.put(fileName, ret);
- } catch(Exception ex) {
- logger.error("lecture de "+fileName, ex);
- }
- }
- return ret;
- }
+ private enum ACTION {
+ FERMETURE, RAPPEL, DECHARGEMENT
+ };
- private enum ACTION {
- FERMETURE, RAPPEL, DECHARGEMENT
- };
+ public static void stringBuilderReplace(final StringBuilder builder, final String strWhat, final String strBy) {
+ stringBuilderReplace(builder, strWhat, strBy, Integer.MAX_VALUE);
+ }
- public static void stringBuilderReplace(StringBuilder builder, String strWhat, String strBy) {
- stringBuilderReplace(builder, strWhat, strBy, Integer.MAX_VALUE);
- }
+ public static void stringBuilderReplace(final StringBuilder builder, final String strWhat, final String strBy, final int maxOccurences) {
+ int pos = 0, occur = 0;
+ while (occur < maxOccurences && (pos = builder.indexOf(strWhat)) > 0) {
+ builder.replace(pos, pos + strWhat.length(), strBy != null ? strBy : "");
+ occur++;
+ }
+ }
- public static void stringBuilderReplace(StringBuilder builder, String strWhat, String strBy, int maxOccurences) {
- int pos = 0, occur = 0;
- while (occur < maxOccurences && (pos = builder.indexOf(strWhat)) > 0) {
- builder.replace(pos, pos + strWhat.length(), strBy != null ? strBy : "");
- occur++;
- }
- }
- private static String getImportStatusText(int status) {
- switch(status) {
- case Importer.IMPORT_JOB_STATUS_ASKED: return "Demandé";
- case Importer.IMPORT_JOB_STATUS_CANCELED: return "Annulé";
- case Importer.IMPORT_JOB_STATUS_ERROR: return "Erreur";
- case Importer.IMPORT_JOB_STATUS_RUNNING: return "En cours";
- default: return "Terminé";
- }
- }
+ private static String getImportStatusText(final int status) {
+ switch (status) {
+ case Importer.IMPORT_JOB_STATUS_ASKED:
+ return "Demandé";
+ case Importer.IMPORT_JOB_STATUS_CANCELED:
+ return "Annulé";
+ case Importer.IMPORT_JOB_STATUS_ERROR:
+ return "Erreur";
+ case Importer.IMPORT_JOB_STATUS_RUNNING:
+ return "En cours";
+ default:
+ return "Terminé";
+ }
+ }
}
//import java.util.zip.ZipFile;
/**
- * Contrôle l'archive, la déplace là où il faut quand tout est bon, dépose la
- * demande d'import initial.
+ * Contrôle l'archive, la déplace là où il faut quand tout est bon, dépose la demande d'import initial.
*
* @author cmarchand
*/
// le fichier de regle à appliquer à l'import
private String importScheme;
- public Repartitor(String[] args) {
+ public Repartitor(final String[] args) {
super(args);
}
@Override
protected void initialize() throws Exception {
super.initialize();
-
- if (!StringUtils.isEmpty(getProps().getProperty("archive.locator"))) {
- String archiveLocatorClassName = getProps().getProperty("archive.locator");
- Class clazz = Class.forName(archiveLocatorClassName);
- archiveLocator = (ArchiveLocator)clazz.newInstance();
- } else if (!StringUtils.isEmpty(getProps().getProperty("xar.storage"))) {
- String xarStoragePath = getProps().getProperty("xar.storage");
- archiveLocator = new BasicArchiveLocator(xarStoragePath);
- }else {
+
+ if (!StringUtils.isEmpty(this.getProps().getProperty("archive.locator"))) {
+ final String archiveLocatorClassName = this.getProps().getProperty("archive.locator");
+ final Class clazz = Class.forName(archiveLocatorClassName);
+ this.archiveLocator = (ArchiveLocator) clazz.newInstance();
+ } else if (!StringUtils.isEmpty(this.getProps().getProperty("xar.storage"))) {
+ final String xarStoragePath = this.getProps().getProperty("xar.storage");
+ this.archiveLocator = new BasicArchiveLocator(xarStoragePath);
+ } else {
throw new Exception("le paramètre 'archive.locator' ou 'xar.storage' doit être renseigné");
}
-
- archiveLocator.initialize();
-
-
- String inputFileName = getProps().getProperty("xar.dir");
- inputDir = new File(inputFileName);
- if (!inputDir.exists()) {
+
+ this.archiveLocator.initialize();
+
+ final String inputFileName = this.getProps().getProperty("xar.dir");
+ this.inputDir = new File(inputFileName);
+ if (!this.inputDir.exists()) {
logger.error(inputFileName + " n'existe pas !");
throw new IllegalArgumentException(inputFileName + " n'existe pas !");
}
- importUser = getProps().getProperty("import.user");
- importScheme = getProps().getProperty("initial.import.scheme");
+ this.importUser = this.getProps().getProperty("import.user");
+ this.importScheme = this.getProps().getProperty("initial.import.scheme");
}
@Override
protected void doProcess() throws Exception {
- Traitement traitement = new Traitement(Repartitor.class, BATCH_NAME);
- if (checkNotRunning()) {
+ final Traitement traitement = new Traitement(Repartitor.class, BATCH_NAME);
+ if (this.checkNotRunning()) {
try {
- File[] xarFiles = inputDir.listFiles(new FilenameFilter() {
+ final File[] xarFiles = this.inputDir.listFiles(new FilenameFilter() {
@Override
- public boolean accept(File dir, String name) {
+ public boolean accept(final File dir, final String name) {
return name.endsWith("_1.xar");
}
});
logger.info("starting repartitor with " + xarFiles.length + " files");
- pool = Executors.newSingleThreadExecutor();
- for (File f : xarFiles) {
- RepartitorImpl ri = new RepartitorImpl(f, archiveLocator, traitement);
- pool.submit(ri);
+ this.pool = Executors.newSingleThreadExecutor();
+ for (final File f : xarFiles) {
+ final RepartitorImpl ri = new RepartitorImpl(f, this.archiveLocator, traitement);
+ this.pool.submit(ri);
}
- Runnable terminator = new Terminator(traitement);
- pool.submit(terminator);
- pool.shutdown();
- pool.awaitTermination(100, TimeUnit.DAYS);
- } catch (Exception ex) {
+ final Runnable terminator = new Terminator(traitement);
+ this.pool.submit(terminator);
+ this.pool.shutdown();
+ this.pool.awaitTermination(100, TimeUnit.DAYS);
+ } catch (final Exception ex) {
logger.error("Repartitor.doProcess()", ex);
} finally {
- clearLock();
+ this.clearLock();
}
} else {
logger.info("cancelling repartitor - another instance is running");
return "1.0";
}
- private void declareMapping(String idCol, String exercice, String archiveName) {
+ private void declareMapping(final String idCol, final String exercice, final String archiveName) {
Connection con = null;
try {
con = PoolManager.getInstance().getConnection();
- String sql = "insert into ARCHIVE_BUDGET (IDCOLL, EXERCICE, ARCHIVE_NAME) VALUES (?,?,?)";
+ final String sql = "insert into ARCHIVE_BUDGET (IDCOLL, EXERCICE, ARCHIVE_NAME) VALUES (?,?,?)";
java.sql.PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, idCol);
ps.setString(2, exercice);
ps.setString(3, archiveName);
try {
ps.executeUpdate();
- } catch (SQLException sqlEx) {
+ } catch (final SQLException sqlEx) {
// il existait déjà ?
ps = con.prepareStatement("update ARCHIVE_BUDGET set ARCHIVE_NAME=? WHERE IDCOLL=? AND EXERCICE=?");
ps.setString(1, archiveName);
ps.setString(3, exercice);
ps.executeUpdate();
}
- } catch (Exception ex) {
+ } catch (final Exception ex) {
logger.error("Repartitor.declarMapping(Strin,String,String)", ex);
} finally {
- if (con != null)
+ if (con != null) {
PoolManager.getInstance().releaseConnection(con);
+ }
}
}
}
private class RepartitorImpl extends RunnableBatch {
- private File volume1;
- private ArchiveLocator locator;
- private Traitement traitement;
+ private final File volume1;
+ private final ArchiveLocator locator;
+ private final Traitement traitement;
- public RepartitorImpl(File volume1, ArchiveLocator locator, Traitement traitement) {
+ public RepartitorImpl(final File volume1, final ArchiveLocator locator, final Traitement traitement) {
super();
this.volume1 = volume1;
this.locator = locator;
@Override
public String getInformations() {
- StringBuilder sb = new StringBuilder();
- sb.append("RepartitorImpl<").append(volume1.getName()).append("> ").append("en cours");
+ final StringBuilder sb = new StringBuilder();
+ sb.append("RepartitorImpl<").append(this.volume1.getName()).append("> ").append("en cours");
return sb.toString();
}
@Override
public void run() {
- logger.debug("contrôle de " + volume1.getName());
- if (!volume1.exists()) {
- logger.info(volume1.getName() + " a déjà été déplacé");
+ logger.debug("contrôle de " + this.volume1.getName());
+ if (!this.volume1.exists()) {
+ logger.info(this.volume1.getName() + " a déjà été déplacé");
} else {
- ArchiveDetector archiveDetector = new DefaultArchiveDetector(ArchiveDetector.NULL, "xar", new Zip32Driver());
- Builder builder = new Builder();
- de.schlichtherle.io.File zipVolume1 = new de.schlichtherle.io.File(volume1, archiveDetector);
- Message msg = traitement.addMessage(Traitement.SEVERITY_INFO, "traitement de " + volume1.getName());
+ final ArchiveDetector archiveDetector = new DefaultArchiveDetector(ArchiveDetector.NULL, "xar", new Zip32Driver());
+ final Builder builder = new Builder();
+ de.schlichtherle.io.File zipVolume1 = new de.schlichtherle.io.File(this.volume1, archiveDetector);
+ final Message msg = this.traitement.addMessage(Traitement.SEVERITY_INFO, "traitement de " + this.volume1.getName());
try {
InputStream isManifeste = null;
Document manifeste = null;
try {
- de.schlichtherle.io.File manifesteFile = new de.schlichtherle.io.File(zipVolume1, ArchiveImporter.MANIFESTE_FILE_NAME, archiveDetector);
+ final de.schlichtherle.io.File manifesteFile = new de.schlichtherle.io.File(zipVolume1, ArchiveImporter.MANIFESTE_FILE_NAME, archiveDetector);
isManifeste = new de.schlichtherle.io.FileInputStream(manifesteFile);
manifeste = builder.build(isManifeste);
- } catch (Exception ex) {
+ } catch (final Exception ex) {
// ici, on devrait savoir si vraiment c'est pas un zip
logger.error("while reading manifeste", ex);
} finally {
- if (isManifeste != null)
+ if (isManifeste != null) {
isManifeste.close();
+ }
if (zipVolume1 != null) {
de.schlichtherle.io.File.umount(zipVolume1);
zipVolume1 = null;
}
}
- String idCol = manifeste.getRootElement().getAttributeValue("cgIdCol");
- String exercice = manifeste.getRootElement().getAttributeValue("exercice");
- Nodes volumes = manifeste.query("/manifeste/volumes/volume");
+ final String idCol = manifeste.getRootElement().getAttributeValue("cgIdCol");
+ final String exercice = manifeste.getRootElement().getAttributeValue("exercice");
+ final Nodes volumes = manifeste.query("/manifeste/volumes/volume");
boolean allVolumesAvailable = true;
- ArrayList<File> volumesFiles = new ArrayList<File>();
+ final ArrayList<File> volumesFiles = new ArrayList<File>();
for (int i = 0; i < volumes.size(); i++) {
- Element volume = (Element) volumes.get(i);
- String volFileName = volume.getAttributeValue("fichier");
- File volFile = new File(volume1.getParentFile(), volFileName);
+ final Element volume = (Element) volumes.get(i);
+ final String volFileName = volume.getAttributeValue("fichier");
+ final File volFile = new File(this.volume1.getParentFile(), volFileName);
if (!volFile.exists()) {
allVolumesAvailable = false;
msg.setInfoCompl(msg.getInfoCompl() + " | " + volFileName + " est introuvable");
- logger.debug(volume1.getName() + " - " + volFileName + " est introuvable");
- } else
+ logger.debug(this.volume1.getName() + " - " + volFileName + " est introuvable");
+ } else {
volumesFiles.add(volFile);
+ }
}
- String archiveName = ArchiveImporter.getArchiveName(volume1);
+ final String archiveName = ArchiveImporter.getArchiveName(this.volume1);
if (allVolumesAvailable) {
// on indique le lien entre le budget principal et le
// nom de l'archive
- declareMapping(idCol, exercice, archiveName);
+ Repartitor.this.declareMapping(idCol, exercice, archiveName);
if (Importer.doesOpennedTaskExistsForArchive(archiveName, exercice)) {
msg.setInfoCompl("Il existe des tâches d'import en attente ou en cours de traitement pour " + archiveName + ", l'import est repoussé");
logger.info("Il existe des tâches d'import en attente ou en cours de traitement pour " + archiveName + ", l'import est repoussé");
} else {
- String baseArchiveDest = DataLayerManager.getImplementation().getParameterValue("pj.archive.base.path");
+ final String baseArchiveDest = DataLayerManager.getImplementation().getParameterValue("pj.archive.base.path");
if (baseArchiveDest == null) {
msg.setInfoCompl("Le paramètre pj.archive.base.path n'est pas défini");
msg.setSeverity(Traitement.SEVERITY_ERROR);
- traitement.addMessage(Traitement.SEVERITY_ERROR, "Le paramètre pj.archive.base.path n'est pas défini");
+ this.traitement.addMessage(Traitement.SEVERITY_ERROR, "Le paramètre pj.archive.base.path n'est pas défini");
logger.error("Le paramètre pj.archive.base.path n'est pas défini");
} else {
- File archiveMoved = locator.getArchiveLocation(archiveName);
- File archiveDir = archiveMoved.getParentFile();
-
-
+ final File archiveMoved = this.locator.getArchiveLocation(archiveName);
+ final File archiveDir = archiveMoved.getParentFile();
+
// ici, tout est bon, donc on contrôle la
// présence de chacun des fichiers référencés
// dans le manifeste
// déplace les archives
boolean tousFichiersPresents = true;
boolean tousFichiersLisibles = true;
- SAXParserFactory saxFactory = FactoryProvider.getSaxParserFactory();
-
+ final SAXParserFactory saxFactory = FactoryProvider.getSaxParserFactory();
+
for (int i = 0; i < volumesFiles.size(); i++) {
- String volumeId = Integer.toString(i + 1);
- de.schlichtherle.io.File zipVolume = new de.schlichtherle.io.File(volumesFiles.get(i), archiveDetector);
- Nodes fichiersVolume = manifeste.query("/manifeste/documents/document[@volume='" + volumeId + "']");
+ final String volumeId = Integer.toString(i + 1);
+ final de.schlichtherle.io.File zipVolume = new de.schlichtherle.io.File(volumesFiles.get(i), archiveDetector);
+ final Nodes fichiersVolume = manifeste.query("/manifeste/documents/document[@volume='" + volumeId + "']");
try {
for (int fichierCount = 0; fichierCount < fichiersVolume.size(); fichierCount++) {
- Element fichierElement = (Element) fichiersVolume.get(fichierCount);
- String fichierPath = fichierElement.getAttributeValue("path");
- de.schlichtherle.io.File fichierEntry = new de.schlichtherle.io.File(zipVolume, fichierPath, archiveDetector);
+ final Element fichierElement = (Element) fichiersVolume.get(fichierCount);
+ final String fichierPath = fichierElement.getAttributeValue("path");
+ final de.schlichtherle.io.File fichierEntry = new de.schlichtherle.io.File(zipVolume, fichierPath, archiveDetector);
if (!fichierEntry.exists()) {
tousFichiersPresents = false;
fichierElement.addAttribute(new Attribute("added:present", Constants.ADDED_NS_URI, "false"));
} else {
- String typeFichier = fichierElement.getAttributeValue("type");
+ final String typeFichier = fichierElement.getAttributeValue("type");
if (!"PJ".equals(typeFichier)) {
// on ne parse pas les PJ
if (!fichierPath.endsWith("zip") && !fichierPath.endsWith("gz")) {
// essayons de le parser
InputStream is = null;
try {
- SAXParser parser = saxFactory.newSAXParser();
- DefaultHandler dh = new DefaultHandler();
+ final SAXParser parser = saxFactory.newSAXParser();
+ final DefaultHandler dh = new DefaultHandler();
is = new de.schlichtherle.io.FileInputStream(fichierEntry);
parser.parse(is, dh);
- } catch (Exception ex) {
+ } catch (final Exception ex) {
// le fichier n'est
// pas bien formé
tousFichiersLisibles = false;
fichierElement.addAttribute(new Attribute("added:lisible", Constants.ADDED_NS_URI, "false"));
} finally {
- if (is != null)
+ if (is != null) {
is.close();
+ }
}
}
}
}
}
- Nodes pjAbsentes = manifeste.query("//document[@pjStatus='NC']");
- if (pjAbsentes.size() > 0)
+ final Nodes pjAbsentes = manifeste.query("//document[@pjStatus='NC']");
+ if (pjAbsentes.size() > 0) {
tousFichiersPresents = false;
+ }
manifeste.getRootElement().addAttribute(new Attribute("tous-presents", Boolean.toString(tousFichiersPresents)));
manifeste.getRootElement().addAttribute(new Attribute("tous-lisibles", Boolean.toString(tousFichiersLisibles)));
// de l'archive
// on déplace les archives...
for (int i = 0; i < volumesFiles.size(); i++) {
- File f = volumesFiles.get(i);
+ final File f = volumesFiles.get(i);
// il faut qu'on puisse écrire dedans
f.setWritable(true, false);
if (i == 0) {
- de.schlichtherle.io.File volume1zip = new de.schlichtherle.io.File(f, archiveDetector);
+ final de.schlichtherle.io.File volume1zip = new de.schlichtherle.io.File(f, archiveDetector);
// on réécrit le manifeste
- logger.debug(volume1.getName() + " - réécriture du manifeste");
+ logger.debug(this.volume1.getName() + " - réécriture du manifeste");
try {
- de.schlichtherle.io.File tfManifesteEntry = new de.schlichtherle.io.File(volume1zip, "manifeste_XeMeLios.xml", archiveDetector);
- de.schlichtherle.io.FileOutputStream tfOs = new de.schlichtherle.io.FileOutputStream(tfManifesteEntry, false);
+ final de.schlichtherle.io.File tfManifesteEntry = new de.schlichtherle.io.File(volume1zip, "manifeste_XeMeLios.xml", archiveDetector);
+ final de.schlichtherle.io.FileOutputStream tfOs = new de.schlichtherle.io.FileOutputStream(tfManifesteEntry, false);
try {
tfOs.write(manifeste.toXML().getBytes("UTF-8"));
tfOs.flush();
} finally {
tfOs.close();
}
- } catch (Exception ex) {
- logger.error(volume1.getName() + " - réécriture du manifeste dans le premier volume", ex);
+ } catch (final Exception ex) {
+ logger.error(this.volume1.getName() + " - réécriture du manifeste dans le premier volume", ex);
} finally {
de.schlichtherle.io.File.umount(volume1zip);
}
}
- File dest = new File(archiveDir, f.getName());
+ final File dest = new File(archiveDir, f.getName());
try {
- logger.debug(volume1.getName() + " - déplacement vers " + dest.getParentFile().getAbsolutePath());
- if (dest.exists())
+ logger.debug(this.volume1.getName() + " - déplacement vers " + dest.getParentFile().getAbsolutePath());
+ if (dest.exists()) {
FileUtils.forceDelete(dest);
+ }
FileUtils.moveFile(f, dest);
- } catch (Exception ex) {
+ } catch (final Exception ex) {
logger.error("lors du déplacement", ex);
}
}
- Importer.createImportJob(archiveName, exercice, importScheme, importUser);
+ Importer.createImportJob(archiveName, exercice, Repartitor.this.importScheme, Repartitor.this.importUser);
msg.setInfoCompl(archiveName + " déplacé vers " + archiveDir.getAbsolutePath());
}
}
msg.setInfoCompl(msg.getInfoCompl() + " | " + archiveName + " incomplète");
logger.info(archiveName + " incomplète");
}
- } catch (Exception ex) {
+ } catch (final Exception ex) {
logger.error("RepartitorImpl.run()", ex);
msg.setInfoCompl(ex.getMessage());
msg.setSeverity(Traitement.SEVERITY_ERROR);
- } catch (Error err) {
+ } catch (final Error err) {
msg.setSeverity(Traitement.SEVERITY_ERROR);
msg.setInfoCompl(err.getMessage());
- traitement.addMessage(Traitement.SEVERITY_ERROR, err.getMessage());
+ this.traitement.addMessage(Traitement.SEVERITY_ERROR, err.getMessage());
}
}
}
private class Terminator implements Runnable {
private final Traitement traitement;
- public Terminator(Traitement traitement) {
+ public Terminator(final Traitement traitement) {
super();
this.traitement = traitement;
}
@Override
public void run() {
- traitement.setEnd(System.currentTimeMillis());
+ this.traitement.setEnd(System.currentTimeMillis());
}
}
package fr.gouv.finances.dgfip.xemelios.batch.chaineimport.repartiteur;
-import de.schlichtherle.io.ArchiveDetector;
-import de.schlichtherle.io.DefaultArchiveDetector;
-import de.schlichtherle.io.archive.zip.Zip32Driver;
-import fr.gouv.finances.dgfip.xemelios.batch.Batch;
-import fr.gouv.finances.dgfip.xemelios.batch.chaineimport.importeur.Importer;
-import fr.gouv.finances.dgfip.xemelios.batch.suivi.Message;
-import fr.gouv.finances.dgfip.xemelios.batch.suivi.Traitement;
-import fr.gouv.finances.dgfip.xemelios.data.ArchiveLocator;
-import fr.gouv.finances.dgfip.xemelios.data.BasicArchiveLocator;
-import fr.gouv.finances.dgfip.xemelios.data.DataLayerManager;
-import fr.gouv.finances.dgfip.xemelios.importers.archives.ArchiveImporter;
import java.io.File;
import java.io.FilenameFilter;
import java.io.InputStream;
-import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+
import nu.xom.Attribute;
import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Nodes;
+
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
+import de.schlichtherle.io.ArchiveDetector;
+import de.schlichtherle.io.DefaultArchiveDetector;
+import de.schlichtherle.io.archive.zip.Zip32Driver;
+import fr.gouv.finances.dgfip.xemelios.batch.Batch;
+import fr.gouv.finances.dgfip.xemelios.batch.chaineimport.importeur.Importer;
+import fr.gouv.finances.dgfip.xemelios.batch.suivi.Message;
+import fr.gouv.finances.dgfip.xemelios.batch.suivi.Traitement;
+import fr.gouv.finances.dgfip.xemelios.data.ArchiveLocator;
+import fr.gouv.finances.dgfip.xemelios.data.BasicArchiveLocator;
+import fr.gouv.finances.dgfip.xemelios.data.DataLayerManager;
+import fr.gouv.finances.dgfip.xemelios.importers.archives.ArchiveImporter;
+
/**
- *
+ *
* @author cmarchand
*/
public class RepartitorEtat extends Batch {
- private static Logger logger = Logger.getLogger(RepartitorEtat.class);
- public static final transient String BATCH_NAME = "xar.repartitor.etat";
-
- // pour s'assurer qu'on a qu'une seule instance à la fois
- private final static Object locker = new Object();
- private static int instanceCount = 0;
- private static Object instance = null;
-
- private ArchiveLocator archiveLocator;
- private File inputDir;
- private ExecutorService pool;
- // l'utilisateur qui va faire la demande d'import
- private String importUser;
- // le fichier de regle à appliquer à l'import
- private String importScheme;
-
- public RepartitorEtat(String[] args) {
- super(args);
- }
-
- @Override
- protected void initialize() throws Exception {
- super.initialize();
-
- if (!StringUtils.isEmpty(getProps().getProperty("archive.locator"))) {
- String archiveLocatorClassName = getProps().getProperty("archive.locator");
- Class clazz = Class.forName(archiveLocatorClassName);
- archiveLocator = (ArchiveLocator)clazz.newInstance();
- } else if (!StringUtils.isEmpty(getProps().getProperty("xar.storage"))) {
- String xarStoragePath = getProps().getProperty("xar.storage");
- archiveLocator = new BasicArchiveLocator(xarStoragePath);
- }else {
+ private static Logger logger = Logger.getLogger(RepartitorEtat.class);
+ public static final transient String BATCH_NAME = "xar.repartitor.etat";
+
+ // pour s'assurer qu'on a qu'une seule instance à la fois
+ private final static Object locker = new Object();
+ private static int instanceCount = 0;
+ private static Object instance = null;
+
+ private ArchiveLocator archiveLocator;
+ private File inputDir;
+ private ExecutorService pool;
+ // l'utilisateur qui va faire la demande d'import
+ private String importUser;
+ // le fichier de regle à appliquer à l'import
+ private String importScheme;
+
+ public RepartitorEtat(final String[] args) {
+ super(args);
+ }
+
+ @Override
+ protected void initialize() throws Exception {
+ super.initialize();
+
+ if (!StringUtils.isEmpty(this.getProps().getProperty("archive.locator"))) {
+ final String archiveLocatorClassName = this.getProps().getProperty("archive.locator");
+ final Class clazz = Class.forName(archiveLocatorClassName);
+ this.archiveLocator = (ArchiveLocator) clazz.newInstance();
+ } else if (!StringUtils.isEmpty(this.getProps().getProperty("xar.storage"))) {
+ final String xarStoragePath = this.getProps().getProperty("xar.storage");
+ this.archiveLocator = new BasicArchiveLocator(xarStoragePath);
+ } else {
throw new Exception("le paramètre 'archive.locator' ou 'xar.storage' doit être renseigné");
}
-
- archiveLocator.initialize();
-
-
- String inputFileName = getProps().getProperty("xar.dir");
- inputDir = new File(inputFileName);
- if(!inputDir.exists()) {
- logger.error(inputFileName+" n'existe pas !");
- throw new IllegalArgumentException(inputFileName+" n'existe pas !");
- }
- importUser = getProps().getProperty("import.user");
- importScheme = getProps().getProperty("initial.import.scheme");
-
- logger.debug("Répartiteur état initialisé");
- }
-
- @Override
- protected void doProcess() throws Exception {
- Traitement traitement = new Traitement(RepartitorEtat.class, BATCH_NAME);
- if(checkNotRunning()) {
- try {
- File[] xarFiles = inputDir.listFiles(new FilenameFilter() {
- @Override
- public boolean accept(File dir, String name) {
- return name.endsWith(".xar");
- }
- });
-
- logger.debug(xarFiles.length + " archives trouvées");
-
- pool = Executors.newSingleThreadExecutor();
- for(File f:xarFiles) {
- RepartitorEtatImpl ri = new RepartitorEtatImpl(f, archiveLocator, traitement);
- pool.submit(ri);
- }
- Runnable terminator = new Terminator(traitement);
- pool.submit(terminator);
- pool.shutdown();
- pool.awaitTermination(100, TimeUnit.DAYS);
- } catch(Exception ex) {
- logger.error("Repartitor.doProcess()",ex);
- } finally {
- clearLock();
- }
- } else {
- logger.warn("Répartitieur déjà en cours d'execution");
- traitement.setResume("Dejà en cours d'execution");
- traitement.setEnd(System.currentTimeMillis());
- }
- }
-
- @Override
- public String getResumeTraitement() {
- return "En cours";
- }
-
- @Override
- public String typeTraitementRefCode() {
- return BATCH_NAME;
- }
-
- @Override
- public String getInformations() {
- return "";
- }
-
- @Override
- protected String getBatchVersion() {
- return "1.0";
- }
- private synchronized boolean checkNotRunning() {
- synchronized(locker) {
- if(instanceCount==0) {
- instanceCount++;
- instance = this;
- return true;
- }
- return false;
- }
- }
- private synchronized void clearLock() {
- synchronized(locker) {
- // on ne peut pas supprimer un lock qu'on a pas posé
- if(instance==this) {
- instanceCount--;
- instance=null;
- }
- }
- }
- private class RepartitorEtatImpl extends RunnableBatch {
- private File volume1;
- private ArchiveLocator locator;
- private Traitement traitement;
- public RepartitorEtatImpl(File volume1, ArchiveLocator locator, Traitement traitement) {
- super();
- this.volume1 = volume1;
- this.locator = locator;
- this.traitement = traitement;
- }
-
- @Override
- public String getInformations() {
- StringBuilder sb = new StringBuilder();
- sb.append("RepartitorEtatImpl<").append(volume1.getName()).append("> ").append("en cours");
- return sb.toString();
- }
-
- @Override
- public void run() {
- Builder builder = new Builder();
- if(!volume1.exists()) {
- logger.info(volume1.getName()+" déjà traité");
- return;
- }
-
- logger.debug("traitement de "+volume1.getName());
- Message msg = traitement.addMessage(Traitement.SEVERITY_INFO, "traitement de "+volume1.getName());
-
- ArchiveDetector archiveDetector = new DefaultArchiveDetector(ArchiveDetector.NULL, "xar", new Zip32Driver());
- try {
- de.schlichtherle.io.File zipVolume1 = new de.schlichtherle.io.File(volume1, archiveDetector);
- de.schlichtherle.io.File manifesteEntry = new de.schlichtherle.io.File(zipVolume1, ArchiveImporter.MANIFESTE_FILE_NAME, archiveDetector);
- InputStream isManifeste = null;
- Document manifeste = null;
- try {
- logger.debug("Lecture du manifeste");
- isManifeste = new de.schlichtherle.io.FileInputStream(manifesteEntry);
- manifeste = builder.build(isManifeste);
- } finally {
- if(isManifeste!=null) isManifeste.close();
- de.schlichtherle.io.File.umount(zipVolume1);
- zipVolume1 = null;
- manifesteEntry = null;
- }
-
- String exercice = manifeste.getRootElement().getAttributeValue("exercice");
- if(exercice==null) {
- // on essaie de l'extraire depuis le nom de fichier
- logger.debug("Récupération de l'exercice depuis le nom du fichier");
-
- String fileName = FilenameUtils.getBaseName(volume1.getName());
- Pattern pattern = Pattern.compile("-[0-9][0-9][0-9][0-9]-");
- Matcher matcher = pattern.matcher(fileName);
-
- if(matcher.find()) {
- String sTmp = matcher.group();
- exercice = sTmp.substring(1, 5);
- } else {
- Pattern p2 = Pattern.compile("_[0-9][0-9][0-9][0-9]-");
- Matcher m2 = p2.matcher(fileName);
- if(m2.find()) {
- String sTmp = m2.group();
- exercice = sTmp.substring(1,5);
- } else {
- logger.error("impossible de détermnier l'année dans "+fileName);
- msg.setInfoCompl(msg.getInfoCompl()+"\nimpossible de déterminer l'année");
- msg.setSeverity(Traitement.SEVERITY_ERROR);
- }
- }
- }
-
- if(exercice==null) return;
-
- Nodes volumes = manifeste.query("/manifeste/volumes/volume");
- boolean allVolumesAvailable = true;
- ArrayList<File> volumesFiles = new ArrayList<File>();
- for(int i=0;i<volumes.size();i++) {
- Element volume = (Element)volumes.get(i);
- String volFileName = volume.getAttributeValue("fichier");
- File volFile = new File(volume1.getParentFile(), volFileName);
- if(!volFile.exists()) {
- allVolumesAvailable = false;
- msg.setInfoCompl(msg.getInfoCompl()+" | "+volFileName+" est introuvable");
- logger.debug(volFileName+" est introuvable");
- }
- else volumesFiles.add(volFile);
- }
-
-
- String archiveName = ArchiveImporter.getArchiveName(volume1);
-
- if(allVolumesAvailable) {
- // on indique le lien entre le budget principal et le nom de l'archive
- if(Importer.doesOpennedTaskExistsForArchive(archiveName, exercice)) {
- msg.setInfoCompl("Il existe des tâches d'import en attente ou en cours de traitement pour "+archiveName+", l'import est repoussé");
- logger.debug("Il existe des tâches d'import en attente ou en cours de traitement pour "+archiveName+", l'import est repoussé");
- } else {
- String baseArchiveDest = DataLayerManager.getImplementation().getParameterValue("pj.archive.base.path");
- if(baseArchiveDest==null) {
- msg.setInfoCompl("Le paramètre pj.archive.base.path n'est pas défini");
- logger.warn("Le paramètre pj.archive.base.path n'est pas défini");
- msg.setSeverity(Traitement.SEVERITY_ERROR);
- traitement.addMessage(Traitement.SEVERITY_ERROR, "Le paramètre pj.archive.base.path n'est pas défini");
- } else {
- File archiveMoved = locator.getArchiveLocation(archiveName);
- File archiveDir = archiveMoved.getParentFile();
-
- logger.debug("Archive path=" + archiveDir.getAbsolutePath());
-
- // ici, tout est bon, donc on contrôle la présence de chacun des fichiers référencés dans le manifeste
- // dans les volumes, on parse les fichiers pour vérifier qu'ils sont lisibles
- // et on met à jour le manifeste
- // seulement quand tout ceci est fait, on déplace les archives
- boolean tousFichiersPresents = true;
- boolean tousFichiersLisibles = true;
- manifeste.getRootElement().addAttribute(new Attribute("tous-presents", Boolean.toString(tousFichiersPresents)));
- manifeste.getRootElement().addAttribute(new Attribute("tous-lisibles", Boolean.toString(tousFichiersLisibles)));
-
-
- // mettre à jour la manifeste du premier volume de l'archive
- // on déplace les archives...
- for(int i=0;i<volumesFiles.size();i++) {
- File f = volumesFiles.get(i);
- // il faut qu'on puisse écrire dedans
- f.setWritable(true,false);
-
- if(i==0) {
- // on réécrit le manifeste
- logger.debug(archiveName+" - réécriture du manifeste");
- try {
- de.schlichtherle.io.File zipFile = new de.schlichtherle.io.File(f, archiveDetector);
- de.schlichtherle.io.File tfManifesteEntry = new de.schlichtherle.io.File(zipFile,ArchiveImporter.MANIFESTE_FILE_NAME,archiveDetector);
- de.schlichtherle.io.FileOutputStream tfOs = new de.schlichtherle.io.FileOutputStream(tfManifesteEntry, false);
- try {
- tfOs.write(manifeste.toXML().getBytes("UTF-8"));
- tfOs.flush();
- } finally {
- tfOs.close();
- de.schlichtherle.io.File.umount(zipFile);
- }
- } catch(Exception ex) {
- logger.error(archiveName+" - réécriture du manifeste dans le premier volume",ex);
- }
- }
- File dest = new File(archiveDir, f.getName());
- // on essaye de renommer
- try {
- logger.debug("Déplacement de l'archive");
-
- if(dest.exists()) {
- FileUtils.forceDelete(dest);
- }
- FileUtils.moveFile(f, dest);
- } catch(Exception ex) {
- logger.error(archiveName+" - lors du déplacement", ex);
- }
- }
- Importer.createImportJob(archiveName, exercice, importScheme, importUser);
- msg.setInfoCompl(archiveName+" déplacé vers "+archiveDir.getAbsolutePath());
- }
- }
- } else {
- logger.info(archiveName+" incomplète");
- msg.setInfoCompl(archiveName+" incomplète");
- }
- } catch(Exception ex) {
- logger.error("RepartitorImpl.run()", ex);
- }
- }
- }
-
- private class Terminator implements Runnable {
- private final Traitement traitement;
- public Terminator(Traitement traitement) {
- super();
- this.traitement=traitement;
- }
-
- @Override
- public void run() {
- traitement.setEnd(System.currentTimeMillis());
- }
- }
+
+ this.archiveLocator.initialize();
+
+ final String inputFileName = this.getProps().getProperty("xar.dir");
+ this.inputDir = new File(inputFileName);
+ if (!this.inputDir.exists()) {
+ logger.error(inputFileName + " n'existe pas !");
+ throw new IllegalArgumentException(inputFileName + " n'existe pas !");
+ }
+ this.importUser = this.getProps().getProperty("import.user");
+ this.importScheme = this.getProps().getProperty("initial.import.scheme");
+
+ logger.debug("Répartiteur état initialisé");
+ }
+
+ @Override
+ protected void doProcess() throws Exception {
+ final Traitement traitement = new Traitement(RepartitorEtat.class, BATCH_NAME);
+ if (this.checkNotRunning()) {
+ try {
+ final File[] xarFiles = this.inputDir.listFiles(new FilenameFilter() {
+ @Override
+ public boolean accept(final File dir, final String name) {
+ return name.endsWith(".xar");
+ }
+ });
+
+ logger.debug(xarFiles.length + " archives trouvées");
+
+ this.pool = Executors.newSingleThreadExecutor();
+ for (final File f : xarFiles) {
+ final RepartitorEtatImpl ri = new RepartitorEtatImpl(f, this.archiveLocator, traitement);
+ this.pool.submit(ri);
+ }
+ final Runnable terminator = new Terminator(traitement);
+ this.pool.submit(terminator);
+ this.pool.shutdown();
+ this.pool.awaitTermination(100, TimeUnit.DAYS);
+ } catch (final Exception ex) {
+ logger.error("Repartitor.doProcess()", ex);
+ } finally {
+ this.clearLock();
+ }
+ } else {
+ logger.warn("Répartitieur déjà en cours d'execution");
+ traitement.setResume("Dejà en cours d'execution");
+ traitement.setEnd(System.currentTimeMillis());
+ }
+ }
+
+ @Override
+ public String getResumeTraitement() {
+ return "En cours";
+ }
+
+ @Override
+ public String typeTraitementRefCode() {
+ return BATCH_NAME;
+ }
+
+ @Override
+ public String getInformations() {
+ return "";
+ }
+
+ @Override
+ protected String getBatchVersion() {
+ return "1.0";
+ }
+
+ private synchronized boolean checkNotRunning() {
+ synchronized (locker) {
+ if (instanceCount == 0) {
+ instanceCount++;
+ instance = this;
+ return true;
+ }
+ return false;
+ }
+ }
+
+ private synchronized void clearLock() {
+ synchronized (locker) {
+ // on ne peut pas supprimer un lock qu'on a pas posé
+ if (instance == this) {
+ instanceCount--;
+ instance = null;
+ }
+ }
+ }
+
+ private class RepartitorEtatImpl extends RunnableBatch {
+ private final File volume1;
+ private final ArchiveLocator locator;
+ private final Traitement traitement;
+
+ public RepartitorEtatImpl(final File volume1, final ArchiveLocator locator, final Traitement traitement) {
+ super();
+ this.volume1 = volume1;
+ this.locator = locator;
+ this.traitement = traitement;
+ }
+
+ @Override
+ public String getInformations() {
+ final StringBuilder sb = new StringBuilder();
+ sb.append("RepartitorEtatImpl<").append(this.volume1.getName()).append("> ").append("en cours");
+ return sb.toString();
+ }
+
+ @Override
+ public void run() {
+ final Builder builder = new Builder();
+ if (!this.volume1.exists()) {
+ logger.info(this.volume1.getName() + " déjà traité");
+ return;
+ }
+
+ logger.debug("traitement de " + this.volume1.getName());
+ final Message msg = this.traitement.addMessage(Traitement.SEVERITY_INFO, "traitement de " + this.volume1.getName());
+
+ final ArchiveDetector archiveDetector = new DefaultArchiveDetector(ArchiveDetector.NULL, "xar", new Zip32Driver());
+ try {
+ de.schlichtherle.io.File zipVolume1 = new de.schlichtherle.io.File(this.volume1, archiveDetector);
+ de.schlichtherle.io.File manifesteEntry = new de.schlichtherle.io.File(zipVolume1, ArchiveImporter.MANIFESTE_FILE_NAME, archiveDetector);
+ InputStream isManifeste = null;
+ Document manifeste = null;
+ try {
+ logger.debug("Lecture du manifeste");
+ isManifeste = new de.schlichtherle.io.FileInputStream(manifesteEntry);
+ manifeste = builder.build(isManifeste);
+ } finally {
+ if (isManifeste != null) {
+ isManifeste.close();
+ }
+ de.schlichtherle.io.File.umount(zipVolume1);
+ zipVolume1 = null;
+ manifesteEntry = null;
+ }
+
+ String exercice = manifeste.getRootElement().getAttributeValue("exercice");
+ if (exercice == null) {
+ // on essaie de l'extraire depuis le nom de fichier
+ logger.debug("Récupération de l'exercice depuis le nom du fichier");
+
+ final String fileName = FilenameUtils.getBaseName(this.volume1.getName());
+ final Pattern pattern = Pattern.compile("-[0-9][0-9][0-9][0-9]-");
+ final Matcher matcher = pattern.matcher(fileName);
+
+ if (matcher.find()) {
+ final String sTmp = matcher.group();
+ exercice = sTmp.substring(1, 5);
+ } else {
+ final Pattern p2 = Pattern.compile("_[0-9][0-9][0-9][0-9]-");
+ final Matcher m2 = p2.matcher(fileName);
+ if (m2.find()) {
+ final String sTmp = m2.group();
+ exercice = sTmp.substring(1, 5);
+ } else {
+ logger.error("impossible de détermnier l'année dans " + fileName);
+ msg.setInfoCompl(msg.getInfoCompl() + "\nimpossible de déterminer l'année");
+ msg.setSeverity(Traitement.SEVERITY_ERROR);
+ }
+ }
+ }
+
+ if (exercice == null) {
+ return;
+ }
+
+ final Nodes volumes = manifeste.query("/manifeste/volumes/volume");
+ boolean allVolumesAvailable = true;
+ final ArrayList<File> volumesFiles = new ArrayList<File>();
+ for (int i = 0; i < volumes.size(); i++) {
+ final Element volume = (Element) volumes.get(i);
+ final String volFileName = volume.getAttributeValue("fichier");
+ final File volFile = new File(this.volume1.getParentFile(), volFileName);
+ if (!volFile.exists()) {
+ allVolumesAvailable = false;
+ msg.setInfoCompl(msg.getInfoCompl() + " | " + volFileName + " est introuvable");
+ logger.debug(volFileName + " est introuvable");
+ } else {
+ volumesFiles.add(volFile);
+ }
+ }
+
+ final String archiveName = ArchiveImporter.getArchiveName(this.volume1);
+
+ if (allVolumesAvailable) {
+ // on indique le lien entre le budget principal et le nom de l'archive
+ if (Importer.doesOpennedTaskExistsForArchive(archiveName, exercice)) {
+ msg.setInfoCompl("Il existe des tâches d'import en attente ou en cours de traitement pour " + archiveName + ", l'import est repoussé");
+ logger.debug("Il existe des tâches d'import en attente ou en cours de traitement pour " + archiveName + ", l'import est repoussé");
+ } else {
+ final String baseArchiveDest = DataLayerManager.getImplementation().getParameterValue("pj.archive.base.path");
+ if (baseArchiveDest == null) {
+ msg.setInfoCompl("Le paramètre pj.archive.base.path n'est pas défini");
+ logger.warn("Le paramètre pj.archive.base.path n'est pas défini");
+ msg.setSeverity(Traitement.SEVERITY_ERROR);
+ this.traitement.addMessage(Traitement.SEVERITY_ERROR, "Le paramètre pj.archive.base.path n'est pas défini");
+ } else {
+ final File archiveMoved = this.locator.getArchiveLocation(archiveName);
+ final File archiveDir = archiveMoved.getParentFile();
+
+ logger.debug("Archive path=" + archiveDir.getAbsolutePath());
+
+ // ici, tout est bon, donc on contrôle la présence de chacun des fichiers référencés dans le manifeste
+ // dans les volumes, on parse les fichiers pour vérifier qu'ils sont lisibles
+ // et on met à jour le manifeste
+ // seulement quand tout ceci est fait, on déplace les archives
+ final boolean tousFichiersPresents = true;
+ final boolean tousFichiersLisibles = true;
+ manifeste.getRootElement().addAttribute(new Attribute("tous-presents", Boolean.toString(tousFichiersPresents)));
+ manifeste.getRootElement().addAttribute(new Attribute("tous-lisibles", Boolean.toString(tousFichiersLisibles)));
+
+ // mettre à jour la manifeste du premier volume de l'archive
+ // on déplace les archives...
+ for (int i = 0; i < volumesFiles.size(); i++) {
+ final File f = volumesFiles.get(i);
+ // il faut qu'on puisse écrire dedans
+ f.setWritable(true, false);
+
+ if (i == 0) {
+ // on réécrit le manifeste
+ logger.debug(archiveName + " - réécriture du manifeste");
+ try {
+ final de.schlichtherle.io.File zipFile = new de.schlichtherle.io.File(f, archiveDetector);
+ final de.schlichtherle.io.File tfManifesteEntry = new de.schlichtherle.io.File(zipFile, ArchiveImporter.MANIFESTE_FILE_NAME, archiveDetector);
+ final de.schlichtherle.io.FileOutputStream tfOs = new de.schlichtherle.io.FileOutputStream(tfManifesteEntry, false);
+ try {
+ tfOs.write(manifeste.toXML().getBytes("UTF-8"));
+ tfOs.flush();
+ } finally {
+ tfOs.close();
+ de.schlichtherle.io.File.umount(zipFile);
+ }
+ } catch (final Exception ex) {
+ logger.error(archiveName + " - réécriture du manifeste dans le premier volume", ex);
+ }
+ }
+ final File dest = new File(archiveDir, f.getName());
+ // on essaye de renommer
+ try {
+ logger.debug("Déplacement de l'archive");
+
+ if (dest.exists()) {
+ FileUtils.forceDelete(dest);
+ }
+ FileUtils.moveFile(f, dest);
+ } catch (final Exception ex) {
+ logger.error(archiveName + " - lors du déplacement", ex);
+ }
+ }
+ Importer.createImportJob(archiveName, exercice, RepartitorEtat.this.importScheme, RepartitorEtat.this.importUser);
+ msg.setInfoCompl(archiveName + " déplacé vers " + archiveDir.getAbsolutePath());
+ }
+ }
+ } else {
+ logger.info(archiveName + " incomplète");
+ msg.setInfoCompl(archiveName + " incomplète");
+ }
+ } catch (final Exception ex) {
+ logger.error("RepartitorImpl.run()", ex);
+ }
+ }
+ }
+
+ private class Terminator implements Runnable {
+ private final Traitement traitement;
+
+ public Terminator(final Traitement traitement) {
+ super();
+ this.traitement = traitement;
+ }
+
+ @Override
+ public void run() {
+ this.traitement.setEnd(System.currentTimeMillis());
+ }
+ }
}
import java.util.jar.JarInputStream;
/**
- * @author CBO
- * Classe permettant de manipuler des ensembles de certificats
+ * @author CBO Classe permettant de manipuler des ensembles de certificats
*/
public abstract class CertificateContainer {
- public abstract Collection<X509Certificate> getAllCertificates();
+ public abstract Collection<X509Certificate> getAllCertificates();
- public static CertificateContainer fromZipURL(URL zipUrl) throws IOException, CertificateException {
- List<X509Certificate> allCertificates = new ArrayList<X509Certificate>();
- CertificateFactory cf = CertificateFactory.getInstance("X.509");
- JarInputStream jarIS = new JarInputStream(zipUrl.openStream());
- try {
- JarEntry entry = null;
- while ((entry = jarIS.getNextJarEntry()) != null) {
- if (!entry.isDirectory()) {
- if (entry.getName().toUpperCase().endsWith(".CER") || entry.getName().toUpperCase().endsWith(".CRT")) {
- X509Certificate certificate = (X509Certificate) cf.generateCertificate(jarIS);
- allCertificates.add(certificate);
- }
- }
- }
- } finally {
- jarIS.close();
- }
- return new SimpleCertificateContainer(allCertificates);
- }
+ public static CertificateContainer fromZipURL(final URL zipUrl) throws IOException, CertificateException {
+ final List<X509Certificate> allCertificates = new ArrayList<X509Certificate>();
+ final CertificateFactory cf = CertificateFactory.getInstance("X.509");
+ final JarInputStream jarIS = new JarInputStream(zipUrl.openStream());
+ try {
+ JarEntry entry = null;
+ while ((entry = jarIS.getNextJarEntry()) != null) {
+ if (!entry.isDirectory()) {
+ if (entry.getName().toUpperCase().endsWith(".CER") || entry.getName().toUpperCase().endsWith(".CRT")) {
+ final X509Certificate certificate = (X509Certificate) cf.generateCertificate(jarIS);
+ allCertificates.add(certificate);
+ }
+ }
+ }
+ } finally {
+ jarIS.close();
+ }
+ return new SimpleCertificateContainer(allCertificates);
+ }
- public static CertificateContainer fromCollection(Collection<X509Certificate> certificates) {
- return new SimpleCertificateContainer(certificates);
- }
+ public static CertificateContainer fromCollection(final Collection<X509Certificate> certificates) {
+ return new SimpleCertificateContainer(certificates);
+ }
- public KeyStore buildJKSKeyStore() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {
- KeyStore keyStore = CertificateUtils.createEmptyJKSKeyStore();
- for (X509Certificate certificate : getAllCertificates()) {
- keyStore.setCertificateEntry(certificate.getSubjectDN().getName(), certificate);
- }
- return keyStore;
- }
+ public KeyStore buildJKSKeyStore() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {
+ final KeyStore keyStore = CertificateUtils.createEmptyJKSKeyStore();
+ for (final X509Certificate certificate : this.getAllCertificates()) {
+ keyStore.setCertificateEntry(certificate.getSubjectDN().getName(), certificate);
+ }
+ return keyStore;
+ }
- public CertStore buildCertStore() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException {
- return CertStore.getInstance("Collection", new CollectionCertStoreParameters(getAllCertificates()));
- }
+ public CertStore buildCertStore() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException {
+ return CertStore.getInstance("Collection", new CollectionCertStoreParameters(this.getAllCertificates()));
+ }
- public Collection<X509Certificate> extractRootCertificates() {
- List<X509Certificate> toReturn = new ArrayList<X509Certificate>();
- for (X509Certificate certificate : getAllCertificates()) {
- if (CertificateUtils.isSelfSigned(certificate)) {
- toReturn.add(certificate);
- }
- }
- return toReturn;
- }
+ public Collection<X509Certificate> extractRootCertificates() {
+ final List<X509Certificate> toReturn = new ArrayList<X509Certificate>();
+ for (final X509Certificate certificate : this.getAllCertificates()) {
+ if (CertificateUtils.isSelfSigned(certificate)) {
+ toReturn.add(certificate);
+ }
+ }
+ return toReturn;
+ }
- public Collection<X509Certificate> extractNotRootCertificates() {
- List<X509Certificate> toReturn = new ArrayList<X509Certificate>();
- for (X509Certificate certificate : getAllCertificates()) {
- if (!CertificateUtils.isSelfSigned(certificate)) {
- toReturn.add(certificate);
- }
- }
- return toReturn;
- }
+ public Collection<X509Certificate> extractNotRootCertificates() {
+ final List<X509Certificate> toReturn = new ArrayList<X509Certificate>();
+ for (final X509Certificate certificate : this.getAllCertificates()) {
+ if (!CertificateUtils.isSelfSigned(certificate)) {
+ toReturn.add(certificate);
+ }
+ }
+ return toReturn;
+ }
- public boolean contains(X509Certificate certificate) throws CertificateEncodingException {
- for (X509Certificate certificateItem : getAllCertificates()) {
- if (CertificateUtils.equals(certificate, certificateItem)) {
- return true;
- }
- }
- return false;
- }
+ public boolean contains(final X509Certificate certificate) throws CertificateEncodingException {
+ for (final X509Certificate certificateItem : this.getAllCertificates()) {
+ if (CertificateUtils.equals(certificate, certificateItem)) {
+ return true;
+ }
+ }
+ return false;
+ }
- public static class SimpleCertificateContainer extends CertificateContainer {
+ public static class SimpleCertificateContainer extends CertificateContainer {
- private Collection<X509Certificate> allCertificates;
+ private final Collection<X509Certificate> allCertificates;
- public SimpleCertificateContainer(Collection<X509Certificate> allCertificates) {
- this.allCertificates = allCertificates;
- }
+ public SimpleCertificateContainer(final Collection<X509Certificate> allCertificates) {
+ this.allCertificates = allCertificates;
+ }
- @Override
- public Collection<X509Certificate> getAllCertificates() {
- return allCertificates;
- }
- }
+ @Override
+ public Collection<X509Certificate> getAllCertificates() {
+ return this.allCertificates;
+ }
+ }
}
import java.security.cert.X509Certificate;
/**
- * @author CBO
- * Classe utilitaire autour des certificas
+ * @author CBO Classe utilitaire autour des certificas
*/
public class CertificateUtils {
- private CertificateUtils() {
- }
+ private CertificateUtils() {
+ }
- public static boolean isSelfSigned(X509Certificate certificate) {
- return certificate.getIssuerDN().equals(certificate.getSubjectDN());
- }
+ public static boolean isSelfSigned(final X509Certificate certificate) {
+ return certificate.getIssuerDN().equals(certificate.getSubjectDN());
+ }
- public static KeyStore createEmptyJKSKeyStore() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {
- return createEmptyKeyStore("JKS");
- }
+ public static KeyStore createEmptyJKSKeyStore() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {
+ return createEmptyKeyStore("JKS");
+ }
- public static KeyStore createEmptyKeyStore(String type) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {
- KeyStore ks = KeyStore.getInstance("JKS");
- ks.load(null, null);
- return ks;
- }
+ public static KeyStore createEmptyKeyStore(final String type) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {
+ final KeyStore ks = KeyStore.getInstance("JKS");
+ ks.load(null, null);
+ return ks;
+ }
- public static boolean equals(X509Certificate firstCertificate, X509Certificate secondCertificate) throws CertificateEncodingException {
- byte firstCertificateBytes[] = firstCertificate.getEncoded();
- byte secondCertificateBytes[] = secondCertificate.getEncoded();
- if (firstCertificateBytes.length != secondCertificateBytes.length) {
- return false;
- }
- for (int i = 0; i < firstCertificateBytes.length; i++) {
- if (firstCertificateBytes[i] != secondCertificateBytes[i]) {
- return false;
- }
- }
- return true;
- }
+ public static boolean equals(final X509Certificate firstCertificate, final X509Certificate secondCertificate) throws CertificateEncodingException {
+ final byte firstCertificateBytes[] = firstCertificate.getEncoded();
+ final byte secondCertificateBytes[] = secondCertificate.getEncoded();
+ if (firstCertificateBytes.length != secondCertificateBytes.length) {
+ return false;
+ }
+ for (int i = 0; i < firstCertificateBytes.length; i++) {
+ if (firstCertificateBytes[i] != secondCertificateBytes[i]) {
+ return false;
+ }
+ }
+ return true;
+ }
}
*/
public class FileUtils {
- private FileUtils() {
- }
+ private FileUtils() {
+ }
- public static void moveFileWithOverwrite(File scr, File dest) throws IOException {
- if (dest.exists()) {
- org.apache.commons.io.FileUtils.forceDelete(dest);
- }
- org.apache.commons.io.FileUtils.moveFile(scr, dest);
- }
+ public static void moveFileWithOverwrite(final File scr, final File dest) throws IOException {
+ if (dest.exists()) {
+ org.apache.commons.io.FileUtils.forceDelete(dest);
+ }
+ org.apache.commons.io.FileUtils.moveFile(scr, dest);
+ }
- public static void deleteDirectory(File directory) throws IOException {
- org.apache.commons.io.FileUtils.deleteDirectory(directory);
- }
+ public static void deleteDirectory(final File directory) throws IOException {
+ org.apache.commons.io.FileUtils.deleteDirectory(directory);
+ }
- public static List<File> listFiles(File directory, FileFilter fileFilter, Comparator<File> orderer) {
- List<File> toReturn = new ArrayList<File>();
- File files[] = null;
- if (fileFilter != null) {
- files = directory.listFiles(fileFilter);
- } else {
- files = directory.listFiles();
- }
- toReturn.addAll(Arrays.asList(files));
- if (orderer != null) {
- Collections.sort(toReturn, orderer);
- }
- return toReturn;
- }
+ public static List<File> listFiles(final File directory, final FileFilter fileFilter, final Comparator<File> orderer) {
+ final List<File> toReturn = new ArrayList<File>();
+ File files[] = null;
+ if (fileFilter != null) {
+ files = directory.listFiles(fileFilter);
+ } else {
+ files = directory.listFiles();
+ }
+ toReturn.addAll(Arrays.asList(files));
+ if (orderer != null) {
+ Collections.sort(toReturn, orderer);
+ }
+ return toReturn;
+ }
- public static class ByExtensionFileFilter implements FileFilter {
+ public static class ByExtensionFileFilter implements FileFilter {
- private String extensionWithoutPoint;
+ private final String extensionWithoutPoint;
- public ByExtensionFileFilter(String extensionWithoutPoint) {
- this.extensionWithoutPoint = extensionWithoutPoint;
- }
+ public ByExtensionFileFilter(final String extensionWithoutPoint) {
+ this.extensionWithoutPoint = extensionWithoutPoint;
+ }
- @Override
- public boolean accept(File file) {
- return !file.isDirectory() && file.getName().toLowerCase().endsWith("." + extensionWithoutPoint);
- }
- }
+ @Override
+ public boolean accept(final File file) {
+ return !file.isDirectory() && file.getName().toLowerCase().endsWith("." + this.extensionWithoutPoint);
+ }
+ }
- public static class LastModifiedFileComparator implements Comparator<File> {
+ public static class LastModifiedFileComparator implements Comparator<File> {
- @Override
- public int compare(File first, File second) {
- return Long.valueOf(first.lastModified()).compareTo(second.lastModified());
- }
- }
+ @Override
+ public int compare(final File first, final File second) {
+ return Long.valueOf(first.lastModified()).compareTo(second.lastModified());
+ }
+ }
}
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
+
import org.apache.xml.security.signature.XMLSignatureInput;
import org.apache.xml.security.utils.resolver.ResourceResolverException;
import org.apache.xml.security.utils.resolver.ResourceResolverSpi;
*/
public class StdResourceResolver extends ResourceResolverSpi {
- static private ThreadLocal<SigningProcessDataHolder> signingProcessDataHolderThreadLocal = new ThreadLocal<SigningProcessDataHolder>();
-
- static void startSigningProcess(File baseDir) {
- signingProcessDataHolderThreadLocal.set(new SigningProcessDataHolder(baseDir));
- }
-
- static void endSigningProcess() {
- for (IsOpenInputStream is : signingProcessDataHolderThreadLocal.get().getUsedInputStreams()) {
- if (is.isOpen()) {
- try {
- is.close();
- } catch(IOException ioe) {
- throw new RuntimeException(ioe);
- }
- }
- }
- signingProcessDataHolderThreadLocal.set(null);
- }
-
- static private File getBaseDir() {
- return signingProcessDataHolderThreadLocal.get().getBaseDir();
- }
-
- @Override
- public XMLSignatureInput engineResolve(Attr uri, String BaseURI) throws ResourceResolverException {
- try {
- IsOpenInputStream iois = new IsOpenInputStream(new File(getBaseDir(), uri.getValue()));
- signingProcessDataHolderThreadLocal.get().usedInputStreams.add(iois);
- return new XMLSignatureInput(iois);
- } catch (FileNotFoundException fnfe) {
- throw new RuntimeException(fnfe);
- }
- }
-
- @Override
- public boolean engineCanResolve(Attr uri, String baseURI) {
- return (baseURI == null || baseURI.isEmpty()) && ! uri.getValue().startsWith("#") && new File(getBaseDir(), uri.getValue()).exists();
- }
-
- static private class IsOpenInputStream extends FileInputStream {
-
- private boolean open = true;
-
- private IsOpenInputStream(File file) throws FileNotFoundException {
- super(file);
- }
-
- @Override
- public void close() throws IOException {
- super.close();
- this.open = false;
- }
-
- public boolean isOpen() {
- return open;
- }
-
- }
-
- static private class SigningProcessDataHolder {
- private File baseDir;
- private List<IsOpenInputStream> usedInputStreams;
-
- private SigningProcessDataHolder(File baseDir) {
- this.baseDir = baseDir;
- usedInputStreams = new ArrayList<IsOpenInputStream>();
- }
-
- public File getBaseDir() {
- return baseDir;
- }
-
- public List<IsOpenInputStream> getUsedInputStreams() {
- return usedInputStreams;
- }
-
-
- }
+ static private ThreadLocal<SigningProcessDataHolder> signingProcessDataHolderThreadLocal = new ThreadLocal<SigningProcessDataHolder>();
+
+ static void startSigningProcess(final File baseDir) {
+ signingProcessDataHolderThreadLocal.set(new SigningProcessDataHolder(baseDir));
+ }
+
+ static void endSigningProcess() {
+ for (final IsOpenInputStream is : signingProcessDataHolderThreadLocal.get().getUsedInputStreams()) {
+ if (is.isOpen()) {
+ try {
+ is.close();
+ } catch (final IOException ioe) {
+ throw new RuntimeException(ioe);
+ }
+ }
+ }
+ signingProcessDataHolderThreadLocal.set(null);
+ }
+
+ static private File getBaseDir() {
+ return signingProcessDataHolderThreadLocal.get().getBaseDir();
+ }
+
+ @Override
+ public XMLSignatureInput engineResolve(final Attr uri, final String BaseURI) throws ResourceResolverException {
+ try {
+ final IsOpenInputStream iois = new IsOpenInputStream(new File(getBaseDir(), uri.getValue()));
+ signingProcessDataHolderThreadLocal.get().usedInputStreams.add(iois);
+ return new XMLSignatureInput(iois);
+ } catch (final FileNotFoundException fnfe) {
+ throw new RuntimeException(fnfe);
+ }
+ }
+
+ @Override
+ public boolean engineCanResolve(final Attr uri, final String baseURI) {
+ return (baseURI == null || baseURI.isEmpty()) && !uri.getValue().startsWith("#") && new File(getBaseDir(), uri.getValue()).exists();
+ }
+
+ static private class IsOpenInputStream extends FileInputStream {
+
+ private boolean open = true;
+
+ private IsOpenInputStream(final File file) throws FileNotFoundException {
+ super(file);
+ }
+
+ @Override
+ public void close() throws IOException {
+ super.close();
+ this.open = false;
+ }
+
+ public boolean isOpen() {
+ return this.open;
+ }
+
+ }
+
+ static private class SigningProcessDataHolder {
+ private final File baseDir;
+ private final List<IsOpenInputStream> usedInputStreams;
+
+ private SigningProcessDataHolder(final File baseDir) {
+ this.baseDir = baseDir;
+ this.usedInputStreams = new ArrayList<IsOpenInputStream>();
+ }
+
+ public File getBaseDir() {
+ return this.baseDir;
+ }
+
+ public List<IsOpenInputStream> getUsedInputStreams() {
+ return this.usedInputStreams;
+ }
+
+ }
}
package fr.gouv.finances.dgfip.xemelios.batch.chaineimport.xasverifier;
-import fr.gouv.finances.dgfip.xemelios.batch.chaineimport.xasverifier.FileUtils.ByExtensionFileFilter;
-import fr.gouv.finances.dgfip.xemelios.batch.chaineimport.xasverifier.FileUtils.LastModifiedFileComparator;
-import fr.gouv.finances.dgfip.utils.xml.FactoryProvider;
-import fr.gouv.finances.dgfip.utils.xml.NamespaceContextImpl;
-import fr.gouv.finances.dgfip.xemelios.batch.Batch;
-import fr.gouv.finances.dgfip.xemelios.batch.suivi.Message;
-import fr.gouv.finances.dgfip.xemelios.batch.suivi.Traitement;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
+
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
+
import org.apache.log4j.Logger;
import org.apache.xml.security.utils.resolver.ResourceResolver;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
+
import xades4j.XAdES4jException;
import xades4j.providers.CertificateValidationProvider;
import xades4j.providers.impl.PKIXCertificateValidationProvider;
import xades4j.verification.XAdESVerificationResult;
import xades4j.verification.XadesVerificationProfile;
import xades4j.verification.XadesVerifier;
+import fr.gouv.finances.dgfip.utils.xml.FactoryProvider;
+import fr.gouv.finances.dgfip.utils.xml.NamespaceContextImpl;
+import fr.gouv.finances.dgfip.xemelios.batch.Batch;
+import fr.gouv.finances.dgfip.xemelios.batch.chaineimport.xasverifier.FileUtils.ByExtensionFileFilter;
+import fr.gouv.finances.dgfip.xemelios.batch.chaineimport.xasverifier.FileUtils.LastModifiedFileComparator;
+import fr.gouv.finances.dgfip.xemelios.batch.suivi.Message;
+import fr.gouv.finances.dgfip.xemelios.batch.suivi.Traitement;
/**
* @author CBO
*/
public class XasVerifier extends Batch {
- private static final Logger logger = Logger.getLogger(XasVerifier.class);
- public static final String SIGNATURE_SPEC_NS = "http://www.w3.org/2000/09/xmldsig#";
-
- // pour s'assurer qu'on en fait pas plusieurs à la fois
- private static int instanceCount = 0;
- private static Object instance = null;
- private static final Object locker = new Object();
-
- public static final String BATCH_NAME = "xas.verifier";
- static {
- ResourceResolver.init();
- ResourceResolver.register(StdResourceResolver.class.getName());
- }
- private static int BUFFER_SIZE = 2048;
- public static final String INPUT_DIR_PROPNAME = "cft.dir";
- public static final String FILE_OK_PROPNAME = "xar.out";
- public static final String FILE_KO_PROPNAME = "xas.error";
- public static final String FILE_ARCH_PROPNAME = "arch.dir";
- public static final String ARCH_PROPNAME = "archive.xas";
-
- private boolean shouldArchivateXas = false;
- private File archiveDir = null;
- private File inputDir = null;
- private File okDir = null;
- private File koDir = null;
-
- private Traitement traitement;
-
- public XasVerifier(String args[]) {
- super(args);
- // on vérifie que le machin ne tourne pas, et sinon on quitte
- traitement = new Traitement(XasVerifier.class, BATCH_NAME);
- }
-
- @Override
- protected void initialize() throws Exception {
- String inputDirFileName = getProps().getProperty(INPUT_DIR_PROPNAME);
- String fileOkDirName = getProps().getProperty(FILE_OK_PROPNAME);
- String fileKODirName = getProps().getProperty(FILE_KO_PROPNAME);
- String fileOkArchDirName = getProps().getProperty(FILE_ARCH_PROPNAME);
- String archivateXas = getProps().getProperty(ARCH_PROPNAME, "false");
- shouldArchivateXas = Boolean.parseBoolean(archivateXas);
- if(shouldArchivateXas ) {
- archiveDir = new File(fileOkArchDirName);
- }
- inputDir = new File(inputDirFileName);
- okDir = new File(fileOkDirName);
- koDir = new File(fileKODirName);
- checkWriteDirectory(inputDir);
- checkWriteDirectory(okDir);
- checkWriteDirectory(koDir);
- if(shouldArchivateXas)
- checkWriteDirectory(archiveDir);
- }
-
- protected void process(File fileInputDir, File fileOKDir, File fileKODir, File fileOKArchDir) throws Exception {
-// logger.info(" Répertoire d'entrée : " + fileInputDir.getAbsolutePath());
-// logger.info(" Répertoire de sortie OK : " + fileOKDir.getAbsolutePath());
-// logger.info(" Répertoire de sortie KO : " + fileKODir.getAbsolutePath());
-// if(fileOKArchDir!=null)
-// logger.info(" Répertoire d'archivage XAS OK : " + fileOKArchDir.getAbsolutePath());
-// else
-// logger.info(" Pas d'archivqge des XAS");
- URL tcUrl = XasVerifier.class.getResource("/fr/gouv/finances/dgfip/xemelios/batch/chaineimport/xasverifier/resources/trustChains.zip");
- CertificateContainer trustChains = CertificateContainer.fromZipURL(tcUrl);
- URL tsUrl = XasVerifier.class.getResource("/fr/gouv/finances/dgfip/xemelios/batch/chaineimport/xasverifier/resources/trustSigners.zip");
- CertificateContainer trustSigners = CertificateContainer.fromZipURL(tsUrl);
- CertificateValidationProvider certValidator = new PKIXCertificateValidationProvider(
- CertificateContainer.fromCollection(trustChains.extractRootCertificates()).buildJKSKeyStore(),
- false,
- CertificateContainer.fromCollection(trustChains.extractNotRootCertificates()).buildCertStore());
- XadesVerificationProfile p = new XadesVerificationProfile(certValidator);
- XadesVerifier v = p.newVerifier();
- for (File xasFile : getXasFiles(fileInputDir)) {
- String _msg = "Vérification du fichier " + xasFile.getAbsolutePath();
- logger.info(_msg);
- Message msg = traitement.addMessage(Traitement.SEVERITY_INFO, _msg);
- UnZippedXasFile unZippedXasFile = null;
- try {
- unZippedXasFile = unzipXasFile(xasFile);
- XAdESVerificationResult result = verifyXas(unZippedXasFile, v, trustSigners); // TODO : log result
- FileUtils.moveFileWithOverwrite(unZippedXasFile.getXarFile(), new File(fileOKDir, unZippedXasFile.getXarFile().getName()));
- unZippedXasFile.delete();
- if(shouldArchivateXas)
- FileUtils.moveFileWithOverwrite(xasFile, new File(fileOKArchDir, xasFile.getName()));
- else
- xasFile.delete();
-// _msg = "Le fichier " + xasFile.getAbsolutePath() + " a été vérifié avec succès.";
- logger.info(_msg);
- msg.setInfoCompl(_msg);
- } catch (InvalidXasFileException e) {
- if (unZippedXasFile != null) {
- unZippedXasFile.delete();
- }
- FileUtils.moveFileWithOverwrite(xasFile, new File(fileKODir, xasFile.getName()));
- _msg = "Le fichier " + xasFile.getAbsolutePath() + " est invalide: ";
-// logger.info(_msg, e);
- msg.setSeverity(Traitement.SEVERITY_WARN);
- msg.setInfoCompl(_msg+e.getMessage());
- } catch (Exception e) {
- if (unZippedXasFile != null) {
- unZippedXasFile.delete();
- }
- FileUtils.moveFileWithOverwrite(xasFile, new File(fileKODir, xasFile.getName()));
- _msg = "Le fichier " + xasFile.getAbsolutePath() + " a provoquée une erreur inattendue. ";
- msg.setSeverity(Traitement.SEVERITY_WARN);
- msg.setInfoCompl(_msg+e.getMessage());
- logger.error(_msg, e);
- }
- }
- logger.info("Fin du processus de vérification xas");
- traitement.setEnd(System.currentTimeMillis());
- }
-
- protected static XAdESVerificationResult verifyXas(UnZippedXasFile unizipedXasFile, XadesVerifier verifier, CertificateContainer trustSigners) throws InvalidXasFileException, FileNotFoundException, ParserConfigurationException, SAXException, IOException, TransformerException, CertificateEncodingException, XPathExpressionException {
- if (!unizipedXasFile.getXadFile().exists()) {
- throw new InvalidXasFileException("Fichier de signature non trouvé : " + unizipedXasFile.getXadFile().getName());
- }
- if (!unizipedXasFile.getXarFile().exists()) {
- throw new InvalidXasFileException("Fichier xar non trouvé : " + unizipedXasFile.getXarFile().getName());
- }
-
- javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();
- dbf.setNamespaceAware(true);
- dbf.setAttribute("http://xml.org/sax/features/namespaces", Boolean.TRUE);
- FileInputStream xadInputStream = new FileInputStream(unizipedXasFile.getXadFile());
- try {
- Document document = dbf.newDocumentBuilder().parse(new InputSource(xadInputStream));
- XPathFactory xpf = FactoryProvider.getXPathFactory();
- XPath xp = xpf.newXPath();
- NamespaceContextImpl nsCtx = new NamespaceContextImpl();
- nsCtx.addMapping("ds", SIGNATURE_SPEC_NS);
- xp.setNamespaceContext(nsCtx);
- NodeList sigs = (NodeList)xp.evaluate("//ds:Signature", document, XPathConstants.NODESET);
- if (sigs.getLength() != 1) {
- throw new InvalidXasFileException("Plusieurs signatures ont été détectées dans le fichier xad, interruption de la vérification");
- }
- Element sigElement = (Element) sigs.item(0);
- StdResourceResolver.startSigningProcess(unizipedXasFile.getDirectory());
- try {
- XAdESVerificationResult xaesvr = verifier.verify(sigElement);
- if (trustSigners.contains(xaesvr.getValidationCertificate())) {
- return xaesvr;
- } else {
- throw new InvalidXasFileException("Le certificat du signataire n'a pas été reconnu (" + xaesvr.getValidationCertificate().getSerialNumber() + ")");
- }
- } catch (XAdES4jException xaese) {
- throw new InvalidXasFileException(xaese);
- } finally {
- StdResourceResolver.endSigningProcess();
- }
- } finally {
- xadInputStream.close();
- }
- }
-
- private static File createCleanTempDir(File xasFile) throws IOException {
- File xasDirectory = new File(xasFile + ".tmpDir");
- if (xasDirectory.exists()) {
- if (!xasDirectory.delete()) {
- throw new IOException("Impossible de supprimer le répertoire " + xasDirectory);
- }
- }
- if (!xasDirectory.mkdir()) {
- throw new IOException("Impossible de créer le répertoire " + xasDirectory);
- }
- return xasDirectory;
- }
-
- private static UnZippedXasFile unzipXasFile(File xasFile) throws IOException, InvalidXasFileException {
- File xasDirectory = createCleanTempDir(xasFile);
- ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(xasFile)));
- try {
- try {
- ZipEntry entry = null;
- String xarFilename = null;
- String xadFilename = null;
- while ((entry = zis.getNextEntry()) != null) {
- String outputFilename = xasDirectory + File.separator + entry.getName();
- if (entry.getName().toLowerCase().endsWith(".xar")) {
- if (xarFilename == null) {
- xarFilename = entry.getName();
- } else {
- throw new InvalidXasFileException("L'archive est invalide : elle contient plusieurs fichiers xar");
- }
- }
- if (entry.getName().toLowerCase().endsWith(".xad")) {
- if (xadFilename == null) {
- xadFilename = entry.getName();
- } else {
- throw new InvalidXasFileException("L'archive est invalide : elle contient plusieurs fichiers xad");
- }
- }
- BufferedOutputStream dest = new BufferedOutputStream(new FileOutputStream(outputFilename), BUFFER_SIZE);
- try {
- byte data[] = new byte[BUFFER_SIZE];
- int count;
- while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) {
- dest.write(data, 0, count);
- }
- } finally {
- dest.flush();
- dest.close();
- }
- }
- if (xarFilename == null) {
- throw new InvalidXasFileException("L'archive est invalide : aucun fichier xar trouvé");
- }
- if (xadFilename == null) {
- throw new InvalidXasFileException("L'archive est invalide : aucun fichier xad trouvé");
- }
- return new UnZippedXasFile(xasDirectory, xasFile, new File(xasDirectory, xadFilename), new File(xasDirectory, xarFilename));
- } finally {
- zis.close();
- }
- } catch (IOException ioe) {
- FileUtils.deleteDirectory(xasDirectory);
- throw ioe;
- } catch (InvalidXasFileException ixfe) {
- FileUtils.deleteDirectory(xasDirectory);
- throw ixfe;
- }
- }
-
- private static List<File> getXasFiles(File fileInputDir) {
- return FileUtils.listFiles(fileInputDir, new ByExtensionFileFilter("xas"), new LastModifiedFileComparator());
- }
-
- @Override
- protected void doProcess() throws Exception {
- if(checkNotRunning()) {
- try {
- process(inputDir, okDir, koDir, archiveDir);
- } catch(Exception ex) {
- logger.error("unexpected exception:",ex);
- throw ex;
- } finally {
- clearLock();
- }
- } else {
- traitement.setResume("Dejà en cours d'execution");
- traitement.setEnd(System.currentTimeMillis());
- }
- }
-
- @Override
- public String getResumeTraitement() {
- return "xas.verifier";
- }
-
- @Override
- public String typeTraitementRefCode() {
- return "xas.verifier";
- }
-
- @Override
- public String getInformations() {
- return "Running";
- }
-
- @Override
- protected String getBatchVersion() {
- return "1.0.0";
- }
-
- private boolean checkNotRunning() {
- synchronized(locker) {
- if(instanceCount==0) {
- instanceCount++;
- instance = this;
- return true;
- }
- return false;
- }
- }
- private void clearLock() {
- synchronized(locker) {
- // on ne peut pas supprimer un lock qu'on a pas posé
- if(instance==this) {
- instanceCount--;
- instance=null;
- }
- }
- }
-
- public static class VerifiedUnizipedXasFile {
-
- private UnZippedXasFile unizipedXasFile;
- private XAdESVerificationResult xaesvr;
-
- public VerifiedUnizipedXasFile(UnZippedXasFile unizipedXasFile, XAdESVerificationResult xaesvr) {
- this.unizipedXasFile = unizipedXasFile;
- this.xaesvr = xaesvr;
- }
-
- public UnZippedXasFile getUnizipedXasFile() {
- return unizipedXasFile;
- }
-
- public XAdESVerificationResult getXaesvr() {
- return xaesvr;
- }
- }
-
- public static class UnZippedXasFile {
-
- private File directory;
- private File xasFile;
- private File xadFile;
- private File xarFile;
-
- public UnZippedXasFile(File directory, File xasFile, File xadFile, File xarFile) {
- this.directory = directory;
- this.xasFile = xasFile;
- this.xadFile = xadFile;
- this.xarFile = xarFile;
- }
-
- public File getDirectory() {
- return directory;
- }
-
- public File getXadFile() {
- return xadFile;
- }
-
- public File getXarFile() {
- return xarFile;
- }
-
- public File getXasFile() {
- return xasFile;
- }
-
- public void delete() throws IOException {
- FileUtils.deleteDirectory(directory);
- }
- }
-
- private void checkWriteDirectory(File file) throws ConfigurationException {
- if (!file.exists() || !file.isDirectory() || !file.canWrite()) {
- String message = "Le répertoire " + file.getAbsolutePath() + " doit exister et l'application doit avoir les droits d'écriure";
- traitement.addMessage(Traitement.SEVERITY_ERROR, message);
- throw new ConfigurationException(message);
- }
- }
-
- public static class InvalidXasFileException extends Exception {
-
- public InvalidXasFileException(String message) {
- super(message);
- }
-
- public InvalidXasFileException(XAdES4jException xaese) {
- super(xaese);
- }
- }
-
- public static class ConfigurationException extends Exception {
-
- public ConfigurationException(String message) {
- super(message);
- }
- }
+ private static final Logger logger = Logger.getLogger(XasVerifier.class);
+ public static final String SIGNATURE_SPEC_NS = "http://www.w3.org/2000/09/xmldsig#";
+
+ // pour s'assurer qu'on en fait pas plusieurs à la fois
+ private static int instanceCount = 0;
+ private static Object instance = null;
+ private static final Object locker = new Object();
+
+ public static final String BATCH_NAME = "xas.verifier";
+ static {
+ ResourceResolver.init();
+ ResourceResolver.register(StdResourceResolver.class.getName());
+ }
+ private static int BUFFER_SIZE = 2048;
+ public static final String INPUT_DIR_PROPNAME = "cft.dir";
+ public static final String FILE_OK_PROPNAME = "xar.out";
+ public static final String FILE_KO_PROPNAME = "xas.error";
+ public static final String FILE_ARCH_PROPNAME = "arch.dir";
+ public static final String ARCH_PROPNAME = "archive.xas";
+
+ private boolean shouldArchivateXas = false;
+ private File archiveDir = null;
+ private File inputDir = null;
+ private File okDir = null;
+ private File koDir = null;
+
+ private final Traitement traitement;
+
+ public XasVerifier(final String args[]) {
+ super(args);
+ // on vérifie que le machin ne tourne pas, et sinon on quitte
+ this.traitement = new Traitement(XasVerifier.class, BATCH_NAME);
+ }
+
+ @Override
+ protected void initialize() throws Exception {
+ final String inputDirFileName = this.getProps().getProperty(INPUT_DIR_PROPNAME);
+ final String fileOkDirName = this.getProps().getProperty(FILE_OK_PROPNAME);
+ final String fileKODirName = this.getProps().getProperty(FILE_KO_PROPNAME);
+ final String fileOkArchDirName = this.getProps().getProperty(FILE_ARCH_PROPNAME);
+ final String archivateXas = this.getProps().getProperty(ARCH_PROPNAME, "false");
+ this.shouldArchivateXas = Boolean.parseBoolean(archivateXas);
+ if (this.shouldArchivateXas) {
+ this.archiveDir = new File(fileOkArchDirName);
+ }
+ this.inputDir = new File(inputDirFileName);
+ this.okDir = new File(fileOkDirName);
+ this.koDir = new File(fileKODirName);
+ this.checkWriteDirectory(this.inputDir);
+ this.checkWriteDirectory(this.okDir);
+ this.checkWriteDirectory(this.koDir);
+ if (this.shouldArchivateXas) {
+ this.checkWriteDirectory(this.archiveDir);
+ }
+ }
+
+ protected void process(final File fileInputDir, final File fileOKDir, final File fileKODir, final File fileOKArchDir) throws Exception {
+ // logger.info(" Répertoire d'entrée : " + fileInputDir.getAbsolutePath());
+ // logger.info(" Répertoire de sortie OK : " + fileOKDir.getAbsolutePath());
+ // logger.info(" Répertoire de sortie KO : " + fileKODir.getAbsolutePath());
+ // if(fileOKArchDir!=null)
+ // logger.info(" Répertoire d'archivage XAS OK : " + fileOKArchDir.getAbsolutePath());
+ // else
+ // logger.info(" Pas d'archivqge des XAS");
+ final URL tcUrl = XasVerifier.class.getResource("/fr/gouv/finances/dgfip/xemelios/batch/chaineimport/xasverifier/resources/trustChains.zip");
+ final CertificateContainer trustChains = CertificateContainer.fromZipURL(tcUrl);
+ final URL tsUrl = XasVerifier.class.getResource("/fr/gouv/finances/dgfip/xemelios/batch/chaineimport/xasverifier/resources/trustSigners.zip");
+ final CertificateContainer trustSigners = CertificateContainer.fromZipURL(tsUrl);
+ final CertificateValidationProvider certValidator = new PKIXCertificateValidationProvider(CertificateContainer.fromCollection(trustChains.extractRootCertificates()).buildJKSKeyStore(), false, CertificateContainer.fromCollection(trustChains.extractNotRootCertificates()).buildCertStore());
+ final XadesVerificationProfile p = new XadesVerificationProfile(certValidator);
+ final XadesVerifier v = p.newVerifier();
+ for (final File xasFile : getXasFiles(fileInputDir)) {
+ String _msg = "Vérification du fichier " + xasFile.getAbsolutePath();
+ logger.info(_msg);
+ final Message msg = this.traitement.addMessage(Traitement.SEVERITY_INFO, _msg);
+ UnZippedXasFile unZippedXasFile = null;
+ try {
+ unZippedXasFile = unzipXasFile(xasFile);
+ final XAdESVerificationResult result = verifyXas(unZippedXasFile, v, trustSigners); // TODO : log result
+ FileUtils.moveFileWithOverwrite(unZippedXasFile.getXarFile(), new File(fileOKDir, unZippedXasFile.getXarFile().getName()));
+ unZippedXasFile.delete();
+ if (this.shouldArchivateXas) {
+ FileUtils.moveFileWithOverwrite(xasFile, new File(fileOKArchDir, xasFile.getName()));
+ } else {
+ xasFile.delete();
+ }
+ // _msg = "Le fichier " + xasFile.getAbsolutePath() + " a été vérifié avec succès.";
+ logger.info(_msg);
+ msg.setInfoCompl(_msg);
+ } catch (final InvalidXasFileException e) {
+ if (unZippedXasFile != null) {
+ unZippedXasFile.delete();
+ }
+ FileUtils.moveFileWithOverwrite(xasFile, new File(fileKODir, xasFile.getName()));
+ _msg = "Le fichier " + xasFile.getAbsolutePath() + " est invalide: ";
+ // logger.info(_msg, e);
+ msg.setSeverity(Traitement.SEVERITY_WARN);
+ msg.setInfoCompl(_msg + e.getMessage());
+ } catch (final Exception e) {
+ if (unZippedXasFile != null) {
+ unZippedXasFile.delete();
+ }
+ FileUtils.moveFileWithOverwrite(xasFile, new File(fileKODir, xasFile.getName()));
+ _msg = "Le fichier " + xasFile.getAbsolutePath() + " a provoquée une erreur inattendue. ";
+ msg.setSeverity(Traitement.SEVERITY_WARN);
+ msg.setInfoCompl(_msg + e.getMessage());
+ logger.error(_msg, e);
+ }
+ }
+ logger.info("Fin du processus de vérification xas");
+ this.traitement.setEnd(System.currentTimeMillis());
+ }
+
+ protected static XAdESVerificationResult verifyXas(final UnZippedXasFile unizipedXasFile, final XadesVerifier verifier, final CertificateContainer trustSigners) throws InvalidXasFileException, FileNotFoundException, ParserConfigurationException, SAXException, IOException, TransformerException,
+ CertificateEncodingException, XPathExpressionException {
+ if (!unizipedXasFile.getXadFile().exists()) {
+ throw new InvalidXasFileException("Fichier de signature non trouvé : " + unizipedXasFile.getXadFile().getName());
+ }
+ if (!unizipedXasFile.getXarFile().exists()) {
+ throw new InvalidXasFileException("Fichier xar non trouvé : " + unizipedXasFile.getXarFile().getName());
+ }
+
+ final javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();
+ dbf.setNamespaceAware(true);
+ dbf.setAttribute("http://xml.org/sax/features/namespaces", Boolean.TRUE);
+ final FileInputStream xadInputStream = new FileInputStream(unizipedXasFile.getXadFile());
+ try {
+ final Document document = dbf.newDocumentBuilder().parse(new InputSource(xadInputStream));
+ final XPathFactory xpf = FactoryProvider.getXPathFactory();
+ final XPath xp = xpf.newXPath();
+ final NamespaceContextImpl nsCtx = new NamespaceContextImpl();
+ nsCtx.addMapping("ds", SIGNATURE_SPEC_NS);
+ xp.setNamespaceContext(nsCtx);
+ final NodeList sigs = (NodeList) xp.evaluate("//ds:Signature", document, XPathConstants.NODESET);
+ if (sigs.getLength() != 1) {
+ throw new InvalidXasFileException("Plusieurs signatures ont été détectées dans le fichier xad, interruption de la vérification");
+ }
+ final Element sigElement = (Element) sigs.item(0);
+ StdResourceResolver.startSigningProcess(unizipedXasFile.getDirectory());
+ try {
+ final XAdESVerificationResult xaesvr = verifier.verify(sigElement);
+ if (trustSigners.contains(xaesvr.getValidationCertificate())) {
+ return xaesvr;
+ } else {
+ throw new InvalidXasFileException("Le certificat du signataire n'a pas été reconnu (" + xaesvr.getValidationCertificate().getSerialNumber() + ")");
+ }
+ } catch (final XAdES4jException xaese) {
+ throw new InvalidXasFileException(xaese);
+ } finally {
+ StdResourceResolver.endSigningProcess();
+ }
+ } finally {
+ xadInputStream.close();
+ }
+ }
+
+ private static File createCleanTempDir(final File xasFile) throws IOException {
+ final File xasDirectory = new File(xasFile + ".tmpDir");
+ if (xasDirectory.exists()) {
+ if (!xasDirectory.delete()) {
+ throw new IOException("Impossible de supprimer le répertoire " + xasDirectory);
+ }
+ }
+ if (!xasDirectory.mkdir()) {
+ throw new IOException("Impossible de créer le répertoire " + xasDirectory);
+ }
+ return xasDirectory;
+ }
+
+ private static UnZippedXasFile unzipXasFile(final File xasFile) throws IOException, InvalidXasFileException {
+ final File xasDirectory = createCleanTempDir(xasFile);
+ final ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(xasFile)));
+ try {
+ try {
+ ZipEntry entry = null;
+ String xarFilename = null;
+ String xadFilename = null;
+ while ((entry = zis.getNextEntry()) != null) {
+ final String outputFilename = xasDirectory + File.separator + entry.getName();
+ if (entry.getName().toLowerCase().endsWith(".xar")) {
+ if (xarFilename == null) {
+ xarFilename = entry.getName();
+ } else {
+ throw new InvalidXasFileException("L'archive est invalide : elle contient plusieurs fichiers xar");
+ }
+ }
+ if (entry.getName().toLowerCase().endsWith(".xad")) {
+ if (xadFilename == null) {
+ xadFilename = entry.getName();
+ } else {
+ throw new InvalidXasFileException("L'archive est invalide : elle contient plusieurs fichiers xad");
+ }
+ }
+ final BufferedOutputStream dest = new BufferedOutputStream(new FileOutputStream(outputFilename), BUFFER_SIZE);
+ try {
+ final byte data[] = new byte[BUFFER_SIZE];
+ int count;
+ while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) {
+ dest.write(data, 0, count);
+ }
+ } finally {
+ dest.flush();
+ dest.close();
+ }
+ }
+ if (xarFilename == null) {
+ throw new InvalidXasFileException("L'archive est invalide : aucun fichier xar trouvé");
+ }
+ if (xadFilename == null) {
+ throw new InvalidXasFileException("L'archive est invalide : aucun fichier xad trouvé");
+ }
+ return new UnZippedXasFile(xasDirectory, xasFile, new File(xasDirectory, xadFilename), new File(xasDirectory, xarFilename));
+ } finally {
+ zis.close();
+ }
+ } catch (final IOException ioe) {
+ FileUtils.deleteDirectory(xasDirectory);
+ throw ioe;
+ } catch (final InvalidXasFileException ixfe) {
+ FileUtils.deleteDirectory(xasDirectory);
+ throw ixfe;
+ }
+ }
+
+ private static List<File> getXasFiles(final File fileInputDir) {
+ return FileUtils.listFiles(fileInputDir, new ByExtensionFileFilter("xas"), new LastModifiedFileComparator());
+ }
+
+ @Override
+ protected void doProcess() throws Exception {
+ if (this.checkNotRunning()) {
+ try {
+ this.process(this.inputDir, this.okDir, this.koDir, this.archiveDir);
+ } catch (final Exception ex) {
+ logger.error("unexpected exception:", ex);
+ throw ex;
+ } finally {
+ this.clearLock();
+ }
+ } else {
+ this.traitement.setResume("Dejà en cours d'execution");
+ this.traitement.setEnd(System.currentTimeMillis());
+ }
+ }
+
+ @Override
+ public String getResumeTraitement() {
+ return "xas.verifier";
+ }
+
+ @Override
+ public String typeTraitementRefCode() {
+ return "xas.verifier";
+ }
+
+ @Override
+ public String getInformations() {
+ return "Running";
+ }
+
+ @Override
+ protected String getBatchVersion() {
+ return "1.0.0";
+ }
+
+ private boolean checkNotRunning() {
+ synchronized (locker) {
+ if (instanceCount == 0) {
+ instanceCount++;
+ instance = this;
+ return true;
+ }
+ return false;
+ }
+ }
+
+ private void clearLock() {
+ synchronized (locker) {
+ // on ne peut pas supprimer un lock qu'on a pas posé
+ if (instance == this) {
+ instanceCount--;
+ instance = null;
+ }
+ }
+ }
+
+ public static class VerifiedUnizipedXasFile {
+
+ private final UnZippedXasFile unizipedXasFile;
+ private final XAdESVerificationResult xaesvr;
+
+ public VerifiedUnizipedXasFile(final UnZippedXasFile unizipedXasFile, final XAdESVerificationResult xaesvr) {
+ this.unizipedXasFile = unizipedXasFile;
+ this.xaesvr = xaesvr;
+ }
+
+ public UnZippedXasFile getUnizipedXasFile() {
+ return this.unizipedXasFile;
+ }
+
+ public XAdESVerificationResult getXaesvr() {
+ return this.xaesvr;
+ }
+ }
+
+ public static class UnZippedXasFile {
+
+ private final File directory;
+ private final File xasFile;
+ private final File xadFile;
+ private final File xarFile;
+
+ public UnZippedXasFile(final File directory, final File xasFile, final File xadFile, final File xarFile) {
+ this.directory = directory;
+ this.xasFile = xasFile;
+ this.xadFile = xadFile;
+ this.xarFile = xarFile;
+ }
+
+ public File getDirectory() {
+ return this.directory;
+ }
+
+ public File getXadFile() {
+ return this.xadFile;
+ }
+
+ public File getXarFile() {
+ return this.xarFile;
+ }
+
+ public File getXasFile() {
+ return this.xasFile;
+ }
+
+ public void delete() throws IOException {
+ FileUtils.deleteDirectory(this.directory);
+ }
+ }
+
+ private void checkWriteDirectory(final File file) throws ConfigurationException {
+ if (!file.exists() || !file.isDirectory() || !file.canWrite()) {
+ final String message = "Le répertoire " + file.getAbsolutePath() + " doit exister et l'application doit avoir les droits d'écriure";
+ this.traitement.addMessage(Traitement.SEVERITY_ERROR, message);
+ throw new ConfigurationException(message);
+ }
+ }
+
+ public static class InvalidXasFileException extends Exception {
+
+ public InvalidXasFileException(final String message) {
+ super(message);
+ }
+
+ public InvalidXasFileException(final XAdES4jException xaese) {
+ super(xaese);
+ }
+ }
+
+ public static class ConfigurationException extends Exception {
+
+ public ConfigurationException(final String message) {
+ super(message);
+ }
+ }
}
package fr.gouv.finances.dgfip.xemelios.batch.exports;
-import fr.gouv.finances.dgfip.xemelios.batch.Batch;
-import fr.gouv.finances.dgfip.xemelios.data.DataLayerManager;
-import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
-import fr.gouv.finances.dgfip.xemelios.export.ExportJob;
-import fr.gouv.finances.dgfip.xemelios.utils.FileUtils;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
+
import org.apache.log4j.Logger;
+import fr.gouv.finances.dgfip.xemelios.batch.Batch;
+import fr.gouv.finances.dgfip.xemelios.data.DataLayerManager;
+import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
+import fr.gouv.finances.dgfip.xemelios.export.ExportJob;
+import fr.gouv.finances.dgfip.xemelios.utils.FileUtils;
+
/**
- *
+ *
* @author Christophe Marchand <christophe.marchand@axyus.com>
*/
public class ExportCleaner extends Batch {
- private static final Logger logger = Logger.getLogger(ExportCleaner.class);
-
- public static final String PROP_MAX_CONCURRENT_THREADS = "max.concurrent.threads";
- public static final String PROP_CLEAN_DELAY = "clean.delay";
- private int maxConcurrentThreads = 1;
- private int cleanDelay = 14;
-
- private ThreadPool<ExportCleanerImpl> pool = null;
-
- public ExportCleaner(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) {}
- }
- sTmp = getProps().getProperty(PROP_CLEAN_DELAY);
- if(sTmp!=null && sTmp.length()>0) {
- try {
- cleanDelay = Integer.parseInt(sTmp);
- } catch(Throwable t) {}
- }
- }
-
- @Override
- protected void doProcess() throws Exception {
- Connection con = null;
- try {
- con = PoolManager.getInstance().getConnection();
- ArrayList<ExportCleanerImpl> threads = new ArrayList<ExportCleanerImpl>();
- if("mysql".equals(DataLayerManager.getImplementation().getLayerName())) {
- StringBuffer sql = new StringBuffer("SELECT EXPORT_ID, UTI_LOGIN FROM EXPORT_JOBS WHERE STATUS IN (");
- sql.append(ExportJob.STATUS_DOWNLOADED).append(",").append(ExportJob.STATUS_ERROR).append(",").append(ExportJob.STATUS_TERMINATED);
- sql.append(") AND TO_DAYS(NOW()) - TO_DAYS(SUBMIT_DATE) > ").append(cleanDelay).append(" AND CLEANED IS NULL");
- ResultSet rs = con.createStatement().executeQuery(sql.toString());
- while(rs.next()) {
- long exportId = rs.getLong(1);
- String user = rs.getString(2);
- threads.add(new ExportCleanerImpl(exportId,user));
- }
- } else if("oracle".equals(DataLayerManager.getImplementation().getLayerName())) {
- StringBuffer sql = new StringBuffer("SELECT EXPORT_ID, UTI_LOGIN FROM EXPORT_JOBS WHERE EXPORT_STATUS IN (");
- sql.append(ExportJob.STATUS_DOWNLOADED).append(",").append(ExportJob.STATUS_ERROR).append(",").append(ExportJob.STATUS_TERMINATED);
- sql.append(") AND trunc(SYSDATE) - trunc(SUBMIT_DATE) > ").append(cleanDelay).append(" AND CLEANED IS NULL");
- ResultSet rs = con.createStatement().executeQuery(sql.toString());
- while(rs.next()) {
- long exportId = rs.getLong(1);
- String user = rs.getString(2);
- threads.add(new ExportCleanerImpl(exportId,user));
- }
- }
- PoolManager.getInstance().releaseConnection(con); con=null;
- pool = new ThreadPool<ExportCleanerImpl>(threads);
- pool.start(maxConcurrentThreads);
- } catch(Exception ex) {
- logger.error("doProcess(): ",ex);
- } finally {
- if(con!=null) {
- PoolManager.getInstance().releaseConnection(con);
- }
- }
- }
-
- @Override
- public String getResumeTraitement() {
- return "export.cleaner.resume";
- }
-
- @Override
- public String typeTraitementRefCode() {
- return "export.cleaner";
- }
-
- @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 ExportCleanerImpl extends RunnableBatch {
- private Long exportId;
- private String user;
-
- public ExportCleanerImpl(Long exportId, String user) {
- super();
- this.exportId=exportId;
- this.user=user;
- setName("ExportCleanerImpl<"+exportId+","+user+">");
- }
-
- @Override
- public void run() {
- logger.debug("cleaning export "+exportId+" from "+user);
- Connection con = null;
- try {
- con = PoolManager.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("SELECT GENERATED_FILE_NAME FROM EXPORT_JOBS WHERE EXPORT_ID=? AND UTI_LOGIN=?");
- ps.setLong(1,exportId);
- ps.setString(2,user);
- ResultSet rs= ps.executeQuery();
- if(rs.next()) {
- String generatedFileName = rs.getString(1);
- if(rs.wasNull()) generatedFileName = null;
- clean(generatedFileName);
- ps = con.prepareStatement("UPDATE EXPORT_JOBS SET CLEANED=1 WHERE EXPORT_ID=? AND UTI_LOGIN=?");
- ps.setLong(1,exportId);
- ps.setString(2,user);
- ps.executeUpdate();
- } else {
- logger.warn("export to clean not found !");
- }
- } catch(Exception ex) {
- logger.error("ExportCleanerImpl.run(): ",ex);
- } finally {
- if(con!=null) {
- PoolManager.getInstance().releaseConnection(con);
- }
- getNotifier().notifyThreadStop(this);
- }
- }
- protected void clean(String fileName) {
- if(fileName==null) return;
- File f = new File(fileName);
- if(f.exists()) {
- File dir = f.getParentFile();
- try {
- FileUtils.dropRecursiveDirectory(dir);
- } catch(Exception ex) {}
- }
- }
-
- @Override
- public String getInformations() {
- if(isAlive()) return "\t".concat(getName()).concat(": running\n");
- else return "\t".concat(getName()).concat(": idle\n");
- }
-
- }
+ private static final Logger logger = Logger.getLogger(ExportCleaner.class);
+
+ public static final String PROP_MAX_CONCURRENT_THREADS = "max.concurrent.threads";
+ public static final String PROP_CLEAN_DELAY = "clean.delay";
+ private int maxConcurrentThreads = 1;
+ private int cleanDelay = 14;
+
+ private ThreadPool<ExportCleanerImpl> pool = null;
+
+ public ExportCleaner(final String[] args) {
+ super(args);
+ }
+
+ @Override
+ protected void initialize() throws Exception {
+ super.initialize();
+ 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) {
+ }
+ }
+ sTmp = this.getProps().getProperty(PROP_CLEAN_DELAY);
+ if (sTmp != null && sTmp.length() > 0) {
+ try {
+ this.cleanDelay = Integer.parseInt(sTmp);
+ } catch (final Throwable t) {
+ }
+ }
+ }
+
+ @Override
+ protected void doProcess() throws Exception {
+ Connection con = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ final ArrayList<ExportCleanerImpl> threads = new ArrayList<ExportCleanerImpl>();
+ if ("mysql".equals(DataLayerManager.getImplementation().getLayerName())) {
+ final StringBuffer sql = new StringBuffer("SELECT EXPORT_ID, UTI_LOGIN FROM EXPORT_JOBS WHERE STATUS IN (");
+ sql.append(ExportJob.STATUS_DOWNLOADED).append(",").append(ExportJob.STATUS_ERROR).append(",").append(ExportJob.STATUS_TERMINATED);
+ sql.append(") AND TO_DAYS(NOW()) - TO_DAYS(SUBMIT_DATE) > ").append(this.cleanDelay).append(" AND CLEANED IS NULL");
+ final ResultSet rs = con.createStatement().executeQuery(sql.toString());
+ while (rs.next()) {
+ final long exportId = rs.getLong(1);
+ final String user = rs.getString(2);
+ threads.add(new ExportCleanerImpl(exportId, user));
+ }
+ } else if ("oracle".equals(DataLayerManager.getImplementation().getLayerName())) {
+ final StringBuffer sql = new StringBuffer("SELECT EXPORT_ID, UTI_LOGIN FROM EXPORT_JOBS WHERE EXPORT_STATUS IN (");
+ sql.append(ExportJob.STATUS_DOWNLOADED).append(",").append(ExportJob.STATUS_ERROR).append(",").append(ExportJob.STATUS_TERMINATED);
+ sql.append(") AND trunc(SYSDATE) - trunc(SUBMIT_DATE) > ").append(this.cleanDelay).append(" AND CLEANED IS NULL");
+ final ResultSet rs = con.createStatement().executeQuery(sql.toString());
+ while (rs.next()) {
+ final long exportId = rs.getLong(1);
+ final String user = rs.getString(2);
+ threads.add(new ExportCleanerImpl(exportId, user));
+ }
+ }
+ PoolManager.getInstance().releaseConnection(con);
+ con = null;
+ this.pool = new ThreadPool<ExportCleanerImpl>(threads);
+ this.pool.start(this.maxConcurrentThreads);
+ } catch (final Exception ex) {
+ logger.error("doProcess(): ", ex);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ }
+ }
+
+ @Override
+ public String getResumeTraitement() {
+ return "export.cleaner.resume";
+ }
+
+ @Override
+ public String typeTraitementRefCode() {
+ return "export.cleaner";
+ }
+
+ @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 ExportCleanerImpl extends RunnableBatch {
+ private final Long exportId;
+ private final String user;
+
+ public ExportCleanerImpl(final Long exportId, final String user) {
+ super();
+ this.exportId = exportId;
+ this.user = user;
+ this.setName("ExportCleanerImpl<" + exportId + "," + user + ">");
+ }
+
+ @Override
+ public void run() {
+ logger.debug("cleaning export " + this.exportId + " from " + this.user);
+ Connection con = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ PreparedStatement ps = con.prepareStatement("SELECT GENERATED_FILE_NAME FROM EXPORT_JOBS WHERE EXPORT_ID=? AND UTI_LOGIN=?");
+ ps.setLong(1, this.exportId);
+ ps.setString(2, this.user);
+ final ResultSet rs = ps.executeQuery();
+ if (rs.next()) {
+ String generatedFileName = rs.getString(1);
+ if (rs.wasNull()) {
+ generatedFileName = null;
+ }
+ this.clean(generatedFileName);
+ ps = con.prepareStatement("UPDATE EXPORT_JOBS SET CLEANED=1 WHERE EXPORT_ID=? AND UTI_LOGIN=?");
+ ps.setLong(1, this.exportId);
+ ps.setString(2, this.user);
+ ps.executeUpdate();
+ } else {
+ logger.warn("export to clean not found !");
+ }
+ } catch (final Exception ex) {
+ logger.error("ExportCleanerImpl.run(): ", ex);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ this.getNotifier().notifyThreadStop(this);
+ }
+ }
+
+ protected void clean(final String fileName) {
+ if (fileName == null) {
+ return;
+ }
+ final File f = new File(fileName);
+ if (f.exists()) {
+ final File dir = f.getParentFile();
+ try {
+ FileUtils.dropRecursiveDirectory(dir);
+ } catch (final Exception ex) {
+ }
+ }
+ }
+
+ @Override
+ public String getInformations() {
+ if (this.isAlive()) {
+ return "\t".concat(this.getName()).concat(": running\n");
+ } else {
+ return "\t".concat(this.getName()).concat(": idle\n");
+ }
+ }
+
+ }
}
package fr.gouv.finances.dgfip.xemelios.batch.exports;
-import fr.gouv.finances.dgfip.xemelios.batch.Batch;
-import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
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 org.apache.log4j.Logger;
+import fr.gouv.finances.dgfip.xemelios.batch.Batch;
+import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
+
/**
- *
+ *
* @author Christophe Marchand <christophe.marchand@axyus.com>
*/
public class ExportEvaluator extends Batch {
- private static final Logger logger = Logger.getLogger(ExportEvaluator.class);
- private static final int EXPORT_MAX_SIZE = 1000;
- private static final int EXPORT_P1_MAX_SIZE = 10;
- private static final int EXPORT_P2_MAX_SIZE = 100;
- public static final int MAX_CONCURRENT_THREADS = 3;
-
- public static final String PROP_EXP_MAX_SIZE = "export.max.size";
- public static final String PROP_P1_MAX_SIZE = "export.p1.max.size";
- public static final String PROP_P2_MAX_SIZE = "export.p2.max.size";
- public static final String PROP_MAX_CONCURRENT_THREADS = "max.concurrent.threads";
-
- private int maxConcurrentThreads = MAX_CONCURRENT_THREADS;
- private int p1MaxSize = EXPORT_P1_MAX_SIZE;
- private int p2MaxSize = EXPORT_P2_MAX_SIZE;
- private int maxSize = EXPORT_MAX_SIZE;
-
- private ThreadPool<ExportEvaluatorImpl> pool=null;
-
- public ExportEvaluator(String args[]) {
- super(args);
- }
-
- @Override
- protected void initialize() throws Exception {
- super.initialize();
- try {
- String s = getProps().getProperty(PROP_EXP_MAX_SIZE);
- if(s!=null && s.length()>0) {
- maxSize = Integer.parseInt(s);
- }
- } catch(Throwable t) {}
- try {
- String s = getProps().getProperty(PROP_P1_MAX_SIZE);
- if(s!=null && s.length()>0) {
- p1MaxSize = Integer.parseInt(s);
- }
- } catch(Throwable t) {}
- try {
- String s = getProps().getProperty(PROP_P2_MAX_SIZE);
- if(s!=null && s.length()>0) {
- p2MaxSize = Integer.parseInt(s);
- }
- } catch(Throwable t) {}
- String sTmp = getProps().getProperty(PROP_MAX_CONCURRENT_THREADS);
- if(sTmp!=null && sTmp.length()>0) {
- try {
- maxConcurrentThreads = Integer.parseInt(sTmp);
- } catch(Throwable t) {}
- }
- }
-
-
-
- @Override
- protected void doProcess() throws Exception {
- Connection con = null;
- Statement st = null;
- ResultSet rs = null;
- ArrayList<ExportEvaluatorImpl> threads = new ArrayList<ExportEvaluatorImpl>();
- 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()) {
- String exportTempDirectory = rs.getString(1);
- rs.close();
- } else {
- logger.error("Parametre 'export.temp.directory' not set !");
- throw new Exception("Parametre : 'export.temp.directory' not set !");
- }
- String sql = "SELECT EXPORT_ID, UTI_LOGIN FROM EXPORT_JOBS WHERE EXPORT_STATUS=0";
- rs = st.executeQuery(sql);
- while(rs.next()) {
- threads.add(new ExportEvaluatorImpl(rs.getLong(1),rs.getString(2),p1MaxSize,p2MaxSize,maxSize));
- }
- PoolManager.getInstance().releaseConnection(con); con=null;
- pool = new ThreadPool<ExportEvaluatorImpl>(threads);
- pool.start(maxConcurrentThreads);
- } catch(SQLException sqlEx) {
- // TODO
- } finally {
- if(con!=null) PoolManager.getInstance().releaseConnection(con);
- }
- }
-
- @Override
- public String getResumeTraitement() {
- return "export.evaluator.resume";
- }
-
- @Override
- public String typeTraitementRefCode() {
- return "export.evaluator";
- }
-
- @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 ExportEvaluatorImpl extends RunnableBatch {
- private final Logger logger = Logger.getLogger(ExportEvaluatorImpl.class);
- private Long exportId;
- private String user;
- private int p1MaxSize, p2MaxSize, maxSize;
- public ExportEvaluatorImpl(Long exportId,String user, int p1MaxSize, int p2MaxSize, int maxSize) {
- this.exportId=exportId;
- this.user=user;
- this.p1MaxSize=p1MaxSize;
- this.p2MaxSize=p2MaxSize;
- this.maxSize=maxSize;
- setName("ExportEvaluatorImpl<"+exportId+">");
- }
-
- @Override
- @SuppressWarnings("unchecked")
- public void run() {
- Connection con = null;
- try {
- int priority = -1;
- String errorMessage=null;
- con = PoolManager.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("SELECT EXPORT_SIZE FROM EXPORT_JOBS WHERE EXPORT_ID=? AND UTI_LOGIN=?");
- ps.setLong(1,exportId);
- ps.setString(2,user);
- ResultSet rs = ps.executeQuery();
- if(rs.next()) {
- int size = rs.getInt(1);
- if(size<p1MaxSize) priority = 1;
- else if(size<p2MaxSize) priority = 2;
- else if(size<maxSize) priority = 3;
- else errorMessage="Il y a trop de données à exporter pour réaliser cet export ; les seuils sont "+p1MaxSize+","+p2MaxSize+","+maxSize+".";
- } else {
- errorMessage = "Erreur inattendue";
- }
- logger.debug("Avant IF Update");
- if(priority>0) {
- logger.debug("Début IF priority>0");
- ps = con.prepareStatement("UPDATE EXPORT_JOBS SET PRIORITY=?, EXPORT_STATUS=1 WHERE EXPORT_ID=? AND UTI_LOGIN=?");
- ps.setInt(1,priority);
- ps.setLong(2,exportId);
- ps.setString(3,user);
- ps.executeUpdate();
- logger.debug("Fin IF priority>0");
- } else {
- logger.debug("Début Else");
- ps = con.prepareStatement("UPDATE EXPORT_JOBS SET ERROR_MESSAGE=?, EXPORT_STATUS=5 WHERE EXPORT_ID=?");
- ps.setString(1,errorMessage);
- ps.setLong(2, exportId);
- ps.executeUpdate();
- logger.debug("Fin Else");
- }
- } catch(SQLException sqlEx) {
- logger.error("run<"+exportId+">()", sqlEx);
- }catch(Throwable t) {
- logger.error("run<"+exportId+">()", t);
- } finally {
- if(con!=null) PoolManager.getInstance().releaseConnection(con);
- getNotifier().notifyThreadStop(this);
- }
- }
-
- @Override
- public String getInformations() {
- return "\tevaluating export :".concat(Long.toString(exportId)).concat("\n");
- }
- }
+ private static final Logger logger = Logger.getLogger(ExportEvaluator.class);
+ private static final int EXPORT_MAX_SIZE = 1000;
+ private static final int EXPORT_P1_MAX_SIZE = 10;
+ private static final int EXPORT_P2_MAX_SIZE = 100;
+ public static final int MAX_CONCURRENT_THREADS = 3;
+
+ public static final String PROP_EXP_MAX_SIZE = "export.max.size";
+ public static final String PROP_P1_MAX_SIZE = "export.p1.max.size";
+ public static final String PROP_P2_MAX_SIZE = "export.p2.max.size";
+ public static final String PROP_MAX_CONCURRENT_THREADS = "max.concurrent.threads";
+
+ private int maxConcurrentThreads = MAX_CONCURRENT_THREADS;
+ private int p1MaxSize = EXPORT_P1_MAX_SIZE;
+ private int p2MaxSize = EXPORT_P2_MAX_SIZE;
+ private int maxSize = EXPORT_MAX_SIZE;
+
+ private ThreadPool<ExportEvaluatorImpl> pool = null;
+
+ public ExportEvaluator(final String args[]) {
+ super(args);
+ }
+
+ @Override
+ protected void initialize() throws Exception {
+ super.initialize();
+ try {
+ final String s = this.getProps().getProperty(PROP_EXP_MAX_SIZE);
+ if (s != null && s.length() > 0) {
+ this.maxSize = Integer.parseInt(s);
+ }
+ } catch (final Throwable t) {
+ }
+ try {
+ final String s = this.getProps().getProperty(PROP_P1_MAX_SIZE);
+ if (s != null && s.length() > 0) {
+ this.p1MaxSize = Integer.parseInt(s);
+ }
+ } catch (final Throwable t) {
+ }
+ try {
+ final String s = this.getProps().getProperty(PROP_P2_MAX_SIZE);
+ if (s != null && s.length() > 0) {
+ this.p2MaxSize = Integer.parseInt(s);
+ }
+ } catch (final Throwable t) {
+ }
+ 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) {
+ }
+ }
+ }
+
+ @Override
+ protected void doProcess() throws Exception {
+ Connection con = null;
+ Statement st = null;
+ ResultSet rs = null;
+ final ArrayList<ExportEvaluatorImpl> threads = new ArrayList<ExportEvaluatorImpl>();
+ 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()) {
+ final String exportTempDirectory = rs.getString(1);
+ rs.close();
+ } else {
+ logger.error("Parametre 'export.temp.directory' not set !");
+ throw new Exception("Parametre : 'export.temp.directory' not set !");
+ }
+ final String sql = "SELECT EXPORT_ID, UTI_LOGIN FROM EXPORT_JOBS WHERE EXPORT_STATUS=0";
+ rs = st.executeQuery(sql);
+ while (rs.next()) {
+ threads.add(new ExportEvaluatorImpl(rs.getLong(1), rs.getString(2), this.p1MaxSize, this.p2MaxSize, this.maxSize));
+ }
+ PoolManager.getInstance().releaseConnection(con);
+ con = null;
+ this.pool = new ThreadPool<ExportEvaluatorImpl>(threads);
+ this.pool.start(this.maxConcurrentThreads);
+ } catch (final SQLException sqlEx) {
+ // TODO
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ }
+ }
+
+ @Override
+ public String getResumeTraitement() {
+ return "export.evaluator.resume";
+ }
+
+ @Override
+ public String typeTraitementRefCode() {
+ return "export.evaluator";
+ }
+
+ @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 ExportEvaluatorImpl extends RunnableBatch {
+ private final Logger logger = Logger.getLogger(ExportEvaluatorImpl.class);
+ private final Long exportId;
+ private final String user;
+ private final int p1MaxSize, p2MaxSize, maxSize;
+
+ public ExportEvaluatorImpl(final Long exportId, final String user, final int p1MaxSize, final int p2MaxSize, final int maxSize) {
+ this.exportId = exportId;
+ this.user = user;
+ this.p1MaxSize = p1MaxSize;
+ this.p2MaxSize = p2MaxSize;
+ this.maxSize = maxSize;
+ this.setName("ExportEvaluatorImpl<" + exportId + ">");
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public void run() {
+ Connection con = null;
+ try {
+ int priority = -1;
+ String errorMessage = null;
+ con = PoolManager.getInstance().getConnection();
+ PreparedStatement ps = con.prepareStatement("SELECT EXPORT_SIZE FROM EXPORT_JOBS WHERE EXPORT_ID=? AND UTI_LOGIN=?");
+ ps.setLong(1, this.exportId);
+ ps.setString(2, this.user);
+ final ResultSet rs = ps.executeQuery();
+ if (rs.next()) {
+ final int size = rs.getInt(1);
+ if (size < this.p1MaxSize) {
+ priority = 1;
+ } else if (size < this.p2MaxSize) {
+ priority = 2;
+ } else if (size < this.maxSize) {
+ priority = 3;
+ } else {
+ errorMessage = "Il y a trop de données à exporter pour réaliser cet export ; les seuils sont " + this.p1MaxSize + "," + this.p2MaxSize + "," + this.maxSize + ".";
+ }
+ } else {
+ errorMessage = "Erreur inattendue";
+ }
+ this.logger.debug("Avant IF Update");
+ if (priority > 0) {
+ this.logger.debug("Début IF priority>0");
+ ps = con.prepareStatement("UPDATE EXPORT_JOBS SET PRIORITY=?, EXPORT_STATUS=1 WHERE EXPORT_ID=? AND UTI_LOGIN=?");
+ ps.setInt(1, priority);
+ ps.setLong(2, this.exportId);
+ ps.setString(3, this.user);
+ ps.executeUpdate();
+ this.logger.debug("Fin IF priority>0");
+ } else {
+ this.logger.debug("Début Else");
+ ps = con.prepareStatement("UPDATE EXPORT_JOBS SET ERROR_MESSAGE=?, EXPORT_STATUS=5 WHERE EXPORT_ID=?");
+ ps.setString(1, errorMessage);
+ ps.setLong(2, this.exportId);
+ ps.executeUpdate();
+ this.logger.debug("Fin Else");
+ }
+ } catch (final SQLException sqlEx) {
+ this.logger.error("run<" + this.exportId + ">()", sqlEx);
+ } catch (final Throwable t) {
+ this.logger.error("run<" + this.exportId + ">()", t);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ this.getNotifier().notifyThreadStop(this);
+ }
+ }
+
+ @Override
+ public String getInformations() {
+ return "\tevaluating export :".concat(Long.toString(this.exportId)).concat("\n");
+ }
+ }
}
package fr.gouv.finances.dgfip.xemelios.batch.exports;
-import fr.gouv.finances.dgfip.xemelios.batch.Batch;
-import fr.gouv.finances.dgfip.xemelios.data.DataLayerManager;
-import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Types;
+
import org.apache.log4j.Logger;
+import fr.gouv.finances.dgfip.xemelios.batch.Batch;
+import fr.gouv.finances.dgfip.xemelios.data.DataLayerManager;
+import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
+
/**
* Initialise les exports qui étaient en cours lors de l'arrêt des batchs
+ *
* @author cmarchand
*/
public class ExportInitiator extends Batch {
- private static final Logger logger = Logger.getLogger(ExportInitiator.class);
-
- public ExportInitiator(String args[]) {
- super(args);
- }
-
- @Override
- protected void initialize() throws Exception {
- super.initialize();
- }
+ private static final Logger logger = Logger.getLogger(ExportInitiator.class);
- @Override
- protected void doProcess() throws Exception {
- logger.info("Running ExportInitiator");
- Connection con = null;
- try {
- con = PoolManager.getInstance().getConnection();
- String statusColName= null;
- if("mysql".equals(DataLayerManager.getImplementation().getLayerName()))
- statusColName = "STATUS";
- else if("oracle".equals(DataLayerManager.getImplementation().getLayerName()))
- statusColName = "EXPORT_STATUS";
- PreparedStatement ps = con.prepareStatement("UPDATE EXPORT_JOBS SET "+statusColName+"=1, RUN_DATE=? WHERE "+statusColName+"=2");
- ps.setNull(1, Types.DATE);
- ps.executeUpdate();
- } catch(Exception ex) {
- logger.error(ex);
- } finally {
- if(con!=null) PoolManager.getInstance().releaseConnection(con);
- }
- }
+ public ExportInitiator(final String args[]) {
+ super(args);
+ }
- @Override
- public String getResumeTraitement() {
- return null;
- }
+ @Override
+ protected void initialize() throws Exception {
+ super.initialize();
+ }
- @Override
- public String typeTraitementRefCode() {
- return "Export initiator";
- }
+ @Override
+ protected void doProcess() throws Exception {
+ logger.info("Running ExportInitiator");
+ Connection con = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ String statusColName = null;
+ if ("mysql".equals(DataLayerManager.getImplementation().getLayerName())) {
+ statusColName = "STATUS";
+ } else if ("oracle".equals(DataLayerManager.getImplementation().getLayerName())) {
+ statusColName = "EXPORT_STATUS";
+ }
+ final PreparedStatement ps = con.prepareStatement("UPDATE EXPORT_JOBS SET " + statusColName + "=1, RUN_DATE=? WHERE " + statusColName + "=2");
+ ps.setNull(1, Types.DATE);
+ ps.executeUpdate();
+ } catch (final Exception ex) {
+ logger.error(ex);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ }
+ }
- @Override
- public String getInformations() {
- return null;
- }
+ @Override
+ public String getResumeTraitement() {
+ return null;
+ }
- @Override
- protected String getBatchVersion() {
- return "1";
- }
+ @Override
+ public String typeTraitementRefCode() {
+ return "Export initiator";
+ }
+ @Override
+ public String getInformations() {
+ return null;
+ }
+ @Override
+ protected String getBatchVersion() {
+ return "1";
+ }
}
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;
+ }
+ }
+
+ }
}
package fr.gouv.finances.dgfip.xemelios.batch.exports.postProcessors;
-import fr.gouv.finances.dgfip.xemelios.batch.PostProcessor;
-import fr.gouv.finances.dgfip.xemelios.export.ExportJob;
import java.io.File;
import java.lang.reflect.Method;
+
import org.apache.log4j.Logger;
+import fr.gouv.finances.dgfip.xemelios.batch.PostProcessor;
+import fr.gouv.finances.dgfip.xemelios.export.ExportJob;
+
/**
- *
+ *
* @author Christophe Marchand <christophe.marchand@axyus.com>
*/
public class ExcelRunner implements PostProcessor {
- private static final Logger logger = Logger.getLogger(ExcelRunner.class);
- private ExportJob job;
+ private static final Logger logger = Logger.getLogger(ExcelRunner.class);
+ private ExportJob job;
- public ExcelRunner() {
- super();
- }
+ public ExcelRunner() {
+ super();
+ }
- public void setConfigData(Object data) {
- job = (ExportJob)data;
- }
+ @Override
+ public void setConfigData(final Object data) {
+ this.job = (ExportJob) data;
+ }
- public void postProcess() throws Exception {
- if(job==null) return;
- File f = new File(job.getGeneratedFileName());
- if(!f.exists()) return;
- if(!f.getName().toUpperCase().endsWith(".XLS")) return;
+ @Override
+ public void postProcess() throws Exception {
+ if (this.job == null) {
+ return;
+ }
+ final File f = new File(this.job.getGeneratedFileName());
+ if (!f.exists()) {
+ return;
+ }
+ if (!f.getName().toUpperCase().endsWith(".XLS")) {
+ return;
+ }
- String osName = System.getProperty("os.name");
- String url = f.toURL().toExternalForm();
- try {
- if (osName.startsWith("Mac OS")) {
- // Mac OS X can handle file types itself (thank goodness!)
- Class fileMgr = Class.forName("com.apple.eio.FileManager");
- Method openURL = fileMgr.getDeclaredMethod("openURL",new Class[] {String.class});
- openURL.invoke(null, new Object[] {url});
- } else if (osName.startsWith("Windows")) { // who knows what Windows does ...
- // il y a un bug avec Windows 2000, il faut que le fichier commence par file:// au lieu de file:/
- if("Windows 2000".equals(System.getProperty("os.name")) || "Windows 98".equals(System.getProperty("os.name"))) {
- if(url.startsWith("file:/") && !url.startsWith("file://"))
- url = "file://".concat(url.substring(6));
- }
- Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
- // we must synchronize this
- p.waitFor();
- } else {
- logger.error("This post processor - ExcelRunner - can only be used on Windows and MacOS. You are using "+osName);
- }
- } catch (Exception e) {
- logger.error(e);
- throw e;
- }
+ final String osName = System.getProperty("os.name");
+ String url = f.toURL().toExternalForm();
+ try {
+ if (osName.startsWith("Mac OS")) {
+ // Mac OS X can handle file types itself (thank goodness!)
+ final Class fileMgr = Class.forName("com.apple.eio.FileManager");
+ final Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class });
+ openURL.invoke(null, new Object[] { url });
+ } else if (osName.startsWith("Windows")) { // who knows what Windows does ...
+ // il y a un bug avec Windows 2000, il faut que le fichier commence par file:// au lieu de file:/
+ if ("Windows 2000".equals(System.getProperty("os.name")) || "Windows 98".equals(System.getProperty("os.name"))) {
+ if (url.startsWith("file:/") && !url.startsWith("file://")) {
+ url = "file://".concat(url.substring(6));
+ }
+ }
+ final Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
+ // we must synchronize this
+ p.waitFor();
+ } else {
+ logger.error("This post processor - ExcelRunner - can only be used on Windows and MacOS. You are using " + osName);
+ }
+ } catch (final Exception e) {
+ logger.error(e);
+ throw e;
+ }
- }
+ }
}
*/
package fr.gouv.finances.dgfip.xemelios.batch.exports.postProcessors;
-import fr.gouv.finances.dgfip.xemelios.batch.PostProcessor;
-import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
-import fr.gouv.finances.dgfip.xemelios.export.ExportJob;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.SQLException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
+
import org.apache.log4j.Logger;
+import fr.gouv.finances.dgfip.xemelios.batch.PostProcessor;
+import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
+import fr.gouv.finances.dgfip.xemelios.export.ExportJob;
+
/**
- *
+ *
* @author Christophe Marchand <christophe.marchand@axyus.com>
*/
public class Zipper implements PostProcessor {
- private static final Logger logger = Logger.getLogger(Zipper.class);
- private ExportJob job=null;
+ private static final Logger logger = Logger.getLogger(Zipper.class);
+ private ExportJob job = null;
- public Zipper() {
- super();
- }
+ public Zipper() {
+ super();
+ }
- public void setConfigData(Object data) {
- this.job=(ExportJob)data;
- }
+ @Override
+ public void setConfigData(final Object data) {
+ this.job = (ExportJob) data;
+ }
- public void postProcess() throws Exception {
- if(job==null) throw new Exception("Job is null !");
- File generatedFile = new File(job.getGeneratedFileName());
- File directory = generatedFile.getParentFile();
- final String fileName = generatedFile.getName().concat(".zip");
- File zipFile = new File(directory,fileName);
- ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
- File[] files = directory.listFiles(new FilenameFilter() {
- public boolean accept(File dir, String name) {
- return !name.equals(fileName);
- }
- });
- zos.setLevel(9);
- for(File f:files) {
- if(!f.isDirectory()) {
- ZipEntry ze = new ZipEntry(f.getName());
- zos.putNextEntry(ze);
- byte[] buffer = new byte[1024*8];
- FileInputStream fis = new FileInputStream(f);
- int read = 0;
- while((read=fis.read(buffer)) > 0) {
- zos.write(buffer, 0, read);
- }
- fis.close();
- zos.closeEntry();
- }
- }
- zos.close();
-// zos.finish();
- Connection con = null;
- try {
- con = PoolManager.getInstance().getConnection();
- String sql = "UPDATE EXPORT_JOBS SET GENERATED_FILE_NAME=? WHERE EXPORT_ID=? AND UTI_LOGIN=?";
- PreparedStatement ps = con.prepareStatement(sql);
- ps.setString(1,zipFile.getAbsolutePath());
- ps.setLong(2,job.getIdExport());
- ps.setString(3,job.getOwner());
- ps.executeUpdate();
- PoolManager.getInstance().releaseConnection(con); con=null;
- job.setGeneratedFileName(zipFile.getAbsolutePath());
- for(File f:files) f.delete();
- } catch(SQLException sqlEx) {
- logger.error("while changing name generated file",sqlEx);
- } finally {
- if(con!=null) PoolManager.getInstance().releaseConnection(con);
- }
- }
+ @Override
+ public void postProcess() throws Exception {
+ if (this.job == null) {
+ throw new Exception("Job is null !");
+ }
+ final File generatedFile = new File(this.job.getGeneratedFileName());
+ final File directory = generatedFile.getParentFile();
+ final String fileName = generatedFile.getName().concat(".zip");
+ final File zipFile = new File(directory, fileName);
+ final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
+ final File[] files = directory.listFiles(new FilenameFilter() {
+ @Override
+ public boolean accept(final File dir, final String name) {
+ return !name.equals(fileName);
+ }
+ });
+ zos.setLevel(9);
+ for (final File f : files) {
+ if (!f.isDirectory()) {
+ final ZipEntry ze = new ZipEntry(f.getName());
+ zos.putNextEntry(ze);
+ final byte[] buffer = new byte[1024 * 8];
+ final FileInputStream fis = new FileInputStream(f);
+ int read = 0;
+ while ((read = fis.read(buffer)) > 0) {
+ zos.write(buffer, 0, read);
+ }
+ fis.close();
+ zos.closeEntry();
+ }
+ }
+ zos.close();
+ // zos.finish();
+ Connection con = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ final String sql = "UPDATE EXPORT_JOBS SET GENERATED_FILE_NAME=? WHERE EXPORT_ID=? AND UTI_LOGIN=?";
+ final PreparedStatement ps = con.prepareStatement(sql);
+ ps.setString(1, zipFile.getAbsolutePath());
+ ps.setLong(2, this.job.getIdExport());
+ ps.setString(3, this.job.getOwner());
+ ps.executeUpdate();
+ PoolManager.getInstance().releaseConnection(con);
+ con = null;
+ this.job.setGeneratedFileName(zipFile.getAbsolutePath());
+ for (final File f : files) {
+ f.delete();
+ }
+ } catch (final SQLException sqlEx) {
+ logger.error("while changing name generated file", sqlEx);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ }
+ }
}
*/
package fr.gouv.finances.dgfip.xemelios.batch.imports;
-import fr.gouv.finances.dgfip.xemelios.utils.XmlUtils;
-import fr.gouv.finances.dgfip.utils.Pair;
-import fr.gouv.finances.dgfip.utils.xml.FactoryProvider;
-import fr.gouv.finances.dgfip.utils.xml.NamespaceContextImpl;
-import fr.gouv.finances.dgfip.xemelios.common.config.DocumentModel;
-import fr.gouv.finances.dgfip.xemelios.common.config.EtatModel;
import java.io.File;
import java.io.IOException;
import java.util.Stack;
+
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
+
import org.apache.log4j.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.ext.DefaultHandler2;
+import fr.gouv.finances.dgfip.utils.Pair;
+import fr.gouv.finances.dgfip.utils.xml.FactoryProvider;
+import fr.gouv.finances.dgfip.utils.xml.NamespaceContextImpl;
+import fr.gouv.finances.dgfip.xemelios.common.config.DocumentModel;
+import fr.gouv.finances.dgfip.xemelios.common.config.EtatModel;
+import fr.gouv.finances.dgfip.xemelios.utils.XmlUtils;
+
/**
- *
+ *
* @author chm
*/
public class BudgCollParser {
- public static final transient String IGNORABLE_ERROR = "This is not an error, only an expected termination";
- private File xmlFile;
- private DocumentModel documentModel;
- private String collectivite, collectiviteLib, budget, budgetLib, errorMessage;
- private boolean inError = false;
+ public static final transient String IGNORABLE_ERROR = "This is not an error, only an expected termination";
+ private final File xmlFile;
+ private final DocumentModel documentModel;
+ private String collectivite, collectiviteLib, budget, budgetLib, errorMessage;
+ private boolean inError = false;
+
+ public BudgCollParser(final File xmlFile, final DocumentModel documentModel) {
+ super();
+ this.xmlFile = xmlFile;
+ this.documentModel = documentModel;
+ }
- public BudgCollParser(File xmlFile, DocumentModel documentModel) {
- super();
- this.xmlFile = xmlFile;
- this.documentModel = documentModel;
- }
+ /**
+ * Start investigation process
+ */
+ public void investigate() {
+ final SAXParserFactory saxFactory = FactoryProvider.getSaxParserFactory();
+ final ParserHandler ph = new ParserHandler(this.documentModel);
+ try {
+ final SAXParser parser = saxFactory.newSAXParser();
+ parser.parse(this.xmlFile, ph);
+ } catch (final ParserConfigurationException pcEx) {
+ pcEx.printStackTrace();
+ } catch (final SAXException saxEx) {
+ if (!saxEx.getMessage().equals(IGNORABLE_ERROR)) {
+ // file is not valid
+ this.setError(saxEx.getMessage());
+ }
+ } catch (final IOException ioEx) {
+ // should never happen
+ ioEx.printStackTrace();
+ } finally {
+ if (ph != null && !this.inError) {
+ if (ph.getCollectivite() != null) {
+ if (ph.collectivite.key != null && ph.collectivite.key.length() > 0 && (this.collectivite == null || this.collectivite.length() == 0)) {
+ this.collectivite = ph.collectivite.key;
+ }
+ if (ph.collectivite.libelle != null && ph.collectivite.libelle.length() > 0 && (this.collectiviteLib == null || this.collectiviteLib.length() == 0)) {
+ this.collectiviteLib = ph.collectivite.libelle;
+ }
+ }
+ if (ph.budget != null) {
+ if (ph.budget.key != null && ph.budget.key.length() > 0 && (this.budget == null || this.budget.length() == 0)) {
+ this.budget = ph.budget.key;
+ }
+ if (ph.budget.libelle != null && ph.budget.libelle.length() > 0 && (this.budgetLib == null || this.budgetLib.length() == 0)) {
+ this.budgetLib = ph.budget.libelle;
+ }
+ }
+ }
+ }
+ }
- /**
- * Start investigation process
- */
- public void investigate() {
- SAXParserFactory saxFactory = FactoryProvider.getSaxParserFactory();
- ParserHandler ph = new ParserHandler(documentModel);
- try {
- SAXParser parser = saxFactory.newSAXParser();
- parser.parse(xmlFile, ph);
- } catch (ParserConfigurationException pcEx) {
- pcEx.printStackTrace();
- } catch (SAXException saxEx) {
- if (!saxEx.getMessage().equals(IGNORABLE_ERROR)) {
- // file is not valid
- setError(saxEx.getMessage());
- }
- } catch(IOException ioEx) {
- // should never happen
- ioEx.printStackTrace();
- } finally {
- if(ph!=null && !inError) {
- if(ph.getCollectivite()!=null) {
- if(ph.collectivite.key!=null && ph.collectivite.key.length()>0 && (collectivite==null || collectivite.length()==0)) {
- collectivite = ph.collectivite.key;
- }
- if(ph.collectivite.libelle!=null && ph.collectivite.libelle.length()>0 && (collectiviteLib==null || collectiviteLib.length()==0)) {
- collectiviteLib = ph.collectivite.libelle;
- }
- }
- if(ph.budget!=null) {
- if(ph.budget.key!=null && ph.budget.key.length()>0 && (budget==null || budget.length()==0)) {
- budget = ph.budget.key;
- }
- if(ph.budget.libelle!=null && ph.budget.libelle.length()>0 && (budgetLib==null || budgetLib.length()==0)) {
- budgetLib = ph.budget.libelle;
- }
- }
- }
- }
- }
+ public String getBudget() {
+ return this.budget;
+ }
- public String getBudget() {
- return budget;
- }
+ public void setBudget(final String budget) {
+ this.budget = budget;
+ }
- public void setBudget(String budget) {
- this.budget = budget;
- }
+ public String getBudgetLib() {
+ return this.budgetLib;
+ }
- public String getBudgetLib() {
- return budgetLib;
- }
+ public void setBudgetLib(final String budgetLib) {
+ this.budgetLib = budgetLib;
+ }
- public void setBudgetLib(String budgetLib) {
- this.budgetLib = budgetLib;
- }
+ public String getCollectivite() {
+ return this.collectivite;
+ }
- public String getCollectivite() {
- return collectivite;
- }
+ public void setCollectivite(final String collectivite) {
+ this.collectivite = collectivite;
+ }
- public void setCollectivite(String collectivite) {
- this.collectivite = collectivite;
- }
+ public String getCollectiviteLib() {
+ return this.collectiviteLib;
+ }
- public String getCollectiviteLib() {
- return collectiviteLib;
- }
+ public void setCollectiviteLib(final String collectiviteLib) {
+ this.collectiviteLib = collectiviteLib;
+ }
- public void setCollectiviteLib(String collectiviteLib) {
- this.collectiviteLib = collectiviteLib;
- }
+ public void setError(final String errorMessage) {
+ this.inError = true;
+ this.errorMessage = errorMessage;
+ }
- public void setError(String errorMessage) {
- inError = true;
- this.errorMessage = errorMessage;
- }
+ public String getErrorMessage() {
+ return this.errorMessage;
+ }
- public String getErrorMessage() {
- return errorMessage;
- }
+ public boolean isInError() {
+ return this.inError;
+ }
- public boolean isInError() {
- return inError;
- }
+ private static class ParserHandler extends DefaultHandler2 {
+ private static final Logger logger = Logger.getLogger(ParserHandler.class);
+ private final NamespaceContextImpl nsCtx;
+ private final Stack<String> tagStack = new Stack<String>();
+ private final Stack<StringBuilder> charactersStack = new Stack<StringBuilder>();
- private static class ParserHandler extends DefaultHandler2 {
- private static final Logger logger = Logger.getLogger(ParserHandler.class);
- private NamespaceContextImpl nsCtx;
- private Stack<String> tagStack = new Stack<String>();
- private Stack<StringBuilder> charactersStack = new Stack<StringBuilder>();
+ private final DocumentModel dm;
+ private Pair budget, collectivite;
- private DocumentModel dm;
- private Pair budget, collectivite;
+ public ParserHandler(final DocumentModel dm) {
+ super();
+ this.dm = dm;
+ this.nsCtx = new NamespaceContextImpl();
+ }
- public ParserHandler(DocumentModel dm) {
- super();
- this.dm = dm;
- nsCtx = new NamespaceContextImpl();
- }
+ @Override
+ public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException {
+ final String tagName = XmlUtils.getShortTagName(uri, localName, qName);
+ switch (this.tagStack.size()) {
+ case 0: {
+ // si ce n'est pas le tag du doc que l'on importe, bye
+ if (!tagName.equals(this.dm.getBalise())) {
+ throw new SAXException("Le fichier importé n'est pas un " + this.dm.getTitre());
+ }
+ break;
+ }
+ case 1: {
+ if (this.dm.getEntetes().contains(tagName)) {
+ } else if (this.dm.getReferentiel().equals(tagName)) {
+ } else {
+ final EtatModel em = this.dm.getEtatByTagName(tagName);
+ if (em != null) {
+ throw new SAXException(IGNORABLE_ERROR);
+ }
+ }
+ break;
+ }
+ case 2: {
+ break;
+ }
+ }
+ this.tagStack.push(tagName);
+ this.charactersStack.push(new StringBuilder());
- @Override
- public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
- String tagName = XmlUtils.getShortTagName(uri, localName, qName);
- switch (tagStack.size()) {
- case 0: {
- // si ce n'est pas le tag du doc que l'on importe, bye
- if (!tagName.equals(dm.getBalise())) {
- throw new SAXException("Le fichier importé n'est pas un " + dm.getTitre());
- }
- break;
- }
- case 1: {
- if (dm.getEntetes().contains(tagName)) {
- } else if (dm.getReferentiel().equals(tagName)) {
- } else {
- EtatModel em = dm.getEtatByTagName(tagName);
- if(em!=null) throw new SAXException(IGNORABLE_ERROR);
- }
- break;
- }
- case 2: {
- break;
- }
- }
- tagStack.push(tagName);
- charactersStack.push(new StringBuilder());
+ final String path = XmlUtils.getPath(this.tagStack);
+ for (int i = 0; i < attributes.getLength(); i++) {
+ final String attrName = attributes.getLocalName(i);
+ final String value = attributes.getValue(i);
+ final String fullPath = path + "/@" + attrName;
+ this.checkBudgetCollectivite(fullPath, value);
+ }
+ }
- String path = XmlUtils.getPath(tagStack);
- for (int i = 0; i < attributes.getLength(); i++) {
- String attrName = attributes.getLocalName(i);
- String value = attributes.getValue(i);
- String fullPath = path + "/@" + attrName;
- checkBudgetCollectivite(fullPath, value);
- }
- }
- @Override
- public void endElement(String uri, String localName, String qName) throws SAXException {
- String tagName = XmlUtils.getShortTagName(uri, localName, qName);
- if(tagStack.size()==1) {
- if (!tagName.equals(dm.getBalise())) {
- throw new SAXException("Fermeture d'un element root inattendu");
- }
- }
- String path = XmlUtils.getPath(tagStack);
- String innerData = charactersStack.pop().toString();
- checkBudgetCollectivite(path, innerData);
- tagStack.pop();
- }
+ @Override
+ public void endElement(final String uri, final String localName, final String qName) throws SAXException {
+ final String tagName = XmlUtils.getShortTagName(uri, localName, qName);
+ if (this.tagStack.size() == 1) {
+ if (!tagName.equals(this.dm.getBalise())) {
+ throw new SAXException("Fermeture d'un element root inattendu");
+ }
+ }
+ final String path = XmlUtils.getPath(this.tagStack);
+ final String innerData = this.charactersStack.pop().toString();
+ this.checkBudgetCollectivite(path, innerData);
+ this.tagStack.pop();
+ }
- @Override
- public void endPrefixMapping(String prefix) throws SAXException {
- super.endPrefixMapping(prefix);
- nsCtx.removeMapping(prefix);
- }
+ @Override
+ public void endPrefixMapping(final String prefix) throws SAXException {
+ super.endPrefixMapping(prefix);
+ this.nsCtx.removeMapping(prefix);
+ }
- @Override
- public void startPrefixMapping(String prefix, String uri) throws SAXException {
- super.startPrefixMapping(prefix, uri);
- nsCtx.addMapping(prefix, uri);
- }
+ @Override
+ public void startPrefixMapping(final String prefix, final String uri) throws SAXException {
+ super.startPrefixMapping(prefix, uri);
+ this.nsCtx.addMapping(prefix, uri);
+ }
- private void checkBudgetCollectivite(String path, String value) throws SAXException {
-// logger.debug("checking coll/budg at"+path);
- if (dm.getBudgetPath() != null) {
- if (dm.getBudgetPath().getCodePath() != null && path.equals(dm.getBudgetPath().getCodePath().getPath())) {
- if (budget == null) {
- budget = new Pair();
- }
- budget.key = value;
- }
- if (dm.getBudgetPath().getLibellePath() != null && path.equals(dm.getBudgetPath().getLibellePath().getPath())) {
- if (budget == null) {
- budget = new Pair();
- }
- budget.libelle = value;
- }
- }
- if (dm.getCollectivitePath() != null) {
- if (dm.getCollectivitePath().getCodePath() != null && path.equals(dm.getCollectivitePath().getCodePath().getPath())) {
- if (collectivite == null) {
- collectivite = new Pair();
- }
- collectivite.key = value;
- }
- if (dm.getCollectivitePath().getLibellePath() != null && path.equals(dm.getCollectivitePath().getLibellePath().getPath())) {
- if (collectivite == null) {
- collectivite = new Pair();
- }
- collectivite.libelle = value;
- }
- }
- if(collectivite!=null &&collectivite.key!=null && collectivite.key.length()>0 && collectivite.libelle!=null && collectivite.libelle.length()>0
- && budget!=null && budget.key!=null && budget.key.length()>0 && budget.libelle!=null && budget.libelle.length()>0) {
- throw new SAXException(IGNORABLE_ERROR);
- }
- }
+ private void checkBudgetCollectivite(final String path, final String value) throws SAXException {
+ // logger.debug("checking coll/budg at"+path);
+ if (this.dm.getBudgetPath() != null) {
+ if (this.dm.getBudgetPath().getCodePath() != null && path.equals(this.dm.getBudgetPath().getCodePath().getPath())) {
+ if (this.budget == null) {
+ this.budget = new Pair();
+ }
+ this.budget.key = value;
+ }
+ if (this.dm.getBudgetPath().getLibellePath() != null && path.equals(this.dm.getBudgetPath().getLibellePath().getPath())) {
+ if (this.budget == null) {
+ this.budget = new Pair();
+ }
+ this.budget.libelle = value;
+ }
+ }
+ if (this.dm.getCollectivitePath() != null) {
+ if (this.dm.getCollectivitePath().getCodePath() != null && path.equals(this.dm.getCollectivitePath().getCodePath().getPath())) {
+ if (this.collectivite == null) {
+ this.collectivite = new Pair();
+ }
+ this.collectivite.key = value;
+ }
+ if (this.dm.getCollectivitePath().getLibellePath() != null && path.equals(this.dm.getCollectivitePath().getLibellePath().getPath())) {
+ if (this.collectivite == null) {
+ this.collectivite = new Pair();
+ }
+ this.collectivite.libelle = value;
+ }
+ }
+ if (this.collectivite != null && this.collectivite.key != null && this.collectivite.key.length() > 0 && this.collectivite.libelle != null && this.collectivite.libelle.length() > 0 && this.budget != null && this.budget.key != null && this.budget.key.length() > 0
+ && this.budget.libelle != null && this.budget.libelle.length() > 0) {
+ throw new SAXException(IGNORABLE_ERROR);
+ }
+ }
- public Pair getBudget() {
- return budget;
- }
+ public Pair getBudget() {
+ return this.budget;
+ }
- public Pair getCollectivite() {
- return collectivite;
- }
- }
+ public Pair getCollectivite() {
+ return this.collectivite;
+ }
+ }
}
*/
package fr.gouv.finances.dgfip.xemelios.batch.imports;
-import fr.gouv.finances.dgfip.xemelios.batch.Batch;
-import fr.gouv.finances.dgfip.xemelios.batch.BatchRunner;
-import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.ImportVOFull;
-import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.ImportVOPK;
-import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.ImportVOWrapper;
-import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.XemImportsFilesVOFull;
-import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.XemImportsFilesVOWrapper;
-import fr.gouv.finances.dgfip.xemelios.common.Constants;
-import fr.gouv.finances.dgfip.xemelios.common.PJRef;
-import fr.gouv.finances.dgfip.xemelios.common.config.DocumentsModel;
-import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
-import fr.gouv.finances.dgfip.xemelios.utils.FileUtils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
+
import org.apache.log4j.Logger;
+import fr.gouv.finances.dgfip.xemelios.batch.Batch;
+import fr.gouv.finances.dgfip.xemelios.batch.BatchRunner;
+import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.ImportVOFull;
+import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.ImportVOPK;
+import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.ImportVOWrapper;
+import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.XemImportsFilesVOFull;
+import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.XemImportsFilesVOWrapper;
+import fr.gouv.finances.dgfip.xemelios.common.Constants;
+import fr.gouv.finances.dgfip.xemelios.common.PJRef;
+import fr.gouv.finances.dgfip.xemelios.common.config.DocumentsModel;
+import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
+import fr.gouv.finances.dgfip.xemelios.utils.FileUtils;
+
/**
* Ce batch vérifie que toutes les conditions sont bien remplies pour réaliser un import
+ *
* @author chm
*/
public class ImportChecker extends Batch {
- private final static Logger logger = Logger.getLogger(ImportChecker.class);
- public static final int MAX_CONCURRENT_THREADS = 3;
- private int maxConcurrentThreads = MAX_CONCURRENT_THREADS;
-
- private String importDirectory = null;
+ private final static Logger logger = Logger.getLogger(ImportChecker.class);
+ public static final int MAX_CONCURRENT_THREADS = 3;
+ private int maxConcurrentThreads = MAX_CONCURRENT_THREADS;
+
+ private String importDirectory = null;
+
+ private ThreadPool<ImportCheckerImpl> pool = null;
+
+ public ImportChecker(final String[] args) {
+ super(args);
+ }
+
+ @Override
+ protected void initialize() throws Exception {
+ super.initialize();
+ final String sTmp = this.getProps().getProperty("max.concurrent.threads");
+ if (sTmp != null && sTmp.length() > 0) {
+ try {
+ this.maxConcurrentThreads = Integer.parseInt(sTmp);
+ } catch (final Throwable t) {
+ }
+ }
+ }
+
+ @Override
+ protected void doProcess() throws Exception {
+ Connection con = null;
+ Statement st = null;
+ ResultSet rs = null;
+ final ArrayList<ImportCheckerImpl> threads = new ArrayList<ImportCheckerImpl>();
+ try {
+ con = PoolManager.getInstance().getConnection();
+ final String sqlParam = "SELECT PARAM_VALUE FROM PARAMETERS WHERE PARAM_NAME='import.directory'";
+ st = con.createStatement();
+ rs = st.executeQuery(sqlParam);
+ if (rs.next()) {
+ this.importDirectory = rs.getString(1);
+ rs.close();
+ } else {
+ throw new Exception("parameter 'import.directory' not set !");
+ }
+ final String sql = "SELECT IMPORT_ID FROM XEM_IMPORTS WHERE STATUS=0 AND CLEANED IS NULL";
+ rs = st.executeQuery(sql);
+ while (rs.next()) {
+ threads.add(new ImportCheckerImpl(rs.getString(1), this.importDirectory, BatchRunner.getInstance().getDocuments()));
+ }
+ PoolManager.getInstance().releaseConnection(con);
+ con = null;
+ this.pool = new ThreadPool<ImportCheckerImpl>(threads);
+ this.pool.start(this.maxConcurrentThreads);
+ } catch (final SQLException sqlEx) {
+ // TODO
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ }
+ }
+
+ @Override
+ public String getResumeTraitement() {
+ return "import.checker resume";
+ }
- private ThreadPool<ImportCheckerImpl> pool = null;
+ @Override
+ public String typeTraitementRefCode() {
+ return "import.checker";
+ }
- public ImportChecker(String[] args) {
- super(args);
- }
+ @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 void initialize() throws Exception {
- super.initialize();
- String sTmp = getProps().getProperty("max.concurrent.threads");
- if(sTmp!=null && sTmp.length()>0) {
- try {
- maxConcurrentThreads = Integer.parseInt(sTmp);
- } catch(Throwable t) {}
- }
- }
+ @Override
+ protected String getBatchVersion() {
+ return "1.0";
+ }
- @Override
- protected void doProcess() throws Exception {
- Connection con = null;
- Statement st = null;
- ResultSet rs = null;
- ArrayList<ImportCheckerImpl> threads = new ArrayList<ImportCheckerImpl>();
- try {
- con = PoolManager.getInstance().getConnection();
- String sqlParam = "SELECT PARAM_VALUE FROM PARAMETERS WHERE PARAM_NAME='import.directory'";
- st = con.createStatement();
- rs = st.executeQuery(sqlParam);
- if(rs.next()) {
- importDirectory = rs.getString(1);
- rs.close();
- } else {
- throw new Exception("parameter 'import.directory' not set !");
- }
- String sql = "SELECT IMPORT_ID FROM XEM_IMPORTS WHERE STATUS=0 AND CLEANED IS NULL";
- rs = st.executeQuery(sql);
- while(rs.next()) {
- threads.add(new ImportCheckerImpl(rs.getString(1),importDirectory,BatchRunner.getInstance().getDocuments()));
- }
- PoolManager.getInstance().releaseConnection(con); con=null;
- pool = new ThreadPool<ImportCheckerImpl>(threads);
- pool.start(maxConcurrentThreads);
- } catch(SQLException sqlEx) {
- // TODO
- } finally {
- if(con!=null) PoolManager.getInstance().releaseConnection(con);
- }
- }
+ protected class ImportCheckerImpl extends RunnableBatch {
+ private final Logger logger = Logger.getLogger(ImportCheckerImpl.class);
+ private final String importId;
+ private ImportVOFull importFull;
+ private List<XemImportsFilesVOFull> files;
+ // utilities
+ private final File importDirectory;
+ private final DocumentsModel documents;
- @Override
- public String getResumeTraitement() {
- return "import.checker resume";
- }
+ public ImportCheckerImpl(final String importId, final String importDirectory, final DocumentsModel documents) {
+ this.setName("importChecker<" + importId + ">");
+ this.importId = importId;
+ this.importDirectory = new File(importDirectory);
+ this.documents = documents;
+ this.setName("ImportCheckerImpl<" + importId + ">");
+ }
- @Override
- public String typeTraitementRefCode() {
- return "import.checker";
- }
+ @Override
+ @SuppressWarnings("unchecked")
+ public void run() {
+ Connection con = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ this.loadImportInfos(con);
+ this.importFull.setStatus(1);
+ ImportVOWrapper.updateImportVO(con, this.importFull);
+ PoolManager.getInstance().releaseConnection(con);
+ con = null;
+ this.checkFile();
+ con = PoolManager.getInstance().getConnection();
+ ImportVOWrapper.updateImportVO(con, this.importFull);
+ } catch (final SQLException sqlEx) {
+ this.logger.error("run<" + this.importId + ">()", sqlEx);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ this.getNotifier().notifyThreadStop(this);
+ }
+ }
- @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();
- }
+ protected boolean checkFile() {
+ final File[] ret = new File[1];
+ ret[0] = new File(new File(this.importDirectory, this.importId), this.importFull.getFileName());
+ final ImportContent ic = new ImportContent();
+ for (final File element : ret) {
+ if (element.getName().toLowerCase().endsWith(".zip")) {
+ if (element.exists()) {
+ ZipFile zf = null;
+ try {
+ zf = new ZipFile(element);
+ for (final Enumeration<? extends ZipEntry> enumer = zf.entries(); enumer.hasMoreElements();) {
+ final ZipEntry ze = enumer.nextElement();
+ if (!ze.isDirectory()) {
+ String fileName = ze.getName();
+ final String entryName = fileName.toLowerCase();
+ fileName = fileName.replace(File.pathSeparatorChar, '_').replace(File.separatorChar, '_').replace(':', '|').replace('\'', '_').replace('/', '_');
+ this.logger.debug(entryName);
+ if (PJRef.isPJ(ze)) {
+ final PJRef pj = new PJRef(ze);
+ pj.writeTmpFile(FileUtils.getTempDir(), zf);
+ ic.pjs.add(pj);
+ } else if ((entryName.endsWith(this.documents.getDocumentById(this.importFull.getFileType()).getExtension().toLowerCase()) || entryName.endsWith(".xml")) && !fileName.startsWith("_")) {
+ // on decompresse le fichier dans le
+ // repertoire temporaire, comme ca il sera
+ // supprime en quittant
+ final InputStream is = zf.getInputStream(ze);
+ final BufferedInputStream bis = new BufferedInputStream(is);
+ final File output = new File(FileUtils.getTempDir(), fileName);
+ final BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(output));
+ final byte[] buffer = new byte[1024];
+ int read = bis.read(buffer);
+ while (read > 0) {
+ bos.write(buffer, 0, read);
+ read = bis.read(buffer);
+ }
+ bos.flush();
+ bos.close();
+ bis.close();
+ ic.filesToImport.add(output);
+ }
+ }
+ }
+ zf.close();
+ } catch (final ZipException zEx) {
+ final String errorMessage = "Le fichier " + element.getName() + " n'est pas une archive ZIP valide";
+ this.importFull.setErrorMessage(errorMessage);
+ } catch (final IOException ioEx) {
+ final String errorMessage = "Le fichier " + element.getName() + " est illisible. Vérifiez que les noms des fichiers\ncontenus dans l'archive ne comportent pas de caractères spéciaux ou accentués.";
+ this.importFull.setErrorMessage(errorMessage);
+ } finally {
+ if (zf != null) {
+ try {
+ zf.close();
+ } catch (final Throwable t) {
+ }
+ }
+ }
+ }
+ } else if (element.getName().toLowerCase().endsWith(".gz")) {
+ try {
+ String fileName = element.getName();
+ fileName = fileName.substring(0, fileName.length() - 3);
+ final File output = new File(FileUtils.getTempDir(), fileName);
+ final GZIPInputStream gis = new GZIPInputStream(new FileInputStream(element));
+ final BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(output));
+ final byte[] buffer = new byte[1024];
+ int read = gis.read(buffer);
+ while (read > 0) {
+ bos.write(buffer, 0, read);
+ read = gis.read(buffer);
+ }
+ bos.flush();
+ bos.close();
+ gis.close();
+ ic.filesToImport.add(output);
+ } catch (final IOException ioEx) {
+ // nothing to do
+ }
+ } else {
+ ic.filesToImport.add(element);
+ }
+ }
+ if (ic.filesToImport.size() == 1) {
+ if (Constants.XEMELIOS_ARCHIVE_SIGN.equals(this.importFull.getFileType())) {
+ // Traitement des archives
+ this.importFull.setStatus(6);
+ } else {
+ // Traitement des autres fichiers
+ boolean needToLookForCollBudg = false;
+ final StringBuffer errors = new StringBuffer();
+ if (this.importFull.getCollectivite() == null || this.importFull.getCollectivite().length() == 0) {
+ needToLookForCollBudg = true;
+ }
+ if (this.importFull.getCollectiviteLib() == null || this.importFull.getCollectiviteLib().length() == 0) {
+ needToLookForCollBudg = true;
+ }
+ if (this.importFull.getBudget() == null || this.importFull.getBudget().length() == 0) {
+ needToLookForCollBudg = true;
+ }
+ if (this.importFull.getBudgetLib() == null || this.importFull.getBudgetLib().length() == 0) {
+ needToLookForCollBudg = true;
+ }
+ if (needToLookForCollBudg) {
+ final BudgCollParser bcp = new BudgCollParser(ic.filesToImport.get(0), this.documents.getDocumentById(this.importFull.getFileType()));
+ bcp.investigate();
+ if (bcp.isInError()) {
+ this.importFull.setErrorMessage(bcp.getErrorMessage());
+ this.importFull.setStatus(5);
+ } else {
+ if (this.importFull.getCollectivite() == null || this.importFull.getCollectivite().length() == 0) {
+ this.importFull.setCollectivite(bcp.getCollectivite());
+ }
+ if (this.importFull.getCollectiviteLib() == null || this.importFull.getCollectiviteLib().length() == 0) {
+ this.importFull.setCollectiviteLib(bcp.getCollectiviteLib());
+ }
+ if (this.importFull.getBudget() == null || this.importFull.getBudget().length() == 0) {
+ this.importFull.setBudget(bcp.getBudget());
+ }
+ if (this.importFull.getBudgetLib() == null || this.importFull.getBudgetLib().length() == 0) {
+ this.importFull.setBudgetLib(bcp.getBudgetLib());
+ }
+ this.importFull.determinateStatus();
+ }
+ }
+ }
+ } else {
+ // TODO: multi sub files
+ }
+ // TODO
+ return false;
+ }
- @Override
- protected String getBatchVersion() {
- return "1.0";
- }
+ @SuppressWarnings("unchecked")
+ public void loadImportInfos(final Connection con) throws SQLException {
+ final ImportVOPK pk = new ImportVOPK(this.importId);
+ this.importFull = ImportVOWrapper.getImportVOFullByPk(con, pk);
+ this.files = XemImportsFilesVOWrapper.getAllXemImportsFilesVOFullBy(con, XemImportsFilesVOFull.class, "SELECT " + XemImportsFilesVOWrapper.getSelectFieldsClause() + " FROM XEM_IMPORTS_FILES WHERE IMPORT_ID='" + this.importId + "'");
+ }
- protected class ImportCheckerImpl extends RunnableBatch {
- private final Logger logger = Logger.getLogger(ImportCheckerImpl.class);
- private String importId;
- private ImportVOFull importFull;
- private List<XemImportsFilesVOFull> files;
- // utilities
- private File importDirectory;
- private DocumentsModel documents;
- public ImportCheckerImpl(String importId,String importDirectory, DocumentsModel documents) {
- setName("importChecker<"+importId+">");
- this.importId=importId;
- this.importDirectory = new File(importDirectory);
- this.documents=documents;
- setName("ImportCheckerImpl<"+importId+">");
- }
+ @Override
+ public String getInformations() {
+ if (this.isAlive()) {
+ return "\t".concat(this.getName()).concat(": running\n");
+ } else {
+ return "\t".concat(this.getName()).concat(": idle\n");
+ }
+ }
+ }
- @Override
- @SuppressWarnings("unchecked")
- public void run() {
- Connection con = null;
- try {
- con = PoolManager.getInstance().getConnection();
- loadImportInfos(con);
- importFull.setStatus(1);
- ImportVOWrapper.updateImportVO(con, importFull);
- PoolManager.getInstance().releaseConnection(con); con=null;
- checkFile();
- con = PoolManager.getInstance().getConnection();
- ImportVOWrapper.updateImportVO(con, importFull);
- } catch(SQLException sqlEx) {
- logger.error("run<"+importId+">()", sqlEx);
- } finally {
- if(con!=null) PoolManager.getInstance().releaseConnection(con);
- getNotifier().notifyThreadStop(this);
- }
- }
+ public static class ImportContent {
+ ArrayList<File> filesToImport;
+ ArrayList<PJRef> pjs;
- protected boolean checkFile() {
- File[] ret = new File[1];
- ret[0] = new File(new File(importDirectory,importId),importFull.getFileName());
- ImportContent ic = new ImportContent();
- for (int i = 0; i < ret.length; i++) {
- if (ret[i].getName().toLowerCase().endsWith(".zip")) {
- if (ret[i].exists()) {
- ZipFile zf = null;
- try {
- zf = new ZipFile(ret[i]);
- for (Enumeration<? extends ZipEntry> enumer = zf.entries(); enumer.hasMoreElements();) {
- ZipEntry ze = enumer.nextElement();
- if (!ze.isDirectory()) {
- String fileName = ze.getName();
- String entryName = fileName.toLowerCase();
- fileName = fileName.replace(
- File.pathSeparatorChar, '_').replace(
- File.separatorChar, '_').replace(':',
- '|').replace('\'', '_').replace('/',
- '_');
- logger.debug(entryName);
- if(PJRef.isPJ(ze)) {
- PJRef pj = new PJRef(ze);
- pj.writeTmpFile(FileUtils.getTempDir(),zf);
- ic.pjs.add(pj);
- } else if ((entryName.endsWith(documents.getDocumentById(importFull.getFileType()).getExtension().toLowerCase()) || entryName.endsWith(".xml")) && !fileName.startsWith("_")) {
- // on decompresse le fichier dans le
- // repertoire temporaire, comme ca il sera
- // supprime en quittant
- InputStream is = zf.getInputStream(ze);
- BufferedInputStream bis = new BufferedInputStream(is);
- File output = new File(FileUtils.getTempDir(), fileName);
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(output));
- byte[] buffer = new byte[1024];
- int read = bis.read(buffer);
- while (read > 0) {
- bos.write(buffer, 0, read);
- read = bis.read(buffer);
- }
- bos.flush();
- bos.close();
- bis.close();
- ic.filesToImport.add(output);
- }
- }
- }
- zf.close();
- } catch (ZipException zEx) {
- String errorMessage = "Le fichier "+ret[i].getName()+ " n'est pas une archive ZIP valide";
- importFull.setErrorMessage(errorMessage);
- } catch (IOException ioEx) {
- String errorMessage = "Le fichier "+ret[i].getName()+" est illisible. Vérifiez que les noms des fichiers\ncontenus dans l'archive ne comportent pas de caractères spéciaux ou accentués.";
- importFull.setErrorMessage(errorMessage);
- } finally {
- if(zf!=null) {
- try { zf.close(); } catch(Throwable t) {}
- }
- }
- }
- } else if(ret[i].getName().toLowerCase().endsWith(".gz")) {
- try {
- String fileName = ret[i].getName();
- fileName = fileName.substring(0, fileName.length()-3);
- File output = new File(FileUtils.getTempDir(), fileName);
- GZIPInputStream gis = new GZIPInputStream(new FileInputStream(ret[i]));
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(output));
- byte[] buffer = new byte[1024];
- int read = gis.read(buffer);
- while (read > 0) {
- bos.write(buffer, 0, read);
- read = gis.read(buffer);
- }
- bos.flush();
- bos.close();
- gis.close();
- ic.filesToImport.add(output);
- } catch(IOException ioEx) {
- // nothing to do
- }
- } else {
- ic.filesToImport.add(ret[i]);
- }
- }
- if(ic.filesToImport.size()==1) {
- if(Constants.XEMELIOS_ARCHIVE_SIGN.equals(importFull.getFileType())) {
- // Traitement des archives
- importFull.setStatus(6);
- } else {
- // Traitement des autres fichiers
- boolean needToLookForCollBudg = false;
- StringBuffer errors = new StringBuffer();
- if(importFull.getCollectivite()==null || importFull.getCollectivite().length()==0) {
- needToLookForCollBudg = true;
- }
- if(importFull.getCollectiviteLib()==null || importFull.getCollectiviteLib().length()==0) {
- needToLookForCollBudg = true;
- }
- if(importFull.getBudget()==null || importFull.getBudget().length()==0) {
- needToLookForCollBudg = true;
- }
- if(importFull.getBudgetLib()==null || importFull.getBudgetLib().length()==0) {
- needToLookForCollBudg = true;
- }
- if(needToLookForCollBudg) {
- BudgCollParser bcp = new BudgCollParser(ic.filesToImport.get(0), documents.getDocumentById(importFull.getFileType()));
- bcp.investigate();
- if(bcp.isInError()) {
- importFull.setErrorMessage(bcp.getErrorMessage());
- importFull.setStatus(5);
- } else {
- if(importFull.getCollectivite()==null || importFull.getCollectivite().length()==0) {
- importFull.setCollectivite(bcp.getCollectivite());
- }
- if(importFull.getCollectiviteLib()==null || importFull.getCollectiviteLib().length()==0) {
- importFull.setCollectiviteLib(bcp.getCollectiviteLib());
- }
- if(importFull.getBudget()==null || importFull.getBudget().length()==0) {
- importFull.setBudget(bcp.getBudget());
- }
- if(importFull.getBudgetLib()==null || importFull.getBudgetLib().length()==0) {
- importFull.setBudgetLib(bcp.getBudgetLib());
- }
- importFull.determinateStatus();
- }
- }
- }
- } else {
- // TODO: multi sub files
- }
- // TODO
- return false;
- }
- @SuppressWarnings("unchecked")
- public void loadImportInfos(Connection con) throws SQLException {
- ImportVOPK pk = new ImportVOPK(importId);
- importFull = ImportVOWrapper.getImportVOFullByPk(con, pk);
- files = XemImportsFilesVOWrapper.getAllXemImportsFilesVOFullBy(con, XemImportsFilesVOFull.class, "SELECT "+XemImportsFilesVOWrapper.getSelectFieldsClause()+" FROM XEM_IMPORTS_FILES WHERE IMPORT_ID='"+importId+"'");
- }
+ public ImportContent() {
+ super();
+ this.filesToImport = new ArrayList<File>();
+ this.pjs = new ArrayList<PJRef>();
+ }
- @Override
- public String getInformations() {
- if(isAlive()) return "\t".concat(getName()).concat(": running\n");
- else return "\t".concat(getName()).concat(": idle\n");
- }
- }
- public static class ImportContent {
- ArrayList<File> filesToImport;
- ArrayList<PJRef> pjs;
- public ImportContent() {
- super();
- filesToImport = new ArrayList<File>();
- pjs = new ArrayList<PJRef>();
- }
- public void setFilesToImport(ArrayList<File> array){
- this.filesToImport=array;
- }
- }
+ public void setFilesToImport(final ArrayList<File> array) {
+ this.filesToImport = array;
+ }
+ }
}
*/
package fr.gouv.finances.dgfip.xemelios.batch.imports;
-import fr.gouv.finances.cp.xemelios.importers.batch.BatchRealImporter;
-import fr.gouv.finances.dgfip.xemelios.batch.Batch;
-import fr.gouv.finances.dgfip.xemelios.batch.BatchRunner;
-import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.ImportVOFull;
-import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.ImportVOPK;
-import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.ImportVOWrapper;
-import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
+import fr.gouv.finances.cp.xemelios.importers.batch.BatchRealImporter;
+import fr.gouv.finances.dgfip.xemelios.batch.Batch;
+import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.ImportVOFull;
+import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.ImportVOPK;
+import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.ImportVOWrapper;
+import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
+
/**
- *
+ *
* @author chm
*/
public class Importer extends Batch {
- public static final int MAX_CONCURRENT_THREADS = 3;
- private int maxConcurrentThreads = MAX_CONCURRENT_THREADS;
- private String documentsDefDirs = null;
-
- private String importDirectory = null;
-
- private ThreadPool<ImporterImpl> pool = null;
-
- public Importer(String[] args) {
- super(args);
- }
-
- @Override
- protected void initialize() throws Exception {
- super.initialize();
- String sTmp = getProps().getProperty("max.concurrent.threads");
- if(sTmp!=null && sTmp.length()>0) {
- try {
- maxConcurrentThreads = Integer.parseInt(sTmp);
- } catch(Throwable t) {}
- }
- }
-
- @Override
- protected void doProcess() throws Exception {
- Connection con = null;
- Statement st = null;
- ResultSet rs = null;
- ArrayList<ImporterImpl> threads = new ArrayList<ImporterImpl>();
- try {
- con = PoolManager.getInstance().getConnection();
- String sqlParam = "SELECT PARAM_VALUE FROM PARAMETERS WHERE PARAM_NAME='import.directory'";
- st = con.createStatement();
- rs = st.executeQuery(sqlParam);
- if(rs.next()) {
- importDirectory = rs.getString(1);
- rs.close();
- } else {
- throw new Exception("parameter 'import.directory' not set !");
- }
- StringBuffer select = new StringBuffer();
- String sql = "SELECT IMPORT_ID FROM XEM_IMPORTS WHERE STATUS=6";
- rs = st.executeQuery(sql);
- while(rs.next()) {
- threads.add(new ImporterImpl(rs.getString(1),importDirectory,documentsDefDirs));
- }
- PoolManager.getInstance().releaseConnection(con); con=null;
- pool = new ThreadPool<ImporterImpl>(threads);
- pool.start(maxConcurrentThreads);
- } catch(SQLException sqlEx) {
- sqlEx.printStackTrace();
- } finally {
- if(con!=null) {
- PoolManager.getInstance().releaseConnection(con);
- }
- }
- }
-
- @Override
- public String getResumeTraitement() {
- return "resume importer";
- }
-
- @Override
- public String typeTraitementRefCode() {
- return "importer";
- }
-
- @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";
- }
-
- private class ImporterImpl extends RunnableBatch {
- private String importId;
- private String importDirectory;
- private String confDir;
-
- // utilities
- ImportVOFull importFull=null;
-
- public ImporterImpl(String importId, String importDirectory, String confDir) {
- this.importId=importId;
- this.importDirectory=importDirectory;
- this.confDir = confDir;
- setName("ImporterImpl<".concat(importId).concat(">"));
- }
-
- @Override
- @SuppressWarnings("unchecked")
- public void run() {
- Connection con = null;
- try {
- con = PoolManager.getInstance().getConnection();
- loadImportInfos(con);
- importFull.setStatus(3);
- ImportVOWrapper.updateImportVO(con, importFull);
- PoolManager.getInstance().releaseConnection(con); con=null;
-
-// String args[] = new String[] {
-// "-g", confDir,
-// "-d",importFull.getFileType(),
-// "-b",importFull.getBudget(),importFull.getBudgetLib(),
-// "-c",importFull.getCollectivite(),importFull.getCollectiviteLib(),
-// "-u",importFull.getUtiLogin(),
-// "-i","no","no",
-// "-f",new File(new File(new File(importDirectory),importFull.getImportId()),importFull.getFileName()).getAbsolutePath()
-// };
-
- ArrayList<String> argList = new ArrayList<String>();
- argList.add("-g"); argList.add(confDir);
- argList.add("-d"); argList.add(importFull.getFileType());
- if(importFull.getBudget()!=null && importFull.getBudgetLib()!=null) {
- argList.add("-b"); argList.add(importFull.getBudget()); argList.add(importFull.getBudgetLib());
- }
- if(importFull.getCollectivite()!=null && importFull.getCollectiviteLib()!=null) {
- argList.add("-c"); argList.add(importFull.getCollectivite()); importFull.getCollectiviteLib();
- }
- argList.add("-u"); argList.add(importFull.getUtiLogin());
- argList.add("-i"); argList.add("no"); argList.add("no");
- argList.add("-f"); argList.add(new File(new File(new File(importDirectory),importFull.getImportId()),importFull.getFileName()).getAbsolutePath());
-
-
-
-
-// args[i++] = "-g";
-// args[i++] = confDir;
-// args[i++] = "-d";
-// args[i++] = importFull.getFileType();
-// args[i++] = "-b";
-// args[i++] = importFull.getBudget();
-// args[i++] = importFull.getBudgetLib();
-// args[i++] = "-c";
-// args[i++] = importFull.getCollectivite();
-// args[i++] = importFull.getCollectiviteLib();
-// args[i++] = "-u";
-// args[i++] = importFull.getUtiLogin();
-// args[i++] = "-i";
-// args[i++] = "no";
-// args[i++] = "no";
-// args[i++] = "-f";
-// args[i++] = new File(new File(new File(importDirectory),importFull.getImportId()),importFull.getFileName()).getAbsolutePath();
- String[] args = new String[argList.size()];
- args = argList.toArray(args);
- BatchRealImporter.main(args);
- importFull.setStatus(4);
- } catch(Exception ex) {
- ex.printStackTrace();
- importFull.setStatus(5);
- importFull.setErrorMessage(ex.getMessage());
- } finally {
- try {
- if(con==null) con=PoolManager.getInstance().getConnection();
- ImportVOWrapper.updateImportVO(con, importFull);
- } catch (SQLException ex1) {
- ex1.printStackTrace();
- }
- if(con!=null) {
- PoolManager.getInstance().releaseConnection(con);
- }
- getNotifier().notifyThreadStop(this);
- }
- }
-
-
- public void loadImportInfos(Connection con) throws SQLException {
- ImportVOPK pk = new ImportVOPK(importId);
- importFull = ImportVOWrapper.getImportVOFullByPk(con, pk);
- }
-
- @Override
- public String getInformations() {
- if(isAlive()) return "\t".concat(getName()).concat(": running\n");
- else return "\t".concat(getName()).concat(": idle\n");
- }
-
-
- }
+ public static final int MAX_CONCURRENT_THREADS = 3;
+ private int maxConcurrentThreads = MAX_CONCURRENT_THREADS;
+ private final String documentsDefDirs = null;
+
+ private String importDirectory = null;
+
+ private ThreadPool<ImporterImpl> pool = null;
+
+ public Importer(final String[] args) {
+ super(args);
+ }
+
+ @Override
+ protected void initialize() throws Exception {
+ super.initialize();
+ final String sTmp = this.getProps().getProperty("max.concurrent.threads");
+ if (sTmp != null && sTmp.length() > 0) {
+ try {
+ this.maxConcurrentThreads = Integer.parseInt(sTmp);
+ } catch (final Throwable t) {
+ }
+ }
+ }
+
+ @Override
+ protected void doProcess() throws Exception {
+ Connection con = null;
+ Statement st = null;
+ ResultSet rs = null;
+ final ArrayList<ImporterImpl> threads = new ArrayList<ImporterImpl>();
+ try {
+ con = PoolManager.getInstance().getConnection();
+ final String sqlParam = "SELECT PARAM_VALUE FROM PARAMETERS WHERE PARAM_NAME='import.directory'";
+ st = con.createStatement();
+ rs = st.executeQuery(sqlParam);
+ if (rs.next()) {
+ this.importDirectory = rs.getString(1);
+ rs.close();
+ } else {
+ throw new Exception("parameter 'import.directory' not set !");
+ }
+ final StringBuffer select = new StringBuffer();
+ final String sql = "SELECT IMPORT_ID FROM XEM_IMPORTS WHERE STATUS=6";
+ rs = st.executeQuery(sql);
+ while (rs.next()) {
+ threads.add(new ImporterImpl(rs.getString(1), this.importDirectory, this.documentsDefDirs));
+ }
+ PoolManager.getInstance().releaseConnection(con);
+ con = null;
+ this.pool = new ThreadPool<ImporterImpl>(threads);
+ this.pool.start(this.maxConcurrentThreads);
+ } catch (final SQLException sqlEx) {
+ sqlEx.printStackTrace();
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ }
+ }
+
+ @Override
+ public String getResumeTraitement() {
+ return "resume importer";
+ }
+
+ @Override
+ public String typeTraitementRefCode() {
+ return "importer";
+ }
+
+ @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";
+ }
+
+ private class ImporterImpl extends RunnableBatch {
+ private final String importId;
+ private final String importDirectory;
+ private final String confDir;
+
+ // utilities
+ ImportVOFull importFull = null;
+
+ public ImporterImpl(final String importId, final String importDirectory, final String confDir) {
+ this.importId = importId;
+ this.importDirectory = importDirectory;
+ this.confDir = confDir;
+ this.setName("ImporterImpl<".concat(importId).concat(">"));
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public void run() {
+ Connection con = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ this.loadImportInfos(con);
+ this.importFull.setStatus(3);
+ ImportVOWrapper.updateImportVO(con, this.importFull);
+ PoolManager.getInstance().releaseConnection(con);
+ con = null;
+
+ // String args[] = new String[] {
+ // "-g", confDir,
+ // "-d",importFull.getFileType(),
+ // "-b",importFull.getBudget(),importFull.getBudgetLib(),
+ // "-c",importFull.getCollectivite(),importFull.getCollectiviteLib(),
+ // "-u",importFull.getUtiLogin(),
+ // "-i","no","no",
+ // "-f",new File(new File(new File(importDirectory),importFull.getImportId()),importFull.getFileName()).getAbsolutePath()
+ // };
+
+ final ArrayList<String> argList = new ArrayList<String>();
+ argList.add("-g");
+ argList.add(this.confDir);
+ argList.add("-d");
+ argList.add(this.importFull.getFileType());
+ if (this.importFull.getBudget() != null && this.importFull.getBudgetLib() != null) {
+ argList.add("-b");
+ argList.add(this.importFull.getBudget());
+ argList.add(this.importFull.getBudgetLib());
+ }
+ if (this.importFull.getCollectivite() != null && this.importFull.getCollectiviteLib() != null) {
+ argList.add("-c");
+ argList.add(this.importFull.getCollectivite());
+ this.importFull.getCollectiviteLib();
+ }
+ argList.add("-u");
+ argList.add(this.importFull.getUtiLogin());
+ argList.add("-i");
+ argList.add("no");
+ argList.add("no");
+ argList.add("-f");
+ argList.add(new File(new File(new File(this.importDirectory), this.importFull.getImportId()), this.importFull.getFileName()).getAbsolutePath());
+
+ // args[i++] = "-g";
+ // args[i++] = confDir;
+ // args[i++] = "-d";
+ // args[i++] = importFull.getFileType();
+ // args[i++] = "-b";
+ // args[i++] = importFull.getBudget();
+ // args[i++] = importFull.getBudgetLib();
+ // args[i++] = "-c";
+ // args[i++] = importFull.getCollectivite();
+ // args[i++] = importFull.getCollectiviteLib();
+ // args[i++] = "-u";
+ // args[i++] = importFull.getUtiLogin();
+ // args[i++] = "-i";
+ // args[i++] = "no";
+ // args[i++] = "no";
+ // args[i++] = "-f";
+ // args[i++] = new File(new File(new File(importDirectory),importFull.getImportId()),importFull.getFileName()).getAbsolutePath();
+ String[] args = new String[argList.size()];
+ args = argList.toArray(args);
+ BatchRealImporter.main(args);
+ this.importFull.setStatus(4);
+ } catch (final Exception ex) {
+ ex.printStackTrace();
+ this.importFull.setStatus(5);
+ this.importFull.setErrorMessage(ex.getMessage());
+ } finally {
+ try {
+ if (con == null) {
+ con = PoolManager.getInstance().getConnection();
+ }
+ ImportVOWrapper.updateImportVO(con, this.importFull);
+ } catch (final SQLException ex1) {
+ ex1.printStackTrace();
+ }
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ this.getNotifier().notifyThreadStop(this);
+ }
+ }
+
+ public void loadImportInfos(final Connection con) throws SQLException {
+ final ImportVOPK pk = new ImportVOPK(this.importId);
+ this.importFull = ImportVOWrapper.getImportVOFullByPk(con, pk);
+ }
+
+ @Override
+ public String getInformations() {
+ if (this.isAlive()) {
+ return "\t".concat(this.getName()).concat(": running\n");
+ } else {
+ return "\t".concat(this.getName()).concat(": idle\n");
+ }
+ }
+
+ }
}
*/
package fr.gouv.finances.dgfip.xemelios.batch.imports;
-import fr.gouv.finances.dgfip.xemelios.batch.Batch;
-import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.ImportVOFull;
-import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.ImportVOPK;
-import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.ImportVOWrapper;
-import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
-import fr.gouv.finances.dgfip.xemelios.data.utils.jdbc.JdbcUtils;
-import fr.gouv.finances.dgfip.xemelios.data.utils.jdbc.PStmtBinder;
import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
+
import org.apache.log4j.Logger;
+import fr.gouv.finances.dgfip.xemelios.batch.Batch;
+import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.ImportVOFull;
+import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.ImportVOPK;
+import fr.gouv.finances.dgfip.xemelios.batch.imports.utils.ImportVOWrapper;
+import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
+import fr.gouv.finances.dgfip.xemelios.data.utils.jdbc.JdbcUtils;
+import fr.gouv.finances.dgfip.xemelios.data.utils.jdbc.PStmtBinder;
+
/**
- *
+ *
* @author chm
*/
public class PostImportCleaner extends Batch {
- public static final int MAX_CONCURRENT_THREADS = 3;
- private int maxConcurrentThreads = MAX_CONCURRENT_THREADS;
- private String importDirectory = null;
-
- private ThreadPool<PostImportCleanerImpl> pool = null;
-
- public PostImportCleaner(String args[]) {
- super(args);
- }
-
- @Override
- protected void initialize() throws Exception {
- super.initialize();
- String sTmp = getProps().getProperty("max.concurrent.threads");
- if(sTmp!=null && sTmp.length()>0) {
- try {
- maxConcurrentThreads = Integer.parseInt(sTmp);
- } catch(Throwable t) {}
- }
- }
-
-
-
- @Override
- protected void doProcess() throws Exception {
- Connection con = null;
- Statement st = null;
- ResultSet rs = null;
- ArrayList<PostImportCleanerImpl> threads = new ArrayList<PostImportCleanerImpl>();
- try {
- con = PoolManager.getInstance().getConnection();
- String sqlParam = "SELECT PARAM_VALUE FROM PARAMETERS WHERE PARAM_NAME='import.directory'";
- st = con.createStatement();
- rs = st.executeQuery(sqlParam);
- if(rs.next()) {
- importDirectory = rs.getString(1);
- rs.close();
- } else {
- throw new Exception("parameter 'import.directory' not set !");
- }
- String sql = "SELECT IMPORT_ID FROM XEM_IMPORTS WHERE STATUS IN (4,5) AND CLEANED IS NULL";
- rs = st.executeQuery(sql);
- while(rs.next()) {
- threads.add(new PostImportCleanerImpl(rs.getString(1),importDirectory));
- }
- PoolManager.getInstance().releaseConnection(con); con=null;
- pool = new ThreadPool<PostImportCleanerImpl>(threads);
- pool.start(maxConcurrentThreads);
- } catch(SQLException sqlEx) {
- // TODO
- } finally {
- if(con!=null) PoolManager.getInstance().releaseConnection(con);
- }
- }
-
- @Override
- public String getResumeTraitement() {
- return "post-import.cleaner.resume";
- }
-
- @Override
- public String typeTraitementRefCode() {
- return "post-import.cleaner";
-
- }
-
- @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";
- }
-
- private class PostImportCleanerImpl extends RunnableBatch {
- private final Logger logger = Logger.getLogger(PostImportCleanerImpl.class);
- private String importId;
- private String importDirectory;
- // utilities
- private ImportVOFull importFull;
-
- public PostImportCleanerImpl(String importId, String importDirectory) {
- this.importDirectory=importDirectory;
- this.importId=importId;
- }
-
- @Override
- @SuppressWarnings("unchecked")
- public void run() {
- Connection con = null;
- try {
- con = PoolManager.getInstance().getConnection();
- loadImportInfos(con);
- File directory = new File(new File(importDirectory),importFull.getImportId());
- File sourceFile = new File(directory,importFull.getFileName());
- ArrayList<File> excludeFiles = new ArrayList<File>();
- if(importFull.getStatus()==5) {
- excludeFiles.add(sourceFile);
- }
- cleanDirectory(directory,excludeFiles);
- JdbcUtils.executeUpdate(con,"UPDATE XEM_IMPORTS SET CLEANED=1 WHERE IMPORT_ID=?",new PStmtBinder.SimplePStmtBinderBuilder().add(importId).toPStmtBinder());
- } catch(SQLException sqlEx) {
- logger.error("PostImportCleanerImpl.run()",sqlEx);
- } finally {
- if(con!=null) {
- PoolManager.getInstance().releaseConnection(con);
- }
- getNotifier().notifyThreadStop(this);
- }
- }
-
- private void cleanDirectory(File directory, List<File> excludes) {
- for(File f:directory.listFiles()) {
- if(excludes.contains(f)) continue;
- else if(f.isDirectory()) {
- cleanDirectory(f, excludes);
- } else {
- f.delete();
- }
- }
- if(directory.listFiles().length==0 && !excludes.contains(directory)) {
- directory.delete();
- }
- }
-
- @SuppressWarnings("unchecked")
- public void loadImportInfos(Connection con) throws SQLException {
- ImportVOPK pk = new ImportVOPK(importId);
- importFull = ImportVOWrapper.getImportVOFullByPk(con, pk);
-// files = XemImportsFilesVOWrapper.getAllXemImportsFilesVOFullBy(con, XemImportsFilesVOFull.class, "SELECT "+XemImportsFilesVOWrapper.getSelectFieldsClause()+" FROM XEM_IMPORTS_FILES WHERE IMPORT_ID='"+importId+"'");
- }
-
- @Override
- public String getInformations() {
- if(isAlive()) return "\t".concat(getName()).concat(": running\n");
- else return "\t".concat(getName()).concat(": idle\n");
- }
-
- }
-
+ public static final int MAX_CONCURRENT_THREADS = 3;
+ private int maxConcurrentThreads = MAX_CONCURRENT_THREADS;
+ private String importDirectory = null;
+
+ private ThreadPool<PostImportCleanerImpl> pool = null;
+
+ public PostImportCleaner(final String args[]) {
+ super(args);
+ }
+
+ @Override
+ protected void initialize() throws Exception {
+ super.initialize();
+ final String sTmp = this.getProps().getProperty("max.concurrent.threads");
+ if (sTmp != null && sTmp.length() > 0) {
+ try {
+ this.maxConcurrentThreads = Integer.parseInt(sTmp);
+ } catch (final Throwable t) {
+ }
+ }
+ }
+
+ @Override
+ protected void doProcess() throws Exception {
+ Connection con = null;
+ Statement st = null;
+ ResultSet rs = null;
+ final ArrayList<PostImportCleanerImpl> threads = new ArrayList<PostImportCleanerImpl>();
+ try {
+ con = PoolManager.getInstance().getConnection();
+ final String sqlParam = "SELECT PARAM_VALUE FROM PARAMETERS WHERE PARAM_NAME='import.directory'";
+ st = con.createStatement();
+ rs = st.executeQuery(sqlParam);
+ if (rs.next()) {
+ this.importDirectory = rs.getString(1);
+ rs.close();
+ } else {
+ throw new Exception("parameter 'import.directory' not set !");
+ }
+ final String sql = "SELECT IMPORT_ID FROM XEM_IMPORTS WHERE STATUS IN (4,5) AND CLEANED IS NULL";
+ rs = st.executeQuery(sql);
+ while (rs.next()) {
+ threads.add(new PostImportCleanerImpl(rs.getString(1), this.importDirectory));
+ }
+ PoolManager.getInstance().releaseConnection(con);
+ con = null;
+ this.pool = new ThreadPool<PostImportCleanerImpl>(threads);
+ this.pool.start(this.maxConcurrentThreads);
+ } catch (final SQLException sqlEx) {
+ // TODO
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ }
+ }
+
+ @Override
+ public String getResumeTraitement() {
+ return "post-import.cleaner.resume";
+ }
+
+ @Override
+ public String typeTraitementRefCode() {
+ return "post-import.cleaner";
+
+ }
+
+ @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";
+ }
+
+ private class PostImportCleanerImpl extends RunnableBatch {
+ private final Logger logger = Logger.getLogger(PostImportCleanerImpl.class);
+ private final String importId;
+ private final String importDirectory;
+ // utilities
+ private ImportVOFull importFull;
+
+ public PostImportCleanerImpl(final String importId, final String importDirectory) {
+ this.importDirectory = importDirectory;
+ this.importId = importId;
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public void run() {
+ Connection con = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ this.loadImportInfos(con);
+ final File directory = new File(new File(this.importDirectory), this.importFull.getImportId());
+ final File sourceFile = new File(directory, this.importFull.getFileName());
+ final ArrayList<File> excludeFiles = new ArrayList<File>();
+ if (this.importFull.getStatus() == 5) {
+ excludeFiles.add(sourceFile);
+ }
+ this.cleanDirectory(directory, excludeFiles);
+ JdbcUtils.executeUpdate(con, "UPDATE XEM_IMPORTS SET CLEANED=1 WHERE IMPORT_ID=?", new PStmtBinder.SimplePStmtBinderBuilder().add(this.importId).toPStmtBinder());
+ } catch (final SQLException sqlEx) {
+ this.logger.error("PostImportCleanerImpl.run()", sqlEx);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ this.getNotifier().notifyThreadStop(this);
+ }
+ }
+
+ private void cleanDirectory(final File directory, final List<File> excludes) {
+ for (final File f : directory.listFiles()) {
+ if (excludes.contains(f)) {
+ continue;
+ } else if (f.isDirectory()) {
+ this.cleanDirectory(f, excludes);
+ } else {
+ f.delete();
+ }
+ }
+ if (directory.listFiles().length == 0 && !excludes.contains(directory)) {
+ directory.delete();
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ public void loadImportInfos(final Connection con) throws SQLException {
+ final ImportVOPK pk = new ImportVOPK(this.importId);
+ this.importFull = ImportVOWrapper.getImportVOFullByPk(con, pk);
+ // files = XemImportsFilesVOWrapper.getAllXemImportsFilesVOFullBy(con, XemImportsFilesVOFull.class,
+ // "SELECT "+XemImportsFilesVOWrapper.getSelectFieldsClause()+" FROM XEM_IMPORTS_FILES WHERE IMPORT_ID='"+importId+"'");
+ }
+
+ @Override
+ public String getInformations() {
+ if (this.isAlive()) {
+ return "\t".concat(this.getName()).concat(": running\n");
+ } else {
+ return "\t".concat(this.getName()).concat(": idle\n");
+ }
+ }
+
+ }
}
import java.sql.ResultSet;
import java.sql.SQLException;
-
import java.sql.Timestamp;
+
import org.apache.log4j.Logger;
public class ImportVOFull extends ImportVOPK implements java.io.Serializable {
- private static final Logger logger = Logger.getLogger(ImportVOFull.class);
-
- /**
- * MAPS UTI_LOGIN column.
- */
- protected java.lang.String utiLogin;
- protected boolean isUtiLoginSet = false;
- protected boolean isUtiLoginModified = false;
- /**
- * MAPS FILE_NAME column.
- */
- protected java.lang.String fileName;
- protected boolean isFileNameSet = false;
- protected boolean isFileNameModified = false;
- /**
- * MAPS FILE_TYPE column.
- */
- protected java.lang.String fileType;
- protected boolean isFileTypeSet = false;
- protected boolean isFileTypeModified = false;
- /**
- * MAPS SUBMIT_DATE column.
- */
- protected java.sql.Timestamp submitDate;
- protected boolean isSubmitDateSet = false;
- protected boolean isSubmitDateModified = false;
- /**
- * MAPS END_DATE column.
- */
- protected java.sql.Timestamp endDate;
- protected boolean isEndDateSet = false;
- protected boolean isEndDateModified = false;
- /**
- * MAPS STATUS column.
- */
- protected java.lang.Integer status;
- protected boolean isStatusSet = false;
- protected boolean isStatusModified = false;
- /**
- * MAPS COLLECTIVITE column.
- */
- protected java.lang.String collectivite;
- protected boolean isCollectiviteSet = false;
- protected boolean isCollectiviteModified = false;
- /**
- * MAPS BUDGET column.
- */
- protected java.lang.String budget;
- protected boolean isBudgetSet = false;
- protected boolean isBudgetModified = false;
- /**
- * MAPS COLLECTIVITE_LIB column.
- */
- protected java.lang.String collectiviteLib;
- protected boolean isCollectiviteLibSet = false;
- protected boolean isCollectiviteLibModified = false;
- /**
- * MAPS BUDGET_LIB column.
- */
- protected java.lang.String budgetLib;
- protected boolean isBudgetLibSet = false;
- protected boolean isBudgetLibModified = false;
- /**
- * MAPS ERROR_MESSAGE column.
- */
- protected java.lang.String errorMessage;
- protected boolean isErrorMessageSet = false;
- protected boolean isErrorMessageModified = false;
-
- /**
- * Default Constructor
- */
- public ImportVOFull() {
- super();
- }
-
- /**
- * Constructs a new Full object, initializing only PK fileds.
- */
- public ImportVOFull(java.lang.String importId) {
- super(importId);
- }
-
- public ImportVOFull(ResultSet rs) throws SQLException {
- this();
- int i = 1;
- setInnerUtiLogin(rs.getString(i++));
- setInnerImportId(rs.getString(i++));
- setInnerFileName(rs.getString(i++));
- setInnerFileType(rs.getString(i++));
- try {
- setInnerSubmitDate(rs.getTimestamp(i++));
- } catch(SQLException sqlEx) {
- // mysql jdbc driver throws a SQLException when value is null
- }
- try {
- setInnerEndDate(rs.getTimestamp(i++));
- } catch(SQLException sqlEx) {
- // mysql jdbc driver throws a SQLException when value is null
- }
- int statusTmp = rs.getInt(i++);
- if (!rs.wasNull()) {
- setInnerStatus(new java.lang.Integer(statusTmp));
- }
- setInnerCollectivite(rs.getString(i++));
- setInnerBudget(rs.getString(i++));
- setInnerCollectiviteLib(rs.getString(i++));
- setInnerBudgetLib(rs.getString(i++));
- setInnerErrorMessage(rs.getString(i++));
- }
-
- public java.lang.String getUtiLogin() {
- return utiLogin;
- }
-
- public void setUtiLogin(java.lang.String utiLogin) {
- this.utiLogin = utiLogin;
- this.isUtiLoginModified = true;
- }
-
- protected void setInnerUtiLogin(java.lang.String utiLogin) {
- this.utiLogin = utiLogin;
- this.isUtiLoginSet = true;
- }
-
- public java.lang.String getStrUtiLogin(java.lang.String returnInCaseOfNull) {
- return utiLogin != null ? utiLogin.toString() : returnInCaseOfNull;
- }
-
- public java.lang.String getFileName() {
- return fileName;
- }
-
- public void setFileName(java.lang.String fileName) {
- this.fileName = fileName;
- this.isFileNameModified = true;
- }
-
- protected void setInnerFileName(java.lang.String fileName) {
- this.fileName = fileName;
- this.isFileNameSet = true;
- }
-
- public java.lang.String getStrFileName(java.lang.String returnInCaseOfNull) {
- return fileName != null ? fileName.toString() : returnInCaseOfNull;
- }
-
- public java.lang.String getFileType() {
- return fileType;
- }
-
- public void setFileType(java.lang.String fileType) {
- this.fileType = fileType;
- this.isFileTypeModified = true;
- }
-
- protected void setInnerFileType(java.lang.String fileType) {
- this.fileType = fileType;
- this.isFileTypeSet = true;
- }
-
- public java.lang.String getStrFileType(java.lang.String returnInCaseOfNull) {
- return fileType != null ? fileType.toString() : returnInCaseOfNull;
- }
-
- public java.sql.Timestamp getSubmitDate() {
- return submitDate;
- }
-
- public void setSubmitDate(java.sql.Timestamp submitDate) {
- this.submitDate = submitDate;
- this.isSubmitDateModified = true;
- }
-
- protected void setInnerSubmitDate(java.sql.Timestamp submitDate) {
- this.submitDate = submitDate;
- this.isSubmitDateSet = true;
- }
-
- public java.lang.String getStrSubmitDate(java.lang.String returnInCaseOfNull) {
- return submitDate != null ? submitDate.toString() : returnInCaseOfNull;
- }
-
- public java.sql.Timestamp getEndDate() {
- return endDate;
- }
-
- public void setEndDate(java.sql.Timestamp endDate) {
- this.endDate = endDate;
- this.isEndDateModified = true;
- }
-
- protected void setInnerEndDate(java.sql.Timestamp endDate) {
- this.endDate = endDate;
- this.isEndDateSet = true;
- }
-
- public java.lang.String getStrEndDate(java.lang.String returnInCaseOfNull) {
- return endDate != null ? endDate.toString() : returnInCaseOfNull;
- }
-
- public java.lang.Integer getStatus() {
- return status;
- }
-
- public void setStatus(java.lang.Integer status) {
- this.status = status;
- this.isStatusModified = true;
- if(status==5 || status==4)
- setEndDate(new Timestamp(System.currentTimeMillis()));
-
- }
-
- protected void setInnerStatus(java.lang.Integer status) {
- this.status = status;
- this.isStatusSet = true;
- }
-
- public java.lang.String getStrStatus(java.lang.String returnInCaseOfNull) {
- return status != null ? status.toString() : returnInCaseOfNull;
- }
-
- public java.lang.String getCollectivite() {
- return collectivite;
- }
-
- public void setCollectivite(java.lang.String collectivite) {
- this.collectivite = collectivite;
- this.isCollectiviteModified = true;
- }
-
- protected void setInnerCollectivite(java.lang.String collectivite) {
- this.collectivite = collectivite;
- this.isCollectiviteSet = true;
- }
-
- public java.lang.String getStrCollectivite(java.lang.String returnInCaseOfNull) {
- return collectivite != null ? collectivite.toString() : returnInCaseOfNull;
- }
-
- public java.lang.String getBudget() {
- return budget;
- }
-
- public void setBudget(java.lang.String budget) {
- this.budget = budget;
- this.isBudgetModified = true;
- }
-
- protected void setInnerBudget(java.lang.String budget) {
- this.budget = budget;
- this.isBudgetSet = true;
- }
-
- public java.lang.String getStrBudget(java.lang.String returnInCaseOfNull) {
- return budget != null ? budget.toString() : returnInCaseOfNull;
- }
-
- public java.lang.String getCollectiviteLib() {
- return collectiviteLib;
- }
-
- public void setCollectiviteLib(java.lang.String collectiviteLib) {
- this.collectiviteLib = collectiviteLib;
- this.isCollectiviteLibModified = true;
- }
-
- protected void setInnerCollectiviteLib(java.lang.String collectiviteLib) {
- this.collectiviteLib = collectiviteLib;
- this.isCollectiviteLibSet = true;
- }
-
- public java.lang.String getStrCollectiviteLib(java.lang.String returnInCaseOfNull) {
- return collectiviteLib != null ? collectiviteLib.toString() : returnInCaseOfNull;
- }
-
- public java.lang.String getBudgetLib() {
- return budgetLib;
- }
-
- public void setBudgetLib(java.lang.String budgetLib) {
- this.budgetLib = budgetLib;
- this.isBudgetLibModified = true;
- }
-
- protected void setInnerBudgetLib(java.lang.String budgetLib) {
- this.budgetLib = budgetLib;
- this.isBudgetLibSet = true;
- }
-
- public java.lang.String getStrBudgetLib(java.lang.String returnInCaseOfNull) {
- return budgetLib != null ? budgetLib.toString() : returnInCaseOfNull;
- }
-
- public java.lang.String getErrorMessage() {
- return errorMessage;
- }
-
- public void setErrorMessage(java.lang.String errorMessage) {
- this.errorMessage = errorMessage;
- this.isErrorMessageModified = true;
- }
-
- protected void setInnerErrorMessage(java.lang.String errorMessage) {
- this.errorMessage = errorMessage;
- this.isErrorMessageSet = true;
- }
-
- public java.lang.String getStrErrorMessage(java.lang.String returnInCaseOfNull) {
- return errorMessage != null ? errorMessage.toString() : returnInCaseOfNull;
- }
-
- public void validate() throws IllegalStateException {
- if (utiLogin == null) {
- throw new IllegalStateException("UTI_LOGIN " + VoConstants.NOT_NULL_MSG);
- }
- if (utiLogin != null && utiLogin.length() > UTI_LOGIN_LEN) {
- throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX + "UTI_LOGIN" + VoConstants.TOO_LONG_MSG_SUFX);
- }
- if (importId == null) {
- throw new IllegalStateException("IMPORT_ID " + VoConstants.NOT_NULL_MSG);
- }
- if (importId != null && importId.length() > IMPORT_ID_LEN) {
- throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX + "IMPORT_ID" + VoConstants.TOO_LONG_MSG_SUFX);
- }
- if (fileName == null) {
- throw new IllegalStateException("FILE_NAME " + VoConstants.NOT_NULL_MSG);
- }
- if (fileName != null && fileName.length() > FILE_NAME_LEN) {
- throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX + "FILE_NAME" + VoConstants.TOO_LONG_MSG_SUFX);
- }
- if (fileType == null) {
- throw new IllegalStateException("FILE_TYPE " + VoConstants.NOT_NULL_MSG);
- }
- if (fileType != null && fileType.length() > FILE_TYPE_LEN) {
- throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX + "FILE_TYPE" + VoConstants.TOO_LONG_MSG_SUFX);
- }
- if (status == null) {
- throw new IllegalStateException("STATUS " + VoConstants.NOT_NULL_MSG);
- }
- if (collectivite != null && collectivite.length() > COLLECTIVITE_LEN) {
- throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX + "COLLECTIVITE" + VoConstants.TOO_LONG_MSG_SUFX);
- }
- if (budget != null && budget.length() > BUDGET_LEN) {
- throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX + "BUDGET" + VoConstants.TOO_LONG_MSG_SUFX);
- }
- if (collectiviteLib != null && collectiviteLib.length() > COLLECTIVITE_LIB_LEN) {
- throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX + "COLLECTIVITE_LIB" + VoConstants.TOO_LONG_MSG_SUFX);
- }
- if (budgetLib != null && budgetLib.length() > BUDGET_LIB_LEN) {
- throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX + "BUDGET_LIB" + VoConstants.TOO_LONG_MSG_SUFX);
- }
- if (errorMessage != null && errorMessage.length() > ERROR_MESSAGE_LEN) {
- throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX + "ERROR_MESSAGE" + VoConstants.TOO_LONG_MSG_SUFX);
- }
- }
- public void determinateStatus() {
- if(status==5) return;
- boolean correct = true;
- if(collectivite==null || collectivite.length()==0) correct = false;
- if(collectiviteLib==null || collectiviteLib.length()==0) correct = false;
- if(budget==null || budget.length()==0) correct=false;
- if(budgetLib==null || budgetLib.length()==0) correct=false;
- if(fileType==null || fileType.length()==0) correct=false;
- if(correct) setStatus(6);
- else setStatus(2);
- }
+ private static final Logger logger = Logger.getLogger(ImportVOFull.class);
+
+ /**
+ * MAPS UTI_LOGIN column.
+ */
+ protected java.lang.String utiLogin;
+ protected boolean isUtiLoginSet = false;
+ protected boolean isUtiLoginModified = false;
+ /**
+ * MAPS FILE_NAME column.
+ */
+ protected java.lang.String fileName;
+ protected boolean isFileNameSet = false;
+ protected boolean isFileNameModified = false;
+ /**
+ * MAPS FILE_TYPE column.
+ */
+ protected java.lang.String fileType;
+ protected boolean isFileTypeSet = false;
+ protected boolean isFileTypeModified = false;
+ /**
+ * MAPS SUBMIT_DATE column.
+ */
+ protected java.sql.Timestamp submitDate;
+ protected boolean isSubmitDateSet = false;
+ protected boolean isSubmitDateModified = false;
+ /**
+ * MAPS END_DATE column.
+ */
+ protected java.sql.Timestamp endDate;
+ protected boolean isEndDateSet = false;
+ protected boolean isEndDateModified = false;
+ /**
+ * MAPS STATUS column.
+ */
+ protected java.lang.Integer status;
+ protected boolean isStatusSet = false;
+ protected boolean isStatusModified = false;
+ /**
+ * MAPS COLLECTIVITE column.
+ */
+ protected java.lang.String collectivite;
+ protected boolean isCollectiviteSet = false;
+ protected boolean isCollectiviteModified = false;
+ /**
+ * MAPS BUDGET column.
+ */
+ protected java.lang.String budget;
+ protected boolean isBudgetSet = false;
+ protected boolean isBudgetModified = false;
+ /**
+ * MAPS COLLECTIVITE_LIB column.
+ */
+ protected java.lang.String collectiviteLib;
+ protected boolean isCollectiviteLibSet = false;
+ protected boolean isCollectiviteLibModified = false;
+ /**
+ * MAPS BUDGET_LIB column.
+ */
+ protected java.lang.String budgetLib;
+ protected boolean isBudgetLibSet = false;
+ protected boolean isBudgetLibModified = false;
+ /**
+ * MAPS ERROR_MESSAGE column.
+ */
+ protected java.lang.String errorMessage;
+ protected boolean isErrorMessageSet = false;
+ protected boolean isErrorMessageModified = false;
+
+ /**
+ * Default Constructor
+ */
+ public ImportVOFull() {
+ super();
+ }
+
+ /**
+ * Constructs a new Full object, initializing only PK fileds.
+ */
+ public ImportVOFull(final java.lang.String importId) {
+ super(importId);
+ }
+
+ public ImportVOFull(final ResultSet rs) throws SQLException {
+ this();
+ int i = 1;
+ this.setInnerUtiLogin(rs.getString(i++));
+ this.setInnerImportId(rs.getString(i++));
+ this.setInnerFileName(rs.getString(i++));
+ this.setInnerFileType(rs.getString(i++));
+ try {
+ this.setInnerSubmitDate(rs.getTimestamp(i++));
+ } catch (final SQLException sqlEx) {
+ // mysql jdbc driver throws a SQLException when value is null
+ }
+ try {
+ this.setInnerEndDate(rs.getTimestamp(i++));
+ } catch (final SQLException sqlEx) {
+ // mysql jdbc driver throws a SQLException when value is null
+ }
+ final int statusTmp = rs.getInt(i++);
+ if (!rs.wasNull()) {
+ this.setInnerStatus(new java.lang.Integer(statusTmp));
+ }
+ this.setInnerCollectivite(rs.getString(i++));
+ this.setInnerBudget(rs.getString(i++));
+ this.setInnerCollectiviteLib(rs.getString(i++));
+ this.setInnerBudgetLib(rs.getString(i++));
+ this.setInnerErrorMessage(rs.getString(i++));
+ }
+
+ public java.lang.String getUtiLogin() {
+ return this.utiLogin;
+ }
+
+ public void setUtiLogin(final java.lang.String utiLogin) {
+ this.utiLogin = utiLogin;
+ this.isUtiLoginModified = true;
+ }
+
+ protected void setInnerUtiLogin(final java.lang.String utiLogin) {
+ this.utiLogin = utiLogin;
+ this.isUtiLoginSet = true;
+ }
+
+ public java.lang.String getStrUtiLogin(final java.lang.String returnInCaseOfNull) {
+ return this.utiLogin != null ? this.utiLogin.toString() : returnInCaseOfNull;
+ }
+
+ public java.lang.String getFileName() {
+ return this.fileName;
+ }
+
+ public void setFileName(final java.lang.String fileName) {
+ this.fileName = fileName;
+ this.isFileNameModified = true;
+ }
+
+ protected void setInnerFileName(final java.lang.String fileName) {
+ this.fileName = fileName;
+ this.isFileNameSet = true;
+ }
+
+ public java.lang.String getStrFileName(final java.lang.String returnInCaseOfNull) {
+ return this.fileName != null ? this.fileName.toString() : returnInCaseOfNull;
+ }
+
+ public java.lang.String getFileType() {
+ return this.fileType;
+ }
+
+ public void setFileType(final java.lang.String fileType) {
+ this.fileType = fileType;
+ this.isFileTypeModified = true;
+ }
+
+ protected void setInnerFileType(final java.lang.String fileType) {
+ this.fileType = fileType;
+ this.isFileTypeSet = true;
+ }
+
+ public java.lang.String getStrFileType(final java.lang.String returnInCaseOfNull) {
+ return this.fileType != null ? this.fileType.toString() : returnInCaseOfNull;
+ }
+
+ public java.sql.Timestamp getSubmitDate() {
+ return this.submitDate;
+ }
+
+ public void setSubmitDate(final java.sql.Timestamp submitDate) {
+ this.submitDate = submitDate;
+ this.isSubmitDateModified = true;
+ }
+
+ protected void setInnerSubmitDate(final java.sql.Timestamp submitDate) {
+ this.submitDate = submitDate;
+ this.isSubmitDateSet = true;
+ }
+
+ public java.lang.String getStrSubmitDate(final java.lang.String returnInCaseOfNull) {
+ return this.submitDate != null ? this.submitDate.toString() : returnInCaseOfNull;
+ }
+
+ public java.sql.Timestamp getEndDate() {
+ return this.endDate;
+ }
+
+ public void setEndDate(final java.sql.Timestamp endDate) {
+ this.endDate = endDate;
+ this.isEndDateModified = true;
+ }
+
+ protected void setInnerEndDate(final java.sql.Timestamp endDate) {
+ this.endDate = endDate;
+ this.isEndDateSet = true;
+ }
+
+ public java.lang.String getStrEndDate(final java.lang.String returnInCaseOfNull) {
+ return this.endDate != null ? this.endDate.toString() : returnInCaseOfNull;
+ }
+
+ public java.lang.Integer getStatus() {
+ return this.status;
+ }
+
+ public void setStatus(final java.lang.Integer status) {
+ this.status = status;
+ this.isStatusModified = true;
+ if (status == 5 || status == 4) {
+ this.setEndDate(new Timestamp(System.currentTimeMillis()));
+ }
+
+ }
+
+ protected void setInnerStatus(final java.lang.Integer status) {
+ this.status = status;
+ this.isStatusSet = true;
+ }
+
+ public java.lang.String getStrStatus(final java.lang.String returnInCaseOfNull) {
+ return this.status != null ? this.status.toString() : returnInCaseOfNull;
+ }
+
+ public java.lang.String getCollectivite() {
+ return this.collectivite;
+ }
+
+ public void setCollectivite(final java.lang.String collectivite) {
+ this.collectivite = collectivite;
+ this.isCollectiviteModified = true;
+ }
+
+ protected void setInnerCollectivite(final java.lang.String collectivite) {
+ this.collectivite = collectivite;
+ this.isCollectiviteSet = true;
+ }
+
+ public java.lang.String getStrCollectivite(final java.lang.String returnInCaseOfNull) {
+ return this.collectivite != null ? this.collectivite.toString() : returnInCaseOfNull;
+ }
+
+ public java.lang.String getBudget() {
+ return this.budget;
+ }
+
+ public void setBudget(final java.lang.String budget) {
+ this.budget = budget;
+ this.isBudgetModified = true;
+ }
+
+ protected void setInnerBudget(final java.lang.String budget) {
+ this.budget = budget;
+ this.isBudgetSet = true;
+ }
+
+ public java.lang.String getStrBudget(final java.lang.String returnInCaseOfNull) {
+ return this.budget != null ? this.budget.toString() : returnInCaseOfNull;
+ }
+
+ public java.lang.String getCollectiviteLib() {
+ return this.collectiviteLib;
+ }
+
+ public void setCollectiviteLib(final java.lang.String collectiviteLib) {
+ this.collectiviteLib = collectiviteLib;
+ this.isCollectiviteLibModified = true;
+ }
+
+ protected void setInnerCollectiviteLib(final java.lang.String collectiviteLib) {
+ this.collectiviteLib = collectiviteLib;
+ this.isCollectiviteLibSet = true;
+ }
+
+ public java.lang.String getStrCollectiviteLib(final java.lang.String returnInCaseOfNull) {
+ return this.collectiviteLib != null ? this.collectiviteLib.toString() : returnInCaseOfNull;
+ }
+
+ public java.lang.String getBudgetLib() {
+ return this.budgetLib;
+ }
+
+ public void setBudgetLib(final java.lang.String budgetLib) {
+ this.budgetLib = budgetLib;
+ this.isBudgetLibModified = true;
+ }
+
+ protected void setInnerBudgetLib(final java.lang.String budgetLib) {
+ this.budgetLib = budgetLib;
+ this.isBudgetLibSet = true;
+ }
+
+ public java.lang.String getStrBudgetLib(final java.lang.String returnInCaseOfNull) {
+ return this.budgetLib != null ? this.budgetLib.toString() : returnInCaseOfNull;
+ }
+
+ public java.lang.String getErrorMessage() {
+ return this.errorMessage;
+ }
+
+ public void setErrorMessage(final java.lang.String errorMessage) {
+ this.errorMessage = errorMessage;
+ this.isErrorMessageModified = true;
+ }
+
+ protected void setInnerErrorMessage(final java.lang.String errorMessage) {
+ this.errorMessage = errorMessage;
+ this.isErrorMessageSet = true;
+ }
+
+ public java.lang.String getStrErrorMessage(final java.lang.String returnInCaseOfNull) {
+ return this.errorMessage != null ? this.errorMessage.toString() : returnInCaseOfNull;
+ }
+
+ public void validate() throws IllegalStateException {
+ if (this.utiLogin == null) {
+ throw new IllegalStateException("UTI_LOGIN " + VoConstants.NOT_NULL_MSG);
+ }
+ if (this.utiLogin != null && this.utiLogin.length() > UTI_LOGIN_LEN) {
+ throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX + "UTI_LOGIN" + VoConstants.TOO_LONG_MSG_SUFX);
+ }
+ if (this.importId == null) {
+ throw new IllegalStateException("IMPORT_ID " + VoConstants.NOT_NULL_MSG);
+ }
+ if (this.importId != null && this.importId.length() > IMPORT_ID_LEN) {
+ throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX + "IMPORT_ID" + VoConstants.TOO_LONG_MSG_SUFX);
+ }
+ if (this.fileName == null) {
+ throw new IllegalStateException("FILE_NAME " + VoConstants.NOT_NULL_MSG);
+ }
+ if (this.fileName != null && this.fileName.length() > FILE_NAME_LEN) {
+ throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX + "FILE_NAME" + VoConstants.TOO_LONG_MSG_SUFX);
+ }
+ if (this.fileType == null) {
+ throw new IllegalStateException("FILE_TYPE " + VoConstants.NOT_NULL_MSG);
+ }
+ if (this.fileType != null && this.fileType.length() > FILE_TYPE_LEN) {
+ throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX + "FILE_TYPE" + VoConstants.TOO_LONG_MSG_SUFX);
+ }
+ if (this.status == null) {
+ throw new IllegalStateException("STATUS " + VoConstants.NOT_NULL_MSG);
+ }
+ if (this.collectivite != null && this.collectivite.length() > COLLECTIVITE_LEN) {
+ throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX + "COLLECTIVITE" + VoConstants.TOO_LONG_MSG_SUFX);
+ }
+ if (this.budget != null && this.budget.length() > BUDGET_LEN) {
+ throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX + "BUDGET" + VoConstants.TOO_LONG_MSG_SUFX);
+ }
+ if (this.collectiviteLib != null && this.collectiviteLib.length() > COLLECTIVITE_LIB_LEN) {
+ throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX + "COLLECTIVITE_LIB" + VoConstants.TOO_LONG_MSG_SUFX);
+ }
+ if (this.budgetLib != null && this.budgetLib.length() > BUDGET_LIB_LEN) {
+ throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX + "BUDGET_LIB" + VoConstants.TOO_LONG_MSG_SUFX);
+ }
+ if (this.errorMessage != null && this.errorMessage.length() > ERROR_MESSAGE_LEN) {
+ throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX + "ERROR_MESSAGE" + VoConstants.TOO_LONG_MSG_SUFX);
+ }
+ }
+
+ public void determinateStatus() {
+ if (this.status == 5) {
+ return;
+ }
+ boolean correct = true;
+ if (this.collectivite == null || this.collectivite.length() == 0) {
+ correct = false;
+ }
+ if (this.collectiviteLib == null || this.collectiviteLib.length() == 0) {
+ correct = false;
+ }
+ if (this.budget == null || this.budget.length() == 0) {
+ correct = false;
+ }
+ if (this.budgetLib == null || this.budgetLib.length() == 0) {
+ correct = false;
+ }
+ if (this.fileType == null || this.fileType.length() == 0) {
+ correct = false;
+ }
+ if (correct) {
+ this.setStatus(6);
+ } else {
+ this.setStatus(2);
+ }
+ }
}
-
\ No newline at end of file
*/
package fr.gouv.finances.dgfip.xemelios.batch.imports.utils;
-
public class ImportVOPK implements java.io.Serializable {
-
- public static final int UTI_LOGIN_LEN = 50;
- public static final int IMPORT_ID_LEN = 200;
- public static final int FILE_NAME_LEN = 200;
- public static final int FILE_TYPE_LEN = 200;
- public static final int SUBMIT_DATE_LEN = 0;
- public static final int END_DATE_LEN = 0;
- public static final int STATUS_LEN = 10;
- public static final int COLLECTIVITE_LEN = 15;
- public static final int BUDGET_LEN = 24;
- public static final int COLLECTIVITE_LIB_LEN = 250;
- public static final int BUDGET_LIB_LEN = 250;
- public static final int ERROR_MESSAGE_LEN = 2147483647;
-
-
-
-
- /**
- * MAPS IMPORT_ID column.
- */
- protected java.lang.String importId;
- protected boolean isImportIdSet = false;
- protected boolean isImportIdModified = false;
-
-
-
-
- /**
- * Default Constructor
- */
- public ImportVOPK() {
- super();
- }
- /**
- * Initializes PK with values in fields.
- */
- public ImportVOPK(java.lang.String importId) {
- this();
- setInnerImportId(importId);
-
- }
-
-
-
-
-
- public java.lang.String getImportId() { return importId; }
- public void setImportId(java.lang.String importId) {
- this.importId=importId;
- this.isImportIdModified = true;
- }
- protected void setInnerImportId(java.lang.String importId) {
- this.importId=importId;
- this.isImportIdSet = true;
- }
- public java.lang.String getStrImportId(java.lang.String returnInCaseOfNull) {
- return importId!=null ? importId.toString() : returnInCaseOfNull;
- }
-
-
- @Override
- public boolean equals(Object o) {
- if(o instanceof ImportVOPK) {
- boolean bRet = true;
- ImportVOPK other = (ImportVOPK)o;
-
-
- bRet = bRet && (getImportId().equals(other.getImportId()));
-
- return bRet;
- }
- return false;
- }
- @Override
- public int hashCode() {
- int ret = 0;
-
-
- ret += getImportId().hashCode();
-
- return ret;
- }
+
+ public static final int UTI_LOGIN_LEN = 50;
+ public static final int IMPORT_ID_LEN = 200;
+ public static final int FILE_NAME_LEN = 200;
+ public static final int FILE_TYPE_LEN = 200;
+ public static final int SUBMIT_DATE_LEN = 0;
+ public static final int END_DATE_LEN = 0;
+ public static final int STATUS_LEN = 10;
+ public static final int COLLECTIVITE_LEN = 15;
+ public static final int BUDGET_LEN = 24;
+ public static final int COLLECTIVITE_LIB_LEN = 250;
+ public static final int BUDGET_LIB_LEN = 250;
+ public static final int ERROR_MESSAGE_LEN = 2147483647;
+
+ /**
+ * MAPS IMPORT_ID column.
+ */
+ protected java.lang.String importId;
+ protected boolean isImportIdSet = false;
+ protected boolean isImportIdModified = false;
+
+ /**
+ * Default Constructor
+ */
+ public ImportVOPK() {
+ super();
+ }
+
+ /**
+ * Initializes PK with values in fields.
+ */
+ public ImportVOPK(final java.lang.String importId) {
+ this();
+ this.setInnerImportId(importId);
+
+ }
+
+ public java.lang.String getImportId() {
+ return this.importId;
+ }
+
+ public void setImportId(final java.lang.String importId) {
+ this.importId = importId;
+ this.isImportIdModified = true;
+ }
+
+ protected void setInnerImportId(final java.lang.String importId) {
+ this.importId = importId;
+ this.isImportIdSet = true;
+ }
+
+ public java.lang.String getStrImportId(final java.lang.String returnInCaseOfNull) {
+ return this.importId != null ? this.importId.toString() : returnInCaseOfNull;
+ }
+
+ @Override
+ public boolean equals(final Object o) {
+ if (o instanceof ImportVOPK) {
+ boolean bRet = true;
+ final ImportVOPK other = (ImportVOPK) o;
+
+ bRet = bRet && (this.getImportId().equals(other.getImportId()));
+
+ return bRet;
+ }
+ return false;
+ }
+
+ @Override
+ public int hashCode() {
+ int ret = 0;
+
+ ret += this.getImportId().hashCode();
+
+ return ret;
+ }
}
-
\ No newline at end of file
*/
package fr.gouv.finances.dgfip.xemelios.batch.imports.utils;
-
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.List;
+
import fr.gouv.finances.dgfip.xemelios.data.utils.jdbc.JdbcUtils;
import fr.gouv.finances.dgfip.xemelios.data.utils.jdbc.PStmtBinder;
import fr.gouv.finances.dgfip.xemelios.data.utils.jdbc.RowMapper;
-import java.util.List;
+public class ImportVOWrapper {
-import java.sql.SQLException;
-import java.sql.PreparedStatement;
-import java.sql.Connection;
+ public static final String getSelectFieldsClause(final String table) {
+ return "" + table + ".UTI_LOGIN, " + table + ".IMPORT_ID, " + table + ".FILE_NAME, " + table + ".FILE_TYPE, " + table + ".SUBMIT_DATE, " + table + ".END_DATE, " + table + ".STATUS, " + table + ".COLLECTIVITE, " + table + ".BUDGET, " + table + ".COLLECTIVITE_LIB, " + table + ".BUDGET_LIB, "
+ + table + ".ERROR_MESSAGE";
+ }
+ public static final String getSelectFieldsClause() {
+ return "UTI_LOGIN, IMPORT_ID, FILE_NAME, FILE_TYPE, SUBMIT_DATE, END_DATE, STATUS, COLLECTIVITE, BUDGET, COLLECTIVITE_LIB, BUDGET_LIB, ERROR_MESSAGE";
+ }
+
+ public static ImportVOFull getImportVOFullBy(final Connection con, final Class clazz, final String sql, final PStmtBinder binder) throws SQLException {
+ return (ImportVOFull) JdbcUtils.queryObject(con, sql, new RowMapper.ClassRowMapper(clazz), binder);
+ }
+
+ public static List getAllImportVOFullBy(final Connection con, final Class clazz, final String sql, final PStmtBinder binder) throws SQLException {
+ return JdbcUtils.queryObjects(con, sql, new RowMapper.ClassRowMapper(clazz), binder);
+ }
+
+ public static List getAllImportVOFullBy(final Connection con, final Class clazz, final String sql) throws SQLException {
+ return JdbcUtils.queryObjects(con, sql, new RowMapper.ClassRowMapper(clazz), null);
+ }
+
+ public static ImportVOFull getImportVOFullByPk(final Connection con, final ImportVOPK data) throws SQLException {
+ return getImportVOFullByPk(con, data, ImportVOFull.class);
+ }
+
+ public static ImportVOFull getImportVOFullByPk(final Connection con, final ImportVOPK data, final Class clazz) throws SQLException {
+ final String sql = "SELECT " + getSelectFieldsClause() + " FROM XEM_IMPORTS WHERE IMPORT_ID=?";
+ return getImportVOFullBy(con, clazz, sql, new PStmtBinder.SimplePStmtBinderBuilder().add(data.importId).toPStmtBinder());
+ }
+
+ public static void deleteImportVO(final Connection con, final ImportVOPK data) throws SQLException {
+ final String sql = "DELETE FROM XEM_IMPORTS WHERE IMPORT_ID=?";
+ JdbcUtils.executeUpdate(con, sql, new PStmtBinder.SimplePStmtBinderBuilder().add(data.importId == null ? (java.lang.String) null : data.importId).toPStmtBinder());
+ }
+
+ public static void insertImportVO(final Connection con, final ImportVOFull data) throws SQLException {
+ int colCount = 0;
+ final StringBuilder sql = new StringBuilder("INSERT INTO XEM_IMPORTS(");
+
+ if (data.isUtiLoginSet | data.isUtiLoginModified) {
+ sql.append("UTI_LOGIN,");
+ colCount++;
+ }
+
+ if (data.isImportIdSet | data.isImportIdModified) {
+ sql.append("IMPORT_ID,");
+ colCount++;
+ }
+
+ if (data.isFileNameSet | data.isFileNameModified) {
+ sql.append("FILE_NAME,");
+ colCount++;
+ }
+
+ if (data.isFileTypeSet | data.isFileTypeModified) {
+ sql.append("FILE_TYPE,");
+ colCount++;
+ }
+
+ if (data.isSubmitDateSet | data.isSubmitDateModified) {
+ sql.append("SUBMIT_DATE,");
+ colCount++;
+ }
+
+ if (data.isEndDateSet | data.isEndDateModified) {
+ sql.append("END_DATE,");
+ colCount++;
+ }
+
+ if (data.isStatusSet | data.isStatusModified) {
+ sql.append("STATUS,");
+ colCount++;
+ }
+
+ if (data.isCollectiviteSet | data.isCollectiviteModified) {
+ sql.append("COLLECTIVITE,");
+ colCount++;
+ }
+
+ if (data.isBudgetSet | data.isBudgetModified) {
+ sql.append("BUDGET,");
+ colCount++;
+ }
+
+ if (data.isCollectiviteLibSet | data.isCollectiviteLibModified) {
+ sql.append("COLLECTIVITE_LIB,");
+ colCount++;
+ }
+
+ if (data.isBudgetLibSet | data.isBudgetLibModified) {
+ sql.append("BUDGET_LIB,");
+ colCount++;
+ }
+
+ if (data.isErrorMessageSet | data.isErrorMessageModified) {
+ sql.append("ERROR_MESSAGE,");
+ colCount++;
+ }
+ if (colCount == 0) {
+ throw new SQLException("No data to insert");
+ }
+ sql.deleteCharAt(sql.length() - 1);
+ sql.append(") VALUES (");
+ for (int i = 0; i < colCount; i++) {
+ sql.append("?,");
+ }
+ sql.deleteCharAt(sql.length() - 1);
+ sql.append(")");
+ PStmtBinder.SimplePStmtBinderBuilder builder = new PStmtBinder.SimplePStmtBinderBuilder();
+
+ if (data.isUtiLoginSet | data.isUtiLoginModified) {
+ if (data.utiLogin == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.utiLogin);
+ }
+ }
+
+ if (data.isImportIdSet | data.isImportIdModified) {
+ if (data.importId == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.importId);
+ }
+ }
+
+ if (data.isFileNameSet | data.isFileNameModified) {
+ if (data.fileName == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.fileName);
+ }
+ }
+
+ if (data.isFileTypeSet | data.isFileTypeModified) {
+ if (data.fileType == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.fileType);
+ }
+ }
+
+ if (data.isSubmitDateSet | data.isSubmitDateModified) {
+ if (data.submitDate == null) {
+ builder = builder.add((java.sql.Timestamp) null);
+ } else {
+ builder = builder.add(data.submitDate);
+ }
+ }
+
+ if (data.isEndDateSet | data.isEndDateModified) {
+ if (data.endDate == null) {
+ builder = builder.add((java.sql.Timestamp) null);
+ } else {
+ builder = builder.add(data.endDate);
+ }
+ }
+
+ if (data.isStatusSet | data.isStatusModified) {
+ if (data.status == null) {
+ builder = builder.add((java.lang.Integer) null);
+ } else {
+ builder = builder.add(data.status);
+ }
+ }
+
+ if (data.isCollectiviteSet | data.isCollectiviteModified) {
+ if (data.collectivite == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.collectivite);
+ }
+ }
+
+ if (data.isBudgetSet | data.isBudgetModified) {
+ if (data.budget == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.budget);
+ }
+ }
+
+ if (data.isCollectiviteLibSet | data.isCollectiviteLibModified) {
+ if (data.collectiviteLib == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.collectiviteLib);
+ }
+ }
+
+ if (data.isBudgetLibSet | data.isBudgetLibModified) {
+ if (data.budgetLib == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.budgetLib);
+ }
+ }
+
+ if (data.isErrorMessageSet | data.isErrorMessageModified) {
+ if (data.errorMessage == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.errorMessage);
+ }
+ }
+
+ JdbcUtils.executeUpdate(con, sql.toString(), builder.toPStmtBinder());
+ }
+
+ public static void updateImportVO(final Connection con, final ImportVOFull data) throws SQLException {
+ final PreparedStatement ps = null;
+ final StringBuilder sql = new StringBuilder("UPDATE XEM_IMPORTS SET ");
+ if (data.isUtiLoginModified) {
+ sql.append("UTI_LOGIN=?,");
+ }
+ if (data.isFileNameModified) {
+ sql.append("FILE_NAME=?,");
+ }
+ if (data.isFileTypeModified) {
+ sql.append("FILE_TYPE=?,");
+ }
+ if (data.isSubmitDateModified) {
+ sql.append("SUBMIT_DATE=?,");
+ }
+ if (data.isEndDateModified) {
+ sql.append("END_DATE=?,");
+ }
+ if (data.isStatusModified) {
+ sql.append("STATUS=?,");
+ }
+ if (data.isCollectiviteModified) {
+ sql.append("COLLECTIVITE=?,");
+ }
+ if (data.isBudgetModified) {
+ sql.append("BUDGET=?,");
+ }
+ if (data.isCollectiviteLibModified) {
+ sql.append("COLLECTIVITE_LIB=?,");
+ }
+ if (data.isBudgetLibModified) {
+ sql.append("BUDGET_LIB=?,");
+ }
+ if (data.isErrorMessageModified) {
+ sql.append("ERROR_MESSAGE=?,");
+ }
+
+ sql.deleteCharAt(sql.length() - 1);
+ sql.append(" WHERE IMPORT_ID=?");
+ PStmtBinder.SimplePStmtBinderBuilder builder = new PStmtBinder.SimplePStmtBinderBuilder();
+
+ if (data.isUtiLoginModified) {
+ if (data.utiLogin == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.utiLogin);
+ }
+ }
+ if (data.isFileNameModified) {
+ if (data.fileName == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.fileName);
+ }
+ }
+ if (data.isFileTypeModified) {
+ if (data.fileType == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.fileType);
+ }
+ }
+ if (data.isSubmitDateModified) {
+ if (data.submitDate == null) {
+ builder = builder.add((java.sql.Timestamp) null);
+ } else {
+ builder = builder.add(data.submitDate);
+ }
+ }
+ if (data.isEndDateModified) {
+ if (data.endDate == null) {
+ builder = builder.add((java.sql.Timestamp) null);
+ } else {
+ builder = builder.add(data.endDate);
+ }
+ }
+ if (data.isStatusModified) {
+ if (data.status == null) {
+ builder = builder.add((java.lang.Integer) null);
+ } else {
+ builder = builder.add(data.status);
+ }
+ }
+ if (data.isCollectiviteModified) {
+ if (data.collectivite == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.collectivite);
+ }
+ }
+ if (data.isBudgetModified) {
+ if (data.budget == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.budget);
+ }
+ }
+ if (data.isCollectiviteLibModified) {
+ if (data.collectiviteLib == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.collectiviteLib);
+ }
+ }
+ if (data.isBudgetLibModified) {
+ if (data.budgetLib == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.budgetLib);
+ }
+ }
+ if (data.isErrorMessageModified) {
+ if (data.errorMessage == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.errorMessage);
+ }
+ }
+ builder = builder.add(data.importId);
+
+ JdbcUtils.executeUpdate(con, sql.toString(), builder.toPStmtBinder());
+ }
-public class ImportVOWrapper {
-
- public static final String getSelectFieldsClause(String table) {
- return ""+table+".UTI_LOGIN, "+table+".IMPORT_ID, "+table+".FILE_NAME, "+table+".FILE_TYPE, "+table+".SUBMIT_DATE, "+table+".END_DATE, "+table+".STATUS, "+table+".COLLECTIVITE, "+table+".BUDGET, "+table+".COLLECTIVITE_LIB, "+table+".BUDGET_LIB, "+table+".ERROR_MESSAGE";
- }
-
- public static final String getSelectFieldsClause() {
- return "UTI_LOGIN, IMPORT_ID, FILE_NAME, FILE_TYPE, SUBMIT_DATE, END_DATE, STATUS, COLLECTIVITE, BUDGET, COLLECTIVITE_LIB, BUDGET_LIB, ERROR_MESSAGE";
- }
-
- public static ImportVOFull getImportVOFullBy(Connection con, Class clazz, String sql, PStmtBinder binder) throws SQLException {
- return (ImportVOFull)JdbcUtils.queryObject(con, sql, new RowMapper.ClassRowMapper(clazz), binder);
- }
-
- public static List getAllImportVOFullBy(Connection con, Class clazz, String sql, PStmtBinder binder) throws SQLException {
- return JdbcUtils.queryObjects(con, sql, new RowMapper.ClassRowMapper(clazz), binder);
- }
-
- public static List getAllImportVOFullBy(Connection con, Class clazz, String sql) throws SQLException {
- return JdbcUtils.queryObjects(con, sql, new RowMapper.ClassRowMapper(clazz), null);
- }
-
- public static ImportVOFull getImportVOFullByPk(Connection con, ImportVOPK data) throws SQLException {
- return getImportVOFullByPk(con, data, ImportVOFull.class);
- }
-
- public static ImportVOFull getImportVOFullByPk(Connection con, ImportVOPK data, Class clazz) throws SQLException {
- String sql = "SELECT " + getSelectFieldsClause() + " FROM XEM_IMPORTS WHERE IMPORT_ID=?";
- return getImportVOFullBy(con, clazz, sql, new PStmtBinder.SimplePStmtBinderBuilder().add(data.importId).toPStmtBinder());
- }
-
- public static void deleteImportVO(Connection con, ImportVOPK data) throws SQLException {
- String sql = "DELETE FROM XEM_IMPORTS WHERE IMPORT_ID=?";
- JdbcUtils.executeUpdate(con,sql,new PStmtBinder.SimplePStmtBinderBuilder().add(data.importId==null ? (java.lang.String)null : data.importId).toPStmtBinder());
- }
-
-
- public static void insertImportVO(Connection con, ImportVOFull data) throws SQLException {
- int colCount=0;
- StringBuilder sql = new StringBuilder("INSERT INTO XEM_IMPORTS(");
-
-
- if(data.isUtiLoginSet | data.isUtiLoginModified) {
- sql.append("UTI_LOGIN,");
- colCount++;
- }
-
- if(data.isImportIdSet | data.isImportIdModified) {
- sql.append("IMPORT_ID,");
- colCount++;
- }
-
- if(data.isFileNameSet | data.isFileNameModified) {
- sql.append("FILE_NAME,");
- colCount++;
- }
-
- if(data.isFileTypeSet | data.isFileTypeModified) {
- sql.append("FILE_TYPE,");
- colCount++;
- }
-
- if(data.isSubmitDateSet | data.isSubmitDateModified) {
- sql.append("SUBMIT_DATE,");
- colCount++;
- }
-
- if(data.isEndDateSet | data.isEndDateModified) {
- sql.append("END_DATE,");
- colCount++;
- }
-
- if(data.isStatusSet | data.isStatusModified) {
- sql.append("STATUS,");
- colCount++;
- }
-
- if(data.isCollectiviteSet | data.isCollectiviteModified) {
- sql.append("COLLECTIVITE,");
- colCount++;
- }
-
- if(data.isBudgetSet | data.isBudgetModified) {
- sql.append("BUDGET,");
- colCount++;
- }
-
- if(data.isCollectiviteLibSet | data.isCollectiviteLibModified) {
- sql.append("COLLECTIVITE_LIB,");
- colCount++;
- }
-
- if(data.isBudgetLibSet | data.isBudgetLibModified) {
- sql.append("BUDGET_LIB,");
- colCount++;
- }
-
- if(data.isErrorMessageSet | data.isErrorMessageModified) {
- sql.append("ERROR_MESSAGE,");
- colCount++;
- }
- if(colCount==0) throw new SQLException("No data to insert");
- sql.deleteCharAt(sql.length() - 1);
- sql.append(") VALUES (");
- for(int i=0;i<colCount;i++) sql.append("?,");
- sql.deleteCharAt(sql.length() - 1);
- sql.append(")");
- PStmtBinder.SimplePStmtBinderBuilder builder = new PStmtBinder.SimplePStmtBinderBuilder();
-
- if(data.isUtiLoginSet | data.isUtiLoginModified) {
- if(data.utiLogin==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.utiLogin);
- }
-
- if(data.isImportIdSet | data.isImportIdModified) {
- if(data.importId==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.importId);
- }
-
- if(data.isFileNameSet | data.isFileNameModified) {
- if(data.fileName==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.fileName);
- }
-
- if(data.isFileTypeSet | data.isFileTypeModified) {
- if(data.fileType==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.fileType);
- }
-
- if(data.isSubmitDateSet | data.isSubmitDateModified) {
- if(data.submitDate==null) builder=builder.add((java.sql.Timestamp)null); else builder=builder.add(data.submitDate);
- }
-
- if(data.isEndDateSet | data.isEndDateModified) {
- if(data.endDate==null) builder=builder.add((java.sql.Timestamp)null); else builder=builder.add(data.endDate);
- }
-
- if(data.isStatusSet | data.isStatusModified) {
- if(data.status==null) builder=builder.add((java.lang.Integer)null); else builder=builder.add(data.status);
- }
-
- if(data.isCollectiviteSet | data.isCollectiviteModified) {
- if(data.collectivite==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.collectivite);
- }
-
- if(data.isBudgetSet | data.isBudgetModified) {
- if(data.budget==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.budget);
- }
-
- if(data.isCollectiviteLibSet | data.isCollectiviteLibModified) {
- if(data.collectiviteLib==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.collectiviteLib);
- }
-
- if(data.isBudgetLibSet | data.isBudgetLibModified) {
- if(data.budgetLib==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.budgetLib);
- }
-
- if(data.isErrorMessageSet | data.isErrorMessageModified) {
- if(data.errorMessage==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.errorMessage);
- }
-
- JdbcUtils.executeUpdate(con,sql.toString(),builder.toPStmtBinder());
- }
-
- public static void updateImportVO(Connection con, ImportVOFull data) throws SQLException {
- PreparedStatement ps = null;
- StringBuilder sql=new StringBuilder("UPDATE XEM_IMPORTS SET ");
- if(data.isUtiLoginModified) sql.append("UTI_LOGIN=?,");
- if(data.isFileNameModified) sql.append("FILE_NAME=?,");
- if(data.isFileTypeModified) sql.append("FILE_TYPE=?,");
- if(data.isSubmitDateModified) sql.append("SUBMIT_DATE=?,");
- if(data.isEndDateModified) sql.append("END_DATE=?,");
- if(data.isStatusModified) sql.append("STATUS=?,");
- if(data.isCollectiviteModified) sql.append("COLLECTIVITE=?,");
- if(data.isBudgetModified) sql.append("BUDGET=?,");
- if(data.isCollectiviteLibModified) sql.append("COLLECTIVITE_LIB=?,");
- if(data.isBudgetLibModified) sql.append("BUDGET_LIB=?,");
- if(data.isErrorMessageModified) sql.append("ERROR_MESSAGE=?,");
-
- sql.deleteCharAt(sql.length()-1);
- sql.append(" WHERE IMPORT_ID=?");
- PStmtBinder.SimplePStmtBinderBuilder builder = new PStmtBinder.SimplePStmtBinderBuilder();
-
- if(data.isUtiLoginModified) {
- if(data.utiLogin==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.utiLogin);
- }
- if(data.isFileNameModified) {
- if(data.fileName==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.fileName);
- }
- if(data.isFileTypeModified) {
- if(data.fileType==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.fileType);
- }
- if(data.isSubmitDateModified) {
- if(data.submitDate==null) builder=builder.add((java.sql.Timestamp)null); else builder=builder.add(data.submitDate);
- }
- if(data.isEndDateModified) {
- if(data.endDate==null) builder=builder.add((java.sql.Timestamp)null); else builder=builder.add(data.endDate);
- }
- if(data.isStatusModified) {
- if(data.status==null) builder=builder.add((java.lang.Integer)null); else builder=builder.add(data.status);
- }
- if(data.isCollectiviteModified) {
- if(data.collectivite==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.collectivite);
- }
- if(data.isBudgetModified) {
- if(data.budget==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.budget);
- }
- if(data.isCollectiviteLibModified) {
- if(data.collectiviteLib==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.collectiviteLib);
- }
- if(data.isBudgetLibModified) {
- if(data.budgetLib==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.budgetLib);
- }
- if(data.isErrorMessageModified) {
- if(data.errorMessage==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.errorMessage);
- }
- builder=builder.add(data.importId);
-
- JdbcUtils.executeUpdate(con,sql.toString(),builder.toPStmtBinder());
- }
-
}
-
\ No newline at end of file
*/
package fr.gouv.finances.dgfip.xemelios.batch.imports.utils;
-
public interface VoConstants {
- public final String NOT_NULL_MSG = "should not be null";
- public final String TOO_LONG_MSG_PRFX = "null";
- public final String TOO_LONG_MSG_SUFX = "is tooo long";
+ public final String NOT_NULL_MSG = "should not be null";
+ public final String TOO_LONG_MSG_PRFX = "null";
+ public final String TOO_LONG_MSG_SUFX = "is tooo long";
}
-
\ No newline at end of file
*/
package fr.gouv.finances.dgfip.xemelios.batch.imports.utils;
-
import java.sql.ResultSet;
import java.sql.SQLException;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
-
public class XemImportsFilesFL extends XemImportsFilesVOPK implements java.io.Serializable {
-
- public static final transient String SELECT_REQ = "SELECT IMPORT_ID, SUB_FILE_NAME FROM XEM_IMPORTS_FILES ";
-
- public XemImportsFilesFL() {
- super();
- }
- public XemImportsFilesFL(ResultSet rs) throws SQLException {
- this();
- int i=1;
- setInnerImportId(rs.getString(i++));
- setInnerSubFileName(rs.getString(i++));
-
- }
-
+
+ public static final transient String SELECT_REQ = "SELECT IMPORT_ID, SUB_FILE_NAME FROM XEM_IMPORTS_FILES ";
+
+ public XemImportsFilesFL() {
+ super();
+ }
+
+ public XemImportsFilesFL(final ResultSet rs) throws SQLException {
+ this();
+ int i = 1;
+ this.setInnerImportId(rs.getString(i++));
+ this.setInnerSubFileName(rs.getString(i++));
+
+ }
+
}
-
\ No newline at end of file
*/
package fr.gouv.finances.dgfip.xemelios.batch.imports.utils;
-
import java.sql.ResultSet;
import java.sql.SQLException;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
-
public class XemImportsFilesVOFull extends XemImportsFilesVOPK implements java.io.Serializable {
-
-
-
- /**
- * MAPS FILE_TYPE column.
- */
- protected java.lang.String fileType;
- protected boolean isFileTypeSet = false;
- protected boolean isFileTypeModified = false;
-
-
-
- /**
- * MAPS STATUS column.
- */
- protected java.lang.Integer status;
- protected boolean isStatusSet = false;
- protected boolean isStatusModified = false;
-
-
-
- /**
- * MAPS COLLECTIVITE column.
- */
- protected java.lang.String collectivite;
- protected boolean isCollectiviteSet = false;
- protected boolean isCollectiviteModified = false;
-
-
-
- /**
- * MAPS BUDGET column.
- */
- protected java.lang.String budget;
- protected boolean isBudgetSet = false;
- protected boolean isBudgetModified = false;
-
-
-
- /**
- * MAPS COLLECTIVITE_LIB column.
- */
- protected java.lang.String collectiviteLib;
- protected boolean isCollectiviteLibSet = false;
- protected boolean isCollectiviteLibModified = false;
-
-
-
- /**
- * MAPS BUDGET_LIB column.
- */
- protected java.lang.String budgetLib;
- protected boolean isBudgetLibSet = false;
- protected boolean isBudgetLibModified = false;
-
-
-
- /**
- * MAPS ERROR_MESSAGE column.
- */
- protected java.lang.String errorMessage;
- protected boolean isErrorMessageSet = false;
- protected boolean isErrorMessageModified = false;
-
-
-
- /**
- * Default Constructor
- */
- public XemImportsFilesVOFull() {
- super();
- }
- /**
- * Constructs a new Full object, initializing only PK fileds.
- */
- public XemImportsFilesVOFull(java.lang.String importId, java.lang.String subFileName) {
- super(importId, subFileName);
- }
- public XemImportsFilesVOFull(ResultSet rs) throws SQLException {
- this();
- int i=1;
- setInnerImportId(rs.getString(i++));
- setInnerSubFileName(rs.getString(i++));
- setInnerFileType(rs.getString(i++));
- int statusTmp = rs.getInt(i++); if(!rs.wasNull()) setInnerStatus(new java.lang.Integer(statusTmp));
- setInnerCollectivite(rs.getString(i++));
- setInnerBudget(rs.getString(i++));
- setInnerCollectiviteLib(rs.getString(i++));
- setInnerBudgetLib(rs.getString(i++));
- setInnerErrorMessage(rs.getString(i++));
-
- }
-
-
-
-
-
- public java.lang.String getFileType() { return fileType; }
- public void setFileType(java.lang.String fileType) {
- this.fileType=fileType;
- this.isFileTypeModified = true;
- }
- protected void setInnerFileType(java.lang.String fileType) {
- this.fileType=fileType;
- this.isFileTypeSet = true;
- }
- public java.lang.String getStrFileType(java.lang.String returnInCaseOfNull) {
- return fileType!=null ? fileType.toString() : returnInCaseOfNull;
- }
-
-
-
-
-
- public java.lang.Integer getStatus() { return status; }
- public void setStatus(java.lang.Integer status) {
- this.status=status;
- this.isStatusModified = true;
- }
- protected void setInnerStatus(java.lang.Integer status) {
- this.status=status;
- this.isStatusSet = true;
- }
- public java.lang.String getStrStatus(java.lang.String returnInCaseOfNull) {
- return status!=null ? status.toString() : returnInCaseOfNull;
- }
-
-
-
-
-
- public java.lang.String getCollectivite() { return collectivite; }
- public void setCollectivite(java.lang.String collectivite) {
- this.collectivite=collectivite;
- this.isCollectiviteModified = true;
- }
- protected void setInnerCollectivite(java.lang.String collectivite) {
- this.collectivite=collectivite;
- this.isCollectiviteSet = true;
- }
- public java.lang.String getStrCollectivite(java.lang.String returnInCaseOfNull) {
- return collectivite!=null ? collectivite.toString() : returnInCaseOfNull;
- }
-
-
-
-
-
- public java.lang.String getBudget() { return budget; }
- public void setBudget(java.lang.String budget) {
- this.budget=budget;
- this.isBudgetModified = true;
- }
- protected void setInnerBudget(java.lang.String budget) {
- this.budget=budget;
- this.isBudgetSet = true;
- }
- public java.lang.String getStrBudget(java.lang.String returnInCaseOfNull) {
- return budget!=null ? budget.toString() : returnInCaseOfNull;
- }
-
-
-
-
-
- public java.lang.String getCollectiviteLib() { return collectiviteLib; }
- public void setCollectiviteLib(java.lang.String collectiviteLib) {
- this.collectiviteLib=collectiviteLib;
- this.isCollectiviteLibModified = true;
- }
- protected void setInnerCollectiviteLib(java.lang.String collectiviteLib) {
- this.collectiviteLib=collectiviteLib;
- this.isCollectiviteLibSet = true;
- }
- public java.lang.String getStrCollectiviteLib(java.lang.String returnInCaseOfNull) {
- return collectiviteLib!=null ? collectiviteLib.toString() : returnInCaseOfNull;
- }
-
-
-
-
-
- public java.lang.String getBudgetLib() { return budgetLib; }
- public void setBudgetLib(java.lang.String budgetLib) {
- this.budgetLib=budgetLib;
- this.isBudgetLibModified = true;
- }
- protected void setInnerBudgetLib(java.lang.String budgetLib) {
- this.budgetLib=budgetLib;
- this.isBudgetLibSet = true;
- }
- public java.lang.String getStrBudgetLib(java.lang.String returnInCaseOfNull) {
- return budgetLib!=null ? budgetLib.toString() : returnInCaseOfNull;
- }
-
-
-
-
-
- public java.lang.String getErrorMessage() { return errorMessage; }
- public void setErrorMessage(java.lang.String errorMessage) {
- this.errorMessage=errorMessage;
- this.isErrorMessageModified = true;
- }
- protected void setInnerErrorMessage(java.lang.String errorMessage) {
- this.errorMessage=errorMessage;
- this.isErrorMessageSet = true;
- }
- public java.lang.String getStrErrorMessage(java.lang.String returnInCaseOfNull) {
- return errorMessage!=null ? errorMessage.toString() : returnInCaseOfNull;
- }
-
-
- public void validate() throws IllegalStateException {
-
- if(importId==null) throw new IllegalStateException("IMPORT_ID "+VoConstants.NOT_NULL_MSG);
-
- if(importId!=null &&importId.length()>IMPORT_ID_LEN) throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX+"IMPORT_ID"+VoConstants.TOO_LONG_MSG_SUFX);
-
-
- if(subFileName==null) throw new IllegalStateException("SUB_FILE_NAME "+VoConstants.NOT_NULL_MSG);
-
- if(subFileName!=null &&subFileName.length()>SUB_FILE_NAME_LEN) throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX+"SUB_FILE_NAME"+VoConstants.TOO_LONG_MSG_SUFX);
-
-
- if(fileType==null) throw new IllegalStateException("FILE_TYPE "+VoConstants.NOT_NULL_MSG);
-
- if(fileType!=null &&fileType.length()>FILE_TYPE_LEN) throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX+"FILE_TYPE"+VoConstants.TOO_LONG_MSG_SUFX);
-
-
-
-
-
-
- if(collectivite!=null &&collectivite.length()>COLLECTIVITE_LEN) throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX+"COLLECTIVITE"+VoConstants.TOO_LONG_MSG_SUFX);
-
-
-
- if(budget!=null &&budget.length()>BUDGET_LEN) throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX+"BUDGET"+VoConstants.TOO_LONG_MSG_SUFX);
-
-
-
- if(collectiviteLib!=null &&collectiviteLib.length()>COLLECTIVITE_LIB_LEN) throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX+"COLLECTIVITE_LIB"+VoConstants.TOO_LONG_MSG_SUFX);
-
-
-
- if(budgetLib!=null &&budgetLib.length()>BUDGET_LIB_LEN) throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX+"BUDGET_LIB"+VoConstants.TOO_LONG_MSG_SUFX);
-
-
-
- if(errorMessage!=null &&errorMessage.length()>ERROR_MESSAGE_LEN) throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX+"ERROR_MESSAGE"+VoConstants.TOO_LONG_MSG_SUFX);
-
-
- }
-
+
+ /**
+ * MAPS FILE_TYPE column.
+ */
+ protected java.lang.String fileType;
+ protected boolean isFileTypeSet = false;
+ protected boolean isFileTypeModified = false;
+
+ /**
+ * MAPS STATUS column.
+ */
+ protected java.lang.Integer status;
+ protected boolean isStatusSet = false;
+ protected boolean isStatusModified = false;
+
+ /**
+ * MAPS COLLECTIVITE column.
+ */
+ protected java.lang.String collectivite;
+ protected boolean isCollectiviteSet = false;
+ protected boolean isCollectiviteModified = false;
+
+ /**
+ * MAPS BUDGET column.
+ */
+ protected java.lang.String budget;
+ protected boolean isBudgetSet = false;
+ protected boolean isBudgetModified = false;
+
+ /**
+ * MAPS COLLECTIVITE_LIB column.
+ */
+ protected java.lang.String collectiviteLib;
+ protected boolean isCollectiviteLibSet = false;
+ protected boolean isCollectiviteLibModified = false;
+
+ /**
+ * MAPS BUDGET_LIB column.
+ */
+ protected java.lang.String budgetLib;
+ protected boolean isBudgetLibSet = false;
+ protected boolean isBudgetLibModified = false;
+
+ /**
+ * MAPS ERROR_MESSAGE column.
+ */
+ protected java.lang.String errorMessage;
+ protected boolean isErrorMessageSet = false;
+ protected boolean isErrorMessageModified = false;
+
+ /**
+ * Default Constructor
+ */
+ public XemImportsFilesVOFull() {
+ super();
+ }
+
+ /**
+ * Constructs a new Full object, initializing only PK fileds.
+ */
+ public XemImportsFilesVOFull(final java.lang.String importId, final java.lang.String subFileName) {
+ super(importId, subFileName);
+ }
+
+ public XemImportsFilesVOFull(final ResultSet rs) throws SQLException {
+ this();
+ int i = 1;
+ this.setInnerImportId(rs.getString(i++));
+ this.setInnerSubFileName(rs.getString(i++));
+ this.setInnerFileType(rs.getString(i++));
+ final int statusTmp = rs.getInt(i++);
+ if (!rs.wasNull()) {
+ this.setInnerStatus(new java.lang.Integer(statusTmp));
+ }
+ this.setInnerCollectivite(rs.getString(i++));
+ this.setInnerBudget(rs.getString(i++));
+ this.setInnerCollectiviteLib(rs.getString(i++));
+ this.setInnerBudgetLib(rs.getString(i++));
+ this.setInnerErrorMessage(rs.getString(i++));
+
+ }
+
+ public java.lang.String getFileType() {
+ return this.fileType;
+ }
+
+ public void setFileType(final java.lang.String fileType) {
+ this.fileType = fileType;
+ this.isFileTypeModified = true;
+ }
+
+ protected void setInnerFileType(final java.lang.String fileType) {
+ this.fileType = fileType;
+ this.isFileTypeSet = true;
+ }
+
+ public java.lang.String getStrFileType(final java.lang.String returnInCaseOfNull) {
+ return this.fileType != null ? this.fileType.toString() : returnInCaseOfNull;
+ }
+
+ public java.lang.Integer getStatus() {
+ return this.status;
+ }
+
+ public void setStatus(final java.lang.Integer status) {
+ this.status = status;
+ this.isStatusModified = true;
+ }
+
+ protected void setInnerStatus(final java.lang.Integer status) {
+ this.status = status;
+ this.isStatusSet = true;
+ }
+
+ public java.lang.String getStrStatus(final java.lang.String returnInCaseOfNull) {
+ return this.status != null ? this.status.toString() : returnInCaseOfNull;
+ }
+
+ public java.lang.String getCollectivite() {
+ return this.collectivite;
+ }
+
+ public void setCollectivite(final java.lang.String collectivite) {
+ this.collectivite = collectivite;
+ this.isCollectiviteModified = true;
+ }
+
+ protected void setInnerCollectivite(final java.lang.String collectivite) {
+ this.collectivite = collectivite;
+ this.isCollectiviteSet = true;
+ }
+
+ public java.lang.String getStrCollectivite(final java.lang.String returnInCaseOfNull) {
+ return this.collectivite != null ? this.collectivite.toString() : returnInCaseOfNull;
+ }
+
+ public java.lang.String getBudget() {
+ return this.budget;
+ }
+
+ public void setBudget(final java.lang.String budget) {
+ this.budget = budget;
+ this.isBudgetModified = true;
+ }
+
+ protected void setInnerBudget(final java.lang.String budget) {
+ this.budget = budget;
+ this.isBudgetSet = true;
+ }
+
+ public java.lang.String getStrBudget(final java.lang.String returnInCaseOfNull) {
+ return this.budget != null ? this.budget.toString() : returnInCaseOfNull;
+ }
+
+ public java.lang.String getCollectiviteLib() {
+ return this.collectiviteLib;
+ }
+
+ public void setCollectiviteLib(final java.lang.String collectiviteLib) {
+ this.collectiviteLib = collectiviteLib;
+ this.isCollectiviteLibModified = true;
+ }
+
+ protected void setInnerCollectiviteLib(final java.lang.String collectiviteLib) {
+ this.collectiviteLib = collectiviteLib;
+ this.isCollectiviteLibSet = true;
+ }
+
+ public java.lang.String getStrCollectiviteLib(final java.lang.String returnInCaseOfNull) {
+ return this.collectiviteLib != null ? this.collectiviteLib.toString() : returnInCaseOfNull;
+ }
+
+ public java.lang.String getBudgetLib() {
+ return this.budgetLib;
+ }
+
+ public void setBudgetLib(final java.lang.String budgetLib) {
+ this.budgetLib = budgetLib;
+ this.isBudgetLibModified = true;
+ }
+
+ protected void setInnerBudgetLib(final java.lang.String budgetLib) {
+ this.budgetLib = budgetLib;
+ this.isBudgetLibSet = true;
+ }
+
+ public java.lang.String getStrBudgetLib(final java.lang.String returnInCaseOfNull) {
+ return this.budgetLib != null ? this.budgetLib.toString() : returnInCaseOfNull;
+ }
+
+ public java.lang.String getErrorMessage() {
+ return this.errorMessage;
+ }
+
+ public void setErrorMessage(final java.lang.String errorMessage) {
+ this.errorMessage = errorMessage;
+ this.isErrorMessageModified = true;
+ }
+
+ protected void setInnerErrorMessage(final java.lang.String errorMessage) {
+ this.errorMessage = errorMessage;
+ this.isErrorMessageSet = true;
+ }
+
+ public java.lang.String getStrErrorMessage(final java.lang.String returnInCaseOfNull) {
+ return this.errorMessage != null ? this.errorMessage.toString() : returnInCaseOfNull;
+ }
+
+ public void validate() throws IllegalStateException {
+
+ if (this.importId == null) {
+ throw new IllegalStateException("IMPORT_ID " + VoConstants.NOT_NULL_MSG);
+ }
+
+ if (this.importId != null && this.importId.length() > IMPORT_ID_LEN) {
+ throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX + "IMPORT_ID" + VoConstants.TOO_LONG_MSG_SUFX);
+ }
+
+ if (this.subFileName == null) {
+ throw new IllegalStateException("SUB_FILE_NAME " + VoConstants.NOT_NULL_MSG);
+ }
+
+ if (this.subFileName != null && this.subFileName.length() > SUB_FILE_NAME_LEN) {
+ throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX + "SUB_FILE_NAME" + VoConstants.TOO_LONG_MSG_SUFX);
+ }
+
+ if (this.fileType == null) {
+ throw new IllegalStateException("FILE_TYPE " + VoConstants.NOT_NULL_MSG);
+ }
+
+ if (this.fileType != null && this.fileType.length() > FILE_TYPE_LEN) {
+ throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX + "FILE_TYPE" + VoConstants.TOO_LONG_MSG_SUFX);
+ }
+
+ if (this.collectivite != null && this.collectivite.length() > COLLECTIVITE_LEN) {
+ throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX + "COLLECTIVITE" + VoConstants.TOO_LONG_MSG_SUFX);
+ }
+
+ if (this.budget != null && this.budget.length() > BUDGET_LEN) {
+ throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX + "BUDGET" + VoConstants.TOO_LONG_MSG_SUFX);
+ }
+
+ if (this.collectiviteLib != null && this.collectiviteLib.length() > COLLECTIVITE_LIB_LEN) {
+ throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX + "COLLECTIVITE_LIB" + VoConstants.TOO_LONG_MSG_SUFX);
+ }
+
+ if (this.budgetLib != null && this.budgetLib.length() > BUDGET_LIB_LEN) {
+ throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX + "BUDGET_LIB" + VoConstants.TOO_LONG_MSG_SUFX);
+ }
+
+ if (this.errorMessage != null && this.errorMessage.length() > ERROR_MESSAGE_LEN) {
+ throw new IllegalStateException(VoConstants.TOO_LONG_MSG_PRFX + "ERROR_MESSAGE" + VoConstants.TOO_LONG_MSG_SUFX);
+ }
+
+ }
+
}
-
\ No newline at end of file
*/
package fr.gouv.finances.dgfip.xemelios.batch.imports.utils;
-
public class XemImportsFilesVOPK implements java.io.Serializable {
-
- public static final int IMPORT_ID_LEN = 200;
- public static final int SUB_FILE_NAME_LEN = 2147483647;
- public static final int FILE_TYPE_LEN = 200;
- public static final int STATUS_LEN = 10;
- public static final int COLLECTIVITE_LEN = 15;
- public static final int BUDGET_LEN = 24;
- public static final int COLLECTIVITE_LIB_LEN = 250;
- public static final int BUDGET_LIB_LEN = 250;
- public static final int ERROR_MESSAGE_LEN = 2147483647;
-
-
-
-
- /**
- * MAPS IMPORT_ID column.
- */
- protected java.lang.String importId;
- protected boolean isImportIdSet = false;
- protected boolean isImportIdModified = false;
-
-
-
-
-
- /**
- * MAPS SUB_FILE_NAME column.
- */
- protected java.lang.String subFileName;
- protected boolean isSubFileNameSet = false;
- protected boolean isSubFileNameModified = false;
-
-
-
-
- /**
- * Default Constructor
- */
- public XemImportsFilesVOPK() {
- super();
- }
- /**
- * Initializes PK with values in fields.
- */
- public XemImportsFilesVOPK(java.lang.String importId, java.lang.String subFileName) {
- this();
- setInnerImportId(importId);
- setInnerSubFileName(subFileName);
-
- }
-
-
-
-
-
- public java.lang.String getImportId() { return importId; }
- public void setImportId(java.lang.String importId) {
- this.importId=importId;
- this.isImportIdModified = true;
- }
- protected void setInnerImportId(java.lang.String importId) {
- this.importId=importId;
- this.isImportIdSet = true;
- }
- public java.lang.String getStrImportId(java.lang.String returnInCaseOfNull) {
- return importId!=null ? importId.toString() : returnInCaseOfNull;
- }
-
-
-
-
-
- public java.lang.String getSubFileName() { return subFileName; }
- public void setSubFileName(java.lang.String subFileName) {
- this.subFileName=subFileName;
- this.isSubFileNameModified = true;
- }
- protected void setInnerSubFileName(java.lang.String subFileName) {
- this.subFileName=subFileName;
- this.isSubFileNameSet = true;
- }
- public java.lang.String getStrSubFileName(java.lang.String returnInCaseOfNull) {
- return subFileName!=null ? subFileName.toString() : returnInCaseOfNull;
- }
-
-
- public boolean equals(Object o) {
- if(o instanceof XemImportsFilesVOPK) {
- boolean bRet = true;
- XemImportsFilesVOPK other = (XemImportsFilesVOPK)o;
-
-
- bRet = bRet && (getImportId().equals(other.getImportId()));
-
-
- bRet = bRet && (getSubFileName().equals(other.getSubFileName()));
-
- return bRet;
- }
- return false;
- }
- public int hashCode() {
- int ret = 0;
-
-
- ret += getImportId().hashCode();
-
-
- ret += getSubFileName().hashCode();
-
- return ret;
- }
+
+ public static final int IMPORT_ID_LEN = 200;
+ public static final int SUB_FILE_NAME_LEN = 2147483647;
+ public static final int FILE_TYPE_LEN = 200;
+ public static final int STATUS_LEN = 10;
+ public static final int COLLECTIVITE_LEN = 15;
+ public static final int BUDGET_LEN = 24;
+ public static final int COLLECTIVITE_LIB_LEN = 250;
+ public static final int BUDGET_LIB_LEN = 250;
+ public static final int ERROR_MESSAGE_LEN = 2147483647;
+
+ /**
+ * MAPS IMPORT_ID column.
+ */
+ protected java.lang.String importId;
+ protected boolean isImportIdSet = false;
+ protected boolean isImportIdModified = false;
+
+ /**
+ * MAPS SUB_FILE_NAME column.
+ */
+ protected java.lang.String subFileName;
+ protected boolean isSubFileNameSet = false;
+ protected boolean isSubFileNameModified = false;
+
+ /**
+ * Default Constructor
+ */
+ public XemImportsFilesVOPK() {
+ super();
+ }
+
+ /**
+ * Initializes PK with values in fields.
+ */
+ public XemImportsFilesVOPK(final java.lang.String importId, final java.lang.String subFileName) {
+ this();
+ this.setInnerImportId(importId);
+ this.setInnerSubFileName(subFileName);
+
+ }
+
+ public java.lang.String getImportId() {
+ return this.importId;
+ }
+
+ public void setImportId(final java.lang.String importId) {
+ this.importId = importId;
+ this.isImportIdModified = true;
+ }
+
+ protected void setInnerImportId(final java.lang.String importId) {
+ this.importId = importId;
+ this.isImportIdSet = true;
+ }
+
+ public java.lang.String getStrImportId(final java.lang.String returnInCaseOfNull) {
+ return this.importId != null ? this.importId.toString() : returnInCaseOfNull;
+ }
+
+ public java.lang.String getSubFileName() {
+ return this.subFileName;
+ }
+
+ public void setSubFileName(final java.lang.String subFileName) {
+ this.subFileName = subFileName;
+ this.isSubFileNameModified = true;
+ }
+
+ protected void setInnerSubFileName(final java.lang.String subFileName) {
+ this.subFileName = subFileName;
+ this.isSubFileNameSet = true;
+ }
+
+ public java.lang.String getStrSubFileName(final java.lang.String returnInCaseOfNull) {
+ return this.subFileName != null ? this.subFileName.toString() : returnInCaseOfNull;
+ }
+
+ @Override
+ public boolean equals(final Object o) {
+ if (o instanceof XemImportsFilesVOPK) {
+ boolean bRet = true;
+ final XemImportsFilesVOPK other = (XemImportsFilesVOPK) o;
+
+ bRet = bRet && (this.getImportId().equals(other.getImportId()));
+
+ bRet = bRet && (this.getSubFileName().equals(other.getSubFileName()));
+
+ return bRet;
+ }
+ return false;
+ }
+
+ @Override
+ public int hashCode() {
+ int ret = 0;
+
+ ret += this.getImportId().hashCode();
+
+ ret += this.getSubFileName().hashCode();
+
+ return ret;
+ }
}
-
\ No newline at end of file
*/
package fr.gouv.finances.dgfip.xemelios.batch.imports.utils;
-
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.List;
import fr.gouv.finances.dgfip.xemelios.data.utils.jdbc.JdbcUtils;
import fr.gouv.finances.dgfip.xemelios.data.utils.jdbc.PStmtBinder;
import fr.gouv.finances.dgfip.xemelios.data.utils.jdbc.RowMapper;
-import java.util.List;
-import java.sql.SQLException;
-import java.sql.PreparedStatement;
-import java.sql.Connection;
+public class XemImportsFilesVOWrapper {
+ public static final String getSelectFieldsClause(final String table) {
+ return "" + table + ".IMPORT_ID, " + table + ".SUB_FILE_NAME, " + table + ".FILE_TYPE, " + table + ".STATUS, " + table + ".COLLECTIVITE, " + table + ".BUDGET, " + table + ".COLLECTIVITE_LIB, " + table + ".BUDGET_LIB, " + table + ".ERROR_MESSAGE";
+ }
+
+ public static final String getSelectFieldsClause() {
+ return "IMPORT_ID, SUB_FILE_NAME, FILE_TYPE, STATUS, COLLECTIVITE, BUDGET, COLLECTIVITE_LIB, BUDGET_LIB, ERROR_MESSAGE";
+ }
+
+ public static XemImportsFilesVOFull getXemImportsFilesVOFullBy(final Connection con, final Class clazz, final String sql, final PStmtBinder binder) throws SQLException {
+ return (XemImportsFilesVOFull) JdbcUtils.queryObject(con, sql, new RowMapper.ClassRowMapper(clazz), binder);
+ }
+
+ public static List getAllXemImportsFilesVOFullBy(final Connection con, final Class clazz, final String sql, final PStmtBinder binder) throws SQLException {
+ return JdbcUtils.queryObjects(con, sql, new RowMapper.ClassRowMapper(clazz), binder);
+ }
+
+ public static List getAllXemImportsFilesVOFullBy(final Connection con, final Class clazz, final String sql) throws SQLException {
+ return JdbcUtils.queryObjects(con, sql, new RowMapper.ClassRowMapper(clazz), null);
+ }
+
+ public static XemImportsFilesVOFull getXemImportsFilesVOFullByPk(final Connection con, final XemImportsFilesVOPK data) throws SQLException {
+ return getXemImportsFilesVOFullByPk(con, data, XemImportsFilesVOFull.class);
+ }
+
+ public static XemImportsFilesVOFull getXemImportsFilesVOFullByPk(final Connection con, final XemImportsFilesVOPK data, final Class clazz) throws SQLException {
+ final String sql = "SELECT " + getSelectFieldsClause() + " FROM XEM_IMPORTS_FILES WHERE IMPORT_ID=? AND SUB_FILE_NAME=?";
+ return getXemImportsFilesVOFullBy(con, clazz, sql, new PStmtBinder.SimplePStmtBinderBuilder().add(data.importId).add(data.subFileName).toPStmtBinder());
+ }
+
+ public static void deleteXemImportsFilesVO(final Connection con, final XemImportsFilesVOPK data) throws SQLException {
+ final String sql = "DELETE FROM XEM_IMPORTS_FILES WHERE IMPORT_ID=? AND SUB_FILE_NAME=?";
+ JdbcUtils.executeUpdate(con, sql, new PStmtBinder.SimplePStmtBinderBuilder().add(data.importId == null ? (java.lang.String) null : data.importId).add(data.subFileName == null ? (java.lang.String) null : data.subFileName).toPStmtBinder());
+ }
+
+ public static void insertXemImportsFilesVO(final Connection con, final XemImportsFilesVOFull data) throws SQLException {
+ int colCount = 0;
+ final StringBuilder sql = new StringBuilder("INSERT INTO XEM_IMPORTS_FILES(");
+
+ if (data.isImportIdSet | data.isImportIdModified) {
+ sql.append("IMPORT_ID,");
+ colCount++;
+ }
+
+ if (data.isSubFileNameSet | data.isSubFileNameModified) {
+ sql.append("SUB_FILE_NAME,");
+ colCount++;
+ }
+
+ if (data.isFileTypeSet | data.isFileTypeModified) {
+ sql.append("FILE_TYPE,");
+ colCount++;
+ }
+
+ if (data.isStatusSet | data.isStatusModified) {
+ sql.append("STATUS,");
+ colCount++;
+ }
+
+ if (data.isCollectiviteSet | data.isCollectiviteModified) {
+ sql.append("COLLECTIVITE,");
+ colCount++;
+ }
+
+ if (data.isBudgetSet | data.isBudgetModified) {
+ sql.append("BUDGET,");
+ colCount++;
+ }
+
+ if (data.isCollectiviteLibSet | data.isCollectiviteLibModified) {
+ sql.append("COLLECTIVITE_LIB,");
+ colCount++;
+ }
+
+ if (data.isBudgetLibSet | data.isBudgetLibModified) {
+ sql.append("BUDGET_LIB,");
+ colCount++;
+ }
+
+ if (data.isErrorMessageSet | data.isErrorMessageModified) {
+ sql.append("ERROR_MESSAGE,");
+ colCount++;
+ }
+ if (colCount == 0) {
+ throw new SQLException("No data to insert");
+ }
+ sql.deleteCharAt(sql.length() - 1);
+ sql.append(") VALUES (");
+ for (int i = 0; i < colCount; i++) {
+ sql.append("?,");
+ }
+ sql.deleteCharAt(sql.length() - 1);
+ sql.append(")");
+ PStmtBinder.SimplePStmtBinderBuilder builder = new PStmtBinder.SimplePStmtBinderBuilder();
+
+ if (data.isImportIdSet | data.isImportIdModified) {
+
+ if (data.importId == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.importId);
+ }
+
+ }
+
+ if (data.isSubFileNameSet | data.isSubFileNameModified) {
+
+ if (data.subFileName == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.subFileName);
+ }
+
+ }
+
+ if (data.isFileTypeSet | data.isFileTypeModified) {
+
+ if (data.fileType == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.fileType);
+ }
+
+ }
+
+ if (data.isStatusSet | data.isStatusModified) {
+
+ if (data.status == null) {
+ builder = builder.add((java.lang.Integer) null);
+ } else {
+ builder = builder.add(data.status);
+ }
+
+ }
+
+ if (data.isCollectiviteSet | data.isCollectiviteModified) {
+
+ if (data.collectivite == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.collectivite);
+ }
+
+ }
+
+ if (data.isBudgetSet | data.isBudgetModified) {
+
+ if (data.budget == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.budget);
+ }
+
+ }
+
+ if (data.isCollectiviteLibSet | data.isCollectiviteLibModified) {
+
+ if (data.collectiviteLib == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.collectiviteLib);
+ }
+
+ }
+
+ if (data.isBudgetLibSet | data.isBudgetLibModified) {
+
+ if (data.budgetLib == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.budgetLib);
+ }
+
+ }
+
+ if (data.isErrorMessageSet | data.isErrorMessageModified) {
+
+ if (data.errorMessage == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.errorMessage);
+ }
+
+ }
+
+ JdbcUtils.executeUpdate(con, sql.toString(), builder.toPStmtBinder());
+
+ }
+
+ public static void updateXemImportsFilesVO(final Connection con, final XemImportsFilesVOFull data) throws SQLException {
+ final PreparedStatement ps = null;
+ final StringBuilder sql = new StringBuilder("UPDATE XEM_IMPORTS_FILES SET ");
+ if (data.isFileTypeModified) {
+ sql.append("FILE_TYPE=?,");
+ }
+ if (data.isStatusModified) {
+ sql.append("STATUS=?,");
+ }
+ if (data.isCollectiviteModified) {
+ sql.append("COLLECTIVITE=?,");
+ }
+ if (data.isBudgetModified) {
+ sql.append("BUDGET=?,");
+ }
+ if (data.isCollectiviteLibModified) {
+ sql.append("COLLECTIVITE_LIB=?,");
+ }
+ if (data.isBudgetLibModified) {
+ sql.append("BUDGET_LIB=?,");
+ }
+ if (data.isErrorMessageModified) {
+ sql.append("ERROR_MESSAGE=?,");
+ }
+
+ sql.deleteCharAt(sql.length() - 1);
+ sql.append(" WHERE IMPORT_ID=? AND SUB_FILE_NAME=?");
+ PStmtBinder.SimplePStmtBinderBuilder builder = new PStmtBinder.SimplePStmtBinderBuilder();
+
+ if (data.isFileTypeModified) {
+
+ if (data.fileType == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.fileType);
+ }
+
+ }
+ if (data.isStatusModified) {
+
+ if (data.status == null) {
+ builder = builder.add((java.lang.Integer) null);
+ } else {
+ builder = builder.add(data.status);
+ }
+
+ }
+ if (data.isCollectiviteModified) {
+
+ if (data.collectivite == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.collectivite);
+ }
+
+ }
+ if (data.isBudgetModified) {
+
+ if (data.budget == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.budget);
+ }
+
+ }
+ if (data.isCollectiviteLibModified) {
+
+ if (data.collectiviteLib == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.collectiviteLib);
+ }
+
+ }
+ if (data.isBudgetLibModified) {
+
+ if (data.budgetLib == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.budgetLib);
+ }
+
+ }
+ if (data.isErrorMessageModified) {
+
+ if (data.errorMessage == null) {
+ builder = builder.add((java.lang.String) null);
+ } else {
+ builder = builder.add(data.errorMessage);
+ }
+
+ }
+
+ builder = builder.add(data.importId);
+
+ builder = builder.add(data.subFileName);
+
+ JdbcUtils.executeUpdate(con, sql.toString(), builder.toPStmtBinder());
+ }
+
+ public static List<XemImportsFilesFL> getChildren(final Connection con, final String request, final Class clazz, final PStmtBinder binder) throws SQLException {
+ return JdbcUtils.queryObjects(con, request, new RowMapper.ClassRowMapper(clazz), binder);
+ }
-public class XemImportsFilesVOWrapper {
-
- public static final String getSelectFieldsClause(String table) {
- return ""+table+".IMPORT_ID, "+table+".SUB_FILE_NAME, "+table+".FILE_TYPE, "+table+".STATUS, "+table+".COLLECTIVITE, "+table+".BUDGET, "+table+".COLLECTIVITE_LIB, "+table+".BUDGET_LIB, "+table+".ERROR_MESSAGE";
- }
-
- public static final String getSelectFieldsClause() {
- return "IMPORT_ID, SUB_FILE_NAME, FILE_TYPE, STATUS, COLLECTIVITE, BUDGET, COLLECTIVITE_LIB, BUDGET_LIB, ERROR_MESSAGE";
- }
-
- public static XemImportsFilesVOFull getXemImportsFilesVOFullBy(Connection con, Class clazz, String sql, PStmtBinder binder) throws SQLException {
- return (XemImportsFilesVOFull)JdbcUtils.queryObject(con, sql, new RowMapper.ClassRowMapper(clazz), binder);
- }
-
- public static List getAllXemImportsFilesVOFullBy(Connection con, Class clazz, String sql, PStmtBinder binder) throws SQLException {
- return JdbcUtils.queryObjects(con, sql, new RowMapper.ClassRowMapper(clazz), binder);
- }
-
- public static List getAllXemImportsFilesVOFullBy(Connection con, Class clazz, String sql) throws SQLException {
- return JdbcUtils.queryObjects(con, sql, new RowMapper.ClassRowMapper(clazz), null);
- }
-
- public static XemImportsFilesVOFull getXemImportsFilesVOFullByPk(Connection con, XemImportsFilesVOPK data) throws SQLException {
- return getXemImportsFilesVOFullByPk(con, data, XemImportsFilesVOFull.class);
- }
-
- public static XemImportsFilesVOFull getXemImportsFilesVOFullByPk(Connection con, XemImportsFilesVOPK data, Class clazz) throws SQLException {
- String sql = "SELECT " + getSelectFieldsClause() + " FROM XEM_IMPORTS_FILES WHERE IMPORT_ID=? AND SUB_FILE_NAME=?";
- return getXemImportsFilesVOFullBy(con, clazz, sql, new PStmtBinder.SimplePStmtBinderBuilder().add(data.importId).add(data.subFileName).toPStmtBinder());
- }
-
- public static void deleteXemImportsFilesVO(Connection con, XemImportsFilesVOPK data) throws SQLException {
- String sql = "DELETE FROM XEM_IMPORTS_FILES WHERE IMPORT_ID=? AND SUB_FILE_NAME=?";
- JdbcUtils.executeUpdate(con,sql,new PStmtBinder.SimplePStmtBinderBuilder().add(data.importId==null ? (java.lang.String)null : data.importId).add(data.subFileName==null ? (java.lang.String)null : data.subFileName).toPStmtBinder());
- }
-
-
- public static void insertXemImportsFilesVO(Connection con, XemImportsFilesVOFull data) throws SQLException {
- int colCount=0;
- StringBuilder sql = new StringBuilder("INSERT INTO XEM_IMPORTS_FILES(");
-
-
- if(data.isImportIdSet | data.isImportIdModified) {
- sql.append("IMPORT_ID,");
- colCount++;
- }
-
- if(data.isSubFileNameSet | data.isSubFileNameModified) {
- sql.append("SUB_FILE_NAME,");
- colCount++;
- }
-
- if(data.isFileTypeSet | data.isFileTypeModified) {
- sql.append("FILE_TYPE,");
- colCount++;
- }
-
- if(data.isStatusSet | data.isStatusModified) {
- sql.append("STATUS,");
- colCount++;
- }
-
- if(data.isCollectiviteSet | data.isCollectiviteModified) {
- sql.append("COLLECTIVITE,");
- colCount++;
- }
-
- if(data.isBudgetSet | data.isBudgetModified) {
- sql.append("BUDGET,");
- colCount++;
- }
-
- if(data.isCollectiviteLibSet | data.isCollectiviteLibModified) {
- sql.append("COLLECTIVITE_LIB,");
- colCount++;
- }
-
- if(data.isBudgetLibSet | data.isBudgetLibModified) {
- sql.append("BUDGET_LIB,");
- colCount++;
- }
-
- if(data.isErrorMessageSet | data.isErrorMessageModified) {
- sql.append("ERROR_MESSAGE,");
- colCount++;
- }
- if(colCount==0) throw new SQLException("No data to insert");
- sql.deleteCharAt(sql.length() - 1);
- sql.append(") VALUES (");
- for(int i=0;i<colCount;i++) sql.append("?,");
- sql.deleteCharAt(sql.length() - 1);
- sql.append(")");
- PStmtBinder.SimplePStmtBinderBuilder builder = new PStmtBinder.SimplePStmtBinderBuilder();
-
- if(data.isImportIdSet | data.isImportIdModified) {
-
- if(data.importId==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.importId);
-
-
- }
-
- if(data.isSubFileNameSet | data.isSubFileNameModified) {
-
- if(data.subFileName==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.subFileName);
-
-
- }
-
- if(data.isFileTypeSet | data.isFileTypeModified) {
-
- if(data.fileType==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.fileType);
-
-
- }
-
- if(data.isStatusSet | data.isStatusModified) {
-
- if(data.status==null) builder=builder.add((java.lang.Integer)null); else builder=builder.add(data.status);
-
-
- }
-
- if(data.isCollectiviteSet | data.isCollectiviteModified) {
-
- if(data.collectivite==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.collectivite);
-
-
- }
-
- if(data.isBudgetSet | data.isBudgetModified) {
-
- if(data.budget==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.budget);
-
-
- }
-
- if(data.isCollectiviteLibSet | data.isCollectiviteLibModified) {
-
- if(data.collectiviteLib==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.collectiviteLib);
-
-
- }
-
- if(data.isBudgetLibSet | data.isBudgetLibModified) {
-
- if(data.budgetLib==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.budgetLib);
-
-
- }
-
- if(data.isErrorMessageSet | data.isErrorMessageModified) {
-
- if(data.errorMessage==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.errorMessage);
-
-
- }
-
-
- JdbcUtils.executeUpdate(con,sql.toString(),builder.toPStmtBinder());
-
-
-
- }
-
- public static void updateXemImportsFilesVO(Connection con, XemImportsFilesVOFull data) throws SQLException {
- PreparedStatement ps = null;
- StringBuilder sql=new StringBuilder("UPDATE XEM_IMPORTS_FILES SET ");
- if(data.isFileTypeModified) sql.append("FILE_TYPE=?,");
- if(data.isStatusModified) sql.append("STATUS=?,");
- if(data.isCollectiviteModified) sql.append("COLLECTIVITE=?,");
- if(data.isBudgetModified) sql.append("BUDGET=?,");
- if(data.isCollectiviteLibModified) sql.append("COLLECTIVITE_LIB=?,");
- if(data.isBudgetLibModified) sql.append("BUDGET_LIB=?,");
- if(data.isErrorMessageModified) sql.append("ERROR_MESSAGE=?,");
-
- sql.deleteCharAt(sql.length()-1);
- sql.append(" WHERE IMPORT_ID=? AND SUB_FILE_NAME=?");
- PStmtBinder.SimplePStmtBinderBuilder builder = new PStmtBinder.SimplePStmtBinderBuilder();
-
- if(data.isFileTypeModified) {
-
- if(data.fileType==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.fileType);
-
-
- }
- if(data.isStatusModified) {
-
- if(data.status==null) builder=builder.add((java.lang.Integer)null); else builder=builder.add(data.status);
-
-
- }
- if(data.isCollectiviteModified) {
-
- if(data.collectivite==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.collectivite);
-
-
- }
- if(data.isBudgetModified) {
-
- if(data.budget==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.budget);
-
-
- }
- if(data.isCollectiviteLibModified) {
-
- if(data.collectiviteLib==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.collectiviteLib);
-
-
- }
- if(data.isBudgetLibModified) {
-
- if(data.budgetLib==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.budgetLib);
-
-
- }
- if(data.isErrorMessageModified) {
-
- if(data.errorMessage==null) builder=builder.add((java.lang.String)null); else builder=builder.add(data.errorMessage);
-
-
- }
-
- builder=builder.add(data.importId);
-
- builder=builder.add(data.subFileName);
-
- JdbcUtils.executeUpdate(con,sql.toString(),builder.toPStmtBinder());
- }
-
-
-
-
- public static List<XemImportsFilesFL> getChildren(Connection con, String request, Class clazz, PStmtBinder binder) throws SQLException {
- return (List<XemImportsFilesFL>)JdbcUtils.queryObjects(con,request, new RowMapper.ClassRowMapper(clazz), binder);
- }
-
}
-
\ No newline at end of file
import org.xhtmlrenderer.pdf.ITextUserAgent;
/**
- *
+ *
* @author cmarchand
*/
public class BatchUserAgentCallback extends ITextUserAgent {
- private static final Logger logger = Logger.getLogger(BatchUserAgentCallback.class);
-
- public BatchUserAgentCallback(ITextOutputDevice device) {
- super(device);
- }
+ private static final Logger logger = Logger.getLogger(BatchUserAgentCallback.class);
- @Override
- public String resolveURI(String uri) {
-// logger.debug("trying to resolve uri: "+uri);
- return super.resolveURI(uri);
- }
+ public BatchUserAgentCallback(final ITextOutputDevice device) {
+ super(device);
+ }
+ @Override
+ public String resolveURI(final String uri) {
+ // logger.debug("trying to resolve uri: "+uri);
+ return super.resolveURI(uri);
+ }
}
package fr.gouv.finances.dgfip.xemelios.batch.pdf;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+import org.apache.log4j.Logger;
+
import fr.gouv.finances.cp.xemelios.data.impl.MySqlDataLayer;
import fr.gouv.finances.dgfip.xemelios.auth.XemeliosUser;
import fr.gouv.finances.dgfip.xemelios.batch.Batch;
import fr.gouv.finances.dgfip.xemelios.data.DataImpl;
import fr.gouv.finances.dgfip.xemelios.data.DataLayerManager;
import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import org.apache.log4j.Logger;
/**
- *
+ *
* @author cmarchand
*/
public class PdfCreator extends Batch {
- private static final Logger logger = Logger.getLogger(PdfCreator.class);
- 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_DOCUMENT_ID = "document.id";
- public static final Integer STATUS_NONE = null;
- public static final Integer STATUS_IDLE = 0;
- public static final Integer STATUS_RUNNING = 1;
- public static final Integer STATUS_TERMINATED = 2;
- public static final Integer STATUS_ERROR = 3;
-
- private static ExecutorService runner = Executors.newFixedThreadPool(3);
-
- private String documentId;
-
- public PdfCreator(String args[]) {
- super(args);
- }
-
- @Override
- protected void initialize() throws Exception {
- super.initialize();
- documentId = getProps().getProperty(PROP_DOCUMENT_ID);
- }
-
- @Override
- protected void doProcess() throws Exception {
- logger.debug("running PdfCreator for document "+documentId);
- XemeliosUser user = new User();
- Connection con = null;
- try {
- EtatModel em = BatchRunner.getInstance().getDocuments().getDocumentById("cg-etat").getEtatById("BalanceGenerale");
- con = PoolManager.getInstance().getConnection();
- DataImpl impl = DataLayerManager.getImplementation();
- if(impl instanceof MySqlDataLayer) {
- MySqlDataLayer msImpl = (MySqlDataLayer)impl;
- if("compteGestionEtat".equals(documentId)) {
- String select = "SELECT COLLECTIVITE, BUDGET, EXERCICE FROM PDF_CREATION WHERE DOCUMENT_ID=? AND ETAT_ID=? AND CREATION_STATUS IS NULL";
- PreparedStatement ps = con.prepareStatement(select);
- ps.setString(1,documentId);
- ps.setString(2,"BalanceGenerale");
- ResultSet rs = ps.executeQuery();
- while(rs.next()) {
- String collectivite = rs.getString(1);
- String budget = rs.getString(2);
- int exercice = rs.getInt(3);
- PdfCreatorImpl process = new PdfCreatorImpl(documentId, "BalanceGenerale", collectivite, budget, exercice, user, msImpl, em);
- runner.submit(process);
- }
-
- }
- } else {
- throw new RuntimeException("PdfCreator only runs with MysqlDataLayer");
- }
- } catch(Exception ex) {
- logger.error("in doProcess",ex);
- } finally {
- if(con!=null) {
- PoolManager.getInstance().releaseConnection(con);
- con = null;
- }
- }
- }
-
- @Override
- public String getResumeTraitement() {
- return "pdf.creator";
- }
-
- @Override
- public String typeTraitementRefCode() {
- return getResumeTraitement()+"<"+documentId+">";
- }
-
- @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";
- }
-
- private class User implements XemeliosUser {
- @Override
- public String getId() {
- return "PdfCreatorUser";
- }
- @Override
- public String getDisplayName() {
- return getId();
- }
- @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;
- }
- }
-
- protected class PdfCreatorImpl extends RunnableBatch {
- private String documentId;
- private String etatId;
- private String collectivite;
- private String budget;
- private int exercice;
- private XemeliosUser user;
- private MySqlDataLayer msLayer;
- private EtatModel em;
-
- public PdfCreatorImpl(String documentId, String etatId, String collectivite, String budget, int exercice, XemeliosUser user, MySqlDataLayer msLayer, EtatModel em) {
- super();
- this.documentId=documentId;
- this.etatId=etatId;
- this.collectivite=collectivite;
- this.budget=budget;
- this.exercice=exercice;
- this.msLayer = msLayer;
- this.user=user;
- this.em=em;
- }
-
- @Override
- public void run() {
- logger.info("starting "+documentId+"."+etatId+" "+collectivite+" "+budget+" "+exercice);
- super.run();
- if("compteGestionEtat".equals(documentId) && "BalanceGenerale".equals(etatId)) {
- generateCgEtatBalance();
- } else {
- getNotifier().notifyThreadStop(this);
- throw new RuntimeException("PdfCreator do not know how to PDF's "+documentId);
- }
- getNotifier().notifyThreadStop(this);
- }
-
- protected void generateCgEtatBalance() {
- PdfCreatorCgEtatBalance generator = new PdfCreatorCgEtatBalance(em,collectivite, budget, exercice,user);
- generator.run();
- }
- @Override
- public String getInformations() {
- StringBuilder sb = new StringBuilder();
- sb.append("\t").append(getName()).append(":");
- if(isAlive()) {
- sb.append("PdfCreator<"+documentId+"> running.");
- } else {
- if(isStarted()) {
- sb.append("PdfCreator<"+documentId+"> fully terminated.");
- } else {
- sb.append("PdfCreator<"+documentId+"> idle");
- }
- }
- sb.append("\n");
- return sb.toString();
- }
- }
+ private static final Logger logger = Logger.getLogger(PdfCreator.class);
+ 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_DOCUMENT_ID = "document.id";
+ public static final Integer STATUS_NONE = null;
+ public static final Integer STATUS_IDLE = 0;
+ public static final Integer STATUS_RUNNING = 1;
+ public static final Integer STATUS_TERMINATED = 2;
+ public static final Integer STATUS_ERROR = 3;
+
+ private static ExecutorService runner = Executors.newFixedThreadPool(3);
+
+ private String documentId;
+
+ public PdfCreator(final String args[]) {
+ super(args);
+ }
+
+ @Override
+ protected void initialize() throws Exception {
+ super.initialize();
+ this.documentId = this.getProps().getProperty(PROP_DOCUMENT_ID);
+ }
+
+ @Override
+ protected void doProcess() throws Exception {
+ logger.debug("running PdfCreator for document " + this.documentId);
+ final XemeliosUser user = new User();
+ Connection con = null;
+ try {
+ final EtatModel em = BatchRunner.getInstance().getDocuments().getDocumentById("cg-etat").getEtatById("BalanceGenerale");
+ con = PoolManager.getInstance().getConnection();
+ final DataImpl impl = DataLayerManager.getImplementation();
+ if (impl instanceof MySqlDataLayer) {
+ final MySqlDataLayer msImpl = (MySqlDataLayer) impl;
+ if ("compteGestionEtat".equals(this.documentId)) {
+ final String select = "SELECT COLLECTIVITE, BUDGET, EXERCICE FROM PDF_CREATION WHERE DOCUMENT_ID=? AND ETAT_ID=? AND CREATION_STATUS IS NULL";
+ final PreparedStatement ps = con.prepareStatement(select);
+ ps.setString(1, this.documentId);
+ ps.setString(2, "BalanceGenerale");
+ final ResultSet rs = ps.executeQuery();
+ while (rs.next()) {
+ final String collectivite = rs.getString(1);
+ final String budget = rs.getString(2);
+ final int exercice = rs.getInt(3);
+ final PdfCreatorImpl process = new PdfCreatorImpl(this.documentId, "BalanceGenerale", collectivite, budget, exercice, user, msImpl, em);
+ runner.submit(process);
+ }
+
+ }
+ } else {
+ throw new RuntimeException("PdfCreator only runs with MysqlDataLayer");
+ }
+ } catch (final Exception ex) {
+ logger.error("in doProcess", ex);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ con = null;
+ }
+ }
+ }
+
+ @Override
+ public String getResumeTraitement() {
+ return "pdf.creator";
+ }
+
+ @Override
+ public String typeTraitementRefCode() {
+ return this.getResumeTraitement() + "<" + this.documentId + ">";
+ }
+
+ @Override
+ public String getInformations() {
+ final StringBuilder sb = new StringBuilder();
+ if (this.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(this.typeTraitementRefCode()).append(" - not started\n");
+ }
+ return sb.toString();
+ }
+
+ @Override
+ protected String getBatchVersion() {
+ return "1.0";
+ }
+
+ private class User implements XemeliosUser {
+ @Override
+ public String getId() {
+ return "PdfCreatorUser";
+ }
+
+ @Override
+ public String getDisplayName() {
+ return this.getId();
+ }
+
+ @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;
+ }
+ }
+
+ protected class PdfCreatorImpl extends RunnableBatch {
+ private final String documentId;
+ private final String etatId;
+ private final String collectivite;
+ private final String budget;
+ private final int exercice;
+ private final XemeliosUser user;
+ private final EtatModel em;
+
+ public PdfCreatorImpl(final String documentId, final String etatId, final String collectivite, final String budget, final int exercice, final XemeliosUser user, final MySqlDataLayer msLayer, final EtatModel em) {
+ super();
+ this.documentId = documentId;
+ this.etatId = etatId;
+ this.collectivite = collectivite;
+ this.budget = budget;
+ this.exercice = exercice;
+ this.user = user;
+ this.em = em;
+ }
+
+ @Override
+ public void run() {
+ logger.info("starting " + this.documentId + "." + this.etatId + " " + this.collectivite + " " + this.budget + " " + this.exercice);
+ super.run();
+ if ("compteGestionEtat".equals(this.documentId) && "BalanceGenerale".equals(this.etatId)) {
+ this.generateCgEtatBalance();
+ } else {
+ this.getNotifier().notifyThreadStop(this);
+ throw new RuntimeException("PdfCreator do not know how to PDF's " + this.documentId);
+ }
+ this.getNotifier().notifyThreadStop(this);
+ }
+
+ protected void generateCgEtatBalance() {
+ final PdfCreatorCgEtatBalance generator = new PdfCreatorCgEtatBalance(this.em, this.collectivite, this.budget, this.exercice, this.user);
+ generator.run();
+ }
+
+ @Override
+ public String getInformations() {
+ final StringBuilder sb = new StringBuilder();
+ sb.append("\t").append(this.getName()).append(":");
+ if (this.isAlive()) {
+ sb.append("PdfCreator<" + this.documentId + "> running.");
+ } else {
+ if (PdfCreator.this.isStarted()) {
+ sb.append("PdfCreator<" + this.documentId + "> fully terminated.");
+ } else {
+ sb.append("PdfCreator<" + this.documentId + "> idle");
+ }
+ }
+ sb.append("\n");
+ return sb.toString();
+ }
+ }
}
package fr.gouv.finances.dgfip.xemelios.batch.pdf;
-import fr.gouv.finances.dgfip.utils.IoUtils;
-import fr.gouv.finances.dgfip.utils.Pair;
-import fr.gouv.finances.dgfip.utils.xml.FactoryProvider;
-import fr.gouv.finances.dgfip.utils.xml.transform.CustomURIResolver;
-import fr.gouv.finances.dgfip.xemelios.auth.XemeliosUser;
-import fr.gouv.finances.dgfip.xemelios.batch.BatchRunner;
-import fr.gouv.finances.dgfip.xemelios.common.PJRef;
-import fr.gouv.finances.dgfip.xemelios.common.config.EtatModel;
-import fr.gouv.finances.dgfip.xemelios.data.DataLayerManager;
-import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
-import fr.gouv.finances.dgfip.xemelios.utils.FileUtils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
+
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
+
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xhtmlrenderer.context.StyleReference;
import org.xhtmlrenderer.pdf.ITextRenderer;
+import fr.gouv.finances.dgfip.utils.IoUtils;
+import fr.gouv.finances.dgfip.utils.Pair;
+import fr.gouv.finances.dgfip.utils.xml.FactoryProvider;
+import fr.gouv.finances.dgfip.utils.xml.transform.AbstractURIResolver;
+import fr.gouv.finances.dgfip.xemelios.auth.XemeliosUser;
+import fr.gouv.finances.dgfip.xemelios.batch.BatchRunner;
+import fr.gouv.finances.dgfip.xemelios.common.PJRef;
+import fr.gouv.finances.dgfip.xemelios.common.config.EtatModel;
+import fr.gouv.finances.dgfip.xemelios.data.DataLayerManager;
+import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
+import fr.gouv.finances.dgfip.xemelios.utils.FileUtils;
+
/**
- *
+ *
* @author cmarchand
*/
public class PdfCreatorCgEtatBalance {
- private EtatModel em;
- private String collectivite;
- private String budget;
- private int exercice;
- private Pair pColl, pBudg;
- private XemeliosUser user;
- private Logger logger = null;
- XPathFactory xpf = FactoryProvider.getXPathFactory();
-
- public PdfCreatorCgEtatBalance(EtatModel em, String collectivite, String budget, int exercice, XemeliosUser user) {
- super();
- this.em=em;
- this.collectivite=collectivite;
- this.budget=budget;
- this.exercice=exercice;
- this.pColl=new Pair(collectivite, collectivite);
- this.pBudg = new Pair(budget, budget);
- this.user=user;
- logger = Logger.getLogger(PdfCreatorCgEtatBalance.class.getName()+"<"+collectivite+","+budget+","+exercice+">");
- }
-
- public void run() {
- if(flagRowStart(null)!=1) {
- logger.warn("cancelling "+collectivite+"-"+budget+"-"+exercice);
- return;
- }
- String xpath=em.getImportableElement().getPath().getPath()+"[n:Pied/@NumPage='1']";
- XPath xp = xpf.newXPath();
- xp.setNamespaceContext(em.getParent().getNamespaces());
- try {
- ArrayList<String> documents = DataLayerManager.getImplementation().getDocumentListFromXPath(em.getImportableElement(), collectivite, budget, Integer.toString(exercice), null, null, xpath, user);
- if(documents.size()==1) {
- Document dom = DataLayerManager.getImplementation().getDocumentAsDom(em, pColl, pBudg, documents.get(0), user);
- String firstPage = "1";
- String lastPage=null;
- // on va chercher les numéros de page
- Element ligneSommaire = (Element)xp.evaluate("/n:CompteGestionEtat/n:Entete/n:Infos/n:Sommaire/n:LigneSommaire[1]", dom, XPathConstants.NODE);
- if(ligneSommaire!=null) {
- firstPage = ligneSommaire.getAttribute("FirstPage");
- lastPage = ligneSommaire.getAttribute("LastPage");
- } else {
- lastPage = xp.evaluate("/n:CompteGestionEtat/n:BalanceGenerale/n:PageBalanceGenerale/@added:LastPage", dom);
- }
- int first = Integer.parseInt(firstPage);
- int last = Integer.parseInt(lastPage);
- ITextRenderer renderer = new ITextRenderer();
- BatchUserAgentCallback callback = new BatchUserAgentCallback(renderer.getOutputDevice());
- callback.setSharedContext(renderer.getSharedContext());
- renderer.getSharedContext().setUserAgentCallback(callback);
- StyleReference css = new StyleReference(callback);
- renderer.getSharedContext().setCss(css);
- File xslFile = new File(new File(new File(System.getProperty("user.home")),"xemelios/documents-def-ovrrd"),em.getXslt());
- if(!xslFile.exists()) xslFile = new File(new File(em.getParent().getBaseDirectory()),em.getXslt());
- InputStream is = IoUtils.getInputStream(xslFile.getPath());
-
- File tmp = File.createTempFile("CGE_Bal_"+collectivite+"_"+budget+"_"+exercice, ".pdf", FileUtils.getTempDir());
-
- OutputStream output = new FileOutputStream(tmp);
- TransformerFactory tFactory = FactoryProvider.getTransformerFactory();
- tFactory.setURIResolver(new CustomURIResolver(tFactory.getURIResolver(),xslFile.getParentFile().toURI().toURL().toExternalForm()));
- Transformer tr = tFactory.newTransformer(new StreamSource(is));
- tr.setParameter("browser-destination", em.useExternalBrowser() ? "external" : "internal");
- // on passe la nomenclature en parametre
- // on recupere l'ID de la nomenclature
- String idRepository = null;
- if (em.getParent().getReferenceNomenclaturePath() != null && em.getParent().getReferenceNomenclaturePath().getCodePath() != null) {
- idRepository = (String) xp.evaluate(em.getParent().getReferenceNomenclaturePath().getCodePath().getPath(), dom.getDocumentElement(), XPathConstants.STRING);
- }
- if (idRepository != null && idRepository.length() == 0) {
- idRepository = null;
- }
- Document repository = DataLayerManager.getImplementation().getRepositoryAsDom(em.getParent(), pColl, idRepository, user);
- if (repository != null) {
- tr.setParameter("repository", repository);
- }
- // on passe la config
- tr.setParameter("config", BatchRunner.getInstance().getDocuments().getSmallDOM());
- tr.setParameter("collectivite", pColl.key);
- tr.setParameter("budget", budget);
-
- boolean isFirstPage = true;
- for(int i=first; i<=last; i++) {
- xpath=em.getImportableElement().getPath().getPath()+"[n:Pied/@NumPage='"+i+"']";
- documents = DataLayerManager.getImplementation().getDocumentListFromXPath(em.getImportableElement(), collectivite, budget, Integer.toString(exercice), null, null, xpath, user);
- Document doc = DataLayerManager.getImplementation().getDocumentAsDom(em, pColl, pBudg, documents.get(0), user);
- DOMResult result = new DOMResult();
- tr.transform(new DOMSource(doc), result);
- Document xhtml = (Document) result.getNode();
- renderer.setDocument(xhtml, "doc.xhtml");
- renderer.layout();
- if (isFirstPage) {
- renderer.createPDF(output, false);
- isFirstPage = false;
- } else {
- renderer.writeNextDocument();
- }
- }
- renderer.finishPDF();
- output.flush(); output.close();
-
- // attachement de la PJ
- PJRef pjRef = new PJRef();
- pjRef.setCollectivite(collectivite);
- pjRef.setFileName("CompteGestionEtat-BalanceGenerale-"+collectivite+"-"+budget+"-"+exercice+".pdf");
- pjRef.setPjName(pjRef.getFileName());
- FileInputStream fis = new FileInputStream(tmp);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- byte[] buffer = new byte[2048];
- int read = fis.read(buffer);
- do {
- baos.write(buffer, 0, read);
- read = fis.read(buffer);
- } while(read > 0);
- fis.close();
- pjRef.setData(baos.toByteArray());
- DataLayerManager.getImplementation().importPj(pjRef, null, user);
- tmp.delete();
- flagRowSuccess(1);
- } else {
- logger.error("ai trouvé "+documents.size()+" page 1");
- }
- } catch(Exception ex) {
- flagRowError(-1);
- logger.error("",ex);
- }
- }
-
- protected int flagRowStart(Integer value) {
- return flagRow(1, value);
- }
- protected int flagRowError(Integer value) {
- return flagRow(2,value);
- }
- protected int flagRowSuccess(Integer value) {
- return flagRow(3, value);
- }
- protected int flagRow(int status, Integer value) {
- Connection con = null;
- try {
- con = PoolManager.getInstance().getConnection();
- String sql = "UPDATE PDF_CREATION SET CREATION_STATUS=? WHERE DOCUMENT_ID='compteGestionEtat' AND ETAT_ID='BalanceGenerale' AND COLLECTIVITE=? AND BUDGET=? AND EXERCICE=?";
- if(value==null) sql = sql.concat(" AND CREATION_STATUS IS NULL");
- else if(value!= -1) sql = sql.concat(" AND CREATION_STATUS=?");
-logger.info(status+"/"+value+" : "+sql);
- PreparedStatement ps = con.prepareStatement(sql);
- ps.setInt(1,status);
- ps.setString(2, collectivite);
- ps.setString(3, budget);
- ps.setInt(4, exercice);
- if(value!=null && value!= -1) ps.setInt(5,value.intValue());
- return ps.executeUpdate();
- } catch(Exception ex) {
- logger.error("flagRow",ex);
- return 0;
- } finally {
- if(con!=null) {
- PoolManager.getInstance().releaseConnection(con);
- con = null;
- }
- }
- }
+ private final EtatModel em;
+ private final String collectivite;
+ private final String budget;
+ private final int exercice;
+ private final Pair pColl, pBudg;
+ private final XemeliosUser user;
+ private Logger logger = null;
+ XPathFactory xpf = FactoryProvider.getXPathFactory();
+
+ public PdfCreatorCgEtatBalance(final EtatModel em, final String collectivite, final String budget, final int exercice, final XemeliosUser user) {
+ super();
+ this.em = em;
+ this.collectivite = collectivite;
+ this.budget = budget;
+ this.exercice = exercice;
+ this.pColl = new Pair(collectivite, collectivite);
+ this.pBudg = new Pair(budget, budget);
+ this.user = user;
+ this.logger = Logger.getLogger(PdfCreatorCgEtatBalance.class.getName() + "<" + collectivite + "," + budget + "," + exercice + ">");
+ }
+
+ public void run() {
+ if (this.flagRowStart(null) != 1) {
+ this.logger.warn("cancelling " + this.collectivite + "-" + this.budget + "-" + this.exercice);
+ return;
+ }
+ String xpath = this.em.getImportableElement().getPath().getPath() + "[n:Pied/@NumPage='1']";
+ final XPath xp = this.xpf.newXPath();
+ xp.setNamespaceContext(this.em.getParent().getNamespaces());
+ try {
+ ArrayList<String> documents = DataLayerManager.getImplementation().getDocumentListFromXPath(this.em.getImportableElement(), this.collectivite, this.budget, Integer.toString(this.exercice), null, null, xpath, this.user);
+ if (documents.size() == 1) {
+ final Document dom = DataLayerManager.getImplementation().getDocumentAsDom(this.em, this.pColl, this.pBudg, documents.get(0), this.user);
+ String firstPage = "1";
+ String lastPage = null;
+ // on va chercher les numéros de page
+ final Element ligneSommaire = (Element) xp.evaluate("/n:CompteGestionEtat/n:Entete/n:Infos/n:Sommaire/n:LigneSommaire[1]", dom, XPathConstants.NODE);
+ if (ligneSommaire != null) {
+ firstPage = ligneSommaire.getAttribute("FirstPage");
+ lastPage = ligneSommaire.getAttribute("LastPage");
+ } else {
+ lastPage = xp.evaluate("/n:CompteGestionEtat/n:BalanceGenerale/n:PageBalanceGenerale/@added:LastPage", dom);
+ }
+ final int first = Integer.parseInt(firstPage);
+ final int last = Integer.parseInt(lastPage);
+ final ITextRenderer renderer = new ITextRenderer();
+ final BatchUserAgentCallback callback = new BatchUserAgentCallback(renderer.getOutputDevice());
+ callback.setSharedContext(renderer.getSharedContext());
+ renderer.getSharedContext().setUserAgentCallback(callback);
+ final StyleReference css = new StyleReference(callback);
+ renderer.getSharedContext().setCss(css);
+ File xslFile = new File(new File(new File(System.getProperty("user.home")), "xemelios/documents-def-ovrrd"), this.em.getXslt());
+ if (!xslFile.exists()) {
+ xslFile = new File(new File(this.em.getParent().getBaseDirectory()), this.em.getXslt());
+ }
+ final InputStream is = IoUtils.getInputStream(xslFile.getPath());
+
+ final File tmp = File.createTempFile("CGE_Bal_" + this.collectivite + "_" + this.budget + "_" + this.exercice, ".pdf", FileUtils.getTempDir());
+
+ final OutputStream output = new FileOutputStream(tmp);
+ final TransformerFactory tFactory = FactoryProvider.getTransformerFactory();
+ final AbstractURIResolver uriResolver = this.em.getParent().createUriResolver();
+ AbstractURIResolver.initializationHelper(uriResolver, dom, tFactory.getURIResolver(), xslFile.getParentFile().toURI().toURL().toExternalForm());
+
+ tFactory.setURIResolver(uriResolver);
+
+ final Transformer tr = tFactory.newTransformer(new StreamSource(is));
+ tr.setParameter("browser-destination", this.em.useExternalBrowser() ? "external" : "internal");
+ // on passe la nomenclature en parametre
+ // on recupere l'ID de la nomenclature
+ String idRepository = null;
+ if (this.em.getParent().getReferenceNomenclaturePath() != null && this.em.getParent().getReferenceNomenclaturePath().getCodePath() != null) {
+ idRepository = (String) xp.evaluate(this.em.getParent().getReferenceNomenclaturePath().getCodePath().getPath(), dom.getDocumentElement(), XPathConstants.STRING);
+ }
+ if (idRepository != null && idRepository.length() == 0) {
+ idRepository = null;
+ }
+ final Document repository = DataLayerManager.getImplementation().getRepositoryAsDom(this.em.getParent(), this.pColl, idRepository, this.user);
+ if (repository != null) {
+ tr.setParameter("repository", repository);
+ }
+ // on passe la config
+ tr.setParameter("config", BatchRunner.getInstance().getDocuments().getSmallDOM());
+ tr.setParameter("collectivite", this.pColl.key);
+ tr.setParameter("budget", this.budget);
+
+ boolean isFirstPage = true;
+ for (int i = first; i <= last; i++) {
+ xpath = this.em.getImportableElement().getPath().getPath() + "[n:Pied/@NumPage='" + i + "']";
+ documents = DataLayerManager.getImplementation().getDocumentListFromXPath(this.em.getImportableElement(), this.collectivite, this.budget, Integer.toString(this.exercice), null, null, xpath, this.user);
+ final Document doc = DataLayerManager.getImplementation().getDocumentAsDom(this.em, this.pColl, this.pBudg, documents.get(0), this.user);
+ final DOMResult result = new DOMResult();
+ tr.transform(new DOMSource(doc), result);
+ final Document xhtml = (Document) result.getNode();
+ renderer.setDocument(xhtml, "doc.xhtml");
+ renderer.layout();
+ if (isFirstPage) {
+ renderer.createPDF(output, false);
+ isFirstPage = false;
+ } else {
+ renderer.writeNextDocument();
+ }
+ }
+ renderer.finishPDF();
+ output.flush();
+ output.close();
+
+ // attachement de la PJ
+ final PJRef pjRef = new PJRef();
+ pjRef.setCollectivite(this.collectivite);
+ pjRef.setFileName("CompteGestionEtat-BalanceGenerale-" + this.collectivite + "-" + this.budget + "-" + this.exercice + ".pdf");
+ pjRef.setPjName(pjRef.getFileName());
+ final FileInputStream fis = new FileInputStream(tmp);
+ final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ final byte[] buffer = new byte[2048];
+ int read = fis.read(buffer);
+ do {
+ baos.write(buffer, 0, read);
+ read = fis.read(buffer);
+ } while (read > 0);
+ fis.close();
+ pjRef.setData(baos.toByteArray());
+ DataLayerManager.getImplementation().importPj(pjRef, null, this.user);
+ tmp.delete();
+ this.flagRowSuccess(1);
+ } else {
+ this.logger.error("ai trouvé " + documents.size() + " page 1");
+ }
+ } catch (final Exception ex) {
+ this.flagRowError(-1);
+ this.logger.error("", ex);
+ }
+ }
+
+ protected int flagRowStart(final Integer value) {
+ return this.flagRow(1, value);
+ }
+
+ protected int flagRowError(final Integer value) {
+ return this.flagRow(2, value);
+ }
+
+ protected int flagRowSuccess(final Integer value) {
+ return this.flagRow(3, value);
+ }
+
+ protected int flagRow(final int status, final Integer value) {
+ Connection con = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ String sql = "UPDATE PDF_CREATION SET CREATION_STATUS=? WHERE DOCUMENT_ID='compteGestionEtat' AND ETAT_ID='BalanceGenerale' AND COLLECTIVITE=? AND BUDGET=? AND EXERCICE=?";
+ if (value == null) {
+ sql = sql.concat(" AND CREATION_STATUS IS NULL");
+ } else if (value != -1) {
+ sql = sql.concat(" AND CREATION_STATUS=?");
+ }
+ this.logger.info(status + "/" + value + " : " + sql);
+ final PreparedStatement ps = con.prepareStatement(sql);
+ ps.setInt(1, status);
+ ps.setString(2, this.collectivite);
+ ps.setString(3, this.budget);
+ ps.setInt(4, this.exercice);
+ if (value != null && value != -1) {
+ ps.setInt(5, value.intValue());
+ }
+ return ps.executeUpdate();
+ } catch (final Exception ex) {
+ this.logger.error("flagRow", ex);
+ return 0;
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ con = null;
+ }
+ }
+ }
}
package fr.gouv.finances.dgfip.xemelios.batch.pdf;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.util.ArrayList;
+import java.util.StringTokenizer;
+
+import org.apache.log4j.Logger;
+
import fr.gouv.finances.cp.xemelios.data.impl.MySqlDataLayer;
import fr.gouv.finances.dgfip.xemelios.auth.XemeliosUser;
import fr.gouv.finances.dgfip.xemelios.batch.Batch;
import fr.gouv.finances.dgfip.xemelios.data.impl.sqlconfig.TEtat;
import fr.gouv.finances.dgfip.xemelios.data.impl.sqlconfig.TPersistenceConfig;
import fr.gouv.finances.dgfip.xemelios.data.impl.sqlconfig.TTable;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.StringTokenizer;
-import org.apache.log4j.Logger;
/**
- *
+ *
* @author cmarchand
*/
public class PdfPreparator extends Batch {
- private static final Logger logger = Logger.getLogger(PdfPreparator.class);
- 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_DOCUMENT_ID = "documents";
- public static final Integer STATUS_NONE = null;
- public static final Integer STATUS_IDLE = 0;
- public static final Integer STATUS_RUNNING = 1;
- public static final Integer STATUS_TERMINATED = 2;
- public static final Integer STATUS_ERROR = 3;
+ private static final Logger logger = Logger.getLogger(PdfPreparator.class);
+ 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_DOCUMENT_ID = "documents";
+ public static final Integer STATUS_NONE = null;
+ public static final Integer STATUS_IDLE = 0;
+ public static final Integer STATUS_RUNNING = 1;
+ public static final Integer STATUS_TERMINATED = 2;
+ public static final Integer STATUS_ERROR = 3;
+
+ private int maxConcurrentThreads = 1;
+
+ private String documents;
+
+ private ThreadPool<PdfPreparatorImpl> pool = null;
+
+ public PdfPreparator(final String args[]) {
+ super(args);
+ }
- private int maxConcurrentThreads = 1;
+ @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) {
+ }
+ }
+ this.documents = this.getProps().getProperty(PROP_DOCUMENT_ID);
+ }
- private String documents;
+ @Override
+ protected void doProcess() throws Exception {
+ logger.debug("running PdfPreparator for document " + this.documents);
+ final XemeliosUser user = new User();
+ final ArrayList<PdfPreparatorImpl> threads = new ArrayList<PdfPreparatorImpl>();
+ try {
+ final DataImpl impl = DataLayerManager.getImplementation();
+ if (impl instanceof MySqlDataLayer) {
+ final MySqlDataLayer msImpl = (MySqlDataLayer) impl;
+ final StringTokenizer st = new StringTokenizer(this.documents, ",");
+ while (st.hasMoreTokens()) {
+ final PdfPreparatorImpl process = new PdfPreparatorImpl(st.nextToken(), user, msImpl);
+ threads.add(process);
+ }
+ this.pool = new ThreadPool<PdfPreparatorImpl>(threads);
+ logger.debug("starting pool with " + this.maxConcurrentThreads + " max concurrent threads");
+ this.pool.start(this.maxConcurrentThreads);
+ } else {
+ throw new RuntimeException("PdfCreator only runs with MysqlDataLayer");
+ }
+ } catch (final Exception ex) {
+ logger.error("in doProcess", ex);
+ }
+ this.pool = new ThreadPool<PdfPreparatorImpl>(threads);
+ logger.debug("starting pool with " + this.maxConcurrentThreads + " max concurrent threads");
+ this.pool.start(this.maxConcurrentThreads);
+ }
- private static boolean initialized = false;
+ @Override
+ public String getResumeTraitement() {
+ return "pdf.preparator";
+ }
- private ThreadPool<PdfPreparatorImpl> pool = null;
+ @Override
+ public String typeTraitementRefCode() {
+ return this.getResumeTraitement();
+ }
- public PdfPreparator(String args[]) {
- super(args);
- }
+ @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 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) {}
- }
- documents = getProps().getProperty(PROP_DOCUMENT_ID);
- }
+ @Override
+ protected String getBatchVersion() {
+ return "1.0";
+ }
- @Override
- protected void doProcess() throws Exception {
- logger.debug("running PdfPreparator for document "+documents);
- XemeliosUser user = new User();
- ArrayList<PdfPreparatorImpl> threads = new ArrayList<PdfPreparatorImpl>();
- try {
- DataImpl impl = DataLayerManager.getImplementation();
- if(impl instanceof MySqlDataLayer) {
- MySqlDataLayer msImpl = (MySqlDataLayer)impl;
- StringTokenizer st = new StringTokenizer(documents, ",");
- while(st.hasMoreTokens()) {
- PdfPreparatorImpl process = new PdfPreparatorImpl(st.nextToken(), user, msImpl);
- threads.add(process);
- }
- pool = new ThreadPool<PdfPreparatorImpl>(threads);
- logger.debug("starting pool with "+maxConcurrentThreads+" max concurrent threads");
- pool.start(maxConcurrentThreads);
- } else {
- throw new RuntimeException("PdfCreator only runs with MysqlDataLayer");
- }
- } catch(Exception ex) {
- logger.error("in doProcess",ex);
- }
- pool = new ThreadPool<PdfPreparatorImpl>(threads);
- logger.debug("starting pool with "+maxConcurrentThreads+" max concurrent threads");
- pool.start(maxConcurrentThreads);
- }
+ private class User implements XemeliosUser {
+ @Override
+ public String getId() {
+ return "PdfPreparatorUser";
+ }
- @Override
- public String getResumeTraitement() {
- return "pdf.preparator";
- }
+ @Override
+ public String getDisplayName() {
+ return this.getId();
+ }
- @Override
- public String typeTraitementRefCode() {
- return getResumeTraitement();
- }
+ @Override
+ public boolean hasRole(final String role) {
+ return true;
+ }
- @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
+ public boolean hasDocument(final String document) {
+ return true;
+ }
- @Override
- protected String getBatchVersion() {
- return "1.0";
- }
+ @Override
+ public boolean hasCollectivite(final String collectivite, final DocumentModel dm) {
+ return true;
+ }
+ }
- private class User implements XemeliosUser {
- @Override
- public String getId() {
- return "PdfPreparatorUser";
- }
- @Override
- public String getDisplayName() {
- return getId();
- }
- @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;
- }
- }
+ protected class PdfPreparatorImpl extends RunnableBatch {
+ private final String documentId;
+ private final XemeliosUser user;
+ private final MySqlDataLayer msLayer;
- protected class PdfPreparatorImpl extends RunnableBatch {
- private String documentId;
- private XemeliosUser user;
- private MySqlDataLayer msLayer;
+ public PdfPreparatorImpl(final String documentId, final XemeliosUser user, final MySqlDataLayer msLayer) {
+ super();
+ this.documentId = documentId;
+ this.msLayer = msLayer;
+ this.user = user;
+ }
- public PdfPreparatorImpl(String documentId, XemeliosUser user, MySqlDataLayer msLayer) {
- super();
- this.documentId=documentId;
- this.msLayer = msLayer;
- this.user=user;
- }
+ @Override
+ public void run() {
+ super.run();
+ if ("cg-etat".equals(this.documentId)) {
+ this.prepareCompteGestionEtat();
+ } else {
+ this.getNotifier().notifyThreadStop(this);
+ throw new RuntimeException("PdfPreparator does not know how to prepare " + this.documentId);
+ }
+ this.getNotifier().notifyThreadStop(this);
+ }
- @Override
- public void run() {
- super.run();
- if("cg-etat".equals(documentId)) {
- prepareCompteGestionEtat();
- } else {
- getNotifier().notifyThreadStop(this);
- throw new RuntimeException("PdfPreparator does not know how to prepare "+documentId);
- }
- getNotifier().notifyThreadStop(this);
- }
+ @Override
+ public String getInformations() {
+ final StringBuilder sb = new StringBuilder();
+ sb.append("\t").append(this.getName()).append(":");
+ if (this.isAlive()) {
+ sb.append("PdfPreparator<" + this.documentId + "> running.");
+ } else {
+ if (PdfPreparator.this.isStarted()) {
+ sb.append("PdfPreparator<" + this.documentId + "> fully terminated.");
+ } else {
+ sb.append("PdfPreparator<" + this.documentId + "> idle");
+ }
+ }
+ sb.append("\n");
+ return sb.toString();
+ }
- @Override
- public String getInformations() {
- StringBuilder sb = new StringBuilder();
- sb.append("\t").append(getName()).append(":");
- if(isAlive()) {
- sb.append("PdfPreparator<"+documentId+"> running.");
- } else {
- if(isStarted()) {
- sb.append("PdfPreparator<"+documentId+"> fully terminated.");
- } else {
- sb.append("PdfPreparator<"+documentId+"> idle");
- }
- }
- sb.append("\n");
- return sb.toString();
- }
- protected void prepareCompteGestionEtat() {
- Connection con = null;
- try {
- con = PoolManager.getInstance().getConnection();
- String createTable =
- "CREATE TABLE IF NOT EXISTS PDF_CREATION ( "+
- "DOCUMENT_ID VARCHAR(100) NOT NULL, "+
- "ETAT_ID VARCHAR(100) NOT NULL, "+
- "COLLECTIVITE VARCHAR(15) NOT NULL, "+
- "BUDGET VARCHAR(15) NOT NULL, "+
- "EXERCICE INTEGER, "+
- "CREATION_STATUS INTEGER ) ENGINE MYISAM";
- con.createStatement().executeUpdate(createTable);
- DocumentModel dm = BatchRunner.getInstance().getDocuments().getDocumentById(documentId);
- TPersistenceConfig pc = msLayer.getPersistenceConfig(dm, user);
- TDocument docP = pc.getLayer(msLayer.getLayerName()).getDocument(documentId);
- PreparedStatement psCheck = con.prepareStatement("SELECT 1 FROM PDF_CREATION WHERE DOCUMENT_ID='cg-etat' AND ETAT_ID=? AND COLLECTIVITE=? AND BUDGET=? AND EXERCICE=?");
- PreparedStatement psInsert = con.prepareStatement("INSERT INTO PDF_CREATION (DOCUMENT_ID,ETAT_ID,COLLECTIVITE,BUDGET,EXERCICE) VALUES ('cg-etat',?,?,?,?)");
- for(Iterator<TEtat> it=docP.getEtats().iterator();it.hasNext();) {
- TEtat etat = it.next();
- TTable table = etat.getMaintable();
- String select = "SELECT DISTINCT COLLECTIVITE, BUDGET, EXERCICE FROM "+table.getBaseName();
-logger.debug(select);
- ResultSet rs = con.createStatement().executeQuery(select);
- while(rs.next()) {
- String collectivite = rs.getString(1);
- String budget = rs.getString(2);
- int exercice = rs.getInt(3);
- psCheck.setString(1,etat.getId());
- psCheck.setString(2,collectivite);
- psCheck.setString(3,budget);
- psCheck.setInt(4, exercice);
- ResultSet rs2 = psCheck.executeQuery();
- if(!rs2.next()) {
- psInsert.setString(1,etat.getId());
- psInsert.setString(2,collectivite);
- psInsert.setString(3,budget);
- psInsert.setInt(4, exercice);
- psInsert.executeUpdate();
- }
- rs2.close();
- }
- }
- psCheck.close();
- psInsert.close();
- } catch(Exception ex) {
- logger.error("prepareCompteGestionEtat",ex);
- } finally {
- if(con!=null) {
- PoolManager.getInstance().releaseConnection(con);
- con = null;
- }
- }
- }
- }
+ protected void prepareCompteGestionEtat() {
+ Connection con = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ final String createTable = "CREATE TABLE IF NOT EXISTS PDF_CREATION ( " + "DOCUMENT_ID VARCHAR(100) NOT NULL, " + "ETAT_ID VARCHAR(100) NOT NULL, " + "COLLECTIVITE VARCHAR(15) NOT NULL, " + "BUDGET VARCHAR(15) NOT NULL, " + "EXERCICE INTEGER, "
+ + "CREATION_STATUS INTEGER ) ENGINE MYISAM";
+ con.createStatement().executeUpdate(createTable);
+ final DocumentModel dm = BatchRunner.getInstance().getDocuments().getDocumentById(this.documentId);
+ final TPersistenceConfig pc = this.msLayer.getPersistenceConfig(dm, this.user);
+ final TDocument docP = pc.getLayer(this.msLayer.getLayerName()).getDocument(this.documentId);
+ final PreparedStatement psCheck = con.prepareStatement("SELECT 1 FROM PDF_CREATION WHERE DOCUMENT_ID='cg-etat' AND ETAT_ID=? AND COLLECTIVITE=? AND BUDGET=? AND EXERCICE=?");
+ final PreparedStatement psInsert = con.prepareStatement("INSERT INTO PDF_CREATION (DOCUMENT_ID,ETAT_ID,COLLECTIVITE,BUDGET,EXERCICE) VALUES ('cg-etat',?,?,?,?)");
+ for (final TEtat etat : docP.getEtats()) {
+ final TTable table = etat.getMaintable();
+ final String select = "SELECT DISTINCT COLLECTIVITE, BUDGET, EXERCICE FROM " + table.getBaseName();
+ logger.debug(select);
+ final ResultSet rs = con.createStatement().executeQuery(select);
+ while (rs.next()) {
+ final String collectivite = rs.getString(1);
+ final String budget = rs.getString(2);
+ final int exercice = rs.getInt(3);
+ psCheck.setString(1, etat.getId());
+ psCheck.setString(2, collectivite);
+ psCheck.setString(3, budget);
+ psCheck.setInt(4, exercice);
+ final ResultSet rs2 = psCheck.executeQuery();
+ if (!rs2.next()) {
+ psInsert.setString(1, etat.getId());
+ psInsert.setString(2, collectivite);
+ psInsert.setString(3, budget);
+ psInsert.setInt(4, exercice);
+ psInsert.executeUpdate();
+ }
+ rs2.close();
+ }
+ }
+ psCheck.close();
+ psInsert.close();
+ } catch (final Exception ex) {
+ logger.error("prepareCompteGestionEtat", ex);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ con = null;
+ }
+ }
+ }
+ }
}
package fr.gouv.finances.dgfip.xemelios.batch.suivi;
-import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
+
import org.apache.log4j.Logger;
+import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
+
/**
- *
+ *
* @author cmarchand
*/
public class Message {
- private static final Logger logger = Logger.getLogger(Message.class);
- private Traitement traitement;
- private long messageId;
- private String severity;
- private Timestamp dateMessage;
- private String message;
- private String category;
- private String infoCompl;
+ private static final Logger logger = Logger.getLogger(Message.class);
+ private final Traitement traitement;
+ private long messageId;
+ private String severity;
+ private final Timestamp dateMessage;
+ private final String message;
+ private String infoCompl;
+
+ public Message(final Traitement traitement, final String severity, final String message) {
+ super();
+ this.traitement = traitement;
+ this.severity = severity;
+ this.message = message;
+ this.dateMessage = new Timestamp(System.currentTimeMillis());
+ this.__createInDatabase();
+ }
+
+ public void setSeverity(final String newSeverity) {
+ Connection con = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ final String sql = "update MESSAGE_TRAITEMENT set MTR_CODE_GRAVITE=? where TRT_ID=? and MTR_ID=?";
+ final PreparedStatement ps = con.prepareStatement(sql);
+ ps.setString(1, newSeverity);
+ ps.setLong(2, this.traitement.getId());
+ ps.setLong(3, this.messageId);
+ ps.executeUpdate();
+ this.severity = newSeverity;
+ this.traitement.__updateSeverity(newSeverity);
+ } catch (final Exception ex) {
+ logger.error("Message.setSeverity(String)", ex);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ }
+ }
+
+ public String getInfoCompl() {
+ return this.infoCompl;
+ }
- public Message(final Traitement traitement, final String severity, final String message) {
- super();
- this.traitement = traitement;
- this.severity = severity;
- this.message=message;
- this.dateMessage = new Timestamp(System.currentTimeMillis());
- __createInDatabase();
- }
- public void setSeverity(String newSeverity) {
- Connection con = null;
- try {
- con = PoolManager.getInstance().getConnection();
- String sql = "update MESSAGE_TRAITEMENT set MTR_CODE_GRAVITE=? where TRT_ID=? and MTR_ID=?";
- PreparedStatement ps = con.prepareStatement(sql);
- ps.setString(1, newSeverity);
- ps.setLong(2, traitement.getId());
- ps.setLong(3, messageId);
- ps.executeUpdate();
- this.severity = newSeverity;
- traitement.__updateSeverity(newSeverity);
- } catch(Exception ex) {
- logger.error("Message.setSeverity(String)", ex);
- } finally {
- if(con!=null) PoolManager.getInstance().releaseConnection(con);
- }
- }
- public String getInfoCompl() { return infoCompl; }
- public void setInfoCompl(String info) {
- Connection con = null;
- if(info.length()>4000) info = info.substring(0, 4000);
- try {
- con = PoolManager.getInstance().getConnection();
- String sql = "update MESSAGE_TRAITEMENT set MTR_INFO_COMP=? where TRT_ID=? and MTR_ID=?";
- PreparedStatement ps = con.prepareStatement(sql);
- ps.setString(1, info);
- ps.setLong(2, traitement.getId());
- ps.setLong(3, messageId);
- ps.executeUpdate();
- this.infoCompl = info;
- } catch(Exception ex) {
- logger.error("Message.setInfoCompl(String)", ex);
- } finally {
- if(con!=null) PoolManager.getInstance().releaseConnection(con);
- }
- }
+ public void setInfoCompl(String info) {
+ Connection con = null;
+ if (info.length() > 4000) {
+ info = info.substring(0, 4000);
+ }
+ try {
+ con = PoolManager.getInstance().getConnection();
+ final String sql = "update MESSAGE_TRAITEMENT set MTR_INFO_COMP=? where TRT_ID=? and MTR_ID=?";
+ final PreparedStatement ps = con.prepareStatement(sql);
+ ps.setString(1, info);
+ ps.setLong(2, this.traitement.getId());
+ ps.setLong(3, this.messageId);
+ ps.executeUpdate();
+ this.infoCompl = info;
+ } catch (final Exception ex) {
+ logger.error("Message.setInfoCompl(String)", ex);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ }
+ }
- protected void __createInDatabase() {
- Connection con = null;
- try {
- con = PoolManager.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("SELECT MAX(MTR_ID) FROM MESSAGE_TRAITEMENT WHERE TRT_ID=?");
- ps.setLong(1,traitement.getId());
- ResultSet rs = ps.executeQuery();
- if(rs.next()) {
- messageId = rs.getLong(1)+1;
- } else {
- messageId = 1;
- }
- String sql = "insert into MESSAGE_TRAITEMENT (TRT_ID, MTR_ID, MTR_CODE_GRAVITE, MTR_MESSAGE) values (?,?,?,?)";
- ps = con.prepareStatement(sql);
- ps.setLong(1, traitement.getId());
- ps.setLong(2, messageId);
- ps.setString(3, severity);
- ps.setString(4, message);
- ps.executeUpdate();
- ps.close();
- } catch(Exception ex) {
- logger.error("Message.__createInDatabase()", ex);
- } finally {
- if(con!=null) PoolManager.getInstance().releaseConnection(con);
- }
- }
+ protected void __createInDatabase() {
+ Connection con = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ PreparedStatement ps = con.prepareStatement("SELECT MAX(MTR_ID) FROM MESSAGE_TRAITEMENT WHERE TRT_ID=?");
+ ps.setLong(1, this.traitement.getId());
+ final ResultSet rs = ps.executeQuery();
+ if (rs.next()) {
+ this.messageId = rs.getLong(1) + 1;
+ } else {
+ this.messageId = 1;
+ }
+ final String sql = "insert into MESSAGE_TRAITEMENT (TRT_ID, MTR_ID, MTR_CODE_GRAVITE, MTR_MESSAGE) values (?,?,?,?)";
+ ps = con.prepareStatement(sql);
+ ps.setLong(1, this.traitement.getId());
+ ps.setLong(2, this.messageId);
+ ps.setString(3, this.severity);
+ ps.setString(4, this.message);
+ ps.executeUpdate();
+ ps.close();
+ } catch (final Exception ex) {
+ logger.error("Message.__createInDatabase()", ex);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ }
+ }
}
package fr.gouv.finances.dgfip.xemelios.batch.suivi;
-import fr.gouv.finances.dgfip.xemelios.data.DataLayerManager;
-import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
import java.io.ByteArrayInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
+
import org.apache.log4j.Logger;
+import fr.gouv.finances.dgfip.xemelios.data.DataLayerManager;
+import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
+
/**
- *
+ *
* @author cmarchand
*/
public class Traitement {
- private static final Logger logger = Logger.getLogger(Traitement.class);
- public static final transient String SEVERITY_INFO = "I";
- public static final transient String SEVERITY_WARN = "EN";
- public static final transient String SEVERITY_ERROR = "EB";
- private long id;
- /**
- * Class lancée pour ce traitement
- */
- private String className;
- /**
- * Nom donné au traitement, issu du fichier de configuration
- */
- private String batchName;
- private Timestamp start;
- private Timestamp end;
- private String severity;
- private String resume;
- private List<Message> messages;
-
- public Traitement(Class batch, String batchName) {
- super();
- this.className = batch.getName();
- this.batchName = batchName;
- this.start = new Timestamp(System.currentTimeMillis());
- this.severity = SEVERITY_INFO;
- messages = new ArrayList<Message>();
- createInDatabase();
- }
- public long getStart() { return start.getTime(); }
- public void setEnd(long end) {
- this.end = new Timestamp(end);
- updateEnd();
- }
- public void setResume(String resume) {
- this.resume = resume;
- updateResume();
- }
- public void setSeverity(String severity) {
- this.severity = severity;
- updateSeverity();
- }
- public Message addMessage(String severity, String message) {
- Message msg = new Message(this, severity, message);
- messages.add(msg);
- __updateSeverity(severity);
- return msg;
- }
- /**
- * Stocke les données binaires sur ce traitement. Attention,
- * ;enregistre les données telles quelle. Il faut donc les
- * GZipper avant d'appeler cette méthode
- * @param data Les données GZippées
- */
- public void setBinaryData(byte[] data) {
- Connection con = null;
- try {
- con = PoolManager.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("update TRAITEMENT set TRT_BINARY_DATA=? where TRT_ID=?");
- ps.setBinaryStream(1, new ByteArrayInputStream(data), data.length);
- ps.setLong(2, id);
- ps.executeUpdate();
- } catch(Throwable ex) {
- logger.error("Traitement.setBinaryData(byte[])",ex);
- } finally {
- if(con!=null) PoolManager.getInstance().releaseConnection(con);
- }
- }
- public long getId() {
- return id;
- }
- protected void createInDatabase() {
- Connection con = null;
- try {
- con = PoolManager.getInstance().getConnection();
- if("mysql".equals(DataLayerManager.getImplementation().getLayerName())) {
- String sql = "insert into TRAITEMENT(TRT_ID,TRT_CLASS,TRT_DESC,TRT_DATE_DEBUT,TRT_CODE_GRAVITE) VALUES(?,?,?,?,?)";
- con.createStatement().executeUpdate("LOCK TABLES TRAITEMENT WRITE");
- ResultSet rs = con.createStatement().executeQuery("SELECT MAX(TRT_ID)+1 FROM TRAITEMENT");
- if(rs.next()) id = rs.getLong(1);
- else id = 1;
- rs.close();
- PreparedStatement ps = con.prepareStatement(sql);
- ps.setLong(1,id);
- ps.setString(2,className);
- ps.setString(3, batchName);
- ps.setTimestamp(4, start);
- ps.setString(5,severity);
- ps.execute();
- ps.close();
- con.createStatement().executeUpdate("UNLOCK TABLES");
- } else if("oracle".equals(DataLayerManager.getImplementation().getLayerName())) {
- ResultSet rs = con.createStatement().executeQuery("select SQ_TRAITEMENT.NEXTVAL from dual");
- if(rs.next())
- id = rs.getLong(1);
- else
- id = 1;
-
- String sql = "insert into TRAITEMENT(TRT_ID,TRT_CLASS,TRT_DESC,TRT_CODE_GRAVITE) VALUES(?,?,?,?)";
-
- logger.debug(sql + " --> with id : " + id);
-
- PreparedStatement ps = con.prepareStatement(sql);
- ps.setLong(1,id);
- ps.setString(2, className);
- ps.setString(3, batchName);
- ps.setString(4,severity);
- ps.execute();
- ps.close();
- }
- } catch(Exception ex) {
- logger.error("Traitement.createInDatabase()",ex);
- } finally {
- if(con!=null) PoolManager.getInstance().releaseConnection(con);
- }
- }
- protected void updateEnd() {
- Connection con = null;
- try {
- con = PoolManager.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("update TRAITEMENT set TRT_DATE_FIN=? where TRT_ID=?");
- ps.setTimestamp(1, end);
- ps.setLong(2,id);
- ps.executeUpdate();
- } catch(Exception ex) {
- logger.error("Traitement.updateEnd()",ex);
- } finally {
- if(con!=null) PoolManager.getInstance().releaseConnection(con);
- }
- }
- protected void updateResume() {
- Connection con = null;
- try {
- con = PoolManager.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("update TRAITEMENT set TRT_RESUME=? where TRT_ID=?");
- ps.setString(1, resume);
- ps.setLong(2,id);
- ps.executeUpdate();
- } catch(Exception ex) {
- logger.error("Traitement.updateResume()",ex);
- } finally {
- if(con!=null) PoolManager.getInstance().releaseConnection(con);
- }
- }
- protected void updateSeverity() {
- Connection con = null;
- try {
- con = PoolManager.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("update TRAITEMENT set TRT_CODE_GRAVITE=? where TRT_ID=?");
- ps.setString(1, severity);
- ps.setLong(2,id);
- ps.executeUpdate();
- } catch(Exception ex) {
- logger.error("Traitement.updateSeverity()",ex);
- } finally {
- if(con!=null) PoolManager.getInstance().releaseConnection(con);
- }
- }
- void __updateSeverity(String newSeverity) {
- int localRank = getSeverityRank(severity);
- int newRank = getSeverityRank(newSeverity);
- if(newRank>localRank) {
- setSeverity(newSeverity);
- }
- }
- private static int getSeverityRank(String sev) {
- if(sev.equals(SEVERITY_INFO)) return 1;
- else if(sev.equals(SEVERITY_WARN)) return 2;
- else return 3;
- }
+ private static final Logger logger = Logger.getLogger(Traitement.class);
+ public static final transient String SEVERITY_INFO = "I";
+ public static final transient String SEVERITY_WARN = "EN";
+ public static final transient String SEVERITY_ERROR = "EB";
+ private long id;
+ /**
+ * Class lancée pour ce traitement
+ */
+ private final String className;
+ /**
+ * Nom donné au traitement, issu du fichier de configuration
+ */
+ private final String batchName;
+ private final Timestamp start;
+ private Timestamp end;
+ private String severity;
+ private String resume;
+ private final List<Message> messages;
+
+ public Traitement(final Class batch, final String batchName) {
+ super();
+ this.className = batch.getName();
+ this.batchName = batchName;
+ this.start = new Timestamp(System.currentTimeMillis());
+ this.severity = SEVERITY_INFO;
+ this.messages = new ArrayList<Message>();
+ this.createInDatabase();
+ }
+
+ public long getStart() {
+ return this.start.getTime();
+ }
+
+ public void setEnd(final long end) {
+ this.end = new Timestamp(end);
+ this.updateEnd();
+ }
+
+ public void setResume(final String resume) {
+ this.resume = resume;
+ this.updateResume();
+ }
+
+ public void setSeverity(final String severity) {
+ this.severity = severity;
+ this.updateSeverity();
+ }
+
+ public Message addMessage(final String severity, final String message) {
+ final Message msg = new Message(this, severity, message);
+ this.messages.add(msg);
+ this.__updateSeverity(severity);
+ return msg;
+ }
+
+ /**
+ * Stocke les données binaires sur ce traitement. Attention, ;enregistre les données telles quelle. Il faut donc les GZipper avant d'appeler cette méthode
+ *
+ * @param data
+ * Les données GZippées
+ */
+ public void setBinaryData(final byte[] data) {
+ Connection con = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ final PreparedStatement ps = con.prepareStatement("update TRAITEMENT set TRT_BINARY_DATA=? where TRT_ID=?");
+ ps.setBinaryStream(1, new ByteArrayInputStream(data), data.length);
+ ps.setLong(2, this.id);
+ ps.executeUpdate();
+ } catch (final Throwable ex) {
+ logger.error("Traitement.setBinaryData(byte[])", ex);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ }
+ }
+
+ public long getId() {
+ return this.id;
+ }
+
+ protected void createInDatabase() {
+ Connection con = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ if ("mysql".equals(DataLayerManager.getImplementation().getLayerName())) {
+ final String sql = "insert into TRAITEMENT(TRT_ID,TRT_CLASS,TRT_DESC,TRT_DATE_DEBUT,TRT_CODE_GRAVITE) VALUES(?,?,?,?,?)";
+ con.createStatement().executeUpdate("LOCK TABLES TRAITEMENT WRITE");
+ final ResultSet rs = con.createStatement().executeQuery("SELECT MAX(TRT_ID)+1 FROM TRAITEMENT");
+ if (rs.next()) {
+ this.id = rs.getLong(1);
+ } else {
+ this.id = 1;
+ }
+ rs.close();
+ final PreparedStatement ps = con.prepareStatement(sql);
+ ps.setLong(1, this.id);
+ ps.setString(2, this.className);
+ ps.setString(3, this.batchName);
+ ps.setTimestamp(4, this.start);
+ ps.setString(5, this.severity);
+ ps.execute();
+ ps.close();
+ con.createStatement().executeUpdate("UNLOCK TABLES");
+ } else if ("oracle".equals(DataLayerManager.getImplementation().getLayerName())) {
+ final ResultSet rs = con.createStatement().executeQuery("select SQ_TRAITEMENT.NEXTVAL from dual");
+ if (rs.next()) {
+ this.id = rs.getLong(1);
+ } else {
+ this.id = 1;
+ }
+
+ final String sql = "insert into TRAITEMENT(TRT_ID,TRT_CLASS,TRT_DESC,TRT_CODE_GRAVITE) VALUES(?,?,?,?)";
+
+ logger.debug(sql + " --> with id : " + this.id);
+
+ final PreparedStatement ps = con.prepareStatement(sql);
+ ps.setLong(1, this.id);
+ ps.setString(2, this.className);
+ ps.setString(3, this.batchName);
+ ps.setString(4, this.severity);
+ ps.execute();
+ ps.close();
+ }
+ } catch (final Exception ex) {
+ logger.error("Traitement.createInDatabase()", ex);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ }
+ }
+
+ protected void updateEnd() {
+ Connection con = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ final PreparedStatement ps = con.prepareStatement("update TRAITEMENT set TRT_DATE_FIN=? where TRT_ID=?");
+ ps.setTimestamp(1, this.end);
+ ps.setLong(2, this.id);
+ ps.executeUpdate();
+ } catch (final Exception ex) {
+ logger.error("Traitement.updateEnd()", ex);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ }
+ }
+
+ protected void updateResume() {
+ Connection con = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ final PreparedStatement ps = con.prepareStatement("update TRAITEMENT set TRT_RESUME=? where TRT_ID=?");
+ ps.setString(1, this.resume);
+ ps.setLong(2, this.id);
+ ps.executeUpdate();
+ } catch (final Exception ex) {
+ logger.error("Traitement.updateResume()", ex);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ }
+ }
+
+ protected void updateSeverity() {
+ Connection con = null;
+ try {
+ con = PoolManager.getInstance().getConnection();
+ final PreparedStatement ps = con.prepareStatement("update TRAITEMENT set TRT_CODE_GRAVITE=? where TRT_ID=?");
+ ps.setString(1, this.severity);
+ ps.setLong(2, this.id);
+ ps.executeUpdate();
+ } catch (final Exception ex) {
+ logger.error("Traitement.updateSeverity()", ex);
+ } finally {
+ if (con != null) {
+ PoolManager.getInstance().releaseConnection(con);
+ }
+ }
+ }
+
+ void __updateSeverity(final String newSeverity) {
+ final int localRank = getSeverityRank(this.severity);
+ final int newRank = getSeverityRank(newSeverity);
+ if (newRank > localRank) {
+ this.setSeverity(newSeverity);
+ }
+ }
+
+ private static int getSeverityRank(final String sev) {
+ if (sev.equals(SEVERITY_INFO)) {
+ return 1;
+ } else if (sev.equals(SEVERITY_WARN)) {
+ return 2;
+ } else {
+ return 3;
+ }
+ }
}