KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > nava > informa > utils > RSS100Settings


1 //
2
// Informa -- RSS Library for Java
3
// Copyright (c) 2002 by Niko Schmuck
4
//
5
// Niko Schmuck
6
// http://sourceforge.net/projects/informa
7
// mailto:niko_schmuck@users.sourceforge.net
8
//
9
// This library is free software.
10
//
11
// You may redistribute it and/or modify it under the terms of the GNU
12
// Lesser General Public License as published by the Free Software Foundation.
13
//
14
// Version 2.1 of the license should be included with this distribution in
15
// the file LICENSE. If the license is not included with this distribution,
16
// you may find a copy at the FSF web site at 'www.gnu.org' or 'www.fsf.org',
17
// or you may write to the Free Software Foundation, 675 Mass Ave, Cambridge,
18
// MA 02139 USA.
19
//
20
// This library is distributed in the hope that it will be useful,
21
// but WITHOUT ANY WARRANTY; without even the implied waranty of
22
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23
// Lesser General Public License for more details.
24

25
26 // $Id: RSS100Settings.java,v 1.3 2004/06/28 19:33:44 niko_schmuck Exp $
27

28 package de.nava.informa.utils;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 import de.nava.informa.core.ChannelIF;
34
35 /**
36  * @author Jean-Guy Avelin
37  */

38 public class RSS100Settings implements CacheSettingsIF {
39
40   private static Log logger = LogFactory.getLog(RSS100Settings.class);
41
42   private long defaultTtl = DEFAULT_TTL;
43
44   public void setDefaultTtl(long defaultTtl) {
45     this.defaultTtl = defaultTtl;
46   }
47
48   /**
49    * Returns the ttl (in order of preference) feed producer ttl (if exists)
50    * wantedTtl (if exists) defaultTtl (if exists).
51    */

52   public long getTtl(ChannelIF channel, long ttlInMs) {
53     String JavaDoc updatePeriod = channel.getUpdatePeriod();
54     int updateFrequency = channel.getUpdateFrequency();
55
56     if (updatePeriod == null) {
57       if (ttlInMs > MINIMAL_TTL)
58         return ttlInMs;
59       return this.defaultTtl;
60     }
61
62     if (updateFrequency < 0) {
63       updateFrequency = 1;
64     }
65
66     long msInPeriod;
67
68     if (updatePeriod.equals(ChannelIF.UPDATE_HOURLY)) {
69       msInPeriod = MILLISECONDS_IN_HOUR;
70     } else if (updatePeriod.equals(ChannelIF.UPDATE_DAILY)) {
71       msInPeriod = MILLISECONDS_IN_DAY;
72     } else if (updatePeriod.equals(ChannelIF.UPDATE_MONTHLY)) {
73       msInPeriod = MILLISECONDS_IN_MONTH;
74     } else if (updatePeriod.equals(ChannelIF.UPDATE_YEARLY)) {
75       msInPeriod = MILLISECONDS_IN_YEAR;
76     } else {
77       throw new IllegalArgumentException JavaDoc("updatePeriod " + updatePeriod +
78                                          " is not valid");
79     }
80
81     long timeToExpire = msInPeriod / updateFrequency;
82
83     if (ttlInMs > timeToExpire) {
84       return ttlInMs;
85     }
86     return timeToExpire;
87   }
88
89 }
Popular Tags