package info.datacrush.splfhsekeep; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.Reader; import java.io.StringReader; import java.text.DateFormat; import java.util.Date; import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Vector; import org.jdom.Document; import org.jdom.Element; import org.jdom.input.SAXBuilder; import com.ibm.as400.access.AS400; import com.ibm.as400.access.SpooledFile; import com.ibm.as400.access.SpooledFileList; public class spooledFiles { private static String configPath; private Vector vec; private String user; private String host; private String pwd; private long today; /** * Constructor to setup spooled files housekeeping. * @param configPath The configuration (XML) file path. */ public spooledFiles(String configPath) { vec = new Vector(); File f = new File(configPath); int len = Integer.parseInt(new String(Long.toString(f.length()))); char[] in = new char[len]; try { BufferedReader br = new BufferedReader(new FileReader(configPath)); int rc = br.read(in, 0, len); br.close(); String xml = String.valueOf(in, 0, rc); Reader xr = new StringReader(xml); SAXBuilder sax = new SAXBuilder(false); Document doc = sax.build(xr); Element eRoot = doc.getRootElement(); host = eRoot.getChildText("host"); user = eRoot.getChildText("user"); pwd = eRoot.getChildText("pwd"); Element eFilter = eRoot.getChild("filter"); if (eFilter != null) { List lOutq = eFilter.getChildren("attributes"); Iterator iter = lOutq.iterator(); while (iter.hasNext()) { Element eOutq = (Element)iter.next(); vec.add(new filter( eOutq.getChildText("outq"), eOutq.getChildText("user"), Integer.parseInt(eOutq.getChildText("retain")), eOutq.getChildText("userdata"), eOutq.getChildText("startdate"), eOutq.getChildText("enddate"))); } } today = new Date().getTime(); } catch (Exception exc) { exc.printStackTrace(); } } /** * Executes spooled files housekeeping operation. * @throws Exception */ public void execute() throws Exception { AS400 sys = null; if (((user != null) && (user.length() > 0)) && ((pwd != null) && (user.length() > 0))) sys = new AS400(host, user, pwd); else sys = new AS400(host); while (vec.size() > 0) { filter scope = (filter)vec.remove(0); SpooledFileList spl = new SpooledFileList(sys); if (scope.getOutq().length() > 0) spl.setQueueFilter(scope.getOutq()); if (scope.getUser().length() > 0) spl.setUserFilter(scope.getUser()); if (scope.getUserData().length() > 0) spl.setUserDataFilter(scope.getUserData()); if (scope.getStartDate().length() > 0) { spl.setStartDateFilter(scope.getStartDate()); if (!scope.getStartDate().equalsIgnoreCase("*FIRST")) spl.setStartTimeFilter("000000"); } if (scope.getEndDate().length() > 0) { spl.setEndDateFilter(scope.getEndDate()); if (!scope.getEndDate().equalsIgnoreCase("*END")) spl.setEndTimeFilter("235959"); } int deleted = 0; spl.openSynchronously(); Enumeration en = spl.getObjects(); while (en.hasMoreElements()) { SpooledFile sf = (SpooledFile)en.nextElement(); String dat = sf.getCreateDate(); StringBuffer sb = new StringBuffer(); sb.append(dat.substring(3, 5)); String prc = dat.substring(3, 5).concat("/").concat(dat.substring(5, 7)).concat("/").concat(dat.substring(1, 3)); long current = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US).parse(prc).getTime(); long dateDelta = today - current; long dateDays = (int)(dateDelta / (24 * 60 * 60 * 1000)); if (dateDays > scope.getRetain()){ sf.delete(); deleted++; } } System.out.println(deleted + " deleted from " + scope.getOutq()); } } /** * Main execution call routine. * @param args Configuration file path. */ public static void main(String[] args) { if (args.length >= 1) { configPath = args[0]; } else { configPath = "config.xml"; } spooledFiles spf = new spooledFiles(configPath); try { spf.execute(); } catch (Exception exc) { exc.printStackTrace(); } System.exit(0); } protected class filter { private String outq; private String user; private int retain; private String userData; private String startDate; private String endDate; /** * Constructs a new filter object. * @param outq Output Queue (OUTQ) object. * @param user User. * @param retain Number of days to retain spooled files. * @param userData User data. * @param startDate Start date. * @param endDate End date. */ public filter(String outq, String user, int retain, String userData, String startDate, String endDate) { this.outq = outq; this.user = user; this.retain = retain; if (userData == null) { this.userData = new String(""); } else { this.userData = userData; } if (startDate == null) { this.startDate = new String(""); } else { this.startDate = startDate; } if (endDate == null) { this.endDate = new String(""); } else { this.endDate = endDate; } } /** * Get output queue. * @return */ public String getOutq() { return outq; } /** * Get user. * @return */ public String getUser() { return user; } /** * Get retention days. * @return */ public int getRetain() { return retain; } /** * Get user data. * @return */ public String getUserData() { return userData; } /** * Get start date. * @return */ public String getStartDate() { return startDate; } /** * Get end date. * @return */ public String getEndDate() { return endDate; } } }