001    package railo.runtime.spooler;
002    
003    import railo.commons.lang.ExceptionUtil;
004    import railo.runtime.config.Config;
005    import railo.runtime.exp.PageException;
006    import railo.runtime.op.Caster;
007    import railo.runtime.type.Array;
008    import railo.runtime.type.ArrayImpl;
009    import railo.runtime.type.Struct;
010    import railo.runtime.type.StructImpl;
011    
012    
013    
014    public abstract class SpoolerTaskSupport implements SpoolerTask {
015    
016            private long creation;
017            private long lastExecution;
018            private int tries=0;
019            private long nextExecution;
020            private Array exceptions=new ArrayImpl();
021            private boolean closed;
022            private String id;
023            private ExecutionPlan[] plans;
024            
025            /**
026             * Constructor of the class
027             * @param plans
028             * @param timeOffset offset from the local time to the config time
029             */
030            public SpoolerTaskSupport(ExecutionPlan[] plans) {
031                    this.plans=plans;
032                    creation=System.currentTimeMillis();
033            }
034    
035            
036            public final String getId() {
037                    return id;
038            }
039            public final void setId(String id) {
040                    this.id= id;
041            }
042    
043            /**
044             * return last execution of this task
045             * @return last execution
046             */
047            public final long lastExecution() {
048                    return lastExecution;
049            }
050    
051            public final void setNextExecution(long nextExecution) {
052                    this.nextExecution=nextExecution;
053            }
054    
055            public final long nextExecution() {
056                    return nextExecution;
057            }
058    
059            /**
060             * returns how many tries to send are already done
061             * @return tries
062             */
063            public final int tries() {
064                    return tries;
065            }
066    
067            final void _execute(Config config) throws PageException {
068    
069                    lastExecution=System.currentTimeMillis();
070                    tries++;
071                    try {
072                            execute(config);
073                    }
074                    catch(Throwable t) {
075                            PageException pe = Caster.toPageException(t);
076                            String st = ExceptionUtil.getStacktrace(t,true);
077                            //config.getErrWriter().write(st+"\n");
078                            
079                            Struct sct=new StructImpl();
080                            sct.setEL("message", pe.getMessage());
081                            sct.setEL("detail", pe.getDetail());
082                            sct.setEL("stacktrace", st);
083                            sct.setEL("time", Caster.toLong(System.currentTimeMillis()));
084                            exceptions.appendEL(sct);
085                            
086                            throw pe;
087                    }
088                    finally {
089                            lastExecution=System.currentTimeMillis();
090                    }
091            }
092    
093            /**
094             * @return the exceptions
095             */
096            public final Array getExceptions() {
097                    return exceptions;
098            }
099    
100            public final void setClosed(boolean closed) {
101                    this.closed=closed;
102            }
103            
104            public final boolean closed() {
105                    return closed;
106            }
107    
108    
109            /**
110             * @return the plans
111             */
112            public ExecutionPlan[] getPlans() {
113                    return plans;
114            }
115    
116    
117            /**
118             * @return the creation
119             */
120            public long getCreation() {
121                    return creation;
122            }
123    
124    
125            public void setLastExecution(long lastExecution) {
126                    this.lastExecution=lastExecution;
127            }
128    
129    }