001    package railo.runtime.schedule;
002    
003    import java.io.IOException;
004    import java.net.MalformedURLException;
005    import java.net.URL;
006    
007    import railo.commons.io.res.Resource;
008    import railo.commons.lang.Md5;
009    import railo.commons.net.HTTPUtil;
010    import railo.commons.security.Credentials;
011    import railo.runtime.net.proxy.ProxyData;
012    import railo.runtime.op.Caster;
013    import railo.runtime.type.dt.Date;
014    import railo.runtime.type.dt.Time;
015    
016    /**
017     * Define a single schedule Task
018     */
019    public final class ScheduleTaskImpl implements ScheduleTask {
020        
021            public static int INTERVAL_EVEREY=-1;
022        private String task;
023        private short operation = OPERATION_HTTP_REQUEST;
024        private Resource file;
025        private Date startDate;
026        private Time startTime;
027        private URL url;
028        private Date endDate;
029        private Time endTime;
030        private int interval;
031        private long timeout;
032        private Credentials credentials;
033        private ProxyData proxy;
034        private boolean resolveURL;
035    
036        private long nextExecution;
037    
038        private String strInterval;
039    
040        private boolean publish;
041        private boolean valid=true;
042            private boolean hidden;
043            private boolean readonly;
044            private boolean paused;
045            private boolean autoDelete;
046            private String md5;
047    
048        
049        
050        
051        /**
052         * constructor of the class
053         * @param task Task name
054         * @param file Output File
055         * @param startDate Start Date
056         * @param startTime Start  Time
057         * @param endDate
058         * @param endTime
059         * @param url URL to invoke
060         * @param port Port of the URL to invoke
061         * @param interval interval of the job
062         * @param timeout request timeout in miilisconds
063         * @param credentials username and password for the request
064         * @param proxyHost
065         * @param proxyPort
066         * @param proxyCredentials proxy username and password
067         * @param resolveURL resolve links in the output page to absolute references or not
068         * @param publish
069         * @throws IOException
070         * @throws ScheduleException
071         */
072        public ScheduleTaskImpl(String task, Resource file, Date startDate, Time startTime, 
073                Date endDate, Time endTime, String url, int port, String interval,
074                long timeout, Credentials credentials, ProxyData proxy, boolean resolveURL, boolean publish,boolean hidden, 
075                boolean readonly,boolean paused, boolean autoDelete) throws IOException, ScheduleException {
076            
077            
078            String md5=task.toLowerCase()+file+startDate+startTime+endDate+endTime+url+port+interval+timeout+
079            credentials+proxy+resolveURL+publish+hidden+readonly+paused;
080            md5=Md5.getDigestAsString(md5);
081            this.md5=md5;
082            
083            if(file!=null && file.toString().trim().length()>0) {
084                    Resource parent = file.getParentResource();
085                    if(parent==null || !parent.exists())
086                        throw new IOException("Directory for output file ["+file+"] doesn't exist");
087                    if(file.exists() && !file.isFile())
088                        throw new IOException("output file ["+file+"] is not a file");
089            }
090            if(timeout<1) {
091                throw new ScheduleException("value timeout must be greater than 0");
092            }
093            if(startDate==null) throw new ScheduleException("start date is required");
094            if(startTime==null)throw new ScheduleException("start time is required");
095            //if(endTime==null)endTime=new Time(23,59,59,999);
096    
097            this.task=task.trim();
098            this.file=file;
099            this.startDate=startDate;
100            this.startTime=startTime;
101            this.endDate=endDate;
102            this.endTime=endTime;
103            this.url=toURL(url,port);
104            this.interval=toInterval(interval);
105            this.strInterval=interval;
106            this.timeout=timeout;
107            this.credentials=credentials;
108            this.proxy=proxy;
109            this.resolveURL=resolveURL;
110            this.publish=publish;
111            this.hidden=hidden;
112            this.readonly=readonly;
113            this.paused=paused;
114            this.autoDelete=autoDelete;
115        }
116    
117    
118    
119        /**
120         * translate a String interval definition to a int definition
121         * @param interval
122         * @return interval
123         * @throws ScheduleException
124         */
125        private static int toInterval(String interval) throws ScheduleException {
126            interval=interval.trim().toLowerCase();
127            int i=Caster.toIntValue(interval,0);
128            if(i==0) {
129                interval=interval.trim();
130                if(interval.equals("once")) return INTERVAL_ONCE;
131                else if(interval.equals("daily")) return INTERVAL_DAY;
132                else if(interval.equals("day")) return INTERVAL_DAY;
133                else if(interval.equals("monthly")) return INTERVAL_MONTH;
134                else if(interval.equals("month")) return INTERVAL_MONTH;
135                else if(interval.equals("weekly")) return INTERVAL_WEEK;
136                else if(interval.equals("week")) return INTERVAL_WEEK;
137                throw new ScheduleException("invalid interval definition ["+interval+"], valid values are [once,daily,monthly,weekly or number]");
138            }
139            if(i<10) {
140                throw new ScheduleException("interval must be at least 10");
141            }
142            return i;
143        }
144    
145        /**
146         * translate a urlString and a port definition to a URL Object
147         * @param url URL String 
148         * @param port URL Port Definition
149         * @return returns a URL Object
150         * @throws MalformedURLException
151         */
152        private static URL toURL(String url, int port) throws MalformedURLException {
153            URL u = HTTPUtil.toURL(url);
154            if(port==-1) return u;
155            return new URL(u.getProtocol(), u.getHost(), port, u.getFile());
156        }    
157    
158        @Override
159        public Credentials getCredentials() {       return credentials;     }
160    
161        @Override
162        public boolean hasCredentials() {   return credentials!=null;       }
163        
164        @Override
165        public Resource getResource() {     
166            return file;
167        }
168        
169        @Override
170        public int getInterval() {  return interval;        }
171        
172        @Override
173        public short getOperation() {       return operation;       }
174        
175        @Override
176        public ProxyData getProxyData() {   return proxy;   }
177        
178        @Override
179        public boolean isResolveURL() {     return resolveURL;      }
180        
181        @Override
182        public String getTask() {   return task;    }
183        
184        @Override
185        public long getTimeout() {  return timeout; }
186        
187        @Override
188        public URL getUrl() {       
189            return url;     
190        }
191    
192        @Override
193        public void setNextExecution(long nextExecution) {  this.nextExecution=nextExecution;       }
194        
195        @Override
196        public long getNextExecution() {    return nextExecution;   }
197        
198        @Override
199        public Date getEndDate() {  return endDate; }
200        
201        @Override
202        public Date getStartDate() {        return startDate;       }
203        
204        @Override
205        public Time getEndTime() {  return endTime; }
206        
207        @Override
208        public Time getStartTime() {        return startTime;       }
209    
210        @Override
211        public String getIntervalAsString() {       return strInterval;     }
212        
213        @Override
214        public String getStringInterval() { return strInterval;     }
215        @Override
216        public boolean isPublish() {
217            return publish;
218        }
219        @Override
220        public boolean isValid() {
221            return valid;
222        }
223        @Override
224        public void setValid(boolean valid) {
225            this.valid = valid;
226        }
227    
228    
229    
230            /**
231             * @return the hidden
232             */
233            public boolean isHidden() {
234                    return hidden;
235            }
236    
237    
238    
239            /** 
240             * @param hidden the hidden to set
241             */
242            public void setHidden(boolean hidden) {
243                    this.hidden = hidden;
244            }
245    
246    
247    
248            /**
249             * @return the readonly
250             */
251            public boolean isReadonly() {
252                    return readonly;
253            }
254    
255    
256    
257            /**
258             * @param readonly the readonly to set
259             */
260            public void setReadonly(boolean readonly) {
261                    this.readonly = readonly;
262            }
263    
264    
265    
266            @Override
267            public boolean isPaused() {
268                    return paused;
269            }
270    
271    
272    
273            public void setPaused(boolean paused) {
274                    this.paused=paused;
275            }
276            
277    
278            public boolean isAutoDelete() {
279                    return autoDelete;
280            }
281    
282    
283    
284            public void setAutoDelete(boolean autoDelete) {
285                    this.autoDelete=autoDelete;
286            }
287    
288    
289    
290            public String md5() {
291                    return md5;
292            }
293    }