KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > util > license > DurationLicense


1 package org.sapia.util.license;
2
3 import java.io.IOException JavaDoc;
4 import java.util.Calendar JavaDoc;
5 import java.util.Date JavaDoc;
6
7 /**
8  * This class implements a <code>License</code> that starts at a given date and lasts for
9  * a given number of days.
10  *
11  * @author Yanick Duchesne
12  *
13  * <dl>
14  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
15  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
16  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
17  * </dl>
18  */

19 public class DurationLicense implements License{
20   
21   static final long serialVersionUID = 1L;
22   
23   private Date JavaDoc _startDate;
24   private int _duration;
25   private Date JavaDoc _endDate;
26   private boolean _activated;
27   
28   public DurationLicense(int durationDays){
29     if(durationDays < 1){
30       throw new IllegalArgumentException JavaDoc("Duration should be equal to or greater than 1");
31     }
32     _duration = durationDays;
33
34   }
35   
36   /**
37    * @return the <code>Date</code> at which this license "starts".
38    */

39   public Date JavaDoc getStartDate(){
40     return _startDate;
41   }
42   
43   /**
44    * @return the number of days for which this license should last.
45    */

46   public int getDurationDays(){
47     return _duration;
48   }
49   
50   /**
51    * Returns the date at which this license terminates.
52    * @return a <code>Date</code>.
53    */

54   public Date JavaDoc getEndDate(){
55     return _endDate;
56   }
57   
58   /**
59    * @see org.sapia.util.license.License#isValid(java.lang.Object)
60    */

61   public boolean isValid(Object JavaDoc context) {
62     Date JavaDoc currentDate = new Date JavaDoc();
63     if(_startDate == null){
64       activate(null);
65     }
66     return currentDate.before(_endDate);
67   }
68   
69   /**
70    * @see org.sapia.util.license.License#activate(java.lang.Object)
71    */

72   public void activate(Object JavaDoc context) {
73     if(!_activated){
74       Date JavaDoc currentDate = new Date JavaDoc();
75       init(currentDate);
76       _activated = true;
77     }
78   }
79   
80   /**
81    * Returns if this license is valid or not, taking the given date as
82    * a base of calculation (validity is computed in the following way:
83    * currentDate < creation date + duration).
84    *
85    * @param fromDate
86    * @return <code>true</code> if this license is valid.
87    */

88   public boolean isValid(Date JavaDoc currentDate){
89     if(_startDate == null){
90       activate(null);
91     }
92     return currentDate.before(_endDate);
93   }
94   
95   private Date JavaDoc computeEndDate(){
96     return computeEndDate(_startDate, _duration);
97   }
98   
99   private synchronized void init(Date JavaDoc currentDate){
100     _startDate = new Date JavaDoc();
101     _endDate = computeEndDate(_startDate, _duration);
102   }
103   
104   static Date JavaDoc computeEndDate(Date JavaDoc from, int durationDays){
105     Calendar JavaDoc cal = Calendar.getInstance();
106     cal.setTime(from);
107     int i = 1;
108     int day = cal.get(Calendar.DAY_OF_YEAR);
109     for(; i <= durationDays; i++, day++){
110       if(day > cal.getActualMaximum(Calendar.DAY_OF_YEAR)){
111         cal.roll(Calendar.YEAR, true);
112         day = 1;
113       }
114       
115       cal.roll(Calendar.DAY_OF_YEAR, true);
116     }
117     return cal.getTime();
118   }
119   
120   /**
121    * @see org.sapia.util.license.License#getBytes()
122    */

123   public byte[] getBytes() throws IOException JavaDoc {
124     return Integer.toString(_duration).getBytes();
125   }
126   
127   
128 }
129
Popular Tags