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